:root {
  --bg: #f6f5f2;
  --surface: #ffffff;
  --surface-2: #efede8;
  --text: #1c1d21;
  --muted: #6b6d74;
  --line: #e1ded7;
  --accent: #c8402f;          /* hanko red */
  --accent-soft: #f6dcd6;
  --ok: #2f7d4f;
  --ok-soft: #dcefe3;
  --warn: #b7791f;
  --warn-soft: #f7ead3;
  --live: #2563eb;
  --radius: 14px;
  --font: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  --font-jp: "Hiragino Sans", "Hiragino Kaku Gothic ProN", "Noto Sans JP", "Yu Gothic UI", "Meiryo", system-ui, sans-serif;
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #0f1115;
    --surface: #181b21;
    --surface-2: #20242c;
    --text: #eceae5;
    --muted: #9a9ca3;
    --line: #2a2e37;
    --accent: #ef6b57;
    --accent-soft: #3a211d;
    --ok: #5cc486;
    --ok-soft: #173123;
    --warn: #e0a84a;
    --warn-soft: #33281a;
    --live: #6b9bff;
    color-scheme: dark;
  }
}
:root[data-theme="dark"] {
  --bg: #0f1115; --surface: #181b21; --surface-2: #20242c; --text: #eceae5; --muted: #9a9ca3;
  --line: #2a2e37; --accent: #ef6b57; --accent-soft: #3a211d; --ok: #5cc486; --ok-soft: #173123;
  --warn: #e0a84a; --warn-soft: #33281a; --live: #6b9bff; color-scheme: dark;
}

* { box-sizing: border-box; }
html, body { margin: 0; }
body {
  background: var(--bg);
  color: var(--text);
  font: 16px/1.5 var(--font);
  min-height: 100dvh;
  display: flex;
  flex-direction: column;
  -webkit-tap-highlight-color: transparent;
}
button, input { font: inherit; color: inherit; }
button { cursor: pointer; }
.jp { font-family: var(--font-jp); }
.muted { color: var(--muted); }
.error { color: var(--accent); }
[hidden] { display: none !important; }

/* ── Top bar ─────────────────────────────────────── */
.topbar {
  display: flex; align-items: center; justify-content: space-between; gap: 12px;
  padding: max(10px, env(safe-area-inset-top)) 16px 10px;
  border-bottom: 1px solid var(--line);
  background: var(--surface);
  position: sticky; top: 0; z-index: 5;
}
.brand { font-weight: 650; display: flex; align-items: center; gap: 8px; letter-spacing: .01em; }
.brand-mark {
  display: inline-grid; place-items: center; width: 28px; height: 28px; border-radius: 7px;
  background: var(--accent); color: #fff; font-family: var(--font-jp); font-size: 16px;
}
.tabs { display: flex; background: var(--surface-2); border-radius: 999px; padding: 3px; }
.tabs button {
  border: 0; background: transparent; padding: 6px 16px; border-radius: 999px; font-weight: 550; color: var(--muted);
}
.tabs button[aria-selected="true"] { background: var(--surface); color: var(--text); box-shadow: 0 1px 2px rgb(0 0 0 / .12); }

.banner {
  margin: 12px 16px 0; padding: 10px 14px; border-radius: 10px;
  background: var(--warn-soft); color: var(--text); border: 1px solid var(--warn);
}
.banner.error { background: var(--accent-soft); border-color: var(--accent); }

.view { flex: 1; width: 100%; max-width: 1180px; margin: 0 auto; padding: 16px; }

/* ── Drive view ──────────────────────────────────── */
.drive {
  display: flex; flex-direction: column; gap: 20px; max-width: 640px;
  padding-bottom: max(16px, env(safe-area-inset-bottom));
}
.captions {
  flex: 1; min-height: 34dvh; display: flex; flex-direction: column; justify-content: flex-end; gap: 10px;
  padding: 8px 4px;
}
.cap-tutor {
  margin: 0; font-size: clamp(22px, 5.4vw, 32px); line-height: 1.45; font-weight: 500;
  overflow-wrap: anywhere; max-height: 42dvh; overflow-y: auto;
}
.cap-you { margin: 0; color: var(--muted); font-size: 18px; min-height: 1.5em; overflow-wrap: anywhere; }
.cap-you:not(:empty)::before { content: "You: "; font-weight: 600; }

