/* ---------------------------------------------------------------------------
   Library.

   Modern first, old iPads degrade gracefully. The rule followed throughout:
   layout is built with flexbox, which iOS 10 Safari handles correctly, and
   then upgraded inside @supports (display:grid) for anything newer. An iPad 4
   gets a slightly less even grid and no dark mode; it does not get a broken
   page, and nothing is hidden from it.

   Custom properties are safe here -- iOS 9.3 and later understand them.
   aspect-ratio is not, hence the padding-top box on cover thumbnails.
--------------------------------------------------------------------------- */

:root {
  --bg:        #faf9f7;
  --surface:   #ffffff;
  --text:      #1b1a17;
  --muted:     #6d6b64;
  --line:      #e3e0d9;
  --accent:    #8a5a2b;
  --accent-in: #ffffff;
  --shadow:    0 1px 2px rgba(28, 25, 20, .06), 0 6px 16px rgba(28, 25, 20, .07);
  --radius:    10px;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg:        #16150f;
    --surface:   #201e18;
    --text:      #ece8df;
    --muted:     #a09b8e;
    --line:      #322f26;
    --accent:    #d9a441;
    --accent-in: #1b1a17;
    --shadow:    0 1px 2px rgba(0, 0, 0, .4), 0 8px 22px rgba(0, 0, 0, .35);
  }
}

* { box-sizing: border-box; }

html { -webkit-text-size-adjust: 100%; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 16px/1.5 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto,
        "Helvetica Neue", Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
}

a { color: inherit; }

/* --- top bar --------------------------------------------------------- */

.topbar {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 12px;
  padding: 14px 20px;
  background: var(--surface);
  border-bottom: 1px solid var(--line);
  position: sticky;
  top: 0;
  z-index: 10;
}

.brand {
  font-weight: 650;
  font-size: 17px;
  letter-spacing: -.01em;
  text-decoration: none;
  margin-right: auto;
}

.search { display: flex; gap: 6px; }

.search input[type=search] {
  -webkit-appearance: none;
  appearance: none;
  width: 15em;
  max-width: 46vw;
  padding: 8px 12px;
  font-size: 15px;
  color: var(--text);
  background: var(--bg);
  border: 1px solid var(--line);
  border-radius: 999px;
}
.search input[type=search]:focus {
  outline: 2px solid var(--accent);
  outline-offset: 1px;
}

.search button {
  padding: 8px 14px;
  font-size: 14px;
  color: var(--accent-in);
  background: var(--accent);
  border: 0;
  border-radius: 999px;
  cursor: pointer;
}

.signout { margin-left: 4px; }

.linkish {
  background: none;
  border: 0;
  padding: 6px 2px;
  font: inherit;
  font-size: 13px;
  color: var(--muted);
  cursor: pointer;
}
.linkish:hover { color: var(--text); }

/* --- page ------------------------------------------------------------ */

.wrap {
  max-width: 1180px;
  margin: 0 auto;
  padding: 22px 20px 40px;
}

.bar {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 14px;
  margin-bottom: 20px;
}

.kinds {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  margin-right: auto;
}

.kinds a {
  display: inline-block;
  padding: 6px 13px;
  font-size: 14px;
  text-decoration: none;
  color: var(--muted);
  border: 1px solid var(--line);
  border-radius: 999px;
}
.kinds a.on {
  color: var(--accent-in);
  background: var(--accent);
  border-color: var(--accent);
}
.kinds .qty { opacity: .6; font-size: 12px; }

.sort { font-size: 13px; color: var(--muted); }
.sort select {
  font: inherit;
  font-size: 14px;
  color: var(--text);
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 6px 8px;
  margin-left: 4px;
}

.result-note { font-size: 14px; color: var(--muted); margin: -6px 0 18px; }
.empty { color: var(--muted); padding: 50px 0; text-align: center; }

/* --- the grid -------------------------------------------------------- */
/* Flexbox first so iOS 10 gets a real grid, then upgraded below. */

.grid {
  list-style: none;
  margin: 0;
  padding: 0;
  display: flex;
  flex-wrap: wrap;
  margin-left: -10px;
  margin-right: -10px;
}

.card {
  width: 50%;
  padding: 0 10px 26px;
}

@media (min-width: 520px)  { .card { width: 33.333%; } }
@media (min-width: 760px)  { .card { width: 25%; } }
@media (min-width: 1000px) { .card { width: 20%; } }

