*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

:root {
  /* Fallback before JS runs; overridden by inline script (visualViewport). */
  --app-height: 100svh;
  --purple:       #7C5CBF;
  --purple-light: #EDE9F8;
  --purple-dark:  #5E42A6;
  --coral:        #FF6B6B;
  --coral-light:  #FFF0F0;
  --teal:         #00BCD4;
  --teal-light:   #E0F7FA;
  --green:        #2E7D32;
  --green-light:  #E8F5E9;
  --bg:           #FAF8F7;
  --white:        #FFFFFF;
  --gray-100:     #F5F5F5;
  --gray-200:     #EEEEEE;
  --gray-400:     #BDBDBD;
  --text-primary: #2D2D2D;
  --text-secondary: #757575;
  --shadow-sm:    0 2px 8px rgba(0,0,0,0.07);
  --shadow-md:    0 4px 20px rgba(0,0,0,0.11);
  --radius-sm:    8px;
  --radius-md:    16px;
  --radius-lg:    24px;
  --radius-pill:  999px;
}

html {
  width: 100%;
  max-width: 100%;
  overflow-x: hidden;
  overflow-y: hidden;
  /* Never taller than small viewport; JS --app-height often tighter */
  height: min(var(--app-height), 100svh);
  max-height: min(var(--app-height), 100svh);
}

body {
  font-family: 'Inter', -apple-system, BlinkMacSystemFont, sans-serif;
  background: var(--bg);
  color: var(--text-primary);
  display: flex;
  width: 100%;
  max-width: 100%;
  /* Fit first paint: cap with 100svh when --app-height is still too large */
  min-height: min(var(--app-height), 100svh);
  max-height: min(var(--app-height), 100svh);
  height: min(var(--app-height), 100svh);
  overflow-x: hidden;
  overflow-y: hidden;
  font-size: 14px;
  line-height: 1.5;
  /* Home indicator / notch safe areas on notched iPhones */
  padding-left: env(safe-area-inset-left, 0);
  padding-right: env(safe-area-inset-right, 0);
}

/* ---- Sidebar ---- */
#sidebar {
  width: 260px;
  min-width: 260px;
  background: var(--white);
  border-right: 1px solid var(--gray-200);
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

.sidebar-header {
  padding: 20px 20px 16px;
  border-bottom: 1px solid var(--gray-200);
}

.logo {
  display: block;
  font-size: 18px;
  font-weight: 700;
  color: var(--purple);
  letter-spacing: -0.3px;
}

.logo-subtitle {
  font-size: 12px;
  color: var(--text-secondary);
  margin-top: 2px;
}

.sidebar-filter {
  display: flex;
  gap: 6px;
  padding: 8px 12px;
  border-bottom: 1px solid var(--gray-200);
}

.filter-btn {
  flex: 1;
  padding: 5px 0;
  border: 1px solid var(--gray-200);
  border-radius: var(--radius-pill);
  background: var(--white);
  font-size: 12px;
  font-weight: 500;
  color: var(--text-secondary);
  cursor: pointer;
  transition: all 0.15s;
}

.filter-btn:hover { background: var(--gray-100); }

.filter-btn.active {
  background: var(--purple);
  border-color: var(--purple);
  color: var(--white);
}

#question-list {
  flex: 1;
  overflow-y: auto;
  padding: 8px;
}

.q-item {
  padding: 10px 12px;
  border-radius: var(--radius-sm);
  cursor: pointer;
  transition: background 0.15s;
  margin-bottom: 2px;
}

.q-item:hover { background: var(--gray-100); }
.q-item.active { background: var(--purple-light); }

.q-item-meta {
  display: flex;
  align-items: center;
  gap: 6px;
  margin-bottom: 4px;
}

.pack-badge {
  font-size: 10px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.5px;
  color: var(--purple);
  background: var(--purple-light);
  padding: 2px 6px;
  border-radius: var(--radius-pill);
}

.q-item.active .pack-badge {
  background: rgba(124,92,191,0.2);
}

.q-num {
  font-size: 12px;
  font-weight: 600;
  color: var(--text-secondary);
}

