/* Say It In Mandarin — phone-first, dark, big touch targets. */

:root {
  --bg:        #0e1116;
  --bg-raised: #171b22;
  --bg-hover:  #1e242d;
  --line:      #262d38;
  --text:      #e8ecf2;
  --text-dim:  #9aa4b2;
  --text-faint:#6b7482;
  --accent:    #ff5c8a;
  --accent-dim:#3a1f2a;

  /* Tone colours, matching Pleco's default mapping (1 red, 2 green, 3 blue,
     4 purple, 5 grey). There is no universal standard, but Pleco is the
     de-facto one and the app a learner is most likely to meet next — and the
     previous scheme here used the *same* colours for *different* tones, which
     is the worst kind of clash to carry forward.

     Colour is deliberately the secondary channel: the one controlled study of
     tone colour-coding found it performed worse than plain tone marks, and
     red/green is the commonest colour-vision deficiency. The contour glyph
     under each syllable is the primary signal. */
  --t1: #ff6b6b;
  --t2: #3ecf7d;
  --t3: #5aa9ff;
  --t4: #c08cff;
  --t5: #8b95a3;

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

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }

/* The UA's [hidden] rule is HTML-namespaced, so it silently does nothing to
   <svg>. Restate it namespace-agnostically. */
[hidden] { display: none !important; }

html, body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 16px/1.5 -apple-system, BlinkMacSystemFont, "Segoe UI", system-ui, sans-serif;
  overscroll-behavior-y: none;
}

button { font: inherit; color: inherit; background: none; border: none; cursor: pointer; }
svg { fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }

/* ── Top bar ─────────────────────────────────────────────── */

.topbar {
  position: sticky; top: 0; z-index: 20;
  padding: calc(var(--safe-t) + 14px) 16px 10px;
  background: rgba(14, 17, 22, .86);
  backdrop-filter: blur(16px);
  -webkit-backdrop-filter: blur(16px);
  border-bottom: 1px solid var(--line);
}

.topbar-row { display: flex; align-items: center; justify-content: space-between; gap: 12px; }

.topbar h1 {
  margin: 0;
  font-size: 19px; font-weight: 700; letter-spacing: -.02em;
}

.ghost-btn {
  flex: none;
  padding: 9px 14px;
  font-size: 14px; font-weight: 600; color: var(--text-dim);
  border: 1px solid var(--line); border-radius: 999px;
}
.ghost-btn:active { background: var(--bg-hover); }

.primary-btn {
  flex: none;
  padding: 9px 18px;
  font-size: 14px; font-weight: 700; color: #fff;
  background: var(--accent); border-radius: 999px;
}
.primary-btn:active { filter: brightness(.92); }

.menu-btn { margin: 0 -8px 0 0; }

.search-wrap { position: relative; margin-top: 12px; }
.search-icon {
  position: absolute; left: 13px; top: 50%; transform: translateY(-50%);
  width: 18px; height: 18px; color: var(--text-faint); pointer-events: none;
}
#search {
  width: 100%; height: 42px;
  padding: 0 14px 0 40px;
  font-size: 16px; color: var(--text);
  background: var(--bg-raised);
  border: 1px solid var(--line); border-radius: 12px;
  -webkit-appearance: none;
}
#search::placeholder { color: var(--text-faint); }
#search:focus { outline: none; border-color: var(--accent); }

.chips {
  display: flex; gap: 8px;
  margin: 12px -16px -2px; padding: 0 16px 4px;
  overflow-x: auto; scrollbar-width: none;
}
.chips::-webkit-scrollbar { display: none; }

.chip {
  flex: none;
  padding: 8px 14px;
  font-size: 14px; font-weight: 600; white-space: nowrap;
  color: var(--text-dim);
  background: var(--bg-raised);
  border: 1px solid var(--line); border-radius: 999px;
}
.chip[aria-pressed="true"] {
  color: #fff;
  background: var(--accent);
  border-color: var(--accent);
}

/* ── Phrase list ─────────────────────────────────────────── */

.list {
  padding: 12px 16px calc(var(--safe-b) + 32px);
  display: flex; flex-direction: column; gap: 8px;
}