@supports (display: grid) {
  .grid {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(150px, 1fr));
    gap: 26px 20px;
    margin: 0;
  }
  .card { width: auto; padding: 0; }
}

.card a {
  display: block;
  text-decoration: none;
  color: inherit;
}

.thumb {
  display: block;
  position: relative;
  padding-top: 145%;              /* stands in for aspect-ratio on old Safari */
  margin-bottom: 9px;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  overflow: hidden;
  box-shadow: var(--shadow);
}

.thumb img {
  position: absolute;
  top: 0; left: 0;
  width: 100%;
  height: 100%;
  object-fit: cover;
  display: block;
}

.card a:hover .thumb { border-color: var(--accent); }

.nocover {
  position: absolute;
  top: 0; left: 0;
  width: 100%;
  height: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 34px;
  font-weight: 600;
  color: var(--muted);
  background: var(--surface);
}
.nocover.big { position: static; height: 260px; border-radius: var(--radius); }

.card .t {
  display: block;
  font-size: 14px;
  line-height: 1.35;
  font-weight: 550;
}
.card .a {
  display: block;
  font-size: 12.5px;
  color: var(--muted);
  margin-top: 2px;
}

/* --- shelf sections & collections ----------------------------------- */

.shelf-h {
  font-size: 12px;
  text-transform: uppercase;
  letter-spacing: .08em;
  color: var(--muted);
  font-weight: 650;
  margin: 4px 0 14px;
}
.grid + .shelf-h { margin-top: 34px; }

/* A collection card: framed so it reads as a stack of things rather than one
   thing, with the member count pinned to the cover. Drawn entirely with
   box-shadow + one badge, so nothing extra lands in the DOM and iOS 10 Safari
   renders it the same as anything modern. */
.card-collection .thumb {
  box-shadow:
    var(--shadow),
    5px 6px 0 -1px var(--bg),
    5px 6px 0 0 var(--line);
}
.card-collection .thumb .count {
  position: absolute;
  right: 8px;
  top: 8px;
  z-index: 2;
  padding: 3px 9px;
  border-radius: 999px;
  background: var(--accent);
  color: var(--accent-in);
  font-size: 12px;
  font-weight: 650;
  box-shadow: var(--shadow);
}

/* --- detail ---------------------------------------------------------- */

.crumb { font-size: 14px; margin: 0 0 18px; }
.crumb a { color: var(--muted); text-decoration: none; }
.crumb a:hover { color: var(--text); }
.crumb .sep { color: var(--line); margin: 0 7px; }

.detail { display: flex; flex-wrap: wrap; gap: 30px; }

.detail-cover { width: 230px; flex: 0 0 auto; }
.detail-cover img {
  width: 100%;
  border-radius: var(--radius);
  border: 1px solid var(--line);
  box-shadow: var(--shadow);
  display: block;
}

.detail-body { flex: 1 1 320px; min-width: 0; }
.detail-body h1 {
  margin: 0 0 6px;
  font-size: 26px;
  line-height: 1.2;
  letter-spacing: -.015em;
}
.byline { margin: 0 0 12px; font-size: 16px; color: var(--muted); }
.meta   { margin: 0 0 14px; font-size: 13.5px; color: var(--muted); }
.hint   { margin: 18px 0 0; font-size: 13.5px; color: var(--muted); }

.progress {
  height: 6px;
  background: var(--line);
  border-radius: 999px;
  overflow: hidden;
  margin: 4px 0 8px;
  max-width: 320px;
}
.progress span { display: block; height: 100%; background: var(--accent); }

.actions { display: flex; flex-wrap: wrap; gap: 10px; margin-top: 18px; }

.btn {
  display: inline-block;
  padding: 10px 18px;
  font-size: 15px;
  text-decoration: none;
  color: var(--text);
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 999px;
  cursor: pointer;
}
.btn:hover { border-color: var(--accent); }
.btn.primary {
  color: var(--accent-in);
  background: var(--accent);
  border-color: var(--accent);
}
.btn.wide { width: 100%; text-align: center; }
.btn .sz { opacity: .65; font-size: 12.5px; margin-left: 6px; }

/* --- login ----------------------------------------------------------- */

