/* ธีม "Trig Neon" — โทเคนและสไตล์ทั้งหมดของเกม
   ค่าทุกค่าตรงกับไฟล์ดีไซน์ (design_handoff_trig_match) แบบตัวต่อตัว
   สีที่เปลี่ยนตามสถานะ (คอมโบ, โหมด, toast) ถูกเซ็ตจาก src/app.js เป็น inline style */

:root {
  --bg: #05070f;
  --sheet: #0a1226;
  --card: rgba(12, 20, 42, 0.8);
  --inset: rgba(10, 18, 38, 0.7);
  --btn: rgba(16, 26, 50, 0.9);
  --border: #23385f;
  --border-soft: #1e3055;

  --text: #e8f1ff;
  --text-strong: #fff;
  --text-2: #cfe3ff;
  --text-3: #b7d0ee;
  --text-4: #9dbde4;
  --muted: #8fb2e0;
  --faint: #5f7ba6;
  --faint-2: #6f8db8;

  --accent: #22e3ff;
  --good: #3dffa8;
  --bad: #ff4d5e;
  --bad-text: #ff8f9a;
  --gold: #ffd447;

  --font-ui: 'IBM Plex Sans Thai', system-ui, -apple-system, sans-serif;
  --font-display: 'Chakra Petch', system-ui, sans-serif;
  --font-math: ui-serif, 'Cambria Math', 'Times New Roman', Georgia, serif;

  --ease-out: cubic-bezier(0.2, 0.8, 0.3, 1);
  --ease-snap: cubic-bezier(0.22, 0.61, 0.36, 1);
}

* { box-sizing: border-box; }

html,
body {
  margin: 0;
  padding: 0;
  background: var(--bg);
  overscroll-behavior: none;
  -webkit-text-size-adjust: 100%;
}

body {
  min-height: 100dvh;
  display: flex;
  justify-content: center;
  font-family: var(--font-ui);
  color: var(--text);
}

#app {
  width: 100%;
  display: flex;
  justify-content: center;
}

button,
input { font: inherit; }
button { cursor: pointer; }

/* ---------- keyframes (board.js ต้องการ trigflow กับ edgePulse) ---------- */

@keyframes trigflow { to { stroke-dashoffset: -24; } }

@keyframes edgePulse {
  0%, 100% { opacity: 1; stroke-width: 7; }
  50% { opacity: 0.55; stroke-width: 10; }
}

@keyframes poprise {
  0% { transform: translate(-50%, 0) scale(0.7); opacity: 0; }
  18% { transform: translate(-50%, -14px) scale(1.12); opacity: 1; }
  100% { transform: translate(-50%, -72px) scale(1); opacity: 0; }
}

@keyframes glowPulse {
  0%, 100% { opacity: 0.45; }
  50% { opacity: 0.9; }
}

@keyframes riseIn {
  from { transform: translateY(18px); opacity: 0; }
  to { transform: translateY(0); opacity: 1; }
}

@keyframes snapDemo {
  0%, 10% { transform: translate(40px, -24px) rotate(24deg); }
  40%, 80% { transform: translate(0, 0) rotate(0deg); }
  94%, 100% { transform: translate(40px, -24px) rotate(24deg); }
}

