/* Mandarin Lab — one clean dark screen */
* { box-sizing: border-box; margin: 0; padding: 0; }

:root {
  --bg: #12151b;
  --panel: #1a1f28;
  --panel-2: #202632;
  --text: #e8ebf0;
  --muted: #8a92a0;
  --accent: #ffd867;
  --border: rgba(255, 255, 255, 0.08);
}

html { height: 100%; }
body {
  background: var(--bg);
  color: var(--text);
  font-family: "Segoe UI", system-ui, -apple-system, sans-serif;
  display: flex;
  flex-direction: column;
  /* The compact two-column dashboard is the desktop design and STAYS. It simply
     gets a scrollbar now: `height:100%` + `overflow:hidden` clipped whatever
     didn't fit a short window with no way to reach it. min-height keeps it
     viewport-filling when there's room; main's min-height (below) gives the
     dashboard a floor so a short window scrolls a little instead of squashing. */
  min-height: 100%;
  overflow-y: auto;
  overflow-x: hidden;
}

header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 14px 22px;
  border-bottom: 1px solid var(--border);
}
.brand { display: flex; align-items: baseline; gap: 14px; }
h1 { font-size: 20px; font-weight: 600; letter-spacing: 0.3px; }
.tagline { color: var(--muted); font-size: 12.5px; }
.header-right { display: flex; align-items: center; gap: 10px; }
.badge {
  background: var(--accent);
  color: #1a1608;
  font-size: 12px;
  font-weight: 700;
  padding: 4px 10px;
  border-radius: 99px;
}

button, .import-label {
  font: inherit;
  cursor: pointer;
  color: var(--text);
  background: var(--panel-2);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 6px 12px;
}
.ghost { font-size: 13px; color: var(--muted); }
/* hover only where a pointer exists — on touch, :hover STICKS after a tap and
   leaves buttons looking permanently active */
@media (hover: hover) {
  .ghost:hover { color: var(--text); border-color: rgba(255, 255, 255, 0.2); }
}
.ghost.small { font-size: 12px; padding: 4px 10px; align-self: center; }
.import-label { display: inline-block; }
.import-label input { display: none; }

main {
  flex: 1;
  display: grid;
  grid-template-columns: 1.25fr 1fr;
  gap: 18px;
  padding: 18px 22px;
  /* the dashboard's comfortable floor: taller windows fill (unchanged compact
     look), shorter ones scroll a little rather than squashing the lanes/mic */
  min-height: 620px;
}

/* left: practice */
.practice {
  display: flex;
  flex-direction: column;
  gap: 14px;
  min-height: 0;
}
.target-card {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 14px;
  padding: 18px 22px;
  display: flex;
  flex-direction: column;
  gap: 6px;
}
.target-head { display: flex; justify-content: space-between; align-items: center; }
.target-label {
  font-size: 11px;
  letter-spacing: 1.6px;
  text-transform: uppercase;
  color: var(--muted);
}
.focus { font-size: 12px; color: var(--accent); }
.target-zh { font-size: 44px; font-weight: 600; letter-spacing: 6px; }
.target-pinyin { font-size: 18px; color: var(--accent); }
.target-sandhi { font-size: 12.5px; color: #4fbf8b; min-height: 16px; }
.target-en { font-size: 14px; color: var(--muted); }

#contour-canvas {
  /* Lane height answers to the VIEWPORT and nothing else. flex-grow (even
     capped) made the lanes answer to the grid ROW, and the row answers to the
     TALLER column — so when the tutor reply / skill map / tracker populate at
     the end of a take, the lanes stretched to match the neighbor column's new
     content (Aaron's exsten.jpg: "at the end, not in real time"). clamp() keeps
     the design growth (taller window → taller lanes) with hard floor/ceiling,
     and fully severs layout feedback from both the drawing buffer and the
     right column. */
  flex: 0 0 auto;
  height: clamp(250px, 38vh, 480px);
  width: 100%;
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 14px;
}

/* One compact line by default so the dashboard stays viewport-sized; the full
   disclosure is one click away (it must stay reachable — public mic app). */
.privacy-note {
  max-width: 640px;
  margin: 8px auto 0;
  text-align: center;
  font-size: 11px;
  line-height: 1.5;
  opacity: 0.5;
}
.privacy-note summary {
  cursor: pointer;
  list-style: none;
}
.privacy-note summary::-webkit-details-marker { display: none; }
.privacy-note summary span { text-decoration: underline; opacity: 0.8; }
.privacy-note p { margin-top: 6px; }

.say-btn {
  font-size: 11px;
  padding: 1px 8px;
  margin-left: 8px;
  vertical-align: middle;
}

.syll-row {
  display: flex;
  justify-content: center;
  gap: 22px;
  padding: 4px 0 2px;
  font-size: 21px;
  min-height: 30px;
}
.syll-row .syll b {
  font-size: 12px;
  font-weight: 600;
  margin-left: 5px;
  opacity: 0.75;
}

