:root {
  --bg: #f7f4ee;
  --surface: #fffdf9;
  --surface-2: #f1ece3;
  --text: #221d17;
  --muted: #6d6458;
  --border: #e3dbcd;
  --accent: #a4520f;
  --accent-ink: #ffffff;
  --user: #efe6d6;
  --shadow: 0 1px 2px rgb(60 40 10 / 6%), 0 4px 16px rgb(60 40 10 / 6%);

  --ok: #1f7a46;     --ok-bg: #e3f3ea;
  --info: #1f5f9e;   --info-bg: #e3eefa;
  --warn: #8a6200;   --warn-bg: #fbf0d0;
  --alert: #b04a10;  --alert-bg: #fde7d6;
  --bad: #b3261e;    --bad-bg: #fbe2e0;
  --idle: #6d6458;   --idle-bg: #ece6dc;

  --sans: "Noto Sans Thai", "Leelawadee UI", "Tahoma", system-ui, sans-serif;
  --serif: "Noto Serif Thai", "Noto Sans Thai", serif;
  --radius: 14px;
  --reader-w: 440px;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #15130f; --surface: #1d1a15; --surface-2: #26221b; --text: #ece5d8; --muted: #a59a89;
    --border: #3a342a; --accent: #f0a04b; --accent-ink: #1d1a15; --user: #2e2820;
    --shadow: 0 1px 2px rgb(0 0 0 / 30%), 0 4px 16px rgb(0 0 0 / 25%);
    --ok: #6fd39a; --ok-bg: #173324; --info: #8cbcf0; --info-bg: #172a3d; --warn: #f0c75a; --warn-bg: #3a2f10;
    --alert: #f5a372; --alert-bg: #3d2416; --bad: #f28b82; --bad-bg: #3d1a18; --idle: #a59a89; --idle-bg: #2e2820;
  }
}
:root[data-theme="dark"] {
  --bg: #15130f; --surface: #1d1a15; --surface-2: #26221b; --text: #ece5d8; --muted: #a59a89;
  --border: #3a342a; --accent: #f0a04b; --accent-ink: #1d1a15; --user: #2e2820;
  --ok: #6fd39a; --ok-bg: #173324; --info: #8cbcf0; --info-bg: #172a3d; --warn: #f0c75a; --warn-bg: #3a2f10;
  --alert: #f5a372; --alert-bg: #3d2416; --bad: #f28b82; --bad-bg: #3d1a18; --idle: #a59a89; --idle-bg: #2e2820;
}

* { box-sizing: border-box; }
html, body { height: 100%; }
body {
  margin: 0; background: var(--bg); color: var(--text);
  font: 16px/1.75 var(--sans); -webkit-font-smoothing: antialiased;
  display: flex; flex-direction: column;
}
button { font: inherit; color: inherit; cursor: pointer; }
.sr-only { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); }
.ghost { background: transparent; border: 1px solid var(--border); border-radius: 999px; padding: 4px 14px; }
.ghost:hover { background: var(--surface-2); }
.muted { color: var(--muted); }

/* ---------- top bar ---------- */
.topbar {
  display: flex; align-items: center; justify-content: space-between; gap: 12px;
  padding: 10px 20px; border-bottom: 1px solid var(--border); background: var(--surface);
}
.brand { display: flex; align-items: center; gap: 12px; min-width: 0; }
.brand .mark { font-size: 30px; color: var(--accent); line-height: 1; }
.brand h1 { margin: 0; font-size: 18px; font-weight: 600; line-height: 1.3; }
.brand p { margin: 0; font-size: 13px; color: var(--muted); line-height: 1.4; }
.topbar-actions { display: flex; align-items: center; gap: 10px; flex-shrink: 0; }
.scope {
  font-size: 13px; padding: 3px 12px; border-radius: 999px;
  background: var(--surface-2); color: var(--muted); white-space: nowrap;
}

/* ---------- layout ---------- */
.layout { flex: 1; min-height: 0; display: flex; }
.chat { flex: 1; min-width: 0; display: flex; flex-direction: column; }
.messages { flex: 1; overflow-y: auto; padding: 24px 20px 12px; scroll-behavior: smooth; }
.messages > * { max-width: 820px; margin-left: auto; margin-right: auto; }

