:root {
  --bg: #f4f1ea;
  --panel: #fbfaf6;
  --ink: #23201c;
  --muted: #6e6a62;
  --line: #e3ded2;
  --accent: #7a3b2e;
  --accent-soft: #f1e4dc;
  --sel: #f7e7b0;
  --legend: #6b4f9a;
  --shadow: 0 1px 2px rgba(30, 25, 20, .06), 0 8px 24px rgba(30, 25, 20, .08);
  --read-size: 19px;
  --serif: "Source Serif 4", "Noto Serif", "Noto Serif SC", Georgia, serif;
  --sans: "IBM Plex Sans", system-ui, -apple-system, "PingFang SC", "Microsoft YaHei", sans-serif;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #131416; --panel: #1b1c1f; --ink: #ebe7df; --muted: #a09b92; --line: #2f3034;
    --accent: #e3a58f; --accent-soft: #33261f; --sel: #4a3f1f; --legend: #b9a2e6; --shadow: 0 8px 24px rgba(0, 0, 0, .4);
    color-scheme: dark;
  }
}
:root[data-theme="dark"] {
  --bg: #131416; --panel: #1b1c1f; --ink: #ebe7df; --muted: #a09b92; --line: #2f3034;
  --accent: #e3a58f; --accent-soft: #33261f; --sel: #4a3f1f; --legend: #b9a2e6; --shadow: 0 8px 24px rgba(0, 0, 0, .4);
  color-scheme: dark;
}
/* Sepia: a warm light page */
:root[data-page="sepia"] {
  --bg: #efe4cc; --panel: #f7efdc; --ink: #3a2e22; --muted: #7a6a58; --line: #e0d1b3;
  --accent: #7a4b2a; --accent-soft: #ecdcc0; --sel: #f0d68f;
}

* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body {
  background: var(--bg); color: var(--ink);
  font: 15px/1.5 var(--sans);
  display: flex; flex-direction: column; overflow: hidden;
}
:lang(zh-Hans) body, html:lang(zh-Hans) { --sans: "IBM Plex Sans", "PingFang SC", "Noto Sans SC", "Microsoft YaHei", system-ui, sans-serif; }
button { font: inherit; color: inherit; cursor: pointer; }
a { color: var(--accent); }
[hidden] { display: none !important; }
.muted { color: var(--muted); }
.small { font-size: 13px; }

/* Top bar */
.bar {
  display: flex; align-items: center; gap: 6px; padding: 8px 12px; min-height: 52px;
  background: var(--panel); border-bottom: 1px solid var(--line); flex: none;
}
.brand { display: flex; align-items: center; gap: 8px; background: none; border: 0; padding: 4px 6px; color: var(--accent); border-radius: 8px; }
.brand-name { font: 600 16px/1 var(--serif); color: var(--ink); white-space: nowrap; }
.brand:hover { background: var(--accent-soft); }
.work-nav { display: flex; align-items: center; gap: 2px; min-width: 0; margin-left: 6px; }
.title-btn { background: none; border: 1px solid var(--line); border-radius: 8px; padding: 5px 10px; display: flex; gap: 6px; align-items: center; min-width: 0; }
.title-btn span:first-child { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-weight: 500; }
.caret { color: var(--muted); font-size: 12px; }
.spacer { flex: 1; }
.icon { background: none; border: 0; width: 36px; height: 36px; border-radius: 8px; display: grid; place-items: center; font-size: 20px; line-height: 1; color: var(--ink); flex: none; }
.icon:hover:not(:disabled) { background: var(--accent-soft); }
.icon:disabled { opacity: .3; cursor: default; }
.icon.lang { font-size: 14px; font-weight: 600; }
.settings-wrap { position: relative; }
.settings {
  position: absolute; right: 0; top: 42px; z-index: 30; width: 290px; padding: 12px 14px;
  background: var(--panel); border: 1px solid var(--line); border-radius: 12px; box-shadow: var(--shadow);
}
.set-row { display: flex; align-items: center; justify-content: space-between; gap: 10px; margin: 6px 0; }
.set-row.check { justify-content: flex-start; cursor: pointer; }
.seg { display: flex; border: 1px solid var(--line); border-radius: 8px; overflow: hidden; }
.seg button { background: none; border: 0; padding: 4px 9px; font-size: 13px; }
.seg button + button { border-left: 1px solid var(--line); }
.seg button[aria-pressed="true"] { background: var(--accent); color: var(--panel); }

