:root {
  --bg: #0b0f14;
  --panel: #131a23;
  --panel-2: #1b2530;
  --line: #263442;
  --text: #e6edf3;
  --muted: #8aa0b3;
  --p1: #36c5ff;
  --p2: #ff7a59;
  --accent: #39ff14;
  --danger: #ff4d4d;
  --radius: 14px;
  --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; }
html, body {
  margin: 0; height: 100%;
  background: var(--bg); color: var(--text);
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  overscroll-behavior: none;
}
body { overflow: hidden; }

.screen { display: none; position: absolute; inset: 0; }
.screen.active { display: block; }

/* ---------------- SETUP ---------------- */
#screen-setup { overflow-y: auto; padding: calc(24px + var(--safe-t)) 18px calc(24px + var(--safe-b)); }
.setup-card { max-width: 460px; margin: 0 auto; }
.setup-card h1 { font-size: 34px; line-height: 1.05; margin: 8px 0 4px; }
.setup-card h1 span { color: var(--accent); }
.tagline { color: var(--muted); margin: 0 0 22px; }
.field { margin: 14px 0; }
.field > label { display: block; color: var(--muted); font-size: 13px; margin-bottom: 6px; font-weight: 600; letter-spacing: .02em; }
.field.two { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }
.field.two > label { color: var(--muted); font-size: 13px; display: flex; flex-direction: column; gap: 6px; font-weight: 600; }
input[type=text], input[type=number], select {
  width: 100%; padding: 13px 12px; font-size: 16px;
  background: var(--panel); color: var(--text);
  border: 1px solid var(--line); border-radius: 10px; outline: none;
}
input:focus, select:focus { border-color: var(--p1); }
.segmented { display: flex; gap: 8px; }
.segmented label { flex: 1; }
.segmented input { position: absolute; opacity: 0; pointer-events: none; }
.segmented span {
  display: block; text-align: center; padding: 12px; border-radius: 10px;
  background: var(--panel); border: 1px solid var(--line); font-weight: 600;
}
.segmented input:checked + span { background: var(--panel-2); border-color: var(--p1); color: #fff; }
.hint, .note { color: var(--muted); font-size: 12.5px; line-height: 1.5; }
.primary {
  background: linear-gradient(135deg, #2bd4ff, #36c5ff); color: #04202b;
  border: none; border-radius: 12px; font-weight: 800; cursor: pointer;
}
.primary.big { width: 100%; padding: 16px; font-size: 18px; margin-top: 18px; }

/* ---------------- MATCH ---------------- */
#screen-match { background: #000; }
#cam, #overlay { position: absolute; inset: 0; width: 100%; height: 100%; }
#cam { object-fit: cover; }
#cam.hidden { display: none; }
#overlay { touch-action: manipulation; }
#overlay.calibrating { cursor: crosshair; background: rgba(57,255,20,.06); }

.hud-speed {
  position: absolute; top: calc(10px + var(--safe-t)); left: 12px;
  background: rgba(8,12,16,.62); backdrop-filter: blur(6px);
  border: 1px solid var(--line); border-radius: var(--radius);
  padding: 8px 14px; text-align: center; min-width: 96px;
}
.big-speed { display: flex; align-items: baseline; justify-content: center; gap: 4px; }
.big-speed span { font-size: 40px; font-weight: 800; line-height: 1; font-variant-numeric: tabular-nums; }
.big-speed small { font-size: 12px; color: var(--muted); }
.big-speed small.locked { color: var(--accent); }
.peak { font-size: 12px; color: var(--muted); }
.peak b { color: var(--text); }
.cam-status { font-size: 10px; color: var(--muted); margin-top: 2px; text-transform: uppercase; letter-spacing: .06em; }

#contact-flash {
  position: absolute; top: 40%; left: 50%; transform: translate(-50%,-50%) scale(.6);
  font-size: 44px; font-weight: 900; color: var(--accent);
  text-shadow: 0 2px 24px rgba(57,255,20,.6); opacity: 0; pointer-events: none;
}
#contact-flash.show { animation: pop .7s ease-out; }
@keyframes pop {
  0% { opacity: 0; transform: translate(-50%,-50%) scale(.5); }
  20% { opacity: 1; transform: translate(-50%,-60%) scale(1.1); }
  100% { opacity: 0; transform: translate(-50%,-90%) scale(1); }
}

.hud-score {
  position: absolute; top: calc(10px + var(--safe-t)); right: 12px; left: 120px;
  display: grid; grid-template-columns: 1fr auto 1fr; align-items: start; gap: 8px;
  background: rgba(8,12,16,.62); backdrop-filter: blur(6px);
  border: 1px solid var(--line); border-radius: var(--radius); padding: 8px 12px;
}
.player { text-align: center; }
.player.p1 .pscore { color: var(--p1); }
.player.p2 .pscore { color: var(--p2); }
.pname { font-size: 13px; font-weight: 700; display: flex; align-items: center; gap: 5px; justify-content: center; }
.player.p2 .pname { justify-content: center; }
.pscore { font-size: 46px; font-weight: 900; line-height: 1; font-variant-numeric: tabular-nums; }
.games { font-size: 11px; color: var(--muted); }
.games b { color: var(--text); }
.serve-dot { width: 9px; height: 9px; border-radius: 50%; background: #33414f; display: inline-block; }
.serve-dot.on { background: var(--accent); box-shadow: 0 0 8px var(--accent); }
.middle { text-align: center; font-size: 10.5px; color: var(--muted); display: flex; flex-direction: column; gap: 3px; padding-top: 4px; min-width: 76px; }
.serveno { color: var(--text); }
.deuce { color: #04202b; background: var(--accent); border-radius: 6px; padding: 1px 6px; font-weight: 800; }
.deuce.hidden { display: none; }
.side { opacity: .7; }

.scorebtns {
  position: absolute; left: 12px; right: 12px; bottom: calc(96px + var(--safe-b));
  display: grid; grid-template-columns: 1fr 1fr; gap: 12px;
}
.scorebtn {
  padding: 22px 10px; font-size: 20px; font-weight: 800; border: none; border-radius: 16px;
  color: #04202b; cursor: pointer; backdrop-filter: blur(2px);
}
.scorebtn.p1 { background: var(--p1); }
.scorebtn.p2 { background: var(--p2); }
.scorebtn:active { filter: brightness(.85); transform: translateY(1px); }

.controls {
  position: absolute; left: 12px; right: 12px; bottom: calc(46px + var(--safe-b));
  display: grid; grid-template-columns: repeat(4, 1fr); gap: 8px;
}
.ctl {
  padding: 11px 4px; font-size: 12.5px; font-weight: 700; border-radius: 11px;
  background: rgba(20,28,36,.8); color: var(--text); border: 1px solid var(--line); cursor: pointer;
}
.ctl.danger { color: var(--danger); border-color: #4a2630; }
.ctl:active { background: var(--panel-2); }

.tuning {
  position: absolute; left: 12px; right: 12px; bottom: calc(8px + var(--safe-b));
  background: rgba(8,12,16,.8); border: 1px solid var(--line); border-radius: 11px;
  padding: 0 12px; font-size: 13px;
}
.tuning summary { padding: 8px 0; cursor: pointer; color: var(--muted); list-style: none; }
.tuning[open] { padding-bottom: 12px; }
.tuning label { display: block; margin: 10px 0; color: var(--muted); }
.tuning input[type=range] { width: 100%; }
.tuning small { display: block; color: #5f7384; font-size: 11px; }
.tuning .check { display: flex; align-items: center; gap: 8px; }

/* ---------------- START OVERLAY ---------------- */
.start-overlay {
  position: absolute; inset: 0; z-index: 30; display: flex; align-items: center; justify-content: center;
  background: radial-gradient(120% 90% at 50% 0%, #16202b, #070b10);
  padding: 24px; text-align: center;
}
.start-overlay.hidden { display: none; }
.so-inner { max-width: 380px; }
.so-emoji { font-size: 64px; }
.so-inner h2 { margin: 8px 0; font-size: 26px; }
.so-inner p { color: var(--muted); line-height: 1.55; margin: 6px 0 20px; }
.so-inner p b { color: var(--accent); }
.ghost { background: none; border: 1px solid var(--line); color: var(--muted);
  width: 100%; padding: 12px; border-radius: 12px; margin-top: 10px; font-weight: 600; cursor: pointer; }

/* ---------------- AUTO STATUS BAR ---------------- */
.auto-bar {
  position: absolute; top: calc(96px + var(--safe-t)); left: 12px; right: 12px; z-index: 6;
  display: flex; align-items: center; gap: 8px; flex-wrap: wrap;
  background: rgba(8,12,16,.55); backdrop-filter: blur(6px);
  border: 1px solid var(--line); border-radius: 10px; padding: 6px 10px; font-size: 12.5px;
}
.auto-bar.hidden { display: none; }
.badge-auto { background: var(--accent); color: #04202b; font-weight: 800; padding: 1px 7px; border-radius: 6px; font-size: 11px; }
.badge-auto.manual { background: var(--p2); }
#auto-status { color: var(--text); flex: 1; }
.identity { color: var(--muted); }

/* ---------------- MENU SHEET ---------------- */
.sheet { z-index: 40; background: rgba(4,8,12,.55); display: none; align-items: flex-end; }
.sheet.active { display: flex; }
.sheet-card {
  width: 100%; max-width: 520px; margin: 0 auto;
  background: var(--panel); border: 1px solid var(--line);
  border-radius: 18px 18px 0 0; padding: 18px 16px calc(20px + var(--safe-b));
}
.sheet-card h2 { margin: 4px 0 14px; }
.menu-row { display: flex; align-items: center; justify-content: space-between; gap: 12px; margin: 12px 0 16px; }
.menu-row > span { color: var(--muted); font-weight: 600; }
.ctl.wide { width: 100%; text-align: left; padding: 14px; margin: 8px 0; font-size: 14px; }

/* ---------------- STATS ---------------- */
#screen-stats { overflow-y: auto; padding: calc(20px + var(--safe-t)) 16px calc(20px + var(--safe-b)); }
.stats-card { max-width: 520px; margin: 0 auto; }
.stats-card h2 { margin: 6px 0 16px; }
.trophy { font-size: 26px; font-weight: 900; text-align: center; }
.sub { text-align: center; color: var(--muted); margin: 4px 0 18px; }
.stats-card h3 { margin: 22px 0 8px; font-size: 15px; color: var(--accent); }
table.games { width: 100%; border-collapse: collapse; margin: 6px 0; }
table.games th, table.games td { padding: 8px; text-align: center; border-bottom: 1px solid var(--line); }
table.games th:first-child, table.games td:first-child { text-align: left; color: var(--muted); }
table.games td.win { color: var(--accent); font-weight: 800; }
.row { display: flex; align-items: center; gap: 8px; }
.row > span { min-width: 28px; text-align: center; font-weight: 800; font-variant-numeric: tabular-nums; }
.bar { flex: 1; display: flex; height: 14px; border-radius: 7px; overflow: hidden; background: var(--panel); }
.bar > span:first-child { background: var(--p1); }
.bar > span:last-child { background: var(--p2); }
.statline { margin: 12px 0; }
.statline label { font-size: 12.5px; color: var(--muted); display: block; margin-bottom: 4px; }
.kv { display: flex; justify-content: space-between; padding: 8px 0; border-bottom: 1px solid var(--line); }
.kv span { color: var(--muted); }

/* ---------------- TOAST ---------------- */
.toast {
  position: fixed; left: 50%; bottom: calc(18px + var(--safe-b)); transform: translateX(-50%) translateY(20px);
  background: #0d141c; border: 1px solid var(--line); color: var(--text);
  padding: 11px 16px; border-radius: 12px; font-size: 13.5px; max-width: 86vw; text-align: center;
  opacity: 0; pointer-events: none; transition: opacity .2s, transform .2s; z-index: 50;
}
.toast.show { opacity: 1; transform: translateX(-50%) translateY(0); }