.login {
  max-width: 340px;
  margin: 8vh auto 0;
  text-align: center;
}
.login h1 { font-size: 22px; margin: 0 0 22px; letter-spacing: -.01em; }
.login label {
  display: block;
  text-align: left;
  font-size: 13px;
  color: var(--muted);
  margin-bottom: 14px;
}
.login input[type=text],
.login input[type=password] {
  -webkit-appearance: none;
  appearance: none;
  display: block;
  width: 100%;
  margin-top: 5px;
  padding: 11px 13px;
  font-size: 16px;            /* 16px stops iOS zooming the page on focus */
  color: var(--text);
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 9px;
}
.login .check {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 13.5px;
  margin: 4px 0 20px;
}
.login .check input { margin: 0; }

.error {
  font-size: 14px;
  color: #b3261e;
  background: rgba(179, 38, 30, .08);
  border-radius: 8px;
  padding: 9px 12px;
  margin: 0 0 16px;
}
@media (prefers-color-scheme: dark) {
  .error { color: #f2b8b5; background: rgba(242, 184, 181, .1); }
}

/* --- pager / footer -------------------------------------------------- */

.pager {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 18px;
  margin-top: 28px;
  font-size: 14px;
}
.pager span { color: var(--muted); }

.foot {
  text-align: center;
  font-size: 12.5px;
  color: var(--muted);
  padding: 0 20px 30px;
}

/* --- instant search results ------------------------------------------ */

.suggest {
  position: absolute;
  z-index: 20;
  margin-top: 4px;
  min-width: 15em;
  max-width: 90vw;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 10px;
  box-shadow: var(--shadow);
  overflow: hidden;
}
.suggest a {
  display: block;
  padding: 9px 13px;
  font-size: 14px;
  text-decoration: none;
  border-bottom: 1px solid var(--line);
}
.suggest a:last-child { border-bottom: 0; }
.suggest a:hover, .suggest a.on { background: var(--bg); }
.suggest .a { display: block; font-size: 12px; color: var(--muted); }

/* ---------------------------------------------------------------------------
   Admin.

   The one part of this stylesheet that does NOT bend over backwards for iOS 10.
   Everything above degrades gracefully because it runs on an iPad 4; the
   management screens are used from the Mac, so grid, gap and aspect-ratio are
   fair game here and there is no @supports ladder.

   Same custom properties as the reader throughout, so it still reads as one
   application rather than a bolted-on control panel.
--------------------------------------------------------------------------- */

:root {
  --danger:    #b3261e;
  --danger-bg: rgba(179, 38, 30, .08);
  --good:      #2e6b3e;
}
@media (prefers-color-scheme: dark) {
  :root {
    --danger:    #f2b8b5;
    --danger-bg: rgba(242, 184, 181, .12);
    --good:      #8fd3a3;
  }
}

.admin-nav {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  margin: 0 0 22px;
}
.admin-nav a {
  padding: 6px 13px;
  font-size: 14px;
  text-decoration: none;
  color: var(--muted);
  border: 1px solid var(--line);
  border-radius: 999px;
}
.admin-nav a.on {
  color: var(--accent-in);
  background: var(--accent);
  border-color: var(--accent);
}

.admin-h1 {
  font-size: 22px;
  letter-spacing: -.015em;
  margin: 0 0 18px;
}
.admin-h1 .qty {
  font-size: 15px;
  font-weight: 400;
  color: var(--muted);
  margin-left: 4px;
}

.admin-lede {
  max-width: 62ch;
  font-size: 14px;
  color: var(--muted);
  margin: -6px 0 22px;
}

.notice {
  font-size: 14px;
  color: var(--good);
  background: color-mix(in srgb, var(--good) 10%, transparent);
  border-radius: 8px;
  padding: 9px 12px;
  margin: 0 0 18px;
}

/* .error is shared with the login form, which uses it as a single line. In here
   it wraps a list of validation messages. */
.error p { margin: 0; }
.error p + p { margin-top: 4px; }

/* --- dashboard ------------------------------------------------------------ */

.stats {
  list-style: none;
  margin: 0 0 26px;
  padding: 0;
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(120px, 1fr));
  gap: 12px;
}
.stats li {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 14px 16px;
}
.stats .n {
  display: block;
  font-size: 22px;
  font-weight: 650;
  letter-spacing: -.02em;
}
.stats .l {
  display: block;
  font-size: 12.5px;
  color: var(--muted);
  margin-top: 2px;
}

