:root {
  --paper: #fbfaf7;
  --desk: #eeebe4;
  --ink: #2b2a28;
  --ink-soft: #6b6862;
  --ink-faint: #9a968e;
  --rule: #cfcbc2;
  --rule-strong: #8d8980;
  --highlight: #a7d3f0;
  --highlight-soft: #d7ecf9;
  --accent: #2f6f9f;
  --accent-ink: #ffffff;
  --danger: #a13a2c;
  --tab: #f6f3ec;
  --shadow: 0 1px 2px rgba(40, 35, 25, .06), 0 8px 28px rgba(40, 35, 25, .08);

  --font-display: 'Lobster Two', 'Brush Script MT', cursive;
  --font-body: 'Figtree', 'Segoe UI', system-ui, sans-serif;

  --text-xs: .75rem;
  --text-sm: .875rem;
  --text-base: 1rem;
  --text-lg: 1.125rem;
  --text-xl: 1.5rem;
  --text-2xl: 2rem;

  --radius: 10px;
  --line-h: 2.1rem;
  color-scheme: light;
}

[data-theme="dark"] {
  --paper: #22211f;
  --desk: #171614;
  --ink: #ece8df;
  --ink-soft: #b3aea4;
  --ink-faint: #837f77;
  --rule: #3d3b37;
  --rule-strong: #6a665f;
  --highlight: #3f7fa8;
  --highlight-soft: #243a49;
  --accent: #8cc6ec;
  --accent-ink: #10202b;
  --danger: #e38a7c;
  --tab: #2a2926;
  --shadow: 0 1px 2px rgba(0, 0, 0, .3), 0 8px 28px rgba(0, 0, 0, .35);
  color-scheme: dark;
}

* { box-sizing: border-box; }
html, body { margin: 0; }
body {
  font-family: var(--font-body);
  font-size: var(--text-base);
  color: var(--ink);
  background: var(--desk);
  -webkit-font-smoothing: antialiased;
  min-height: 100dvh;
}
button, input, select, textarea { font: inherit; color: inherit; }
button { cursor: pointer; }
:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
.sr-only { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; }

/* ---------- Top bar ---------- */
.topbar {
  position: sticky; top: 0; z-index: 20;
  display: flex; align-items: center; gap: 1rem;
  padding: .6rem clamp(.75rem, 2vw, 1.5rem);
  padding-top: max(.6rem, env(safe-area-inset-top));
  background: color-mix(in srgb, var(--desk) 88%, transparent);
  backdrop-filter: blur(10px);
  border-bottom: 1px solid var(--rule);
}
.brand { display: flex; align-items: center; gap: .6rem; color: var(--ink); text-decoration: none; }
.logo { width: 30px; height: 30px; flex: none; }
.brand-text { display: flex; flex-direction: column; line-height: 1.1; }
.brand-name { font-weight: 700; font-size: var(--text-base); letter-spacing: -.01em; }
.brand-sub { font-size: var(--text-xs); color: var(--ink-soft); letter-spacing: .08em; text-transform: uppercase; }

.switch {
  margin-inline: auto;
  display: inline-flex; padding: 3px; gap: 2px;
  background: var(--tab); border: 1px solid var(--rule); border-radius: 999px;
}
.switch button {
  border: 0; background: transparent; padding: .4rem 1rem; border-radius: 999px;
  font-weight: 600; font-size: var(--text-sm); color: var(--ink-soft);
}
.switch button[aria-selected="true"] { background: var(--ink); color: var(--paper); }

.top-actions { display: flex; gap: .25rem; }
.icon-btn {
  width: 38px; height: 38px; display: grid; place-items: center;
  border: 0; background: transparent; border-radius: 8px; color: var(--ink-soft);
}
.icon-btn:hover { background: var(--tab); color: var(--ink); }
.icon-btn svg { width: 20px; height: 20px; }

/* ---------- Layout ---------- */
.layout {
  display: grid;
  grid-template-columns: 88px 1fr;
  max-width: 1100px; margin: 0 auto;
  gap: clamp(.75rem, 2vw, 2rem);
  padding: 1.25rem clamp(.75rem, 2vw, 1.5rem) 4rem;
}