.cat-head {
  margin: 14px 2px 2px;
  font-size: 12px; font-weight: 700; letter-spacing: .09em; text-transform: uppercase;
  color: var(--text-faint);
}
.cat-head:first-child { margin-top: 2px; }

.list-intro {
  margin: 2px 2px 6px;
  font-size: 13px; line-height: 1.5; color: var(--text-dim);
}

.card {
  display: flex; align-items: center; gap: 12px;
  width: 100%; padding: 13px 14px;
  text-align: left;
  background: var(--bg-raised);
  border: 1px solid var(--line); border-radius: 14px;
}
.card:active { background: var(--bg-hover); }

.card-text { flex: 1; min-width: 0; }
.card-en {
  font-size: 16px; font-weight: 600; letter-spacing: -.01em;
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}
.card-zh { margin-top: 3px; font-size: 15px; }
.card-say { font-weight: 600; }
.card-say .t1 { color: var(--t1); } .card-say .t2 { color: var(--t2); }
.card-say .t3 { color: var(--t3); } .card-say .t4 { color: var(--t4); }
.card-say .t5 { color: var(--t5); }
.card-script { color: var(--text-faint); }
.card-fav { flex: none; width: 15px; height: 15px; color: var(--accent); fill: var(--accent); }

.card-play {
  flex: none;
  display: grid; place-items: center;
  width: 40px; height: 40px;
  color: var(--text-dim);
  background: var(--bg-hover);
  border-radius: 50%;
}
.card-play svg { width: 18px; height: 18px; fill: currentColor; stroke: none; }
.card-play:active, .card-play.playing { color: #fff; background: var(--accent); }

.empty { padding: 40px 16px; text-align: center; color: var(--text-faint); }

/* ── Detail sheet ────────────────────────────────────────── */

.sheet {
  position: fixed; inset: 0; z-index: 40;
  background: var(--bg);
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  animation: rise .22s cubic-bezier(.22,.75,.3,1);
}
@keyframes rise { from { opacity: 0; transform: translateY(14px); } }

.sheet-inner {
  max-width: 620px; margin: 0 auto;
  padding: calc(var(--safe-t) + 10px) 20px calc(var(--safe-b) + 40px);
}

.sheet-head { display: flex; align-items: center; justify-content: space-between; }

.icon-btn {
  display: grid; place-items: center;
  width: 42px; height: 42px; margin-left: -10px;
  color: var(--text-dim);
  border-radius: 50%;
}
.icon-btn svg { width: 24px; height: 24px; }
.icon-btn.star { margin: 0 -8px 0 0; }
.icon-btn.star[aria-pressed="true"] { color: var(--accent); }
.icon-btn.star[aria-pressed="true"] svg { fill: var(--accent); }

.d-en {
  margin: 14px 0 0;
  font-size: 27px; font-weight: 700; line-height: 1.24; letter-spacing: -.025em;
}

.d-note {
  margin: 12px 0 0; padding: 11px 13px;
  font-size: 14px; line-height: 1.45; color: var(--text-dim);
  background: var(--bg-raised);
  border-left: 3px solid var(--accent); border-radius: 0 10px 10px 0;
}

/* Hanzi + pinyin stack, one column per syllable */

.hanzi-row {
  display: flex; flex-wrap: wrap; gap: 6px;
  margin: 26px 0 0;
}

.syl {
  display: flex; flex-direction: column; align-items: center; gap: 3px;
  min-width: 46px; padding: 8px 6px 7px;
  border-radius: 11px;
  border: 1px solid transparent;
  transition: background .12s, border-color .12s, transform .12s;
}
.syl.word-end { margin-right: 10px; }
.syl.active {
  background: var(--accent-dim);
  border-color: var(--accent);
  transform: translateY(-2px);
}

/* The respelling is the headline — it's what actually gets read aloud. */
.syl-say {
  font-size: 26px; line-height: 1.15; font-weight: 700; letter-spacing: -.015em;
}
.syl-han { font-size: 20px; line-height: 1.2; font-weight: 500; opacity: .85; }
.syl-py  { font-size: 13px; font-weight: 600; letter-spacing: .01em; opacity: .85; }
.syl-tone { width: 15px; height: 9px; opacity: .85; }
.syl-tone path { stroke-width: 2.4; }

.t1 .syl-say, .t1 .syl-han, .t1 .syl-py, .t1 .syl-tone { color: var(--t1); }
.t2 .syl-say, .t2 .syl-han, .t2 .syl-py, .t2 .syl-tone { color: var(--t2); }
.t3 .syl-say, .t3 .syl-han, .t3 .syl-py, .t3 .syl-tone { color: var(--t3); }
.t4 .syl-say, .t4 .syl-han, .t4 .syl-py, .t4 .syl-tone { color: var(--t4); }
.t5 .syl-say, .t5 .syl-han, .t5 .syl-py, .t5 .syl-tone { color: var(--t5); }

.syl-punct {
  align-self: center;
  padding-bottom: 14px;
  font-size: 28px; color: var(--text-faint);
}

.d-phon {
  margin: 18px 0 0;
  font-size: 15px; color: var(--text-dim);
}
.d-phon b { color: var(--text); font-weight: 600; }

.d-sandhi {
  margin: 10px 0 0; padding: 11px 13px;
  font-size: 13px; line-height: 1.5; color: var(--text-dim);
  background: var(--bg-raised); border-radius: 10px;
}
.d-sandhi b { color: var(--text); font-weight: 600; }

/* Marks a syllable whose tone shifts because of the one after it. */
.syl.sandhi .syl-say {
  text-decoration: underline dotted currentColor 2px;
  text-underline-offset: 4px;
}

/* ── Player ──────────────────────────────────────────────── */

.player {
  display: flex; align-items: center; gap: 18px;
  margin: 28px 0 0; padding: 18px;
  background: var(--bg-raised);
  border: 1px solid var(--line); border-radius: 18px;
}

.play-btn {
  flex: none;
  display: grid; place-items: center;
  width: 68px; height: 68px;
  color: #fff;
  background: var(--accent);
  border-radius: 50%;
  box-shadow: 0 6px 20px -6px var(--accent);
  transition: transform .12s;
}
.play-btn:active { transform: scale(.94); }
.play-btn svg { grid-area: 1 / 1; width: 30px; height: 30px; fill: currentColor; stroke: none; }
.play-btn .i-play { margin-left: 2px; }
.play-btn:not(.playing) .i-stop { display: none; }
.play-btn.playing .i-play { display: none; }

.speed { flex: 1; min-width: 0; }
.speed-head { display: flex; align-items: baseline; justify-content: space-between; }
.speed-head label { font-size: 13px; font-weight: 600; color: var(--text-dim); }
#speed-val {
  font-size: 15px; font-weight: 700;
  font-variant-numeric: tabular-nums;
}