.panel {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 18px 20px;
  margin-bottom: 20px;
}
.panel h2 {
  font-size: 15px;
  font-weight: 650;
  margin: 0 0 12px;
}
.panel > .meta:last-child { margin-bottom: 0; }

.orphan-list {
  list-style: none;
  margin: 12px 0;
  padding: 0;
  font-size: 14px;
}
.orphan-list li {
  padding: 5px 0;
  border-bottom: 1px solid var(--line);
}
.orphan-list li:last-child { border-bottom: 0; }
.orphan-list .meta { margin: 0 0 0 8px; display: inline; }

.bars { list-style: none; margin: 0; padding: 0; }
.bars li {
  display: grid;
  grid-template-columns: minmax(9em, 16em) 1fr auto auto;
  align-items: center;
  gap: 12px;
  padding: 7px 0;
  font-size: 13.5px;
}
.bars .bar-l { color: var(--text); }
.bars .progress { margin: 0; max-width: none; }
.bars .bar-n { color: var(--muted); font-variant-numeric: tabular-nums; }
.bars .bar-a { font-size: 12.5px; color: var(--muted); }
.bars .bar-a:hover { color: var(--accent); }

@media (max-width: 620px) {
  .bars li { grid-template-columns: 1fr auto; }
  .bars .progress { grid-column: 1 / -1; }
}

.taglist {
  list-style: none;
  margin: 0 0 14px;
  padding: 0;
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
}
.taglist a {
  display: inline-block;
  padding: 5px 11px;
  font-size: 13px;
  text-decoration: none;
  color: var(--muted);
  border: 1px solid var(--line);
  border-radius: 999px;
}
.taglist a:hover { border-color: var(--accent); color: var(--text); }
.taglist .qty { opacity: .55; font-size: 11.5px; margin-left: 3px; }

.inline-form { display: inline-block; margin: 0; }

/* --- filters + table ------------------------------------------------------ */

.admin-filters {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 8px;
  margin-bottom: 18px;
}
.admin-filters input[type=search],
.admin-filters select {
  -webkit-appearance: none;
  appearance: none;
  font: inherit;
  font-size: 14px;
  color: var(--text);
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 8px 11px;
}
.admin-filters input[type=search] { flex: 1 1 18em; min-width: 12em; }
.admin-filters input[type=search]:focus,
.admin-filters select:focus {
  outline: 2px solid var(--accent);
  outline-offset: 1px;
}

/* A wide table must scroll inside its own box; the page itself never does. */
.table-scroll {
  overflow-x: auto;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: var(--surface);
}

.admin-table {
  width: 100%;
  border-collapse: collapse;
  font-size: 13.5px;
}
.admin-table th,
.admin-table td {
  text-align: left;
  padding: 9px 14px;
  border-bottom: 1px solid var(--line);
  white-space: nowrap;
}
.admin-table th {
  font-size: 11.5px;
  text-transform: uppercase;
  letter-spacing: .06em;
  color: var(--muted);
  font-weight: 650;
  position: sticky;
  top: 0;
  background: var(--surface);
}
.admin-table tbody tr:last-child td { border-bottom: 0; }
.admin-table tbody tr:hover { background: var(--bg); }
.admin-table .num { text-align: right; font-variant-numeric: tabular-nums; }
.admin-table .thin { color: var(--muted); }
.admin-table .t {
  white-space: normal;
  min-width: 18em;
  font-weight: 550;
}
.admin-table .t a { text-decoration: none; }
.admin-table .t a:hover { color: var(--accent); }

.badge {
  display: inline-block;
  margin-left: 6px;
  padding: 1px 7px;
  font-size: 11px;
  font-weight: 600;
  letter-spacing: .02em;
  color: var(--accent-in);
  background: var(--accent);
  border-radius: 999px;
  vertical-align: 1px;
}

/* --- the edit screen ------------------------------------------------------ */

.admin-edit {
  display: grid;
  grid-template-columns: minmax(0, 1fr) 280px;
  gap: 30px;
  align-items: start;
}
@media (max-width: 860px) {
  .admin-edit { grid-template-columns: 1fr; }
}