.q-preview {
  font-size: 12px;
  color: var(--text-secondary);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.q-item.active .q-preview { color: var(--purple-dark); }

.tpo-group-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 8px 12px;
  cursor: pointer;
  font-size: 12px;
  font-weight: 700;
  letter-spacing: 0.04em;
  color: var(--gray-600);
  user-select: none;
  border-radius: var(--radius-sm);
}
.tpo-group-header:hover { background: var(--gray-100); }
.tpo-group-header .chevron { transition: transform 0.15s; font-style: normal; }
.tpo-group.open > .tpo-group-header .chevron { transform: rotate(90deg); }
.tpo-group-items { display: none; }
.tpo-group.open > .tpo-group-items { display: block; }

/* ---- Main panel ---- */
#main {
  flex: 1;
  display: flex;
  flex-direction: column;
  overflow: hidden;
  min-width: 0;
  /* Allow column children (#chat-area) to shrink so #recording-bar stays in view */
  min-height: 0;
  position: relative;
}

/* ---- Question card ---- */
#question-card {
  background: var(--white);
  border-bottom: 1px solid var(--gray-200);
  box-shadow: var(--shadow-sm);
  padding: 16px 20px;
  flex-shrink: 0;
  max-height: 60vh;
  overflow-y: auto;
}

.q-card-meta {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-bottom: 12px;
}

#q-pack-badge {
  font-size: 11px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.5px;
  color: var(--teal);
  background: var(--teal-light);
  padding: 3px 10px;
  border-radius: var(--radius-pill);
}

#q-number {
  font-size: 16px;
  font-weight: 700;
  color: var(--purple);
}

/* Audio player */
.audio-row {
  display: flex;
  align-items: center;
  gap: 10px;
  margin-bottom: 12px;
  background: var(--gray-100);
  border-radius: var(--radius-md);
  padding: 8px 12px;
}

#btn-play-question {
  width: 36px;
  height: 36px;
  border-radius: 50%;
  background: var(--purple);
  border: none;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  transition: background 0.15s, transform 0.1s;
}

#btn-play-question:hover { background: var(--purple-dark); transform: scale(1.05); }

#btn-play-question svg { fill: white; width: 14px; height: 14px; }

.audio-progress-bar {
  flex: 1;
  height: 4px;
  background: var(--gray-400);
  border-radius: 2px;
  overflow: hidden;
  cursor: pointer;
}

#audio-progress {
  height: 100%;
  background: var(--purple);
  width: 0%;
  transition: width 0.1s linear;
}

#audio-time {
  font-size: 12px;
  color: var(--text-secondary);
  min-width: 36px;
  text-align: right;
}

/* Collapsibles */
.collapsible { margin-bottom: 6px; }

.collapsible-toggle {
  width: 100%;
  background: none;
  border: 1px solid var(--gray-200);
  border-radius: var(--radius-sm);
  padding: 8px 12px;
  text-align: left;
  font-size: 13px;
  font-weight: 500;
  color: var(--text-primary);
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: space-between;
  transition: background 0.15s;
}

.collapsible-toggle:hover { background: var(--gray-100); }
.collapsible-toggle.open { background: var(--purple-light); border-color: var(--purple); color: var(--purple); }

.chevron { font-size: 10px; transition: transform 0.2s; }
.collapsible-toggle.open .chevron { transform: rotate(180deg); }

.collapsible-content {
  padding: 10px 12px;
  font-size: 13px;
  color: var(--text-secondary);
  line-height: 1.6;
  background: var(--gray-100);
  border-radius: 0 0 var(--radius-sm) var(--radius-sm);
  border: 1px solid var(--gray-200);
  border-top: none;
}

/* Sample answer */
#btn-sample-answer {
  width: 100%;
  padding: 9px 16px;
  border: 2px solid var(--purple);
  border-radius: var(--radius-sm);
  background: none;
  color: var(--purple);
  font-size: 13px;
  font-weight: 600;
  cursor: pointer;
  margin-top: 8px;
  transition: background 0.15s, color 0.15s;
}

