:root {
  --bg: #f4f7fb;
  --card: #ffffff;
  --ink: #2c333a;
  --muted: #7b8694;
  --line: #e6edf5;
  --blue: #5b93e6;
  --blue-2: #3f78d4;
  --blue-soft: #e8f1fc;
  --ok: #3d9a6a;
  --warn: #d9853a;
  --text-2: #4d5b6a;
  --shadow: 0 10px 30px rgba(44, 74, 120, 0.06);
  --radius: 18px;
  --side: 248px;
}

html[data-theme="dark"] {
  --bg: #171d27;
  --card: #222a36;
  --ink: #e7eef6;
  --muted: #9aa6b5;
  --line: #313b4a;
  --blue: #7eacf0;
  --blue-2: #9cc0f5;
  --blue-soft: #2a384c;
  --text-2: #c5d0dc;
  --shadow: 0 10px 30px rgba(0, 0, 0, 0.28);
}

* { box-sizing: border-box; }
html, body { margin: 0; min-height: 100%; }
body {
  background: var(--bg);
  color: var(--ink);
  font-family: "Noto Sans SC", "Microsoft YaHei", "Sarabun", "Leelawadee UI", sans-serif;
  font-size: 16px;
  line-height: 1.6;
}
button, input, textarea, select { font: inherit; color: inherit; }
button { cursor: pointer; }
h1, h2, h3, p { margin: 0; }
h1 { font-size: 28px; font-weight: 700; letter-spacing: -0.02em; }
h2 { font-size: 20px; font-weight: 700; }
a { color: var(--blue-2); }

.app { display: grid; grid-template-columns: var(--side) 1fr; min-height: 100vh; }
.back-key {
  width: 36px;
  height: 36px;
  border: 0;
  border-radius: 10px;
  background: transparent;
  font-size: 22px;
  line-height: 1;
  color: var(--ink);
}
.back-key:hover { background: var(--blue-soft); }
.side {
  position: sticky;
  top: 0;
  height: 100vh;
  background: var(--card);
  border-right: 1px solid var(--line);
  padding: 22px 16px;
  display: flex;
  flex-direction: column;
  gap: 8px;
  overflow: auto;
}
.side-label { color: var(--muted); font-size: 12px; padding: 12px 10px 4px; }
.book-btn, .nav-link {
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  gap: 2px;
  width: 100%;
  text-align: left;
  border: 0;
  background: transparent;
  border-radius: 12px;
  padding: 10px 12px;
}
.book-btn small, .nav-link small { color: var(--muted); font-size: 12px; }
.book-btn, .nav-link { position: relative; }
.book-btn.is-on, .nav-link.is-on { background: var(--blue-soft); color: var(--blue-2); }
.book-btn.is-on::before, .nav-link.is-on::before {
  content: "";
  position: absolute;
  left: 0;
  top: 8px;
  bottom: 8px;
  width: 3px;
  border-radius: 2px;
  background: var(--blue);
}
.book-btn:hover, .nav-link:hover { background: var(--blue-soft); }
.nav-link { flex-direction: row; align-items: center; gap: 8px; }
.nav-ico { width: 18px; height: 18px; flex: 0 0 auto; }

