/* ------------------------------------------------------------------ *
 * Tennis match stats — styles
 *
 * Built for a phone held one-handed, outdoors, while watching a match.
 * That drives three things: very large tap targets in the bottom half of
 * the screen, a high-contrast "sunlight" theme, and layouts that use dvh
 * so the mobile browser's disappearing toolbar never clips the buttons.
 * ------------------------------------------------------------------ */

:root {
  --bg: #0e1116;
  --surface: #171c24;
  --surface-2: #1f2630;
  --line: #2b333f;
  --text: #f3f6fa;
  --muted: #98a4b6;
  --good: #16a34a;
  --good-soft: #123524;
  --good-line: #1d7a42;
  --bad: #be123c;
  --bad-soft: #34121d;
  --bad-line: #8f1436;
  --accent: #3b82f6;
  --warn: #f59e0b;
  --radius: 14px;
  --btn-h: clamp(50px, 8.6dvh, 76px);
  --shadow: 0 2px 10px rgba(0, 0, 0, .35);
  --fw-btn: 700;
  color-scheme: dark;
}

[data-theme="light"] {
  --bg: #f4f6f9;
  --surface: #ffffff;
  --surface-2: #eef1f6;
  --line: #d5dbe4;
  --text: #10151c;
  --muted: #5c6878;
  --good: #15803d;
  --good-soft: #dcfce7;
  --good-line: #86cfa4;
  --bad: #be123c;
  --bad-soft: #ffe4ea;
  --bad-line: #f0a3b6;
  --shadow: 0 2px 10px rgba(16, 21, 28, .12);
  color-scheme: light;
}

/* Bright sunlight: maximum contrast, heavier type, hard borders. */
[data-theme="sun"] {
  --bg: #ffffff;
  --surface: #ffffff;
  --surface-2: #ffffff;
  --line: #000000;
  --text: #000000;
  --muted: #333333;
  --good: #006b1f;
  --good-soft: #ffffff;
  --good-line: #006b1f;
  --bad: #a30021;
  --bad-soft: #ffffff;
  --bad-line: #a30021;
  --accent: #0032a0;
  --shadow: none;
  --fw-btn: 900;
  color-scheme: light;
}
[data-theme="sun"] .btn,
[data-theme="sun"] .chip,
[data-theme="sun"] .card { border-width: 3px; }
[data-theme="sun"] body { font-weight: 600; }

* { box-sizing: border-box; }

html, body {
  margin: 0;
  height: 100%;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, system-ui, sans-serif;
  -webkit-text-size-adjust: 100%;
}

body {
  /* Nothing here should ever bounce, drag-select or long-press-magnify. */
  overscroll-behavior: none;
  -webkit-user-select: none;
  user-select: none;
  -webkit-tap-highlight-color: transparent;
}

#app {
  height: 100dvh;
  display: flex;
  flex-direction: column;
  padding-bottom: env(safe-area-inset-bottom);
}

/* ---------------- generic bits ---------------- */