/* Binder tabs */
.tabs {
  position: sticky; top: 76px; align-self: start;
  display: flex; flex-direction: column; gap: 2px;
  max-height: calc(100dvh - 96px); overflow-y: auto;
  scrollbar-width: thin;
}
.tabs .group-label {
  font-size: 10px; letter-spacing: .12em; text-transform: uppercase;
  color: var(--ink-faint); padding: .6rem .5rem .2rem;
}
.tab {
  display: flex; align-items: center; justify-content: space-between; gap: .25rem;
  border: 1px solid transparent; background: transparent;
  padding: .28rem .55rem; border-radius: 6px;
  font-size: var(--text-sm); color: var(--ink-soft); text-align: left;
}
.tab:hover { background: var(--tab); color: var(--ink); }
.tab[aria-current="true"] { background: var(--paper); color: var(--ink); border-color: var(--rule); box-shadow: var(--shadow); }
.tab .dot { width: 6px; height: 6px; border-radius: 50%; background: var(--highlight); flex: none; }
.tab.letter { font-family: var(--font-display); font-size: 1.15rem; line-height: 1.2; }
.letters-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 2px; }

/* Pager */
.stage { min-width: 0; }
.pager {
  display: flex; align-items: center; gap: .5rem; margin-bottom: .9rem;
  max-width: 760px; margin-inline: auto;
}
.flip {
  width: 40px; height: 40px; flex: none; display: grid; place-items: center;
  border: 1px solid var(--rule); background: var(--paper); border-radius: 999px; color: var(--ink);
}
.flip:disabled { opacity: .35; cursor: default; }
.flip svg { width: 18px; height: 18px; }
.page-pick select {
  appearance: none; border: 1px solid var(--rule); background: var(--paper);
  border-radius: 999px; padding: .5rem 1rem; font-size: var(--text-sm); font-weight: 600;
  max-width: 62vw;
}
.saved { margin-left: auto; font-size: var(--text-xs); color: var(--ink-faint); }

/* ---------- The sheet (US Letter proportions) ---------- */
.sheet {
  position: relative;
  max-width: 760px; margin: 0 auto;
  min-height: min(983px, 129vw);
  background: var(--paper);
  border-radius: 4px;
  box-shadow: var(--shadow);
  padding: clamp(1.25rem, 5vw, 3.25rem) clamp(1rem, 5vw, 3.5rem) clamp(1.5rem, 5vw, 3rem);
  animation: turn .28s ease-out;
}
@keyframes turn { from { opacity: 0; transform: translateY(6px); } to { opacity: 1; transform: none; } }
@media (prefers-reduced-motion: reduce) { .sheet { animation: none; } }

.letter-mark {
  position: absolute; top: clamp(.5rem, 2vw, 1.25rem); right: clamp(.9rem, 3vw, 2rem);
  font-family: var(--font-display); font-weight: 700; font-style: italic;
  font-size: clamp(3.25rem, 11vw, 5.5rem); line-height: 1; color: var(--ink);
  pointer-events: none;
}
.page-title {
  font-family: var(--font-display); font-weight: 400; font-style: italic;
  font-size: var(--text-2xl); text-align: center; margin: .25rem 0 .5rem;
}

/* Written-on-the-line inputs */
.field { display: flex; align-items: flex-end; gap: .4rem; min-width: 0; }
.lbl {
  font-family: var(--font-display); font-weight: 700; font-size: 1.05rem;
  color: var(--ink); white-space: nowrap; line-height: 1.6;
}
.line {
  flex: 1; min-width: 0; border: 0; border-bottom: 1.5px solid var(--rule-strong);
  background: transparent; padding: .25rem .2rem .15rem; font-size: var(--text-base);
  border-radius: 0; line-height: 1.4;
}
.line:focus { outline: none; border-bottom-color: var(--accent); background: var(--highlight-soft); }
.line.hl { background: var(--highlight); border-bottom-color: var(--ink); }
.line.hl:focus { background: var(--highlight); box-shadow: 0 0 0 2px var(--accent); }
.line.short { flex: 0 0 5.5rem; }

