/* ---------- tokens ---------- */

:root {
  --bg: #fbf8f3;
  --surface: #ffffff;
  --surface-2: #f3eee6;
  --text: #1c1917;
  --muted: #6b625a;
  --line: #e4ddd2;
  --accent: #b4532a;
  --accent-ink: #ffffff;
  --focus: #2563eb;
  --r13: #2f7d5b;
  --r16: #b7791f;
  --r18: #b42a3a;
  --shadow: 0 1px 2px rgb(28 25 23 / .06), 0 6px 20px rgb(28 25 23 / .06);

  --font-ui: 'Inter', system-ui, -apple-system, 'Segoe UI', sans-serif;
  --font-display: 'Fraunces', Georgia, serif;
  --font-read: 'Literata', Georgia, 'Times New Roman', serif;

  --size-step: 0;
  --read-size: calc(1.15rem * (1 + var(--size-step) * .1));
  --measure: 38rem;
  --gutter: clamp(16px, 4vw, 40px);
  --radius: 14px;
  color-scheme: light;
}

:root[data-theme="sepia"] {
  --bg: #f4ecd8;
  --surface: #fbf5e6;
  --surface-2: #ebe0c6;
  --text: #3b2f22;
  --muted: #7a6a55;
  --line: #ddd0b2;
  --accent: #9c4a21;
  color-scheme: light;
}

:root[data-theme="dark"] {
  --bg: #141210;
  --surface: #1e1b18;
  --surface-2: #29251f;
  --text: #ece6dd;
  --muted: #a39a8f;
  --line: #36312a;
  --accent: #e08a5c;
  --accent-ink: #1a1410;
  --focus: #7aa7ff;
  --r13: #5cc195;
  --r16: #e0b04f;
  --r18: #f07583;
  --shadow: 0 1px 2px rgb(0 0 0 / .3), 0 8px 24px rgb(0 0 0 / .25);
  color-scheme: dark;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme]) {
    --bg: #141210; --surface: #1e1b18; --surface-2: #29251f; --text: #ece6dd;
    --muted: #a39a8f; --line: #36312a; --accent: #e08a5c; --accent-ink: #1a1410;
    color-scheme: dark;
  }
}

:root[data-font="sans"] { --font-read: 'Inter', system-ui, sans-serif; }
:root[data-width="narrow"] { --measure: 32rem; }
:root[data-width="wide"] { --measure: 46rem; }

/* ---------- base ---------- */

*, *::before, *::after { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; scroll-padding-top: 80px; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 400 1rem/1.55 var(--font-ui);
  min-height: 100vh;
  display: flex;
  flex-direction: column;
  transition: background-color .25s, color .25s;
}
main:focus, main:focus-visible { outline: none; }
main { flex: 1; width: 100%; max-width: 1180px; margin: 0 auto; padding: 0 var(--gutter) 64px; outline: none; }
a { color: var(--accent); text-underline-offset: 3px; }
h1, h2, h3 { font-family: var(--font-display); font-weight: 700; line-height: 1.15; margin: 0; letter-spacing: -.01em; }
button { font: inherit; color: inherit; }
:focus-visible { outline: 2px solid var(--focus); outline-offset: 2px; border-radius: 6px; }
svg { width: 22px; height: 22px; fill: none; stroke: currentColor; stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round; }

.skip { position: absolute; left: -999px; top: 8px; background: var(--surface); padding: 8px 12px; border-radius: 8px; z-index: 50; }
.skip:focus { left: 8px; }

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

.topbar {
  position: sticky; top: 0; z-index: 20;
  display: flex; align-items: center; gap: 16px;
  padding: 12px var(--gutter);
  background: color-mix(in srgb, var(--bg) 86%, transparent);
  backdrop-filter: blur(12px);
  -webkit-backdrop-filter: blur(12px);
  border-bottom: 1px solid var(--line);
}
.brand { display: flex; align-items: center; gap: 10px; text-decoration: none; color: var(--text); font: 700 1.2rem var(--font-display); }
.brand svg { width: 30px; height: 30px; stroke: none; }
.brand svg rect { fill: var(--accent); }
.brand svg path { fill: var(--bg); }
.nav { display: flex; gap: 4px; margin-left: auto; }
.nav a { color: var(--muted); text-decoration: none; padding: 8px 12px; border-radius: 999px; font-weight: 500; font-size: .95rem; }
.nav a:hover { color: var(--text); background: var(--surface-2); }
.nav a[aria-current="page"] { color: var(--text); background: var(--surface-2); }
.icon-btn { display: grid; place-items: center; width: 40px; height: 40px; border-radius: 999px; border: 1px solid var(--line); background: var(--surface); cursor: pointer; flex: none; }
.icon-btn:hover { background: var(--surface-2); }

