:root {
  --bg: #f6f4ef;
  --surface: #ffffff;
  --surface-2: #efece4;
  --text: #1d1f23;
  --muted: #6b6f76;
  --border: #e2ded4;
  --accent: #2f6fde;
  --accent-soft: #e3ecfc;
  --me: #2f6fde;
  --me-text: #ffffff;
  --them: #ffffff;
  --sel: #f5c518;
  --sel-text: #1d1f23;
  --ok: #1f9d55;
  --bad: #d64545;
  --beginner: #1f9d55;
  --intermediate: #c27c0e;
  --advanced: #b03a8a;
  --radius: 18px;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #16181c;
    --surface: #1f2228;
    --surface-2: #272b33;
    --text: #e9eaec;
    --muted: #9aa0a8;
    --border: #2f343c;
    --accent: #6c9bff;
    --accent-soft: #23314d;
    --me: #3a6fd8;
    --them: #262a31;
    --sel: #f2cf4a;
    --ok: #4cc77f;
    --bad: #ff7676;
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 17px/1.5 -apple-system, BlinkMacSystemFont, "SF Pro Text", "Segoe UI", Roboto, sans-serif;
  padding-bottom: env(safe-area-inset-bottom);
}
a { color: var(--accent); text-decoration: none; }
button { font: inherit; color: inherit; }
.muted { color: var(--muted); }

