/* ==========================================================================
   Talk Surf (#/surf) — lướt sóng bằng giọng nói (docs/talksurf-plan.md).
   Mọi luật nằm dưới .tsf-root; luật theo body.s8-play / .s8-imm / .view-wide viết bằng :is() thay vì đảo
   vế, để selector tầng ngoài cùng LUÔN mở bằng .tsf- (tools/test.mjs quét từng dòng). Biến khai trong
   .tsf-root, không ở gốc tài liệu. Mọi lớp chồng dưới trần 70 của repo (70 là của nút thoát toàn màn hình).
   Canvas nằm DƯỚI mọi màn: bến, chuẩn bị, phát lại đều thấy biển — trên điện thoại màn là một "tấm"
   trượt từ dưới lên, trên máy rộng là một thẻ bên phải.
   ========================================================================== */
.tsf-root {
  --tsf-ink: #15324a;
  --tsf-dim: #4a6275;
  --tsf-card: rgba(255, 255, 255, 0.94);
  --tsf-line: #d7e6ef;
  --tsf-navy: #241b4a;
  --tsf-mint: #00ce90;
  --tsf-mint-ink: #05271c;
  --tsf-hes: #ff9f40;
  --tsf-sink: #e5484d;
  --tsf-um: #7c5cff;
  --tsf-r: 16px;
  margin: -18px -16px -64px;
  background: #0e6f8f;
  color: var(--tsf-ink);
  -webkit-tap-highlight-color: transparent;
}
.tsf-root:is(.view-wide *) { margin-top: 0; margin-left: 0; margin-right: 0; }
.tsf-root * { box-sizing: border-box; }

/* Cột cố định, không cuộn trang. 60px = thanh trên của app. */
.tsf-root #tsfApp {
  position: relative;
  height: calc(100vh - 60px);
  height: calc(100dvh - 60px);
  min-height: 420px;
  overflow: hidden;
  overscroll-behavior: none;
  padding-left: env(safe-area-inset-left);
  padding-right: env(safe-area-inset-right);
}
.tsf-root .tsf-cv { position: absolute; inset: 0; width: 100%; height: 100%; display: block; }
.tsf-root .tsf-sr { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; }

