:root {
  --bg: #0b0f14;
  --bg-elev: #151b23;
  --bg-elev-2: #1e2733;
  --text: #e6edf3;
  --text-dim: #9aa7b4;
  --border: #2a3441;
  --accent: #16a34a;
  --accent-dim: #14532d;
  --danger: #dc2626;
  --tone-mid: #9ca3af;
  --tone-low: #3b82f6;
  --tone-falling: #ef4444;
  --tone-high: #f59e0b;
  --tone-rising: #22c55e;
}

@media (prefers-color-scheme: light) {
  :root {
    --bg: #f7f8fa;
    --bg-elev: #ffffff;
    --bg-elev-2: #eef1f4;
    --text: #12181f;
    --text-dim: #5b6672;
    --border: #dde3e9;
    --accent: #15803d;
    --accent-dim: #dcfce7;
    --tone-mid: #6b7280;
    --tone-low: #2563eb;
    --tone-falling: #dc2626;
    --tone-high: #d97706;
    --tone-rising: #16a34a;
  }
}

* { box-sizing: border-box; }
.hidden { display: none !important; }

html, body {
  margin: 0;
  padding: 0;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Noto Sans Thai", "Leelawadee UI", sans-serif;
  height: 100%;
  overscroll-behavior-y: contain;
}

#app { display: flex; flex-direction: column; min-height: 100vh; }

.screen { display: none; flex: 1; flex-direction: column; padding: 16px; padding-bottom: 90px; }
.screen.active { display: flex; }

h1, h2, h3 { margin: 0 0 8px 0; }
p { margin: 4px 0; color: var(--text-dim); }

button {
  font: inherit;
  border: none;
  border-radius: 12px;
  padding: 14px 18px;
  cursor: pointer;
  min-height: 48px;
}