main { flex: 1; min-height: 0; display: flex; }
main > section { flex: 1; overflow-y: auto; }

/* Shelf */
.shelf { padding: 40px 20px 60px; }
.shelf-head { max-width: 760px; margin: 0 auto 28px; text-align: center; }
.shelf-head h1 { font: 600 30px/1.25 var(--serif); margin: 0 0 10px; }
.shelf-head p { color: var(--muted); margin: 0; font-size: 16px; }
.cards { max-width: 980px; margin: 0 auto; display: grid; grid-template-columns: repeat(auto-fill, minmax(210px, 1fr)); gap: 16px; }
.card {
  display: flex; flex-direction: column; gap: 4px; min-height: 230px; padding: 18px 18px 16px; text-decoration: none; color: var(--ink);
  background: var(--panel); border: 1px solid var(--line); border-radius: 14px; box-shadow: var(--shadow);
  border-top: 6px solid var(--accent); transition: transform .15s;
}
a.card:hover { transform: translateY(-2px); }
.card.soon { opacity: .62; border-top-color: var(--line); box-shadow: none; }
.card-form { font-size: 12px; letter-spacing: .06em; text-transform: uppercase; color: var(--muted); }
.card-title { font: 600 24px/1.2 var(--serif); margin-top: 6px; }
.card-sub { font: 17px/1.3 var(--serif); color: var(--muted); }
.card-author { margin-top: auto; font-size: 14px; }
.card-cta { font-size: 13px; font-weight: 600; color: var(--accent); }
.card.soon .card-cta { color: var(--muted); font-weight: 500; }

/* A work's front page */
.front { padding: 40px 20px 80px; }
.front-inner { max-width: 680px; margin: 0 auto; }
.front-form { color: var(--muted); font-size: 13px; letter-spacing: .05em; text-transform: uppercase; margin: 0; }
.front h1 { font: 600 40px/1.15 var(--serif); margin: 8px 0 4px; }
.front h1 small { font-size: 26px; color: var(--muted); font-weight: 400; margin-left: 8px; }
.front-author { font: italic 19px var(--serif); margin: 0 0 20px; }
.front-actions { display: flex; align-items: center; gap: 14px; flex-wrap: wrap; margin-bottom: 24px; }
.front-intro p { font: var(--read-size)/1.65 var(--serif); margin: 0 0 1em; }
.front h2 { font: 600 20px var(--serif); margin: 32px 0 8px; }
.book-list { padding-left: 0; list-style: none; columns: 2 260px; column-gap: 24px; margin: 0; }
.book-list li { break-inside: avoid; padding: 5px 0; border-bottom: 1px solid var(--line); }
.book-list a { text-decoration: none; color: var(--ink); }
.book-list a:hover { color: var(--accent); }
.pill {
  display: inline-flex; align-items: center; gap: 6px; padding: 8px 16px; border-radius: 999px; text-decoration: none;
  border: 1px solid var(--line); background: var(--panel); color: var(--ink); font-weight: 500;
}
.pill.primary { background: var(--accent); border-color: var(--accent); color: var(--panel); }
.credit, .reader p.credit { color: var(--muted); font: 13px/1.5 var(--sans); margin-top: 28px; }

