:root {
  --bg: #0b1220;
  --bg-elev: #131c2e;
  --bg-card: #182236;
  --line: #243046;
  --text: #e6ecf5;
  --muted: #98a3b8;
  --accent: #f59e0b;
  --accent-2: #f97316;
  --en: #93c5fd;
  --good: #4ade80;
  --bad: #fca5a5;
  --user-bubble: #1e3a5f;
  --assistant-bubble: #1f2937;
  --radius: 14px;
  --shadow: 0 4px 14px rgba(0,0,0,0.25);
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Hiragino Sans", "Yu Gothic", system-ui, sans-serif;
  font-size: 16px;
  line-height: 1.55;
  -webkit-text-size-adjust: 100%;
}

body {
  max-width: 720px;
  margin: 0 auto;
  padding: env(safe-area-inset-top) env(safe-area-inset-right) env(safe-area-inset-bottom) env(safe-area-inset-left);
}

/* ------- Header ------- */
.app-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  padding: 12px 16px 8px;
  position: sticky;
  top: 0;
  background: linear-gradient(180deg, var(--bg) 0%, rgba(11,18,32,0.92) 100%);
  backdrop-filter: blur(8px);
  z-index: 10;
}
.brand h1 { font-size: 17px; margin: 0; letter-spacing: 0.02em; }
.tagline { font-size: 11px; margin: 2px 0 0; color: var(--muted); }
.header-actions { display: flex; gap: 6px; align-items: center; }
.voice-select {
  background: var(--bg-elev);
  color: var(--text);
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 6px 8px;
  max-width: 140px;
  font-size: 12px;
}

/* ------- Tabs ------- */
.tabs {
  display: flex;
  gap: 8px;
  padding: 8px 16px 4px;
  position: sticky;
  top: 64px;
  background: var(--bg);
  z-index: 9;
}
.tabs button {
  flex: 1 1 0;
  background: var(--bg-elev);
  color: var(--text);
  border: 1px solid var(--line);
  border-radius: 999px;
  padding: 10px 12px;
  font-size: 14px;
  cursor: pointer;
  font-family: inherit;
}
.tabs button[aria-selected="true"] {
  background: var(--accent);
  border-color: var(--accent);
  color: #1a1a1a;
  font-weight: 700;
}

.tab-panel { padding: 0 16px 96px; }

.section { margin-top: 16px; }
.section-head { margin-bottom: 10px; }
.section-head h2 { margin: 0 0 4px; font-size: 15px; letter-spacing: 0.02em; }

.phrase-list { display: flex; flex-direction: column; gap: 10px; }