.main { min-width: 0; display: flex; flex-direction: column; }
.content-head {
  display: grid;
  grid-template-columns: 36px minmax(0, 1fr);
  align-items: center;
  gap: 8px;
  padding: 16px 28px 4px;
}
.content-head.is-root { grid-template-columns: minmax(0, 1fr); }
.content-title {
  margin: 0;
  font-size: 20px;
  font-weight: 700;
  line-height: 1.3;
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.logo {
  border: 0;
  background: transparent;
  padding: 0;
  display: flex;
  align-items: baseline;
  gap: 8px;
}
.logo b { font-family: "Sarabun", "Leelawadee UI", sans-serif; font-size: 28px; font-weight: 700; color: var(--blue-2); }
.logo span { color: var(--muted); font-size: 13px; }
.top-links { display: flex; gap: 8px; margin-left: auto; }
.top-links button, .text-btn {
  border: 0;
  background: transparent;
  color: var(--muted);
  border-radius: 999px;
  padding: 8px 12px;
}
.top-links button.is-on, .text-btn.is-on { background: var(--blue-soft); color: var(--blue-2); }

.progress-block { padding: 4px 28px 0; }
.progress-meta { display: flex; justify-content: space-between; color: var(--muted); font-size: 13px; margin-bottom: 6px; }
.track { height: 6px; background: var(--line); border-radius: 99px; overflow: hidden; }
.track span { display: block; height: 100%; background: var(--blue); border-radius: 99px; }

.stage { flex: 1; display: flex; flex-direction: column; align-items: center; justify-content: flex-start; padding: 8px 20px 28px; }
.page { padding: 12px 28px 48px; max-width: 1080px; }
.day-progress { width: min(560px, 100%); margin: 0 0 8px; color: var(--text-2, #4d5b6a); font-size: 13px; }

.flip { width: min(560px, 100%); perspective: 1000px; }
.flip-inner {
  position: relative;
  min-height: 440px;
  transform-style: preserve-3d;
  transition: transform 0.35s ease;
}
.flip.is-back .flip-inner { transform: rotateY(180deg); }
.face {
  position: absolute;
  inset: 0;
  backface-visibility: hidden;
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: 22px;
  box-shadow: var(--shadow);
  padding: 28px 28px 22px;
  display: flex;
  flex-direction: column;
  text-align: center;
  gap: 8px;
}
.face-front { align-items: center; justify-content: center; }
.face-back {
  transform: rotateY(180deg);
  align-items: stretch;
  text-align: left;
  justify-content: flex-start;
  gap: 10px;
  overflow: hidden;
}
.thai-xl {
  font-family: "Sarabun", "Leelawadee UI", sans-serif;
  font-size: 84px;
  line-height: 1.12;
  font-weight: 700;
}
.face-front .roman { font-size: 26px; font-weight: 500; }
.face-front .badge { margin-top: 2px; }
.face-front .hint { margin-top: 18px; }
.roman { color: var(--blue-2); font-size: 20px; }
.ipa { color: var(--muted); font-size: 14px; line-height: 1.4; }
.pos { color: var(--blue-2); font-size: 13px; background: var(--blue-soft); border-radius: 999px; padding: 2px 10px; align-self: flex-start; }
.zh-lg { font-size: 26px; font-weight: 700; line-height: 1.35; }
.card-tags { display: flex; align-items: center; gap: 6px; }
.tag-bar { color: var(--muted); background: transparent; padding: 0; }
.say-list { display: flex; flex-direction: column; }
.say-row {
  display: grid;
  grid-template-columns: 2em minmax(0, 1fr) auto;
  gap: 4px 10px;
  align-items: center;
  padding: 8px 0;
  border-top: 1px solid var(--line);
}
.say-who { color: var(--blue-2); font-size: 14px; font-weight: 700; }
.say-main .th { font-family: "Sarabun", "Leelawadee UI", sans-serif; font-size: 20px; line-height: 1.35; }
.btn.say-play {
  position: relative;
  overflow: hidden;
  display: inline-flex;
  align-items: center;
  gap: 6px;
  flex: 0 0 auto;
  padding: 6px 12px;
  white-space: nowrap;
}
.play-bars { display: none; width: 14px; height: 12px; align-items: flex-end; gap: 2px; }
.play-bars i { display: block; width: 3px; height: 10px; background: currentColor; transform-origin: center bottom; border-radius: 1px; animation: playeq 0.75s ease-in-out infinite; }
.play-bars i:nth-child(1) { animation-delay: 0s; }
.play-bars i:nth-child(2) { animation-delay: 0.16s; }
.play-bars i:nth-child(3) { animation-delay: 0.32s; }
.say-play.is-playing .play-tri { display: none; }
.say-play.is-playing .play-bars { display: inline-flex; }
.say-play.is-playing { color: var(--blue-2); }
.play-tri, .play-bars, .play-label { position: relative; z-index: 1; }
.play-bar { position: absolute; left: 0; top: 0; bottom: 0; width: 0; background: rgba(91, 147, 230, 0.45); pointer-events: none; }
@keyframes playeq {
  0%, 100% { transform: scaleY(0.35); }
  50% { transform: scaleY(1); }
}
.card .text-btn { display: block; width: 100%; text-align: left; margin-top: 6px; }
.talk { display: flex; flex-direction: column; gap: 10px; margin-top: 8px; }
.bubble { max-width: min(460px, 100%); border: 1px solid var(--line); border-radius: 16px; padding: 12px 14px; background: var(--card); }
.bubble.m { align-self: flex-start; }
.bubble.f { align-self: flex-end; background: var(--blue-soft); }
.bubble .th { font-family: "Sarabun", "Leelawadee UI", sans-serif; font-size: 20px; line-height: 1.4; }
.bubble .btn { margin-top: 8px; }
.say-tip { margin-top: 2px; }
.flip-hint { margin-top: auto; }
.hint { color: var(--muted); font-size: 13px; }
.roman-line { display: inline-flex; align-items: center; justify-content: center; gap: 6px; max-width: 100%; flex-wrap: wrap; }
.syl { border: 0; background: transparent; color: inherit; font: inherit; padding: 0 1px; border-bottom: 1px dashed var(--blue); cursor: pointer; }
.horn { display: inline-flex; align-items: center; justify-content: center; width: 28px; height: 28px; padding: 0; border: 0; border-radius: 50%; background: transparent; color: var(--blue); cursor: pointer; flex: 0 0 auto; }
.horn svg { width: 22px; height: 22px; overflow: visible; }
.horn-body { fill: currentColor; }
.horn-wave { fill: none; stroke: currentColor; stroke-width: 1.8; stroke-linecap: round; opacity: 0.35; }
.horn.is-playing svg { animation: hornshake 0.55s ease-in-out infinite; transform-origin: 8px 12px; }
.horn.is-playing .horn-wave { animation: hornwave 0.7s ease-in-out infinite; }
.horn.is-playing .horn-wave-2 { animation-delay: 0.16s; }
@keyframes hornshake {
  0%, 100% { transform: rotate(0deg); }
  30% { transform: rotate(-8deg); }
  70% { transform: rotate(7deg); }
}
@keyframes hornwave {
  0%, 100% { opacity: 0.2; }
  50% { opacity: 1; }
}
.syl-dash { color: var(--muted); }
.ipa-fold { margin-top: 8px; color: var(--muted); font-size: 13px; }
.ipa-fold summary { cursor: pointer; }
.quiz-option.is-bad { border-color: var(--warn); }
.ph-card .roman { font-size: 28px; }
.ph-head { display: flex; align-items: baseline; justify-content: space-between; gap: 12px; flex-wrap: wrap; }
.ph-progress { color: var(--muted); font-size: 15px; }
.ph-lead { margin-top: 12px; }
.ph-lessons { display: flex; flex-direction: column; gap: 10px; margin-top: 16px; }
.ph-lesson {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 14px 16px;
  border: 1px solid var(--line);
  border-radius: 14px;
  background: var(--card);
  box-shadow: var(--shadow);
  text-align: left;
}
.ph-lesson-name { display: flex; align-items: center; color: var(--ink); font-size: 16px; font-weight: 500; }
.ph-lesson.is-locked { box-shadow: none; }
.ph-lesson.is-locked .ph-lesson-name { color: var(--muted); }
.ph-lesson .btn, .ph-actions .btn, .ph-letters .btn { flex: 0 0 auto; }
.ph-actions { display: flex; gap: 8px; flex-wrap: wrap; margin-top: 16px; }
.ph-letters { margin-top: 16px; }
.ph-letters .btn { margin-top: 12px; }

.actions { display: flex; gap: 10px; margin-top: 14px; width: min(560px, 100%); }
.actions .has-tip:hover::after, .actions .has-tip:focus-visible::after { left: 50%; right: auto; transform: translateX(-50%); }
.btn {
  border-radius: 999px;
  border: 1px solid var(--line);
  background: var(--card);
  padding: 12px 16px;
  flex: 1;
}
.btn-primary { background: var(--blue); border-color: var(--blue); color: white; }
.btn-ghost { background: transparent; }
.btn-warn { color: var(--warn); }
.btn:disabled { opacity: 0.5; cursor: default; }

.fab {
  position: fixed;
  right: 28px;
  bottom: 28px;
  width: 56px;
  height: 56px;
  border-radius: 50%;
  border: 0;
  background: var(--blue);
  color: white;
  box-shadow: var(--shadow);
  font-size: 22px;
  z-index: 4;
}
.bottom-nav { display: none; }

.grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(280px, 1fr)); gap: 14px; }
.grid-2 { display: grid; grid-template-columns: 1.4fr 1fr; gap: 14px; }
.card {
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 18px;
  box-shadow: var(--shadow);
}
.side .logo { margin: 0 8px 8px; }
.side .logo span { color: var(--muted); font-size: 12px; font-weight: 400; }
.book-btn.is-locked, .nav-link.is-locked { color: var(--muted); }
.today {
  background: var(--card);
  border: 1px solid var(--line);
  border-top: 4px solid var(--blue);
  border-radius: 22px;
  padding: 28px 28px 24px;
  box-shadow: var(--shadow);
}
.today h2 { margin-top: 14px; }
.book-jump {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  margin-top: 14px;
  padding: 0;
  border: 0;
  background: transparent;
  font-size: 22px;
  font-weight: 700;
}
.book-jump .chev { color: var(--muted); font-weight: 500; }
.lex-list { max-height: 360px; overflow: auto; margin-top: 10px; }
.wheel-wrap { position: relative; margin-top: 12px; }
.wheel-wrap::after {
  content: "";
  position: absolute;
  left: 8px;
  right: 8px;
  top: 52px;
  height: 52px;
  border-radius: 12px;
  background: var(--blue-soft);
  pointer-events: none;
}
.wheel {
  position: relative;
  z-index: 1;
  height: 156px;
  overflow-y: auto;
  scroll-snap-type: y mandatory;
  background: transparent;
}
.wheel-pad { height: 52px; }
.wheel-item {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 100%;
  height: 52px;
  border: 0;
  background: transparent;
  scroll-snap-align: center;
  color: var(--muted);
}
.wheel-item.is-on { color: var(--ink); font-weight: 700; }
.today .desc { margin-top: 4px; }
.today-head { display: flex; justify-content: space-between; align-items: baseline; gap: 12px; }
.today-meta { color: var(--text-2, #4d5b6a); font-size: 14px; line-height: 1.5; }
.today-stats { display: flex; align-items: flex-start; gap: 0; margin: 18px 0 4px; }
.today-stats b { display: block; font-size: 42px; line-height: 1; font-weight: 700; letter-spacing: -0.03em; }
.today-stats span { display: block; margin-top: 6px; color: var(--text-2, #4d5b6a); font-size: 14px; }
.today-dot { margin: 0 22px; color: #c5ced8; font-size: 28px; line-height: 42px; font-weight: 500; }
.eyebrow { color: var(--blue-2); font-size: 13px; font-weight: 700; letter-spacing: 0.04em; }
.lock-icon { width: 12px; height: 12px; margin-right: 4px; vertical-align: -1px; }
.has-tip { position: relative; }
.has-tip:hover::after, .has-tip:focus-visible::after {
  content: attr(data-tip);
  position: absolute;
  left: 16px;
  bottom: calc(100% + 8px);
  width: max-content;
  max-width: 260px;
  white-space: normal;
  background: var(--ink);
  color: var(--bg);
  font-size: 12px;
  font-weight: 400;
  line-height: 1.5;
  text-align: left;
  padding: 8px 10px;
  border-radius: 8px;
  z-index: 4;
  pointer-events: none;
}
.book-card.has-tip:hover::after { left: 18px; bottom: 64px; }
.desc { color: var(--text-2, #4d5b6a); font-size: 15px; line-height: 1.65; }
.today-actions { margin-top: 18px; }
.today-actions .btn { flex: 0 0 auto; }
.today-actions .btn-primary { min-width: 148px; padding-left: 22px; padding-right: 22px; }
.section-title { margin: 28px 0 12px; font-size: 16px; }
.card-top { display: flex; align-items: center; gap: 8px; margin-bottom: 8px; }
.badge, .state { font-size: 12px; line-height: 1.4; border-radius: 999px; padding: 2px 8px; }
.badge, .state-on { background: var(--blue-soft); color: var(--blue-2); }
.state-done { color: var(--ok); background: transparent; border: 1px solid var(--line); }
.state-lock { display: inline-flex; align-items: center; color: var(--muted); background: transparent; border: 1px solid var(--line); }
.progress-label { margin-top: 12px; font-size: 14px; color: var(--text-2, #4d5b6a); }
.mini-track { height: 6px; margin-top: 6px; background: var(--line); border-radius: 99px; overflow: hidden; }
.mini-track span { display: block; height: 100%; background: var(--blue); }
.book-card.is-locked { opacity: 0.62; }
.book-card .desc { margin-top: 6px; }
.card button.block, .block { width: 100%; }
.muted { color: var(--muted); }
.row { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }
.chips { display: flex; gap: 8px; flex-wrap: wrap; margin: 12px 0; }
.chip {
  border: 1px solid var(--line);
  background: var(--card);
  border-radius: 999px;
  padding: 6px 12px;
  color: var(--muted);
}
.chip.is-on { background: var(--blue-soft); color: var(--blue-2); border-color: transparent; }

.auth {
  min-height: 100vh;
  display: grid;
  grid-template-columns: 1.1fr 0.9fr;
}
.auth-brand, .auth-form { display: flex; flex-direction: column; justify-content: center; padding: 48px; }
.auth-brand { background: var(--blue-soft); }
.field { display: flex; flex-direction: column; gap: 6px; margin-top: 12px; }
.field input, .field textarea, .search {
  border: 1px solid var(--line);
  background: var(--card);
  border-radius: 12px;
  padding: 12px 14px;
}
.form-error { color: #c45b4a; font-size: 14px; min-height: 1.2em; }

.modal-back {
  position: fixed;
  inset: 0;
  background: rgba(23, 29, 39, 0.35);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px;
  z-index: 5;
}
.modal { width: min(420px, 100%); }

.list { display: flex; flex-direction: column; gap: 8px; }
.word-row, .check-row {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 12px;
  border: 1px solid var(--line);
  border-radius: 14px;
  background: var(--card);
}
.word-row .th, .thai {
  font-family: "Sarabun", "Leelawadee UI", sans-serif;
  font-size: 22px;
}
.grow { flex: 1; min-width: 0; }
.calendar { display: grid; grid-template-columns: repeat(7, 1fr); gap: 6px; }
.day {
  aspect-ratio: 1;
  border-radius: 8px;
  background: var(--line);
  display: flex;
  align-items: flex-end;
  justify-content: center;
  font-size: 11px;
  color: var(--muted);
  padding-bottom: 4px;
}
.day.is-on { background: var(--blue); color: white; }
.bars { display: flex; align-items: flex-end; gap: 8px; height: 120px; }
.bar { flex: 1; background: var(--blue-soft); border-radius: 8px 8px 4px 4px; min-height: 6px; position: relative; }
.bar span { position: absolute; left: 0; right: 0; bottom: 0; background: var(--blue); border-radius: inherit; }
.badges { display: flex; gap: 8px; flex-wrap: wrap; }
.badge { background: var(--blue-soft); color: var(--blue-2); border-radius: 999px; padding: 6px 10px; font-size: 13px; }

.quiz-option {
  width: 100%;
  text-align: left;
  border: 1px solid var(--line);
  background: var(--card);
  border-radius: 14px;
  padding: 14px 16px;
  margin-top: 8px;
}
.quiz-option.is-on { border-color: var(--blue); background: var(--blue-soft); }

#toast {
  position: fixed;
  left: 50%;
  bottom: 24px;
  transform: translateX(-50%) translateY(12px);
  background: var(--ink);
  color: var(--bg);
  border-radius: 999px;
  padding: 10px 16px;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.2s ease;
  z-index: 6;
}
#toast.show { opacity: 1; transform: translateX(-50%); }

.mobile-books { display: none; }

@media (max-width: 900px) {
  .app { display: block; }
  .side { display: none; }
  .mobile-books { display: flex; gap: 8px; overflow: auto; padding: 0 16px 8px; }
  .mobile-books button { flex: none; }
  .grid-2, .auth { grid-template-columns: 1fr; }
  .auth-brand { min-height: auto; padding: 28px 20px; }
  .auth-form, .page { padding: 16px 16px 128px; }
  .page .btn { scroll-margin-bottom: 96px; }
  .stage { padding: 4px 16px 96px; }
  .content-head {
    grid-template-columns: 36px minmax(0, 1fr) 36px;
    padding: 12px 16px 6px;
  }
  .content-head.is-root { grid-template-columns: minmax(0, 1fr); }
  .content-title { text-align: center; font-size: 17px; }
  .logo { white-space: nowrap; }
  .logo b { font-size: 22px; line-height: 1; }
  .logo span { font-size: 12px; white-space: nowrap; }
  .top-links {
    margin-left: 0;
    justify-content: flex-end;
    gap: 2px;
    min-width: 0;
    overflow-x: auto;
  }
  .top-links button {
    flex: 0 0 auto;
    white-space: nowrap;
    font-size: 13px;
    line-height: 1.2;
    padding: 6px 8px;
  }
  .mobile-books { padding: 6px 16px 10px; }
  .mobile-books .chip, .chip { white-space: nowrap; font-size: 14px; line-height: 1.3; }
  .today { padding: 20px 16px; }
  .today-head { flex-direction: column; align-items: flex-start; }
  .today-stats b { font-size: 34px; }
  .today-dot { margin: 0 16px; font-size: 24px; line-height: 34px; }
  .today-actions .btn { flex: 1 1 auto; }
  .page h1 { font-size: 22px; line-height: 1.35; letter-spacing: 0; }
  .page h2, .card h2 { font-size: 17px; line-height: 1.4; }
  .page .muted, .card .muted, .hint { font-size: 14px; line-height: 1.65; }
  .progress-block { padding: 0 16px; }
  .thai-xl { font-size: 60px; }
  .face-front .roman { font-size: 20px; }
  .zh-lg { font-size: 22px; }
  .say-main .th { font-size: 18px; }
  .actions { display: grid; grid-template-columns: 1fr 1fr; }
  .fab { right: 16px; bottom: 78px; }
  .bottom-nav {
    display: flex;
    position: fixed;
    left: 0;
    right: 0;
    bottom: 0;
    z-index: 5;
    background: var(--card);
    border-top: 1px solid var(--line);
    padding: 6px 4px calc(6px + env(safe-area-inset-bottom));
  }
  .bottom-nav button {
    flex: 1;
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 2px;
    border: 0;
    background: transparent;
    color: var(--muted);
    border-radius: 10px;
    padding: 6px 0 4px;
    font-size: 12px;
  }
  .bottom-nav .nav-ico { width: 20px; height: 20px; }
  .bottom-nav button.is-on { color: var(--blue-2); background: var(--blue-soft); }
  .top-links button span { display: none; }
}