/* The reading room: text left, context right */
.room { flex: 1; min-height: 0; display: grid; grid-template-columns: minmax(0, 1fr) minmax(320px, 420px); }
.reader { overflow-y: auto; padding: 32px max(24px, calc((100% - 700px) / 2)) 80px; }
.book-head { margin-bottom: 18px; }
.book-kicker { margin: 0; color: var(--muted); font-size: 13px; letter-spacing: .05em; text-transform: uppercase; }
.book-head h1 { font: 600 30px/1.2 var(--serif); margin: 4px 0 0; }
.book-head h1 small { display: block; font: italic 400 20px var(--serif); color: var(--muted); margin-top: 4px; }
.reader p { font: var(--read-size)/1.7 var(--serif); margin: 0 0 1em; hyphens: auto; }
.psg { cursor: pointer; border-radius: 3px; transition: background .12s; -webkit-box-decoration-break: clone; box-decoration-break: clone; }
.psg:hover { background: color-mix(in srgb, var(--sel) 45%, transparent); }
.psg.sel { background: var(--sel); }
.ln {
  display: inline-block; font: 500 11px/1 var(--sans); color: var(--muted); vertical-align: 2px; margin-right: 4px;
  min-width: 1.6em; text-decoration: none; user-select: none;
}
.term {
  font: italic 0.9em var(--serif); color: var(--accent); background: var(--accent-soft); border: 0;
  border-radius: 4px; padding: 0 4px; margin: 0 1px; cursor: pointer;
}
.book-foot { display: flex; justify-content: space-between; gap: 10px; margin-top: 30px; }
.psg.speaking { outline: 2px solid var(--accent); outline-offset: 2px; }
.setting { color: var(--muted); font-style: italic; margin: 6px 0 0 !important; }

/* Tools over the text: reading aid, tones, listen */
.tools { display: flex; gap: 6px; flex-wrap: wrap; margin-top: 12px; }
.chip { background: var(--panel); border: 1px solid var(--line); border-radius: 999px; padding: 3px 12px; font: 500 13px var(--sans); color: var(--muted); }
.chip[aria-pressed="true"] { background: var(--accent-soft); border-color: color-mix(in srgb, var(--accent) 35%, transparent); color: var(--accent); }

/* Chinese originals */
.reader:lang(zh-Hans) p, .reader:lang(zh-Hans) .vl { font-family: "Noto Serif SC", var(--serif); line-height: 1.9; hyphens: none; }
.aid { display: block; font: calc(var(--read-size) * .82)/1.55 var(--serif); color: var(--muted); margin: 4px 0 2px; }
.para .aid, .verse .aid { border-left: 2px solid var(--line); padding-left: 10px; margin-top: 8px; }
.aid:lang(en) { font-family: "Source Serif 4", var(--serif); }
.reader .para .ln, .reader .verse .ln { margin-right: 6px; }

/* A play: speeches under their speakers, stage directions in italics */
.speech { display: block; margin: 0 0 .7em; padding: 2px 4px 2px 0; }
.speech .sp { font: 600 13px/1.4 var(--sans); letter-spacing: .06em; text-transform: uppercase; color: var(--accent); margin: 0 0 2px !important; }
.speech .sp .alt { text-transform: none; letter-spacing: 0; font-weight: 400; }
.vl { display: block; font: var(--read-size)/1.6 var(--serif); position: relative; padding-left: 2.6em; }
.vl .ln { position: absolute; left: 0; top: .55em; }
.reader .sd { font-style: italic; color: var(--muted); margin: .4em 0 .8em 2.6em; font-size: calc(var(--read-size) * .88); }

/* A novel's verse paragraph */
.verse { display: block; margin: 0 0 1em; padding: 4px 0; }
.verse .vl { padding-left: 2em; }
.verse > .ln { float: left; margin-top: .55em; }

/* A poem: its lines, centred, with tones and rhymes */
.poem-head { text-align: center; }
.poem-head h1 { font: 600 34px/1.3 "Noto Serif SC", var(--serif); letter-spacing: .08em; }
.poem-by { margin: 6px 0 0 !important; color: var(--muted); }
.poem-en { margin: 2px 0 0 !important; font-style: italic; color: var(--muted); }
.poem-head .tools { justify-content: center; }
.poem { margin: 26px auto 0; width: fit-content; max-width: 100%; }
.pl { display: block; padding: 6px 10px; margin: 2px 0; text-align: center; }
.pl.st { margin-top: 18px; }
.pt { font: calc(var(--read-size) * 1.3)/1.8 "Noto Serif SC", var(--serif); letter-spacing: .12em; }
.tones .pt .z { text-emphasis: dot; -webkit-text-emphasis: dot; text-emphasis-position: under right; -webkit-text-emphasis-position: under; text-emphasis-color: var(--muted); }
.tones .pt .rh { color: var(--accent); font-weight: 600; }
.tones-key { text-align: center; max-width: 520px; margin: 18px auto 0 !important; font-family: var(--sans) !important; font-size: 13px !important; }
.summary { margin: 0 0 14px; }
.book-list.poems { columns: 1; }

