:root {
  --navy: #111f4d;
  --navy-2: #172b68;
  --blue: #263c89;
  --gold: #d5a84a;
  --gold-2: #f3d37b;
  --cream: #fff8ea;
  --white: #ffffff;
  --ink: #14213d;
  --muted: #667085;
  --line: rgba(17, 31, 77, 0.12);
  --shadow: 0 18px 45px rgba(17, 31, 77, 0.14);
  --radius: 24px;
  --danger: #b42318;
  --success: #12805c;
}

* { box-sizing: border-box; }

html,
body {
  margin: 0;
  max-width: 100%;
  overflow-x: hidden;
  font-family: system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", "Noto Sans Thai", sans-serif;
  color: var(--ink);
  background:
    radial-gradient(circle at 12% 8%, rgba(213, 168, 74, 0.18), transparent 32%),
    linear-gradient(180deg, #f8fbff 0%, #f4f0e8 100%);
}

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

.topbar {
  position: sticky;
  top: 0;
  z-index: 40;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  padding: 14px clamp(16px, 4vw, 44px);
  background: rgba(255, 255, 255, 0.88);
  backdrop-filter: blur(14px);
  border-bottom: 1px solid var(--line);
}

.brand {
  display: flex;
  align-items: center;
  gap: 12px;
  color: var(--navy);
  text-decoration: none;
}

.brand-mark {
  width: 46px;
  height: 46px;
  display: grid;
  place-items: center;
  border-radius: 14px;
  background: var(--navy);
  color: var(--gold-2);
  font-weight: 900;
  letter-spacing: 0.02em;
  box-shadow: 0 10px 25px rgba(17, 31, 77, 0.2);
}

.brand small {
  display: block;
  color: var(--muted);
  margin-top: 2px;
}

.main-nav {
  display: flex;
  align-items: center;
  gap: 10px;
}

.main-nav a {
  color: var(--navy);
  text-decoration: none;
  padding: 9px 12px;
  border-radius: 999px;
  font-weight: 700;
}

.main-nav a:hover { background: rgba(213, 168, 74, 0.16); }

.menu-toggle {
  display: none;
  border: 1px solid var(--line);
  background: var(--white);
  color: var(--navy);
  border-radius: 14px;
  width: 44px;
  height: 44px;
  font-size: 22px;
}

.page-shell {
  width: min(1180px, calc(100% - 28px));
  margin: 0 auto;
  padding: 26px 0 56px;
}

.game-hero {
  display: grid;
  grid-template-columns: minmax(220px, 310px) 1fr;
  align-items: center;
  gap: 24px;
  margin-bottom: 18px;
}

.game-logo-wrap {
  position: relative;
  border-radius: 28px;
  background: rgba(255, 255, 255, 0.75);
  border: 1px solid var(--line);
  box-shadow: var(--shadow);
  padding: 8px;
  overflow: hidden;
  isolation: isolate;
}

.game-logo-wrap::before {
  content: "";
  position: absolute;
  inset: -35% auto -35% -52%;
  width: 38%;
  transform: rotate(18deg);
  background: linear-gradient(90deg, transparent, rgba(255,255,255,.15), rgba(255,255,255,.92), rgba(255,255,255,.18), transparent);
  animation: heroShimmer 4s ease-in-out infinite;
  z-index: 2;
  pointer-events: none;
}

.game-logo-wrap::after {
  content: "";
  position: absolute;
  inset: 0;
  border-radius: inherit;
  box-shadow: inset 0 0 0 1px rgba(255,255,255,.38), 0 0 26px rgba(213,168,74,.12);
  z-index: 3;
  pointer-events: none;
}

.game24-logo { display: block; width: 100%; height: auto; position: relative; z-index: 1; }

.logo-spark {
  position: absolute;
  width: 14px;
  height: 14px;
  border-radius: 50%;
  background: radial-gradient(circle, rgba(255,255,255,1) 0%, rgba(255,247,200,.95) 40%, rgba(255,247,200,0) 75%);
  filter: drop-shadow(0 0 10px rgba(255, 239, 175, 0.95));
  animation: sparkTwinkle 2.8s ease-in-out infinite;
  z-index: 4;
}

.logo-spark::before,
.logo-spark::after {
  content: "";
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
  border-radius: 999px;
  background: linear-gradient(180deg, rgba(255,255,255,.95), rgba(255,240,185,.12));
}
.logo-spark::before { width: 2px; height: 16px; }
.logo-spark::after { width: 16px; height: 2px; }
.sparkle-a { right: 18px; top: 16px; animation-delay: .1s; }
.sparkle-b { left: 22px; top: 54px; width: 11px; height: 11px; animation-delay: 1.2s; }
.sparkle-c { right: 54px; bottom: 18px; width: 9px; height: 9px; animation-delay: 2.1s; }

.eyebrow {
  margin: 0 0 6px;
  color: var(--gold);
  text-transform: uppercase;
  letter-spacing: 0.12em;
  font-size: 0.78rem;
  font-weight: 900;
}

h1,
h2 { margin: 0; color: var(--navy); }

h1 { font-size: clamp(2rem, 5vw, 4.2rem); line-height: 1; }

.hero-copy p:last-child {
  font-size: clamp(1rem, 2vw, 1.25rem);
  margin: 10px 0 0;
  color: var(--muted);
}

.setup-card,
.play-card,
.status-bar,
.leaderboard-card {
  background: rgba(255, 255, 255, 0.92);
  border: 1px solid var(--line);
  box-shadow: var(--shadow);
  border-radius: var(--radius);
}

.setup-card {
  padding: clamp(16px, 3vw, 24px);
  margin-bottom: 16px;
}

.player-panel {
  display: grid;
  grid-template-columns: auto 1fr;
  gap: 16px;
  align-items: center;
  margin-bottom: 18px;
}

.avatar-button {
  width: 88px;
  height: 88px;
  border: 3px solid var(--gold);
  border-radius: 24px;
  padding: 4px;
  background: var(--cream);
  box-shadow: 0 10px 25px rgba(213, 168, 74, 0.22);
}

.avatar-button img,
.status-player img,
.leader-avatar,
.avatar-option img {
  width: 100%;
  height: 100%;
  object-fit: contain;
  display: block;
}

.player-field label,
.control-label {
  display: block;
  color: var(--navy);
  font-weight: 900;
  margin-bottom: 7px;
}

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

#playerName {
  flex: 1;
  min-width: 0;
  padding: 13px 14px;
  border: 2px solid rgba(17, 31, 77, 0.12);
  border-radius: 16px;
  background: var(--white);
  color: var(--navy);
  font-weight: 800;
  outline: none;
}

