/* ---------- base ---------- */

:root {
  --felt: #0e6b3b;
  --felt-dark: #073d21;
  --cushion: #4a2a14;
  --text: #f4f1e6;
  --muted: #b9cbbd;
  --gold: #f2c14e;
  --panel: rgba(0, 0, 0, 0.25);
  --red: #c8102e;
  --yellow: #f5d000;
  --green: #1f8f3a;
  --brown: #7b4a1e;
  --blue: #1d4ed8;
  --pink: #f08fb0;
  --black: #111;
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
/* `hidden` must win over display rules like .btn's flex */
[hidden] { display: none !important; }

html, body {
  margin: 0;
  min-height: 100%;
  color: var(--text);
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  background: radial-gradient(ellipse at center, var(--felt) 0%, var(--felt-dark) 100%) fixed;
  background-color: var(--felt-dark);
}

body { border: 10px solid var(--cushion); min-height: 100vh; min-height: 100dvh; }

a { color: inherit; }

/* Every page keeps clear of notches and the home indicator (installed on a phone's home screen). */
#app {
  max-width: 900px; margin: 0 auto;
  padding: calc(12px + env(safe-area-inset-top)) calc(16px + env(safe-area-inset-right))
    calc(24px + env(safe-area-inset-bottom)) calc(16px + env(safe-area-inset-left));
}
#app:focus { outline: none; }

h1, h2 { margin: 0.4em 0; font-weight: 600; }
h2 { font-size: 1.1rem; }
.muted { color: var(--muted); }
.center { text-align: center; }
.win { color: var(--gold); font-weight: 700; }

/* ---------- controls ---------- */