@media (max-width: 560px) {
  .brand span { display: none; }
  .nav { margin-left: 0; flex: 1; justify-content: center; }
  .nav a { padding: 8px 10px; font-size: .9rem; }
}

.progress { position: fixed; top: 0; left: 0; right: 0; height: 3px; z-index: 30; opacity: 0; transition: opacity .2s; }
.progress.on { opacity: 1; }
.progress span { display: block; height: 100%; width: calc(var(--p, 0) * 100%); background: var(--accent); }

/* ---------- buttons, badges ---------- */

.btn {
  display: inline-flex; align-items: center; gap: 8px;
  padding: 11px 18px; border-radius: 999px;
  background: var(--accent); color: var(--accent-ink);
  border: 1px solid var(--accent);
  font-weight: 600; font-size: .95rem; text-decoration: none; cursor: pointer;
  transition: transform .15s, background-color .15s;
}
.btn:hover { transform: translateY(-1px); }
.btn.ghost { background: transparent; color: var(--text); border-color: var(--line); }
.btn.ghost:hover { background: var(--surface-2); }
.btn.ghost[aria-pressed="true"] { border-color: var(--accent); color: var(--accent); }
.btn.danger { color: var(--r18); }
.heart { font-size: 1.05em; }

.badge { display: inline-block; font: 600 .72rem/1 var(--font-ui); padding: 4px 7px; border-radius: 6px; letter-spacing: .02em; }
.rating.r13 { color: var(--r13); background: color-mix(in srgb, var(--r13) 14%, transparent); }
.rating.r16 { color: var(--r16); background: color-mix(in srgb, var(--r16) 14%, transparent); }
.rating.r18 { color: var(--r18); background: color-mix(in srgb, var(--r18) 14%, transparent); }
.meta-dot { color: var(--muted); font-size: .85rem; }
.eyebrow { text-transform: uppercase; letter-spacing: .12em; font-size: .75rem; font-weight: 600; color: var(--accent); margin: 0 0 10px; }
.lede { color: var(--muted); font-size: 1.08rem; max-width: 40rem; }
.hint { color: var(--muted); font-size: .85rem; margin: 8px 0 0; }
.empty { color: var(--muted); padding: 32px 0; }

/* ---------- covers ---------- */

.cover {
  position: relative; overflow: hidden;
  aspect-ratio: 2 / 3;
  border-radius: 10px;
  background:
    radial-gradient(120% 80% at 100% 0%, color-mix(in srgb, var(--to) 70%, transparent), transparent 60%),
    linear-gradient(160deg, var(--from), color-mix(in srgb, var(--from) 60%, var(--to)));
  color: var(--ink);
  display: flex; flex-direction: column; justify-content: flex-end;
  padding: 14px;
  box-shadow: var(--shadow), inset 0 0 0 1px rgb(255 255 255 / .06);
}
.cover::before { /* spine */
  content: ''; position: absolute; inset: 0 auto 0 0; width: 8px;
  background: linear-gradient(90deg, rgb(0 0 0 / .25), transparent);
}
.cover::after { /* grain */
  content: ''; position: absolute; inset: 0; opacity: .18; mix-blend-mode: overlay;
  background-image: repeating-linear-gradient(45deg, rgb(255 255 255 / .2) 0 1px, transparent 1px 4px);
}
.cover-genre { position: absolute; top: 14px; left: 18px; font: 600 .62rem var(--font-ui); letter-spacing: .16em; text-transform: uppercase; opacity: .8; }
.cover-title { position: relative; z-index: 1; font: 700 1.15rem/1.08 var(--font-display); }
.cover-rule { position: relative; z-index: 1; display: block; width: 28px; height: 2px; background: currentColor; opacity: .6; margin-top: 10px; }
.cover-lg .cover-title { font-size: clamp(1.4rem, 3vw, 1.9rem); }
.cover-lg { padding: 20px; }
.cover-lg .cover-genre { top: 20px; left: 24px; }

/* ---------- cards & grids ---------- */

.grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(min(100%, 230px), 1fr)); gap: 28px 22px; }
.card { display: flex; flex-direction: column; gap: 12px; text-decoration: none; color: inherit; }
.card .cover { transition: transform .2s ease, box-shadow .2s; }
.card:hover .cover { transform: translateY(-4px) rotate(-.4deg); }
.card-body h3 { font-size: 1.12rem; display: flex; align-items: baseline; gap: 6px; }
.fav-mark { color: var(--accent); font-size: .9rem; }
.card-meta { display: flex; align-items: center; gap: 10px; margin: 6px 0; }
.card-sum { color: var(--muted); font-size: .9rem; margin: 0; display: -webkit-box; -webkit-line-clamp: 3; -webkit-box-orient: vertical; overflow: hidden; }
.bar { display: block; height: 4px; background: var(--surface-2); border-radius: 4px; margin-top: 10px; overflow: hidden; }
.bar span { display: block; height: 100%; width: calc(var(--p) * 100%); background: var(--accent); }
.status.done { display: inline-block; margin-top: 8px; font-size: .78rem; font-weight: 600; color: var(--r13); }