.attempt {
  align-self: flex-start; display: inline-flex; gap: 10px; align-items: baseline;
  padding: 8px 14px; border-radius: 999px; font-size: 17px; animation: pop .25s ease-out;
}
.attempt.ok { background: var(--ok-soft); color: var(--ok); }
.attempt.miss { background: var(--accent-soft); color: var(--accent); }
.attempt .jp { font-size: 20px; color: var(--text); }
@keyframes pop { from { transform: scale(.92); opacity: 0; } to { transform: none; opacity: 1; } }

.controls { display: flex; flex-direction: column; align-items: center; gap: 14px; }
.status-row { display: flex; align-items: center; gap: 8px; font-weight: 550; font-size: 17px; }
.dot { width: 10px; height: 10px; border-radius: 50%; background: var(--muted); }
.dot[data-state="listening"] { background: var(--ok); }
.dot[data-state="speaking"] { background: var(--live); }
.dot[data-state="connecting"], .dot[data-state="reconnecting"] { background: var(--warn); animation: blink 1s infinite; }
.dot[data-state="error"] { background: var(--accent); }
@keyframes blink { 50% { opacity: .3; } }

.btn-main {
  position: relative; width: min(56vw, 210px); aspect-ratio: 1; border-radius: 50%; border: 0;
  background: var(--accent); color: #fff; font-size: 26px; font-weight: 650; letter-spacing: .02em;
  box-shadow: 0 8px 28px rgb(200 64 47 / .35);
  transition: transform .1s ease, background .2s ease;
}
.btn-main:active { transform: scale(.97); }
.btn-main[data-state="running"] { background: var(--surface-2); color: var(--text); box-shadow: none; border: 2px solid var(--line); }
.btn-main .ring {
  position: absolute; inset: -8px; border-radius: 50%; border: 3px solid var(--ok);
  opacity: 0; transform: scale(var(--level, 1)); transition: transform .08s linear, opacity .2s;
  pointer-events: none;
}
.btn-main[data-state="running"] .ring { opacity: .8; }
.btn-main:focus-visible, .btn-pill:focus-visible, .chip:focus-visible, .tabs button:focus-visible {
  outline: 3px solid var(--live); outline-offset: 3px;
}

.secondary { display: flex; gap: 10px; }
.btn-pill {
  border: 1px solid var(--line); background: var(--surface); padding: 10px 20px; border-radius: 999px;
  font-weight: 550; min-height: 44px;
}
.btn-pill[aria-pressed="true"] { background: var(--accent-soft); border-color: var(--accent); color: var(--accent); }
.btn-pill:disabled { opacity: .45; cursor: default; }
.btn-pill.small { padding: 4px 12px; min-height: 32px; font-size: 14px; }
.hint { margin: 0; color: var(--muted); font-size: 14px; text-align: center; }

.parked {
  border: 1px solid var(--line); border-radius: var(--radius); background: var(--surface); padding: 4px 14px;
}
.parked summary { padding: 10px 0; cursor: pointer; color: var(--muted); font-weight: 550; }
.parked[open] summary { margin-bottom: 6px; }
.ask { display: flex; gap: 8px; margin-bottom: 12px; }
.ask input, .search, #passcode-input {
  flex: 1; min-width: 0; padding: 10px 14px; border-radius: 10px; border: 1px solid var(--line);
  background: var(--bg);
}
.toggle { display: flex; gap: 10px; align-items: flex-start; font-size: 14px; color: var(--muted); margin-bottom: 12px; }
.toggle input { margin-top: 3px; width: 18px; height: 18px; accent-color: var(--accent); }

/* ── Review view ─────────────────────────────────── */
.review { display: flex; flex-direction: column; gap: 16px; }
.stats { display: grid; grid-template-columns: repeat(auto-fit, minmax(140px, 1fr)); gap: 12px; }
.stat { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); padding: 14px 16px; }
.stat .label { color: var(--muted); font-size: 13px; }
.stat .value { font-size: 28px; font-weight: 650; font-variant-numeric: tabular-nums; line-height: 1.2; }