.mic-area {
  display: flex;
  align-items: center;
  gap: 14px;
  justify-content: center;
  padding-top: 2px;
}
#mic-btn {
  width: 72px;
  height: 72px;
  border-radius: 50%;
  background: var(--panel-2);
  border: 2px solid var(--border);
  color: var(--text);
  display: flex;
  align-items: center;
  justify-content: center;
  transition: transform 0.1s, background 0.15s, border-color 0.15s;
  user-select: none;
  -webkit-user-select: none;
  -webkit-touch-callout: none;  /* iOS long-press must record, not pop a menu */
  touch-action: none;
}
@media (hover: hover) {
  #mic-btn:hover { border-color: var(--accent); }
}
#mic-btn.recording {
  background: #b03a3a;
  border-color: #ff7a7a;
  transform: scale(1.08);
  animation: pulse 1.2s infinite;
}
@keyframes pulse {
  0%, 100% { box-shadow: 0 0 0 0 rgba(255, 122, 122, 0.4); }
  50% { box-shadow: 0 0 0 14px rgba(255, 122, 122, 0); }
}
#mic-label { color: var(--muted); font-size: 14px; }
.status {
  text-align: center;
  color: var(--muted);
  font-size: 13.5px;
  min-height: 20px;
  padding-bottom: 2px;
}

/* right: skill map + feed */
.side {
  display: flex;
  flex-direction: column;
  gap: 18px;
  /* the row's height is the LEFT column's business: contribute nothing to row
     sizing (height 0), then stretch to whatever the row resolved to
     (min-height 100%). A long tutor conversation now scrolls INSIDE its card
     instead of growing the column and the page (Aaron: "scroll down as it
     populates, without having to scroll the whole page"). */
  height: 0;
  min-height: 100%;
}
.panel {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 14px;
  padding: 16px 18px;
  display: flex;
  flex-direction: column;
  min-height: 0;
}
.panel-head {
  display: flex;
  justify-content: space-between;
  align-items: baseline;
  margin-bottom: 12px;
}
.panel h2 { font-size: 14px; font-weight: 600; letter-spacing: 0.5px; }
.sub { color: var(--muted); font-size: 12px; }

.skill-panel { flex: 0 0 auto; }
.tone-row {
  display: grid;
  grid-template-columns: 110px 1fr 64px;
  align-items: center;
  gap: 10px;
  padding: 4px 0;
  font-size: 13px;
}
.tone-name { font-weight: 600; }
.bar {
  height: 8px;
  background: rgba(255, 255, 255, 0.06);
  border-radius: 99px;
  overflow: hidden;
}
.fill { height: 100%; border-radius: 99px; transition: width 0.5s; }
.tone-val { text-align: right; color: var(--muted); }
.tone-val em { font-style: normal; font-size: 11px; }