.shelf-row { margin-top: 48px; }
.row-head { display: flex; align-items: baseline; justify-content: space-between; margin-bottom: 18px; }
.row-head h2 { font-size: 1.45rem; }
.row-head a { font-size: .9rem; font-weight: 500; text-decoration: none; }
.row-scroll {
  display: grid; grid-auto-flow: column; grid-auto-columns: minmax(180px, 210px);
  gap: 20px; overflow-x: auto; scroll-snap-type: x mandatory;
  padding: 4px 4px 12px; margin: 0 -4px;
  scrollbar-width: thin;
}
.row-scroll > * { scroll-snap-align: start; }

/* ---------- home hero ---------- */

.hero {
  display: grid; grid-template-columns: 1.15fr 1fr; gap: 40px; align-items: center;
  padding: clamp(32px, 7vw, 80px) 0 24px;
}
.hero h1 { font-size: clamp(2.2rem, 5.5vw, 3.8rem); margin-bottom: 18px; }
.hero-actions { display: flex; flex-wrap: wrap; gap: 12px; margin-top: 26px; }
.hero-stack { position: relative; height: 380px; }
.hero-stack .cover { position: absolute; width: 210px; }
.hero-stack .cover:nth-child(1) { left: 8%; top: 30px; transform: rotate(-7deg); z-index: 1; }
.hero-stack .cover:nth-child(2) { left: 32%; top: 0; transform: rotate(2deg); z-index: 3; }
.hero-stack .cover:nth-child(3) { left: 56%; top: 46px; transform: rotate(9deg); z-index: 2; }
@media (max-width: 820px) {
  .hero { grid-template-columns: 1fr; }
  .hero-stack { height: 250px; order: -1; }
  .hero-stack .cover { width: 140px; }
}

/* ---------- library ---------- */

.page-head { padding: clamp(28px, 6vw, 56px) 0 8px; }
.page-head h1 { font-size: clamp(2rem, 5vw, 3rem); margin-bottom: 10px; }
.filters { display: flex; flex-direction: column; gap: 16px; margin: 20px 0 32px; }
.search { display: flex; align-items: center; gap: 10px; background: var(--surface); border: 1px solid var(--line); border-radius: 999px; padding: 0 16px; max-width: 520px; }
.search:focus-within { border-color: var(--accent); }
.search svg { color: var(--muted); flex: none; width: 20px; height: 20px; }
.search input { flex: 1; min-width: 0; border: 0; background: transparent; padding: 12px 0; font: inherit; color: var(--text); outline: none; }
.chips { display: flex; flex-wrap: wrap; gap: 8px; }
.chip { padding: 7px 14px; border-radius: 999px; border: 1px solid var(--line); background: var(--surface); cursor: pointer; font-size: .9rem; font-weight: 500; }
.chip:hover { background: var(--surface-2); }
.chip[aria-pressed="true"] { background: var(--text); color: var(--bg); border-color: var(--text); }
.filter-row { display: flex; flex-wrap: wrap; gap: 14px; align-items: center; justify-content: space-between; }
.select { display: flex; align-items: center; gap: 8px; font-size: .9rem; color: var(--muted); }
.select select { font: inherit; color: var(--text); background: var(--surface); border: 1px solid var(--line); border-radius: 10px; padding: 8px 10px; }

/* segmented control (used in library + settings) */
.seg { display: inline-flex; background: var(--surface-2); border-radius: 999px; padding: 3px; gap: 2px; flex-wrap: wrap; }
.seg button { border: 0; background: transparent; padding: 7px 14px; border-radius: 999px; cursor: pointer; font-size: .88rem; font-weight: 500; color: var(--muted); }
.seg button[aria-pressed="true"] { background: var(--surface); color: var(--text); box-shadow: var(--shadow); }
.seg.small button { padding: 6px 12px; font-size: .84rem; }

/* ---------- shelf ---------- */

.tabs { display: flex; gap: 6px; border-bottom: 1px solid var(--line); margin: 20px 0 28px; overflow-x: auto; }
.tabs a { padding: 10px 14px; color: var(--muted); text-decoration: none; font-weight: 500; border-bottom: 2px solid transparent; margin-bottom: -1px; white-space: nowrap; }
.tabs a span { margin-left: 8px; font-size: .78rem; background: var(--surface-2); padding: 2px 7px; border-radius: 999px; }
.tabs a[aria-current="page"] { color: var(--text); border-color: var(--accent); }

