:root,
:root[data-theme="dark"] {
  --bg:           #111;
  --fg:           #eee;
  --muted:        #aaa;
  --card-bg:      #1c1c1c;
  --bar-bg:       #1c1c1c;
  --border:       #333;
  --accent:       #2a6e2a;
  --accent-hover: #348534;
  --status-bg:    #555;
  --status-conn:  #2a6e2a;
  --status-err:   #8a2a2a;
  --error-fg:     #f88;
  --warn-fg:      #ffb;
  --hint-fg:      #888;
  --won-fg:       #8f8;
  --lost-fg:      #f88;
  --ref-fg:       #ccc;
  --button-secondary-bg: #555;
  --button-secondary-bg-hover: #777;
  --chip-bg: #2a2a2a;
  --chip-bg-hover: #3a3a3a;
}
:root[data-theme="light"] {
  --bg:           #f5f5f5;
  --fg:           #222;
  --muted:        #555;
  --card-bg:      #fff;
  --bar-bg:       #eef0f3;
  --border:       #ccd2d8;
  --accent:       #2a6e2a;
  --accent-hover: #1f5520;
  --status-bg:    #b0b6bd;
  --status-conn:  #2a6e2a;
  --status-err:   #b53939;
  --error-fg:     #b53939;
  --warn-fg:      #826e0a;
  --hint-fg:      #555;
  --won-fg:       #1d6f1d;
  --lost-fg:      #b53939;
  --ref-fg:       #555;
  --button-secondary-bg: #b0b6bd;
  --button-secondary-bg-hover: #98a0aa;
  --chip-bg: #e2e6eb;
  --chip-bg-hover: #cfd5dc;
}

* { box-sizing: border-box; }
body {
  margin: 0;
  font-family: system-ui, sans-serif;
  background: var(--bg);
  color: var(--fg);
}
main {
  max-width: 720px;
  margin: 0 auto;
  padding: 16px;
}
header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 8px;
}
h1 { margin: 0; font-size: 1.4rem; }

.status {
  font-size: 0.85rem;
  padding: 2px 8px;
  border-radius: 4px;
  background: var(--status-bg);
}
.status.connected { background: var(--status-conn); color: #fff; }
.status.error     { background: var(--status-err);  color: #fff; }

#theme-toggle {
  font-size: 0.85rem;
  padding: 2px 8px;
  background: var(--button-secondary-bg);
  color: var(--fg);
  border: none;
  border-radius: 4px;
  cursor: pointer;
}
#theme-toggle:hover { background: var(--button-secondary-bg-hover); }

#hls-video {
  width: 100%;
  background: #000;
  margin: 12px 0;
}
.hls-note { font-size: 0.8rem; color: var(--muted); margin: -8px 0 12px 0; }

#round-panel {
  background: var(--card-bg);
  padding: 12px;
  border-radius: 6px;
}
.round-state, .round-countdown { margin: 4px 0; }
#markets { margin-top: 8px; }
.market {
  padding: 6px 0;
  border-top: 1px solid var(--border);
  font-size: 0.9rem;
}
.market:first-child { border-top: none; }

form { margin: 12px 0; }
form label { display: block; margin: 6px 0; }
form input, form select {
  padding: 4px 6px;
  font-size: 1rem;
  background: var(--card-bg);
  color: var(--fg);
  border: 1px solid var(--border);
  border-radius: 3px;
}
form button {
  padding: 6px 12px;
  font-size: 1rem;
  cursor: pointer;
  background: var(--accent);
  color: white;
  border: none;
  border-radius: 4px;
}
form button:hover:not(:disabled) { background: var(--accent-hover); }
form button:disabled {
  background: var(--button-secondary-bg);
  cursor: not-allowed;
}

.stake-chips {
  display: flex;
  gap: 6px;
  margin: 4px 0 8px 0;
  flex-wrap: wrap;
}
.stake-chip {
  padding: 3px 10px;
  background: var(--chip-bg);
  color: var(--fg);
  border: 1px solid var(--border);
  border-radius: 12px;
  font-size: 0.85rem;
  cursor: pointer;
}
.stake-chip:hover { background: var(--chip-bg-hover); }

.projected-payout {
  font-size: 0.95rem;
  margin: 6px 0;
  color: var(--muted);
}
.projected-payout strong { color: var(--fg); }

.msg { min-height: 1.2rem; color: var(--warn-fg); font-size: 0.9rem; }
.msg.error { color: var(--error-fg); }

#my-bets, #leaderboard {
  list-style: none;
  padding: 0;
  font-size: 0.9rem;
}
#my-bets li, #leaderboard li {
  padding: 4px 0;
  border-top: 1px solid var(--border);
}
#my-bets li.won  { color: var(--won-fg); }
#my-bets li.lost { color: var(--lost-fg); }
#my-bets li.ref  { color: var(--ref-fg); }

#leaderboard li .lb-handle { font-weight: 500; }
#leaderboard li .lb-balance { float: right; }
#leaderboard li .lb-profit-pos { color: var(--won-fg); }
#leaderboard li .lb-profit-neg { color: var(--lost-fg); }

.user-bar {
  font-size: 0.95rem;
  margin-bottom: 8px;
  background: var(--bar-bg);
  padding: 6px 10px;
  border-radius: 4px;
}

.logout-btn {
  margin-left: 12px;
  font-size: 0.85rem;
  padding: 2px 8px;
  background: var(--button-secondary-bg);
  color: var(--fg);
  border: none;
  border-radius: 4px;
  cursor: pointer;
}
.logout-btn:hover { background: var(--button-secondary-bg-hover); }