/* ---- nút ---- */
.tsf-root button { font: inherit; touch-action: manipulation; cursor: pointer; }
.tsf-root .tsf-btn {
  min-height: 44px; padding: 10px 16px; border-radius: 12px; border: 2px solid var(--tsf-line);
  background: #fff; color: var(--tsf-ink); font-weight: 800; font-size: 15px;
}
.tsf-root .tsf-btn:active { transform: translateY(1px); }
.tsf-root .tsf-btn:disabled { opacity: .55; cursor: default; }
.tsf-root .tsf-btn-main { background: var(--tsf-mint); border-color: var(--tsf-mint); color: var(--tsf-mint-ink); }
.tsf-root .tsf-back {
  min-height: 44px; min-width: 44px; padding: 8px 10px; border: 0; background: transparent;
  color: var(--tsf-dim); font-weight: 800; font-size: 15px;
}
.tsf-root .tsf-pill {
  display: inline-block; padding: 4px 10px; border-radius: 999px; background: #e7f5fb; color: var(--tsf-navy);
  font-size: 13px; font-weight: 800;
}
.tsf-root .tsf-row { display: flex; align-items: center; gap: 8px; margin-bottom: 8px; }
.tsf-root .tsf-row h2 { margin: 0; font-size: 18px; }
.tsf-root .tsf-tip { margin: 10px 0 0; color: var(--tsf-dim); font-size: 13px; line-height: 1.45; }
.tsf-root .tsf-note { margin: 8px 0; padding: 10px 12px; border-radius: 12px; background: #fff4e5; font-size: 14px; }
.tsf-root .tsf-warn { color: #9a3b00; }

/* ---- tấm màn (điện thoại: tấm dưới; máy rộng: thẻ phải) ---- */
.tsf-root .tsf-panel {
  position: absolute; left: 0; right: 0; bottom: 0; z-index: 3;
  max-height: 70%; overflow-y: auto; overscroll-behavior: contain;
  padding: 0 12px calc(12px + env(safe-area-inset-bottom));
}
.tsf-root .tsf-card {
  background: var(--tsf-card); border-radius: var(--tsf-r) var(--tsf-r) 0 0; padding: 14px 16px 16px;
  box-shadow: 0 -6px 24px rgba(8, 40, 60, 0.18);
}
.tsf-root .tsf-title { margin: 0; font-size: 28px; letter-spacing: -0.5px; color: var(--tsf-navy); }
.tsf-root .tsf-sub { margin: 4px 0 12px; color: var(--tsf-dim); font-size: 15px; }

/* bến */
.tsf-root .tsf-modes { display: grid; gap: 10px; }
.tsf-root .tsf-mode {
  display: grid; grid-template-columns: 48px 1fr; grid-template-rows: auto auto; column-gap: 12px; text-align: left;
  min-height: 72px; padding: 12px 14px; border-radius: 14px; border: 2px solid #bfe6f2; background: #effaff; color: var(--tsf-ink);
}
.tsf-root .tsf-mode-p2 { background: #fff3ea; border-color: #ffd2b5; }
.tsf-root .tsf-mode-ic { grid-row: span 2; font-size: 34px; line-height: 48px; text-align: center; }
.tsf-root .tsf-mode b { font-size: 16px; }
.tsf-root .tsf-mode small { color: var(--tsf-dim); font-size: 13px; }
.tsf-root .tsf-stats { display: flex; flex-wrap: wrap; gap: 6px 14px; margin: 12px 0 0; font-size: 14px; color: var(--tsf-dim); }
.tsf-root .tsf-stats b { color: var(--tsf-navy); }
.tsf-root .tsf-recent h3 { margin: 14px 0 6px; font-size: 15px; }
.tsf-root .tsf-ride {
  display: grid; grid-template-columns: auto 1fr auto; grid-template-areas: "p t s" "p n s"; gap: 0 10px; width: 100%;
  min-height: 52px; padding: 8px 10px; margin-bottom: 6px; border-radius: 12px; border: 1px solid var(--tsf-line);
  background: #fff; text-align: left; color: var(--tsf-ink);
}
.tsf-root .tsf-ride-p { grid-area: p; align-self: center; font-weight: 900; color: var(--tsf-navy); }
.tsf-root .tsf-ride-t { grid-area: t; font-size: 14px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.tsf-root .tsf-ride-n { grid-area: n; font-size: 13px; color: var(--tsf-dim); }
.tsf-root .tsf-ride-s { grid-area: s; align-self: center; color: #e2a100; letter-spacing: 1px; }
.tsf-root .tsf-how { margin-top: 12px; font-size: 14px; color: var(--tsf-dim); }
.tsf-root .tsf-how summary { min-height: 44px; display: flex; align-items: center; font-weight: 800; color: var(--tsf-ink); cursor: pointer; }
.tsf-root .tsf-how ul { margin: 0; padding-left: 18px; line-height: 1.5; }

/* chủ đề Part 1 */
.tsf-root .tsf-topics .tsf-btn-main { width: 100%; margin-bottom: 10px; }
.tsf-root .tsf-chips { display: grid; grid-template-columns: repeat(auto-fill, minmax(140px, 1fr)); gap: 8px; }
.tsf-root .tsf-chip {
  min-height: 52px; padding: 8px 10px; border-radius: 12px; border: 1px solid var(--tsf-line); background: #fff;
  text-align: left; font-weight: 700; font-size: 14px; color: var(--tsf-ink);
}
.tsf-root .tsf-chip small { display: block; font-weight: 500; color: var(--tsf-dim); font-size: 12px; }

/* chuẩn bị Part 2 + sẵn sàng */
.tsf-root .tsf-cue-head { margin: 4px 0 6px; font-size: 18px; line-height: 1.35; }
.tsf-root .tsf-cue-say { margin: 0 0 4px; font-weight: 700; color: var(--tsf-dim); font-size: 14px; }
.tsf-root .tsf-cue { margin: 0; padding: 0; list-style: none; display: grid; gap: 6px; }
.tsf-root .tsf-cue li { display: grid; grid-template-columns: 26px 1fr; gap: 8px; align-items: start; font-size: 15px; line-height: 1.4; }
.tsf-root .tsf-cue-n {
  width: 26px; height: 26px; border-radius: 50%; background: #ffd23f; color: var(--tsf-navy);
  display: grid; place-items: center; font-weight: 900; font-size: 13px;
}
.tsf-root .tsf-prep-foot { display: flex; flex-wrap: wrap; align-items: center; gap: 8px; margin-top: 12px; }
.tsf-root .tsf-clock { font-size: 26px; font-weight: 900; color: var(--tsf-navy); min-width: 72px; font-variant-numeric: tabular-nums; }
.tsf-root .tsf-q { margin: 4px 0 8px; font-size: 20px; line-height: 1.35; color: var(--tsf-navy); }
.tsf-root .tsf-q-sm { font-size: 16px; }
.tsf-root .tsf-say { white-space: pre-line; font-size: 14px; color: var(--tsf-dim); margin-bottom: 10px; }
.tsf-root .tsf-btn-go { width: 100%; font-size: 17px; min-height: 52px; }

/* đếm ngược: nằm giữa, trên biển */
.tsf-root #tsfApp[data-screen="count"] .tsf-panel { top: 0; max-height: none; display: grid; place-items: center; background: rgba(8, 40, 60, 0.25); }
.tsf-root .tsf-count { display: grid; justify-items: center; gap: 6px; color: #fff; text-shadow: 0 2px 8px rgba(0, 0, 0, 0.35); }
.tsf-root .tsf-count-n { font-size: 96px; font-weight: 900; line-height: 1; }
.tsf-root .tsf-count small { font-size: 15px; font-weight: 700; }

/* gửi / lỗi: thẻ giữa */
.tsf-root .tsf-send { display: grid; justify-items: center; gap: 10px; text-align: center; }
.tsf-root .tsf-send p { margin: 0; font-weight: 700; }
.tsf-root .tsf-fail { display: grid; gap: 8px; }
.tsf-root .tsf-fail-msg { margin: 0; font-weight: 800; }

/* ---- HUD lúc lướt ---- */
.tsf-root .tsf-hud {
  position: absolute; inset: 0; z-index: 2; pointer-events: none;
  display: flex; flex-direction: column; justify-content: space-between;
  padding: calc(10px + env(safe-area-inset-top)) 12px calc(12px + env(safe-area-inset-bottom));
}
.tsf-root .tsf-hud[hidden] { display: none; }
.tsf-root .tsf-hud-top { display: flex; gap: 8px; justify-content: space-between; }
.tsf-root .tsf-hud-top span {
  padding: 6px 12px; border-radius: 999px; background: rgba(21, 50, 74, 0.72); color: #fff;
  font-weight: 900; font-size: 16px; font-variant-numeric: tabular-nums;
}
.tsf-root .tsf-banner {
  max-width: min(100%, 640px); padding: 10px 14px; border-radius: 14px;
  background: rgba(255, 255, 255, 0.92); color: var(--tsf-navy); font-weight: 800; font-size: 16px; line-height: 1.35; text-align: center;
  box-shadow: 0 4px 16px rgba(8, 40, 60, 0.18);
}
.tsf-root .tsf-banner-k { display: inline-block; margin-right: 4px; padding: 1px 8px; border-radius: 999px; background: #ffd23f; font-size: 13px; }
.tsf-root .tsf-pop { animation: tsf-pop .45s ease-out; }
@keyframes tsf-pop { 0% { transform: scale(0.8); } 60% { transform: scale(1.06); } 100% { transform: scale(1); } }
.tsf-root .tsf-cuecard {
  pointer-events: auto; width: min(100%, 640px); padding: 0 12px; border-radius: 12px;
  background: rgba(255, 255, 255, 0.85); font-size: 14px;
}
.tsf-root .tsf-cuecard summary { min-height: 44px; display: flex; align-items: center; font-weight: 800; cursor: pointer; }
.tsf-root .tsf-cuecard .tsf-cue { padding-bottom: 10px; }
.tsf-root .tsf-hud-bot { margin-top: auto; display: grid; justify-items: center; gap: 8px; }
.tsf-root .tsf-hint {
  margin: 0; min-height: 1.4em; color: #fff; font-weight: 900; font-size: 18px; text-shadow: 0 2px 6px rgba(0, 0, 0, 0.45);
}
.tsf-root .tsf-hint-hot { color: #ffe066; }
.tsf-root .tsf-btn-done {
  pointer-events: auto; min-width: 160px; min-height: 52px; font-size: 18px; border: 0;
  background: #fff; color: var(--tsf-navy); box-shadow: 0 4px 16px rgba(8, 40, 60, 0.3);
}

/* ---- phát lại ---- */
.tsf-root .tsf-rstats { display: flex; flex-wrap: wrap; gap: 6px 14px; font-size: 14px; color: var(--tsf-dim); margin-bottom: 8px; }
.tsf-root .tsf-rstats b { color: var(--tsf-navy); }
.tsf-root .tsf-stars { color: #e2a100; letter-spacing: 1px; }
.tsf-root .tsf-rbar { display: grid; grid-template-columns: 48px 1fr auto; align-items: center; gap: 10px; margin-bottom: 22px; }
.tsf-root .tsf-rplay {
  width: 48px; height: 48px; border-radius: 50%; border: 0; background: var(--tsf-navy); color: #fff; font-size: 18px; font-weight: 900;
}
.tsf-root .tsf-rtrack { position: relative; height: 44px; border-radius: 10px; background: #e7f2f8; overflow: visible; }
.tsf-root .tsf-rfill { position: absolute; left: 0; top: 0; bottom: 0; width: 0; border-radius: 10px; background: #c9e9f5; }
.tsf-root .tsf-rtime { font-size: 13px; color: var(--tsf-dim); font-variant-numeric: tabular-nums; }
/* Mốc: một nút rộng 28px (chạm trúng trên điện thoại), chấm màu vẽ bằng ::before. */
.tsf-root .tsf-mk {
  position: absolute; top: 0; bottom: 0; width: 28px; margin-left: -14px; padding: 0; border: 0; background: transparent;
}
.tsf-root .tsf-mk::before {
  content: ""; position: absolute; left: 50%; width: 12px; height: 12px; margin-left: -6px; border-radius: 50%;
  border: 2px solid #fff; box-shadow: 0 1px 3px rgba(0, 0, 0, 0.3);
}
.tsf-root .tsf-mk-hes::before { top: 5px; background: var(--tsf-hes); }
.tsf-root .tsf-mk-sink::before { top: 5px; background: var(--tsf-sink); width: 14px; height: 14px; margin-left: -7px; }
.tsf-root .tsf-mk-um::before { bottom: 5px; background: var(--tsf-um); }
.tsf-root .tsf-st {
  position: absolute; top: calc(100% + 3px); width: 20px; height: 18px; margin-left: -10px; border-radius: 5px;
  display: grid; place-items: center; font-size: 11px; font-weight: 900; background: #ffd23f; color: var(--tsf-navy);
  pointer-events: none;
}
.tsf-root .tsf-st-ok { background: #1fbf75; color: #fff; }
.tsf-root .tsf-st-no { background: var(--tsf-sink); color: #fff; }
.tsf-root .tsf-st-miss { opacity: .55; }
.tsf-root .tsf-legend { margin: 6px 0 10px; font-size: 12px; color: var(--tsf-dim); }
.tsf-root .tsf-lg { display: inline-block; width: 10px; height: 10px; border-radius: 50%; vertical-align: -1px; }
.tsf-root .tsf-lg.tsf-mk-hes { background: var(--tsf-hes); }
.tsf-root .tsf-lg.tsf-mk-sink { background: var(--tsf-sink); }
.tsf-root .tsf-lg.tsf-mk-um { background: var(--tsf-um); }
.tsf-root .tsf-ai { padding: 12px; border-radius: 14px; background: #f3f8fb; margin-bottom: 10px; }
.tsf-root .tsf-ai:empty { display: none; }
.tsf-root .tsf-ai-p { margin: 0 0 8px; font-size: 14px; line-height: 1.45; }
.tsf-root .tsf-ai-wait { display: flex; align-items: center; gap: 10px; }
.tsf-root .tsf-ai-btns { display: flex; flex-wrap: wrap; gap: 8px; }
.tsf-root .tsf-ai-score { display: flex; align-items: baseline; gap: 4px; color: var(--tsf-navy); }
.tsf-root .tsf-ai-score b { font-size: 40px; line-height: 1; }
.tsf-root .tsf-ai-crit { display: flex; flex-wrap: wrap; gap: 6px 12px; margin: 6px 0 10px; font-size: 13px; color: var(--tsf-dim); }
.tsf-root .tsf-ai-crit b { color: var(--tsf-navy); }
.tsf-root .tsf-tx { margin-bottom: 10px; font-size: 14px; line-height: 1.55; }
.tsf-root .tsf-tx summary { min-height: 44px; display: flex; align-items: center; font-weight: 800; cursor: pointer; }
.tsf-root .tsf-tx p { margin: 0; }
.tsf-root .tsf-um { background: #ece6ff; color: #4b2fd6; border-radius: 4px; padding: 0 2px; }
.tsf-root .tsf-actions { display: flex; flex-wrap: wrap; gap: 8px; }

/* ---- khổ điện thoại: thêm 56px thanh tab dưới ---- */
@media (max-width: 719px) {
  .tsf-root #tsfApp { height: calc(100vh - 124px); }
  .tsf-root #tsfApp {
    height: calc(100dvh - 60px - 56px - env(safe-area-inset-top) - env(safe-area-inset-bottom));
  }
  .tsf-root .tsf-title { font-size: 24px; }
  .tsf-root .tsf-q { font-size: 18px; }
  /* Tấm màn chừa phần trên cho biển: engine đặt người lướt ở ~1/3 chiều cao khi màn dọc. */
  .tsf-root .tsf-panel { max-height: 60%; }
  .tsf-root #tsfApp[data-screen="replay"] .tsf-panel { max-height: 56%; }
}

/* ---- máy rộng: thẻ bên phải, biển bên trái ---- */
@media (min-width: 720px) {
  .tsf-root .tsf-panel {
    left: auto; top: 16px; right: 16px; bottom: 16px; width: min(440px, 46%); max-height: none;
    padding: 0 0 env(safe-area-inset-bottom);
  }
  .tsf-root .tsf-card { border-radius: var(--tsf-r); box-shadow: 0 8px 28px rgba(8, 40, 60, 0.22); }
  .tsf-root #tsfApp[data-screen="send"] .tsf-panel,
  .tsf-root #tsfApp[data-screen="fail"] .tsf-panel { top: auto; bottom: 24px; }
  .tsf-root .tsf-hud-top span { font-size: 18px; }
  .tsf-root .tsf-banner { font-size: 18px; }
}

/* ---- body.s8-play: thanh tab dưới đã ẩn (css/ispeak.css) ⇒ trả lại 56px ---- */
@media (max-width: 979px) {
  .tsf-root:is(body.s8-play *) { margin-bottom: 0; }
}
@media (max-width: 719px) {
  .tsf-root:is(body.s8-play *) #tsfApp {
    height: calc(100vh - 60px);
    height: calc(100dvh - 60px - env(safe-area-inset-top) - env(safe-area-inset-bottom));
  }
}

/* ---- body.s8-imm: thanh trên cũng đi ⇒ trả lại nốt 60px; chừa 56px góc phải cho nút thoát ---- */
.tsf-root:is(body.s8-play.s8-imm *) #tsfApp {
  height: calc(100vh - env(safe-area-inset-top));
  height: calc(100dvh - env(safe-area-inset-top) - env(safe-area-inset-bottom));
}
.tsf-root:is(body.s8-play.s8-imm *) .tsf-hud-top { padding-right: 56px; }
@media (min-width: 720px) {
  .tsf-root:is(body.s8-play.s8-imm *) .tsf-panel { top: 72px; }
}

@media (prefers-reduced-motion: reduce) {
  .tsf-root .tsf-pop { animation: none; }
  .tsf-root .tsf-btn:active { transform: none; }
}