.btn-primary { background: var(--accent); color: #fff; font-weight: 600; }
.btn-secondary { background: var(--bg-elev-2); color: var(--text); }
.btn-danger { background: var(--danger); color: #fff; }
.btn-ghost { background: transparent; color: var(--text-dim); }
.btn-row { display: flex; gap: 10px; }
.btn-row button { flex: 1; }

input, textarea, select {
  font: inherit;
  background: var(--bg-elev);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 12px;
  width: 100%;
}

/* ---------- nav ---------- */
#bottom-nav {
  position: fixed;
  bottom: 0; left: 0; right: 0;
  display: flex;
  background: var(--bg-elev);
  border-top: 1px solid var(--border);
  padding: 6px env(safe-area-inset-right) env(safe-area-inset-bottom) env(safe-area-inset-left);
  z-index: 50;
}
#bottom-nav.hidden { display: none; }
.nav-btn {
  flex: 1;
  background: none;
  border: none;
  color: var(--text-dim);
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 2px;
  font-size: 11px;
  padding: 8px 4px;
  border-radius: 10px;
}
.nav-btn .nav-icon { font-size: 20px; }
.nav-btn.active { color: var(--accent); }

/* ---------- login ---------- */
#screen-login { justify-content: center; align-items: center; }
.login-card { width: 100%; max-width: 340px; display: flex; flex-direction: column; gap: 12px; }
.login-card h1 { text-align: center; }
.error-text { color: var(--danger); min-height: 18px; font-size: 14px; }

/* ---------- home ---------- */
.home-stats { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; margin: 16px 0; }
.stat-card { background: var(--bg-elev); border: 1px solid var(--border); border-radius: 14px; padding: 16px; text-align: center; }
.stat-card .stat-num { font-size: 28px; font-weight: 700; }
.stat-card .stat-label { color: var(--text-dim); font-size: 13px; }
#start-btn { font-size: 18px; padding: 20px; margin-top: 8px; }
.streak-badge { display: inline-flex; align-items: center; gap: 6px; color: var(--tone-high); font-weight: 600; }

.home-empty { text-align: center; margin-top: 8px; }
.home-empty .btn-row { margin-top: 10px; }
.home-empty-sub { color: var(--text-dim); font-size: 13px; margin-top: 10px; }

/* ---------- stack ---------- */
.scope-pills { display: flex; gap: 8px; margin-bottom: 12px; }
.scope-pill {
  flex: 1; background: var(--bg-elev-2); color: var(--text-dim);
  border-radius: 999px; padding: 8px 10px; font-size: 13px; min-height: auto;
}
.scope-pill.active { background: var(--accent); color: #fff; }
.stack-list { display: flex; flex-direction: column; gap: 12px; }
.stack-card {
  background: var(--bg-elev); border: 1px solid var(--border);
  border-radius: 14px; padding: 14px;
}
.stack-card-row { display: flex; align-items: center; justify-content: space-between; gap: 10px; margin-top: 6px; }
.stack-due { color: var(--text-dim); font-size: 13px; }

/* ---------- session card ---------- */
.card-box {
  background: var(--bg-elev);
  border: 1px solid var(--border);
  border-radius: 18px;
  padding: 24px 18px;
  flex: 1;
  display: flex;
  flex-direction: column;
  gap: 16px;
  position: relative;
}
.card-progress { color: var(--text-dim); font-size: 13px; }
.card-kind-badge {
  align-self: flex-start;
  background: var(--bg-elev-2);
  color: var(--text-dim);
  padding: 4px 10px;
  border-radius: 999px;
  font-size: 12px;
}
.thai-display {
  font-size: 34px;
  line-height: 1.6;
  text-align: center;
  padding: 12px 0;
}
.english-prompt { font-size: 24px; text-align: center; padding: 20px 0; }
.syllable {
  cursor: pointer;
  border-bottom: 2px dashed currentColor;
  padding: 0 1px;
}
.syllable.target-word { text-decoration: underline; text-decoration-thickness: 3px; }
.tone-mid { color: var(--tone-mid); }
.tone-low { color: var(--tone-low); }
.tone-falling { color: var(--tone-falling); }
.tone-high { color: var(--tone-high); }
.tone-rising { color: var(--tone-rising); }

.tone-legend { display: flex; gap: 10px; flex-wrap: wrap; font-size: 12px; justify-content: center; }
.tone-legend span { display: flex; align-items: center; gap: 4px; }
.tone-legend .dot { width: 10px; height: 10px; border-radius: 50%; display: inline-block; }

.romanization { text-align: center; color: var(--text-dim); font-size: 16px; }
.word-meanings { display: flex; flex-wrap: wrap; gap: 8px; justify-content: center; }
.word-meanings .wm { background: var(--bg-elev-2); border-radius: 10px; padding: 6px 10px; font-size: 13px; }

.audio-row { display: flex; justify-content: center; align-items: center; gap: 10px; }
.play-btn { border-radius: 50%; width: 56px; height: 56px; background: var(--accent); color: #fff; font-size: 22px; display: flex; align-items: center; justify-content: center; padding: 0; min-height: 0; }
.speed-btn {
  min-height: 0; padding: 6px 10px; border-radius: 999px;
  background: var(--bg-elev-2); color: var(--text-dim); font-size: 12px; font-weight: 600;
}
.audio-ctl { display: inline-flex; align-items: center; gap: 8px; }

.rate-row { display: grid; grid-template-columns: repeat(4, 1fr); gap: 8px; }
.rate-btn { flex-direction: column; padding: 10px 4px; font-size: 13px; }
.rate-again { background: #7f1d1d; color: #fff; }
.rate-hard { background: #78350f; color: #fff; }
.rate-good { background: #14532d; color: #fff; }
.rate-easy { background: #1e3a8a; color: #fff; }
.rate-key { font-size: 11px; opacity: 0.8; }

.popover {
  position: absolute;
  background: var(--bg-elev-2);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 10px 12px;
  font-size: 13px;
  max-width: 260px;
  z-index: 20;
  box-shadow: 0 8px 24px rgba(0,0,0,0.4);
}
.popover.hidden { display: none; }
.popover .p-roman { font-weight: 600; margin-bottom: 4px; }
.popover .p-tone { color: var(--accent); text-transform: capitalize; }

/* ---------- onboard ---------- */
.onboard-word { text-align: center; padding: 30px 0; }
.onboard-word .thai-big { font-size: 46px; }
.onboard-word .meaning { font-size: 18px; color: var(--text-dim); margin-top: 8px; }
.onboard-progress { text-align: center; color: var(--text-dim); font-size: 13px; margin-bottom: 8px; }

/* ---------- words list ---------- */
.words-filters { display: flex; gap: 8px; margin-bottom: 10px; }
.words-filters select, .words-filters input { flex: 1; }
.word-row {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 12px 4px;
  border-bottom: 1px solid var(--border);
}
.word-row .thai-word { font-size: 18px; }
.word-row .meaning-word { color: var(--text-dim); font-size: 13px; }
.status-pill { font-size: 11px; padding: 4px 8px; border-radius: 999px; background: var(--bg-elev-2); }
.status-known { color: var(--accent); }
.status-learning { color: var(--tone-high); }
.status-new { color: var(--text-dim); }

/* ---------- progress ---------- */
.coverage-head { text-align: center; padding: 10px 0 16px; }
.coverage-num { font-size: 44px; font-weight: 700; color: var(--accent); }
.coverage-label { color: var(--text-dim); font-size: 13px; margin-top: 2px; }
.coverage-sub { color: var(--text-dim); font-size: 12px; margin-top: 4px; }
.grid-legend { display: flex; gap: 10px; flex-wrap: wrap; font-size: 11px; color: var(--text-dim); margin-bottom: 8px; }
.grid-legend .dot { width: 9px; height: 9px; border-radius: 3px; display: inline-block; margin-right: 3px; }
.word-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(14px, 1fr));
  gap: 2px;
  margin-bottom: 20px;
}
.word-tile { aspect-ratio: 1; border-radius: 3px; min-width: 14px; min-height: 14px; cursor: pointer; }
.sheet-backdrop {
  position: fixed; inset: 0; background: rgba(0,0,0,0.5);
  display: flex; align-items: flex-end; z-index: 100;
}
.sheet-backdrop.hidden { display: none; }
.sheet {
  background: var(--bg-elev); border-top: 1px solid var(--border);
  border-radius: 18px 18px 0 0; padding: 20px 18px calc(20px + env(safe-area-inset-bottom));
  width: 100%; text-align: center;
}
.sheet-meta { color: var(--text-dim); font-size: 13px; margin: 6px 0 16px; }
.upcoming-strip { display: flex; justify-content: space-between; gap: 4px; margin-bottom: 20px; }
.upcoming-bar { display: flex; flex-direction: column; align-items: center; flex: 1; }
.bar-count { font-size: 11px; color: var(--text-dim); margin-bottom: 2px; }
.bar-stack { display: flex; flex-direction: column-reverse; width: 12px; background: var(--bg-elev-2); border-radius: 3px; overflow: hidden; }
.bar-due { background: var(--accent); width: 100%; }
.bar-new { background: var(--tone-low); width: 100%; opacity: 0.6; }
.bar-label { font-size: 10px; color: var(--text-dim); margin-top: 4px; }
.streak-inline { font-size: 13px; color: var(--tone-high); font-weight: 500; }
.heatmap { display: flex; flex-wrap: wrap; gap: 3px; }
.heat-cell { width: 16px; height: 16px; border-radius: 3px; }

/* ---------- chat ---------- */
#screen-chat { padding: 0 0 calc(62px + env(safe-area-inset-bottom)); }
.chat-header { padding: 12px 16px; border-bottom: 1px solid var(--border); display: flex; align-items: center; justify-content: space-between; }
.chat-messages { flex: 1; overflow-y: auto; padding: 16px; display: flex; flex-direction: column; gap: 12px; }
.chat-msg { max-width: 85%; padding: 10px 14px; border-radius: 14px; line-height: 1.5; }
.chat-msg.user { align-self: flex-end; background: var(--accent); color: #fff; }
.chat-msg.assistant { align-self: flex-start; background: var(--bg-elev); border: 1px solid var(--border); }
.chat-toast {
  align-self: center;
  background: var(--bg-elev-2);
  color: var(--accent);
  font-size: 13px;
  padding: 6px 14px;
  border-radius: 999px;
}
.chat-input-row {
  display: flex;
  gap: 8px;
  padding: 10px 16px 10px;
  border-top: 1px solid var(--border);
  background: var(--bg-elev);
}
.chat-input-row textarea { flex: 1; min-height: 44px; max-height: 120px; resize: none; }
.icon-btn { width: 48px; padding: 0; display: flex; align-items: center; justify-content: center; font-size: 20px; background: var(--bg-elev-2); }

/* ---------- settings ---------- */
.setting-row { display: flex; justify-content: space-between; align-items: center; padding: 14px 0; border-bottom: 1px solid var(--border); gap: 12px; }
.setting-row label { flex: 1; }
.setting-row .setting-control { flex: 1; max-width: 160px; }

.top-bar { display: flex; align-items: center; justify-content: space-between; margin-bottom: 8px; }
.link-btn { background: none; color: var(--accent); padding: 6px; min-height: auto; }

/* ---------- usage (settings) ---------- */
.usage-box { margin-top: 20px; padding-top: 14px; border-top: 1px solid var(--border); }
.usage-line { color: var(--text-dim); font-size: 13px; }
.usage-detail { margin-top: 8px; display: flex; flex-direction: column; gap: 4px; }
.usage-row {
  display: flex; justify-content: space-between; gap: 10px;
  font-size: 12px; color: var(--text-dim); padding: 4px 0; border-bottom: 1px solid var(--border);
}

/* ---------- story entry / screen ---------- */
.story-entry { text-align: left; }
.story-paragraph {
  background: var(--bg-elev); border: 1px solid var(--border);
  border-radius: 14px; padding: 14px; margin-bottom: 12px;
}
.story-paragraph-controls { display: flex; align-items: center; gap: 10px; margin-top: 8px; }
.story-new-word {
  display: flex; justify-content: space-between; align-items: center;
  padding: 10px 0; border-bottom: 1px solid var(--border); font-size: 15px;
}
.story-question {
  background: var(--bg-elev); border: 1px solid var(--border);
  border-radius: 14px; padding: 14px; margin-bottom: 14px;
}
.story-answer-input { min-height: 60px; margin-top: 8px; resize: vertical; }
.story-check-btn { margin-top: 8px; width: 100%; }
.story-answer-feedback { margin-top: 10px; padding: 10px 12px; border-radius: 10px; font-size: 14px; }
.story-answer-feedback.ok { background: var(--accent-dim); color: var(--text); }
.story-answer-feedback.amber { background: #78350f; color: #fff; }
.story-answer-feedback .better-thai { margin-top: 6px; font-weight: 600; }

/* ---------- talk (voice conversation) ---------- */
#screen-talk { padding: 16px; }
#talk-setup .scope-pills { flex-wrap: wrap; margin-bottom: 16px; }
#talk-setup .scope-pill { flex: 0 0 auto; padding: 10px 14px; }
#talk-conversation { display: flex; flex-direction: column; flex: 1; min-height: 0; }
.talk-scenario-badge {
  text-align: center; color: var(--text-dim); font-size: 13px;
  padding: 6px 0 10px; border-bottom: 1px solid var(--border); margin-bottom: 10px;
}
#talk-messages { flex: 1; overflow-y: auto; display: flex; flex-direction: column; gap: 12px; padding-bottom: 10px; }
.talk-reply-thai { font-size: 20px; margin-bottom: 6px; }
.talk-reply-controls { display: flex; align-items: center; gap: 10px; margin-top: 4px; }
.talk-reply-english { margin-top: 6px; color: var(--text-dim); font-size: 14px; }
.talk-status { text-align: center; color: var(--text-dim); font-size: 13px; min-height: 18px; margin: 6px 0; }
.talk-hold-row { display: flex; justify-content: center; padding: 10px 0 10px; }
.talk-hold-btn {
  width: 100%; max-width: 320px; border-radius: 999px;
  background: var(--accent); color: #fff; font-size: 17px; font-weight: 600; padding: 20px;
  user-select: none; touch-action: none;
}
.talk-hold-btn.recording { background: var(--danger); }
.correction-box {
  align-self: stretch; background: #78350f; color: #fff;
  border-radius: 12px; padding: 10px 14px; font-size: 13px; line-height: 1.5;
}
.correction-box .correction-why { margin-top: 4px; opacity: 0.85; }

::-webkit-scrollbar { width: 6px; }
::-webkit-scrollbar-thumb { background: var(--border); border-radius: 3px; }

@media (min-width: 640px) {
  #app { max-width: 480px; margin: 0 auto; box-shadow: 0 0 0 1px var(--border); min-height: 100vh; }
}
.onboard-audio{margin-top:14px;display:flex;justify-content:center}
.word-row .word-audio-slot{margin:0 8px}