.hint { font-size: 0.85rem; color: var(--hint-fg); margin: 6px 0 0; }

/* ─── Mobile responsive (≤ 600px) ─── */
@media (max-width: 600px) {
  main { padding: 10px; }

  header { flex-wrap: wrap; }
  header h1 { font-size: 1.2rem; }

  /* Stack form labels above inputs for thumb-friendly tap targets. */
  form label {
    display: block;
    margin: 8px 0;
  }
  form label input,
  form label select {
    display: block;
    width: 100%;
    min-height: 44px;          /* Apple HIG min touch target */
    margin-top: 4px;
    font-size: 1rem;
  }

  form button {
    width: 100%;
    min-height: 44px;
    font-size: 1rem;
    margin-top: 8px;
  }

  /* Stake chips: wider tap target, wrap fluidly. */
  .stake-chip {
    min-height: 36px;
    padding: 8px 14px;
    font-size: 0.95rem;
  }

  /* User bar: keep handle + balance + logout on one line by allowing wrap. */
  .user-bar {
    display: flex;
    align-items: center;
    flex-wrap: wrap;
    gap: 8px;
  }
  .logout-btn { margin-left: auto; }

  /* Leaderboard: ditch float, use flex so balance aligns right reliably. */
  #leaderboard li {
    display: flex;
    justify-content: space-between;
    align-items: baseline;
    gap: 8px;
  }
  #leaderboard li .lb-balance {
    float: none;
    text-align: right;
  }

  /* Round panel + countdown: smaller text to fit narrow screens. */
  .round-state, .round-countdown { font-size: 0.9rem; }
}

/* === Video stage === */

#video-stage {
  position: relative;
  width: 100%;
  aspect-ratio: 16 / 9;
  background: #000;
  border-radius: 8px;
  overflow: hidden;
  margin-bottom: 8px;
}

#hls-video {
  width: 100%;
  height: 100%;
  object-fit: cover;
  display: block;
  margin: 0;
}

#detection-line-overlay {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  pointer-events: none;
  filter: drop-shadow(0 0 4px rgba(0, 0, 0, 0.7));
}

/* === Round status panel (countdown + counter + flash) === */

#round-status {
  position: relative;
  display: grid;
  grid-template-columns: 1fr auto;
  align-items: center;
  gap: 16px;
  padding: 16px 20px;
  margin: 12px 0;
  border-radius: 8px;
  background: rgba(0, 0, 0, 0.04);
  border: 1px solid rgba(0, 0, 0, 0.08);
}

[data-theme="dark"] #round-status {
  background: rgba(255, 255, 255, 0.04);
  border-color: rgba(255, 255, 255, 0.08);
}

.countdown-big {
  display: flex;
  align-items: baseline;
  gap: 10px;
  flex-wrap: wrap;
}

.countdown-label {
  font-size: clamp(11px, 1.6vw, 14px);
  font-weight: 600;
  letter-spacing: 0.06em;
  text-transform: uppercase;
  opacity: 0.85;
  flex-basis: 100%;
  margin-bottom: 2px;
}

.countdown-value {
  font-size: clamp(40px, 9vw, 96px);
  font-weight: 800;
  line-height: 1;
  font-variant-numeric: tabular-nums;
}

.countdown-unit {
  font-size: clamp(14px, 2.4vw, 22px);
  opacity: 0.7;
}

.live-counter {
  display: flex;
  flex-direction: column;
  align-items: flex-end;
  background: rgba(0, 0, 0, 0.08);
  padding: 8px 14px;
  border-radius: 8px;
  text-align: right;
}

[data-theme="dark"] .live-counter {
  background: rgba(255, 255, 255, 0.08);
}

.live-counter-label {
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  opacity: 0.8;
}

.live-counter-value {
  font-size: clamp(28px, 5vw, 48px);
  font-weight: 700;
  line-height: 1;
  font-variant-numeric: tabular-nums;
}

.lock-flash {
  position: absolute;
  inset: 0;
  background: transparent;
  border-radius: 8px;
  pointer-events: none;
}

/* Phase-driven colors on the countdown value */
#round-status[data-phase="betting"]      .countdown-value { color: #16a34a; }
#round-status[data-phase="live"]         .countdown-value { color: #dc2626; }
#round-status[data-phase="settling"]     .countdown-value { color: #6b7280; }
#round-status[data-phase="cancelled"]    .countdown-value { color: #6b7280; }
#round-status[data-phase="idle"]         .countdown-value { color: #6b7280; }

[data-theme="dark"] #round-status[data-phase="betting"]   .countdown-value { color: #4ade80; }
[data-theme="dark"] #round-status[data-phase="live"]      .countdown-value { color: #f87171; }
[data-theme="dark"] #round-status[data-phase="settling"]  .countdown-value { color: #94a3b8; }
[data-theme="dark"] #round-status[data-phase="cancelled"] .countdown-value { color: #94a3b8; }
[data-theme="dark"] #round-status[data-phase="idle"]      .countdown-value { color: #94a3b8; }

#round-status[data-phase="locked-flash"] .lock-flash {
  background: rgba(239, 68, 68, 0.55);
  animation: lock-flash-pulse 700ms ease-out;
}

@keyframes lock-flash-pulse {
  0%   { background: rgba(239, 68, 68, 0.7); }
  100% { background: rgba(239, 68, 68, 0.0); }
}