.admin-form label {
  display: block;
  font-size: 13px;
  color: var(--muted);
  margin-bottom: 18px;
}
.admin-form input[type=text],
.admin-form textarea {
  -webkit-appearance: none;
  appearance: none;
  display: block;
  width: 100%;
  margin-top: 5px;
  padding: 10px 12px;
  font: inherit;
  font-size: 15px;
  color: var(--text);
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 9px;
}
.admin-form textarea { resize: vertical; line-height: 1.5; }
.admin-form input[type=text]:focus,
.admin-form textarea:focus {
  outline: 2px solid var(--accent);
  outline-offset: 1px;
}

.field-note {
  display: block;
  font-size: 12px;
  color: var(--muted);
  margin-top: 5px;
}
.field-note code {
  font-size: 11.5px;
  background: var(--bg);
  border-radius: 4px;
  padding: 1px 5px;
}

.admin-form-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
  align-items: center;
}

.admin-aside h3 {
  font-size: 11.5px;
  text-transform: uppercase;
  letter-spacing: .07em;
  color: var(--muted);
  font-weight: 650;
  margin: 22px 0 8px;
}
.admin-aside h3:first-child { margin-top: 0; }

.admin-cover {
  display: block;
  width: 100%;
  max-width: 180px;
  border-radius: var(--radius);
  border: 1px solid var(--line);
  box-shadow: var(--shadow);
  margin-bottom: 20px;
}

/* The freeze state, said out loud. An item whose title no longer follows the
   manifest is otherwise indistinguishable from one that does, which turns a
   later "why didn't renaming the file work?" into a mystery. */
.admin-freeze {
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 13px 15px;
  margin-bottom: 4px;
}
.admin-freeze.on {
  border-color: var(--accent);
  background: color-mix(in srgb, var(--accent) 7%, transparent);
}
.admin-freeze h3 { margin: 0 0 6px; }
.admin-freeze .meta { font-size: 12.5px; margin-bottom: 10px; }
.admin-freeze form { margin: 0; }

.admin-facts {
  margin: 0;
  font-size: 13px;
  display: grid;
  grid-template-columns: auto 1fr;
  gap: 4px 12px;
}
.admin-facts dt { color: var(--muted); }
.admin-facts dd { margin: 0; }
.admin-facts code,
.admin-files code {
  font-size: 11.5px;
  background: var(--bg);
  border-radius: 4px;
  padding: 1px 5px;
}

.admin-files {
  list-style: none;
  margin: 0;
  padding: 0;
  font-size: 12.5px;
}
.admin-files li { padding: 4px 0; }
.admin-files .meta { display: inline; margin: 0 0 0 6px; }

/* Long paths and original filenames have no spaces to break on. */
.wrap-any { overflow-wrap: anywhere; word-break: break-word; }

/* --- import --------------------------------------------------------------- */

.import-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 26px;
}
.import-actions form { margin: 0; }
.import-actions .field-note { max-width: 22ch; }

.danger-form {
  display: flex;
  flex-wrap: wrap;
  align-items: flex-end;
  gap: 12px;
  margin: 16px 0 0;
  padding-top: 16px;
  border-top: 1px solid var(--line);
}
.danger-form label {
  display: block;
  font-size: 12.5px;
  color: var(--muted);
  margin: 0;
}
.danger-form input[type=text] {
  -webkit-appearance: none;
  appearance: none;
  display: block;
  margin-top: 5px;
  width: 11em;
  padding: 9px 11px;
  font: inherit;
  font-size: 14px;
  color: var(--text);
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 8px;
}
.danger-form code {
  font-size: 12px;
  background: var(--bg);
  border-radius: 4px;
  padding: 1px 5px;
}

.btn.small { padding: 7px 13px; font-size: 13.5px; }
.btn.danger {
  color: var(--danger);
  background: var(--danger-bg);
  border-color: color-mix(in srgb, var(--danger) 35%, transparent);
}
.btn.danger:hover { border-color: var(--danger); }

.log {
  margin: 0;
  padding: 14px 16px;
  max-height: 26em;
  overflow: auto;
  font-size: 12.5px;
  line-height: 1.55;
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  color: var(--text);
  background: var(--bg);
  border: 1px solid var(--line);
  border-radius: 9px;
  white-space: pre-wrap;
  overflow-wrap: anywhere;
}

/* --- the way in from the reader ------------------------------------------ */

.crumb-edit {
  float: right;
  font-size: 13px;
  color: var(--muted);
  text-decoration: none;
  border: 1px solid var(--line);
  border-radius: 999px;
  padding: 3px 12px;
}
.crumb-edit:hover { border-color: var(--accent); color: var(--text); }
