/* ===================================================================
   Hawraa — live English tutor
   Designed thumb-first for iPhone Safari. Everything scales from the
   --state-* custom properties so the whole screen responds as one.
   =================================================================== */

:root {
  --ink:        #f4eefc;
  --ink-dim:    #b7a9cd;
  --ink-faint:  #7e719a;

  --bg-0: #0d0819;
  --bg-1: #1a1030;
  --bg-2: #2a1745;

  --accent:  #f5b942;   /* amber — corrections, primary action        */
  --teal:    #2f8c81;   /* Hawraa's veil — listening                  */
  --violet:  #8b5cf6;   /* thinking                                   */
  --rose:    #e0806f;

  --surface:  #17102b;
  --surface-2:#211736;
  --line:     rgba(244,238,252,.10);

  --safe-t: env(safe-area-inset-top, 0px);
  --safe-b: env(safe-area-inset-bottom, 0px);

  --r-sm: 10px;
  --r-md: 16px;
  --r-lg: 26px;

  --ease: cubic-bezier(.22,.61,.36,1);
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  height: 100%;
  overflow: hidden;
  overscroll-behavior: none;
}

body {
  font: 400 16px/1.5 -apple-system, BlinkMacSystemFont, "SF Pro Text",
        "Segoe UI", system-ui, sans-serif;
  color: var(--ink);
  background: var(--bg-0);
  -webkit-font-smoothing: antialiased;
  -webkit-user-select: none;
  user-select: none;
  -webkit-tap-highlight-color: transparent;
  touch-action: manipulation;
}

/* ------------------------------------------------------------------ *
   Stage — the whole app is one screen, 100dvh so Safari's collapsing
   toolbar never clips the microphone button.
 * ------------------------------------------------------------------ */
.stage {
  position: relative;
  height: 100dvh;
  display: grid;
  grid-template-rows: auto 1fr auto auto auto;
  padding: calc(var(--safe-t) + 10px) 18px calc(var(--safe-b) + 14px);
  isolation: isolate;
}

