/* ---------- Tokens ---------- */
:root {
  --bg: #ffffff;
  --bg-2: #f5f5f6;
  --bg-3: #ececee;
  --ink: #111113;
  --ink-2: #4f5057;
  --ink-3: #8a8b92;
  --line: #e6e6e9;
  --accent: #2c5be8;
  --accent-ink: #ffffff;
  --word-bg: #fff0b3;
  --word-bg-hover: #ffe27a;
  --pat: #2c5be8;
  --pat-bg: rgba(44, 91, 232, 0.07);
  --playing: #f2f5ff;
  --scrim: rgba(17, 17, 19, 0.36);
  --shadow: 0 12px 40px rgba(17, 17, 19, 0.16), 0 2px 6px rgba(17, 17, 19, 0.06);

  --font-ui: "Inter", ui-sans-serif, system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  --font-th: "IBM Plex Sans Thai Looped", "Noto Sans Thai Looped", Thonburi, "Leelawadee UI", sans-serif;
  --th-size: 22px;
  --radius: 12px;
  color-scheme: light;
}

:root[data-thai-font="loopless"] {
  --font-th: "IBM Plex Sans Thai", "Noto Sans Thai", "Sukhumvit Set", "Leelawadee UI", sans-serif;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #111113;
    --bg-2: #1b1b1e;
    --bg-3: #26262a;
    --ink: #f2f2f3;
    --ink-2: #b4b5bb;
    --ink-3: #7d7e85;
    --line: #2a2a2f;
    --accent: #7b9bff;
    --accent-ink: #0d0f1a;
    --word-bg: rgba(250, 204, 21, 0.2);
    --word-bg-hover: rgba(250, 204, 21, 0.34);
    --pat: #8ea9ff;
    --pat-bg: rgba(123, 155, 255, 0.1);
    --playing: #181c2b;
    --scrim: rgba(0, 0, 0, 0.55);
    --shadow: 0 12px 40px rgba(0, 0, 0, 0.5);
    color-scheme: dark;
  }
}

:root[data-theme="dark"] {
  --bg: #111113;
  --bg-2: #1b1b1e;
  --bg-3: #26262a;
  --ink: #f2f2f3;
  --ink-2: #b4b5bb;
  --ink-3: #7d7e85;
  --line: #2a2a2f;
  --accent: #7b9bff;
  --accent-ink: #0d0f1a;
  --word-bg: rgba(250, 204, 21, 0.2);
  --word-bg-hover: rgba(250, 204, 21, 0.34);
  --pat: #8ea9ff;
  --pat-bg: rgba(123, 155, 255, 0.1);
  --playing: #181c2b;
  --scrim: rgba(0, 0, 0, 0.55);
  --shadow: 0 12px 40px rgba(0, 0, 0, 0.5);
  color-scheme: dark;
}

/* ---------- Base ---------- */
* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0;
  min-width: 320px;
  background: var(--bg);
  color: var(--ink);
  font: 400 16px/1.5 var(--font-ui);
  font-feature-settings: "cv11", "ss01";
  -webkit-font-smoothing: antialiased;
}
button, select, input { font: inherit; color: inherit; }
button { cursor: pointer; }
a { color: inherit; }
[hidden] { display: none !important; }
[lang="th"] { font-family: var(--font-th); }
:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; border-radius: 6px; }
svg { display: block; }

