:root {
  --bg: #f3efe7;
  --paper: #fffcf8;
  --ink: #1c1916;
  --muted: #6f675e;
  --faint: #8a8176;
  --line: #e4dcd0;
  --cinnabar: #9f2d2d;
  --cinnabar-dark: #7e2222;
  --cinnabar-soft: #f8ece7;
  --play: #0f6e62;
  --play-soft: #e5f4f1;
  --blue: #e7f2fb;
  --blue-line: #c5dff3;
  --side: #221e1b;
  --side-text: #f6f1ea;
  --side-muted: #b7aea3;
  --side-line: rgba(255, 255, 255, 0.08);
  --sidebar-w: 280px;
  --sans: "Be Vietnam Pro", "Segoe UI", sans-serif;
  --zh: "Noto Serif SC", "Noto Sans SC", serif;
  --py: "Noto Sans", "Segoe UI", sans-serif;
}

* { box-sizing: border-box; }
html { scroll-behavior: smooth; }
body {
  margin: 0;
  min-height: 100dvh;
  background:
    radial-gradient(900px 420px at 90% -10%, rgba(159, 45, 45, 0.07), transparent 55%),
    var(--bg);
  color: var(--ink);
  font-family: var(--sans);
  font-size: 16px;
  line-height: 1.55;
}
button, input { font: inherit; color: inherit; }
.zh { font-family: var(--zh); font-weight: 600; }
.py, em { font-family: var(--py); font-style: italic; color: var(--muted); }

