:root {
  --bg: #f6f7fb;
  --card: #ffffff;
  --text: #1b1f2a;
  --muted: #667085;
  --line: #e4e7ec;
  --brand: #2f5bea;
  --brand-soft: #e8eeff;
  --good: #12a150;
  --good-soft: #e3f6ea;
  --bad: #d92d20;
  --bad-soft: #fde8e7;
  --warn: #b54708;
  --warn-soft: #fef0dc;
  --radius: 16px;
  --shadow: 0 1px 2px rgba(16, 24, 40, .06), 0 1px 3px rgba(16, 24, 40, .08);
  --tabbar-h: 64px;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0f1218;
    --card: #181c25;
    --text: #e8eaf0;
    --muted: #98a2b3;
    --line: #2a303c;
    --brand: #6d8dff;
    --brand-soft: #1f2a4d;
    --good: #3ccb7f;
    --good-soft: #143222;
    --bad: #ff6b5f;
    --bad-soft: #3a1714;
    --warn: #f5a524;
    --warn-soft: #3a2a10;
    --shadow: none;
    color-scheme: dark;
  }
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { margin: 0; }
body {
  background: var(--bg);
  color: var(--text);
  font: 16px/1.5 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  min-height: 100dvh;
}
button, input, textarea, select { font: inherit; color: inherit; }