#speed {
  width: 100%; height: 34px; margin: 2px 0 0;
  background: none; -webkit-appearance: none; appearance: none;
}
#speed::-webkit-slider-runnable-track {
  height: 6px; border-radius: 3px;
  background: linear-gradient(var(--accent), var(--accent)) 0/var(--fill,50%) 100% no-repeat, var(--bg-hover);
}
#speed::-moz-range-track { height: 6px; border-radius: 3px; background: var(--bg-hover); }
#speed::-moz-range-progress { height: 6px; border-radius: 3px; background: var(--accent); }
#speed::-webkit-slider-thumb {
  -webkit-appearance: none;
  width: 26px; height: 26px; margin-top: -10px;
  background: #fff; border-radius: 50%;
  box-shadow: 0 2px 8px rgba(0,0,0,.5);
}
#speed::-moz-range-thumb {
  width: 26px; height: 26px; border: none;
  background: #fff; border-radius: 50%;
}

.speed-ticks {
  display: flex; justify-content: space-between;
  font-size: 11px; color: var(--text-faint);
}

/* ── Mode buttons ────────────────────────────────────────── */

.modes { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; margin-top: 12px; }

.mode-btn {
  display: flex; flex-direction: column; gap: 2px;
  padding: 12px 8px;
  background: var(--bg-raised);
  border: 1px solid var(--line); border-radius: 14px;
}
.mode-title { font-size: 14px; font-weight: 600; }
.mode-sub   { font-size: 11px; color: var(--text-faint); }
.mode-btn[aria-pressed="true"] {
  color: #fff;
  background: var(--accent);
  border-color: var(--accent);
}
.mode-btn[aria-pressed="true"] .mode-sub { color: rgba(255,255,255,.8); }

