:root {
  --bg: #0b1220;
  --panel: #131c2e;
  --panel-2: #1b2740;
  --text: #f4f7fb;
  --muted: #93a1b8;
  --line: #2a3755;
  --a: #33b5ff;
  --a-soft: rgba(51, 181, 255, .16);
  --b: #ff8a3d;
  --b-soft: rgba(255, 138, 61, .16);
  --gold: #ffd23f;
  --danger: #ff5a5f;
  --radius: 18px;
  --safe-t: env(safe-area-inset-top, 0px);
  --safe-b: env(safe-area-inset-bottom, 0px);
  --safe-l: env(safe-area-inset-left, 0px);
  --safe-r: env(safe-area-inset-right, 0px);
  color-scheme: dark;
}

* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body {
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "SF Pro Display", "Segoe UI", Roboto, system-ui, sans-serif;
  -webkit-user-select: none;
  user-select: none;
  -webkit-tap-highlight-color: transparent;
  touch-action: manipulation;
  overflow: hidden;
  overscroll-behavior: none;
}
button, select, input { font: inherit; color: inherit; }
button {
  background: var(--panel-2);
  border: 1px solid var(--line);
  border-radius: 12px;
  padding: .6em 1em;
  cursor: pointer;
}
button:active { transform: scale(.97); }
button.primary { background: var(--a); border-color: var(--a); color: #04121f; font-weight: 700; }
button.danger { background: transparent; border-color: var(--danger); color: var(--danger); }
button.big { font-size: 1.4rem; padding: .8em 2em; }
.hidden { display: none !important; }

/* ---------- Board ---------- */
#board {
  height: 100dvh;
  display: grid;
  grid-template-rows: auto 1fr auto;
  gap: 1.2vmin;
  padding: calc(1.2vmin + var(--safe-t)) calc(1.6vmin + var(--safe-r)) calc(1.2vmin + var(--safe-b)) calc(1.6vmin + var(--safe-l));
}

.topbar { display: flex; align-items: center; justify-content: space-between; gap: 1rem; min-height: 6vmin; }
.sets { display: flex; gap: .6rem; flex-wrap: wrap; align-items: center; }
.set-chip {
  display: inline-flex; align-items: baseline; gap: .4em;
  background: var(--panel); border: 1px solid var(--line); border-radius: 999px;
  padding: .3em .9em; font-size: clamp(14px, 2.4vmin, 26px); font-variant-numeric: tabular-nums;
}
.set-chip .n { color: var(--muted); font-size: .8em; }
.set-chip .wa { color: var(--a); font-weight: 700; }
.set-chip .wb { color: var(--b); font-weight: 700; }
.set-chip.current { border-color: var(--muted); }
.set-chip sup { font-size: .6em; color: var(--muted); }
.status {
  font-size: clamp(16px, 3vmin, 34px); font-weight: 800; letter-spacing: .04em; text-transform: uppercase;
  color: var(--gold); white-space: nowrap;
}