.sidebar {
  position: fixed;
  inset: 0 auto 0 0;
  z-index: 40;
  width: min(var(--sidebar-w), 88vw);
  height: 100dvh;
  display: flex;
  flex-direction: column;
  padding-bottom: calc(64px + env(safe-area-inset-bottom));
  background: var(--side);
  color: var(--side-text);
  transform: translateX(-105%);
  transition: transform 0.22s ease;
}
.sidebar.is-open { transform: none; }
.side-brand {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 16px 14px;
  border-bottom: 1px solid var(--side-line);
}
.side-brand strong { display: block; font-size: 16px; }
.side-brand span { color: var(--side-muted); font-size: 12px; }
.seal {
  width: 42px;
  height: 42px;
  display: grid;
  place-items: center;
  border-radius: 12px;
  background: var(--cinnabar);
  color: white;
  font-weight: 700;
}
.icon-btn {
  margin-left: auto;
  border: 0;
  background: transparent;
  color: inherit;
  width: 36px;
  height: 36px;
  border-radius: 10px;
  cursor: pointer;
}
.side-settings {
  margin-left: auto;
  width: 40px;
  height: 40px;
  flex: none;
  display: grid;
  place-items: center;
  border: 1px solid var(--side-line);
  border-radius: 12px;
  background: transparent;
  color: var(--side-text);
  cursor: pointer;
}
.side-settings:hover { background: rgba(255, 255, 255, 0.06); }
.sidebar-close { margin-left: 6px; }
.side-brand { flex: none; }
.lesson-list {
  flex: 1;
  min-height: 0;
  display: grid;
  align-content: start;
  gap: 4px;
  padding: 12px;
  overflow-y: auto;
  overscroll-behavior: contain;
}
.lesson-list::-webkit-scrollbar { width: 8px; }
.lesson-list::-webkit-scrollbar-thumb {
  background: rgba(255, 255, 255, 0.18);
  border-radius: 99px;
}
.lesson-link {
  width: 100%;
  display: flex;
  gap: 12px;
  align-items: center;
  text-align: left;
  border: 0;
  border-radius: 12px;
  padding: 8px;
  background: transparent;
  color: inherit;
  cursor: pointer;
}
.lesson-link > span:first-child {
  width: 36px;
  height: 36px;
  display: grid;
  place-items: center;
  flex: none;
  border-radius: 10px;
  background: rgba(255, 255, 255, 0.06);
  color: var(--side-muted);
  font-size: 12px;
  font-weight: 700;
}
.lesson-link:hover { background: rgba(255, 255, 255, 0.05); }
.lesson-link.is-on {
  background: rgba(255, 255, 255, 0.1);
  box-shadow: inset 3px 0 0 var(--cinnabar);
}
.lesson-link.is-on > span:first-child { background: var(--cinnabar); color: white; }
.lesson-link b { display: block; font-size: 18px; }
.lesson-link small { color: var(--side-muted); }
.lesson-link.is-on small { color: #f3e6df; }
.side-foot {
  flex: none;
  padding: 14px 16px calc(14px + env(safe-area-inset-bottom));
  border-top: 1px solid var(--side-line);
  color: var(--side-muted);
  font-size: 12px;
}
.backdrop {
  position: fixed;
  inset: 0;
  z-index: 30;
  background: rgba(0, 0, 0, 0.35);
}
.backdrop[hidden] { display: none; }

.main { min-height: 100dvh; }
.topbar {
  position: sticky;
  top: 0;
  z-index: 20;
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 10px 14px;
  background: rgba(243, 239, 231, 0.92);
  backdrop-filter: blur(10px);
  border-bottom: 1px solid transparent;
}
.top-kicker { display: block; color: var(--faint); font-size: 11px; letter-spacing: 0.08em; text-transform: uppercase; }
.loop { margin-left: auto; display: flex; align-items: center; gap: 6px; color: var(--muted); font-size: 14px; }
.lesson { padding: 8px 16px 96px; max-width: 880px; }

.lesson-head h1 { margin: 0; font-size: 40px; letter-spacing: -0.03em; }
.lesson-head h1 small { font-size: 20px; font-weight: 500; }
.eyebrow { margin: 0 0 6px; color: var(--cinnabar); font-weight: 650; font-size: 13px; }
.lead { margin: 6px 0 0; color: var(--muted); }
.head-actions { display: flex; flex-wrap: wrap; align-items: center; gap: 14px; margin-top: 14px; }
.head-actions .loop { margin-left: 0; }

.card {
  margin-top: 16px;
  padding: 16px;
  background: var(--paper);
  border: 1px solid var(--line);
  border-radius: 18px;
  box-shadow: 0 10px 30px rgba(48, 32, 18, 0.04);
}
.card.is-on { border-color: #b7ddd6; box-shadow: 0 0 0 3px var(--play-soft); }
.card-head, .plays { display: flex; align-items: center; justify-content: space-between; gap: 10px; }
.card h2, .card h3 { margin: 0; }
.card h2 { font-size: 20px; }
.card h2 span {
  display: inline-grid;
  place-items: center;
  width: 28px;
  height: 28px;
  margin-right: 6px;
  border-radius: 8px;
  background: var(--cinnabar);
  color: white;
  font-size: 14px;
}
.card h3 { margin: 16px 0 8px; font-size: 15px; }
.hint { color: var(--muted); font-size: 14px; }
.plain { margin: 8px 0 0; padding-left: 18px; }
.plain li { margin: 6px 0; }

.play {
  border: 0;
  border-radius: 999px;
  background: var(--play-soft);
  color: var(--play);
  font-weight: 700;
  min-height: 36px;
  padding: 0 14px;
  cursor: pointer;
}
.play.is-on, .play-main.is-on { background: var(--cinnabar); color: white; }
.play-main { background: var(--ink); color: white; }

.bubbles { display: grid; gap: 8px; }
.bubble {
  display: grid;
  grid-template-columns: auto 1fr;
  gap: 4px 12px;
  align-items: center;
  padding: 10px 12px;
  border-radius: 14px;
  background: var(--blue);
}
.bubble .vi { grid-column: 2; margin: 0; }
.num { font-weight: 750; color: #1d5f8a; }
.bubble .zh, .line .zh, .swap-out .zh, .clip .zh { margin: 0; font-size: 22px; }
.clip {
  display: grid;
  grid-template-columns: auto minmax(0, 1fr) auto;
  gap: 2px 12px;
  align-items: center;
  width: 100%;
  margin: 0 0 8px;
  padding: 10px 12px;
  border: 0;
  border-radius: 14px;
  background: var(--blue);
  text-align: left;
  cursor: pointer;
}
.clip .py, .clip .vi, .clip .pos { margin: 0; }
.clip .vi { grid-column: 2; }
.clip .pos { color: var(--faint); font-size: 12px; }
.clip.word { background: transparent; border-bottom: 1px solid var(--line); border-radius: 0; }
.clip.word .zh { font-size: 26px; }
.clip.is-on { background: var(--play-soft); }
.clip.is-on .zh { color: var(--play); }
.clip.line { background: transparent; border-top: 1px solid var(--line); border-radius: 0; margin: 0; }
.clip.line.is-role { opacity: 0.55; }
.clip.line.is-turn { background: #fff4e8; opacity: 1; }
.turn-label { display: none; grid-column: 2; color: var(--cinnabar); font-size: 13px; font-weight: 700; }
.clip.line.is-turn .turn-label { display: block; }
.who small { display: block; font-family: var(--py); font-style: italic; font-weight: 500; color: var(--faint); font-size: 12px; }
.who-a { color: #9f2d2d; }
.who-b { color: #0f6e62; }
.stack { display: grid; }
.talk-head { display: flex; align-items: center; justify-content: space-between; gap: 8px; margin-top: 8px; }
.rolebar {
  display: flex;
  flex-wrap: wrap;
  gap: 8px 16px;
  margin: 8px 0;
  padding: 10px 12px;
  border: 1px solid var(--line);
  border-radius: 14px;
  background: #f7f4ee;
}
.rolebar label { display: inline-flex; align-items: center; gap: 6px; font-weight: 650; }
.role-hint { margin: 0 0 8px; color: var(--faint); font-size: 13px; }
.settings {
  position: fixed;
  inset: 0;
  z-index: 80;
  display: grid;
  place-items: center;
  padding: 20px;
}
.settings[hidden] { display: none !important; }
.settings-backdrop { position: absolute; inset: 0; border: 0; background: rgba(22, 16, 12, 0.48); cursor: pointer; }
.settings-panel {
  position: relative;
  width: min(440px, 100%);
  padding: 18px;
  border-radius: 18px;
  background: var(--paper);
}
.settings-head { display: flex; align-items: center; justify-content: space-between; }
.settings-head h2 { margin: 0; }
.set-field { margin-top: 16px; }
.set-field p { margin: 4px 0 8px; color: var(--muted); font-size: 14px; }
.set-row { display: flex; align-items: center; gap: 8px; }
.set-row input { width: 6rem; min-height: 40px; padding: 0 10px; border: 1px solid var(--line); border-radius: 10px; background: white; }
.set-row select {
  min-width: 11rem;
  min-height: 40px;
  padding: 0 10px;
  border: 1px solid var(--line);
  border-radius: 10px;
  background: white;
}
.topbar .icon-btn { margin-left: auto; width: auto; padding: 0 10px; font-weight: 700; color: var(--cinnabar); }

.clip.sent,
.clip.line,
.clip.word {
  display: grid;
  grid-template-columns: auto minmax(0, 1fr);
  align-items: start;
  column-gap: 12px;
  row-gap: 2px;
}
.clip .num {
  min-width: 2.6rem;
  grid-column: 1;
  grid-row: 1 / span 3;
  align-self: center;
}
.clip.line .who-role,
.clip.line .who-name,
.clip.line .who-py {
  grid-column: 1;
  align-self: center;
  line-height: 1.3;
}
.clip.line .who-role { grid-row: 1; font-weight: 750; font-size: 18px; }
.clip.line .who-name { grid-row: 2; font-family: var(--zh); font-weight: 700; font-size: 16px; }
.clip.line .who-py {
  grid-row: 3;
  font-family: var(--py);
  font-style: italic;
  font-weight: 500;
  font-size: 13px;
  color: var(--faint);
}
.clip.sent .zh,
.clip.line .zh,
.clip.word .zh,
.clip.sent .py,
.clip.line .py,
.clip.word .py,
.clip.sent .vi,
.clip.line .vi,
.clip.word .vi {
  grid-column: 2;
  min-width: 0;
  white-space: normal;
}
.clip.sent .zh,
.clip.line .zh,
.clip.word .zh { grid-row: 1; }
.clip.sent .py,
.clip.line .py,
.clip.word .py { grid-row: 2; margin-left: 0; }
.clip.sent .vi,
.clip.line .vi,
.clip.word .vi { grid-row: 3; }
.clip.name .zh,
.clip.name .py,
.clip.name .vi { grid-column: 1; }
.clip.line .turn-label { grid-column: 2; grid-row: 4; }
.swap-base {
  display: grid;
  grid-template-columns: auto minmax(0, 1fr);
  gap: 2px 12px;
  align-items: start;
}
.swap-base > span { grid-row: 1 / span 2; align-self: center; }
.swap-base .zh,
.swap-base em { grid-column: 2; margin: 0; }
.extend p {
  display: grid;
  grid-template-columns: auto minmax(0, 1fr);
  gap: 2px 12px;
  align-items: start;
}
.extend p b { grid-row: 1 / span 2; align-self: center; min-width: 1.4rem; }
.extend p .zh,
.extend p em { grid-column: 2; margin: 0; }
.extend p:not(:has(b)) { grid-template-columns: minmax(0, 1fr); }
.extend p:not(:has(b)) .zh,
.extend p:not(:has(b)) em { grid-column: 1; }
.plain li:has(.zh) { display: grid; gap: 2px; }
.plain li:has(.zh) em { margin: 0; }

.bubble .py, .line .py, .bubble .vi, .line .vi { margin: 0; }

.talk + .talk { margin-top: 14px; }
.talk h3 { margin: 0 0 8px; color: var(--faint); font-size: 12px; letter-spacing: 0.08em; text-transform: uppercase; }
.line { display: grid; grid-template-columns: 92px 1fr; gap: 10px; padding: 8px 0; border-top: 1px solid var(--line); }
.who { font-family: var(--zh); font-weight: 700; }
.who small { display: block; font-family: var(--py); font-weight: 400; font-style: italic; color: var(--faint); font-size: 12px; }

.note { padding: 8px 0; border-top: 1px solid var(--line); }
.note p { margin: 4px 0; }

.swap { margin: 10px 0; padding: 10px 12px; border: 1px solid var(--line); border-radius: 14px; }
.swap-base { margin: 0 0 8px; }
.chips { display: flex; flex-wrap: wrap; gap: 8px; }
.chip {
  border: 1px solid var(--blue-line);
  background: var(--blue);
  border-radius: 999px;
  padding: 6px 12px;
  cursor: pointer;
}
.chip small { color: var(--muted); }
.chip.is-on { background: var(--ink); color: white; border-color: var(--ink); }
.chip.is-on small { color: #ddd; }
.swap-out { margin: 8px 0 0; }
.extend-wrap { display: grid; gap: 10px; }
.extend { padding: 10px 12px; border-radius: 14px; background: #f7f4ee; }
.extend p { margin: 4px 0; }
.extend .zh { font-size: 18px; }

.table-wrap { overflow-x: auto; }
.words, .grid { width: 100%; border-collapse: collapse; }
.words th, .words td, .grid th, .grid td { padding: 8px 10px; border-bottom: 1px solid var(--line); text-align: left; }
.words .zh { font-size: 20px; }
.grid { font-family: var(--py); font-size: 14px; }
.grid th, .grid .init { background: #f6f1ea; font-style: normal; color: var(--ink); font-weight: 700; }
.sound-row { display: flex; flex-wrap: wrap; gap: 8px; }
.sound-row span {
  min-width: 42px;
  padding: 6px 8px;
  border-radius: 8px;
  background: var(--blue);
  text-align: center;
  font-family: var(--py);
}

.quiz { margin: 10px 0; padding: 10px 12px; border-radius: 14px; background: #f7f4ee; }
.quiz p { margin: 6px 0; }
.quiz input {
  width: 9rem;
  border: 0;
  border-bottom: 1px solid #b7aea3;
  background: transparent;
  padding: 2px 4px;
  font-family: var(--zh);
  font-size: 18px;
}
.quiz input.is-ok { border-bottom-color: var(--play); background: var(--play-soft); }
.quiz input.is-bad { border-bottom-color: var(--cinnabar); background: var(--cinnabar-soft); }
.quiz-actions { display: flex; align-items: center; gap: 12px; }
.phrase-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(140px, 1fr)); gap: 10px; }
.phrase { display: grid; gap: 6px; padding: 12px; border-radius: 14px; background: var(--blue); }
.phrase span { font-family: var(--zh); font-size: 18px; }

.playbar {
  position: fixed;
  z-index: 50;
  left: 50%;
  bottom: calc(16px + env(safe-area-inset-bottom));
  transform: translateX(-50%);
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 8px 8px 8px 16px;
  border-radius: 999px;
  background: var(--ink);
  color: white;
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.2);
}
.playbar[hidden] { display: none !important; }
.playbar button {
  border: 0;
  border-radius: 999px;
  background: white;
  color: var(--cinnabar-dark);
  font-weight: 700;
  min-height: 36px;
  padding: 0 14px;
  cursor: pointer;
}

.app-tabs {
  position: fixed;
  z-index: 70;
  left: 0;
  right: 0;
  bottom: 0;
  display: flex;
  gap: 2px;
  padding: 6px 4px calc(6px + env(safe-area-inset-bottom));
  background: #1a1614;
  border-top: 1px solid var(--side-line);
}
.app-tab {
  flex: 1 1 0;
  min-width: 0;
  border: 0;
  border-radius: 12px;
  padding: 10px 2px;
  background: transparent;
  color: var(--side-muted);
  font-size: 13px;
  font-weight: 650;
  text-align: center;
  white-space: nowrap;
  cursor: pointer;
}
.app-tab.is-on { background: var(--cinnabar); color: white; }

.quiz-go {
  border: 0;
  border-radius: 999px;
  min-height: 40px;
  padding: 8px 16px;
  background: var(--cinnabar);
  color: white;
  font-weight: 700;
  cursor: pointer;
}
.quiz-go:hover { background: var(--cinnabar-dark); }

.type-page {
  min-height: calc(100dvh - 150px);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  text-align: center;
  gap: 8px;
  padding: 12px 8px 28px;
}
.type-top { display: flex; flex-direction: column; align-items: center; gap: 12px; }
.type-kicker {
  margin: 0;
  color: var(--faint);
  font-size: 13px;
  font-weight: 650;
  letter-spacing: 0.08em;
  text-transform: uppercase;
}
.type-mode {
  display: inline-flex;
  gap: 4px;
  padding: 4px;
  border: 1px solid var(--line);
  border-radius: 999px;
  background: var(--paper);
}
.type-mode button {
  border: 0;
  border-radius: 999px;
  padding: 8px 16px;
  background: transparent;
  color: var(--muted);
  font-weight: 650;
  cursor: pointer;
}
.type-mode button.is-on { background: var(--cinnabar); color: white; }
.type-tools { display: flex; align-items: center; gap: 10px; }
.type-sound {
  width: 40px;
  height: 40px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  border: 1px solid var(--line);
  border-radius: 999px;
  background: var(--paper);
  color: var(--ink);
  cursor: pointer;
}
.type-sound .type-mute { display: none; }
.type-sound.is-off { color: var(--faint); }
.type-sound.is-off .type-wave { display: none; }
.type-sound.is-off .type-mute { display: block; }
.type-bar {
  width: min(220px, 60%);
  height: 3px;
  margin: 8px 0 4px;
  border-radius: 99px;
  background: var(--line);
  overflow: hidden;
}
.type-bar span { display: block; height: 100%; background: var(--cinnabar); }
.type-zh {
  margin: 8px 0 6px;
  font-family: var(--zh);
  font-weight: 600;
  font-size: clamp(72px, 16vw, 128px);
  line-height: 1.05;
}
.type-form {
  width: min(440px, 100%);
  display: flex;
  flex-direction: column;
  align-items: center;
}
.type-label {
  margin: 0 0 6px;
  color: var(--muted);
  font-size: 14px;
  font-weight: 650;
}
.type-input {
  width: 100%;
  border: 0;
  border-bottom: 2px solid var(--line);
  background: transparent;
  color: var(--ink);
  font-family: var(--py);
  font-size: 28px;
  font-style: normal;
  text-align: center;
  padding: 8px 4px 10px;
}
.type-input:focus { outline: none; border-bottom-color: var(--cinnabar); }
.type-feedback {
  min-height: 2.6em;
  margin: 12px 0 4px;
  color: var(--muted);
  font-size: 16px;
}
.type-feedback.is-ok { color: var(--play); font-weight: 650; }
.type-feedback.is-near { color: #8a5a12; }
.type-feedback.is-no { color: var(--cinnabar-dark); }
.type-empty,
.type-done,
.type-score { margin: 8px 0; }
.type-done { font-family: var(--zh); font-size: 40px; font-weight: 600; }
.type-score { color: var(--muted); font-size: 18px; }
.type-day { margin: 0 0 6px; color: var(--ink); font-size: 16px; }
.type-day.is-done { color: var(--play); font-weight: 700; }
.type-empty { max-width: 28em; color: var(--muted); font-size: 18px; }

@media (max-width: 959px) {
  .lesson { padding-bottom: 120px; }
  .playbar { bottom: calc(72px + env(safe-area-inset-bottom)); }
  body.is-type .lesson { padding-top: 8px; }
  body.is-type .type-page:not(:has(.type-done)):not(:has(.type-empty)) {
    min-height: 0;
    justify-content: flex-start;
    padding-top: 0;
    gap: 4px;
  }
  body.is-type .type-zh { margin: 2px 0 0; font-size: clamp(56px, 18vw, 84px); }
  body.is-type .type-top { gap: 8px; }
  body.is-type .type-bar { margin: 4px 0 0; }
}

@media (min-width: 960px) {
  body { padding-left: var(--sidebar-w); }
  .sidebar { transform: none; }
  .topbar .icon-btn, .sidebar-close, .backdrop { display: none; }
  .lesson { padding: 28px 32px 110px; }
  .bubble { grid-template-columns: auto 1fr auto; }
  .bubble .vi { grid-column: auto; }
  .app-tabs { width: var(--sidebar-w); right: auto; }
}
@media (max-width: 720px) {
  .head-actions .loop { display: none; }
}