.btn {
  display: flex; align-items: center; justify-content: center;
  padding: 12px 16px;
  border: 1px solid rgba(255, 255, 255, 0.25);
  border-radius: 12px;
  background: var(--panel);
  color: var(--text);
  font-size: 1rem; font-weight: 600;
  text-decoration: none;
  cursor: pointer;
  user-select: none;
  touch-action: manipulation;
}
.btn:active { transform: scale(0.97); }
.btn:disabled { opacity: 0.35; }
.btn.big { padding: 18px; font-size: 1.15rem; width: 100%; }
.btn.primary { background: var(--gold); color: #2b1d00; border-color: var(--gold); }
.btn.danger { background: #8b1a1a; border-color: #b33; }
/* White, so the Foul button stands out and isn't mistaken for the red ball */
.btn.foul { background: #fff; color: #222; border-color: #fff; }
.btn.accent { background: #1d5fa8; border-color: #3b82f6; }
.icon { background: none; border: none; color: #f99; font-size: 1.1rem; cursor: pointer; min-width: 44px; min-height: 44px; }

input, select {
  width: 100%;
  padding: 12px;
  font-size: 1rem;
  border-radius: 10px;
  border: 1px solid rgba(255, 255, 255, 0.3);
  background: rgba(0, 0, 0, 0.3);
  color: var(--text);
}
select option { color: #000; }
label, .label { display: flex; flex-direction: column; gap: 6px; font-weight: 600; }

/* Segmented choice (the reds; the statistics filter) */
.seg { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; }
.seg button {
  padding: 12px 4px; border-radius: 10px; border: 1px solid rgba(255, 255, 255, 0.25);
  background: var(--panel); color: var(--text); font-weight: 600; font-size: 0.95rem;
}
.seg button.on { background: var(--gold); color: #2b1d00; }
.handicap { margin: 0; }
.handicap select:first-child { flex: 2; }
.handicap select:last-child { flex: 1; }
.handicap-note { margin: 4px 0; }

#toasts {
  position: fixed; left: 50%; bottom: calc(24px + env(safe-area-inset-bottom)); transform: translateX(-50%);
  display: flex; flex-direction: column; align-items: center; gap: 8px; z-index: 100; pointer-events: none;
}
.toast { background: rgba(0, 0, 0, 0.85); padding: 12px 18px; border-radius: 10px; }

/* ---------- layout ---------- */

.row { display: flex; gap: 10px; align-items: center; margin: 12px 0; }
.row.between { justify-content: space-between; }
.stack { display: flex; flex-direction: column; gap: 16px; }

.bar { display: flex; align-items: center; gap: 12px; }
.bar h1 { font-size: 1.2rem; flex: 1; }
.back { text-decoration: none; font-size: 1.6rem; min-width: 44px; min-height: 44px; display: inline-flex; align-items: center; justify-content: center; }

/* ---------- lists & tables ---------- */

.card {
  display: flex; flex-direction: column; gap: 4px;
  padding: 14px 16px; margin: 8px 0;
  border-radius: 12px; background: var(--panel);
  text-decoration: none;
}
.card b { color: var(--gold); margin: 0 6px; }
.card small { color: var(--muted); }
.tag { font-style: normal; text-transform: uppercase; font-size: 0.75em; }
.tag.live { color: var(--gold); }
.tag.abandoned { color: #f99; }

table.stats { width: 100%; border-collapse: collapse; margin: 12px 0; background: var(--panel); border-radius: 12px; overflow: hidden; }
.stats th, .stats td { padding: 10px 8px; text-align: center; border-bottom: 1px solid rgba(255, 255, 255, 0.1); }
/* Tables clip to keep their rounded corners, so they must fit narrow phones: long names and headers
   may wrap anywhere, and the padding shrinks. */
.stats th, .stats td { overflow-wrap: anywhere; }
@media (max-width: 420px) {
  .stats th, .stats td { padding: 8px 4px; }
}
.stats th:first-child, .stats td:first-child { text-align: left; }
.stats tfoot th { color: var(--gold); }
.stats a { text-decoration: none; font-weight: 600; }
tr.link { cursor: pointer; }
tr.link:hover { background: rgba(255, 255, 255, 0.05); }

.result { display: grid; grid-template-columns: 1fr auto 1fr; align-items: center; text-align: center; gap: 8px; font-size: 1.2rem; margin-top: 16px; }
.big-score { font-size: 3rem; font-weight: 800; font-variant-numeric: tabular-nums; }

/* ---------- balls ---------- */

.dot { display: inline-block; width: 14px; height: 14px; border-radius: 50%; box-shadow: inset -2px -2px 3px rgba(0,0,0,.4); }
.dot.red, .ball.red { background: var(--red); }
.dot.yellow, .ball.yellow { background: var(--yellow); }
.dot.green, .ball.green { background: var(--green); }
.dot.brown, .ball.brown { background: var(--brown); }
.dot.blue, .ball.blue { background: var(--blue); }
.dot.pink, .ball.pink { background: var(--pink); }
.dot.black, .ball.black { background: var(--black); }

.ball {
  aspect-ratio: 1; width: 100%; max-width: 120px; justify-self: center;
  border-radius: 50%; border: none; cursor: pointer;
  box-shadow: inset -6px -8px 12px rgba(0, 0, 0, 0.45), inset 4px 5px 8px rgba(255, 255, 255, 0.35), 0 4px 8px rgba(0, 0, 0, 0.4);
  color: #fff; font-weight: 800; font-size: clamp(0.9rem, 4vw, 1.5rem);
  text-shadow: 0 1px 2px rgba(0, 0, 0, 0.8);
  touch-action: manipulation;
  transition: transform 0.08s, opacity 0.2s;
}
.ball:active:not(:disabled) { transform: scale(0.9); }
.ball:disabled { opacity: 0.18; cursor: default; box-shadow: none; }
.ball.yellow span, .ball.pink span { color: #222; text-shadow: none; }
/* Extra red potted in the same shot */
.ball.red.extra { opacity: 0.75; outline: 2px dashed rgba(255, 255, 255, 0.7); outline-offset: 3px; }
.ball.red.extra span::before { content: "+"; }

/* ---------- menu ---------- */

.menu { display: flex; flex-direction: column; gap: 14px; max-width: 420px; margin: 5vh auto 0; }
.menu h2 { font-size: 1rem; color: var(--muted); margin-bottom: 0; }
.title { text-align: center; font-size: 1.8rem; }
.logo { display: flex; justify-content: center; gap: 6px; }
.logo .dot { width: 22px; height: 22px; }

/* ---------- scoreboard ---------- */

/* The scoreboard fills the screen height; the player panels take the room that's left. */
.board { display: flex; flex-direction: column; gap: 10px; }
#app.board { min-height: calc(100vh - 20px); min-height: calc(100dvh - 20px); }
/* Equal side columns (← on the left, the connection dot on the right), so the title is centred on the
   screen, not just between the two. */
.board .bar { display: grid; grid-template-columns: 1fr auto 1fr; }
.board .bar h1 { text-align: center; font-size: 1rem; color: var(--muted); }
.board .bar .back { justify-self: start; } /* not stretched: only the arrow itself goes back */
.board .bar .board-tools { justify-self: end; }
.board-tools { display: flex; align-items: center; gap: 10px; }
.fullscreen-toggle {
  width: 44px; height: 44px; display: grid; place-items: center;
  border: 1px solid rgba(255, 255, 255, 0.25); border-radius: 8px;
  background: var(--panel); color: var(--text); font-size: 1.35rem; line-height: 1;
  cursor: pointer; touch-action: manipulation;
}
.fullscreen-toggle:active { transform: scale(0.95); }
.conn { width: 12px; height: 12px; border-radius: 50%; background: #4ade80; }
body.offline .conn { background: #ef4444; }

/* Frames won, one number above each player's panel, "frames" centred between them */
.frames-score {
  display: grid; grid-template-columns: 1fr auto 1fr; align-items: center; text-align: center;
  font-variant-numeric: tabular-nums;
}
.frames-score b { color: var(--gold); font-size: clamp(2.5rem, 10vw, 4.2rem); font-weight: 800; line-height: 1; }
.frames-score span { color: var(--muted); font-size: 1.15rem; text-transform: uppercase; letter-spacing: 0.08em; }

/* On phones the panels keep their size and the middle (break, messages) takes the spare height, so a
   growing break fills that space instead of pushing the pot buttons down. Tablets: see below. */
.players { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.middle { flex: 1 0 auto; display: flex; flex-direction: column; gap: 10px; }
.player {
  display: flex; flex-direction: column; gap: 4px;
  container-type: inline-size; /* the score is sized from the panel's width (cqi units) */
  /* As large as the panel allows (cqi = % of the panel's width), and at most 22% of the screen height
     so tablets don't get oversized digits. Three characters (147, -12) step down so they still fit. */
  --score: clamp(3rem, min(70cqi, 22vh), 16rem);
  --score-long: clamp(3rem, min(50cqi, 22vh), 16rem);
  --marker: clamp(16px, 4.5vw, 28px);
  padding: 10px 8px 6px;
  border-radius: 16px;
  background: var(--panel);
  text-align: center;
  border: 3px solid transparent;
  transition: border-color 0.2s, background 0.2s;
}
.player.active { border-color: var(--gold); background: rgba(242, 193, 78, 0.12); }
/* The player not at the table: tap to pass the turn to them */
.player[data-act] { cursor: pointer; user-select: none; touch-action: manipulation; }
.player[data-act]:active { transform: scale(0.98); background: rgba(255, 255, 255, 0.08); }
/* A gold ball marks the player at the table, centred between the panel's top and the name (the score
   has the same space below it); the other panel keeps the space, so the names stay level. */
.player::before {
  content: ""; align-self: center; flex: none; margin-block: auto;
  width: var(--marker); aspect-ratio: 1; border-radius: 50%;
  background: var(--gold);
  box-shadow: inset -3px -4px 6px rgba(0, 0, 0, 0.4), inset 2px 3px 4px rgba(255, 255, 255, 0.45), 0 2px 4px rgba(0, 0, 0, 0.35);
  visibility: hidden;
}
.player.active::before { visibility: visible; }
.player .name { font-size: clamp(1rem, 4.5vw, 1.6rem); font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
/* The score's box keeps the same height whatever the digits, so nothing moves as the score changes. */
.player .score {
  font-size: var(--score); height: var(--score); margin-bottom: auto;
  display: flex; align-items: center; justify-content: center;
  font-weight: 800; line-height: 1; font-variant-numeric: tabular-nums;
}
.player .score.long { font-size: var(--score-long); }
.player .hcp { color: var(--muted); font-size: 0.85rem; }

.info { display: flex; flex-wrap: wrap; justify-content: center; gap: 6px 18px; color: var(--muted); }
.info b { color: var(--text); }
.info .warn { color: var(--gold); font-weight: 600; }
/* Balls potted in the current visit, with the break total right under them */
.current-break { display: flex; flex-direction: column; gap: 2px; }
/* Phone size at least, larger on tablets. Capped by the height too, so a (wide but short) landscape
   phone keeps the phone size. The empty row keeps one ball's height, so the first pot doesn't move things. */
.breakballs {
  --ball: clamp(20px, min(4vw, 4.5vh), 36px);
  display: flex; flex-wrap: wrap; justify-content: center; min-height: var(--ball);
  gap: max(5px, calc(var(--ball) / 5)); /* spacing grows with the balls */
}
.breakballs .dot { width: var(--ball); height: var(--ball); }
.break-now {
  text-align: center; color: var(--gold); font-weight: 800; line-height: 1.2; min-height: 1.2em;
  font-size: clamp(1.6rem, min(4.5vw, 5vh), 2.6rem);
}
/* Messages about the last action; one line is reserved so the balls below don't jump */
.event { text-align: center; color: var(--gold); min-height: 1.3em; font-weight: 600; }
.msg-balls { display: inline-flex; flex-wrap: wrap; gap: 3px; vertical-align: middle; margin-left: 4px; }
.msg-balls .dot { width: 12px; height: 12px; }

/* The balls fill the row: on phones slightly narrower gaps leave each ball a pixel more. */
.balls { display: grid; grid-template-columns: repeat(7, 1fr); gap: 7px; padding: 10px 3px; }
/* Narrow phones: tighter gaps and margins keep the balls about 42 px wide, a comfortable tap target. */
@media (max-width: 400px) {
  #app.board { padding-left: calc(8px + env(safe-area-inset-left)); padding-right: calc(8px + env(safe-area-inset-right)); }
  .balls { gap: 5px; padding: 10px 0; }
}

.actions { display: flex; gap: 8px; justify-content: center; flex-wrap: wrap; }
.actions:empty { display: none; }
.actions.main { display: grid; grid-template-columns: 1fr 1fr 1.4fr auto; }
.actions .btn { padding: 16px 12px; }

/* Tablets (not landscape phones): the panels take the spare height, and everything a step larger. */
@media (min-width: 700px) and (min-height: 600px) {
  .players { flex: 1 0 auto; }
  .middle { flex: none; }
  .player {
    --marker: clamp(28px, 4.5vw, 40px);
    --score: clamp(3rem, min(72cqi, 26vh), 16rem);
    --score-long: clamp(3rem, min(52cqi, 26vh), 16rem);
  }
  .player .name { font-size: clamp(1.8rem, 4.2vw, 2.8rem); }
  /* The spare height in four equal parts: above the ball, ball–name, name–score, below the score. */
  .player .score { margin-top: auto; }
  .frames-score b { font-size: clamp(3rem, 10vw, 5rem); }
  .frames-score span { font-size: 1.7rem; }
  .info { font-size: 1.75rem; gap: 8px 28px; }
  .breakballs { --ball: clamp(28px, min(5vw, 5.5vh), 46px); }
  .break-now { font-size: clamp(2rem, min(5.5vw, 6vh), 3.2rem); }
  .actions .btn { padding: 22px 12px; }
  .balls { gap: 8px; padding: 10px 4px; }
  .actions.main { grid-template-columns: 1fr 1fr 1.3fr minmax(96px, 0.6fr); } /* a wider ⋯ */
}

/* Dialogs over the scoreboard */
.overlay {
  position: fixed; inset: 0; z-index: 50;
  background: rgba(0, 0, 0, 0.6);
  display: flex; align-items: center; justify-content: center; padding: 16px;
}
.sheet {
  width: 100%; max-width: 420px;
  display: flex; flex-direction: column; gap: 12px;
  padding: 20px; border-radius: 18px; text-align: center;
  background: var(--felt-dark); border: 4px solid var(--cushion);
}
.sheet .row { margin: 0; }
.stepper { display: flex; align-items: center; gap: 14px; font-size: 1.3rem; }
.stepper button {
  width: 44px; height: 44px; border-radius: 50%; font-size: 1.4rem;
  border: 1px solid rgba(255, 255, 255, 0.3); background: var(--panel); color: var(--text);
}
.stepper button:disabled { opacity: 0.35; }
.check input { width: 28px; height: 28px; }
/* A checkbox with its explanation beside it */
label.option { flex-direction: row; align-items: center; gap: 12px; }
label.option input { width: 26px; height: 26px; flex: none; }
label.option small { display: block; font-weight: 400; margin-top: 2px; }
label.option:has(input:disabled) { opacity: 0.6; }
.user-form { gap: 12px; margin: 12px 0; }
.nowrap { white-space: nowrap; }
.view-only { text-align: center; padding: 14px; color: var(--muted); }
.foul-values { display: grid; grid-template-columns: repeat(4, 1fr); gap: 12px; }

/* ---------- match details ---------- */

/* Collapsible rows: frame details, and users in Manage users */
details.frame, details.fold { background: var(--panel); border-radius: 12px; margin: 8px 0; padding: 0 14px; }
details.frame summary, details.fold summary {
  display: flex; gap: 12px; align-items: center; padding: 14px 0; cursor: pointer; list-style: none;
}
details.frame summary::-webkit-details-marker, details.fold summary::-webkit-details-marker { display: none; }
details.frame summary::before, details.fold summary::before { content: "▸"; color: var(--gold); transition: transform 0.15s; }
details.frame[open] summary::before, details.fold[open] summary::before { transform: rotate(90deg); }
/* min-width: 0 lets a long username shorten with "…" instead of pushing the role off the row */
details.fold .fold-title { flex: 1; min-width: 0; font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
details.fold summary .tag { flex: none; }
details.fold .user-form { margin: 0 0 14px; }
details.frame summary b { flex: 1; text-align: center; color: var(--gold); }
.pp-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; margin-bottom: 8px; }
.pp { display: flex; flex-direction: column; gap: 4px; }
.pp small { color: var(--muted); }
.timeline { list-style: none; margin: 0 0 14px; padding: 0; }
.timeline li {
  display: flex; flex-wrap: wrap; align-items: center; gap: 4px 10px;
  padding: 8px 0 8px 10px; border-top: 1px solid rgba(255, 255, 255, 0.08);
  border-left: 3px solid transparent;
}
.timeline li.p0 { border-left-color: var(--gold); }
.timeline li.p1 { border-left-color: #7dd3fc; }
.timeline .t { color: var(--muted); font-variant-numeric: tabular-nums; min-width: 3.2em; font-size: 0.85rem; }
.timeline .who { font-weight: 600; }
.timeline .foul { color: #fca5a5; }
.timeline .adjust { color: var(--muted); font-style: italic; }
.balls-mini { display: flex; flex-wrap: wrap; gap: 3px; flex-basis: 100%; padding-left: calc(3.2em + 10px); }
.balls-mini .dot { width: 10px; height: 10px; }

/* ---------- player stats ---------- */

.tiles { display: grid; grid-template-columns: repeat(auto-fill, minmax(130px, 1fr)); gap: 10px; margin: 12px 0; }
.tile { background: var(--panel); border-radius: 12px; padding: 12px; text-align: center; }
.tile .v { font-size: 1.6rem; font-weight: 800; color: var(--gold); font-variant-numeric: tabular-nums; }
.tile .l { font-weight: 600; }
.tile small { display: block; color: var(--muted); margin-top: 2px; }
.form { display: flex; gap: 6px; flex-wrap: wrap; }
.chip {
  display: inline-flex; align-items: center; justify-content: center;
  width: 26px; height: 26px; border-radius: 6px; font-weight: 800; font-size: 0.85rem;
}
.chip.W { background: #15803d; }
.chip.L { background: #9f1239; }
.chip.D { background: #57534e; }
.top-break b { font-size: 1.3rem; margin: 0 6px 0 0; }
.balls-row { display: flex; flex-wrap: wrap; gap: 3px; margin-top: 4px; }
.balls-row .dot { width: 12px; height: 12px; }
#reds-filter { margin: 8px 0 4px; }
#vs { margin-bottom: 8px; }
.h2h td, .h2h th { width: 33%; }
.h2h td:first-child, .h2h th:first-child { text-align: right; }
.h2h td:nth-child(2) { font-size: 0.85rem; }
.h2h td:first-child, .h2h td:last-child { font-weight: 700; font-size: 1.1rem; }

/* ---------- landscape phones: shrink the scores and balls to fit the height ---------- */

@media (orientation: landscape) and (max-height: 600px) {
  #app.board { max-width: none; }
  .player { --score: clamp(3rem, min(70cqi, 18vh), 7rem); --score-long: clamp(3rem, min(50cqi, 18vh), 7rem); }
  .balls { gap: 6px; padding: 4px; }
  .ball { max-width: 64px; }
  /* Height is what's short here: the frames score and the ball marking the player at the table
     scale with it rather than with the (wide) screen width. */
  .frames-score b { font-size: clamp(1.8rem, 9vh, 3rem); }
  .player { --marker: clamp(12px, 4vh, 20px); }
}

/* ---------- tables and the camera device ---------- */

.btn.small { padding: 8px 12px; font-size: 0.9rem; }
label.option.sub { margin-left: 38px; }
.table-row { display: flex; flex-wrap: wrap; align-items: center; gap: 4px 8px; padding: 6px 0; border-top: 1px solid rgba(255, 255, 255, 0.1); }
.table-row .name { flex: 1; min-width: 120px; font-weight: 600; }
.pairing { flex-basis: 100%; display: flex; flex-wrap: wrap; align-items: center; gap: 8px; padding-bottom: 6px; }
.pairing small { color: var(--muted); flex: 1; min-width: 160px; }
.picker { flex-basis: 100%; padding: 10px; border-radius: 10px; background: var(--panel); }
.stream-line { margin: 6px 0 12px; display: flex; flex-wrap: wrap; justify-content: center; align-items: center; gap: 10px; }

/* The paired tablet's status: bottom left on every page (toasts are bottom centre) */
.camera-chip {
  position: fixed; left: calc(12px + env(safe-area-inset-left)); bottom: calc(12px + env(safe-area-inset-bottom)); z-index: 40;
  display: flex; align-items: center; gap: 8px; padding: 8px 12px; border-radius: 999px; border: none;
  background: rgba(0, 0, 0, 0.75); color: var(--text); font: 600 0.85rem system-ui, sans-serif; cursor: pointer;
}
.camera-chip::before { content: ""; width: 10px; height: 10px; border-radius: 50%; background: #9ca3af; flex: none; }
.camera-chip.ready::before { background: #4ade80; }
.camera-chip.live::before { background: #ef4444; animation: camera-blink 1s infinite; }
.camera-chip.reconnecting::before { background: #f59e0b; }
@keyframes camera-blink { 50% { opacity: 0.3; } }
/* On the scoreboard only the dot shows, tucked into the bottom margin, so no control is covered. */
body:has(#app.board) .camera-chip { font-size: 0; gap: 0; padding: 6px; bottom: calc(4px + env(safe-area-inset-bottom)); }