#playerName:focus {
  border-color: var(--gold);
  box-shadow: 0 0 0 4px rgba(213, 168, 74, 0.18);
}

.player-field small { display: block; color: var(--muted); margin-top: 6px; }

.setup-grid {
  display: grid;
  grid-template-columns: 1.2fr 1.15fr 1.1fr auto;
  gap: 14px;
  align-items: end;
}

.control-group { min-width: 0; }
.muted-group.is-disabled { opacity: 0.52; pointer-events: none; }

.segmented { display: flex; flex-wrap: wrap; gap: 8px; }

.segmented button,
.soft-btn,
.primary-btn,
.mini-btn,
.leaderboard-fab {
  border: 0;
  border-radius: 999px;
  font-weight: 900;
  transition: transform .16s ease, box-shadow .16s ease, background .16s ease, opacity .16s ease;
}

.segmented button {
  padding: 10px 13px;
  background: #eef2ff;
  color: var(--navy);
  border: 1px solid rgba(17, 31, 77, 0.08);
}

.segmented button.active {
  background: var(--navy);
  color: var(--gold-2);
  box-shadow: 0 10px 20px rgba(17, 31, 77, 0.18);
}

.soft-btn {
  padding: 11px 15px;
  background: #fff5dc;
  color: var(--navy);
  border: 1px solid rgba(213, 168, 74, 0.34);
}

.primary-btn {
  padding: 13px 18px;
  background: linear-gradient(135deg, var(--gold), var(--gold-2));
  color: var(--navy);
  box-shadow: 0 12px 22px rgba(213, 168, 74, 0.28);
}

.primary-btn:disabled,
.soft-btn:disabled { cursor: not-allowed; opacity: 0.55; }

.mini-btn {
  padding: 8px 12px;
  background: #eef2ff;
  color: var(--navy);
  border: 1px solid rgba(17, 31, 77, 0.08);
}

.leaderboard-fab {
  position: fixed;
  top: 84px;
  right: 18px;
  z-index: 48;
  padding: 12px 16px;
  background: rgba(17,31,77,.95);
  color: var(--gold-2);
  box-shadow: 0 16px 32px rgba(17,31,77,.24);
}

.status-bar {
  display: flex;
  justify-content: space-between;
  gap: 16px;
  align-items: center;
  padding: 14px 18px;
  margin-bottom: 16px;
}

