:root {
  --bg: #fafaf7;
  --surface: #ffffff;
  --teacher: #f3eef9;
  --note: #fff8d8;
  --note-border: #f0d062;
  --learner: #e3edff;
  --text: #2a2a2a;
  --muted: #777;
  --accent: #6b4ec0;
  --radius: 16px;
}
* { box-sizing: border-box; }
html, body { margin: 0; padding: 0; background: var(--bg); color: var(--text); font: 16px/1.5 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif; }
body { display: flex; flex-direction: column; height: 100vh; max-width: 720px; margin: 0 auto; }
header { padding: 14px 16px 8px; border-bottom: 1px solid #ececec; background: var(--surface); }
h1 { font-size: 17px; font-weight: 500; margin: 0 0 8px; color: var(--muted); }
.row { display: flex; gap: 10px; flex-wrap: wrap; align-items: center; }
.row label { font-size: 14px; color: var(--muted); }
select, button { font: inherit; padding: 6px 12px; border-radius: 12px; border: 1px solid #ccc; background: white; cursor: pointer; }
button { background: var(--accent); color: white; border: none; }
button.ghost { background: transparent; color: var(--accent); border: 1px solid var(--accent); }
button:disabled { opacity: 0.5; cursor: progress; }

/* ── Avatar ─────────────────────────────────────────── */
#avatar-stage { display: flex; flex-direction: column; align-items: center; padding: 10px 0 6px; background: var(--surface); border-bottom: 1px solid #ececec; }
#avatar-frame {
  position: relative;
  width: 156px; height: 156px;
  border-radius: 50%;
  overflow: hidden;
  background: linear-gradient(135deg, #d4c2f0, #c8e0f0);
  box-shadow: 0 4px 18px rgba(107,78,192,0.18), inset 0 0 0 3px rgba(255,255,255,0.65);
  transition: transform 0.35s ease;
}
#avatar-img { width: 100%; height: 100%; object-fit: cover; display: block; transition: opacity 0.35s, filter 0.35s; }
/* If image missing, show an elegant emoji placeholder via ::before */
#avatar-frame.no-img::before {
  content: "👩‍🏫";
  position: absolute; inset: 0;
  display: flex; align-items: center; justify-content: center;
  font-size: 64px;
  filter: drop-shadow(0 2px 8px rgba(0,0,0,0.15));
}
.halo-ring {
  position: absolute; inset: -6px;
  border-radius: 50%;
  background: conic-gradient(from 0deg, transparent 0%, var(--accent) 25%, transparent 50%);
  filter: blur(6px); opacity: 0;
  pointer-events: none;
  animation: spin 5s linear infinite;
  transition: opacity 0.3s;
}
@keyframes spin { to { transform: rotate(360deg); } }

/* Idle: gentle breathing */
#avatar-stage[data-state="idle"] #avatar-frame { animation: breathe 4.5s ease-in-out infinite; }
@keyframes breathe { 0%,100% { transform: scale(1); } 50% { transform: scale(1.012); } }

/* State overlays */
.state-overlay { position: absolute; inset: 0; display: flex; align-items: center; justify-content: center; pointer-events: none; opacity: 0; transition: opacity 0.25s; }

/* Listening: red pulse rings around the frame */
.listen-overlay .ring-pulse {
  position: absolute; inset: 0;
  border: 3px solid #e84855;
  border-radius: 50%;
  animation: ringPulse 1.4s ease-out infinite;
}
.listen-overlay .ring-pulse.delay { animation-delay: 0.7s; }
@keyframes ringPulse {
  0% { transform: scale(0.95); opacity: 0.9; }
  100% { transform: scale(1.4); opacity: 0; }
}
#avatar-stage[data-state="listening"] .listen-overlay { opacity: 1; }
#avatar-stage[data-state="listening"] #avatar-frame { transform: scale(1.04); }

/* Thinking: blurry warmth + dots over avatar */
.think-overlay { gap: 6px; bottom: 16%; top: auto; height: 24px; }
.think-overlay .t-dot { width: 8px; height: 8px; border-radius: 50%; background: rgba(255,255,255,0.95); box-shadow: 0 1px 3px rgba(0,0,0,0.35); animation: dotBounce 1s ease-in-out infinite; }
.think-overlay .t-dot:nth-child(2) { animation-delay: 0.13s; }
.think-overlay .t-dot:nth-child(3) { animation-delay: 0.26s; }
@keyframes dotBounce { 0%,100% { transform: translateY(0); } 50% { transform: translateY(-6px); } }
#avatar-stage[data-state="thinking"] .think-overlay { opacity: 1; }
#avatar-stage[data-state="thinking"] #avatar-img { filter: blur(0.6px) saturate(0.9); }

/* Speaking: animated halo glow + slight zoom synced to TTS */
#avatar-stage[data-state="speaking"] .halo-ring { opacity: 0.85; }
#avatar-stage[data-state="speaking"] #avatar-frame {
  animation: speakPulse 0.42s ease-in-out infinite alternate;
  box-shadow: 0 0 0 4px rgba(107,78,192,0.35), 0 6px 22px rgba(107,78,192,0.4);
}
@keyframes speakPulse {
  0% { transform: scale(1.0); }
  100% { transform: scale(1.025); }
}