/* ---------- Top bar ---------- */
.topbar {
  position: sticky;
  top: 0;
  z-index: 10;
  background: color-mix(in srgb, var(--bg) 88%, transparent);
  backdrop-filter: saturate(1.4) blur(12px);
  -webkit-backdrop-filter: saturate(1.4) blur(12px);
  border-bottom: 1px solid var(--line);
}
.topbar-inner {
  width: min(720px, 100%);
  height: 56px;
  margin: 0 auto;
  padding: 0 16px;
  display: flex;
  align-items: center;
  gap: 8px;
}
.brand { font-weight: 650; font-size: 16px; letter-spacing: -0.01em; text-decoration: none; }
.back {
  display: inline-flex;
  align-items: center;
  gap: 2px;
  margin-left: -6px;
  padding: 6px 8px 6px 4px;
  border-radius: 8px;
  color: var(--ink-2);
  font-size: 15px;
  font-weight: 500;
  text-decoration: none;
}
.back:hover { color: var(--ink); background: var(--bg-2); }
.back svg { width: 20px; height: 20px; }
.topbar-title {
  min-width: 0;
  flex: 1;
  overflow: hidden;
  color: var(--ink-2);
  font-size: 14px;
  font-weight: 500;
  text-align: center;
  text-overflow: ellipsis;
  white-space: nowrap;
  opacity: 0;
  transition: opacity 0.15s ease;
}
.topbar-title.is-visible { opacity: 1; }
.spacer { flex: 1; }
.icon-btn {
  width: 40px;
  height: 40px;
  display: grid;
  place-items: center;
  border: 0;
  border-radius: 10px;
  background: transparent;
  color: var(--ink-2);
}
.icon-btn:hover { background: var(--bg-2); color: var(--ink); }
.icon-btn svg { width: 20px; height: 20px; }
.icon-btn.is-on { color: #e8a200; }
.icon-btn .aa { font-size: 15px; font-weight: 600; letter-spacing: -0.02em; }

/* ---------- Page ---------- */
.page {
  width: min(720px, 100%);
  margin: 0 auto;
  padding: 36px 20px 120px;
}
h1 {
  margin: 0;
  font-size: clamp(28px, 6vw, 36px);
  font-weight: 650;
  letter-spacing: -0.025em;
  line-height: 1.15;
}
.lede { margin: 10px 0 0; color: var(--ink-2); font-size: 16px; max-width: 54ch; }

/* ---------- Home ---------- */
.tabs {
  display: inline-flex;
  gap: 2px;
  margin: 28px 0 8px;
  padding: 3px;
  border-radius: 10px;
  background: var(--bg-2);
}
.tab {
  padding: 6px 12px;
  border: 0;
  border-radius: 8px;
  background: transparent;
  color: var(--ink-2);
  font-size: 14px;
  font-weight: 500;
}
.tab .count { margin-left: 4px; color: var(--ink-3); font-variant-numeric: tabular-nums; }
.tab[aria-selected="true"] { background: var(--bg); color: var(--ink); box-shadow: 0 1px 2px rgba(0, 0, 0, 0.08); }
.tab[aria-selected="true"] .count { color: var(--ink-2); }

.dlist { margin: 0; padding: 0; list-style: none; }
.ditem {
  position: relative;
  display: flex;
  align-items: center;
  border-bottom: 1px solid var(--line);
}
.ditem-link {
  flex: 1;
  min-width: 0;
  display: grid;
  grid-template-columns: 36px minmax(0, 1fr);
  gap: 4px 10px;
  padding: 18px 4px 18px 0;
  text-decoration: none;
}
.ditem-link:hover .ditem-title { text-decoration: underline; text-underline-offset: 3px; text-decoration-thickness: 1px; }
.ditem-num {
  padding-top: 2px;
  color: var(--ink-3);
  font-size: 14px;
  font-weight: 500;
  font-variant-numeric: tabular-nums;
}
.ditem-body { min-width: 0; display: grid; gap: 3px; }
.ditem-title { font-size: 17px; font-weight: 600; letter-spacing: -0.01em; line-height: 1.3; }
.ditem-th { color: var(--ink-2); font-size: 16px; line-height: 1.5; }
.ditem-meta { display: flex; flex-wrap: wrap; align-items: center; gap: 4px 10px; margin-top: 2px; color: var(--ink-3); font-size: 13px; }
.pill {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  padding: 1px 8px;
  border-radius: 999px;
  background: var(--bg-2);
  color: var(--ink-2);
  font-size: 12px;
  font-weight: 500;
}
.pill.next { background: var(--accent); color: var(--accent-ink); }
.pill svg { width: 12px; height: 12px; }
.ditem.is-read .ditem-num { color: var(--ink-3); }
.ditem .icon-btn { flex: none; color: var(--ink-3); }
.ditem .icon-btn.is-on { color: #e8a200; }

.empty { padding: 48px 0; color: var(--ink-3); text-align: center; }

/* ---------- Reader header ---------- */
.eyebrow {
  margin: 0 0 10px;
  color: var(--ink-3);
  font-size: 13px;
  font-weight: 500;
  letter-spacing: 0.01em;
}
.title-th { margin: 6px 0 0; color: var(--ink-2); font-size: 20px; line-height: 1.5; }
.scene { margin: 16px 0 0; color: var(--ink-2); font-size: 16px; line-height: 1.6; }
.cast { display: flex; flex-wrap: wrap; gap: 8px 22px; margin: 18px 0 0; padding: 0; }
.cast div { display: flex; align-items: center; gap: 8px; }
.cast dd { margin: 0; font-size: 14px; font-weight: 500; }
.cast dd span { color: var(--ink-3); font-weight: 400; }

.badge {
  width: 26px;
  height: 26px;
  flex: none;
  display: grid;
  place-items: center;
  border-radius: 50%;
  font-size: 12px;
  font-weight: 650;
  line-height: 1;
}
.badge.a { background: var(--ink); color: var(--bg); }
.badge.b { box-shadow: inset 0 0 0 1.5px var(--ink-3); color: var(--ink-2); }

.toolbar {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 8px;
  margin: 24px 0 0;
  padding: 0 0 20px;
  border-bottom: 1px solid var(--line);
}
.btn-primary {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  height: 40px;
  padding: 0 16px 0 13px;
  border: 0;
  border-radius: 999px;
  background: var(--ink);
  color: var(--bg);
  font-size: 15px;
  font-weight: 600;
}
.btn-primary:hover { opacity: 0.9; }
.btn-primary svg { width: 16px; height: 16px; }
.chip {
  height: 36px;
  padding: 0 12px;
  border: 1px solid var(--line);
  border-radius: 999px;
  background: var(--bg);
  color: var(--ink-2);
  font-size: 14px;
  font-weight: 500;
  font-variant-numeric: tabular-nums;
}
.chip:hover { background: var(--bg-2); color: var(--ink); }
.chip[aria-pressed="true"] { border-color: var(--ink); color: var(--ink); }
.legend {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 6px 14px;
  margin: 14px 0 0;
  color: var(--ink-3);
  font-size: 13px;
}
.legend .mk { cursor: default; pointer-events: none; font-family: var(--font-ui); }

/* ---------- Highlights ---------- */
.mk { border-radius: 3px; cursor: pointer; transition: background 0.12s ease; }
.mk-word {
  background: var(--word-bg);
  box-decoration-break: clone;
  -webkit-box-decoration-break: clone;
  padding: 0 1px;
}
.mk-word:hover { background: var(--word-bg-hover); }
.mk-pat {
  text-decoration: underline;
  text-decoration-color: var(--pat);
  text-decoration-thickness: 2px;
  text-underline-offset: 0.32em;
  text-decoration-skip-ink: none;
}
.mk-pat:hover { background: var(--pat-bg); }
.mk.is-focus { outline: 2px solid var(--accent); outline-offset: 1px; }

/* ---------- Lines ---------- */
.lines { margin: 0; padding: 8px 0 0; list-style: none; }
.line {
  position: relative;
  display: grid;
  grid-template-columns: 26px minmax(0, 1fr) 40px;
  gap: 14px;
  margin: 0 -12px;
  padding: 18px 12px;
  border-radius: var(--radius);
  transition: background 0.15s ease;
}
.line + .line::before {
  content: "";
  position: absolute;
  top: 0;
  left: 52px;
  right: 12px;
  border-top: 1px solid var(--line);
}
.line.is-playing { background: var(--playing); }
.line.is-playing::before, .line.is-playing + .line::before { border-color: transparent; }
.line .badge { margin-top: calc((var(--th-size) * 1.7 - 26px) / 2); }
.th {
  margin: 0;
  font-size: var(--th-size);
  font-weight: 400;
  line-height: 1.7;
  word-break: normal;
  overflow-wrap: anywhere;
}
.en { margin: 6px 0 0; color: var(--ink-2); font-size: 15.5px; line-height: 1.55; }
.reveal {
  margin: 6px 0 0;
  padding: 2px 0;
  border: 0;
  background: none;
  color: var(--ink-3);
  font-size: 14px;
  text-decoration: underline dotted;
  text-underline-offset: 3px;
}
.reveal:hover { color: var(--ink-2); }
.note {
  margin: 10px 0 0;
  padding: 8px 12px;
  border-radius: 8px;
  background: var(--bg-2);
  color: var(--ink-2);
  font-size: 14px;
  line-height: 1.55;
}
.note b { margin-right: 6px; color: var(--ink); font-size: 11px; font-weight: 650; letter-spacing: 0.06em; text-transform: uppercase; }
.note [lang="th"], .sheet-note [lang="th"] { font-size: 1.05em; }
.line-play {
  width: 40px;
  height: 40px;
  margin-top: calc((var(--th-size) * 1.7 - 40px) / 2);
  display: grid;
  place-items: center;
  border: 0;
  border-radius: 50%;
  background: transparent;
  color: var(--ink-3);
}
.line-play:hover { background: var(--bg-2); color: var(--ink); }
.line-play svg { width: 16px; height: 16px; }
.line.is-playing .line-play { background: var(--ink); color: var(--bg); }

/* ---------- Study sections ---------- */
.study { margin-top: 48px; }
.study h2 {
  display: flex;
  align-items: baseline;
  gap: 8px;
  margin: 0 0 4px;
  font-size: 20px;
  font-weight: 650;
  letter-spacing: -0.015em;
}
.study h2 span { color: var(--ink-3); font-size: 15px; font-weight: 500; }
.study-hint { margin: 0 0 14px; color: var(--ink-3); font-size: 14px; }
.study + .study { margin-top: 44px; }

.words { margin: 0; padding: 0; list-style: none; border-top: 1px solid var(--line); }
.word {
  display: grid;
  grid-template-columns: minmax(0, 1fr) 36px;
  gap: 2px 10px;
  padding: 12px 0;
  border-bottom: 1px solid var(--line);
}
.word-head { display: flex; flex-wrap: wrap; align-items: baseline; gap: 2px 12px; }
.word-th { font-size: 20px; line-height: 1.5; }
.word-en { color: var(--ink); font-size: 15px; }
.word-note { grid-column: 1; margin: 2px 0 0; color: var(--ink-3); font-size: 14px; line-height: 1.5; }
.say {
  width: 36px;
  height: 36px;
  grid-row: 1;
  grid-column: 2;
  display: grid;
  place-items: center;
  border: 0;
  border-radius: 50%;
  background: transparent;
  color: var(--ink-3);
}
.say:hover { background: var(--bg-2); color: var(--ink); }
.say svg { width: 14px; height: 14px; }

.patterns { display: grid; gap: 12px; }
.pcard { padding: 18px 18px 14px; border: 1px solid var(--line); border-radius: var(--radius); }
.pform { margin: 0; color: var(--pat); font-size: 21px; font-weight: 500; line-height: 1.5; }
.pen { margin: 2px 0 0; font-size: 15.5px; font-weight: 600; }
.pnote { margin: 6px 0 0; color: var(--ink-2); font-size: 14.5px; line-height: 1.6; }
.pnote [lang="th"] { font-size: 1.05em; }
.examples { margin: 12px 0 0; padding: 0; list-style: none; border-top: 1px solid var(--line); }
.example {
  display: grid;
  grid-template-columns: minmax(0, 1fr) 36px;
  gap: 0 10px;
  padding: 10px 0 6px;
}
.example + .example { border-top: 1px dashed var(--line); }
.example .ex-th { margin: 0; font-size: 18px; line-height: 1.6; }
.example .ex-en { margin: 0; color: var(--ink-3); font-size: 14px; }

/* ---------- Footer ---------- */
.reader-foot {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  margin-top: 48px;
  padding-top: 20px;
  border-top: 1px solid var(--line);
}
.btn {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  height: 42px;
  padding: 0 16px;
  border: 1px solid var(--line);
  border-radius: 999px;
  background: var(--bg);
  font-size: 15px;
  font-weight: 500;
  text-decoration: none;
}
.btn:hover { background: var(--bg-2); }
.btn svg { width: 16px; height: 16px; }
.btn.is-done { border-color: transparent; background: var(--bg-2); color: var(--ink-2); }
.next-link { max-width: 100%; }
.next-link span { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

/* ---------- Sheet ---------- */
.scrim { position: fixed; inset: 0; z-index: 30; background: var(--scrim); animation: fade 0.15s ease; }
.sheet {
  position: fixed;
  left: 50%;
  bottom: 0;
  z-index: 31;
  width: min(560px, 100%);
  max-height: min(86vh, 760px);
  overflow-y: auto;
  overscroll-behavior: contain;
  transform: translateX(-50%);
  padding: 22px 22px calc(24px + env(safe-area-inset-bottom));
  border-radius: 18px 18px 0 0;
  background: var(--bg);
  box-shadow: var(--shadow);
  animation: rise 0.18s ease;
}
@media (min-width: 640px) {
  .sheet { bottom: auto; top: 50%; transform: translate(-50%, -50%); border-radius: 18px; animation: pop 0.16s ease; }
}
.sheet-head { display: flex; align-items: flex-start; justify-content: space-between; gap: 12px; }
.sheet-kicker { margin: 0 0 4px; color: var(--ink-3); font-size: 12px; font-weight: 600; letter-spacing: 0.06em; text-transform: uppercase; }
.sheet-title { margin: 0; font-size: 22px; font-weight: 650; letter-spacing: -0.015em; }
.sheet-th { margin: 0; font-size: 30px; font-weight: 500; line-height: 1.45; }
.sheet-th.is-pattern { color: var(--pat); font-size: 26px; }
.sheet-word-row { display: flex; align-items: center; gap: 10px; }
.sheet-word-row .say { width: 40px; height: 40px; background: var(--bg-2); color: var(--ink); }
.sheet-en { margin: 6px 0 0; font-size: 17px; font-weight: 600; }
.sheet-note { margin: 8px 0 0; color: var(--ink-2); font-size: 15px; line-height: 1.6; }
.sheet-uses { margin: 14px 0 0; color: var(--ink-3); font-size: 13px; }
.sheet .examples { margin-top: 16px; }

/* Settings */
.setting { padding: 14px 0; border-bottom: 1px solid var(--line); }
.setting:last-of-type { border-bottom: 0; }
.setting-label { display: flex; align-items: baseline; justify-content: space-between; gap: 12px; margin-bottom: 8px; font-size: 15px; font-weight: 600; }
.setting-label small { color: var(--ink-3); font-size: 13px; font-weight: 400; }
.setting-help { margin: 8px 0 0; color: var(--ink-3); font-size: 13px; line-height: 1.5; }
.seg { display: flex; padding: 3px; border-radius: 10px; background: var(--bg-2); }
.seg button {
  flex: 1;
  height: 34px;
  border: 0;
  border-radius: 8px;
  background: transparent;
  color: var(--ink-2);
  font-size: 14px;
  font-weight: 500;
  white-space: nowrap;
}
.seg button[aria-pressed="true"] { background: var(--bg); color: var(--ink); box-shadow: 0 1px 2px rgba(0, 0, 0, 0.1); }
.seg .seg-th { font-family: var(--font-th); font-size: 16px; }
.seg .seg-th.looped { font-family: "IBM Plex Sans Thai Looped", "Noto Sans Thai Looped", sans-serif; }
.seg .seg-th.loopless { font-family: "IBM Plex Sans Thai", "Noto Sans Thai", sans-serif; }
.select {
  width: 100%;
  height: 40px;
  padding: 0 10px;
  border: 1px solid var(--line);
  border-radius: 10px;
  background: var(--bg);
  font-size: 15px;
}
.range { width: 100%; accent-color: var(--ink); }
.size-preview { margin: 6px 0 0; font-size: var(--th-size); line-height: 1.6; }
.check { display: flex; align-items: center; gap: 10px; font-size: 15px; }
.check input { width: 18px; height: 18px; accent-color: var(--ink); }
.check.is-disabled { color: var(--ink-3); }
.sheet-foot { display: flex; justify-content: space-between; align-items: center; margin-top: 10px; color: var(--ink-3); font-size: 13px; }
.text-btn { padding: 6px 0; border: 0; background: none; color: var(--ink-2); font-size: 14px; text-decoration: underline; text-underline-offset: 3px; }
.text-btn:hover { color: var(--ink); }

/* ---------- Player ---------- */
.player {
  position: fixed;
  left: 50%;
  bottom: calc(16px + env(safe-area-inset-bottom));
  z-index: 20;
  display: flex;
  align-items: center;
  gap: 4px;
  padding: 6px 6px 6px 16px;
  transform: translateX(-50%);
  border-radius: 999px;
  background: var(--ink);
  color: var(--bg);
  box-shadow: var(--shadow);
  font-size: 14px;
  font-weight: 500;
  font-variant-numeric: tabular-nums;
  animation: rise 0.18s ease;
}
.player-label { min-width: 92px; padding-right: 6px; }
.player button {
  width: 36px;
  height: 36px;
  display: grid;
  place-items: center;
  border: 0;
  border-radius: 50%;
  background: transparent;
  color: inherit;
}
.player button:hover { background: color-mix(in srgb, var(--bg) 16%, transparent); }
.player svg { width: 16px; height: 16px; }

/* ---------- Toast ---------- */
.toast {
  position: fixed;
  left: 50%;
  bottom: calc(76px + env(safe-area-inset-bottom));
  z-index: 40;
  width: max-content;
  max-width: calc(100vw - 32px);
  padding: 10px 14px;
  transform: translateX(-50%);
  border-radius: 10px;
  background: var(--ink);
  color: var(--bg);
  font-size: 14px;
  line-height: 1.45;
  box-shadow: var(--shadow);
}

@keyframes fade { from { opacity: 0; } }
@keyframes rise { from { opacity: 0; transform: translate(-50%, 12px); } }
@keyframes pop { from { opacity: 0; transform: translate(-50%, -48%); } }

/* ---------- Small screens ---------- */
@media (max-width: 560px) {
  .page { padding: 24px 16px 120px; }
  .line { grid-template-columns: 24px minmax(0, 1fr) 36px; gap: 10px; margin: 0 -8px; padding: 16px 8px; }
  .line + .line::before { left: 42px; right: 8px; }
  .line .badge { width: 24px; height: 24px; font-size: 11px; margin-top: calc((var(--th-size) * 1.7 - 24px) / 2); }
  .line-play { width: 36px; height: 36px; margin-top: calc((var(--th-size) * 1.7 - 36px) / 2); }
  .en { font-size: 15px; }
  .pcard { padding: 16px 14px 12px; }
  .sheet { padding-left: 18px; padding-right: 18px; }
}

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