/* ---------- reader ---------- */

.story-head {
  display: grid; grid-template-columns: 220px 1fr; gap: 36px; align-items: end;
  padding: clamp(28px, 6vw, 56px) 0 40px;
  border-bottom: 1px solid var(--line);
  max-width: 60rem; margin: 0 auto;
}
.story-head h1 { font-size: clamp(2.1rem, 5vw, 3.4rem); }
.byline { color: var(--muted); margin: 8px 0 4px; font-style: italic; }
.story-sum { font-size: 1.05rem; max-width: 36rem; margin: 10px 0 0; }
.warnings { margin-top: 14px; font-size: .9rem; color: var(--muted); }
.warnings summary { cursor: pointer; font-weight: 500; }
.warnings p { margin: 6px 0 0; }
.story-actions { display: flex; flex-wrap: wrap; gap: 10px; margin-top: 20px; }
@media (max-width: 640px) {
  .story-head { grid-template-columns: 1fr; gap: 24px; }
  .story-head .cover { width: 150px; }
}

.prose {
  max-width: var(--measure); margin: 48px auto 0;
  font-family: var(--font-read);
  font-size: var(--read-size);
  line-height: 1.75;
  hanging-punctuation: first;
  font-optical-sizing: auto;
}
.prose p { margin: 0 0 1.1em; }
.prose > p:first-child::first-letter {
  float: left; font-family: var(--font-display); font-weight: 700;
  font-size: 3.4em; line-height: .85; padding: .08em .1em 0 0; color: var(--accent);
}
.prose h2, .prose h3 { font-size: 1.3em; margin: 1.6em 0 .6em; }
.prose blockquote { margin: 1.4em 0; padding-left: 1.2em; border-left: 3px solid var(--line); color: var(--muted); font-style: italic; }
.prose strong { font-weight: 600; }
.prose hr.scene-break { border: 0; margin: 2.2em 0; text-align: center; height: 1em; }
.prose hr.scene-break::before { content: '•   •   •'; white-space: pre; color: var(--muted); font-size: .9em; }

.the-end { max-width: 60rem; margin: 64px auto 0; padding-top: 40px; border-top: 1px solid var(--line); }
.end-mark { text-align: center; font: italic 500 1.3rem var(--font-display); color: var(--muted); margin: 0 0 40px; }
.the-end h2 { font-size: 1.4rem; margin-bottom: 18px; }
.the-end > p:last-child { margin-top: 32px; }

.loading { padding: 80px 0; text-align: center; color: var(--muted); }

/* ---------- gate ---------- */

.gate { display: grid; grid-template-columns: 220px 1fr; gap: 40px; align-items: center; padding: clamp(40px, 8vw, 90px) 0; max-width: 54rem; margin: 0 auto; }
.gate h1 { font-size: clamp(2rem, 5vw, 3rem); margin-bottom: 12px; }
.gate .eyebrow { color: var(--r18); }
@media (max-width: 640px) { .gate { grid-template-columns: 1fr; } .gate .cover { width: 150px; } }

/* ---------- settings dialog ---------- */

dialog {
  border: 1px solid var(--line); border-radius: 18px; padding: 0;
  background: var(--surface); color: var(--text);
  width: min(420px, calc(100vw - 32px));
  max-height: calc(100vh - 32px);
  box-shadow: 0 24px 60px rgb(0 0 0 / .25);
}
dialog::backdrop { background: rgb(15 12 10 / .45); backdrop-filter: blur(2px); }
.settings { padding: 22px 22px 26px; }
.settings-head { display: flex; align-items: center; justify-content: space-between; margin-bottom: 8px; }
.settings-head h2 { font-size: 1.3rem; }
fieldset { border: 0; padding: 0; margin: 18px 0 0; }
legend { font-size: .78rem; font-weight: 600; text-transform: uppercase; letter-spacing: .1em; color: var(--muted); margin-bottom: 10px; }
.stepper { display: flex; align-items: center; gap: 12px; }
.stepper button { width: 44px; height: 38px; border-radius: 10px; border: 1px solid var(--line); background: var(--surface-2); cursor: pointer; font-weight: 600; }
.stepper output { min-width: 4ch; text-align: center; font-variant-numeric: tabular-nums; }
.toggle { display: flex; align-items: center; gap: 10px; cursor: pointer; font-weight: 500; }
.toggle input { width: 18px; height: 18px; accent-color: var(--accent); }

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

.foot { text-align: center; color: var(--muted); font-size: .82rem; padding: 28px var(--gutter) 36px; border-top: 1px solid var(--line); }
.foot p { margin: 0; }

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { transition: none !important; scroll-behavior: auto !important; }
}