.card {
  background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); padding: 16px;
  min-width: 0;
}
.card h2 { font-size: 17px; margin: 0 0 8px; }
.card-head { display: flex; justify-content: space-between; align-items: center; gap: 12px; flex-wrap: wrap; margin-bottom: 10px; }
.card-head h2 { margin: 0; }
.lesson p { margin: 4px 0; }
.lesson #lesson-next:not(:empty)::before { content: "Next up: "; font-weight: 600; }
.link { color: var(--live); font-size: 14px; text-decoration: none; }
.link:hover { text-decoration: underline; }

.review-grid { display: grid; grid-template-columns: minmax(0, 1.7fr) minmax(0, 1fr); gap: 16px; align-items: start; }
@media (max-width: 860px) { .review-grid { grid-template-columns: minmax(0, 1fr); } }

.filters { display: flex; gap: 6px; flex-wrap: wrap; }
.chip { border: 1px solid var(--line); background: transparent; padding: 4px 12px; border-radius: 999px; font-size: 14px; color: var(--muted); }
.chip[aria-pressed="true"] { background: var(--text); color: var(--bg); border-color: var(--text); }
.search { width: 100%; margin-bottom: 10px; }

.table-wrap { overflow-x: auto; }
.vocab { width: 100%; border-collapse: collapse; font-size: 15px; }
.vocab th { text-align: left; font-size: 12px; text-transform: uppercase; letter-spacing: .04em; color: var(--muted); font-weight: 600; padding: 6px 8px; border-bottom: 1px solid var(--line); }
.vocab td { padding: 8px; border-bottom: 1px solid var(--line); vertical-align: top; }
.vocab tr:last-child td { border-bottom: 0; }
.vocab .term { font-family: var(--font-jp); font-size: 22px; line-height: 1.2; }
.vocab .reading .kana { font-family: var(--font-jp); display: block; }
.vocab .reading .romaji { color: var(--muted); font-size: 13px; }
.vocab .note { display: block; color: var(--muted); font-size: 13px; }
.num { text-align: right; font-variant-numeric: tabular-nums; white-space: nowrap; }
.miss-count { color: var(--accent); }
.bar { width: 72px; height: 6px; border-radius: 3px; background: var(--surface-2); overflow: hidden; margin-top: 4px; }
.bar > span { display: block; height: 100%; background: var(--ok); }
.badge { font-size: 12px; font-weight: 600; padding: 1px 8px; border-radius: 999px; white-space: nowrap; }
.badge.weak { background: var(--accent-soft); color: var(--accent); }
.badge.learning { background: var(--warn-soft); color: var(--warn); }
.badge.mastered { background: var(--ok-soft); color: var(--ok); }
.badge.live { background: var(--live); color: #fff; }

.session-list { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 6px; max-height: 520px; overflow-y: auto; }
.session-list button {
  width: 100%; text-align: left; border: 1px solid var(--line); background: var(--bg); border-radius: 10px; padding: 10px 12px;
}
.session-list button:hover, .session-list button[aria-current="true"] { border-color: var(--text); }
.session-list .when { font-weight: 600; display: flex; justify-content: space-between; gap: 8px; }
.session-list .meta { color: var(--muted); font-size: 13px; }
.session-list .sum { font-size: 13px; margin-top: 2px; display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden; }

.transcript { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 10px; }
.transcript li { max-width: 80ch; }
.transcript .who { font-size: 12px; font-weight: 700; text-transform: uppercase; letter-spacing: .05em; color: var(--muted); }
.transcript .tutor .text { font-family: var(--font-jp); font-size: 18px; }
.transcript .you { align-self: flex-end; text-align: right; }
.transcript .you .text { background: var(--surface-2); display: inline-block; padding: 6px 12px; border-radius: 12px; }
.transcript .cut { color: var(--muted); font-size: 12px; }
.transcript .event { align-self: center; font-size: 14px; }
.empty { margin: 8px 0 0; }

dialog { border: 1px solid var(--line); border-radius: var(--radius); background: var(--surface); color: var(--text); padding: 20px; width: min(92vw, 380px); }
dialog::backdrop { background: rgb(0 0 0 / .5); }
dialog form { display: flex; flex-direction: column; gap: 12px; }
dialog h2 { margin: 0; }
dialog p { margin: 0; }

@media (prefers-reduced-motion: reduce) { * { animation: none !important; transition: none !important; } }
