/*
 * Stories — one stylesheet for the whole site.
 *
 * No framework and no build step. Caddy serves these files straight off /srv,
 * so what is written here is what ships; the only "build" is a publish rsync.
 *
 * The type scale is set for reading a novel, not for a dashboard: the reader's
 * measure is capped at 34rem (roughly 65 characters at the default size),
 * because line length is the single biggest lever on reading comfort and every
 * other choice here is downstream of it.
 */

:root {
  color-scheme: light dark;

  --bg: #fbfaf7;
  --bg-raised: #ffffff;
  --bg-sunken: #f2f0ea;
  --text: #1b1a17;
  --text-dim: #5f5b52;
  --text-faint: #8b857a;
  --line: #e2ded4;
  --line-strong: #cfc9bb;
  --accent: #7a4a2b;
  --accent-text: #ffffff;
  --focus: #3d6fd6;
  --warn-bg: #fdf3e3;
  --warn-line: #e8c98a;

  --measure: 34rem;
  --radius: 10px;
  --font-ui: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  --font-read: Iowan Old Style, "Palatino Linotype", Palatino, Georgia, "Times New Roman", serif;

  /* Overridden per-reader from localStorage by the reader page. */
  --reader-size: 20px;
  --reader-leading: 1.75;
}

[data-theme="dark"] {
  --bg: #16151a;
  --bg-raised: #1e1d23;
  --bg-sunken: #111014;
  --text: #e8e5df;
  --text-dim: #a9a49a;
  --text-faint: #7c776d;
  --line: #2e2c34;
  --line-strong: #423f49;
  --accent: #d9a479;
  --accent-text: #1b1a17;
  --focus: #7fa5ee;
  --warn-bg: #2a2318;
  --warn-line: #6b5730;
}

* { box-sizing: border-box; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-family: var(--font-ui);
  font-size: 16px;
  line-height: 1.55;
  -webkit-text-size-adjust: 100%;
}

a { color: inherit; text-decoration-color: var(--line-strong); text-underline-offset: 2px; }
a:hover { text-decoration-color: currentColor; }
:focus-visible { outline: 2px solid var(--focus); outline-offset: 2px; border-radius: 3px; }

img { max-width: 100%; }

/* ---------------- chrome ---------------- */

.site-header {
  border-bottom: 1px solid var(--line);
  background: var(--bg-raised);
  position: sticky;
  top: 0;
  z-index: 20;
}
.site-header .inner {
  max-width: 62rem;
  margin: 0 auto;
  padding: 0.7rem 1.2rem;
  display: flex;
  align-items: center;
  gap: 1.2rem;
}
.brand {
  font-weight: 700;
  letter-spacing: -0.01em;
  text-decoration: none;
  font-size: 1.05rem;
}
.brand span { color: var(--accent); }
.site-header nav { margin-left: auto; display: flex; gap: 1rem; align-items: center; font-size: 0.9rem; }
.site-header nav a { text-decoration: none; color: var(--text-dim); }
.site-header nav a:hover, .site-header nav a[aria-current="page"] { color: var(--text); }

main { max-width: 62rem; margin: 0 auto; padding: 2rem 1.2rem 5rem; }
main.narrow { max-width: 42rem; }

footer.site-footer {
  border-top: 1px solid var(--line);
  color: var(--text-faint);
  font-size: 0.85rem;
  padding: 2rem 1.2rem;
  text-align: center;
}
footer.site-footer a { color: inherit; }

h1 { font-size: 1.9rem; line-height: 1.2; letter-spacing: -0.02em; margin: 0 0 0.4rem; }
h2 { font-size: 1.2rem; margin: 2.2rem 0 0.8rem; letter-spacing: -0.01em; }
.lede { color: var(--text-dim); margin: 0 0 2rem; max-width: 40rem; }

/* ---------------- controls ---------------- */

button, .btn {
  font: inherit;
  font-size: 0.9rem;
  padding: 0.45rem 0.9rem;
  border-radius: var(--radius);
  border: 1px solid var(--line-strong);
  background: var(--bg-raised);
  color: var(--text);
  cursor: pointer;
  text-decoration: none;
  display: inline-flex;
  align-items: center;
  gap: 0.4rem;
}
button:hover, .btn:hover { border-color: var(--text-faint); }
button:disabled { opacity: 0.5; cursor: default; }
button.primary, .btn.primary {
  background: var(--accent);
  border-color: var(--accent);
  color: var(--accent-text);
  font-weight: 600;
}
button.ghost { border-color: transparent; background: transparent; color: var(--text-dim); }