.status-player { display: flex; align-items: center; gap: 12px; }

.status-player img {
  width: 52px;
  height: 52px;
  border-radius: 18px;
  border: 2px solid var(--gold);
  background: var(--cream);
}

.status-player strong { display: block; color: var(--navy); font-size: 1.1rem; }
.status-player small { color: var(--muted); }

.status-metrics { display: flex; gap: 10px; }

.metric {
  min-width: 104px;
  padding: 10px 12px;
  border-radius: 18px;
  background: #f8fafc;
  border: 1px solid var(--line);
  text-align: center;
}

.metric span { display: block; color: var(--muted); font-size: .82rem; font-weight: 700; }
.metric strong { display: block; color: var(--navy); font-size: 1.35rem; line-height: 1.1; }

.game-layout { display: block; }

.play-card,
.leaderboard-card { padding: clamp(16px, 3vw, 24px); }

.card-head,
.leaderboard-head,
.expression-title-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
}

.card-head { margin-bottom: 18px; }
.section-label { margin: 0 0 10px; color: var(--muted); font-weight: 900; }

.numbers-panel,
.operators-panel,
.expression-panel {
  padding: 16px;
  border: 1px solid var(--line);
  border-radius: 22px;
  background: #fbfdff;
  margin-bottom: 14px;
}

.numbers-grid,
.operators-grid {
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
}

.num-token,
.op-token {
  display: grid;
  place-items: center;
  border: 0;
  font-weight: 1000;
  color: var(--navy);
  box-shadow: 0 10px 20px rgba(17, 31, 77, 0.12);
}