#btn-sample-answer:hover { background: var(--purple); color: white; }

#sample-answer-panel {
  margin-top: 10px;
  border: 1px solid var(--gray-200);
  border-radius: var(--radius-sm);
  overflow: hidden;
}

.sa-section {
  padding: 12px 14px;
}

.sa-section + .sa-section {
  border-top: 1px solid var(--gray-200);
}

.sa-label {
  font-size: 11px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.5px;
  color: var(--text-secondary);
  margin-bottom: 6px;
}

.sa-text {
  font-size: 13px;
  line-height: 1.7;
  color: var(--text-primary);
}

.vocab-section { padding: 10px 14px; background: var(--gray-100); }

.vocab-chip {
  display: inline-block;
  padding: 3px 10px;
  border-radius: var(--radius-pill);
  font-size: 12px;
  font-weight: 500;
  margin: 2px 3px 2px 0;
  cursor: default;
}

.vocab-word   { background: var(--purple-light); color: var(--purple); }
.vocab-phrase { background: var(--teal-light); color: #006064; }

/* ---- Chat area ---- */
#chat-area {
  flex: 1;
  min-height: 0;
  overflow-y: auto;
  padding: 16px 0 8px;
  scroll-behavior: smooth;
}

.empty-state {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  height: 100%;
  gap: 8px;
  color: var(--text-secondary);
  font-size: 14px;
}

.empty-icon {
  font-size: 48px;
  margin-bottom: 8px;
}

/* Bubble rows */
.bubble-row {
  display: flex;
  padding: 4px 20px;
}

.bubble-right { justify-content: flex-end; }
.bubble-left  { justify-content: flex-start; }

.bubble {
  max-width: 72%;
  padding: 12px 16px;
  border-radius: var(--radius-md);
  box-shadow: var(--shadow-sm);
  word-break: break-word;
}

.bubble-user {
  background: var(--purple);
  color: white;
  border-bottom-right-radius: 4px;
}

.bubble-ai {
  background: var(--white);
  border-bottom-left-radius: 4px;
  border: 1px solid var(--gray-200);
}

.bubble-spinner {
  background: var(--white);
  border: 1px solid var(--gray-200);
  border-bottom-left-radius: 4px;
  display: flex;
  align-items: center;
  gap: 10px;
  color: var(--text-secondary);
  font-size: 13px;
}

.bubble-error {
  background: var(--coral-light);
  color: var(--coral);
  border: 1px solid #FFCDD2;
  border-bottom-left-radius: 4px;
  font-size: 13px;
}

.bubble-text { font-size: 14px; line-height: 1.6; }

.bubble-time {
  display: block;
  font-size: 11px;
  opacity: 0.65;
  margin-top: 4px;
  text-align: right;
}

/* Pronunciation bubble */
.result-header {
  display: flex;
  align-items: center;
  gap: 10px;
  margin-bottom: 10px;
}
.result-header.toggleable {
  cursor: pointer;
  user-select: none;
}
.result-header.toggleable:hover { opacity: 0.8; }
.section-chevron { font-size: 13px; color: var(--text-secondary); }
.section-body.collapsed { display: none; }
.transcript-text {
  font-size: 14px;
  line-height: 1.6;
  color: var(--text-primary);
  margin: 0 0 4px;
  white-space: pre-wrap;
}

.result-title {
  font-size: 13px;
  font-weight: 600;
  color: var(--text-primary);
}

.score-badge {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 22px;
  height: 22px;
  border-radius: 50%;
  font-size: 11px;
  font-weight: 700;
  color: white;
  flex-shrink: 0;
}