.hint {
  margin: 12px 0 0; padding: 11px 13px;
  font-size: 13px; line-height: 1.45; color: var(--text-dim);
  background: var(--bg-raised); border-radius: 12px;
}

/* ── Record & compare ────────────────────────────────────── */

.compare { margin-top: 12px; }

.rec-btn {
  display: flex; align-items: center; justify-content: center; gap: 10px;
  width: 100%; padding: 14px;
  font-size: 15px; font-weight: 600;
  background: var(--bg-raised);
  border: 1px solid var(--line); border-radius: 14px;
}
.rec-btn:active { background: var(--bg-hover); }

.rec-dot {
  width: 12px; height: 12px;
  background: var(--accent); border-radius: 50%;
}
.rec-btn.recording { border-color: var(--accent); color: var(--accent); }
.rec-btn.recording .rec-dot { animation: pulse 1.1s ease-in-out infinite; }
@keyframes pulse { 50% { opacity: .25; transform: scale(.8); } }

.cmp-actions { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; margin-top: 8px; }

.cmp-btn {
  padding: 12px 8px;
  font-size: 14px; font-weight: 600;
  background: var(--bg-raised);
  border: 1px solid var(--line); border-radius: 14px;
}
.cmp-btn:active { background: var(--bg-hover); }
.cmp-btn.accent { color: #fff; background: var(--accent); border-color: var(--accent); }

/* ── Tone legend ─────────────────────────────────────────── */

.tone-key {
  display: flex; flex-wrap: wrap; align-items: center; gap: 6px 12px;
  margin-top: 26px; padding-top: 18px;
  border-top: 1px solid var(--line);
  font-size: 12px; color: var(--text-faint);
}
.tone-key-label { font-weight: 700; letter-spacing: .07em; text-transform: uppercase; }
.tk { display: inline-flex; align-items: center; gap: 5px; }
.tk i { width: 10px; height: 10px; border-radius: 3px; }
.tk.t1 i { background: var(--t1); } .tk.t2 i { background: var(--t2); }
.tk.t3 i { background: var(--t3); } .tk.t4 i { background: var(--t4); }
.tk.t5 i { background: var(--t5); }

/* ── Settings ────────────────────────────────────────────── */

.sheet-title {
  margin: 10px 0 4px;
  font-size: 28px; font-weight: 700; letter-spacing: -.025em;
}

.group { margin-top: 26px; }

.group-head {
  margin: 0 2px 8px;
  font-size: 12px; font-weight: 700; letter-spacing: .09em; text-transform: uppercase;
  color: var(--text-faint);
}

.group-body {
  background: var(--bg-raised);
  border: 1px solid var(--line); border-radius: 14px;
  overflow: hidden;
}

.row {
  display: flex; align-items: center; gap: 14px;
  width: 100%; padding: 14px;
  text-align: left;
  border-top: 1px solid var(--line);
}
.row:first-child { border-top: none; }
.row.action:active { background: var(--bg-hover); }

.row-text { flex: 1; min-width: 0; }
.row-title { display: block; font-size: 15px; font-weight: 600; }
.row-sub {
  display: block; margin-top: 3px;
  font-size: 13px; line-height: 1.45; color: var(--text-dim);
}
.row-sub a { color: var(--accent); }
.row.block { display: block; }
.row.block .row-title { margin-bottom: 4px; }
.row-sub b { color: var(--text); font-weight: 600; }

.inline-ico {
  width: 15px; height: 15px; vertical-align: -3px;
  color: var(--accent);
}

.pill {
  flex: none;
  padding: 7px 14px;
  font-size: 13px; font-weight: 700; white-space: nowrap;
  color: #fff; background: var(--accent);
  border-radius: 999px;
}
.pill.danger { color: var(--text-dim); background: var(--bg-hover); }
.pill[data-state="busy"] { color: var(--text-dim); background: var(--bg-hover); }
.pill[data-state="done"] { color: #fff; background: var(--t3); }

/* Toggle switch */
.row.toggle { cursor: pointer; }
.row.toggle input { position: absolute; opacity: 0; pointer-events: none; }

.switch {
  flex: none; position: relative;
  width: 50px; height: 30px;
  background: var(--bg-hover);
  border: 1px solid var(--line); border-radius: 999px;
  transition: background .16s, border-color .16s;
}
.switch::after {
  content: ""; position: absolute; top: 3px; left: 3px;
  width: 22px; height: 22px;
  background: var(--text-dim); border-radius: 50%;
  transition: transform .16s, background .16s;
}
.row.toggle input:checked ~ .switch { background: var(--accent); border-color: var(--accent); }
.row.toggle input:checked ~ .switch::after { transform: translateX(20px); background: #fff; }
.row.toggle input:focus-visible ~ .switch { outline: 2px solid var(--accent); outline-offset: 2px; }

/* ── Update prompt ───────────────────────────────────────── */

.update-bar {
  position: fixed; left: 12px; right: 12px; bottom: calc(var(--safe-b) + 12px); z-index: 70;
  display: flex; align-items: center; gap: 14px; flex-wrap: wrap;
  max-width: 560px; margin: 0 auto; padding: 14px 16px;
  background: var(--bg-hover);
  border: 1px solid var(--line); border-radius: 16px;
  box-shadow: 0 12px 36px rgba(0,0,0,.55);
  animation: rise .24s cubic-bezier(.22,.75,.3,1);
}
.update-text { flex: 1; min-width: 150px; }
.update-text strong { display: block; font-size: 15px; }
.update-text span { font-size: 13px; color: var(--text-dim); }
.update-actions { display: flex; gap: 8px; }

/* ── Display preferences ─────────────────────────────────── */

/* Tone colouring off: fall back to plain text, but keep the contour marks,
   which still carry the tone without relying on colour. */
html.no-tone-colour .syl-say,
html.no-tone-colour .syl-han,
html.no-tone-colour .syl-py,
html.no-tone-colour .card-say .t1,
html.no-tone-colour .card-say .t2,
html.no-tone-colour .card-say .t3,
html.no-tone-colour .card-say .t4,
html.no-tone-colour .card-say .t5 { color: var(--text); }

html.hide-pinyin .syl-py { display: none; }

html.hide-hanzi .syl-han,
html.hide-hanzi .card-script { display: none; }

/* ── Ear training ────────────────────────────────────────── */

.ear-btn { margin-left: auto; margin-right: 4px; color: var(--text-dim); }

.drill-score { font-size: 14px; font-weight: 700; color: var(--text-dim); font-variant-numeric: tabular-nums; }

.drill-lead { margin: 6px 0 0; font-size: 14px; line-height: 1.5; color: var(--text-dim); }

.drill-card {
  margin-top: 20px; padding: 22px 18px;
  text-align: center;
  background: var(--bg-raised);
  border: 1px solid var(--line); border-radius: 16px;
}
.drill-meaning { margin: 0; font-size: 14px; color: var(--text-dim); }
.drill-syl { margin: 8px 0 2px; font-size: 40px; font-weight: 700; letter-spacing: -.02em; }
.drill-where { margin: 0 0 16px; font-size: 13px; color: var(--text-faint); }

.drill-choices { display: grid; grid-template-columns: repeat(2, 1fr); gap: 8px; margin-top: 12px; }

.drill-choice {
  display: flex; flex-direction: column; align-items: center; gap: 6px;
  padding: 18px 10px;
  background: var(--bg-raised);
  border: 1px solid var(--line); border-radius: 14px;
  font-size: 15px; font-weight: 600;
}
.drill-choice svg { width: 26px; height: 15px; }
.drill-choice svg path { stroke-width: 2.4; }
.drill-choice:active { background: var(--bg-hover); }
.drill-choice[disabled] { opacity: .45; }
.drill-choice.correct { border-color: var(--t2); background: color-mix(in srgb, var(--t2) 18%, transparent); opacity: 1; }
.drill-choice.wrong   { border-color: var(--t1); background: color-mix(in srgb, var(--t1) 18%, transparent); opacity: 1; }

.drill-feedback { margin: 16px 0 0; font-size: 15px; line-height: 1.5; }
.drill-feedback b { font-weight: 700; }
.drill-next { width: 100%; margin-top: 12px; padding: 15px; }

/* ── Present mode ────────────────────────────────────────── */

.sheet-head-right { display: flex; align-items: center; gap: 6px; }

.pill-btn {
  padding: 8px 14px;
  font-size: 14px; font-weight: 600;
  border: 1px solid var(--line); border-radius: 999px;
}
.pill-btn:active { background: var(--bg-hover); }

.present {
  position: fixed; inset: 0; z-index: 80;
  display: flex; flex-direction: column;
  /* Near-black behind near-white: maximum legibility across a dim table. */
  background: #000; color: #fff;
  padding: calc(var(--safe-t) + 16px) 16px calc(var(--safe-b) + 16px);
}

.present-body {
  flex: 1;
  display: flex; flex-direction: column; align-items: center; justify-content: center;
  gap: 4vh; text-align: center;
  transition: transform .2s;
}
.present.flipped .present-body { transform: rotate(180deg); }

.present-en { margin: 0; font-size: 20px; font-weight: 600; color: #9aa4b2; }
.present-zh {
  margin: 0;
  /* Scales with the viewport so it stays readable across a table. */
  font-size: min(19vw, 13vh); line-height: 1.25; font-weight: 500;
  letter-spacing: .02em;
}
.present-py { margin: 0; font-size: 22px; color: #c3cad4; }

.present-bar { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; }
.present .cmp-btn { background: #14181e; border-color: #2a313c; color: #fff; }

/* ── Quick strip ─────────────────────────────────────────── */

.quickbar {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 30;
  padding: 8px 12px calc(var(--safe-b) + 8px);
  background: rgba(14, 17, 22, .93);
  backdrop-filter: blur(16px); -webkit-backdrop-filter: blur(16px);
  border-top: 1px solid var(--line);
}

.quick-tiles {
  display: flex; gap: 8px;
  overflow-x: auto; scrollbar-width: none;
}
.quick-tiles::-webkit-scrollbar { display: none; }

.quick-tile {
  flex: none;
  display: flex; flex-direction: column; gap: 2px;
  /* Comfortably past the 44pt minimum — this gets tapped one-handed, walking. */
  min-width: 116px; min-height: 56px;
  padding: 9px 12px;
  text-align: left;
  background: var(--bg-raised);
  border: 1px solid var(--line); border-radius: 12px;
}
.quick-tile:active { background: var(--bg-hover); border-color: var(--accent); }
.quick-tile.playing { border-color: var(--accent); }
.quick-tile-en {
  font-size: 13px; font-weight: 600; white-space: nowrap;
  overflow: hidden; text-overflow: ellipsis; max-width: 150px;
}
.quick-tile-say {
  font-size: 12px; color: var(--text-dim); white-space: nowrap;
  overflow: hidden; text-overflow: ellipsis; max-width: 150px;
}

/* Keep the last cards clear of the fixed strip. */
body.has-quickbar .list { padding-bottom: calc(var(--safe-b) + 104px); }

/* ── Toast ───────────────────────────────────────────────── */

.toast {
  position: fixed; left: 50%; bottom: calc(var(--safe-b) + 24px); z-index: 60;
  transform: translateX(-50%);
  padding: 11px 18px;
  font-size: 14px; font-weight: 600; white-space: nowrap;
  background: var(--bg-hover);
  border: 1px solid var(--line); border-radius: 999px;
  box-shadow: 0 8px 28px rgba(0,0,0,.5);
  animation: rise .2s;
}

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