:root {
  --bg: #f6f5f1;
  --surface: #ffffff;
  --text: #1c1c1e;
  --muted: #6b6b73;
  --border: #e3e1da;
  --accent: #1f3a8a;
  --accent-text: #ffffff;
  --me: #1f3a8a;
  --me-text: #ffffff;
  --tutor: #ffffff;
  --fix-bg: #fff6e0;
  --fix-border: #f0d48a;
  --better: #1b7a3d;
  --wrong: #b3261e;
  --danger-bg: #fde8e7;
  --danger-text: #8c1d18;
  --rec: #d93025;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #111114;
    --surface: #1d1d22;
    --text: #f1f1f3;
    --muted: #9b9ba5;
    --border: #2e2e35;
    --accent: #7c9cff;
    --accent-text: #0b1330;
    --me: #2c4bb0;
    --me-text: #ffffff;
    --tutor: #1d1d22;
    --fix-bg: #2a2516;
    --fix-border: #5c4c1f;
    --better: #6fd68f;
    --wrong: #ff8a80;
    --danger-bg: #3a1714;
    --danger-text: #ffb4ab;
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }
html, body { height: 100%; margin: 0; }
body {
  background: var(--bg);
  color: var(--text);
  font: 16px/1.45 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  -webkit-text-size-adjust: 100%;
}
button, select, input { font: inherit; color: inherit; }
button { cursor: pointer; }

.primary, .secondary {
  border: 0; border-radius: 12px; padding: 12px 18px; font-weight: 600;
}
.primary { background: var(--accent); color: var(--accent-text); }
.secondary { background: var(--surface); border: 1px solid var(--border); }
.link { background: none; border: 0; color: var(--muted); text-decoration: underline; padding: 12px; }
.icon-btn {
  display: grid; place-items: center; width: 40px; height: 40px; flex: none;
  border: 1px solid var(--border); border-radius: 10px; background: var(--surface);
  font-size: 22px; line-height: 1;
}

/* Gate */
.gate { min-height: 100%; display: grid; place-items: center; padding: 24px 16px; }
.gate-card {
  width: 100%; max-width: 340px; display: flex; flex-direction: column; gap: 12px; text-align: center;
}
.gate-card img { align-self: center; border-radius: 14px; }
.gate-card h1 { margin: 0; font-size: 28px; }
.gate-card p { margin: 0 0 8px; color: var(--muted); }
.gate-card input {
  padding: 12px 14px; border-radius: 12px; border: 1px solid var(--border); background: var(--surface);
  font-size: 16px; /* >=16px prevents iOS zoom on focus */
}
.gate-error { color: var(--wrong); min-height: 1.4em; }

/* App layout */
.app {
  height: 100%; display: flex; flex-direction: column;
  max-width: 720px; margin: 0 auto;
  padding: env(safe-area-inset-top) env(safe-area-inset-right) 0 env(safe-area-inset-left);
}
.topbar { display: flex; gap: 8px; padding: 10px 16px; align-items: center; }
.topbar select {
  min-width: 0; height: 40px; padding: 0 10px; border-radius: 10px;
  border: 1px solid var(--border); background: var(--surface); font-size: 16px;
}
.topbar #scenario { flex: 1; }

.chat {
  flex: 1; overflow-y: auto; -webkit-overflow-scrolling: touch;
  padding: 8px 16px 16px; display: flex; flex-direction: column; gap: 10px;
}
.empty { margin: auto; text-align: center; color: var(--muted); }
.empty p { margin: 0 0 12px; }

.msg { max-width: 85%; display: flex; flex-direction: column; gap: 6px; }
.msg.me { align-self: flex-end; align-items: flex-end; }
.msg.tutor { align-self: flex-start; }
.bubble {
  padding: 10px 14px; border-radius: 18px; white-space: pre-wrap; overflow-wrap: anywhere;
}
.me .bubble { background: var(--me); color: var(--me-text); border-bottom-right-radius: 6px; }
.tutor .bubble { background: var(--tutor); border: 1px solid var(--border); border-bottom-left-radius: 6px; }
.bubble.pending { opacity: .6; font-style: italic; }
.tutor-row { display: flex; align-items: flex-end; gap: 6px; }
.replay {
  flex: none; width: 34px; height: 34px; border-radius: 50%; border: 1px solid var(--border);
  background: var(--surface); display: grid; place-items: center; color: var(--accent);
}
.replay[aria-busy="true"] { opacity: .5; }