.score-good { background: #43A047; }
.score-ok   { background: #FB8C00; }
.score-bad  { background: var(--coral); }

.pron-errors { display: flex; flex-direction: row; flex-wrap: wrap; gap: 8px; }

.pron-word {
  background: var(--gray-100);
  border-radius: var(--radius-sm);
  padding: 8px 10px;
  width: fit-content;
  flex: 0 0 auto;
}

.pron-word-header {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-bottom: 4px;
}

.word-label {
  font-weight: 600;
  font-size: 13px;
  color: var(--text-primary);
}

.word-error-badge {
  font-size: 10px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.4px;
  padding: 2px 6px;
  border-radius: var(--radius-pill);
  background: var(--coral-light);
  color: var(--coral);
}

.phoneme-row {
  display: flex;
  align-items: center;
  gap: 6px;
  font-size: 12px;
  color: var(--text-secondary);
  font-family: monospace;
  white-space: nowrap;
}

.phoneme-expected { color: #2E7D32; }
.phoneme-arrow { color: var(--gray-400); }
.phoneme-actual { color: var(--coral); }

.pron-play-row {
  display: flex;
  gap: 6px;
  margin-top: 6px;
}
.pron-play-btn {
  font-size: 11px;
  padding: 3px 8px;
  border-radius: var(--radius-pill);
  border: 1px solid var(--gray-300);
  background: var(--gray-100);
  cursor: pointer;
  color: var(--text-secondary);
  transition: background 0.12s;
}
.pron-play-btn:hover { background: var(--gray-200); }
.pron-play-btn:disabled { cursor: wait; opacity: 0.5; }
.pron-play-correct { border-color: #2E7D32; color: #2E7D32; }
.pron-play-user { border-color: var(--coral); color: var(--coral); }

.no-errors {
  font-size: 13px;
  color: #43A047;
  font-weight: 500;
}

/* Grammar bubble */
.grammar-mistake {
  background: var(--gray-100);
  border-radius: var(--radius-sm);
  padding: 8px 10px;
  margin-bottom: 8px;
}

.grammar-mistake:last-child { margin-bottom: 0; }

.mistake-inline {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  gap: 6px;
  margin-bottom: 4px;
}

.mistake-span {
  color: var(--coral);
  text-decoration: underline wavy var(--coral);
  font-weight: 500;
  font-size: 13px;
}

.mistake-arrow { color: var(--gray-400); font-size: 12px; }

.mistake-correction {
  color: var(--green);
  font-weight: 600;
  font-size: 13px;
}

.mistake-explanation {
  font-size: 12px;
  color: var(--text-secondary);
  line-height: 1.5;
}

details.corrected-answer {
  margin-top: 8px;
  border-top: 1px solid var(--gray-200);
  padding-top: 8px;
}

details.corrected-answer summary {
  font-size: 12px;
  color: var(--text-secondary);
  cursor: pointer;
  font-weight: 500;
}

details.corrected-answer p {
  font-size: 13px;
  color: var(--text-primary);
  line-height: 1.6;
  margin-top: 6px;
}

/* Speaking assessment bubble */
.speaking-dimensions {
  display: flex;
  flex-direction: column;
  gap: 8px;
  margin-top: 4px;
}

.speaking-dimension {
  background: var(--gray-100);
  border-radius: var(--radius-sm);
  padding: 8px 10px;
}

.dimension-label {
  font-size: 11px;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.5px;
  color: var(--text-secondary);
  margin-bottom: 3px;
}

.dimension-text {
  font-size: 12px;
  color: var(--text-primary);
  line-height: 1.55;
  margin: 0;
}

.speaking-summary {
  margin-top: 10px;
  padding-top: 8px;
  border-top: 1px solid var(--gray-200);
  font-size: 12px;
  font-style: italic;
  color: var(--text-secondary);
  line-height: 1.5;
}

/* Repeat diff */
.repeat-diff {
  font-size: 15px;
  line-height: 2;
  letter-spacing: 0.01em;
  margin: 8px 0 6px;
}

.diff-correct {
  color: #43A047;
}

.diff-missing {
  color: var(--coral);
  text-decoration: line-through;
  opacity: 0.75;
}

.diff-wrong {
  color: #FB8C00;
  position: relative;
  display: inline-flex;
  flex-direction: column;
  align-items: center;
  vertical-align: bottom;
  line-height: 1.2;
}

.diff-wrong-got {
  font-size: 10px;
  color: var(--coral);
  opacity: 0.85;
  line-height: 1;
  text-decoration: none;
}

.diff-extra {
  color: #9E9E9E;
  font-style: italic;
}

.repeat-legend {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  margin-top: 8px;
  padding-top: 8px;
  border-top: 1px solid var(--gray-200);
  font-size: 11px;
  color: var(--text-secondary);
}

.legend-item {
  display: inline-flex;
  align-items: center;
  gap: 3px;
}

/* Repeat badge in sidebar */
.repeat-badge {
  display: inline-block;
  font-size: 9px;
  font-weight: 600;
  letter-spacing: 0.04em;
  padding: 1px 5px;
  border-radius: 3px;
  background: #E3F2FD;
  color: #1565C0;
  vertical-align: middle;
  margin-left: 4px;
  text-transform: uppercase;
}

/* ---- Type sub-groups inside each TPO pack ---- */
.type-subgroup-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 4px 8px 4px 16px;
  font-size: 11px;
  font-weight: 600;
  letter-spacing: 0.05em;
  text-transform: uppercase;
  cursor: pointer;
  border-radius: var(--radius-sm);
  user-select: none;
  transition: background 0.15s;
}
.type-subgroup-header .chevron { font-size: 10px; transition: transform 0.15s; }
.type-subgroup.open > .type-subgroup-header .chevron { transform: rotate(90deg); }
.type-subgroup-items { display: none; }
.type-subgroup.open > .type-subgroup-items { display: block; }

/* Interview: purple */
.type-subgroup--interview > .type-subgroup-header { color: var(--purple); }
.type-subgroup--interview > .type-subgroup-header:hover { background: var(--purple-light); }

/* Repeat: teal */
.type-subgroup--repeat > .type-subgroup-header { color: var(--teal); }
.type-subgroup--repeat > .type-subgroup-header:hover { background: var(--teal-light); }

/* Question items indented under sub-group */
.type-subgroup-items .q-item { padding-left: 24px; }

/* Spinner animation */
.spinner {
  width: 18px;
  height: 18px;
  border: 2px solid var(--gray-200);
  border-top-color: var(--purple);
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
  flex-shrink: 0;
}

@keyframes spin { to { transform: rotate(360deg); } }

/* ---- Recording bar ---- */
#recording-bar {
  background: var(--white);
  border-top: 1px solid var(--gray-200);
  box-shadow: 0 -2px 12px rgba(0,0,0,0.06);
  padding: 12px 20px;
  padding-bottom: calc(12px + env(safe-area-inset-bottom, 0px));
  flex-shrink: 0;
}

.recording-container {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 16px;
}

#countdown-display {
  font-size: 22px;
  font-weight: 700;
  color: var(--coral);
  min-width: 60px;
  text-align: center;
  font-variant-numeric: tabular-nums;
}

#countdown-display span { font-size: 14px; font-weight: 500; }

#btn-mic {
  width: 60px;
  height: 60px;
  border-radius: 50%;
  background: var(--purple);
  border: none;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: background 0.2s, transform 0.1s, box-shadow 0.2s;
  box-shadow: 0 4px 16px rgba(124,92,191,0.35);
}