/* Echoes and art */
.ecard { border-bottom: 1px solid var(--line); padding: 10px 0 12px; }
.ecard header { display: flex; align-items: baseline; gap: 8px; flex-wrap: wrap; }
.ecard header .tag { margin-left: 0; order: -1; }
.ecard b { font: 600 16px var(--serif); }
.ecard p { margin: 5px 0; }
.ecard.earlier .tag { color: var(--legend); }
.ecard.later .tag { color: var(--accent); }
.ecard.art img { display: block; width: 100%; max-height: 260px; object-fit: cover; border-radius: 8px; background: var(--bg); margin-bottom: 8px; }
.tag.speak { color: var(--accent); }
.pinyin { color: var(--muted); font-size: 14px; }
.aid-lines p { margin: 0 0 6px; font-family: var(--serif); }

.panel {
  border-left: 1px solid var(--line); background: var(--panel); display: flex; flex-direction: column; min-height: 0;
}
.panel-head { display: flex; align-items: center; gap: 8px; padding: 12px 14px 6px; }
.scope { flex: 1; display: flex; align-items: baseline; gap: 10px; flex-wrap: wrap; }
.scope b { font: 600 15px var(--sans); }
.text-btn { background: none; border: 0; padding: 0; color: var(--accent); font-size: 13px; }
.panel-x { display: none; }
.tabs { display: flex; gap: 2px; padding: 0 8px; border-bottom: 1px solid var(--line); overflow-x: auto; scrollbar-width: none; }
.tabs button { background: none; border: 0; padding: 8px 8px; white-space: nowrap; flex: none; border-bottom: 2px solid transparent; font-weight: 500; color: var(--muted); }
.tabs button[aria-selected="true"] { color: var(--ink); border-bottom-color: var(--accent); }
.tabs button i { font-style: normal; font-size: 12px; background: var(--accent-soft); color: var(--accent); border-radius: 999px; padding: 0 6px; margin-left: 2px; }
.tabs button.empty { opacity: .6; }
.tab-body { flex: 1; overflow-y: auto; padding: 12px 14px 40px; }
.hint { color: var(--muted); font-size: 13px; margin: 0 0 12px; }
.about { margin: 0 0 14px; border: 1px solid var(--line); border-radius: 10px; padding: 8px 12px; }
.about summary { cursor: pointer; font-weight: 500; }
.about .warn { color: var(--muted); font-size: 13px; }
.pcard, .plcard, .wcard { border-bottom: 1px solid var(--line); padding: 10px 0 12px; }
.pcard header, .plcard header, .wcard header { display: flex; align-items: baseline; gap: 8px; flex-wrap: wrap; }
.pcard b, .plcard b, .wcard b { font: 600 17px var(--serif); }
.alt { color: var(--muted); font-family: var(--serif); }
.tag { font-size: 11px; text-transform: uppercase; letter-spacing: .05em; color: var(--muted); border: 1px solid var(--line); border-radius: 999px; padding: 0 7px; margin-left: auto; }
.plcard.legend .tag { color: var(--legend); border-color: color-mix(in srgb, var(--legend) 40%, transparent); }
.role { margin: 4px 0; font-style: italic; }
.pcard p, .plcard p, .wcard p { margin: 5px 0; }
.note { display: flex; gap: 8px; }
.nb { flex: none; font: 600 11px/18px var(--sans); color: var(--muted); border: 1px solid var(--line); border-radius: 5px; min-width: 22px; text-align: center; height: 18px; margin-top: 2px; }
.greek, [lang="grc"] { font-family: "Noto Serif", "Source Serif 4", serif; }
.wcard .greek { font-size: 17px; color: var(--muted); }
.gloss { font-weight: 500; }
.orig { margin-bottom: 14px; }
.orig h3 { font: 600 14px var(--sans); margin: 4px 0 8px; display: flex; gap: 8px; align-items: baseline; }
.orig-lines { background: var(--bg); border-radius: 10px; padding: 10px 12px; max-height: 340px; overflow-y: auto; }
.gl { display: flex; gap: 10px; font-size: 16px; line-height: 1.55; }
.gl i { font: 11px/26px var(--sans); color: var(--muted); min-width: 2.2em; text-align: right; font-style: normal; }
.ai-note { color: var(--muted); font-size: 12px; margin-top: 16px; }
.map-box { position: relative; height: 300px; border-radius: 10px; overflow: hidden; background: var(--bg); margin-bottom: 10px; border: 1px solid var(--line); }
.map-box iframe { position: absolute; inset: 0; width: 100%; height: 100%; border: 0; opacity: 0; transition: opacity .3s; }
.map-box.live iframe { opacity: 1; }
.map-box.failed iframe { display: none; }
.map-msg { position: absolute; inset: 0; display: grid; place-items: center; text-align: center; padding: 20px; margin: 0; color: var(--muted); font-size: 13px; }
.map-box.live .map-msg { display: none; }