.author-row {
  display: grid; grid-template-columns: auto 1fr 1fr; gap: .5rem .9rem; align-items: end;
  padding-right: clamp(3rem, 12vw, 6rem);
}
.author-row .lbl.u { text-decoration: underline; text-underline-offset: 3px; }

/* Sheet toolbar (inserts) */
.sheet-tools {
  display: flex; flex-wrap: wrap; align-items: center; gap: .5rem;
  font-size: var(--text-sm); color: var(--ink-soft); margin: -.25rem 0 1rem;
}
.chip {
  border: 1px solid var(--rule); background: var(--tab); border-radius: 999px;
  padding: .3rem .75rem; font-size: var(--text-sm); color: var(--ink-soft);
}
.chip:hover { color: var(--ink); border-color: var(--rule-strong); }
.chip[aria-pressed="true"] { background: var(--ink); color: var(--paper); border-color: var(--ink); }
.chip.add { border-style: dashed; background: transparent; }
.chip.remove { margin-left: auto; color: var(--danger); background: transparent; border-color: transparent; }

/* Master entry block */
.entry { padding: 1.35rem 0 1.1rem; display: grid; gap: .9rem; }
.entry + .entry { border-top: 1px dashed var(--rule); }
.entry .indent { padding-left: clamp(0rem, 3vw, 1.5rem); }
.entry .indent2 { padding-left: clamp(.25rem, 5vw, 2.75rem); }
.series-row { display: flex; flex-wrap: wrap; gap: .5rem 1.25rem; align-items: flex-end; }
.series-row .field:last-child { flex: 1 1 16rem; }

.bb { display: flex; align-items: center; gap: .35rem; }
.bb .slash { font-family: var(--font-display); font-weight: 700; color: var(--ink-soft); }
.pick {
  font-family: var(--font-display); font-weight: 700; font-size: 1.05rem;
  border: 1.5px solid transparent; background: transparent; border-radius: 999px;
  padding: .05rem .6rem; color: var(--ink);
}
.pick:hover { border-color: var(--rule-strong); }
.pick[aria-pressed="true"] { border-color: var(--ink); background: var(--highlight); }

.sources { display: flex; flex-wrap: wrap; gap: .55rem 1.1rem; align-items: center; }
.check { display: inline-flex; align-items: center; gap: .4rem; cursor: pointer; user-select: none; }
.check input {
  appearance: none; width: 1.55rem; height: 1.1rem; margin: 0;
  border: 0; border-bottom: 1.5px solid var(--rule-strong); background: transparent; position: relative; cursor: pointer;
}
.check input:checked::after {
  content: "✓"; position: absolute; left: 50%; bottom: -1px; transform: translateX(-50%);
  font-weight: 700; font-size: 1.05rem; color: var(--accent);
}
.check input:focus-visible { outline: 2px solid var(--accent); }
.other { display: inline-flex; align-items: flex-end; gap: .4rem; flex: 1 1 13rem; }
.other .line { min-width: 5rem; }

.comments textarea {
  width: 100%; border: 0; resize: vertical; background: transparent;
  min-height: calc(var(--line-h) * 2);
  line-height: var(--line-h); padding: 0 .2rem;
  background-image: linear-gradient(to bottom, transparent calc(var(--line-h) - 1.5px), var(--rule-strong) calc(var(--line-h) - 1.5px), var(--rule-strong) var(--line-h));
  background-size: 100% var(--line-h);
  background-attachment: local;
  text-indent: 6.4rem;
}
.comments { position: relative; }
.comments .lbl { position: absolute; left: 0; top: calc(var(--line-h) - 1.9rem); }
.comments textarea:focus { outline: none; background-color: var(--highlight-soft); }