#btn-mic:hover { background: var(--purple-dark); transform: scale(1.05); }

#btn-mic svg { fill: white; width: 24px; height: 24px; }

#btn-mic.recording {
  background: var(--coral);
  box-shadow: 0 0 0 0 rgba(255,107,107,0.4);
  animation: mic-pulse 1.4s ease-in-out infinite;
}

@keyframes mic-pulse {
  0%   { box-shadow: 0 0 0 0    rgba(255,107,107,0.4); }
  70%  { box-shadow: 0 0 0 18px rgba(255,107,107,0);   }
  100% { box-shadow: 0 0 0 0    rgba(255,107,107,0);   }
}

.recording-hint {
  font-size: 12px;
  color: var(--text-secondary);
  text-align: center;
  margin-top: 6px;
}

.recording-status {
  font-size: 13px;
  color: var(--coral);
  font-weight: 500;
  display: flex;
  align-items: center;
  gap: 6px;
}

.rec-dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: var(--coral);
  animation: blink 1s ease-in-out infinite;
}

@keyframes blink { 0%,100% { opacity: 1; } 50% { opacity: 0.2; } }

#btn-discard {
  width: 44px;
  height: 44px;
  border-radius: 50%;
  background: var(--gray-100);
  border: 1.5px solid var(--gray-300);
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: background 0.15s, border-color 0.15s;
  flex-shrink: 0;
}