.vocab { display: flex; flex-wrap: wrap; gap: 6px; padding-left: 4px; }
.vocab span {
  font-size: 13px; padding: 3px 9px; border-radius: 999px; background: var(--surface); border: 1px solid var(--border);
}
.vocab b { font-weight: 600; }
.vocab i { color: var(--muted); font-style: normal; }

.fixes {
  width: 100%; max-width: 420px; text-align: left;
  background: var(--fix-bg); border: 1px solid var(--fix-border); border-radius: 12px; font-size: 14px;
}
.fixes summary { padding: 8px 12px; cursor: pointer; font-weight: 600; }
.fixes.none { background: transparent; border-color: var(--border); color: var(--muted); }
.fixes.none summary { cursor: default; list-style: none; font-weight: 500; }
.fixes.none summary::-webkit-details-marker { display: none; }
.fixes ul { margin: 0; padding: 0 12px 10px; list-style: none; display: flex; flex-direction: column; gap: 10px; }
.fix-said { color: var(--wrong); text-decoration: line-through; }
.fix-better { color: var(--better); font-weight: 600; }
.fix-why { color: var(--muted); margin-top: 2px; }

.error-banner {
  margin: 0 16px 8px; padding: 10px 12px; border-radius: 12px;
  background: var(--danger-bg); color: var(--danger-text);
  display: flex; gap: 10px; align-items: flex-start; font-size: 14px;
}
.error-banner span { flex: 1; white-space: pre-wrap; overflow-wrap: anywhere; }
.error-banner button { background: none; border: 0; font-size: 20px; line-height: 1; padding: 0 4px; color: inherit; }

/* Controls */
.controls {
  display: flex; flex-direction: column; align-items: center; gap: 8px;
  padding: 10px 16px calc(14px + env(safe-area-inset-bottom));
  border-top: 1px solid var(--border); background: var(--bg);
}
.options { display: flex; gap: 16px; align-items: center; }
.segmented { display: flex; border: 1px solid var(--border); border-radius: 10px; overflow: hidden; background: var(--surface); }
.segmented button { border: 0; background: none; padding: 6px 14px; font-size: 14px; }
.segmented button[aria-checked="true"] { background: var(--accent); color: var(--accent-text); }
.toggle { display: flex; gap: 6px; align-items: center; font-size: 14px; }
.toggle input { width: 18px; height: 18px; accent-color: var(--accent); }

.talk {
  width: 96px; height: 96px; border-radius: 50%; border: 0;
  background: var(--accent); color: var(--accent-text);
  display: grid; place-items: center;
  box-shadow: 0 6px 18px rgb(0 0 0 / .18);
  touch-action: none; user-select: none; -webkit-user-select: none; -webkit-touch-callout: none;
  -webkit-tap-highlight-color: transparent;
  transition: transform .12s, background .12s;
}
.talk.recording { background: var(--rec); color: #fff; transform: scale(1.08); animation: pulse 1.2s infinite; }
.talk:disabled { opacity: .5; }
@keyframes pulse {
  0% { box-shadow: 0 0 0 0 rgb(217 48 37 / .5); }
  100% { box-shadow: 0 0 0 22px rgb(217 48 37 / 0); }
}
.status { margin: 0; min-height: 1.4em; color: var(--muted); font-size: 14px; }

/* History sheet */
.sheet {
  width: min(520px, calc(100% - 32px)); max-height: 80vh; padding: 0;
  border: 1px solid var(--border); border-radius: 16px; background: var(--surface); color: var(--text);
}
.sheet::backdrop { background: rgb(0 0 0 / .4); }
.sheet-head { display: flex; justify-content: space-between; align-items: center; padding: 12px 16px; border-bottom: 1px solid var(--border); }
.sheet h2 { margin: 0; font-size: 18px; }
#history-list { list-style: none; margin: 0; padding: 8px; }
#history-list li { display: flex; gap: 8px; align-items: center; }
#history-list .open {
  flex: 1; min-width: 0; text-align: left; background: none; border: 0; padding: 10px 8px; border-radius: 10px;
}
#history-list .open:hover { background: var(--bg); }
#history-list .open small { display: block; color: var(--muted); }
#history-list .open span { display: block; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
#history-list li.current .open { background: var(--bg); }
#history-list .del { background: none; border: 0; color: var(--muted); font-size: 20px; padding: 8px; }
#history-list .none { color: var(--muted); padding: 12px 8px; }
#logout { display: block; margin: 0 auto 8px; }