/* ---------- welcome ---------- */
.welcome h2 { font-size: 22px; margin: 8px 0 6px; }
.welcome h3 { font-size: 15px; margin: 22px 0 8px; color: var(--muted); font-weight: 500; }
.welcome p { margin: 0; color: var(--muted); }
.welcome-head { display: flex; flex-wrap: wrap; align-items: center; gap: 4px 12px; margin: 8px 0 6px; }
.welcome-head h2 { margin: 0; }
.welcome .stats {
  padding: 2px 14px; border-radius: 999px; white-space: nowrap;
  background: var(--surface-2); color: var(--text); font-size: 0.8em;
}
.welcome .stats b { color: var(--accent); }
.legend { list-style: none; padding: 0; margin: 10px 0 0; display: flex; flex-wrap: wrap; gap: 6px 16px; font-size: 14px; color: var(--muted); }
.examples { display: flex; flex-wrap: wrap; gap: 8px; }
.example {
  border: 1px solid var(--border); background: var(--surface); border-radius: 999px;
  padding: 6px 14px; font-size: 14px; text-align: left;
}
.example:hover { border-color: var(--accent); color: var(--accent); }
.volume-list { display: grid; grid-template-columns: repeat(auto-fill, minmax(230px, 1fr)); gap: 8px; }
.vol-group { grid-column: 1 / -1; margin: 10px 0 0; font-size: 14px; font-weight: 600; color: var(--accent); }
.linklike { background: none; border: 0; padding: 0; color: var(--accent); text-decoration: underline; font-size: inherit; }
.trailer { margin-top: 12px; font-size: 14px; color: var(--muted); }
.trailer summary { cursor: pointer; }
.scripture.small { font-size: 15px; line-height: 1.9; color: var(--muted); }
.toc { list-style: none; margin: 10px 0 0; padding: 0; font-size: 14.5px; }
.toc-div { margin-top: 12px; font-weight: 600; }
.toc-div.l1 { color: var(--accent); }
.toc-div.l2 { padding-left: 12px; }
.toc-unit { display: flex; justify-content: space-between; gap: 8px; padding: 2px 0; }
.toc-unit.in1 { padding-left: 12px; }
.toc-unit.in2 { padding-left: 24px; }
.toc-unit button { background: none; border: 0; padding: 0; text-align: left; color: var(--text); }
.toc-unit button:hover { color: var(--accent); text-decoration: underline; }
.toc-unit span { color: var(--muted); white-space: nowrap; }
.sutta-card {
  text-align: left; border: 1px solid var(--border); background: var(--surface);
  border-radius: 10px; padding: 8px 12px; line-height: 1.5;
}
.sutta-card:hover { border-color: var(--accent); }
.sutta-card b { display: block; font-weight: 500; }
.sutta-card span { font-size: 13px; color: var(--muted); }

/* ---------- messages ---------- */
.msg { margin-bottom: 22px; }
.msg.user { display: flex; justify-content: flex-end; }
.msg.user .bubble {
  background: var(--user); padding: 8px 16px; border-radius: var(--radius) var(--radius) 4px var(--radius);
  max-width: 85%; white-space: pre-wrap;
}
.msg.bot .card {
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  padding: 14px 18px; box-shadow: var(--shadow);
}
.steps { font-size: 14px; color: var(--muted); margin-bottom: 8px; }
.steps summary { cursor: pointer; user-select: none; }
.steps ol { margin: 6px 0 4px; padding-left: 22px; }
.steps li { line-height: 1.6; }
.answer { overflow-wrap: anywhere; }
.answer > :first-child { margin-top: 0; }
.answer > :last-child { margin-bottom: 0; }
.answer h1, .answer h2, .answer h3, .answer h4 { font-size: 17px; margin: 18px 0 6px; }
.answer blockquote {
  margin: 10px 0; padding: 6px 14px; border-left: 3px solid var(--accent);
  background: var(--surface-2); border-radius: 0 8px 8px 0; font-family: var(--serif);
}
.answer ul, .answer ol { padding-left: 22px; }
.typing { color: var(--muted); }
.typing::after { content: "…"; animation: blink 1.2s steps(3) infinite; }
@keyframes blink { 50% { opacity: 0.3; } }
.error { color: var(--bad); margin: 0; }