@keyframes seamGlow {
  0%, 32% { stroke: #22e3ff; stroke-width: 3; }
  42%, 80% { stroke: #3dffa8; stroke-width: 5; }
  92%, 100% { stroke: #22e3ff; stroke-width: 3; }
}

/* ---------- โครงหน้าจอ ---------- */

.app {
  position: relative;
  width: 100%;
  max-width: 540px;
  height: 100dvh;
  display: flex;
  flex-direction: column;
  overflow: hidden;
  background: radial-gradient(120% 60% at 50% 0%, #12224a 0%, #080d1e 55%, #05070f 100%);
}

/* ตารางจาง ๆ ให้พื้นหลังไม่ตายสนิท */
.app::before {
  content: '';
  position: absolute;
  inset: 0;
  pointer-events: none;
  background-image:
    linear-gradient(rgba(34, 227, 255, 0.045) 1px, transparent 1px),
    linear-gradient(90deg, rgba(34, 227, 255, 0.045) 1px, transparent 1px);
  background-size: 34px 34px;
  -webkit-mask-image: radial-gradient(80% 60% at 50% 40%, #000 30%, transparent 100%);
  mask-image: radial-gradient(80% 60% at 50% 40%, #000 30%, transparent 100%);
}

/* ---------- หัวเกม ---------- */

.hud {
  position: relative;
  z-index: 3;
  padding: 14px 16px 10px;
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.hud-row {
  display: flex;
  align-items: center;
  gap: 10px;
}

.hud-dot {
  width: 9px;
  height: 9px;
  border-radius: 50%;
  background: var(--good);
  box-shadow: 0 0 12px var(--good);
  animation: glowPulse 2.4s ease-in-out infinite;
}

.hud-brand {
  font-family: var(--font-display);
  font-weight: 700;
  font-size: 13px;
  letter-spacing: 0.16em;
  color: #7fa4d8;
  white-space: nowrap;
}

.spacer { flex: 1; }

.pill {
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 5px 11px;
  border-radius: 999px;
  border: 1px solid var(--border);
  background: var(--inset);
}

.pill-label {
  font-size: 11px;
  color: var(--faint-2);
}

.pill-value {
  font-family: var(--font-display);
  font-weight: 600;
  font-size: 14px;
  font-variant-numeric: tabular-nums;
  color: var(--text-2);
}

.icon-btn {
  width: 32px;
  height: 32px;
  flex: none;
  border-radius: 10px;
  border: 1px solid var(--border);
  background: var(--inset);
  color: var(--muted);
  font-size: 15px;
  font-weight: 700;
  line-height: 1;
  transition: border-color 160ms, color 160ms;
}

.icon-btn:hover {
  border-color: #3a6ba8;
  color: var(--text-2);
}

.hud-main {
  display: flex;
  align-items: flex-end;
  gap: 14px;
}

.score-box {
  display: flex;
  flex-direction: column;
  gap: 1px;
}

.eyebrow {
  font-size: 10px;
  letter-spacing: 0.2em;
  color: var(--faint);
}

.score-value {
  font-family: var(--font-display);
  font-weight: 700;
  font-size: 38px;
  line-height: 1;
  font-variant-numeric: tabular-nums;
  color: var(--text-strong);
  text-shadow: 0 0 22px rgba(34, 227, 255, 0.45);
}

.combo-box {
  display: flex;
  flex-direction: column;
  align-items: flex-end;
  gap: 5px;
}

.combo-head {
  display: flex;
  align-items: baseline;
  gap: 6px;
}

.combo-value {
  font-family: var(--font-display);
  font-weight: 700;
  font-size: 26px;
  line-height: 1;
  color: var(--accent);
}

.combo-track {
  width: 126px;
  height: 6px;
  border-radius: 999px;
  background: rgba(255, 255, 255, 0.09);
  overflow: hidden;
}

.combo-fill {
  height: 100%;
  width: 0;
  border-radius: 999px;
  background: var(--accent);
  transition: width 280ms var(--ease-out);
}

.progress-track {
  flex: 1;
  height: 9px;
  border-radius: 999px;
  background: rgba(255, 255, 255, 0.07);
  border: 1px solid rgba(255, 255, 255, 0.06);
  overflow: hidden;
}

.progress-fill {
  height: 100%;
  width: 0;
  background: linear-gradient(90deg, #22e3ff, #3dffa8);
  box-shadow: 0 0 14px rgba(61, 255, 168, 0.6);
  transition: width 400ms var(--ease-out);
}

.progress-count {
  font-family: var(--font-display);
  font-size: 13px;
  font-weight: 600;
  font-variant-numeric: tabular-nums;
  color: var(--text-4);
}

.progress-wrong {
  font-size: 11px;
  color: #ff6b7a;
  font-variant-numeric: tabular-nums;
}

/* ---------- กระดาน ---------- */

.board-host {
  position: relative;
  z-index: 2;
  flex: 1;
  min-height: 0;
}

/* คะแนนเด้ง — อยู่ในระบบพิกัดเดียวกับ svg ของกระดาน */
.pop-layer {
  position: absolute;
  inset: 0;
  pointer-events: none;
  z-index: 6;
  overflow: visible;
}

.pop {
  position: absolute;
  transform: translate(-50%, 0);
  font-family: var(--font-display);
  font-weight: 700;
  white-space: nowrap;
  animation: poprise 1000ms var(--ease-out) forwards;
}

/* ---------- แถบล่าง ---------- */

.dock {
  position: relative;
  z-index: 3;
  padding: 8px 12px calc(12px + env(safe-area-inset-bottom, 0px));
  display: flex;
  flex-direction: column;
  gap: 8px;
  background: linear-gradient(to top, rgba(5, 7, 15, 0.95), rgba(5, 7, 15, 0));
}

.chip {
  display: flex;
  align-items: center;
  gap: 10px;
  min-height: 46px;
  padding: 6px 14px;
  border-radius: 14px;
  border: 1px solid var(--border);
  background: rgba(10, 18, 38, 0.78);
  transition: border-color 200ms;
}

.chip-label {
  font-size: 11px;
  color: var(--faint-2);
  white-space: nowrap;
}

.chip-slot {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: 30px;
  overflow: hidden;
}

.dock-row {
  display: flex;
  gap: 7px;
}

.dock-btn {
  flex: 1;
  min-height: 46px;
  border-radius: 13px;
  border: 1px solid var(--border);
  background: var(--btn);
  color: #bcd6f5;
  font-family: var(--font-ui);
  font-weight: 600;
  font-size: 13px;
}

.dock-btn:active { transform: translateY(1px); }

/* ---------- toast ---------- */

.toast {
  position: absolute;
  left: 50%;
  bottom: 118px;
  transform: translateX(-50%);
  z-index: 7;
  pointer-events: none;
  max-width: calc(100% - 32px);
  padding: 9px 18px;
  border-radius: 999px;
  background: rgba(10, 18, 38, 0.94);
  border: 1px solid #1e5540;
  color: var(--good);
  font-size: 13px;
  font-weight: 600;
  text-align: center;
  opacity: 0;
  transition: opacity 200ms;
}

/* ---------- ฉากคลุม (intro / จบเกม) ---------- */

.overlay {
  position: absolute;
  inset: 0;
  overflow-y: auto;
  background: linear-gradient(180deg, rgba(6, 10, 24, 0.97), rgba(5, 7, 15, 0.99));
}

.overlay[hidden] { display: none; }

/* ตัวคอนเทนเนอร์ที่เลื่อนต้องไม่ justify-content:center เอง ไม่งั้นส่วนที่ล้น
   จะโผล่เหนือ scrollTop:0 แล้วหัวเรื่องจะถูกตัดหายบนจอเตี้ย — ใส่ชั้นในแทน */
.overlay-inner {
  min-height: 100%;
  display: flex;
  flex-direction: column;
  justify-content: center;
  gap: 18px;
  padding: 24px 22px calc(24px + env(safe-area-inset-bottom, 0px));
}

.intro { z-index: 10; }

.rise { animation: riseIn 460ms var(--ease-out) both; }
.rise-2 { animation: riseIn 460ms 90ms var(--ease-out) both; }
.rise-3 { animation: riseIn 460ms 180ms var(--ease-out) both; }

.intro-eyebrow {
  font-family: var(--font-display);
  font-size: 12px;
  letter-spacing: 0.3em;
  color: var(--accent);
}

.intro-title {
  margin: 6px 0 0;
  font-family: var(--font-display);
  font-weight: 700;
  font-size: 46px;
  line-height: 1.02;
  color: var(--text-strong);
  text-shadow: 0 0 34px rgba(34, 227, 255, 0.5);
}

.intro-body {
  margin: 12px 0 0;
  font-size: 14px;
  line-height: 1.75;
  color: var(--text-4);
  max-width: 30ch;
  text-wrap: pretty;
}

.intro-body strong { color: var(--accent); }

.demo {
  margin-top: 14px;
  padding: 10px 12px 8px;
  border-radius: 16px;
  border: 1px solid var(--border-soft);
  background: rgba(10, 18, 38, 0.6);
}

.demo svg {
  width: 100%;
  height: 96px;
  display: block;
}

.demo-caption {
  margin-top: 4px;
  text-align: center;
  font-size: 12px;
  color: var(--muted);
}

.field {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.field-label {
  font-size: 11px;
  letter-spacing: 0.18em;
  color: var(--faint);
}

.mode-btn {
  display: flex;
  align-items: center;
  gap: 12px;
  text-align: left;
  padding: 14px 16px;
  border-radius: 16px;
  border: 1px solid var(--border);
  background: rgba(12, 20, 42, 0.7);
  color: inherit;
  font: inherit;
  transition: border-color 180ms, background 180ms;
}

.mode-glyph { font-size: 22px; }

.mode-text {
  display: flex;
  flex-direction: column;
  gap: 2px;
}

.mode-text strong {
  font-size: 15px;
  color: var(--text-strong);
}

.mode-text span {
  font-size: 12px;
  color: var(--muted);
}

.cta-row {
  display: flex;
  gap: 8px;
}

.cta {
  flex: 2;
  min-height: 56px;
  border-radius: 16px;
  border: 0;
  background: var(--accent);
  color: #04121c;
  font-family: var(--font-display);
  font-weight: 700;
  font-size: 18px;
  letter-spacing: 0.04em;
  box-shadow: 0 0 30px rgba(34, 227, 255, 0.45);
}

.cta:active { transform: translateY(1px); }

.cta-ghost {
  flex: 1;
  min-height: 56px;
  border-radius: 16px;
  border: 1px solid var(--border);
  background: var(--btn);
  color: #bcd6f5;
  font-weight: 600;
  font-size: 14px;
}

/* ---------- แผ่นวิธีเล่น ---------- */

.sheet-scrim {
  position: absolute;
  inset: 0;
  z-index: 11;
  display: flex;
  align-items: flex-end;
  background: rgba(4, 7, 16, 0.72);
}

.sheet-scrim[hidden] { display: none; }

.sheet {
  width: 100%;
  max-height: 100%;
  overflow-y: auto;
  padding: 24px 22px calc(26px + env(safe-area-inset-bottom, 0px));
  border-radius: 24px 24px 0 0;
  border-top: 1px solid var(--border);
  background: var(--sheet);
  animation: riseIn 300ms var(--ease-out) both;
}

.sheet h2 {
  margin: 0 0 14px;
  font-family: var(--font-display);
  font-size: 22px;
  color: var(--text-strong);
}

.sheet ul {
  margin: 0 0 20px;
  padding-left: 18px;
  font-size: 14px;
  line-height: 1.85;
  color: var(--text-3);
}

.sheet-confirm {
  width: 100%;
  min-height: 52px;
  border-radius: 14px;
  border: 0;
  background: var(--accent);
  color: #04121c;
  font-family: var(--font-display);
  font-weight: 700;
  font-size: 16px;
}

/* ---------- หน้าสรุป ---------- */

.end { z-index: 12; }
.end .overlay-inner { padding: 28px 22px calc(28px + env(safe-area-inset-bottom, 0px)); }

.grade-row {
  display: flex;
  align-items: center;
  gap: 16px;
  animation: riseIn 420ms var(--ease-out) both;
}

.grade-badge {
  width: 84px;
  height: 84px;
  flex: none;
  border-radius: 22px;
  display: flex;
  align-items: center;
  justify-content: center;
  border: 2px solid var(--gold);
  background: rgba(255, 212, 71, 0.1);
  font-family: var(--font-display);
  font-weight: 700;
  font-size: 44px;
  color: var(--gold);
  text-shadow: 0 0 26px rgba(255, 212, 71, 0.6);
}

.grade-text {
  display: flex;
  flex-direction: column;
  gap: 3px;
}

.grade-head {
  font-size: 12px;
  letter-spacing: 0.2em;
  color: var(--faint);
}

.grade-score {
  font-family: var(--font-display);
  font-size: 40px;
  line-height: 1;
  color: var(--text-strong);
  font-variant-numeric: tabular-nums;
}

.grade-word {
  font-size: 13px;
  color: var(--good);
}

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

.stat {
  padding: 11px 12px;
  border-radius: 14px;
  border: 1px solid var(--border-soft);
  background: var(--card);
}

.stat-label {
  font-size: 10px;
  color: var(--faint);
}

.stat-value {
  font-family: var(--font-display);
  font-size: 19px;
  font-variant-numeric: tabular-nums;
  color: var(--text-2);
}

.stat-value.bad { color: var(--bad-text); }
.stat-value.gold { color: var(--gold); }

.name-row {
  display: flex;
  gap: 8px;
}

.name-row[hidden] { display: none; }

.name-input {
  flex: 1;
  min-width: 0;
  min-height: 50px;
  padding: 0 16px;
  border-radius: 14px;
  border: 1px solid var(--border);
  background: rgba(12, 20, 42, 0.9);
  color: var(--text-strong);
  font-size: 15px;
  outline: none;
}

.name-input:focus { border-color: var(--accent); }

.name-save {
  min-width: 96px;
  min-height: 50px;
  border-radius: 14px;
  border: 0;
  background: var(--good);
  color: #04120c;
  font-weight: 700;
  font-size: 15px;
}

.section {
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.section[hidden] { display: none; }

.section-head {
  display: flex;
  align-items: center;
  gap: 8px;
}

.section-head span:first-child {
  font-size: 11px;
  letter-spacing: 0.2em;
  color: var(--faint);
  white-space: nowrap;
}

.rule {
  flex: 1;
  height: 1px;
  background: var(--border-soft);
}

.leader {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 10px 14px;
  border-radius: 12px;
  border: 1px solid var(--border-soft);
  background: var(--card);
}

.leader.top {
  border-color: #4a3d16;
  background: rgba(255, 212, 71, 0.09);
}

.leader-rank {
  font-family: var(--font-display);
  font-weight: 700;
  font-size: 15px;
  min-width: 20px;
  color: var(--muted);
}

.leader.top .leader-rank { color: var(--gold); }
.leader.second .leader-rank { color: var(--text-2); }

.leader-name {
  flex: 1;
  min-width: 0;
  font-size: 14px;
  font-weight: 600;
  color: #dbe9fb;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.leader-time {
  font-size: 12px;
  color: var(--faint-2);
  font-variant-numeric: tabular-nums;
}

.leader-score {
  font-family: var(--font-display);
  font-size: 17px;
  color: var(--text-strong);
  font-variant-numeric: tabular-nums;
}

.leader-empty {
  padding: 16px;
  border-radius: 12px;
  border: 1px dashed var(--border-soft);
  color: var(--faint);
  font-size: 13px;
  text-align: center;
}

.answer {
  display: flex;
  gap: 10px;
  padding: 9px 12px;
  border-radius: 10px;
  background: rgba(12, 20, 42, 0.6);
  font-size: 13px;
  line-height: 1.6;
}

.answer-n {
  color: var(--good);
  font-family: var(--font-display);
}

.answer-text {
  color: var(--text-3);
  font-family: var(--font-math);
}

/* ---------- ข้อความเมื่อโหลดชุดโจทย์ไม่สำเร็จ ---------- */

.fatal {
  position: absolute;
  inset: auto 16px 16px;
  z-index: 20;
  margin: 0;
  padding: 14px 16px;
  border-radius: 14px;
  border: 1px solid #5a2530;
  background: rgba(24, 8, 14, 0.95);
  color: var(--bad-text);
  font-family: var(--font-ui);
  font-size: 13px;
  line-height: 1.7;
  white-space: pre-wrap;
}

.fatal[hidden] { display: none; }