.teams { display: grid; grid-template-columns: 1fr 1fr; gap: 1.6vmin; min-height: 0; }
.team {
  position: relative;
  background: var(--panel);
  border: 2px solid var(--line);
  border-radius: var(--radius);
  display: grid;
  grid-template-rows: auto 1fr auto auto;
  align-items: center;
  justify-items: center;
  padding: 2vmin;
  min-height: 0;
  overflow: hidden;
  transition: border-color .2s, box-shadow .2s;
}
#teamA { border-top: 8px solid var(--a); }
#teamB { border-top: 8px solid var(--b); }
.team .name {
  background: none; border: 0; padding: .2em .4em;
  font-size: clamp(20px, 4.6vmin, 56px); font-weight: 700;
  display: inline-flex; align-items: center; gap: .4em; max-width: 100%;
}
.team .name .label { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
#teamA .name { color: var(--a); }
#teamB .name { color: var(--b); }
.serve {
  width: .6em; height: .6em; border-radius: 50%;
  background: radial-gradient(circle at 35% 35%, #f6ff9e, #d4e300 60%, #9aa500);
  box-shadow: 0 0 .4em rgba(212, 227, 0, .7);
  visibility: hidden; flex: none;
}
.team.serving .serve { visibility: visible; }
.points {
  font-size: clamp(80px, 30vmin, 400px);
  font-weight: 800;
  line-height: 1;
  font-variant-numeric: tabular-nums;
  letter-spacing: -.03em;
}
.meta { display: flex; gap: 6vmin; }
.meta > div { display: flex; flex-direction: column; align-items: center; }
.meta .k { color: var(--muted); font-size: clamp(12px, 2vmin, 22px); text-transform: uppercase; letter-spacing: .1em; }
.meta .v { font-size: clamp(32px, 9vmin, 120px); font-weight: 800; font-variant-numeric: tabular-nums; line-height: 1.05; }
.add { margin-top: 1.5vmin; width: 100%; font-size: clamp(16px, 3vmin, 32px); font-weight: 700; padding: .7em; }
#teamA .add { background: var(--a-soft); border-color: var(--a); }
#teamB .add { background: var(--b-soft); border-color: var(--b); }

.team.flash { animation: flash .6s ease-out; }
#teamA.flash { --flash: var(--a); }
#teamB.flash { --flash: var(--b); }
@keyframes flash {
  0% { box-shadow: inset 0 0 0 100vmax color-mix(in srgb, var(--flash) 45%, transparent); }
  100% { box-shadow: inset 0 0 0 100vmax transparent; }
}
body.golden .team { border-color: var(--gold); box-shadow: 0 0 3vmin rgba(255, 210, 63, .35); }
body.golden .points { color: var(--gold); }

.bottombar {
  display: flex; gap: 1vmin; align-items: center; justify-content: space-between;
}
.bottombar button { font-size: clamp(14px, 2.4vmin, 24px); padding: .7em 1.1em; }

/* Clicker indicator: one dot per click in the current burst + a draining bar. */
.clicker { position: relative; display: flex; gap: .5em; padding: .6em 1em; border: 1px dashed var(--line); border-radius: 999px; min-width: 7em; justify-content: center; }
.clicker .dot { width: 1em; height: 1em; border-radius: 50%; background: var(--line); transition: background .1s; }
.clicker[data-n="1"] .dot:nth-child(-n+1),
.clicker[data-n="2"] .dot:nth-child(-n+2),
.clicker[data-n="3"] .dot:nth-child(-n+3) { background: var(--text); }
.clicker[data-n="1"] .dot:nth-child(1) { background: var(--a); }
.clicker[data-n="2"] .dot:nth-child(-n+2) { background: var(--b); }
.clicker[data-n="3"] .dot { background: var(--danger); }
.clicker .bar { position: absolute; left: 10%; bottom: 3px; height: 3px; width: 80%; background: var(--text); border-radius: 2px; transform-origin: left; transform: scaleX(0); }
.clicker.counting .bar { animation: drain var(--win, 500ms) linear forwards; }
@keyframes drain { from { transform: scaleX(1); } to { transform: scaleX(0); } }

.toast {
  position: fixed; left: 50%; bottom: calc(14vmin + var(--safe-b)); transform: translateX(-50%) translateY(20px);
  background: var(--panel-2); border: 1px solid var(--line); border-radius: 999px;
  padding: .7em 1.4em; font-size: clamp(16px, 3vmin, 30px); font-weight: 700;
  opacity: 0; pointer-events: none; transition: opacity .2s, transform .2s; white-space: nowrap;
}
.toast.show { opacity: 1; transform: translateX(-50%) translateY(0); }
.banner {
  position: fixed; left: 50%; top: 50%; transform: translate(-50%, -50%) scale(.9);
  background: var(--gold); color: #1a1400; border-radius: var(--radius);
  padding: .5em 1em; font-size: clamp(28px, 8vmin, 96px); font-weight: 900; text-transform: uppercase;
  opacity: 0; pointer-events: none; transition: opacity .25s, transform .25s; text-align: center;
}
.banner.show { opacity: 1; transform: translate(-50%, -50%) scale(1); }

/* ---------- Overlays and dialogs ---------- */
.overlay {
  position: fixed; inset: 0; background: rgba(5, 9, 18, .86);
  display: grid; place-items: center; padding: 16px; z-index: 10;
}
.card {
  background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius);
  padding: 2rem; max-width: 640px; width: 100%; text-align: center;
}
.card h1 { margin: 0 0 .5em; font-size: clamp(28px, 6vmin, 56px); }
.card p { color: var(--muted); font-size: 1.1rem; line-height: 1.5; }
.card .row { display: flex; gap: 1rem; justify-content: center; flex-wrap: wrap; margin-top: 1rem; }
.big-score { font-size: clamp(28px, 6vmin, 60px) !important; color: var(--text) !important; font-weight: 800; font-variant-numeric: tabular-nums; }
.hint { font-size: .95rem !important; color: var(--muted); }

dialog {
  background: var(--panel); color: var(--text);
  border: 1px solid var(--line); border-radius: var(--radius);
  padding: 0; width: min(760px, calc(100vw - 32px)); max-height: calc(100dvh - 32px);
  -webkit-user-select: text; user-select: text;
}
dialog[open] { display: flex; flex-direction: column; }
dialog::backdrop { background: rgba(5, 9, 18, .8); }
.dlg-head, .dlg-foot { display: flex; justify-content: space-between; align-items: center; padding: 1rem 1.25rem; border-bottom: 1px solid var(--line); }
.dlg-foot { border-bottom: 0; border-top: 1px solid var(--line); }
.dlg-head h2 { margin: 0; }
.dlg-body { overflow: auto; padding: 1rem 1.25rem; -webkit-overflow-scrolling: touch; }

.settings fieldset { border: 1px solid var(--line); border-radius: 12px; margin: 0 0 1rem; padding: .8rem 1rem 1rem; }
.settings legend { color: var(--muted); padding: 0 .4em; }
.settings label { display: flex; justify-content: space-between; align-items: center; gap: 1rem; padding: .5rem 0; flex-wrap: wrap; }
.settings label.switch { justify-content: flex-start; }
.settings input[type=checkbox] { width: 1.4em; height: 1.4em; accent-color: var(--a); }
.settings input:not([type]), .settings select {
  background: var(--panel-2); border: 1px solid var(--line); border-radius: 10px; padding: .55em .8em; min-width: 14em;
}
.settings input[type=range] { width: 100%; accent-color: var(--a); }
.tester { background: var(--bg); border-radius: 10px; padding: .6rem 1rem; margin-top: .5rem; }
.tester p { margin: .4rem 0; }
.lastkey { font-family: ui-monospace, Menlo, monospace; font-size: 1.2rem; color: var(--gold); }

/* History */
.match { border: 1px solid var(--line); border-radius: 12px; margin-bottom: .8rem; overflow: hidden; }
.match > summary { padding: .8rem 1rem; cursor: pointer; display: flex; justify-content: space-between; gap: 1rem; flex-wrap: wrap; background: var(--panel-2); list-style: none; }
.match > summary::-webkit-details-marker { display: none; }
.match .when { color: var(--muted); font-size: .9rem; }
.match .body { padding: .5rem 1rem 1rem; }
.set-head { margin: .8rem 0 .3rem; font-weight: 700; }
.game { display: grid; grid-template-columns: 6.5em 1fr auto; gap: .6rem; align-items: start; padding: .35rem 0; border-top: 1px solid var(--line); font-size: .95rem; }
.game .gn { color: var(--muted); }
.game .seq { display: flex; flex-wrap: wrap; gap: .3rem; }
.pt { font-variant-numeric: tabular-nums; padding: .1em .45em; border-radius: 6px; font-size: .85rem; }
.pt.A { background: var(--a-soft); color: var(--a); }
.pt.B { background: var(--b-soft); color: var(--b); }
.pt.gold { outline: 1px solid var(--gold); }
.game .w.A { color: var(--a); font-weight: 700; }
.game .w.B { color: var(--b); font-weight: 700; }
.empty { color: var(--muted); }

/* ---------- Smaller screens ---------- */
@media (orientation: portrait) and (max-width: 700px) {
  .teams { grid-template-columns: 1fr; }
  .points { font-size: clamp(56px, 15vh, 200px); }
  .add { margin-top: .5vh; padding: .45em; }
  .meta .v { font-size: clamp(28px, 6vh, 64px); }
  .bottombar { flex-wrap: wrap; justify-content: center; }
  .clicker { order: -1; width: 100%; }
}
@media (orientation: portrait) and (min-width: 701px) {
  .points { font-size: clamp(80px, 26vw, 360px); }
}
@media (max-height: 500px) {
  .add { display: none; }
  .meta .v { font-size: clamp(24px, 8vh, 60px); }
}