/* ---------- citation chips ---------- */
.cite {
  display: inline-block; margin: 0 2px; padding: 0 7px; border-radius: 6px; border: 1px solid transparent;
  font: 500 12.5px/1.7 var(--sans); vertical-align: 1px; white-space: nowrap;
  background: var(--idle-bg); color: var(--idle);
}
button.cite:hover { border-color: currentColor; }
.cite.s-verified { background: var(--ok-bg); color: var(--ok); }
.cite.s-outline  { background: var(--info-bg); color: var(--info); }
.cite.s-seen     { background: var(--warn-bg); color: var(--warn); }
.cite.s-unread   { background: var(--alert-bg); color: var(--alert); }
.cite.s-invalid  { background: var(--bad-bg); color: var(--bad); text-decoration: line-through; }

.sources { margin-top: 14px; border-top: 1px dashed var(--border); padding-top: 10px; }
.sources h4 { margin: 0 0 6px; font-size: 14px; font-weight: 500; color: var(--muted); }
.sources ul { list-style: none; margin: 0; padding: 0; display: grid; gap: 4px; }
.sources li { font-size: 14px; line-height: 1.5; display: flex; gap: 8px; align-items: baseline; }
.sources li .where { color: var(--muted); }
.warnings { margin-top: 10px; font-size: 13.5px; color: var(--warn); background: var(--warn-bg); border-radius: 10px; padding: 8px 12px; }
.warnings ul { margin: 4px 0 0; padding-left: 18px; }
.warnings:empty { display: none; }

/* ---------- composer ---------- */
.composer {
  display: flex; gap: 10px; align-items: flex-end;
  width: 100%; max-width: 860px; margin: 0 auto; padding: 8px 20px 4px;
}
.composer textarea {
  flex: 1; resize: none; max-height: 180px; padding: 10px 14px; border-radius: var(--radius);
  border: 1px solid var(--border); background: var(--surface); color: var(--text); font: inherit;
}
.composer textarea:focus { outline: 2px solid var(--accent); outline-offset: -1px; }
#send {
  background: var(--accent); color: var(--accent-ink); border: 0; border-radius: var(--radius);
  padding: 10px 22px; font-weight: 600;
}
#send:disabled { opacity: 0.5; cursor: progress; }
.send-col { display: flex; flex-direction: column; align-items: center; gap: 2px; }
.quota { font-size: 0.72em; line-height: 1.3; color: var(--muted); white-space: nowrap; }
.quota b { color: var(--accent); }
.quota.out b { color: var(--bad); }
.disclaimer { text-align: center; font-size: 12px; color: var(--muted); margin: 2px auto 10px; padding: 0 20px; max-width: 860px; line-height: 1.5; }

/* ---------- reader ---------- */
.reader {
  width: var(--reader-w); flex-shrink: 0; border-left: 1px solid var(--border);
  background: var(--surface); display: flex; flex-direction: column; min-height: 0;
}
.reader[hidden] { display: none; }
.reader-head {
  display: flex; justify-content: space-between; align-items: center; gap: 8px;
  padding: 10px 16px; border-bottom: 1px solid var(--border); font-weight: 500;
}
.reader-body { flex: 1; overflow-y: auto; padding: 16px 20px 28px; }
.crumbs { font-size: 13px; color: var(--muted); line-height: 1.5; }
.reader-body h2 { font-size: 20px; margin: 4px 0 2px; }
.meta { font-size: 13px; color: var(--muted); margin-bottom: 10px; }
.badge { display: inline-block; font-size: 13px; padding: 1px 10px; border-radius: 999px; margin-bottom: 10px; }
.badge.s-verified { background: var(--ok-bg); color: var(--ok); }
.badge.s-outline  { background: var(--info-bg); color: var(--info); }
.badge.s-seen     { background: var(--warn-bg); color: var(--warn); }
.badge.s-unread   { background: var(--alert-bg); color: var(--alert); }
.scripture { font: 17px/2.05 var(--serif); white-space: pre-wrap; overflow-wrap: anywhere; }
.pager { display: flex; justify-content: space-between; gap: 8px; margin: 18px 0; }
.pager button { max-width: 48%; text-align: left; }
.pager .next { margin-left: auto; text-align: right; }
.outline { font-size: 14px; border-top: 1px solid var(--border); padding-top: 10px; }
.outline summary { cursor: pointer; color: var(--muted); }
.outline ul { list-style: none; margin: 6px 0 0; padding: 0; }
.outline li { display: flex; justify-content: space-between; gap: 8px; padding: 2px 0; }
.outline li.sub { padding-left: 18px; }
.outline li button { background: none; border: 0; padding: 0; text-align: left; color: var(--text); }
.outline li button:hover { color: var(--accent); text-decoration: underline; }
.outline li.current button { color: var(--accent); font-weight: 600; }
.outline li span { color: var(--muted); white-space: nowrap; }
.range-list { list-style: none; margin: 10px 0 0; padding: 0; display: grid; gap: 6px; }
.range-list button {
  width: 100%; text-align: left; background: var(--surface-2); border: 1px solid transparent;
  border-radius: 10px; padding: 8px 12px; line-height: 1.55;
}
.range-list button:hover { border-color: var(--accent); }
.range-list b { color: var(--accent); font-weight: 600; margin-right: 6px; }
.range-list small { display: block; color: var(--muted); }