input, select, textarea {
  font: inherit;
  font-size: 0.95rem;
  padding: 0.5rem 0.65rem;
  border: 1px solid var(--line-strong);
  border-radius: var(--radius);
  background: var(--bg-raised);
  color: var(--text);
  width: 100%;
}
textarea { min-height: 12rem; font-family: ui-monospace, SFMono-Regular, Menlo, monospace; font-size: 0.85rem; line-height: 1.6; }
label { display: block; font-size: 0.8rem; color: var(--text-dim); margin: 0 0 0.25rem; }
.field { margin: 0 0 1rem; }
.row { display: flex; gap: 0.8rem; flex-wrap: wrap; align-items: flex-end; }
.row > * { flex: 1 1 12rem; }

.msg { padding: 0.7rem 0.9rem; border-radius: var(--radius); font-size: 0.9rem; margin: 0 0 1rem; }
.msg.error { background: var(--warn-bg); border: 1px solid var(--warn-line); }
.msg.ok { background: var(--bg-sunken); border: 1px solid var(--line); }
.msg:empty { display: none; }

/* ---------------- browse ---------------- */

.filters { display: flex; gap: 0.6rem; flex-wrap: wrap; margin: 0 0 1.5rem; }
.filters input, .filters select { width: auto; flex: 0 1 auto; min-width: 8rem; }

.story-grid { display: grid; gap: 1rem; grid-template-columns: repeat(auto-fill, minmax(16rem, 1fr)); }
.story-card {
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: var(--bg-raised);
  padding: 1rem 1.1rem;
  display: flex;
  flex-direction: column;
  gap: 0.4rem;
  text-decoration: none;
}
.story-card:hover { border-color: var(--line-strong); }
.story-card h3 { margin: 0; font-size: 1.05rem; letter-spacing: -0.01em; }
.story-card .by { color: var(--text-dim); font-size: 0.85rem; }
.story-card p { margin: 0.2rem 0 0; color: var(--text-dim); font-size: 0.9rem; }
.meta { color: var(--text-faint); font-size: 0.8rem; display: flex; gap: 0.6rem; flex-wrap: wrap; margin-top: auto; padding-top: 0.6rem; }

.tag {
  display: inline-block;
  font-size: 0.72rem;
  padding: 0.1rem 0.45rem;
  border: 1px solid var(--line);
  border-radius: 99px;
  color: var(--text-dim);
}

.pager { display: flex; gap: 0.6rem; justify-content: center; margin: 2rem 0 0; align-items: center; }
.empty { color: var(--text-faint); padding: 3rem 0; text-align: center; }

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

.story-head { display: flex; gap: 1.5rem; flex-wrap: wrap; margin: 0 0 1.5rem; }
.story-head .cover {
  width: 11rem; aspect-ratio: 2 / 3; border-radius: var(--radius);
  background: var(--bg-sunken); border: 1px solid var(--line);
  object-fit: cover; flex: 0 0 auto;
}
.story-head .info { flex: 1 1 20rem; }

.chapter-list { list-style: none; padding: 0; margin: 0; border-top: 1px solid var(--line); }
.chapter-list li { border-bottom: 1px solid var(--line); }
.chapter-list a {
  display: flex; gap: 1rem; align-items: baseline;
  padding: 0.75rem 0.2rem; text-decoration: none;
}
.chapter-list a:hover { background: var(--bg-sunken); }
.chapter-list .n { color: var(--text-faint); font-variant-numeric: tabular-nums; min-width: 2.5rem; font-size: 0.85rem; }
.chapter-list .w { margin-left: auto; color: var(--text-faint); font-size: 0.8rem; }

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