/* Ruled lists */
.ruled { list-style: none; margin: 0; padding: 0; }
.ruled li { display: flex; align-items: flex-end; gap: .5rem; }
.ruled .num { width: 1.6rem; flex: none; font-size: var(--text-xs); color: var(--ink-faint); text-align: right; padding-bottom: .35rem; }
.ruled .line { height: var(--line-h); }
.ruled.two li { display: grid; grid-template-columns: 1.6rem 1fr 9rem; gap: 1rem; }
.col-heads { display: grid; grid-template-columns: 1.6rem 1fr 9rem; gap: 1rem; margin: 1.25rem 0 .5rem; }
.col-heads span { font-family: var(--font-display); font-style: italic; font-size: 1.05rem; color: var(--ink-soft); text-align: center; }

.wish-modes { display: flex; justify-content: center; gap: .5rem; margin-bottom: 1.25rem; }
.wish-modes .chip { font-family: var(--font-display); font-style: italic; font-size: 1rem; }

/* Series page */
.series-title-row { margin: 1.5rem 0 .75rem; padding-right: 0; }
.series-title-row .lbl { font-size: 1.1rem; }

/* Cover */
.cover { text-align: center; padding-top: clamp(2rem, 8vw, 5rem); }
.cover h1 {
  font-family: var(--font-display); font-weight: 400;
  font-size: clamp(3rem, 12vw, 6rem); line-height: 1.05; margin: 0 0 .5rem;
}
.cover .kind { font-size: var(--text-sm); letter-spacing: .14em; text-transform: uppercase; color: var(--ink-soft); margin-bottom: 3rem; }
.cover .belongs { font-family: var(--font-display); font-size: var(--text-xl); margin-bottom: .5rem; }
.cover .line.big { font-size: var(--text-xl); text-align: center; border-bottom-style: dashed; width: min(100%, 26rem); }
.cover .note { margin: 2.5rem auto 0; width: 100%; }
.cover .dates { display: grid; gap: .75rem; max-width: 18rem; margin-top: 2rem; text-align: left; }
.cover .dates .lbl { font-family: var(--font-body); font-weight: 500; font-size: var(--text-sm); color: var(--ink-soft); width: 6.5rem; }
.cover .stats { margin-top: 3rem; display: flex; justify-content: center; gap: 2rem; flex-wrap: wrap; color: var(--ink-soft); font-size: var(--text-sm); }
.cover .stats b { display: block; font-size: var(--text-xl); color: var(--ink); font-variant-numeric: tabular-nums; }
.storage-note { margin-top: 2rem; font-size: var(--text-xs); color: var(--danger); }

/* Dialogs */
.dlg {
  width: min(640px, calc(100vw - 1.5rem)); max-height: min(80dvh, 720px);
  border: 1px solid var(--rule); border-radius: 14px; padding: 0;
  background: var(--paper); color: var(--ink); box-shadow: var(--shadow);
}
.dlg::backdrop { background: rgba(20, 18, 14, .45); }
.dlg-head { display: flex; align-items: center; gap: .5rem; padding: .75rem; border-bottom: 1px solid var(--rule); }
.dlg-head input { flex: 1; border: 0; background: transparent; font-size: var(--text-lg); padding: .4rem .5rem; }
.dlg-head input:focus { outline: none; }
.dlg-title { flex: 1; margin: 0 .5rem; font-size: var(--text-lg); }
.results { list-style: none; margin: 0; padding: .5rem; overflow-y: auto; max-height: 60dvh; }
.results li button {
  width: 100%; text-align: left; border: 0; background: transparent; border-radius: 8px;
  padding: .6rem .75rem; display: grid; gap: .1rem;
}
.results li button:hover, .results li button:focus-visible { background: var(--tab); }
.results .r-main { font-weight: 600; }
.results .r-sub { font-size: var(--text-sm); color: var(--ink-soft); }
.results .empty { padding: 1.5rem; text-align: center; color: var(--ink-soft); font-size: var(--text-sm); }
.menu-body { padding: 1rem 1.25rem 1.25rem; display: grid; gap: .75rem; }
.menu-body p { margin: 0; color: var(--ink-soft); font-size: var(--text-sm); line-height: 1.5; }
.menu-body hr { border: 0; border-top: 1px solid var(--rule); width: 100%; margin: .25rem 0; }
.btn {
  display: inline-flex; justify-content: center; border: 1px solid var(--ink); background: var(--ink); color: var(--paper);
  border-radius: 999px; padding: .6rem 1rem; font-weight: 600; cursor: pointer; text-align: center;
}
.btn-ghost { background: transparent; color: var(--ink); }
.btn-danger { background: transparent; color: var(--danger); border-color: var(--danger); }