/* Happy: warm glow */
#avatar-stage[data-state="happy"] #avatar-frame {
  animation: happyGlow 1.6s ease-in-out;
  box-shadow: 0 0 0 4px rgba(255,200,80,0.55), 0 6px 22px rgba(255,180,90,0.45);
}
@keyframes happyGlow {
  0%,100% { transform: scale(1); }
  50% { transform: scale(1.04); }
}

/* When portrait is being upgraded later, swap idle.png and the rest just works */
#avatar-stage[data-state="speaking"] #avatar-img[data-has-speaking="1"] { content: url("/static/avatar/speaking.png"); }
#avatar-stage[data-state="thinking"] #avatar-img[data-has-thinking="1"] { content: url("/static/avatar/thinking.png"); }

#avatar-status { font-size: 13px; color: var(--muted); margin: 6px 0 4px; min-height: 18px; transition: opacity 0.2s; }

main { flex: 1; overflow-y: auto; padding: 14px 16px; display: flex; flex-direction: column; gap: 10px; }
.bubble { max-width: 80%; padding: 10px 14px; border-radius: var(--radius); white-space: pre-wrap; word-wrap: break-word; }
.bubble.learner { align-self: flex-end; background: var(--learner); border-bottom-right-radius: 4px; }
.bubble.teacher { align-self: flex-start; background: var(--teacher); border-bottom-left-radius: 4px; }
.bubble.note { align-self: flex-start; background: var(--note); border-left: 3px solid var(--note-border); font-size: 14px; max-width: 90%; }
.bubble.note .cat { display: inline-block; font-size: 11px; color: var(--muted); margin-left: 6px; padding: 1px 7px; border: 1px solid var(--muted); border-radius: 8px; vertical-align: middle; }