.btn {
  appearance: none;
  border: 2px solid var(--line);
  background: var(--surface-2);
  color: var(--text);
  border-radius: var(--radius);
  font: inherit;
  font-weight: var(--fw-btn);
  padding: 12px 14px;
  min-height: 48px;
  cursor: pointer;
  touch-action: manipulation;
  transition: transform .06s ease, filter .12s ease;
}
.btn:active { transform: scale(.96); filter: brightness(1.25); }
.btn:disabled { opacity: .4; }
.btn-primary { background: var(--accent); border-color: var(--accent); color: #fff; }
.btn-good { background: var(--good-soft); border-color: var(--good-line); color: var(--text); }
.btn-bad { background: var(--bad-soft); border-color: var(--bad-line); color: var(--text); }
.btn-ghost { background: transparent; }
.btn-block { width: 100%; }

.card {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 14px;
  box-shadow: var(--shadow);
}

.muted { color: var(--muted); }
.tiny { font-size: 12px; }
.small { font-size: 14px; }
.center { text-align: center; }
.row { display: flex; gap: 10px; align-items: center; }
.row-between { display: flex; gap: 10px; align-items: center; justify-content: space-between; }
.grow { flex: 1; min-width: 0; }
.stack { display: flex; flex-direction: column; gap: 12px; }
.nowrap { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.hidden { display: none !important; }

h1 { font-size: 22px; margin: 0; }
h2 { font-size: 17px; margin: 0 0 8px; }

label.field { display: block; font-size: 13px; color: var(--muted); margin-bottom: 6px; font-weight: 600; }
input[type=text], input[type=date], input[type=number], input[type=password], select, textarea {
  width: 100%;
  font: inherit;
  padding: 12px;
  border-radius: 10px;
  border: 2px solid var(--line);
  background: var(--surface-2);
  color: var(--text);
  -webkit-user-select: text;
  user-select: text;
}
input:focus, select:focus, textarea:focus { outline: 2px solid var(--accent); outline-offset: 1px; }

/* ---------------- scrolling page shell ---------------- */

.page { flex: 1; overflow-y: auto; -webkit-overflow-scrolling: touch; padding: 14px; }
.page-head {
  display: flex; align-items: center; gap: 10px;
  padding: 12px 14px; border-bottom: 1px solid var(--line); background: var(--surface);
  padding-top: calc(12px + env(safe-area-inset-top));
}
.page-head h1 { flex: 1; font-size: 18px; }

/* ---------------- live tracking ---------------- */

.live { display: flex; flex-direction: column; height: 100%; }

.score-bar {
  background: var(--surface);
  border-bottom: 1px solid var(--line);
  padding: 8px 12px calc(8px);
  padding-top: calc(8px + env(safe-area-inset-top));
}
.score-line { display: flex; align-items: baseline; gap: 10px; }
.score-sets { font-size: 13px; color: var(--muted); font-variant-numeric: tabular-nums; }
.score-games {
  font-size: clamp(26px, 8vw, 38px); font-weight: 800; letter-spacing: -.5px;
  font-variant-numeric: tabular-nums;
}
.score-points {
  margin-left: auto; font-size: clamp(26px, 8vw, 38px); font-weight: 800;
  font-variant-numeric: tabular-nums;
}
.serve-dot { font-size: 12px; color: var(--muted); display: flex; align-items: center; gap: 5px; }
.serve-dot b { color: var(--good); font-size: 16px; }

.pressure {
  margin-top: 6px; padding: 5px 10px; border-radius: 999px; font-size: 13px;
  font-weight: 800; letter-spacing: .6px; text-align: center;
}
.pressure.good { background: var(--good); color: #fff; }
.pressure.bad { background: var(--bad); color: #fff; }

.recent {
  display: flex; flex-direction: column; gap: 2px;
  padding: 6px 12px; min-height: 46px;
  font-size: 12px; color: var(--muted);
  border-bottom: 1px solid var(--line);
}

/* Live running totals. These sit in the space between the recent points and
 * the buttons, which would otherwise be dead — and they answer the question a
 * parent is already asking ("how many has he given away?") without a tap. */
.glance {
  /* Six tiles laid out 3x2 — an auto-fit grid strands a lone tile on the
   * second row with an empty cell beside it. */
  display: grid; grid-template-columns: repeat(3, 1fr);
  gap: 1px; background: var(--line);
  border-bottom: 1px solid var(--line);
}
@media (min-width: 560px) { .glance { grid-template-columns: repeat(6, 1fr); } }
.glance div { background: var(--bg); padding: 7px 6px; text-align: center; }
.glance .k { font-size: 9px; font-weight: 800; letter-spacing: .5px; color: var(--muted); text-transform: uppercase; }
.glance .v { font-size: 17px; font-weight: 800; font-variant-numeric: tabular-nums; }
@media (orientation: landscape) and (max-height: 500px) { .glance { display: none; } }
.recent-row { display: flex; gap: 8px; }
.recent-row .sc { font-variant-numeric: tabular-nums; min-width: 52px; color: var(--text); opacity: .75; }

/* The entry zone. It GROWS to fill whatever the score bar and the recent strip
 * leave behind, so on a tall phone the targets are enormous rather than leaving
 * a dead void in the middle of the screen. Caps stop them looking silly on a
 * tablet. Everything here must stay thumb-reachable. */
.entry {
  margin-top: auto; flex: 1; min-height: 0;
  padding: 10px 10px calc(10px + env(safe-area-inset-bottom));
  display: flex; flex-direction: column; justify-content: flex-end; gap: 8px;
}

.serve-block { display: flex; flex-direction: column; justify-content: flex-end; gap: 5px; min-height: 0; }
/* Serve stage: the three serve buttons are the only thing on screen, so let
 * them take the space. */
.entry > .serve-block:only-child { flex: 1; }
.entry > .serve-block:only-child .serve-row { flex: 1; max-height: 160px; }

.serve-row { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; }
.serve-row .btn { height: 100%; min-height: var(--btn-h); font-size: clamp(13px, 3.6vw, 16px); }
.serve-label { font-size: 11px; letter-spacing: .8px; color: var(--muted); font-weight: 700; text-align: center; }

.outcome-block { flex: 1; min-height: 0; display: flex; flex-direction: column; gap: 6px; }
.outcome-head { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }
.outcome-head div {
  text-align: center; font-size: 11px; font-weight: 800; letter-spacing: .8px; color: var(--muted);
}
.outcome-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; flex: 1; min-height: 0; }
/* Once the buttons hit their cap, any space left over belongs ABOVE them —
 * a gap under the buttons would push them out of the thumb zone. */
.outcome-grid > .stack { min-height: 0; justify-content: flex-end; }
.outcome-grid .btn {
  flex: 1; height: auto; min-height: var(--btn-h); max-height: 132px;
  display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 2px;
  font-size: clamp(13px, 3.8vw, 17px); line-height: 1.1;
}
.outcome-grid .btn .sub { font-size: 10px; font-weight: 600; color: var(--muted); letter-spacing: .4px; }

/* Optional detail chips — these appear AFTER the point is committed.
 * One group per row: mixed together they wrap into a jumble where the group
 * labels end up stranded mid-line and nothing can be found at a glance. */
.chips {
  display: flex; flex-direction: column; gap: 5px;
  padding: 7px 10px calc(7px + env(safe-area-inset-bottom));
  border-top: 1px solid var(--line);
  background: var(--surface);
  animation: chips-in .16s ease-out;
}
.chips.empty { display: none; }
@keyframes chips-in { from { opacity: 0; transform: translateY(6px); } to { opacity: 1; transform: none; } }

.chip-row {
  display: flex; align-items: center; gap: 6px;
  overflow-x: auto; scrollbar-width: none;
  /* If a row does have to scroll, fade the edge so a cut-off chip reads as
   * "there is more", not as a rendering fault. */
  -webkit-mask-image: linear-gradient(to right, #000 calc(100% - 22px), transparent 100%);
  mask-image: linear-gradient(to right, #000 calc(100% - 22px), transparent 100%);
}
.chip-row::-webkit-scrollbar { display: none; }
.chip-row .chip { flex: none; }
.chip-label { flex: none; width: 44px; font-size: 10px; font-weight: 800; color: var(--muted); letter-spacing: .6px; }

.chip {
  appearance: none; font: inherit; font-size: 13px; font-weight: 700;
  padding: 7px 13px; min-height: 36px;
  border-radius: 999px; border: 2px solid var(--line);
  background: var(--surface-2); color: var(--text);
  cursor: pointer; touch-action: manipulation;
}
.chip:active { transform: scale(.94); }
.chip.on { background: var(--accent); border-color: var(--accent); color: #fff; }
.chip-group { display: flex; gap: 6px; }

.undo-btn { padding: 8px 12px; min-height: 40px; font-size: 13px; }

.changeover-banner {
  margin: 8px 10px 0; padding: 10px 12px; border-radius: var(--radius);
  background: var(--warn); color: #1a1206; font-weight: 800; font-size: 14px;
  display: flex; align-items: center; justify-content: space-between; gap: 10px;
}

/* ---------------- reports ---------------- */

.insight {
  border-left: 4px solid var(--accent);
  padding: 10px 12px; margin-bottom: 8px;
  background: var(--surface); border-radius: 0 var(--radius) var(--radius) 0;
  font-size: 14px; line-height: 1.45;
}
.insight.good { border-left-color: var(--good); }
.insight.bad { border-left-color: var(--bad); }
.insight b { font-weight: 800; }

.statgrid { display: grid; grid-template-columns: repeat(auto-fit, minmax(140px, 1fr)); gap: 8px; }
.stat { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); padding: 10px 12px; }
.stat .k { font-size: 11px; color: var(--muted); font-weight: 700; letter-spacing: .4px; text-transform: uppercase; }
.stat .v { font-size: 24px; font-weight: 800; font-variant-numeric: tabular-nums; }
.stat .sub { font-size: 11px; color: var(--muted); }

table.stats { width: 100%; border-collapse: collapse; font-size: 14px; }
table.stats th, table.stats td { padding: 8px 6px; border-bottom: 1px solid var(--line); text-align: right; }
table.stats th:first-child, table.stats td:first-child { text-align: left; }
table.stats thead th { color: var(--muted); font-size: 11px; text-transform: uppercase; letter-spacing: .5px; }
.table-wrap { overflow-x: auto; }

/* Same edge fade as the chip rows, so the tabs that run off the right read as
 * scrollable rather than missing. */
.tabs {
  display: flex; gap: 6px; overflow-x: auto; padding-bottom: 4px; scrollbar-width: none;
  -webkit-mask-image: linear-gradient(to right, #000 calc(100% - 24px), transparent 100%);
  mask-image: linear-gradient(to right, #000 calc(100% - 24px), transparent 100%);
}
.tabs::-webkit-scrollbar { display: none; }
.tabs .chip { flex: none; }

.bar-track { background: var(--surface-2); border-radius: 6px; height: 10px; overflow: hidden; }
.bar-fill { height: 100%; background: var(--accent); }

.pointlog { font-size: 13px; }
.pointlog .pt { display: flex; gap: 8px; padding: 7px 4px; border-bottom: 1px solid var(--line); align-items: center; }
.pointlog .pt .sc { font-variant-numeric: tabular-nums; color: var(--muted); min-width: 92px; font-size: 12px; }
.pointlog .pt.win { border-left: 3px solid var(--good); }
.pointlog .pt.lose { border-left: 3px solid var(--bad); }
.pointlog .pt .tag { font-size: 10px; font-weight: 800; padding: 2px 6px; border-radius: 4px; background: var(--surface-2); color: var(--muted); }

.match-row { display: flex; gap: 12px; align-items: center; padding: 12px; border-bottom: 1px solid var(--line); cursor: pointer; }
.match-row .res { font-weight: 900; font-size: 15px; width: 22px; text-align: center; }
.match-row .res.w { color: var(--good); }
.match-row .res.l { color: var(--bad); }

.sync-pill { font-size: 11px; padding: 3px 8px; border-radius: 999px; background: var(--surface-2); color: var(--muted); font-weight: 700; }
.sync-pill.offline { background: var(--warn); color: #1a1206; }
.sync-pill.error { background: var(--bad); color: #fff; }
.sync-pill.syncing { background: var(--accent); color: #fff; }

/* ---------------- short screens ---------------- */

/* An iPhone SE, or any phone in landscape. Everything the app needs in order
 * to work still fits; the running totals and the third recent point are the
 * first things to go, because they are nice-to-have rather than essential. */
@media (max-height: 680px) {
  .glance { display: none; }
  .recent { min-height: 0; padding: 4px 12px; }
  .recent .recent-row:nth-child(n+3) { display: none; }
  .score-games, .score-points { font-size: clamp(22px, 7vw, 30px); }
  .pressure { font-size: 12px; padding: 4px 10px; }
  .chip { min-height: 34px; padding: 6px 11px; font-size: 12px; }
  .chips { gap: 4px; padding-top: 5px; }
}

@media (orientation: landscape) and (max-height: 500px) {
  :root { --btn-h: clamp(40px, 15dvh, 58px); }
  .recent { display: none; }
  .score-games, .score-points { font-size: 24px; }
  .entry { flex-direction: row; align-items: flex-end; gap: 10px; }
  .entry > .serve-block { flex: 0 0 34%; }
  .entry > .outcome-block { flex: 1; }
  /* The serve buttons stack in landscape, so three of them need more room
   * than the portrait height cap allows — lift it and let them share the
   * column evenly. */
  .serve-row { grid-template-columns: 1fr; grid-auto-rows: 1fr; }
  .entry > .serve-block:only-child .serve-row { max-height: none; }
  /* Stacked chip rows are four deep — too tall to fit beside the buttons in
   * landscape. Lay every group out on one scrolling line instead. */
  .chips { flex-direction: row; align-items: center; gap: 8px; overflow-x: auto; scrollbar-width: none; }
  .chips::-webkit-scrollbar { display: none; }
  .chip-row { overflow: visible; -webkit-mask-image: none; mask-image: none; }
  .chip-label { width: auto; }
}

@media print {
  .no-print { display: none !important; }
  body, .card, .stat { background: #fff !important; color: #000 !important; }
  #app { height: auto; }
  .page { overflow: visible; }
}

/* ------------------------------------------------------------------ *
 * Chart tokens
 *
 * Categorical slots 1 and 2 and the blue<->red diverging pair from the
 * validated reference palette. Both modes are stepped for their own
 * surface rather than flipped. The "sun" theme reuses the light steps and
 * leans on always-visible direct labels for relief.
 * ------------------------------------------------------------------ */
:root {
  --viz-1: #3987e5;          /* categorical slot 1 — blue */
  --viz-2: #d95926;          /* categorical slot 2 — orange */
  --viz-pos: #3987e5;        /* diverging: ahead */
  --viz-neg: #e66767;        /* diverging: behind */
  --viz-mid: #383835;        /* diverging midpoint / zero line */
  --viz-grid: #2b333f;
  --viz-ink: #f3f6fa;
  --viz-ink-2: #98a4b6;
}
[data-theme="light"], [data-theme="sun"] {
  --viz-1: #2a78d6;
  --viz-2: #eb6834;
  --viz-pos: #2a78d6;
  --viz-neg: #e34948;
  --viz-mid: #f0efec;
  --viz-grid: #d5dbe4;
  --viz-ink: #10151c;
  --viz-ink-2: #52514e;
}
[data-theme="sun"] { --viz-mid: #000000; --viz-grid: #000000; --viz-ink-2: #000000; }

.chart { width: 100%; height: auto; display: block; touch-action: pan-y; }
.chart text { font-family: inherit; }
.legend { display: flex; flex-wrap: wrap; gap: 12px; font-size: 12px; color: var(--muted); margin-top: 6px; }
.legend .key { display: flex; align-items: center; gap: 6px; }
.legend .swatch { width: 12px; height: 12px; border-radius: 3px; flex: none; }
.chart-tip {
  position: fixed; z-index: 70; pointer-events: none;
  background: var(--surface); border: 1px solid var(--line); color: var(--text);
  border-radius: 8px; padding: 6px 9px; font-size: 12px; font-weight: 600;
  box-shadow: var(--shadow); white-space: nowrap;
}
.chart-caption { font-size: 12px; color: var(--muted); margin-top: 4px; }