/* the mood behind everything, tinted by state */
.stage::before {
  content: "";
  position: absolute;
  inset: 0;
  z-index: -2;
  background:
    radial-gradient(120% 78% at 50% 8%,  var(--glow, #3a2260) 0%, transparent 62%),
    linear-gradient(180deg, var(--bg-1) 0%, var(--bg-0) 55%, #08050f 100%);
  transition: background 900ms var(--ease);
}

body[data-state="listening"] { --glow: #1d5c58; }
body[data-state="thinking"]  { --glow: #402a72; }
body[data-state="speaking"]  { --glow: #4a2f66; }
body[data-state="idle"]      { --glow: #2c1a4d; }

/* ------------------------------- top bar ------------------------------- */
.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
}

.brand {
  font: 600 15px/1 "SF Pro Display", system-ui, sans-serif;
  letter-spacing: .22em;
  text-transform: uppercase;
  color: var(--ink-faint);
}

.chip {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  height: 38px;
  padding: 0 14px;
  border: 1px solid var(--line);
  border-radius: 999px;
  background: rgba(255,255,255,.05);
  color: var(--ink-dim);
  font: 500 13.5px/1 inherit;
  cursor: pointer;
  -webkit-backdrop-filter: blur(14px);
  backdrop-filter: blur(14px);
  transition: color .25s, border-color .25s, background .25s;
}
.chip:active { transform: scale(.96); }

.chip--icon { padding: 0; width: 38px; justify-content: center; }

.chip__dot {
  width: 7px; height: 7px;
  border-radius: 50%;
  background: var(--accent);
  box-shadow: 0 0 10px var(--accent);
  transition: background .25s, box-shadow .25s;
}
body[data-mode="free"] .chip__dot { background: var(--teal); box-shadow: 0 0 10px var(--teal); }
body[data-mode="correct"] .chip--mode { color: var(--accent); border-color: rgba(245,185,66,.35); }
body[data-mode="free"]    .chip--mode { color: var(--teal);   border-color: rgba(47,140,129,.4); }

/* ------------------------------- avatar ------------------------------- */
.avatar-wrap {
  position: relative;
  min-height: 0;
  display: grid;
  place-items: end center;
}

.hawraa {
  height: 100%;
  max-height: 100%;
  width: auto;
  max-width: 100%;
  display: block;
  filter: drop-shadow(0 26px 44px rgba(0,0,0,.55));
}

/* the aura that tells you, at a glance, that she is hearing you */
.halo {
  position: absolute;
  top: 4%;
  left: 50%;
  width: min(74vw, 340px);
  aspect-ratio: 1;
  transform: translateX(-50%);
  border-radius: 50%;
  background: radial-gradient(circle, var(--halo, transparent) 0%, transparent 68%);
  opacity: 0;
  transition: opacity .6s var(--ease);
  pointer-events: none;
}
body[data-state="listening"] .halo { --halo: rgba(47,140,129,.55); opacity: 1; animation: breathe 2.4s ease-in-out infinite; }
body[data-state="speaking"]  .halo { --halo: rgba(245,185,66,.28); opacity: 1; }
body[data-state="thinking"]  .halo { --halo: rgba(139,92,246,.34); opacity: 1; animation: breathe 1.5s ease-in-out infinite; }

@keyframes breathe {
  0%, 100% { transform: translateX(-50%) scale(1);    }
  50%      { transform: translateX(-50%) scale(1.13); }
}

/* ------------------------- the portrait itself ------------------------- */
/* Every animated part needs fill-box, otherwise Safari scales around the
   whole viewBox origin and her eyes shoot off the canvas.               */
#body, #eyes, #eyes .eye, #mouth {
  transform-box: fill-box;
  transform-origin: center;
}

/* she breathes, always — this is what stops her looking like a sticker */
#body {
  transform-origin: 50% 92%;
  animation: hw-breathe 5.6s ease-in-out infinite;
}
@keyframes hw-breathe {
  0%, 100% { transform: scale(1)      translateY(0)      rotate(0deg);    }
  30%      { transform: scale(1.014)  translateY(-2px)   rotate(0.35deg); }
  62%      { transform: scale(1.006)  translateY(-1px)   rotate(-0.3deg); }
}
.hawraa[data-state="listening"] #body { animation-duration: 4.2s; }
.hawraa[data-state="thinking"]  #body { animation-duration: 6.6s; }

/* blink */
#eyes .eye { transition: transform 90ms ease-in; }
#eyes.is-blink .eye { transform: scaleY(0.07); }

/* brows lift a little when she is listening to you */
#brows { transform-box: fill-box; transform-origin: center; transition: transform .55s var(--ease); }
.hawraa[data-state="listening"] #brows { transform: translateY(-3.5px); }
.hawraa[data-state="thinking"]  #brows { transform: translateY(-1.5px) rotate(-1.2deg); }

/* lips are moved by JS every frame; keep the transition short so the
   procedural viseme motion stays crisp rather than smearing */
#lipTop, #lipBottom, #mouthHole, #mouthTongue {
  transform-box: fill-box;
  transform-origin: center;
}

/* thinking dots float just above her head */
.thinking {
  position: absolute;
  top: 6%;
  left: 50%;
  transform: translateX(-50%);
  display: flex;
  gap: 7px;
  padding: 9px 14px;
  border-radius: 999px;
  background: rgba(20,12,38,.72);
  -webkit-backdrop-filter: blur(10px);
  backdrop-filter: blur(10px);
  opacity: 0;
  transition: opacity .3s;
  pointer-events: none;
}
.thinking i {
  width: 7px; height: 7px;
  border-radius: 50%;
  background: var(--violet);
  animation: bob 1.05s ease-in-out infinite;
}
.thinking i:nth-child(2) { animation-delay: .16s; }
.thinking i:nth-child(3) { animation-delay: .32s; }
body[data-state="thinking"] .thinking { opacity: 1; }

@keyframes bob {
  0%, 60%, 100% { transform: translateY(0);    opacity: .45; }
  30%           { transform: translateY(-6px); opacity: 1;   }
}

/* ------------------------------ caption ------------------------------ */
.caption {
  min-height: 74px;
  display: grid;
  place-items: center;
  padding: 12px 4px 6px;
  text-align: center;
}

.caption__text {
  margin: 0;
  max-width: 34ch;
  font-size: 17.5px;
  line-height: 1.42;
  color: var(--ink);
  text-wrap: balance;
  transition: color .3s, opacity .3s;
}
.caption__text.is-interim { color: var(--teal); font-style: italic; }
.caption__text.is-idle    { color: var(--ink-faint); font-size: 15.5px; }

body:not([data-captions="on"]) .caption__text:not(.is-interim):not(.is-idle) { opacity: 0; }

/* ------------------------------ controls ------------------------------ */
.controls {
  display: grid;
  grid-template-columns: 56px 1fr 56px;
  align-items: center;
  justify-items: center;
  padding-top: 6px;
}

.ghost-btn {
  position: relative;
  width: 50px; height: 50px;
  display: grid;
  place-items: center;
  border: 1px solid var(--line);
  border-radius: 50%;
  background: rgba(255,255,255,.045);
  color: var(--ink-dim);
  cursor: pointer;
  transition: transform .18s var(--ease), color .2s, background .2s;
}
.ghost-btn:active { transform: scale(.9); }

.ghost-btn__badge {
  position: absolute;
  top: -3px; right: -3px;
  min-width: 19px; height: 19px;
  padding: 0 5px;
  display: grid; place-items: center;
  border-radius: 999px;
  background: var(--accent);
  color: #241703;
  font: 700 11px/1 inherit;
}

/* the one button that matters */
.mic {
  position: relative;
  width: 86px; height: 86px;
  display: grid;
  place-items: center;
  border: 0;
  border-radius: 50%;
  background: linear-gradient(160deg, #fbd070, var(--accent) 46%, #d8912a);
  color: #2a1a02;
  cursor: pointer;
  box-shadow: 0 12px 30px rgba(245,185,66,.30), inset 0 1px 0 rgba(255,255,255,.5);
  transition: transform .2s var(--ease), background .45s var(--ease),
              box-shadow .45s var(--ease);
}
.mic:active { transform: scale(.93); }

.mic__stop { display: none; }

body[data-state="listening"] .mic {
  background: linear-gradient(160deg, #5fd6c4, var(--teal) 48%, #17544e);
  color: #04211e;
  box-shadow: 0 12px 34px rgba(47,140,129,.42), inset 0 1px 0 rgba(255,255,255,.4);
}
body[data-state="speaking"] .mic,
body[data-state="thinking"] .mic {
  background: linear-gradient(160deg, #b79bf7, var(--violet) 50%, #5b34c4);
  color: #150a2e;
  box-shadow: 0 12px 34px rgba(139,92,246,.4), inset 0 1px 0 rgba(255,255,255,.4);
}
body[data-state="speaking"] .mic__icon,
body[data-state="thinking"] .mic__icon { display: none; }
body[data-state="speaking"] .mic__stop,
body[data-state="thinking"] .mic__stop { display: block; }

/* sonar rings, only while listening */
.mic__ring {
  position: absolute;
  inset: 0;
  border-radius: 50%;
  border: 2px solid var(--teal);
  opacity: 0;
  pointer-events: none;
}
body[data-state="listening"] .mic__ring    { animation: sonar 2.1s ease-out infinite; }
body[data-state="listening"] .mic__ring--2 { animation-delay: 1.05s; }

@keyframes sonar {
  0%   { transform: scale(1);    opacity: .65; }
  100% { transform: scale(1.85); opacity: 0;   }
}

.hint {
  margin: 10px 0 0;
  text-align: center;
  font-size: 12.5px;
  letter-spacing: .05em;
  color: var(--ink-faint);
  min-height: 18px;
}

/* ------------------------------- sheets ------------------------------- */
.scrim {
  position: fixed;
  inset: 0;
  z-index: 20;
  background: rgba(6,3,14,.6);
  -webkit-backdrop-filter: blur(3px);
  backdrop-filter: blur(3px);
  animation: fade .28s var(--ease);
}
@keyframes fade { from { opacity: 0; } }

.sheet {
  position: fixed;
  z-index: 30;
  left: 0; right: 0; bottom: 0;
  max-height: 82dvh;
  display: flex;
  flex-direction: column;
  padding-bottom: var(--safe-b);
  background: var(--surface);
  border-top: 1px solid var(--line);
  border-radius: var(--r-lg) var(--r-lg) 0 0;
  box-shadow: 0 -20px 50px rgba(0,0,0,.55);
  transform: translateY(102%);
  transition: transform .38s var(--ease);
  visibility: hidden;
}
.sheet[aria-hidden="false"] { transform: translateY(0); visibility: visible; }
.sheet--full { max-height: 92dvh; }

.sheet__grip {
  width: 40px; height: 4px;
  margin: 10px auto 2px;
  border-radius: 99px;
  background: var(--ink-faint);
  opacity: .5;
  flex: none;
}

.sheet__head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 8px 20px 12px;
  flex: none;
}
.sheet__head h2 { margin: 0; font-size: 17px; font-weight: 600; }

.sheet__close {
  width: 34px; height: 34px;
  border: 0; border-radius: 50%;
  background: rgba(255,255,255,.07);
  color: var(--ink-dim);
  font-size: 21px; line-height: 1;
  cursor: pointer;
}

.sheet__body {
  padding: 0 20px 26px;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  overscroll-behavior: contain;
  -webkit-user-select: text;
  user-select: text;
}

.empty { color: var(--ink-faint); text-align: center; padding: 30px 0; font-size: 14.5px; }

/* --------------------------- transcript rows --------------------------- */
.turn { margin: 0 0 14px; display: flex; }
.turn--me  { justify-content: flex-end; }
.turn--her { justify-content: flex-start; }

.bubble {
  max-width: 82%;
  padding: 11px 15px;
  border-radius: 18px;
  font-size: 15.5px;
  line-height: 1.48;
}
.turn--me  .bubble { background: var(--surface-2); border-bottom-right-radius: 6px; }
.turn--her .bubble { background: rgba(47,140,129,.16); border: 1px solid rgba(47,140,129,.26); border-bottom-left-radius: 6px; }

/* a mistake he made — tap to see the fix */
.fixmark {
  border-bottom: 2px dashed var(--accent);
  color: var(--accent);
  cursor: pointer;
  padding-bottom: 1px;
}

.fixcard {
  margin: 8px 0 0;
  padding: 10px 13px;
  border-left: 3px solid var(--accent);
  border-radius: 0 var(--r-sm) var(--r-sm) 0;
  background: rgba(245,185,66,.09);
  font-size: 14px;
  line-height: 1.5;
}
.fixcard .was  { color: var(--ink-faint); text-decoration: line-through; }
.fixcard .now  { color: var(--accent); font-weight: 600; }
.fixcard .why  { display: block; margin-top: 4px; color: var(--ink-dim); font-size: 13px; }

/* ---------------------------- report cards ---------------------------- */
.stats {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 10px;
  margin-bottom: 20px;
}
.stat {
  padding: 14px 10px;
  border: 1px solid var(--line);
  border-radius: var(--r-md);
  background: var(--surface-2);
  text-align: center;
}
.stat b { display: block; font-size: 24px; font-weight: 650; letter-spacing: -.02em; }
.stat span { font-size: 11.5px; color: var(--ink-faint); text-transform: uppercase; letter-spacing: .07em; }

.report-h {
  margin: 22px 0 10px;
  font-size: 12px;
  letter-spacing: .11em;
  text-transform: uppercase;
  color: var(--ink-faint);
}

.vocab { display: flex; flex-wrap: wrap; gap: 8px; }
.vocab span {
  padding: 6px 11px;
  border: 1px solid rgba(47,140,129,.32);
  border-radius: 999px;
  background: rgba(47,140,129,.13);
  font-size: 13.5px;
}

/* ------------------------------- fields ------------------------------- */
.field { display: block; margin-bottom: 20px; }
.field__label {
  display: flex;
  justify-content: space-between;
  margin-bottom: 8px;
  font-size: 13.5px;
  color: var(--ink-dim);
}
.field__label b { color: var(--accent); font-weight: 600; }

.field__control {
  width: 100%;
  height: 44px;
  padding: 0 12px;
  border: 1px solid var(--line);
  border-radius: var(--r-sm);
  background: var(--surface-2);
  color: var(--ink);
  font: inherit;
  font-size: 15px;
}
input[type="range"].field__control {
  height: 30px;
  padding: 0;
  border: 0;
  background: transparent;
  accent-color: var(--accent);
}

.switch {
  display: flex;
  align-items: flex-start;
  gap: 13px;
  margin-bottom: 20px;
  cursor: pointer;
}
.switch input { position: absolute; opacity: 0; pointer-events: none; }

.switch__track {
  flex: none;
  width: 48px; height: 29px;
  margin-top: 1px;
  border-radius: 999px;
  background: var(--surface-2);
  border: 1px solid var(--line);
  transition: background .22s;
}
.switch__knob {
  display: block;
  width: 23px; height: 23px;
  margin: 2px;
  border-radius: 50%;
  background: var(--ink-faint);
  transition: transform .22s var(--ease), background .22s;
}
.switch input:checked + .switch__track { background: rgba(245,185,66,.28); border-color: rgba(245,185,66,.45); }
.switch input:checked + .switch__track .switch__knob { transform: translateX(19px); background: var(--accent); }

.switch__text { font-size: 15px; }
.switch__text small { display: block; margin-top: 3px; color: var(--ink-faint); font-size: 13px; line-height: 1.42; }

.danger-btn {
  width: 100%;
  height: 46px;
  border: 1px solid rgba(224,128,111,.4);
  border-radius: var(--r-sm);
  background: rgba(224,128,111,.1);
  color: var(--rose);
  font: 500 15px/1 inherit;
  cursor: pointer;
}

.fineprint { margin-top: 16px; color: var(--ink-faint); font-size: 12.5px; line-height: 1.5; }

/* ------------------------------- toast ------------------------------- */
.toast {
  position: fixed;
  z-index: 60;
  left: 50%;
  bottom: calc(var(--safe-b) + 118px);
  transform: translateX(-50%);
  max-width: min(88vw, 380px);
  padding: 12px 18px;
  border: 1px solid var(--line);
  border-radius: var(--r-md);
  background: rgba(24,15,44,.94);
  -webkit-backdrop-filter: blur(16px);
  backdrop-filter: blur(16px);
  font-size: 14.5px;
  line-height: 1.45;
  text-align: center;
  box-shadow: 0 14px 34px rgba(0,0,0,.5);
  animation: rise .3s var(--ease);
}
@keyframes rise { from { opacity: 0; transform: translate(-50%, 12px); } }

/* --------------------------- short screens --------------------------- */
@media (max-height: 700px) {
  .caption { min-height: 58px; padding-top: 6px; }
  .caption__text { font-size: 16px; }
  .mic { width: 76px; height: 76px; }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after {
    animation-duration: .001ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: .001ms !important;
  }
}