/* ---------- Mobile ---------- */
@media (max-width: 760px) {
  .layout { grid-template-columns: 1fr; padding-top: .75rem; }
  .brand-text { display: none; }
  .switch { margin-inline: auto; }
  .tabs {
    position: sticky; top: 58px; z-index: 10; flex-direction: row; max-height: none;
    overflow-x: auto; overflow-y: hidden; gap: 4px; padding: .35rem 0 .5rem;
    background: var(--desk); margin: -.25rem 0 .25rem;
  }
  .tabs .group-label { display: none; }
  .letters-grid { display: contents; }
  .tab { flex: none; white-space: nowrap; }
  .tab .dot { display: none; }
  .tab.has-data { box-shadow: inset 0 -3px 0 var(--highlight); }
  .tab.lower { display: none; }
  .author-row { grid-template-columns: 1fr 1fr; }
  .author-row > .lbl.u { grid-column: 1 / -1; }
  .ruled.two li, .col-heads { grid-template-columns: 1.2rem 1fr 6.5rem; gap: .5rem; }
  .ruled .num { width: 1.2rem; }
  .comments textarea { text-indent: 6rem; }
}

/* refinements */
.cover .note .line { display: block; width: 100%; }
.author-row > .lbl.u { justify-self: start; }

/* ---------- Combined dividers ---------- */
.spacer { flex: 1; }
.hl-label { background: var(--highlight); padding: 0 .3rem; border-radius: 3px; }
.divider-bar {
  display: flex; flex-wrap: wrap; gap: .5rem 1.25rem; align-items: center;
  margin: calc(-1 * clamp(.5rem, 2.5vw, 1.75rem)) 0 1.5rem;
  padding-right: clamp(3.5rem, 13vw, 6.5rem);
}
.sub-group { display: inline-flex; align-items: center; gap: .25rem; }
.sub-kind { font-size: var(--text-xs); letter-spacing: .12em; text-transform: uppercase; color: var(--ink-faint); margin-right: .25rem; }
.sub {
  position: relative; min-width: 2.25rem; height: 2.25rem; padding: 0 .5rem;
  border: 1px solid var(--rule); background: var(--tab); border-radius: 8px;
  font-family: var(--font-display); font-weight: 700; font-size: 1.15rem; color: var(--ink-soft);
}
.sub:hover { color: var(--ink); border-color: var(--rule-strong); }
.sub[aria-current="true"] { background: var(--ink); color: var(--paper); border-color: var(--ink); }
.sub-dot { position: absolute; top: 3px; right: 3px; width: 6px; height: 6px; border-radius: 50%; background: var(--highlight); }
.sheet-series { border-left: 6px solid var(--highlight); }
.letters-grid { grid-template-columns: 1fr 1fr; }
@media (max-width: 760px) {
  .divider-bar { gap: .5rem .9rem; }
  .sub { min-width: 2.5rem; height: 2.5rem; }
}