#btn-discard:hover { background: #fee2e2; border-color: #f87171; }
#btn-discard svg { fill: #ef4444; width: 20px; height: 20px; }

/* Scrollbar */
::-webkit-scrollbar { width: 5px; }
::-webkit-scrollbar-track { background: transparent; }
::-webkit-scrollbar-thumb { background: var(--gray-400); border-radius: 3px; }

/* ---- Sidebar header with auth button ---- */
.sidebar-header-top {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 8px;
}

.btn-auth {
  flex-shrink: 0;
  font-size: 11px;
  font-weight: 600;
  padding: 5px 10px;
  border-radius: var(--radius-pill);
  border: 1.5px solid var(--purple);
  background: none;
  color: var(--purple);
  cursor: pointer;
  transition: background 0.15s, color 0.15s;
  white-space: nowrap;
  margin-top: 2px;
}

.btn-auth:hover { background: var(--purple); color: white; }
.btn-auth.btn-auth-loggedin { background: var(--purple-light); border-color: var(--purple-light); color: var(--purple-dark); }
.btn-auth.btn-auth-loggedin:hover { background: var(--purple); color: white; }

/* ---- History section in sidebar ---- */
.history-section {
  border-top: 1px solid var(--gray-200);
  flex-shrink: 0;
  max-height: 40vh;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

#history-panel {
  overflow-y: auto;
  flex: 1;
}

.history-loading {
  padding: 12px 16px;
  font-size: 12px;
  color: var(--text-secondary);
}

.history-item {
  padding: 10px 12px;
  border-bottom: 1px solid var(--gray-200);
  cursor: default;
  transition: background 0.12s;
}

.history-item:hover { background: var(--gray-100); }

.history-item-top {
  display: flex;
  align-items: center;
  gap: 6px;
  margin-bottom: 4px;
}

.history-item-bottom {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 6px;
}

.history-meta {
  font-size: 10px;
  font-weight: 700;
  color: var(--purple-dark);
  background: var(--purple-light);
  padding: 1px 6px;
  border-radius: var(--radius-pill);
  white-space: nowrap;
  flex-shrink: 0;
  display: inline-block;
  margin-bottom: 2px;
}

.history-item-main {
  flex: 1;
  min-width: 0;
  display: flex;
  flex-direction: column;
  gap: 1px;
}

.history-item-meta-row {
  display: flex;
  align-items: center;
  gap: 4px;
}

.history-expand-btn {
  flex-shrink: 0;
  width: 20px;
  height: 20px;
  border-radius: 50%;
  background: var(--gray-100);
  border: none;
  cursor: pointer;
  font-size: 11px;
  color: var(--text-secondary);
  display: flex;
  align-items: center;
  justify-content: center;
  transition: background 0.15s, color 0.15s;
  padding: 0;
}

.history-expand-btn:hover { background: var(--purple-light); color: var(--purple); }
.history-item.expanded .history-expand-btn { background: var(--purple-light); color: var(--purple); }

.expand-icon { display: inline-block; transition: transform 0.2s; line-height: 1; }
.history-item.expanded .expand-icon { transform: rotate(180deg); }

.history-detail {
  display: none;
  padding: 8px 0 4px;
  border-top: 1px solid var(--gray-200);
  margin-top: 6px;
}

.history-detail-section {
  margin-top: 10px;
}

.history-detail-section:first-child {
  margin-top: 0;
}

.history-score {
  flex-shrink: 0;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 28px;
  height: 28px;
  border-radius: 50%;
  font-size: 11px;
  font-weight: 700;
  color: white;
}

.history-question {
  font-size: 12px;
  color: var(--text-primary);
  min-width: 0;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  display: block;
}