/* ------- Buttons ------- */
.icon-btn {
  background: var(--bg-elev);
  border: 1px solid var(--line);
  color: var(--text);
  width: 36px; height: 36px;
  border-radius: 10px;
  font-size: 16px;
  cursor: pointer;
  display: grid; place-items: center;
  flex: 0 0 auto;
  font-family: inherit;
}
.icon-btn:active { transform: scale(0.96); }
.icon-btn.delete { background: #3a1212; border-color: #6b2424; }
.icon-btn.edit   { background: #1a2a45; border-color: #2d4a72; }

.primary-btn {
  background: var(--accent);
  color: #1a1a1a;
  border: none;
  border-radius: 10px;
  padding: 10px 14px;
  font-weight: 700;
  cursor: pointer;
  font-size: 14px;
  font-family: inherit;
}
.primary-btn:disabled { opacity: 0.6; cursor: progress; }

.ghost-btn {
  background: transparent;
  color: var(--text);
  border: 1px solid var(--line);
  border-radius: 10px;
  padding: 8px 12px;
  font-size: 13px;
  cursor: pointer;
  font-family: inherit;
}

.add-note-btn { margin-top: 10px; width: 100%; padding: 12px; }

/* ------- Vocab card ------- */
.card {
  background: var(--bg-card);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 14px;
  box-shadow: var(--shadow);
}
.vocab-head {
  display: flex; align-items: center; justify-content: space-between;
  gap: 8px;
}
.vocab-en {
  font-size: 18px;
  font-weight: 700;
  color: var(--en);
  line-height: 1.3;
}
.vocab-ja { margin: 6px 0 4px; font-size: 14px; }
.vocab-pron { margin: 0; color: #ffd28a; font-size: 12px; }
.meta { margin-top: 8px; }
.meta summary {
  color: var(--muted);
  font-size: 12px;
  cursor: pointer;
  list-style: none;
}
.meta summary::-webkit-details-marker { display: none; }
.meta summary::before { content: "▸ "; }
.meta[open] summary::before { content: "▾ "; }
.vocab-example-en {
  color: var(--en);
  font-size: 14px;
  margin: 6px 0 2px;
  font-weight: 600;
}
.vocab-example-ja {
  color: var(--muted);
  font-size: 13px;
  margin: 0 0 4px;
}

/* ------- Note card ------- */
.note-head {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 8px;
  margin-bottom: 6px;
}
.note-topic {
  font-size: 11px;
  color: #1a1a1a;
  background: #fcd34d;
  padding: 2px 8px;
  border-radius: 999px;
  font-weight: 700;
  align-self: flex-start;
  min-height: 18px;
}
.note-topic:empty { display: none; }
.card-actions { display: flex; gap: 4px; flex-wrap: wrap; justify-content: flex-end; }
.note-ja { margin: 4px 0; font-size: 14px; color: var(--text); }
.note-en {
  margin: 6px 0 0;
  color: var(--en);
  font-size: 15px;
  font-weight: 600;
  line-height: 1.45;
}

/* ------- Modal ------- */
.modal {
  border: none;
  border-radius: var(--radius);
  padding: 0;
  background: var(--bg-elev);
  color: var(--text);
  max-width: 92vw;
  width: 520px;
}
.modal::backdrop { background: rgba(0,0,0,0.6); }
.modal-inner { padding: 16px; display: flex; flex-direction: column; gap: 10px; }
.modal-head {
  display: flex; align-items: center; justify-content: space-between;
  margin-bottom: 4px;
}
.modal-head h2 { margin: 0; font-size: 17px; }
.modal-label {
  display: flex; flex-direction: column; gap: 6px;
  font-size: 13px; color: var(--muted);
}
.modal-label textarea, .modal-label input, .modal-label select {
  background: var(--bg-card);
  border: 1px solid var(--line);
  color: var(--text);
  border-radius: 10px;
  padding: 10px;
  font-size: 14px;
  font-family: inherit;
}
.modal-actions {
  display: flex; gap: 8px; justify-content: flex-end;
  margin-top: 6px; flex-wrap: wrap;
}

/* ------- Roleplay ------- */
.rp-setup { padding: 8px 0; display: flex; flex-direction: column; gap: 10px; }

.rp-chat { display: flex; flex-direction: column; height: calc(100dvh - 200px); min-height: 420px; }
.rp-bar {
  display: flex; align-items: center; justify-content: space-between;
  padding: 8px 0;
}
.rp-scene-label {
  font-size: 13px; color: var(--muted);
}
.rp-thread {
  flex: 1;
  overflow-y: auto;
  padding: 8px 0;
  display: flex; flex-direction: column; gap: 10px;
}
.rp-bubble {
  max-width: 86%;
  padding: 10px 12px;
  border-radius: 14px;
  position: relative;
  background: var(--assistant-bubble);
  border: 1px solid var(--line);
}
.rp-bubble.rp-user {
  background: var(--user-bubble);
  align-self: flex-end;
  border-color: #2d4a72;
}
.rp-bubble.rp-assistant {
  align-self: flex-start;
}
.rp-bubble.rp-thinking { opacity: 0.65; font-style: italic; }
.rp-bubble.rp-error { border-color: var(--bad); background: rgba(252,165,165,0.1); }
.rp-en { margin: 0; font-size: 15px; line-height: 1.45; color: var(--text); }
.rp-bubble.rp-assistant .rp-en { color: var(--en); font-weight: 600; }
.rp-ja {
  margin: 6px 0 0;
  font-size: 12px;
  color: var(--muted);
  line-height: 1.5;
}
.rp-speak {
  margin-top: 6px;
  width: 32px; height: 32px;
  font-size: 14px;
}
.rp-feedback {
  align-self: flex-start;
  max-width: 86%;
  padding: 8px 10px;
  font-size: 12px;
  color: #1a1a1a;
  background: #fde68a;
  border-radius: 10px;
  font-weight: 600;
}
.rp-input-row {
  display: flex; gap: 8px;
  padding: 8px 0;
  border-top: 1px solid var(--line);
  background: var(--bg);
  position: sticky; bottom: 0;
}
.rp-input-row input {
  flex: 1;
  min-width: 0;
  background: var(--bg-card);
  border: 1px solid var(--line);
  color: var(--text);
  border-radius: 10px;
  padding: 10px 12px;
  font-size: 15px;
  font-family: inherit;
}
.rp-mic.recording {
  background: #b91c1c;
  border-color: #ef4444;
  color: #fff;
  animation: micPulse 1.1s ease-in-out infinite;
}
@keyframes micPulse {
  0%, 100% { box-shadow: 0 0 0 0 rgba(239, 68, 68, 0.55); }
  50%      { box-shadow: 0 0 0 10px rgba(239, 68, 68, 0); }
}

/* ------- Quiz ------- */
.quiz-progress {
  display: flex; justify-content: space-between; gap: 8px;
  font-size: 13px; color: var(--muted);
  background: var(--bg-card);
  border: 1px solid var(--line);
  border-radius: 999px;
  padding: 6px 14px;
  margin: 10px 0;
}
.quiz-progress #quizScore { color: var(--text); font-weight: 700; }
.quiz-progress #quizStreak { color: var(--accent); font-weight: 700; }
.quiz-question {
  font-size: 18px;
  font-weight: 600;
  background: var(--bg-card);
  border: 1px solid var(--line);
  border-radius: 12px;
  padding: 16px;
  margin: 6px 0;
  line-height: 1.5;
}
.quiz-options { display: flex; flex-direction: column; gap: 10px; }
.quiz-options button {
  background: var(--bg-card);
  color: var(--text);
  border: 1.5px solid var(--line);
  border-radius: 12px;
  padding: 14px 16px;
  font-size: 15px;
  text-align: left;
  cursor: pointer;
  line-height: 1.4;
  font-family: inherit;
}
.quiz-options button:active { transform: scale(0.98); }
.quiz-options button[disabled] { cursor: default; }
.quiz-options button.correct { background: rgba(74,222,128,0.18); border-color: var(--good); }
.quiz-options button.wrong   { background: rgba(252,165,165,0.16); border-color: var(--bad); }
.quiz-options button.fade    { opacity: 0.5; }
.quiz-feedback {
  background: var(--bg-card);
  border: 1px solid var(--line);
  border-radius: 10px;
  padding: 10px 12px;
  font-size: 14px;
  line-height: 1.6;
  margin-top: 8px;
}
.quiz-feedback .verdict { font-weight: 700; }
.quiz-feedback .verdict.ok { color: var(--good); }
.quiz-feedback .verdict.ng { color: var(--bad); }
.quiz-stats {
  background: var(--bg-card);
  border: 1px solid var(--line);
  border-radius: 10px;
  padding: 10px 12px;
  font-size: 13px;
  color: var(--muted);
}
.quiz-stats strong { color: var(--text); }
.quiz-result-score { text-align: center; padding: 8px 0; }
#quizResultBig {
  font-size: 44px;
  font-weight: 900;
  margin: 6px 0 4px;
  background: linear-gradient(135deg, var(--accent) 0%, var(--accent-2) 100%);
  -webkit-background-clip: text;
  background-clip: text;
  color: transparent;
}
#quizResultBig.poor {
  background: linear-gradient(135deg, #94a3b8, #64748b);
  -webkit-background-clip: text; background-clip: text; color: transparent;
}
#quizMistakes { max-height: 40vh; overflow-y: auto; }
.quiz-mistake {
  background: var(--bg-card);
  border: 1px solid var(--line);
  border-left: 3px solid var(--bad);
  border-radius: 8px;
  padding: 8px 10px;
  margin-bottom: 6px;
  font-size: 13px;
}
.quiz-mistake .ja { color: var(--muted); font-size: 12px; margin: 0 0 2px; }
.quiz-mistake .en { color: var(--en); font-size: 14px; margin: 0; font-weight: 600; }
.quiz-mistake .your-answer { color: var(--bad); font-size: 12px; margin: 2px 0 0; }

.muted { color: var(--muted); font-size: 13px; }
.app-footer { text-align: center; padding: 20px 16px 40px; color: var(--muted); }

@media (prefers-color-scheme: light) {
  :root {
    --bg: #f8fafc;
    --bg-elev: #ffffff;
    --bg-card: #ffffff;
    --line: #e2e8f0;
    --text: #0f172a;
    --muted: #64748b;
    --en: #1d4ed8;
    --user-bubble: #dbeafe;
    --assistant-bubble: #ffffff;
  }
  .app-header { background: linear-gradient(180deg, var(--bg) 0%, rgba(248,250,252,0.92) 100%); }
}