/* ---------- Print ---------- */
#print-root { display: none; }
@page { size: letter; margin: 0.4in; }
@media print {
  :root, [data-theme="dark"] {
    --paper: #fff; --desk: #fff; --ink: #1d1c1a; --ink-soft: #444; --ink-faint: #777;
    --rule: #bbb; --rule-strong: #555; --highlight: #a7d3f0; --highlight-soft: transparent; --accent: #1d1c1a;
    color-scheme: light;
  }
  html, body { background: #fff !important; }
  body > *:not(#print-root) { display: none !important; }
  body.printing #print-root { display: block; }
  * { -webkit-print-color-adjust: exact; print-color-adjust: exact; }
  .print-sheet {
    box-shadow: none; border-radius: 0; max-width: none; min-height: 0; margin: 0;
    padding: .15in .2in 0; animation: none !important;
    break-after: page; page-break-after: always; break-inside: avoid;
    font-size: 11pt; --line-h: 1.7rem;
  }
  .print-sheet:last-child { break-after: auto; page-break-after: auto; }
  .print-sheet.sheet-series { border-left: 0; }
  .print-sheet .divider-bar, .print-sheet .sheet-tools, .print-sheet .stats, .print-sheet .storage-note { display: none !important; }
  .print-sheet .letter-mark { top: 0; right: .1in; font-size: 60pt; }
  .print-sheet .entry { padding: .7rem 0 .55rem; gap: .5rem; }
  .print-sheet .entry + .entry { border-top: 0; margin-top: .6rem; }
  .print-sheet .lbl, .print-sheet .pick { font-size: 11.5pt; }
  .print-sheet .line { font-size: 11pt; padding: .1rem .15rem .05rem; }
  .print-sheet .ruled .line { height: 1.55rem; }
  .print-sheet .ruled .num { color: transparent; }
  .print-sheet .comments textarea { resize: none; overflow: hidden; min-height: calc(var(--line-h) * 2); height: calc(var(--line-h) * 2); }
  .print-sheet .pick[aria-pressed="false"] { border-color: transparent; }
  .print-sheet .check input:checked::after { color: #1d1c1a; }
  .print-sheet .cover { padding-top: 1in; }
  .print-sheet .cover .kind { display: none; }
  .print-sheet input[type="date"]::-webkit-calendar-picker-indicator { display: none; }
  .print-insert { position: absolute; top: 0; left: .2in; font-size: 8pt; color: #777; letter-spacing: .1em; text-transform: uppercase; }
  .print-sheet .page-title { margin-top: 0; }
  .print-sheet .wish-modes .chip[aria-pressed="false"] { border-color: transparent; background: transparent; }
  .print-sheet .wish-modes .chip[aria-pressed="true"] { background: transparent; color: #1d1c1a; border: 1.5px solid #1d1c1a; }
}

/* ---------- License screen ---------- */
body.locked > *:not(.gate) { display: none !important; }
.gate {
  min-height: 100dvh; display: grid; place-items: center; padding: 1.5rem;
  background: var(--desk);
}
.gate-card {
  width: min(440px, 100%); background: var(--paper); border-radius: 6px; box-shadow: var(--shadow);
  padding: 2.25rem 1.75rem 1.75rem; display: grid; gap: .6rem; text-align: center;
  border-top: 6px solid var(--highlight);
}
.gate-logo { width: 44px; height: 44px; margin: 0 auto .25rem; color: var(--ink); }
.gate-title { font-family: var(--font-display); font-weight: 700; font-size: var(--text-2xl); margin: 0; }
.gate-sub { margin: 0 0 .75rem; color: var(--ink-soft); font-size: var(--text-sm); line-height: 1.5; }
.gate-label { text-align: left; font-size: var(--text-xs); letter-spacing: .1em; text-transform: uppercase; color: var(--ink-soft); }
.gate-input {
  border: 1.5px solid var(--rule-strong); border-radius: 8px; background: var(--paper);
  padding: .75rem .6rem; font-size: .95rem; letter-spacing: .02em; min-width: 0; width: 100%; text-align: center; font-variant-numeric: tabular-nums;
}
.gate-input:focus { outline: none; border-color: var(--accent); box-shadow: 0 0 0 3px var(--highlight-soft); }
.gate-msg { min-height: 1.25rem; margin: 0; color: var(--danger); font-size: var(--text-sm); }
.gate-btn { width: 100%; padding: .8rem 1rem; }
.gate-btn:disabled { opacity: .6; cursor: default; }
.gate-links { display: flex; flex-wrap: wrap; justify-content: center; gap: .4rem 1.25rem; margin-top: .5rem; font-size: var(--text-sm); }
.gate-links a { color: var(--accent); }
.license-box { display: grid; gap: .6rem; }
.license-note { font-size: var(--text-xs) !important; }
@media print { .gate { display: none !important; } }