.history-audio-btn {
  flex-shrink: 0;
  width: 24px;
  height: 24px;
  border-radius: 50%;
  background: var(--purple-light);
  border: none;
  cursor: pointer;
  font-size: 10px;
  color: var(--purple);
  display: flex;
  align-items: center;
  justify-content: center;
  transition: background 0.15s;
}

.history-audio-btn:hover { background: var(--purple); color: white; }

.history-pron {
  font-size: 11px;
  color: var(--text-secondary);
  background: var(--gray-100);
  padding: 1px 6px;
  border-radius: var(--radius-pill);
}

.history-date {
  font-size: 11px;
  color: var(--text-secondary);
}

/* ---- Profile overlay ---- */
#profile-overlay {
  position: absolute;
  inset: 0;
  background: var(--bg);
  z-index: 100;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

.profile-header {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 14px 20px;
  background: var(--white);
  border-bottom: 1px solid var(--gray-200);
  flex-shrink: 0;
}

.profile-back-btn {
  background: none;
  border: none;
  font-size: 13px;
  font-weight: 600;
  color: var(--purple);
  cursor: pointer;
  padding: 4px 8px;
  border-radius: var(--radius-sm);
  transition: background 0.15s;
  flex-shrink: 0;
}

.profile-back-btn:hover { background: var(--purple-light); }

.profile-title {
  font-size: 15px;
  font-weight: 700;
  color: var(--text-primary);
}

.profile-user-card {
  display: flex;
  align-items: center;
  gap: 14px;
  padding: 16px 20px;
  background: linear-gradient(135deg, var(--purple-light) 0%, var(--white) 65%);
  border-bottom: 1px solid var(--gray-200);
  flex-shrink: 0;
}

.profile-avatar {
  width: 52px;
  height: 52px;
  border-radius: 50%;
  background: var(--purple-light);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 20px;
  font-weight: 700;
  color: var(--purple);
  flex-shrink: 0;
  box-shadow: 0 2px 8px rgba(124,92,191,0.25);
}

.profile-user-info {
  display: flex;
  flex-direction: column;
  gap: 3px;
  flex: 1;
  min-width: 0;
}

.profile-nickname {
  font-size: 15px;
  font-weight: 700;
  color: var(--text-primary);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.profile-points {
  display: inline-flex;
  align-items: center;
  background: var(--purple-light);
  color: var(--purple-dark);
  padding: 2px 8px;
  border-radius: var(--radius-pill);
  font-size: 12px;
  font-weight: 600;
}

.profile-logout-btn {
  flex-shrink: 0;
  font-size: 12px;
  font-weight: 600;
  padding: 6px 12px;
  border-radius: var(--radius-pill);
  border: 1.5px solid var(--coral);
  background: none;
  color: var(--coral);
  cursor: pointer;
  transition: background 0.15s, color 0.15s;
  white-space: nowrap;
}

.profile-logout-btn:hover { background: var(--coral); color: white; }

.profile-history-section {
  flex: 1;
  overflow-y: auto;
  padding: 0;
}

.profile-history-title {
  padding: 12px 20px 8px 17px;
  font-size: 11px;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.5px;
  color: var(--text-secondary);
  background: var(--bg);
  position: sticky;
  top: 0;
  border-left: 3px solid var(--purple);
}

#profile-history-list .history-item {
  padding: 12px 20px;
}

/* ---- Auth modal ---- */
.modal-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.45);
  z-index: 1000;
  display: flex;
  align-items: center;
  justify-content: center;
}

.modal {
  background: var(--white);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow-md);
  width: 340px;
  padding: 28px 28px 24px;
  position: relative;
}

.modal-close {
  position: absolute;
  top: 14px;
  right: 16px;
  background: none;
  border: none;
  font-size: 16px;
  color: var(--text-secondary);
  cursor: pointer;
  line-height: 1;
  padding: 2px 6px;
  border-radius: var(--radius-sm);
  transition: background 0.15s;
}

.modal-close:hover { background: var(--gray-100); }

