/* Nine Books — one shelf: header, 3×3 grid of 2:3 book frames, the "Most chosen" list below. */
:root {
  --paper: #eef0ec;
  --surface: #f9faf7;
  --ink: #1d2420;
  --muted: #5f6a63;
  --rule: #c9cfc8;
  --accent: #2f5d48;       /* bookcloth green */
  --accent-ink: #f4f7f2;
  --danger: #9b2f2a;
  --shadow: 0 1px 2px rgba(20,30,25,.12), 0 6px 18px rgba(20,30,25,.10);
  --display: "Young Serif", Georgia, "Times New Roman", serif;
  --body: "Instrument Sans", system-ui, -apple-system, "Segoe UI", sans-serif;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root {
    --paper: #161a18;
    --surface: #1f2421;
    --ink: #e7ebe6;
    --muted: #9aa59e;
    --rule: #39413c;
    --accent: #7fb79a;
    --accent-ink: #10201a;
    --danger: #e0857e;
    --shadow: 0 1px 2px rgba(0,0,0,.4), 0 8px 22px rgba(0,0,0,.35);
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }
html, body { margin: 0; }
body {
  background: var(--paper);
  color: var(--ink);
  font: 15px/1.5 var(--body);
  padding-block: clamp(24px, 5vw, 56px);
  padding-left: max(16px, env(safe-area-inset-left, 0px));
  padding-right: max(16px, env(safe-area-inset-right, 0px));
}
.wrap { max-width: 640px; margin: 0 auto; display: grid; gap: 28px; }
.wrap.narrow { max-width: 560px; }

.masthead { display: flex; align-items: end; justify-content: space-between; gap: 16px; flex-wrap: wrap; }
h1 { font: 400 clamp(30px, 6vw, 44px)/1.05 var(--display); margin: 0; letter-spacing: -.01em; text-wrap: balance; }
.brand { font: 400 26px/1.1 var(--display); color: var(--ink); text-decoration: none; }
.count { font-size: 12px; letter-spacing: .08em; text-transform: uppercase; color: var(--muted); font-variant-numeric: tabular-nums; margin-top: 6px; }

/* Buttons */
.btn {
  font: inherit; font-weight: 600; padding: 10px 18px; border-radius: 7px; border: 1px solid transparent;
  background: var(--accent); color: var(--accent-ink); cursor: pointer; text-decoration: none; display: inline-block; text-align: center;
}
.btn:hover:not(:disabled) { filter: brightness(1.08); }
.btn:disabled { background: var(--rule); color: var(--muted); cursor: not-allowed; }
.btn.ghostbtn { background: transparent; color: var(--ink); border-color: var(--rule); }
.btn.ghostbtn:hover:not(:disabled) { border-color: var(--accent); filter: none; }
.btn.big { padding: 13px 24px; font-size: 16px; }
button.text { background: none; border: 0; padding: 4px 0; font: inherit; color: var(--muted); cursor: pointer; text-decoration: underline; text-underline-offset: 3px; }
button.text:hover { color: var(--ink); }
button.text.danger { color: var(--danger); }
button:focus-visible, input:focus-visible, a:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

/* Grid */
.grid { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: clamp(12px, 3vw, 24px); }
.slot { display: grid; gap: 8px; min-width: 0; align-content: start; }
.frame {
  position: relative; width: 100%; max-width: 100%; aspect-ratio: 2 / 3;
  border-radius: 3px; border: 0; padding: 0; margin: 0;
  background: transparent; color: inherit; cursor: pointer; font: inherit;
  display: grid; place-items: center; overflow: hidden;
  -webkit-user-select: none; user-select: none;
}
.frame:focus-visible { outline: 3px solid var(--accent); outline-offset: 3px; }
.frame.empty {
  border: 1.5px dashed var(--rule);
  background: color-mix(in srgb, var(--surface) 60%, transparent);
  transition: border-color .15s, background .15s;
}
.frame.empty:hover { border-color: var(--accent); background: var(--surface); }
.plus { width: 34%; max-width: 44px; aspect-ratio: 1; color: var(--muted); transition: color .15s, transform .15s; }
.frame.empty:hover .plus { color: var(--accent); transform: scale(1.08); }
.frame.filled { box-shadow: var(--shadow); touch-action: none; cursor: grab; background: var(--surface); }
.frame.filled img { width: 100%; height: 100%; object-fit: cover; display: block; pointer-events: none; }
.frame.lifted { opacity: .25; }
.frame.target { outline: 3px solid var(--accent); outline-offset: 3px; }
.frame.landed { animation: land .35s ease-out; }
@keyframes land { from { transform: scale(.94); } to { transform: none; } }

.caption { min-height: 2.6em; font-size: 13px; line-height: 1.3; min-width: 0; }
.caption .t { font-weight: 600; display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden; overflow-wrap: anywhere; }
.caption .a { color: var(--muted); font-size: 12px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }

/* Typographic cover for books without artwork */
.gen {
  width: 100%; height: 100%; padding: 12% 10%;
  display: flex; flex-direction: column; justify-content: space-between;
  color: #f6f3ea; background: var(--cloth, #2f5d48);
  box-shadow: inset 0 0 0 4px rgba(255,255,255,.08), inset 0 0 0 5px rgba(0,0,0,.18);
  text-align: left; overflow: hidden;
}
.gen .gt { font: 400 clamp(11px, 2.6vw, 17px)/1.15 var(--display); overflow-wrap: anywhere; display: -webkit-box; -webkit-line-clamp: 6; -webkit-box-orient: vertical; overflow: hidden; }
.gen .ga { font-size: clamp(9px, 1.8vw, 11px); letter-spacing: .08em; text-transform: uppercase; opacity: .8; overflow-wrap: anywhere; }
.gen.mini { padding: 10% 8%; }
.gen.mini .gt { font-size: 8px; -webkit-line-clamp: 4; }
.gen.mini .ga { display: none; }

.gridfoot { display: flex; justify-content: space-between; align-items: center; gap: 12px; flex-wrap: wrap; color: var(--muted); font-size: 13px; }
kbd { font: 12px var(--body); border: 1px solid var(--rule); border-bottom-width: 2px; border-radius: 4px; padding: 0 5px; }
.confirm { display: inline-flex; gap: 12px; align-items: center; }

.ghost { position: fixed; z-index: 50; pointer-events: none; border-radius: 3px; overflow: hidden; box-shadow: 0 12px 32px rgba(0,0,0,.3); transform: rotate(-3deg) scale(1.04); }
.ghost img { width: 100%; height: 100%; object-fit: cover; display: block; }

/* Most chosen */
.top { border-top: 1px solid var(--rule); padding-top: 24px; display: grid; gap: 14px; }
.top-head { display: flex; justify-content: space-between; align-items: baseline; gap: 12px; flex-wrap: wrap; }
.top-head h2 { margin: 0; font: 400 24px/1.2 var(--display); }
.top-head .count { margin: 0; }
.top-list { list-style: none; margin: 0; padding: 0; display: grid; }
.top-item {
  display: grid; grid-template-columns: 2ch 36px minmax(0, 1fr) auto; gap: 14px; align-items: center;
  padding: 10px 0; border-bottom: 1px solid color-mix(in srgb, var(--rule) 60%, transparent);
}
.top-item .rank { font: 400 18px/1 var(--display); color: var(--muted); text-align: right; font-variant-numeric: tabular-nums; }
.top-item .thumb { width: 36px; }
.top-item .info { min-width: 0; display: grid; gap: 4px; }
.top-item .rt { font-weight: 600; line-height: 1.3; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.top-item .bar { height: 4px; border-radius: 2px; background: color-mix(in srgb, var(--rule) 60%, transparent); overflow: hidden; }
.top-item .bar i { display: block; height: 100%; background: var(--accent); }
.top-item .side { display: flex; align-items: center; gap: 10px; }
.top-item .n { font-size: 13px; color: var(--muted); font-variant-numeric: tabular-nums; white-space: nowrap; }
.add-mini { font: inherit; font-size: 12px; font-weight: 600; border: 1px solid var(--rule); background: transparent; color: var(--ink); border-radius: 999px; padding: 3px 10px; cursor: pointer; }
.add-mini:hover { border-color: var(--accent); color: var(--accent); }
.note { margin: 0; font-size: 13px; color: var(--muted); }

/* Dialogs */
dialog {
  width: min(560px, calc(100vw - 24px)); max-height: min(760px, calc(100dvh - 24px));
  padding: 0; border: 1px solid var(--rule); border-radius: 10px;
  background: var(--surface); color: var(--ink); box-shadow: 0 24px 64px rgba(0,0,0,.3);
}
dialog.wide { width: min(880px, calc(100vw - 24px)); }
dialog[open] { display: flex; flex-direction: column; }
dialog::backdrop { background: rgba(15,20,18,.5); }
.dhead { display: flex; justify-content: space-between; align-items: center; gap: 12px; padding: 18px 20px 0; }
.dhead h2 { margin: 0; font: 400 22px/1.2 var(--display); }
.x { background: none; border: 0; font-size: 24px; line-height: 1; color: var(--muted); cursor: pointer; padding: 4px 8px; border-radius: 6px; }
.x:hover { color: var(--ink); background: var(--paper); }
.search { padding: 14px 20px; }
.field { width: 100%; font: inherit; font-size: 16px; padding: 11px 13px; border-radius: 7px; border: 1px solid var(--rule); background: var(--paper); color: var(--ink); min-width: 0; }
.field:focus { border-color: var(--accent); outline: none; box-shadow: 0 0 0 3px color-mix(in srgb, var(--accent) 25%, transparent); }
.results { flex: 1; overflow-y: auto; border-top: 1px solid var(--rule); border-bottom: 1px solid var(--rule); min-height: 140px; }
.status { padding: 28px 20px; color: var(--muted); text-align: center; }
.result {
  display: grid; grid-template-columns: 46px minmax(0, 1fr); gap: 14px; align-items: center;
  width: 100%; padding: 10px 20px; border: 0; border-bottom: 1px solid color-mix(in srgb, var(--rule) 55%, transparent);
  background: none; color: inherit; font: inherit; text-align: left; cursor: pointer;
}
.result:hover, .result:focus-visible { background: var(--paper); outline: none; }
.result:focus-visible { box-shadow: inset 3px 0 0 var(--accent); }
.thumb { width: 46px; aspect-ratio: 2 / 3; border-radius: 2px; overflow: hidden; background: var(--paper); box-shadow: 0 1px 3px rgba(0,0,0,.2); }
.thumb img { width: 100%; height: 100%; object-fit: cover; display: block; }
.rt { font-weight: 600; line-height: 1.3; overflow-wrap: anywhere; }
.rm { color: var(--muted); font-size: 13px; overflow-wrap: anywhere; }

.own { padding: 14px 20px 18px; display: grid; gap: 12px; }
.own summary { cursor: pointer; color: var(--accent); font-weight: 600; width: fit-content; }
.own form { display: grid; gap: 10px; margin-top: 12px; }
.own label, .stack { font-size: 12px; letter-spacing: .06em; text-transform: uppercase; color: var(--muted); display: grid; gap: 4px; }
.own .row { display: flex; gap: 10px; align-items: center; flex-wrap: wrap; justify-content: space-between; }
.dfoot { display: flex; justify-content: space-between; align-items: center; padding: 0 20px 16px; gap: 12px; }
input[type=file] { font-size: 13px; color: var(--muted); max-width: 100%; }

/* Share dialog */
.share-body { display: grid; grid-template-columns: minmax(0, 1fr) minmax(0, 1.1fr); gap: 24px; padding: 18px 20px 20px; overflow-y: auto; }
.poster-wrap { min-width: 0; }
#poster { width: 100%; max-width: 100%; height: auto; display: block; border-radius: 4px; box-shadow: var(--shadow); border: 1px solid var(--rule); }
.share-side { display: grid; gap: 18px; align-content: start; min-width: 0; }
.themes { border: 0; padding: 0; margin: 0; display: flex; gap: 10px; flex-wrap: wrap; }
.themes legend { font-size: 12px; letter-spacing: .06em; text-transform: uppercase; color: var(--muted); padding: 0; margin-bottom: 8px; }
.swatch { display: inline-flex; align-items: center; gap: 8px; padding: 6px 12px 6px 6px; border: 1px solid var(--rule); border-radius: 999px; cursor: pointer; font-size: 14px; }
.swatch input { position: absolute; opacity: 0; pointer-events: none; }
.swatch .chip { width: 22px; height: 22px; border-radius: 50%; background: var(--c); box-shadow: inset 0 0 0 1px rgba(0,0,0,.2); position: relative; }
.swatch .chip::after { content: ""; position: absolute; inset: 8px 5px; border-radius: 1px; background: var(--i); opacity: .7; }
.swatch:has(input:checked) { border-color: var(--accent); box-shadow: 0 0 0 1px var(--accent); }
.swatch:has(input:focus-visible) { outline: 2px solid var(--accent); outline-offset: 2px; }
.actions { display: flex; gap: 10px; flex-wrap: wrap; }
.msg { min-height: 1.4em; font-size: 13px; color: var(--accent); margin-top: -8px; }
.msg.err { color: var(--danger); }
.linkbox { display: grid; gap: 6px; }
.linkbox .label { font-size: 12px; letter-spacing: .06em; text-transform: uppercase; color: var(--muted); }
.linkrow { display: flex; gap: 8px; }
.linkrow .field { font-size: 14px; padding: 9px 11px; }
.platforms { display: flex; flex-wrap: wrap; gap: 8px; }
.platforms a {
  font-size: 14px; font-weight: 500; padding: 7px 14px; border-radius: 999px; border: 1px solid var(--rule);
  color: var(--ink); text-decoration: none; background: var(--paper);
}
.platforms a:hover { border-color: var(--accent); color: var(--accent); }
.platforms a[aria-disabled="true"] { opacity: .5; pointer-events: none; }

/* Share landing page */
.shared { display: grid; gap: 24px; justify-items: center; }
.shared-poster { width: 100%; max-width: 100%; height: auto; border-radius: 4px; box-shadow: var(--shadow); }
.shared-list { width: 100%; margin: 0; padding-left: 1.6em; display: grid; gap: 6px; }
.shared-list .t { font-weight: 600; }
.shared-list .a { color: var(--muted); }
.lede { font-size: 18px; color: var(--muted); }

@media (max-width: 720px) {
  .share-body { grid-template-columns: minmax(0, 1fr); }
  .poster-wrap { max-width: 320px; justify-self: center; width: 100%; }
}
@media (max-width: 420px) {
  .caption { font-size: 12px; min-height: 2.4em; }
  .caption .a { display: none; }
  .top-item { grid-template-columns: 2ch 30px minmax(0, 1fr) auto; gap: 10px; }
  .top-item .thumb { width: 30px; }
  .add-mini { display: none; }
}
@media (prefers-reduced-motion: reduce) {
  * { animation: none !important; transition: none !important; }
  .ghost { transform: none; }
}