.reader-bar {
  position: sticky; top: 0; z-index: 30;
  background: color-mix(in srgb, var(--bg-raised) 92%, transparent);
  backdrop-filter: saturate(1.4) blur(8px);
  border-bottom: 1px solid var(--line);
}
.reader-bar .inner {
  max-width: 52rem; margin: 0 auto; padding: 0.5rem 1rem;
  display: flex; align-items: center; gap: 0.6rem;
}
.reader-bar .where { font-size: 0.85rem; color: var(--text-dim); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.reader-bar .tools { margin-left: auto; display: flex; gap: 0.35rem; align-items: center; }
.reader-bar button { padding: 0.3rem 0.55rem; font-size: 0.85rem; }

.reading {
  max-width: var(--measure);
  margin: 0 auto;
  padding: 2.5rem 1.25rem 3rem;
  font-family: var(--font-read);
  font-size: var(--reader-size);
  line-height: var(--reader-leading);
}
.reading h1 { font-family: var(--font-read); font-size: 1.6em; line-height: 1.25; margin: 0 0 0.2em; }
.reading .chapter-of { font-family: var(--font-ui); font-size: 0.8rem; color: var(--text-faint); margin: 0 0 2.5em; text-transform: uppercase; letter-spacing: 0.06em; }
.reading p { margin: 0 0 1.15em; hyphens: auto; }
.reading h3, .reading h4, .reading h5 { font-size: 1.05em; margin: 2em 0 0.6em; }
.reading blockquote {
  margin: 1.5em 0; padding: 0 0 0 1.2em;
  border-left: 2px solid var(--line-strong); color: var(--text-dim); font-style: italic;
}
.reading hr { border: 0; text-align: center; margin: 2.2em 0; }
.reading hr::before { content: "* * *"; color: var(--text-faint); letter-spacing: 0.5em; }
.reading img { border-radius: var(--radius); margin: 1.5em 0; }
.reading code { font-family: ui-monospace, monospace; font-size: 0.85em; background: var(--bg-sunken); padding: 0.1em 0.3em; border-radius: 4px; }

.chapter-nav {
  max-width: var(--measure); margin: 0 auto; padding: 0 1.25rem 4rem;
  display: flex; gap: 0.8rem; justify-content: space-between;
}
.chapter-nav .btn { flex: 0 1 auto; }
.chapter-nav .spacer { flex: 1 1 auto; }

.progress-rail { position: fixed; left: 0; top: 0; height: 2px; background: var(--accent); width: 0; z-index: 40; transition: width 0.1s linear; }

/*
 * AD SLOTS.
 *
 * Reserved here and only here: between the end of a chapter and its navigation,
 * and on the browse page between rows. Never inside .reading's paragraph flow.
 *
 * That is not a taste decision. An ad injected mid-paragraph is the single
 * fastest way to make a reader leave, which destroys the dwell time the whole
 * payout model is measured in — the ad would be cannibalising the metric that
 * pays for the story it interrupted. The height is reserved up front so the
 * text never reflows when a unit loads, because a layout shift mid-sentence
 * costs a reader too.
 */
.ad-slot {
  max-width: var(--measure);
  margin: 2.5rem auto;
  min-height: 250px;
  border: 1px dashed var(--line);
  border-radius: var(--radius);
  background: var(--bg-sunken);
  display: flex;
  align-items: center;
  justify-content: center;
  color: var(--text-faint);
  font-size: 0.75rem;
  letter-spacing: 0.08em;
  text-transform: uppercase;
}
.ad-slot[data-filled="true"] { border: 0; background: none; min-height: 0; display: block; }

/* ---------------- library / dashboard tables ---------------- */

table { width: 100%; border-collapse: collapse; font-size: 0.9rem; }
th, td { text-align: left; padding: 0.55rem 0.6rem; border-bottom: 1px solid var(--line); }
th { color: var(--text-dim); font-weight: 600; font-size: 0.8rem; }
td.num, th.num { text-align: right; font-variant-numeric: tabular-nums; }

.stat-row { display: flex; gap: 1.5rem; flex-wrap: wrap; margin: 0 0 1.5rem; }
.stat { border: 1px solid var(--line); border-radius: var(--radius); padding: 0.8rem 1rem; background: var(--bg-raised); min-width: 9rem; }
.stat .k { font-size: 0.75rem; color: var(--text-dim); text-transform: uppercase; letter-spacing: 0.05em; }
.stat .v { font-size: 1.4rem; font-variant-numeric: tabular-nums; }
.stat .note { font-size: 0.75rem; color: var(--text-faint); }

.panel { border: 1px solid var(--line); border-radius: var(--radius); background: var(--bg-raised); padding: 1.2rem; margin: 0 0 1.5rem; }
.panel h2 { margin-top: 0; }

code.inline { font-family: ui-monospace, monospace; font-size: 0.85em; background: var(--bg-sunken); padding: 0.15em 0.4em; border-radius: 4px; word-break: break-all; }

@media (max-width: 640px) {
  h1 { font-size: 1.5rem; }
  .site-header nav { gap: 0.7rem; font-size: 0.85rem; }
  .story-head .cover { width: 7rem; }
}

@media print {
  .site-header, .reader-bar, .chapter-nav, .ad-slot, footer.site-footer { display: none; }
}