/* ---------- small screens: reader becomes a full-screen sheet ---------- */
@media (max-width: 900px) {
  .reader { position: fixed; inset: 0; width: auto; z-index: 10; border-left: 0; }
  .topbar { padding: 8px 16px; }
  .brand p, .scope { display: none; }
  .messages { padding: 16px 16px 8px; }
  .composer { padding: 8px 16px 4px; }
  .msg.user .bubble { max-width: 92%; }
}


/* ================= Easy reading (older readers) =================
   Larger text that follows the ก-/ก+ buttons (--fs), stronger contrast, bigger targets. */
:root { --fs: 17px; --muted: #4f473d; --border: #cfc4b2; }
@media (prefers-color-scheme: dark) { :root:not([data-theme="light"]) { --muted: #c9bfae; --border: #4a4236; } }
body { font-size: var(--fs); line-height: 1.85; }
.brand h1 { font-size: 1.15em; }
.brand p, .scope, .disclaimer, .meta, .crumbs, .steps, .sources h4, .warnings, .legend { font-size: 0.8em; }
.font-size { display: flex; gap: 4px; }
.font-size button, #new-chat { font-size: 0.85em; padding: 4px 14px; min-height: 40px; font-weight: 600; }
.welcome h2 { font-size: 1.35em; }
.welcome h3 { font-size: 0.9em; }
.hint { margin-top: 10px !important; }
.example { font-size: 0.85em; padding: 10px 18px; }
.sutta-card { font-size: 0.85em; padding: 12px 14px; }
.composer textarea { font-size: 1em; padding: 14px 16px; border-width: 2px; min-height: 64px; }
#send { font-size: 1em; padding: 14px 30px; min-height: 64px; }
.cite { font-size: 0.78em; padding: 2px 10px; margin: 2px 3px; border-radius: 8px; vertical-align: 0; line-height: 1.6; white-space: normal; }
button.cite { text-decoration: underline; text-underline-offset: 3px; }
.sources li { font-size: 0.9em; align-items: flex-start; }
.sources li small { font-size: 0.85em; color: var(--muted); }
.reader { --reader-w: 520px; }
.reader-head { font-size: 1em; }
#reader-close { min-height: 44px; min-width: 44px; font-size: 1em; }
.sutta-title { font-size: 1.4em !important; margin: 0 0 2px !important; color: var(--accent); }
.cite-line { font-size: 0.9em; font-weight: 600; margin-bottom: 4px; }
.scripture { font-size: 1.05em; line-height: 2.1; }
.pager button { min-height: 48px; font-size: 0.9em; padding: 6px 16px; }
.outline, .toc { font-size: 0.85em; }
.range-list button { font-size: 0.9em; padding: 12px 14px; }
@media (max-width: 900px) { .brand p, .scope { display: none; } .font-size button { padding: 4px 10px; } }

/* sources as a dropdown */
.sources details > summary { cursor: pointer; font-weight: 600; color: var(--muted); padding: 6px 0; min-height: 40px; list-style-position: inside; }
.sources details > summary:hover { color: var(--accent); }
.sources details[open] > summary { margin-bottom: 6px; }

/* draft answer while it is being checked against the scripture */
.answer.checking > * { opacity: 0.5; }
.answer.checking::before {
  content: attr(data-note) "…"; display: block; margin-bottom: 10px; padding: 8px 12px;
  border-radius: 8px; background: var(--surface-2); color: var(--text);
  font-weight: 600;
}

/* ================= left tabs: chat / short dramas ================= */
[hidden] { display: none !important; }
.sidenav {
  width: 96px; flex-shrink: 0; display: flex; flex-direction: column; gap: 6px;
  padding: 12px 8px; border-right: 1px solid var(--border); background: var(--surface);
}
.sidenav button {
  display: flex; flex-direction: column; align-items: center; gap: 4px; min-height: 72px;
  padding: 10px 4px; border: 0; border-radius: 12px; background: transparent;
  font-size: 0.72em; font-weight: 600; color: var(--muted); line-height: 1.3;
}
.sidenav .ico { font-size: 1.7em; line-height: 1; }
.sidenav button:hover { background: var(--surface-2); }
.sidenav button[aria-current="page"] { background: var(--surface-2); color: var(--accent); box-shadow: inset 3px 0 0 var(--accent); }
.sidenav button:focus-visible, .reel-btn:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

/* vertical feed: one 9:16 clip per screen, snap scrolling like Reels/TikTok */
.drama { flex: 1; min-width: 0; display: flex; flex-direction: column; background: #0f0d0b; }
.drama-feed { flex: 1; min-height: 0; overflow-y: auto; scroll-snap-type: y mandatory; overscroll-behavior: contain; }
.drama-note { color: #cfc4b2; text-align: center; padding: 40px 16px; margin: 0; grid-column: 1 / -1; }
/* the shelf of stories */
.drama-list {
  flex: 1; min-height: 0; overflow-y: auto; padding: 20px 16px; display: grid; align-content: start;
  grid-template-columns: repeat(auto-fill, minmax(160px, 1fr)); grid-auto-rows: max-content; gap: 16px;
}
.story {
  display: flex; flex-direction: column; padding: 0; border: 0; border-radius: 14px;
  background: #1d1915; color: #f3ece0; text-align: left; cursor: pointer;
}
.story img, .story-blank { width: 100%; aspect-ratio: 9 / 16; object-fit: cover; display: block; flex: none; border-radius: 14px 14px 0 0; }
.story-blank {
  display: flex; align-items: center; justify-content: center; font: 3em/1 var(--serif); color: rgb(255 255 255 / 35%);
  background: radial-gradient(120% 80% at 20% 0%, #7a3d10 0%, #2a1a0d 55%, #120e0a 100%);
}
.story-text { display: flex; flex-direction: column; gap: 4px; padding: 10px 12px 14px; }
.story strong { font-size: 0.95em; line-height: 1.45; }
.story-meta { font-size: 0.72em; line-height: 1.5; color: #cfc4b2; }
.story:hover { background: #2a241e; }
.story:focus-visible, .drama-back:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
/* one story open: its episodes as the feed, a way back to the shelf */
.drama-story { flex: 1; min-height: 0; position: relative; display: flex; flex-direction: column; }
.drama-back {
  position: absolute; top: 12px; left: 12px; z-index: 2; min-height: 44px; padding: 6px 14px; border: 0;
  border-radius: 999px; background: rgb(0 0 0 / 55%); color: #fff; font-size: 0.8em; font-weight: 600;
}
.drama-back:hover { background: rgb(0 0 0 / 75%); }
.reel { height: 100%; scroll-snap-align: start; scroll-snap-stop: always; display: flex; justify-content: center; align-items: center; padding: 14px 0; }
.reel-frame {
  position: relative; height: 100%; aspect-ratio: 9 / 16; max-width: 100%;
  border-radius: 18px; overflow: hidden; color: #fff;
  background: radial-gradient(120% 80% at 20% 0%, #7a3d10 0%, #2a1a0d 55%, #120e0a 100%);
}
.reel-video { width: 100%; height: 100%; object-fit: cover; display: block; cursor: pointer; }
.reel-placeholder {
  position: absolute; inset: 0; display: flex; flex-direction: column; justify-content: center; gap: 14px;
  padding: 28px 24px 42%; font: 1em/1.85 var(--serif);
}
.reel-placeholder p { margin: 0; }
.soon { align-self: flex-start; font: 600 0.68em/1.8 var(--sans); padding: 0 12px; border-radius: 999px; background: rgb(255 255 255 / 16%); }
.reel-info { position: absolute; inset: auto 0 0 0; padding: 56px 16px 18px; background: linear-gradient(transparent, rgb(0 0 0 / 78%)); }
/* while a clip plays the info fades away so the film's own subtitles are clear; pausing brings it back */
.reel-info { transition: opacity .5s ease, transform .5s ease; }
.reel.watching .reel-info { opacity: 0; transform: translateY(12px); pointer-events: none; }
@media (prefers-reduced-motion: reduce) { .reel-info { transition: none; } }
.reel-info h3 { margin: 0 0 4px; font-size: 1.15em; line-height: 1.4; }
.reel-info p { margin: 0 0 10px; font-size: 0.8em; line-height: 1.6; opacity: 0.92; }
.reel-actions { display: flex; flex-wrap: wrap; gap: 8px; }
.reel-btn {
  min-height: 44px; padding: 6px 14px; border-radius: 999px; font-size: 0.72em; font-weight: 600;
  border: 1px solid rgb(255 255 255 / 55%); background: rgb(0 0 0 / 38%); color: #fff;
}
.reel-btn:hover { background: rgb(255 255 255 / 18%); }
.reel-sound {
  position: absolute; top: 12px; right: 12px; width: 44px; height: 44px; border-radius: 50%;
  border: 0; background: rgb(0 0 0 / 45%); color: #fff; font-size: 1.1em;
}
@media (prefers-reduced-motion: reduce) { .drama-feed { scroll-behavior: auto; } }

/* phones: tabs move to a bottom bar, clips go full screen */
@media (max-width: 900px) {
  .layout { flex-direction: column; }
  .sidenav {
    order: 2; width: auto; flex-direction: row; gap: 4px; border-right: 0; border-top: 1px solid var(--border);
    padding: 4px 8px calc(4px + env(safe-area-inset-bottom, 0px));
  }
  .sidenav button { flex: 1; flex-direction: row; justify-content: center; gap: 8px; min-height: 52px; font-size: 0.8em; }
  .sidenav .ico { font-size: 1.3em; }
  .sidenav button[aria-current="page"] { box-shadow: inset 0 3px 0 var(--accent); }
  .chat, .drama { min-height: 0; }
  .reel { padding: 0; }
  .reel-frame { width: 100%; aspect-ratio: auto; border-radius: 0; }
}

/* ================= phones: less chrome, more room for the answer ================= */
@media (min-width: 601px) { .disc-short, .only-phone { display: none; } }
@media (max-width: 600px) {
  .topbar { padding: 6px 12px; gap: 8px; }
  .brand { gap: 8px; }
  .brand .mark { font-size: 24px; }
  .brand h1 { font-size: 1em; white-space: nowrap; }
  .topbar-actions { gap: 6px; }
  .font-size button, #new-chat { min-height: 38px; padding: 2px 10px; font-size: 0.75em; }
  .messages { padding: 12px 14px 8px; }
  .msg { margin-bottom: 18px; }
  /* the answer reads as plain text on the page, not a boxed card: full width, no frame */
  .msg.bot .card { background: transparent; border: 0; box-shadow: none; padding: 0; }
  .composer { padding: 6px 12px 2px; gap: 8px; }
  .composer textarea { min-height: 50px; padding: 10px 12px; }
  #send { min-height: 50px; padding: 8px 18px; }
  .disclaimer { margin: 0 auto 6px; padding: 0 12px; font-size: 12px !important; line-height: 1.4; }
  .disc-full { display: none; }
  .welcome h2 { font-size: 1.2em; }
  .credit { margin-top: 18px !important; font-size: 0.7em; line-height: 1.5; }
  .sidenav button { min-height: 46px; }
}