.num-token {
  width: clamp(58px, 10vw, 78px);
  height: clamp(58px, 10vw, 78px);
  border-radius: 22px;
  background: linear-gradient(180deg, var(--white), #eef2ff);
  font-size: clamp(1.45rem, 4vw, 2rem);
  border: 2px solid rgba(17, 31, 77, 0.08);
}

.num-token.used { opacity: 0.32; transform: scale(0.96); }

.op-token {
  min-width: 48px;
  height: 46px;
  padding: 0 15px;
  border-radius: 16px;
  background: linear-gradient(180deg, var(--cream), #ffe8a8);
  font-size: 1.32rem;
  border: 2px solid rgba(213, 168, 74, 0.26);
}

.expression-strip {
  min-height: 66px;
  width: 100%;
  display: flex;
  align-items: center;
  gap: clamp(4px, 1.1vw, 10px);
  padding: 12px 10px;
  overflow: hidden;
  white-space: nowrap;
  border-radius: 18px;
  background: linear-gradient(180deg, #fff, #f8fafc);
  border: 2px dashed rgba(17, 31, 77, 0.13);
}

.expression-strip.empty { justify-content: center; }

.placeholder-line {
  color: rgba(17, 31, 77, 0.34);
  font-weight: 900;
  letter-spacing: .24em;
  font-size: clamp(1.3rem, 5vw, 2rem);
}

.expr-token {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  flex: 0 0 auto;
  min-width: auto;
  padding: 0 2px 3px;
  border: 0;
  background: transparent;
  border-bottom: 3px solid rgba(213, 168, 74, 0.55);
  color: var(--navy);
  font-size: clamp(1.45rem, 4.9vw, 2.35rem);
  line-height: 1.05;
  font-weight: 1000;
}

.expr-token.operator,
.expr-token.paren {
  color: #8a5c00;
  border-bottom-color: rgba(17, 31, 77, 0.22);
}

.expression-strip.compact .expr-token {
  font-size: clamp(1.1rem, 4.4vw, 1.75rem);
  padding-left: 0;
  padding-right: 0;
}
.expression-strip.compact { gap: 3px; }

.live-result-row {
  display: flex;
  justify-content: space-between;
  gap: 12px;
  align-items: center;
  margin-top: 10px;
  color: var(--muted);
}

.live-result-row strong { color: var(--navy); font-size: 1.35rem; }

.feedback {
  padding: 13px 15px;
  border-radius: 18px;
  margin: 10px 0 14px;
  font-weight: 800;
  border: 1px solid var(--line);
}
.feedback.info { background: #eef6ff; color: #1849a9; }
.feedback.correct { background: #ecfdf3; color: var(--success); }
.feedback.wrong { background: #fff1f0; color: var(--danger); }

.actions-row { display: flex; justify-content: flex-end; gap: 10px; }

.leaderboard-drawer {
  position: fixed;
  top: 0;
  right: 0;
  bottom: 0;
  width: min(380px, 100vw);
  z-index: 70;
  padding: 86px 12px 12px;
  transform: translateX(0);
  transition: transform .22s ease;
  pointer-events: auto;
}

.leaderboard-drawer.is-collapsed {
  transform: translateX(calc(100% + 16px));
  pointer-events: none;
}

.leaderboard-card {
  height: 100%;
  overflow: auto;
  background: rgba(255,255,255,.97);
}

.leaderboard-head h2 { font-size: 1.35rem; }
.leaderboard-head small { color: var(--muted); font-weight: 800; }

.final-result {
  padding: 14px;
  border-radius: 18px;
  background: #fff7e6;
  border: 1px solid rgba(213, 168, 74, 0.35);
  margin: 14px 0;
  color: var(--navy);
  font-weight: 900;
}
.final-result.hidden { display: none; }

.leaderboard-list { display: grid; gap: 8px; margin-top: 14px; }

.leader-row {
  display: grid;
  grid-template-columns: 32px 40px 1fr auto;
  align-items: center;
  gap: 9px;
  padding: 9px;
  border-radius: 16px;
  background: #f8fafc;
  border: 1px solid var(--line);
}

.leader-rank { font-weight: 1000; color: var(--gold); text-align: center; }
.leader-avatar { width: 40px; height: 40px; border-radius: 14px; background: var(--cream); }
.leader-name { min-width: 0; font-weight: 900; color: var(--navy); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.leader-score { font-weight: 1000; color: var(--navy); }
.loading-text, .empty-text { color: var(--muted); font-weight: 800; }

.modal.hidden,
.countdown-overlay.hidden,
.result-overlay.hidden { display: none; }

.modal,
.countdown-overlay,
.result-overlay {
  position: fixed;
  inset: 0;
  z-index: 85;
  display: grid;
  place-items: center;
  padding: 20px;
}

.modal-backdrop,
.countdown-backdrop {
  position: absolute;
  inset: 0;
  background: rgba(8, 18, 48, 0.55);
  backdrop-filter: blur(5px);
}

.modal-card {
  position: relative;
  width: min(760px, 100%);
  max-height: min(760px, calc(100dvh - 40px));
  overflow: auto;
  background: var(--white);
  border-radius: 28px;
  padding: 20px;
  box-shadow: 0 25px 70px rgba(0,0,0,.22);
}

.modal-head {
  display: flex;
  justify-content: space-between;
  gap: 12px;
  align-items: center;
  margin-bottom: 14px;
}

.avatar-grid { display: grid; grid-template-columns: repeat(5, 1fr); gap: 12px; }

.avatar-option {
  border: 3px solid transparent;
  border-radius: 22px;
  padding: 4px;
  background: #f6f8ff;
  aspect-ratio: 1 / 1;
}
.avatar-option.active { border-color: var(--gold); box-shadow: 0 0 0 4px rgba(213, 168, 74, 0.18); }

.countdown-stage,
.result-card {
  position: relative;
  width: min(860px, 100%);
  min-height: min(520px, calc(100dvh - 40px));
  overflow: hidden;
  border-radius: 36px;
  background: linear-gradient(180deg, rgba(10,10,18,.9), rgba(17,31,77,.92));
  border: 1px solid rgba(255,255,255,.18);
  box-shadow: 0 28px 80px rgba(0,0,0,.4);
  display: grid;
  place-items: center;
}

.countdown-track {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  object-fit: cover;
  opacity: .34;
  transform: scale(1.03);
}

.countdown-copy,
.result-card {
  text-align: center;
  padding: 24px;
}

.countdown-mode-label {
  margin: 0 0 14px;
  font-weight: 900;
  font-size: clamp(1rem, 2.2vw, 1.35rem);
  letter-spacing: .03em;
  color: rgba(255,255,255,.9);
}

.countdown-number {
  font-size: clamp(5rem, 18vw, 12rem);
  line-height: .9;
  font-weight: 1000;
  color: var(--gold-2);
  text-shadow: 0 0 24px rgba(243,211,123,.38), 0 12px 30px rgba(0,0,0,.42);
  animation: countdownPulse .7s ease;
}

.countdown-number.is-go {
  font-size: clamp(4rem, 12vw, 8rem);
  color: #ffffff;
}

.countdown-subtext {
  margin-top: 10px;
  color: rgba(255,255,255,.92);
  font-size: clamp(1rem, 2.4vw, 1.35rem);
  font-weight: 800;
}

.result-card {
  min-height: auto;
  width: min(520px, 100%);
  background: linear-gradient(180deg, rgba(11, 22, 58, 0.97), rgba(23,43,104,.96));
  color: white;
}

.result-card h2 {
  color: white;
  font-size: clamp(2.4rem, 8vw, 4.4rem);
  margin-bottom: 10px;
}

.result-card p { margin: 0; }
.result-card .eyebrow { color: var(--gold-2); margin-bottom: 10px; }
#timeupSummary { font-size: clamp(1.05rem, 3vw, 1.35rem); color: rgba(255,255,255,.9); }
.result-actions { margin-top: 20px; display: flex; justify-content: center; }

body.mobile-menu-open { overflow: hidden; }

@keyframes heroShimmer {
  0%, 100% { left: -52%; opacity: 0; }
  15% { opacity: 0; }
  28% { opacity: 1; }
  55% { left: 118%; opacity: .95; }
  70% { opacity: 0; }
}

@keyframes sparkTwinkle {
  0%, 100% { transform: scale(.72); opacity: .18; }
  30% { transform: scale(1.12); opacity: 1; }
  45% { transform: scale(.84); opacity: .5; }
  65% { transform: scale(1); opacity: .88; }
}

@keyframes countdownPulse {
  0% { transform: scale(.5); opacity: 0; }
  35% { transform: scale(1.12); opacity: 1; }
  100% { transform: scale(1); opacity: 1; }
}

@media (max-width: 960px) {
  .setup-grid { grid-template-columns: 1fr 1fr; }
}

@media (max-width: 720px) {
  .topbar { padding: 10px 14px; }
  .brand span:last-child { display: none; }
  .menu-toggle { display: grid; place-items: center; }

  .main-nav {
    position: fixed;
    top: 68px;
    left: 14px;
    right: 14px;
    display: none;
    flex-direction: column;
    align-items: stretch;
    padding: 14px;
    border-radius: 22px;
    background: rgba(255,255,255,.97);
    box-shadow: var(--shadow);
    border: 1px solid var(--line);
    max-height: min(72vh, 520px);
    overflow: auto;
  }

  .main-nav.open { display: flex; }

  .leaderboard-fab {
    top: auto;
    bottom: 14px;
    right: 14px;
    padding: 12px 14px;
  }

  .game-hero { grid-template-columns: 1fr; gap: 10px; }
  .game-logo-wrap { width: min(260px, 82vw); margin: 0 auto; }
  .hero-copy { text-align: center; }

  .player-panel {
    grid-template-columns: 1fr;
    justify-items: center;
    text-align: center;
  }

  .avatar-button { width: 76px; height: 76px; }
  .name-row { flex-direction: column; }
  .setup-grid { grid-template-columns: 1fr; }
  .segmented { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); }
  .control-group:first-child .segmented { grid-template-columns: repeat(2, minmax(0, 1fr)); }

  .status-bar { align-items: stretch; flex-direction: column; }
  .status-metrics { display: grid; grid-template-columns: 1fr 1fr; }
  .metric { min-width: 0; }

  .card-head { align-items: flex-start; flex-direction: column; }
  .numbers-grid, .operators-grid { justify-content: center; }

  .num-token {
    width: 58px;
    height: 58px;
    border-radius: 18px;
  }

  .op-token {
    min-width: 42px;
    height: 42px;
    border-radius: 14px;
    padding: 0 10px;
  }

  .expression-strip { min-height: 56px; padding: 10px 8px; }
  .expr-token { font-size: clamp(1.05rem, 5vw, 1.45rem); border-bottom-width: 2px; }
  .actions-row { display: grid; grid-template-columns: 1fr 1fr; }
  .avatar-grid { grid-template-columns: repeat(4, 1fr); gap: 9px; }

  .leaderboard-drawer {
    width: 100vw;
    padding: 72px 10px 88px;
  }

  .countdown-stage,
  .result-card {
    min-height: min(460px, calc(100dvh - 28px));
    border-radius: 28px;
  }
}

@media (max-width: 390px) {
  .page-shell { width: min(100% - 18px, 1180px); }
  .segmented button { padding: 9px 8px; font-size: .92rem; }
  .num-token { width: 52px; height: 52px; }
  .op-token { min-width: 38px; height: 38px; padding: 0 8px; }
  .expression-strip { gap: 2px; }
  .avatar-grid { grid-template-columns: repeat(3, 1fr); }
  .countdown-copy { padding: 18px; }
}
