:root {
  --bg: #f4efe4;
  --paper: #fffdf8;
  --paper-2: #f9f5ec;
  --ink: #1f1a14;
  --muted: #6f6556;
  --line: #e2d8c4;
  --accent: #a8402c;
  --accent-ink: #fff;
  --up: #2f7d4f;
  --down: #b0452f;
  --gold: linear-gradient(120deg, #f6d36b 0%, #d99a27 35%, #fff0b3 50%, #d99a27 65%, #f6d36b 100%);
  --shadow: 0 1px 2px rgba(40, 30, 10, .10), 0 3px 10px rgba(40, 30, 10, .07);
  --radius: 12px;
  --serif: 'Fraunces', Georgia, 'Times New Roman', serif;
  --sans: 'Inter', system-ui, -apple-system, 'Segoe UI', sans-serif;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #15120e;
    --paper: #1f1b16;
    --paper-2: #26211b;
    --ink: #f0e8da;
    --muted: #a69b89;
    --line: #362f26;
    --accent: #e27d62;
    --accent-ink: #1a120c;
    --up: #6cc08e;
    --down: #eb8a72;
    --shadow: 0 1px 2px rgba(0, 0, 0, .45), 0 3px 10px rgba(0, 0, 0, .25);
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }
html, body { margin: 0; }
body {
  background: var(--bg);
  color: var(--ink);
  font: 15px/1.5 var(--sans);
  -webkit-font-smoothing: antialiased;
}
a { color: var(--accent); }
code { font: .85em ui-monospace, 'Cascadia Code', Consolas, monospace; background: var(--paper-2); padding: 1px 5px; border-radius: 4px; }
h1, h2 { font-family: var(--serif); font-weight: 650; letter-spacing: -.01em; }
h2 { font-size: 1.05rem; margin: 1.2em 0 .5em; }
h2:first-child { margin-top: 0; }
.muted { color: var(--muted); }
small { color: var(--muted); font-size: .8em; }

/* ── Top bar ─────────────────────────────── */
.topbar {
  position: sticky; top: 0; z-index: 20;
  display: flex; align-items: center; gap: 16px; flex-wrap: wrap;
  padding: 10px 24px;
  background: color-mix(in srgb, var(--bg) 88%, transparent);
  backdrop-filter: blur(8px);
  border-bottom: 1px solid var(--line);
}
.brand { display: flex; align-items: center; gap: 10px; color: inherit; text-decoration: none; margin-right: auto; }
.brand-mark {
  display: grid; place-items: center; width: 34px; height: 34px; border-radius: 9px;
  background: var(--ink); color: var(--bg); font-size: 18px;
}
.brand-name { display: block; font: 700 1.08rem/1.1 var(--serif); }
.tabs { display: flex; gap: 2px; background: var(--paper-2); border: 1px solid var(--line); border-radius: 10px; padding: 3px; }
.tab {
  border: 0; background: transparent; color: var(--muted); font: 500 .88rem var(--sans);
  padding: 6px 14px; border-radius: 7px; cursor: pointer;
}
.tab:hover { color: var(--ink); }
.tab.active { background: var(--paper); color: var(--ink); box-shadow: var(--shadow); }
.top-actions { display: flex; gap: 6px; align-items: center; }

/* ── Controls ────────────────────────────── */
.btn, .icon-btn {
  font: 500 .88rem var(--sans); color: var(--ink);
  background: var(--paper); border: 1px solid var(--line); border-radius: 8px;
  padding: 8px 14px; cursor: pointer; box-shadow: var(--shadow);
  transition: transform .08s, background .15s;
}
.btn:hover, .icon-btn:hover { background: var(--paper-2); }
.btn:active, .icon-btn:active { transform: translateY(1px); }
.btn:disabled, .icon-btn:disabled { opacity: .45; cursor: default; transform: none; }
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); }
.btn.primary:hover { filter: brightness(1.06); }
.btn.ghost { background: transparent; box-shadow: none; }
.btn.danger { color: var(--down); }
.btn.danger:not(.ghost) { background: var(--down); border-color: var(--down); color: #fff; }
.btn.small { padding: 5px 10px; font-size: .8rem; }
.icon-btn { width: 34px; height: 34px; padding: 0; font-size: 1.1rem; line-height: 1; }
.btn-row { display: flex; gap: 8px; flex-wrap: wrap; margin-top: 12px; }

select, input[type=search], input[type=number], textarea {
  font: .88rem var(--sans); color: var(--ink);
  background: var(--paper); border: 1px solid var(--line); border-radius: 7px; padding: 6px 8px;
}
input[type=search] { min-width: 180px; }
textarea { width: 100%; font: .82rem/1.45 ui-monospace, Consolas, monospace; resize: vertical; }
:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

.field { display: inline-flex; align-items: center; gap: 6px; font-size: .82rem; color: var(--muted); }
.field.stack { flex-direction: column; align-items: stretch; gap: 3px; }
.check, .radio { display: flex; gap: 8px; align-items: flex-start; font-size: .88rem; cursor: pointer; margin: 8px 0; }
.check input, .radio input { margin-top: 3px; accent-color: var(--accent); }
.check small, .radio small { display: block; }
fieldset { border: 0; padding: 0; margin: 12px 0; }
legend { font-size: .82rem; color: var(--muted); margin-bottom: 2px; }

.seg { display: inline-flex; background: var(--paper-2); border: 1px solid var(--line); border-radius: 8px; padding: 2px; }
.seg.full { display: flex; }
.seg button {
  flex: 1; border: 0; background: transparent; color: var(--muted); font: 500 .82rem var(--sans);
  padding: 5px 10px; border-radius: 6px; cursor: pointer; white-space: nowrap;
}
.seg button.active { background: var(--paper); color: var(--ink); box-shadow: var(--shadow); }

.panel { background: var(--paper); border: 1px solid var(--line); border-radius: var(--radius); padding: 20px; box-shadow: var(--shadow); min-width: 0; }
.hint { color: var(--muted); font-size: .84rem; margin: 6px 0; }
.stats { color: var(--muted); font-size: .84rem; margin: 4px 0 12px; font-variant-numeric: tabular-nums; }
.warning { background: color-mix(in srgb, var(--down) 12%, transparent); color: var(--down); border-radius: 8px; padding: 8px 12px; font-size: .85rem; }

main { padding: 20px 24px 64px; max-width: 1400px; margin: 0 auto; }

/* ── Landing ─────────────────────────────── */
.landing { max-width: 720px; margin: 4vh auto 0; }
.landing h1 { font-size: clamp(2rem, 5vw, 3.2rem); line-height: 1.05; margin: .25em 0 .4em; }

.lede { font-size: 1.05rem; color: var(--muted); max-width: 60ch; }
.dropzone {
  margin: 28px 0 12px; padding: 36px 20px; text-align: center; cursor: pointer;
  border: 2px dashed var(--line); border-radius: var(--radius); background: var(--paper);
  display: flex; flex-direction: column; gap: 4px; align-items: center;
  transition: border-color .15s, background .15s;
}
.dropzone:hover, .dropzone:focus-visible { border-color: var(--accent); background: var(--paper-2); }
.dropzone strong { font: 600 1.15rem var(--serif); }
.dropzone span { color: var(--muted); font-size: .88rem; }
.dz-icon { font-size: 1.8rem !important; color: var(--accent) !important; }
.landing-actions { display: flex; gap: 8px; flex-wrap: wrap; }
.steps { margin: 32px 0 0; padding-left: 1.2em; }
.steps li { margin: 8px 0; }
.fineprint { color: var(--muted); font-size: .8rem; margin-top: 24px; }

/* ── Tier list ───────────────────────────── */
.toolbar { display: flex; flex-wrap: wrap; gap: 10px 14px; align-items: center; margin-bottom: 6px; }
.tiers { display: flex; flex-direction: column; gap: 6px; }
.tier {
  display: grid; grid-template-columns: 88px 1fr;
  background: var(--paper); border: 1px solid var(--line); border-radius: 10px; overflow: hidden;
}
.tier-label {
  background: var(--tier); color: var(--tier-ink);
  display: flex; flex-direction: column; align-items: center; justify-content: center;
  padding: 8px 4px; text-align: center;
}
.tier-label.bonus { background: var(--gold); background-size: 300% 100%; animation: shimmer 7s ease-in-out infinite; color: #3a2800; }
@keyframes shimmer { 0%, 100% { background-position: 0% 0; } 50% { background-position: 100% 0; } }
@media (prefers-reduced-motion: reduce) { .tier-label.bonus { animation: none; } }
.tier-value { font: 700 1.55rem/1 var(--serif); font-variant-numeric: tabular-nums; white-space: nowrap; }
.tier-value .star { font-size: .62em; margin-left: 1px; vertical-align: .18em; }
.tier-count { font: 500 .72rem var(--sans); opacity: .75; margin-top: 4px; min-height: 1em; }
.tier-items { display: flex; flex-wrap: wrap; gap: 6px; padding: 6px; align-content: flex-start; min-height: 76px; }
.tiers.hide-empty .tier.empty { display: none; }
body.is-dragging .tiers.hide-empty .tier.empty { display: grid; }
body.is-dragging .tier.empty .tier-items { min-height: 40px; }

.tier.pool { grid-template-columns: 1fr; border-style: dashed; background: transparent; margin-top: 14px; }
.pool-head { display: flex; align-items: center; gap: 12px; flex-wrap: wrap; padding: 10px 12px 0; }
.pool-head .hint { margin: 0; flex: 1; }
.pool .tier-items { min-height: 60px; }

.card { position: relative; width: 58px; cursor: grab; user-select: none; -webkit-user-select: none; border-radius: 4px; }
.card:active { cursor: grabbing; }
.cover {
  position: relative; aspect-ratio: 2 / 3; border-radius: 4px; overflow: hidden;
  background: linear-gradient(160deg, oklch(0.6 0.09 var(--h)), oklch(0.38 0.07 calc(var(--h) + 50)));
  box-shadow: var(--shadow); transition: transform .12s;
}
.card:hover .cover { transform: translateY(-2px); }
.cover img { position: absolute; inset: 0; width: 100%; height: 100%; object-fit: cover; }
.fallback { position: absolute; inset: 0; padding: 5px 4px; display: flex; flex-direction: column; justify-content: space-between; color: #fff; overflow: hidden; }
.fb-title { font: 600 8.5px/1.15 var(--serif); display: -webkit-box; -webkit-line-clamp: 5; -webkit-box-orient: vertical; overflow: hidden; overflow-wrap: anywhere; }
.fb-author { font: 7px/1.15 var(--sans); opacity: .8; display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden; }
.card .meta { display: none; }
.card .orig { display: none; }
.pool .card .orig {
  display: block; position: absolute; top: -5px; right: -5px; z-index: 1;
  background: var(--ink); color: var(--bg); font: 600 9px/1 var(--sans); padding: 3px 4px; border-radius: 99px;
}
.mode-titles .card { width: auto; max-width: 230px; }
.mode-titles .cover { display: none; }
.mode-titles .card .meta {
  display: flex; flex-direction: column; background: var(--paper-2); border: 1px solid var(--line);
  border-radius: 6px; padding: 3px 8px; min-width: 0;
}
.mode-titles .tier-items { min-height: 44px; }
.m-title { font: 500 .8rem/1.3 var(--sans); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.m-author { font-size: .7rem; line-height: 1.2; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.mode-titles .pool .card .orig { top: -6px; right: -6px; }
.card.dim { opacity: .15; }
.card.match .cover, .card.match .meta { outline: 3px solid var(--accent); outline-offset: 2px; }
.sortable-ghost { opacity: .3; }
.sortable-chosen .cover { transform: scale(1.05); }

/* ── Curve ───────────────────────────────── */
.curve-layout { display: grid; grid-template-columns: 300px 1fr; gap: 16px; align-items: start; }
.controls { position: sticky; top: 76px; }
.row2 { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; margin-top: 10px; }
.slider { display: block; margin: 10px 0; font-size: .85rem; }
.slider span { display: flex; justify-content: space-between; color: var(--muted); }
.slider output { color: var(--ink); font-weight: 600; font-variant-numeric: tabular-nums; }
.slider input { width: 100%; accent-color: var(--accent); }
.preview-head { display: flex; justify-content: space-between; align-items: flex-start; gap: 12px; flex-wrap: wrap; }
.preview-head .stats { margin-bottom: 8px; }

.dist-table { display: grid; grid-template-columns: 64px 1fr auto 170px; gap: 3px 12px; align-items: center; margin: 8px 0 16px; }
.dist-table.no-cons { grid-template-columns: 64px 1fr auto; }
.dt-row, .dt-head { display: contents; }
.dt-head > span { font-size: .74rem; color: var(--muted); padding-bottom: 4px; }
.dt-label {
  background: var(--tier); color: var(--tier-ink); border-radius: 6px; text-align: center;
  font: 650 .88rem/1 var(--serif); padding: 6px 0; font-variant-numeric: tabular-nums;
}
.dt-label.bonus { background: var(--gold); color: #3a2800; }
.dt-row.off .dt-label { opacity: .5; }
.dt-bars { display: flex; flex-direction: column; gap: 2px; }
.bar { display: block; height: 9px; border-radius: 3px; min-width: 1px; transition: width .25s ease; }
.bar.cur { background: color-mix(in srgb, var(--muted) 35%, transparent); }
.bar.new { background: var(--tier); }
.dt-row .bar.new.bonus { background: var(--gold); }
.num { text-align: right; font-size: .85rem; font-variant-numeric: tabular-nums; white-space: nowrap; }
.key { display: inline-block; width: 10px; height: 8px; border-radius: 2px; margin: 0 3px 0 8px; vertical-align: 0; }
.key.cur { background: color-mix(in srgb, var(--muted) 35%, transparent); }
.key.new { background: var(--accent); }
.cons { display: flex; gap: 4px; }
.cons select { flex: 1; padding: 3px 4px; font-size: .8rem; }
.cons input { width: 58px; padding: 3px 4px; font-size: .8rem; }

.changes summary { cursor: pointer; font: 650 1.05rem var(--serif); }
.change-list { list-style: none; margin: 8px 0 0; padding: 0; max-height: 460px; overflow: auto; border-top: 1px solid var(--line); }
.change-list li { display: grid; grid-template-columns: 44px 1fr auto; gap: 10px; align-items: center; padding: 6px 4px; border-bottom: 1px solid var(--line); font-size: .86rem; }
.change-list .rank { color: var(--muted); font-variant-numeric: tabular-nums; font-size: .78rem; }
.change-list .ct { min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.change-list .ct small { margin-left: 6px; }
.change-list .delta { font-variant-numeric: tabular-nums; white-space: nowrap; color: var(--muted); }
.change-list .up .delta b { color: var(--up); }
.change-list .down .delta b { color: var(--down); }
.change-list .same .delta b { color: var(--ink); }
.change-list .more { display: block; text-align: center; color: var(--muted); }

/* ── Export ──────────────────────────────── */
.export-layout { display: grid; grid-template-columns: 1fr 1fr; gap: 16px; align-items: start; }
.csv-preview {
  margin: 14px 0 0; padding: 10px; background: var(--paper-2); border-radius: 8px; border: 1px solid var(--line);
  font: .72rem/1.5 ui-monospace, Consolas, monospace; overflow: auto; max-height: 220px; white-space: pre;
}

/* ── Dialogs, toast, overlay ─────────────── */
.dialog {
  border: 1px solid var(--line); border-radius: var(--radius); background: var(--paper); color: var(--ink);
  padding: 22px; width: min(560px, calc(100vw - 32px)); box-shadow: 0 20px 60px rgba(0, 0, 0, .3);
}
.dialog.narrow { width: min(440px, calc(100vw - 32px)); }
.dialog::backdrop { background: rgba(20, 15, 8, .45); }
.dlg-close { position: absolute; top: 10px; right: 10px; }
.dlg-close button { border: 0; background: transparent; color: var(--muted); font-size: 1rem; cursor: pointer; padding: 6px; }
.book { display: grid; grid-template-columns: 120px 1fr; gap: 18px; }
.book .cover { width: 120px; border-radius: 6px; }
.book .fb-title { font-size: 14px; -webkit-line-clamp: 8; }
.book .fb-author { font-size: 11px; }
.book h2 { margin: 0 24px 2px 0; font-size: 1.3rem; line-height: 1.2; }
.book .by { color: var(--muted); margin: 0 0 12px; }
.facts { display: grid; grid-template-columns: auto 1fr; gap: 2px 12px; font-size: .84rem; margin: 0 0 14px; }
.facts dt { color: var(--muted); }
.facts dd { margin: 0; font-variant-numeric: tabular-nums; }
.book .btn-row { margin-top: 8px; }

.toast {
  position: fixed; left: 50%; bottom: 24px; transform: translate(-50%, 20px); opacity: 0; pointer-events: none;
  background: var(--ink); color: var(--bg); padding: 10px 16px; border-radius: 10px; font-size: .88rem;
  box-shadow: var(--shadow); transition: opacity .2s, transform .2s; max-width: calc(100vw - 32px); z-index: 50;
}
.toast.show { opacity: 1; transform: translate(-50%, 0); }
.drop-overlay {
  position: fixed; inset: 0; z-index: 40; display: none; place-items: center;
  background: color-mix(in srgb, var(--bg) 80%, transparent); backdrop-filter: blur(3px);
}
.drop-overlay span { font: 650 1.6rem var(--serif); border: 3px dashed var(--accent); border-radius: 18px; padding: 40px 60px; }
.drop-overlay.show { display: grid; }

/* ── Small screens ───────────────────────── */
@media (max-width: 900px) {
  .curve-layout, .export-layout { grid-template-columns: 1fr; }
  .controls { position: static; }
}
@media (max-width: 640px) {
  .topbar { padding: 8px 16px; gap: 8px; }
  .tabs { order: 3; width: 100%; }
  .tab { flex: 1; }
  main { padding: 14px 16px 48px; }
  .tier { grid-template-columns: 58px 1fr; }
  .tier-value { font-size: 1.15rem; }
  .card { width: 48px; }
  input[type=search] { min-width: 0; width: 100%; }
  .dist-table { grid-template-columns: 52px 1fr auto; }
  .dist-table .cons { grid-column: 2 / -1; margin-bottom: 6px; }
  .dist-table .dt-head > span:last-child { display: none; }
  .book { grid-template-columns: 84px 1fr; gap: 12px; }
  .book .cover { width: 84px; }
  .change-list li { grid-template-columns: 36px 1fr auto; gap: 6px; }
}