.modal-tabs {
  display: flex;
  gap: 0;
  margin-bottom: 20px;
  border-bottom: 2px solid var(--gray-200);
}

.modal-tab {
  flex: 1;
  background: none;
  border: none;
  padding: 8px 0;
  font-size: 14px;
  font-weight: 500;
  color: var(--text-secondary);
  cursor: pointer;
  border-bottom: 2px solid transparent;
  margin-bottom: -2px;
  transition: color 0.15s, border-color 0.15s;
}

.modal-tab.tab-active {
  color: var(--purple);
  border-bottom-color: var(--purple);
  font-weight: 600;
}

.modal-form {
  flex-direction: column;
  gap: 10px;
}

.form-label {
  font-size: 12px;
  font-weight: 600;
  color: var(--text-secondary);
  text-transform: uppercase;
  letter-spacing: 0.4px;
  margin-bottom: -4px;
}

.form-input {
  padding: 9px 12px;
  border: 1.5px solid var(--gray-200);
  border-radius: var(--radius-sm);
  font-size: 14px;
  font-family: inherit;
  color: var(--text-primary);
  outline: none;
  transition: border-color 0.15s;
  background: var(--white);
}

.form-input:focus { border-color: var(--purple); }

.form-error {
  font-size: 12px;
  color: var(--coral);
  min-height: 16px;
}

.form-submit {
  margin-top: 4px;
  padding: 10px;
  background: var(--purple);
  color: white;
  border: none;
  border-radius: var(--radius-sm);
  font-size: 14px;
  font-weight: 600;
  cursor: pointer;
  transition: background 0.15s;
}

.form-submit:hover { background: var(--purple-dark); }

/* ---- Mobile sidebar toggle button (hidden on desktop) ---- */
#btn-sidebar-toggle {
  display: none;
  align-items: center;
  justify-content: center;
  background: none;
  border: none;
  color: var(--text-primary);
  cursor: pointer;
  padding: 6px;
  border-radius: var(--radius-sm);
  margin-right: 8px;
  flex-shrink: 0;
}
#btn-sidebar-toggle:hover { background: var(--gray-100); }

/* ---- Sidebar backdrop (mobile only) ---- */
#sidebar-backdrop {
  display: none;
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.35);
  z-index: 199;
}
#sidebar-backdrop.visible { display: block; }

/* ---- Mobile layout ---- */
@media (max-width: 700px) {
  /* Reserve space for fixed recording bar (mic + hint + home indicator inset). */
  #main {
    --recording-bar-reserved: calc(118px + env(safe-area-inset-bottom, 0px));
    width: 100%;
    min-width: 0;
    max-width: 100%;
    min-height: 0;
    overflow-x: hidden;
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
    overscroll-behavior-y: contain;
    padding-bottom: var(--recording-bar-reserved);
  }

  #sidebar {
    position: fixed;
    left: 0;
    top: 0;
    bottom: 0;
    z-index: 200;
    transform: translateX(-100%);
    transition: transform 0.25s ease;
    width: 80vw;
    max-width: 300px;
    min-width: 0;
    box-shadow: var(--shadow-md);
    /* Match drawer to same capped height as shell */
    height: min(var(--app-height), 100svh);
    max-height: min(var(--app-height), 100svh);
    padding-bottom: env(safe-area-inset-bottom, 0);
  }
  #sidebar.open {
    transform: translateX(0);
  }
  #recording-bar {
    position: fixed;
    left: 0;
    right: 0;
    bottom: 0;
    z-index: 40;
    flex-shrink: 0;
  }
  #btn-sidebar-toggle { display: flex; }
  #question-card {
    /* More vertical room on short phone screens */
    max-height: min(55vh, 420px);
  }
  .modal-overlay {
    align-items: flex-start;
    padding: max(16px, env(safe-area-inset-top)) 16px 16px;
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
  }
  .modal {
    width: min(340px, calc(100vw - 32px));
    max-height: min(90dvh, calc(100dvh - env(safe-area-inset-top) - env(safe-area-inset-bottom) - 32px));
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
  }
}