/* living per-sentence record */
.sentence-log { margin-top: 12px; font-size: 12.5px; }
.sentence-log summary {
  cursor: pointer;
  list-style: none;
  color: var(--muted);
  padding: 4px 0;
  border-top: 1px solid var(--border);
}
.sentence-log summary::-webkit-details-marker { display: none; }
.sentence-log summary span { text-decoration: underline; }
.sentence-log summary b { color: var(--text); }
.sentence-log summary::after { content: " ▾"; opacity: 0.6; }
.sentence-log[open] summary::after { content: " ▴"; }
#sentence-rows { max-height: 210px; overflow-y: auto; padding-top: 4px; }
.sent-row {
  display: grid;
  grid-template-columns: 1fr auto auto;
  align-items: center;
  gap: 10px;
  padding: 5px 0;
  border-bottom: 1px solid rgba(255, 255, 255, 0.04);
}
.sent-zh { font-size: 15px; }
.spark { display: inline-flex; align-items: flex-end; gap: 2px; height: 18px; }
.spark i { display: block; width: 4px; border-radius: 1px; }
.sent-nums { color: var(--muted); font-size: 11.5px; white-space: nowrap; }
.sent-nums b { color: var(--text); }
.sent-nums em { font-style: normal; margin-left: 4px; }
.sent-nums em.up { color: #4fbf8b; }
.sent-nums em.down { color: #e0736d; }
.sent-empty { color: var(--muted); font-size: 12px; padding: 6px 0; }

.chips { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 12px; }
.chip {
  background: var(--panel-2);
  border: 1px solid var(--border);
  border-radius: 99px;
  font-size: 12.5px;
  padding: 3px 10px;
  color: var(--muted);
}
.chip b { color: var(--text); }

.feed-panel { flex: 1; min-height: 180px; }  /* tutor never squeezed below usefulness */
#feed {
  flex: 1;
  min-height: 0;       /* flex children default to min-height:auto and GROW — this
                          is what lets overflow-y actually engage */
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  gap: 10px;
  padding-right: 4px;
  scrollbar-width: thin;
}
#feed::-webkit-scrollbar, #sentence-rows::-webkit-scrollbar { width: 8px; }
#feed::-webkit-scrollbar-thumb, #sentence-rows::-webkit-scrollbar-thumb {
  background: rgba(255, 255, 255, 0.14);
  border-radius: 4px;
}
#feed::-webkit-scrollbar-thumb:hover, #sentence-rows::-webkit-scrollbar-thumb:hover {
  background: rgba(255, 255, 255, 0.25);
}
#sentence-rows {
  /* expanding the practiced-sentence list must not squeeze the tutor card
     out of the bounded column — it scrolls in place too */
  max-height: 180px;
  overflow-y: auto;
  scrollbar-width: thin;
}
.msg {
  border-radius: 12px;
  padding: 10px 14px;
  max-width: 92%;
  font-size: 14px;
  line-height: 1.45;
}
.msg.tutor { background: var(--panel-2); align-self: flex-start; }
.msg.user { background: #2a3547; align-self: flex-end; }
.msg .zh { font-size: 17px; }
.msg .pinyin { color: var(--accent); font-size: 13px; }
.msg .detail { color: var(--muted); font-size: 12.5px; margin-top: 2px; }
.msg .assessment {
  margin-top: 6px;
  padding-top: 6px;
  border-top: 1px solid var(--border);
  font-size: 13px;
  color: #cfd6e0;
}

/* ---------------------------------------------------------------- mobile
   The desktop layout is a FIXED-HEIGHT app shell (body overflow hidden, grid
   panes that flex to the viewport). On a phone that produces overflow, cut-off
   controls and overlapping panels — so below 900px we switch to NATURAL
   DOCUMENT FLOW: everything stacks, scrolls, and the mic stays thumb-reachable. */
@media (max-width: 900px) {
  html, body { height: auto; overflow: visible; overflow-x: hidden; }
  header {
    flex-wrap: wrap;
    gap: 8px 12px;
    padding: 12px 16px;
    position: sticky;         /* level + export stay reachable while scrolling */
    top: 0;
    z-index: 5;
    background: var(--bg);
  }
  .brand { flex: 1 1 auto; min-width: 0; }
  h1 { font-size: 17px; }
  .tagline { display: none; }       /* decoration; the app title carries it */
  .header-right { gap: 8px; }

  main {
    display: block;                  /* stacked, natural height */
    padding: 14px 16px 24px;
  }
  .practice { display: block; }
  /* the tutor's answer is what you want right after speaking — on a phone it must
     come BEFORE the skill map, not below it */
  .side {
    display: flex; flex-direction: column; margin-top: 16px;
    height: auto; min-height: 0;   /* stacked flow: the desktop height-0 severing
                                      would clip the whole column here */
  }
  .feed-panel { order: 1; min-height: 0; }
  .skill-panel { order: 2; }
  .panel { margin-bottom: 14px; }

  .target-card { padding: 14px 16px; }
  .target-zh { font-size: 34px; letter-spacing: 3px; }
  .target-pinyin { font-size: 16px; }
  .target-head { flex-wrap: wrap; gap: 4px; }
  .focus { font-size: 11px; }

  #contour-canvas {
    height: 200px;                   /* flex:1 has no meaning in normal flow */
    min-height: 0;
    margin-top: 12px;
    display: block;
  }
  .syll-row { gap: 14px; font-size: 19px; }

  /* thumb-sized, always the visual anchor of the screen */
  .mic-area { padding-top: 10px; }
  #mic-btn { width: 84px; height: 84px; }
  #mic-label { font-size: 13px; }
  .status { font-size: 13px; padding: 6px 4px 0; }
  .privacy-note { font-size: 12px; opacity: 0.62; margin-top: 14px; }  /* readable on a real phone */

  .tone-row { grid-template-columns: 92px 1fr 52px; gap: 8px; font-size: 12.5px; }
  #feed { max-height: 60vh; }        /* feed scrolls internally, page stays sane */
  .msg { max-width: 100%; }
}

/* touch devices at ANY width (tablets land in the desktop layout): real 44px
   targets and no double-tap zoom stealing a deliberate tap */
@media (pointer: coarse) {
  button, .import-label { min-height: 44px; padding: 10px 14px; touch-action: manipulation; }
  .ghost.small { min-height: 40px; }
  .say-btn { min-height: 32px; padding: 2px 12px; }
}

@media (prefers-reduced-motion: reduce) {
  * { animation: none !important; transition: none !important; scroll-behavior: auto !important; }
}

@media (max-width: 420px) {
  .target-zh { font-size: 30px; letter-spacing: 2px; }
  #contour-canvas { height: 180px; }
  .syll-row { gap: 10px; font-size: 18px; }
  .tone-row { grid-template-columns: 84px 1fr 46px; }
}