/* Book picker */
.books-dialog { border: 1px solid var(--line); border-radius: 14px; background: var(--panel); color: var(--ink); padding: 0; width: min(440px, 92vw); max-height: 80vh; box-shadow: var(--shadow); }
.books-dialog::backdrop { background: rgba(0, 0, 0, .3); }
.books-dialog header { display: flex; align-items: center; justify-content: space-between; padding: 10px 14px; border-bottom: 1px solid var(--line); font-family: var(--serif); font-size: 18px; position: sticky; top: 0; background: var(--panel); }
.books-dialog ol { list-style: none; margin: 0; padding: 6px 0; }
.books-dialog a { display: block; padding: 7px 16px; text-decoration: none; color: var(--ink); }
.books-dialog a:hover, .books-dialog a.on { background: var(--accent-soft); }

.toast { position: fixed; bottom: 24px; left: 50%; transform: translateX(-50%); background: var(--ink); color: var(--bg); padding: 8px 16px; border-radius: 999px; font-size: 14px; z-index: 50; }

/* Phones: the panel is a sheet that rises when a passage is selected */
@media (max-width: 820px) {
  .brand-name { display: none; }
  .room { grid-template-columns: 1fr; }
  .reader { padding: 20px 18px 50vh; }
  .panel {
    position: fixed; left: 0; right: 0; bottom: 0; height: 52vh; z-index: 20; border-left: 0; border-top: 1px solid var(--line);
    border-radius: 16px 16px 0 0; box-shadow: 0 -8px 24px rgba(0, 0, 0, .15); transform: translateY(105%); transition: transform .2s;
  }
  body.has-sel .panel { transform: none; }
  .panel-x { display: grid; }
  .map-box { height: 220px; }
  .front h1 { font-size: 32px; }
}
@media (max-width: 420px) {
  .work-nav .icon { display: none; }
}

/* The companion (Ask) */
.ask-modes { display: flex; flex-wrap: wrap; gap: 6px; margin-bottom: 10px; }
.ask-voice { display: flex; align-items: center; gap: 8px; font-size: 13px; color: var(--muted); margin-bottom: 10px; }
.ask-voice select { font: inherit; color: var(--ink); background: var(--panel); border: 1px solid var(--line); border-radius: 6px; padding: 2px 6px; }
.ask-log { display: flex; flex-direction: column; gap: 10px; margin-bottom: 10px; }
.msg { border-radius: 12px; padding: 8px 12px; }
.msg p { margin: 0 0 .6em; }
.msg p:last-child { margin-bottom: 0; }
.msg ul { margin: 0 0 .6em; padding-left: 1.2em; }
.msg.user { align-self: flex-end; max-width: 88%; background: var(--accent-soft); white-space: pre-wrap; }
.msg.assistant { background: var(--bg); font-family: var(--serif); font-size: 15.5px; line-height: 1.6; }
.msg.busy { color: var(--muted); font-style: italic; }
.msg.error { border: 1px solid var(--line); color: var(--muted); }
.msg q { quotes: "“" "”" "‘" "’"; }
.msg q.ok { background: color-mix(in srgb, var(--sel) 55%, transparent); border-radius: 3px; }
.msg q.aid { border-bottom: 1px dotted var(--muted); }
.msg q.no { text-decoration: underline wavy var(--accent); text-underline-offset: 3px; }
.msg q.no::after { content: " ⚠"; font-size: .8em; }
.ask-form textarea { width: 100%; font: 15px/1.45 var(--sans); color: var(--ink); background: var(--panel); border: 1px solid var(--line); border-radius: 10px; padding: 8px 10px; resize: vertical; }
.ask-row { display: flex; justify-content: space-between; align-items: center; gap: 10px; margin-top: 6px; }
.ask-row .text-btn { text-align: left; }
.pill:disabled { opacity: .5; }