.topbar {
  position: sticky; top: 0; z-index: 10;
  display: flex; align-items: center; gap: 12px;
  padding: calc(10px + env(safe-area-inset-top)) 16px 10px;
  background: color-mix(in srgb, var(--bg) 88%, transparent);
  backdrop-filter: blur(12px);
  -webkit-backdrop-filter: blur(12px);
  border-bottom: 1px solid var(--border);
}
.brand { font-weight: 700; color: var(--text); white-space: nowrap; }
.topbar-title { flex: 1; min-width: 0; color: var(--muted); font-size: 15px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.icon-btn { background: none; border: 0; font-size: 22px; padding: 4px 6px; cursor: pointer; color: var(--muted); }

.container { max-width: 760px; margin: 0 auto; padding: 16px 16px 80px; }

/* ---------- Home ---------- */
.hero h1 { font-size: 28px; line-height: 1.2; margin: 8px 0 6px; }
.hero p { margin: 0 0 14px; color: var(--muted); }
.stats { display: flex; gap: 10px; flex-wrap: wrap; margin-bottom: 14px; }
.stat { background: var(--surface); border: 1px solid var(--border); border-radius: 12px; padding: 8px 12px; font-size: 14px; }
.stat b { font-size: 18px; display: block; }
.howto { background: var(--accent-soft); border-radius: 12px; padding: 12px 14px; font-size: 15px; margin-bottom: 18px; }
.howto ol { margin: 6px 0 0; padding-left: 20px; }

.chips { display: flex; gap: 8px; overflow-x: auto; padding-bottom: 4px; margin-bottom: 8px; scrollbar-width: none; }
.chip {
  border: 1px solid var(--border); background: var(--surface); border-radius: 999px;
  padding: 6px 14px; font-size: 14px; cursor: pointer; white-space: nowrap;
}
.chip.on { background: var(--text); color: var(--bg); border-color: var(--text); }

.category { margin-top: 22px; }
.category h2 { font-size: 20px; margin: 0 0 2px; display: flex; justify-content: space-between; align-items: baseline; gap: 8px; }
.category h2 small { font-size: 13px; color: var(--muted); font-weight: 500; }
.category > p { margin: 0 0 10px; color: var(--muted); font-size: 15px; }
.cards { display: grid; gap: 8px; }
.card {
  display: flex; align-items: center; gap: 12px;
  background: var(--surface); border: 1px solid var(--border); border-radius: 14px;
  padding: 12px 14px; color: var(--text);
}
.card:active { transform: scale(.99); }
.card .num { width: 28px; height: 28px; flex: none; border-radius: 50%; display: grid; place-items: center; background: var(--surface-2); font-size: 13px; font-weight: 600; color: var(--muted); }
.card.done .num { background: var(--ok); color: #fff; }
.card .body { flex: 1; min-width: 0; }
.card .title { font-weight: 600; }
.card .meta { font-size: 13px; color: var(--muted); }

.level { font-size: 12px; font-weight: 600; text-transform: capitalize; }
.level.beginner { color: var(--beginner); }
.level.intermediate { color: var(--intermediate); }
.level.advanced { color: var(--advanced); }

/* ---------- Conversation ---------- */
.convo-head h1 { font-size: 24px; line-height: 1.25; margin: 6px 0 4px; }
.setting { background: var(--surface); border: 1px solid var(--border); border-radius: 12px; padding: 10px 14px; font-size: 15px; color: var(--muted); margin: 10px 0; }
.cast { font-size: 14px; color: var(--muted); margin: 0 0 6px; }
.cast b { color: var(--text); }

.toolbar { display: flex; flex-wrap: wrap; gap: 8px; align-items: center; margin: 12px 0 18px; font-size: 14px; }

.thread { display: flex; flex-direction: column; gap: 6px; }
.turn { display: flex; flex-direction: column; align-items: flex-start; max-width: 100%; }
.turn.right { align-items: flex-end; }
.who { font-size: 12px; color: var(--muted); margin: 8px 12px 2px; }
.bubble {
  max-width: 88%;
  background: var(--them); color: var(--text);
  border: 1px solid var(--border);
  border-radius: var(--radius); border-bottom-left-radius: 6px;
  padding: 9px 14px;
  position: relative;
  cursor: pointer;
  -webkit-tap-highlight-color: transparent;
  transition: box-shadow .12s, transform .08s;
}
.bubble:active { transform: scale(.985); }
.turn[data-sp="2"] .bubble { background: var(--accent-soft); }
.bubble.sel, .bubble.playing { box-shadow: 0 0 0 3px var(--sel); }
.slot { width: 100%; }
.numw { text-decoration: underline dotted; text-underline-offset: 3px; text-decoration-thickness: 2px; }
.say {
  max-width: 88%; margin: 4px 6px 2px; font-size: 14px; line-height: 1.45; color: var(--muted);
}
.turn.right .say { text-align: right; }
.say b { color: var(--text); font-weight: 600; }
.say i { font-style: normal; color: var(--accent); }
.say::before { content: "🔢 "; }
.phrase .say { max-width: none; margin: 2px 0; }
.phrase-text { font-weight: 600; cursor: pointer; border-radius: 6px; -webkit-tap-highlight-color: transparent; }
.phrase-text.sel { background: var(--sel); color: var(--sel-text); padding: 0 4px; }
.turn.right .bubble { background: var(--me); color: var(--me-text); border-color: transparent; border-radius: var(--radius); border-bottom-right-radius: 6px; }

/* ---------- Practice panel ---------- */
.practice {
  width: 100%;
  margin: 6px 0 10px;
  background: var(--surface); border: 1px solid var(--border); border-radius: 14px;
  padding: 10px; box-shadow: 0 4px 18px rgba(0,0,0,.06);
}
.practice textarea {
  width: 100%; min-height: 76px; resize: vertical;
  font: inherit; font-size: 17px; /* >=16px prevents iOS zoom */
  color: var(--text); background: var(--bg);
  border: 1px solid var(--border); border-radius: 10px; padding: 10px 12px;
}
.practice textarea:focus { outline: 2px solid var(--accent); outline-offset: -1px; }
.practice .row { display: flex; gap: 6px; flex-wrap: wrap; align-items: center; margin-top: 8px; }
.practice .spacer { flex: 1; }
.btn {
  border: 1px solid var(--border); background: var(--surface); border-radius: 999px;
  padding: 6px 12px; font-size: 14px; cursor: pointer; white-space: nowrap;
}
.btn.primary { background: var(--accent); border-color: var(--accent); color: #fff; }
.btn.ghost { background: none; }
.btn.danger { color: var(--bad); }
.btn:active { transform: scale(.97); }


/* ---------- Phrases & footer ---------- */
.phrases { margin-top: 28px; }
.phrases h2 { font-size: 18px; margin: 0 0 8px; }
.phrase { background: var(--surface); border: 1px solid var(--border); border-radius: 12px; padding: 10px 14px; margin-bottom: 8px; }
.phrase .m { font-size: 14px; color: var(--muted); }

.convo-foot { display: flex; gap: 8px; flex-wrap: wrap; justify-content: space-between; align-items: center; margin-top: 26px; padding-top: 16px; border-top: 1px solid var(--border); }
.convo-foot .btn { padding: 9px 16px; font-size: 15px; }
.btn.done-on { background: var(--ok); border-color: var(--ok); color: #fff; }

/* ---------- Settings dialog ---------- */
dialog { border: 1px solid var(--border); border-radius: 16px; background: var(--surface); color: var(--text); padding: 0; width: min(440px, calc(100vw - 32px)); }
dialog::backdrop { background: rgba(0,0,0,.35); }
.settings-form { padding: 18px; display: grid; gap: 14px; }
.settings-form h2 { margin: 0; font-size: 20px; }
.settings-form label { display: grid; gap: 6px; font-size: 15px; }
.settings-form label.check { grid-template-columns: auto 1fr; align-items: start; gap: 10px; }
.settings-form select { font: inherit; font-size: 16px; padding: 6px; border-radius: 8px; border: 1px solid var(--border); background: var(--bg); color: var(--text); max-width: 100%; }
.settings-actions { display: flex; justify-content: space-between; gap: 8px; }