/* Top bar */
.topbar {
  position: sticky; top: 0; z-index: 10;
  display: flex; align-items: center; gap: 8px;
  padding: calc(env(safe-area-inset-top) + 10px) 16px 10px;
  background: color-mix(in srgb, var(--bg) 88%, transparent);
  backdrop-filter: blur(10px);
  border-bottom: 1px solid var(--line);
}
.topbar h1 { font-size: 18px; margin: 0; flex: 1; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.icon-btn {
  border: 0; background: transparent; font-size: 30px; line-height: 1; padding: 0 8px 4px 0; cursor: pointer; color: var(--brand);
}

main {
  max-width: 720px; margin: 0 auto;
  padding: 16px 16px calc(var(--tabbar-h) + env(safe-area-inset-bottom) + 24px);
  outline: none;
}

/* Tab bar */
.tabbar {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 10;
  display: grid; grid-template-columns: repeat(5, 1fr);
  background: var(--card); border-top: 1px solid var(--line);
  padding-bottom: env(safe-area-inset-bottom);
}
.tabbar button {
  border: 0; background: none; height: var(--tabbar-h);
  display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 2px;
  font-size: 11px; color: var(--muted); cursor: pointer;
}
.tabbar button span { font-size: 20px; filter: grayscale(1); opacity: .7; }
.tabbar button.active { color: var(--brand); font-weight: 600; }
.tabbar button.active span { filter: none; opacity: 1; }

/* Cards & layout */
.card {
  background: var(--card); border: 1px solid var(--line); border-radius: var(--radius);
  padding: 16px; margin-bottom: 12px; box-shadow: var(--shadow);
}
.card h2, .card h3 { margin: 0 0 6px; }
h2 { font-size: 20px; }
h3 { font-size: 16px; }
.muted { color: var(--muted); }
.small { font-size: 13px; }
.row { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }
.row.between { justify-content: space-between; }
.stack > * + * { margin-top: 10px; }
.grid2 { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.section-title { font-size: 13px; font-weight: 700; text-transform: uppercase; letter-spacing: .04em; color: var(--muted); margin: 20px 4px 8px; }

.stat { text-align: center; padding: 12px 8px; }
.stat b { display: block; font-size: 26px; line-height: 1.2; color: var(--brand); }
.stat span { font-size: 12px; color: var(--muted); }

.list-item {
  display: flex; align-items: center; gap: 12px; width: 100%; text-align: left;
  background: var(--card); border: 1px solid var(--line); border-radius: var(--radius);
  padding: 14px 16px; margin-bottom: 10px; cursor: pointer; box-shadow: var(--shadow);
}
.list-item .emoji { font-size: 26px; }
.list-item .grow { flex: 1; min-width: 0; }
.list-item .grow b { display: block; }
.list-item .chev { color: var(--muted); font-size: 22px; }

.badge {
  display: inline-block; font-size: 12px; font-weight: 600; padding: 2px 8px; border-radius: 999px;
  background: var(--brand-soft); color: var(--brand);
}
.badge.good { background: var(--good-soft); color: var(--good); }
.badge.bad { background: var(--bad-soft); color: var(--bad); }
.badge.warn { background: var(--warn-soft); color: var(--warn); }

/* Buttons */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  min-height: 44px; padding: 10px 16px; border-radius: 12px; border: 1px solid var(--line);
  background: var(--card); cursor: pointer; font-weight: 600;
}
.btn.primary { background: var(--brand); border-color: var(--brand); color: #fff; }
.btn.good { background: var(--good); border-color: var(--good); color: #fff; }
.btn.bad { background: var(--bad); border-color: var(--bad); color: #fff; }
.btn.block { width: 100%; }
.btn.sm { min-height: 36px; padding: 6px 12px; font-size: 14px; }
.btn:disabled { opacity: .5; cursor: default; }
.btn-ghost { border: 0; background: none; color: var(--brand); font-weight: 600; cursor: pointer; padding: 6px; }

.mic {
  width: 76px; height: 76px; border-radius: 50%; border: 0; cursor: pointer;
  background: var(--brand); color: #fff; font-size: 30px; box-shadow: 0 4px 14px rgba(47, 91, 234, .35);
}
.mic.on { background: var(--bad); animation: pulse 1.2s infinite; }
@keyframes pulse { 0% { box-shadow: 0 0 0 0 rgba(217, 45, 32, .5); } 70% { box-shadow: 0 0 0 18px rgba(217, 45, 32, 0); } 100% { box-shadow: 0 0 0 0 rgba(217, 45, 32, 0); } }
.mic-wrap { display: flex; flex-direction: column; align-items: center; gap: 8px; margin: 16px 0; }

/* Forms */
input[type=text], input[type=password], input[type=search], textarea, select {
  width: 100%; padding: 12px; border-radius: 12px; border: 1px solid var(--line); background: var(--bg);
}
textarea { min-height: 160px; resize: vertical; line-height: 1.5; }
label { display: block; font-weight: 600; font-size: 14px; margin: 12px 0 6px; }
input[type=range] { width: 100%; accent-color: var(--brand); }

/* Segmented control */
.seg { display: flex; background: var(--line); padding: 3px; border-radius: 12px; gap: 3px; margin-bottom: 12px; }
.seg button { flex: 1; border: 0; background: none; padding: 8px 4px; border-radius: 9px; font-weight: 600; font-size: 14px; cursor: pointer; color: var(--muted); }
.seg button.active { background: var(--card); color: var(--text); box-shadow: var(--shadow); }

/* Flashcard */
.flash { min-height: 300px; display: flex; flex-direction: column; justify-content: center; text-align: center; }
.flash .term { font-size: 28px; font-weight: 700; margin: 8px 0; }
.flash .meaning { font-size: 17px; }
.flash .example { font-style: italic; margin-top: 10px; }
.rate { display: grid; grid-template-columns: repeat(4, 1fr); gap: 8px; }
.rate .btn { flex-direction: column; gap: 0; padding: 8px 4px; font-size: 14px; }
.rate .btn small { font-weight: 400; font-size: 11px; opacity: .85; }

/* Script text */
.script-text { font-size: 18px; line-height: 1.75; }
.sent { cursor: pointer; border-radius: 6px; padding: 1px 0; }
.sent.playing { background: var(--brand-soft); }
.w-hidden {
  display: inline-block; min-width: 1.6em; border-bottom: 2px solid var(--brand); color: transparent;
  cursor: pointer; line-height: 1.2; user-select: none;
}
.w-hint { color: var(--brand); font-weight: 600; }
.w-ok { color: var(--good); }
.w-miss { background: var(--bad-soft); color: var(--bad); text-decoration: line-through; text-decoration-thickness: 1px; border-radius: 4px; padding: 0 2px; }
.w-extra { background: var(--warn-soft); color: var(--warn); border-radius: 4px; padding: 0 2px; }

.transcript { min-height: 60px; padding: 12px; border-radius: 12px; background: var(--bg); border: 1px dashed var(--line); }
.transcript .interim { color: var(--muted); }

.score-ring { font-size: 40px; font-weight: 800; text-align: center; }
.progress { height: 8px; background: var(--line); border-radius: 99px; overflow: hidden; }
.progress > div { height: 100%; background: var(--brand); border-radius: 99px; }

.chip { display: inline-block; padding: 4px 10px; border-radius: 999px; margin: 3px 3px 0 0; font-size: 14px; background: var(--bg); border: 1px solid var(--line); }
.chip.used { background: var(--good-soft); border-color: transparent; color: var(--good); }
.chip.missed { background: var(--bad-soft); border-color: transparent; color: var(--bad); }

/* Chat */
.chat { display: flex; flex-direction: column; gap: 10px; }
.bubble { max-width: 88%; padding: 10px 14px; border-radius: 18px; }
.bubble.ai { background: var(--card); border: 1px solid var(--line); align-self: flex-start; border-bottom-left-radius: 6px; }
.bubble.me { background: var(--brand); color: #fff; align-self: flex-end; border-bottom-right-radius: 6px; }
.coach { align-self: stretch; background: var(--good-soft); border-radius: 14px; padding: 10px 14px; font-size: 14px; }
.coach b { color: var(--good); }

details summary { cursor: pointer; font-weight: 600; color: var(--brand); padding: 4px 0; }
.word-row { padding: 12px 0; border-bottom: 1px solid var(--line); }
.word-row:last-child { border-bottom: 0; }

#toast {
  position: fixed; left: 50%; bottom: calc(var(--tabbar-h) + env(safe-area-inset-bottom) + 14px);
  transform: translateX(-50%) translateY(20px); opacity: 0; transition: .25s;
  background: var(--text); color: var(--bg); padding: 10px 16px; border-radius: 12px; font-size: 14px;
  pointer-events: none; z-index: 20; max-width: calc(100% - 32px);
}
#toast.show { opacity: 1; transform: translateX(-50%) translateY(0); }

.spinner { width: 22px; height: 22px; border: 3px solid var(--line); border-top-color: var(--brand); border-radius: 50%; animation: spin .8s linear infinite; display: inline-block; vertical-align: middle; }
@keyframes spin { to { transform: rotate(360deg); } }
.empty { text-align: center; padding: 30px 10px; color: var(--muted); }