/* The library: browsing, reading paths and the workshop */
.browse { max-width: 980px; margin: 0 auto 18px; display: flex; flex-wrap: wrap; gap: 6px; align-items: center; justify-content: center; }
.browse .chip { text-decoration: none; }
.group-head { max-width: 980px; margin: 26px auto 10px; font: 600 18px/1.3 var(--serif); color: var(--muted); }
.shelf-head.sub { margin-top: 48px; }
.shelf-head.sub h2 { font: 600 24px/1.25 var(--serif); margin: 0 0 8px; }
.atlas-links { display: flex; flex-wrap: wrap; justify-content: center; gap: 8px; }
.card.path { border-top-color: var(--legend); }
.card.path .card-title { font-size: 21px; }
.card-intro { font-size: 14px; color: var(--muted); }
.tour-stops { padding-left: 1.4em; }
.tour-stops li { margin: 0 0 14px; }
.tour-stops p { margin: 2px 0 0; color: var(--muted); }
.tour-bar { margin: -12px 0 24px; padding: 12px 16px; background: var(--panel); font: 14px/1.5 var(--sans);
  border: 1px solid var(--line); border-left: 4px solid var(--legend); border-radius: 12px; }
.tour-bar .pill { font-size: 13px; padding: 4px 12px; }
.tour-bar p { margin: 0 0 6px; }
.tour-meta { font-size: 12px; color: var(--muted); }
.tour-nav { display: flex; gap: 10px; align-items: center; margin: 0 !important; }
.ws { max-width: 680px; margin: 0 auto; }
.ws-modes { display: flex; gap: 6px; flex-wrap: wrap; }
.ws-modes .chip { text-decoration: none; }
.ws-rules { color: var(--muted); }
.ws-label { display: block; font-weight: 600; margin: 18px 0 6px; }
.ws-text { width: 100%; font: var(--read-size)/1.6 var(--serif); padding: 12px 14px; border: 1px solid var(--line); border-radius: 12px;
  background: var(--panel); color: var(--ink); resize: vertical; }
.ws-text.zh { letter-spacing: .12em; }
.ws h2 { font: 600 18px var(--serif); margin: 22px 0 8px; }
.ws-pattern { margin: 0 0 8px; }
.ws-poem { display: grid; gap: 8px; }
.ws-line { display: flex; gap: 4px; }
.ws-ch { display: grid; justify-items: center; min-width: 2.2em; padding: 4px 2px; border-radius: 8px; background: var(--panel); border: 1px solid var(--line); }
.ws-ch b { font: 400 22px/1.2 var(--serif); }
.ws-ch i { font: normal 11px var(--sans); color: var(--muted); }
.ws-ch i.want { opacity: .55; }
.ws-ch.off { border-color: var(--accent); background: var(--accent-soft); }
.ws-ch.rh b { color: var(--accent); }
.ws-lines { padding-left: 1.6em; margin: 0; }
.ws-lines li { display: flex; gap: 10px; align-items: baseline; padding: 2px 0; }
.ws-text-line { flex: 1; font-family: var(--serif); }
.ws-count { font-size: 12px; color: var(--muted); white-space: nowrap; }
.ws-count.off { color: var(--accent); font-weight: 600; }
.ws-letter { font: 600 13px var(--sans); color: var(--legend); width: 1.2em; text-align: center; }
.ws-notes { color: var(--accent); padding-left: 1.2em; }
.ws-ok { color: var(--legend); font-weight: 600; }
.ws-ask { margin-top: 18px; }
.ws-ask .msg { background: var(--panel); border: 1px solid var(--line); }