/* Pronunciation drill panel */
.drill { margin-top: 8px; padding-top: 8px; border-top: 1px dashed rgba(0,0,0,0.15); display: flex; flex-direction: column; gap: 6px; font-size: 13px; }
.drill-btn, .drill-stop { align-self: flex-start; padding: 6px 12px; border: none; border-radius: 8px; background: var(--accent); color: white; font: inherit; cursor: pointer; font-size: 13px; }
.drill-stop { background: #d8395a; animation: pulse 1.2s ease-in-out infinite; }
.drill-prompt { font-size: 14px; }
.drill-prompt b { color: var(--accent); }
.drill-result { font-weight: 600; }
.drill-target { font-size: 16px; padding: 4px 0; }
.drill-target .t-ok { color: #2e8a4d; font-weight: 600; }
.drill-target .t-missing { color: #b0681e; text-decoration: underline dotted; }
.drill-target .t-wrong { color: #c0392b; text-decoration: line-through; }
.drill-heard { font-size: 12px; color: var(--muted); }
.drill-err { font-size: 13px; color: #c0392b; }

form { display: flex; gap: 8px; padding: 10px 12px; background: var(--surface); border-top: 1px solid #ececec; align-items: stretch; }
textarea { flex: 1; padding: 10px 14px; border: 1px solid #ccc; border-radius: var(--radius); font: inherit; resize: none; }
form button { padding: 0 18px; }
#mic { padding: 0 14px; font-size: 22px; line-height: 1; user-select: none; touch-action: manipulation; -webkit-tap-highlight-color: transparent; transition: background 0.15s, transform 0.15s; }
#mic.recording { background: #e84855; transform: scale(1.08); animation: pulse 1.1s ease-in-out infinite; }
@keyframes pulse { 50% { box-shadow: 0 0 0 10px rgba(232,72,85,0.18); } }
#mic-hint { font-size: 12px; color: var(--muted); padding: 0 16px 8px; background: var(--surface); text-align: center; }

dialog { border: none; border-radius: var(--radius); padding: 20px; max-width: 90%; }
dialog h2 { margin-top: 0; font-size: 18px; }
dialog h3 { font-size: 14px; color: var(--muted); margin: 14px 0 6px; }
dialog ul { padding-left: 18px; }
dialog li { margin: 6px 0; font-size: 14px; }

/* Streak chip */
.chip { display: inline-flex; align-items: center; gap: 4px; padding: 5px 12px; border-radius: 20px; font-size: 14px; background: linear-gradient(135deg,#ff8c42,#ff5e62); color: white; font-weight: 600; box-shadow: 0 1px 2px rgba(0,0,0,0.1); }
.chip.dim { background: #ddd; color: #666; }

/* Progress modal layout */
.prog-row { display: flex; gap: 14px; justify-content: space-around; margin: 8px 0 4px; }
.kpi { display: flex; flex-direction: column; align-items: center; padding: 10px 14px; background: var(--bg); border-radius: 12px; min-width: 80px; }
.kpi span { font-size: 26px; font-weight: 700; color: var(--accent); }
.kpi small { font-size: 11px; color: var(--muted); margin-top: 2px; }

#weekly-strip { display: flex; gap: 4px; padding: 6px 0; }
.day-cell { flex: 1; height: 28px; border-radius: 6px; background: #ececec; position: relative; display: flex; align-items: end; justify-content: center; font-size: 9px; color: rgba(0,0,0,0.5); }
.day-cell.lvl-1 { background: #c5edd1; }
.day-cell.lvl-2 { background: #80d49a; }
.day-cell.lvl-3 { background: #4ab26b; color: white; }
.day-cell.lvl-4 { background: #2e8a4d; color: white; }
.day-cell.today { outline: 2px solid var(--accent); outline-offset: 1px; }

#cat-bars { list-style: none; padding-left: 0; }
#cat-bars li { display: flex; align-items: center; gap: 8px; margin: 4px 0; font-size: 13px; }
#cat-bars .bar { flex: 1; height: 10px; background: #ececec; border-radius: 5px; overflow: hidden; }
#cat-bars .bar > span { display: block; height: 100%; background: var(--accent); border-radius: 5px; }
#cat-bars .label { width: 110px; }
#cat-bars .num { width: 22px; text-align: right; color: var(--muted); }

/* Badges */
#badges-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(96px, 1fr)); gap: 8px; }
.badge { padding: 10px 8px; text-align: center; border-radius: 12px; background: var(--bg); border: 1px solid #ececec; transition: transform 0.15s; }
.badge.held { background: linear-gradient(135deg, #fff4d8, #ffe8b8); border-color: #f0c060; }
.badge.locked { opacity: 0.45; filter: grayscale(0.6); }
.badge .icon { font-size: 26px; line-height: 1; display: block; margin-bottom: 4px; }
.badge .ttl { font-size: 11px; font-weight: 600; color: var(--text); }
.badge .desc { font-size: 10px; color: var(--muted); margin-top: 3px; line-height: 1.25; }

/* Vocab flashcard */
#flashcard { background: var(--bg); border-radius: 14px; padding: 22px 16px; margin: 12px 0; text-align: center; min-height: 110px; display: flex; flex-direction: column; justify-content: space-between; gap: 12px; }
.card-front { font-size: 28px; font-weight: 600; color: var(--text); }
.card-back { font-size: 18px; color: var(--accent); font-style: italic; }
.card-buttons { display: flex; gap: 8px; justify-content: center; flex-wrap: wrap; }
.card-buttons button { padding: 8px 16px; }
#card-hard { background: #d8395a; }
#card-good { background: #2e8a4d; }
#vocab-list { list-style: none; padding: 0; max-height: 280px; overflow-y: auto; }
#vocab-list li { display: flex; justify-content: space-between; align-items: center; padding: 6px 8px; border-bottom: 1px solid #ececec; font-size: 13px; }
#vocab-list li .word { font-weight: 600; }
#vocab-list li .ru { color: var(--muted); }
#vocab-list li .box { font-size: 11px; padding: 2px 7px; background: var(--accent); color: white; border-radius: 8px; }
#vocab-list li .box.b0 { background: #aaa; }
#vocab-list li .box.b5 { background: #2e8a4d; }
#vocab-empty { text-align: center; padding: 16px; color: var(--muted); }

/* Toast for new badge */
.toast { position: fixed; bottom: 80px; left: 50%; transform: translateX(-50%);
  background: var(--accent); color: white; padding: 12px 18px; border-radius: 14px;
  font-size: 14px; box-shadow: 0 4px 16px rgba(0,0,0,0.2); z-index: 99;
  animation: toastIn 0.3s ease-out; max-width: 80%; text-align: center; }
.toast.hide { animation: toastOut 0.3s ease-in forwards; }
@keyframes toastIn { from { transform: translate(-50%, 30px); opacity: 0; } to { transform: translate(-50%, 0); opacity: 1; } }
@keyframes toastOut { to { transform: translate(-50%, 30px); opacity: 0; } }

@media (prefers-color-scheme: dark) {
  .day-cell { background: #2a2c33; color: rgba(255,255,255,0.4); }
  .kpi { background: #1f2025; }
  #cat-bars .bar { background: #2c2d31; }
  .badge { background: #1f2025; border-color: #333; }
  .badge.held { background: linear-gradient(135deg, #4a3a18, #6a4a1a); border-color: #aa8530; }
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #1a1a1c;
    --surface: #25262a;
    --teacher: #312f3a;
    --note: #3a3325;
    --note-border: #c4a346;
    --learner: #2a3550;
    --text: #ececec;
    --muted: #aaa;
    --accent: #9b85e5;
  }
  select, textarea { background: #2c2d31; color: var(--text); border-color: #444; }
  button.ghost { background: transparent; }
  dialog { background: var(--surface); color: var(--text); }
}
