:root {
  --felt: #0b6b3a;
  --felt-dark: #084a28;
  --bg: #0e1116;
  --panel: #171c24;
  --panel-2: #1f2630;
  --ink: #e8edf2;
  --muted: #93a1b0;
  --line: #2a323d;
  --gold: #f0c419;
  --green: #36d07a;
  --red: #ff5d5d;
  --yellow: #ffd24d;
  --accent: #4aa8ff;
  --shadow: 0 6px 24px rgba(0,0,0,.35);
  --mono: ui-monospace, "JetBrains Mono", "SF Mono", Menlo, Consolas, monospace;
}

* { box-sizing: border-box; }
html, body { margin: 0; }
button, .mark-cell, .segmented, input[type="range"] { touch-action: manipulation; }
body {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  background: radial-gradient(1200px 600px at 50% -10%, #16202b, var(--bg)) fixed;
  color: var(--ink);
  line-height: 1.45;
  padding-bottom: 40px;
}
h1, h2, h3, h4 { margin: 0 0 .4em; font-weight: 650; }
.muted { color: var(--muted); }
.small { font-size: .82rem; }
button { font: inherit; cursor: pointer; }
code { font-family: var(--mono); font-size: .9em; }
input, select { font: inherit; }

/* ---------- top bar ---------- */
.topbar {
  text-align: center;
  padding: 22px 16px 14px;
  background: linear-gradient(180deg, var(--felt), var(--felt-dark));
  border-bottom: 3px solid var(--gold);
  box-shadow: var(--shadow);
  position: relative;
}
.brand { display: flex; gap: .5rem; align-items: center; justify-content: center; flex-wrap: wrap; }
.brand h1 { font-size: 1.6rem; letter-spacing: .3px; }
.brand h1 .muted { font-weight: 400; }
.brand .dart { font-size: 1.3rem; }
.tagline { margin: 6px 0 0; color: #d8efe0; font-size: .92rem; }
.home-link { position: absolute; left: 16px; top: 18px; color: #d8efe0; text-decoration: none; font-size: .85rem; opacity: .85; }
.home-link:hover { opacity: 1; }

/* ---------- tabs / layout ---------- */
.mode-tabs { max-width: 1180px; margin: 16px auto 0; padding: 0 16px; display: flex; gap: 8px; flex-wrap: wrap; }
.mode-tabs button { background: var(--panel); border: 1px solid var(--line); color: var(--muted); border-radius: 999px; padding: 7px 18px; font-weight: 650; }
.mode-tabs button.active { background: var(--gold); border-color: var(--gold); color: #241c02; }
.mode-tabs button:not(.active):hover { border-color: var(--gold); color: var(--gold); }
.hidden { display: none !important; }

main#lab-main, .match-wrap, .cal-wrap {
  max-width: 1180px; margin: 18px auto; padding: 0 16px;
}
main#lab-main { display: grid; grid-template-columns: 1.15fr 1fr; gap: 16px; align-items: start; }
.match-wrap { display: grid; grid-template-columns: 1.05fr 1fr; gap: 16px; align-items: start; }
.cal-wrap { display: flex; flex-direction: column; gap: 16px; }

.panel {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 14px;
  padding: 16px 18px;
  box-shadow: var(--shadow);
}
.panel > h2 { font-size: 1.05rem; color: var(--gold); }

.ghost { background: transparent; border: 1px solid var(--line); color: var(--ink); border-radius: 8px; padding: 6px 12px; }
.ghost:hover { border-color: var(--gold); color: var(--gold); }
.ghost:disabled { opacity: .45; cursor: default; }
.ghost:disabled:hover { border-color: var(--line); color: var(--ink); }
.small-btn { font-size: .82rem; padding: 4px 10px; }
button.primary { background: var(--accent); color: #04121f; border: none; border-radius: 8px; padding: 8px 16px; font-weight: 650; }
button.primary:disabled { opacity: .5; cursor: default; }
button:focus-visible, input:focus-visible, select:focus-visible { outline: 2px solid var(--gold); outline-offset: 1px; }

.segmented { display: inline-flex; border: 1px solid var(--line); border-radius: 8px; overflow: hidden; flex-wrap: wrap; }
.segmented button { background: var(--panel-2); color: var(--muted); border: none; padding: 5px 12px; }
.segmented button.active { background: var(--accent); color: #04121f; font-weight: 650; }

/* ---------- lab controls ---------- */
.lab-controls { display: flex; flex-direction: column; gap: 8px; margin-bottom: 12px; }
.ctl { display: flex; align-items: center; gap: 10px; flex-wrap: wrap; }
.ctl-lbl { font-size: .78rem; color: var(--muted); min-width: 64px; }

/* ---------- the board ---------- */
.board-stack { position: relative; width: min(100%, 540px); aspect-ratio: 1; margin: 0 auto; touch-action: manipulation; }
.board-stack canvas, .board-stack svg { position: absolute; inset: 0; width: 100%; height: 100%; display: block; }
.board-stack canvas { border-radius: 50%; background: #0a0d12; }
.board-stack svg { cursor: crosshair; }
.board-legend { display: flex; gap: 16px; justify-content: center; flex-wrap: wrap; margin-top: 8px; font-size: .78rem; color: var(--muted); }
.board-legend .lg { display: inline-flex; align-items: center; gap: 6px; }
.pin-demo { width: 11px; height: 11px; border-radius: 50%; display: inline-block; }
.pin-demo.best { background: var(--gold); box-shadow: 0 0 6px var(--gold); }
.pin-demo.tap { border: 2px dashed #fff; }
.heat-scale i { width: 64px; height: 9px; border-radius: 4px; display: inline-block;
  background: linear-gradient(90deg,#0b0e13,#123a5c,#147a6c,#36d07a,#f0c419); }

.sigma-row { margin-top: 14px; }
.sigma-row label { display: flex; justify-content: space-between; font-size: .85rem; color: var(--muted); margin-bottom: 4px; }
.sigma-row label b { color: var(--ink); font-family: var(--mono); font-weight: 650; }
.sigma-row input[type="range"] { width: 100%; accent-color: var(--gold); }
.sigma-ticks { display: flex; justify-content: space-between; font-size: .68rem; color: #6f7d8c; margin-top: 2px; }
.sigma-row .small-btn { margin-top: 8px; }

/* ---------- readouts ---------- */
.verdict { font-size: 1.02rem; background: var(--panel-2); border: 1px solid var(--line); border-radius: 10px; padding: 10px 14px; margin-bottom: 12px; }
.verdict .big { color: var(--gold); font-weight: 750; }
.verdict .sub { color: var(--muted); font-size: .85rem; margin-top: 3px; }
.big-verdict { flex: 1; min-width: 220px; }

.aims-table { width: 100%; border-collapse: collapse; font-size: .88rem; margin-bottom: 14px; }
.aims-table td { padding: 6px 8px; border-bottom: 1px solid var(--line); }
.aims-table .a-name { color: var(--muted); }
.aims-table .a-ev { text-align: right; font-family: var(--mono); font-variant-numeric: tabular-nums; font-weight: 650; white-space: nowrap; }
.aims-table .a-delta { text-align: right; font-family: var(--mono); font-variant-numeric: tabular-nums; white-space: nowrap; }
.aims-table .a-bar { width: 38%; }
.bar { height: 14px; border-radius: 4px; background: var(--panel-2); position: relative; overflow: hidden; }
.bar > span { position: absolute; top: 0; bottom: 0; left: 0; background: linear-gradient(90deg,#1d7a4a,var(--green)); border-radius: 4px; }
.aims-table tr.best td { background: rgba(240,196,25,.07); }
.aims-table tr.best .a-name { color: var(--gold); }
.aims-table tr.tap-row td { background: rgba(255,255,255,.04); }
.ev-pos { color: var(--green); } .ev-neg { color: var(--red); } .ev-zero { color: var(--yellow); }

.dist-block { margin-top: 4px; }
.dist-head { font-size: .85rem; color: var(--muted); margin-bottom: 6px; }
.dist-head span { color: var(--ink); }
.prob-bar { display: flex; height: 22px; border-radius: 6px; overflow: hidden; border: 1px solid var(--line); font-size: .72rem; font-weight: 700; }
.prob-bar > span { display: flex; align-items: center; justify-content: center; color: #06140d; white-space: nowrap; min-width: 0; overflow: hidden; }
.prob-legend { display: flex; gap: 12px; font-size: .74rem; color: var(--muted); margin-top: 5px; flex-wrap: wrap; }
.prob-legend i { width: 10px; height: 10px; border-radius: 3px; display: inline-block; margin-right: 4px; vertical-align: -1px; }
.dist-stats { display: flex; gap: 14px; margin-top: 10px; flex-wrap: wrap; }
.dist-stats .chip { background: var(--panel-2); border: 1px solid var(--line); border-radius: 999px; padding: 3px 12px; font-size: .82rem; }
.dist-stats .chip b { font-family: var(--mono); font-weight: 650; }

/* ---------- match: gauge & candidates ---------- */
.gauge-row { display: flex; gap: 14px; align-items: center; margin-bottom: 12px; flex-wrap: wrap; }
#mini-board { width: 118px; min-width: 118px; height: 118px; background: #0a0d12; border-radius: 50%; border: 1px solid var(--line); }
.gauge { width: 118px; min-width: 118px; text-align: center; background: var(--panel-2); border: 1px solid var(--line); border-radius: 12px; padding: 10px 6px; }
.gauge .g-num { font-size: 1.7rem; font-weight: 750; font-family: var(--mono); font-variant-numeric: tabular-nums; color: var(--gold); }
.gauge .g-num.working { color: var(--muted); animation: pulse 1.1s ease-in-out infinite; }
.gauge .g-lbl { font-size: .68rem; color: var(--muted); letter-spacing: .04em; }
.gauge .g-ci { font-size: .68rem; color: #6f7d8c; font-family: var(--mono); }
@keyframes pulse { 50% { opacity: .45; } }
@media (prefers-reduced-motion: reduce) { .gauge .g-num.working { animation: none; } }

.cand-table { width: 100%; border-collapse: collapse; font-size: .86rem; margin-bottom: 12px; }
.cand-table td { padding: 6px 8px; border-bottom: 1px solid var(--line); }
.cand-table tr { cursor: pointer; }
.cand-table tr:hover td { background: rgba(74,168,255,.06); }
.cand-table tr.best td { background: rgba(240,196,25,.08); }
.cand-table tr.picked td { background: rgba(74,168,255,.12); }
.cand-table .c-play b { color: var(--ink); }
.cand-table .c-play .style { color: var(--muted); font-size: .78rem; }
.cand-table .c-bar { width: 30%; }
.cand-table .c-p { text-align: right; font-family: var(--mono); font-variant-numeric: tabular-nums; font-weight: 650; white-space: nowrap; }
.cand-table .c-d { text-align: right; font-family: var(--mono); font-variant-numeric: tabular-nums; color: var(--muted); white-space: nowrap; }

/* ---------- match: recorder ---------- */
.recorder { border-top: 1px dashed var(--line); padding-top: 12px; }
.rec-head { font-size: .85rem; color: var(--muted); margin-bottom: 8px; }
.rec-head b { color: var(--ink); }
.rec-buttons { display: flex; gap: 8px; flex-wrap: wrap; }
.rec-buttons button {
  min-width: 64px; min-height: 46px; border-radius: 10px; font-weight: 700;
  background: var(--panel-2); border: 1px solid var(--line); color: var(--ink);
}
.rec-buttons button:hover { border-color: var(--gold); }
.rec-buttons button.hit { border-color: #1d7a4a; color: var(--green); }
.rec-buttons button.hit:hover { border-color: var(--green); }
.rec-buttons button.big-hit { background: #143524; border-color: #1d7a4a; color: var(--green); }
.rec-buttons button.miss { color: var(--muted); }
.rec-note { font-size: .78rem; color: #6f7d8c; margin-top: 8px; }

/* ---------- match: scoreboard ---------- */
.who-row { display: flex; gap: 22px; align-items: center; margin-bottom: 10px; flex-wrap: wrap; }
.pips { display: inline-flex; gap: 5px; }
.pips i { width: 10px; height: 22px; border-radius: 3px; background: var(--panel-2); border: 1px solid var(--line); }
.pips i.on { background: var(--gold); border-color: var(--gold); }
.rec-head .pips.inline { gap: 3px; margin: 0 6px; vertical-align: -3px; }
.rec-head .pips.inline i { width: 7px; height: 15px; }

.score-grid { width: 100%; border-collapse: collapse; }
.score-grid td { border-bottom: 1px solid var(--line); padding: 3px 4px; text-align: center; }
.score-grid .seg-lbl { font-weight: 750; font-size: 1.05rem; width: 56px; color: var(--ink); }
.score-grid .seg-lbl small { display: block; font-size: .62rem; color: #6f7d8c; font-weight: 400; }
.mark-cell {
  width: 46%; min-height: 40px; height: 40px; border-radius: 8px; cursor: pointer; user-select: none;
  font-size: 1.15rem; font-weight: 700; font-family: var(--mono);
  background: var(--panel-2); border: 1px solid var(--line); color: var(--muted);
  display: flex; align-items: center; justify-content: center;
}
td.mark-cell:hover { border-color: var(--accent); }
.mark-cell.m1, .mark-cell.m2 { color: var(--yellow); }
.mark-cell.m3 { color: var(--green); border-color: #1d7a4a; background: #12281c; }
.mark-cell.dead { opacity: .55; }
.mark-cell.scoring { box-shadow: inset 0 0 0 1px var(--gold); }

.pts-row { display: flex; gap: 14px; margin-top: 12px; flex-wrap: wrap; }
.pts-box { flex: 1; min-width: 150px; background: var(--panel-2); border: 1px solid var(--line); border-radius: 10px; padding: 8px 10px; font-size: .85rem; color: var(--muted); }
.stepper { display: flex; gap: 6px; align-items: center; margin-top: 4px; }
.stepper button { width: 34px; height: 34px; border-radius: 8px; background: var(--panel); border: 1px solid var(--line); color: var(--ink); font-size: 1.1rem; }
.stepper input { width: 74px; text-align: center; background: var(--bg); color: var(--ink); border: 1px solid var(--line); border-radius: 8px; padding: 6px 4px; font-family: var(--mono); font-size: 1rem; }
.stepper input::-webkit-outer-spin-button, .stepper input::-webkit-inner-spin-button { -webkit-appearance: none; }
.stepper input[type=number] { -moz-appearance: textfield; appearance: textfield; }

.opp-row { display: flex; gap: 10px; align-items: center; margin-top: 12px; flex-wrap: wrap; }
.opp-row label { font-size: .85rem; color: var(--muted); display: inline-flex; gap: 8px; align-items: center; }
.opp-row select { background: var(--bg); color: var(--ink); border: 1px solid var(--line); border-radius: 6px; padding: 4px 6px; }
.match-actions { display: flex; gap: 8px; margin-top: 12px; flex-wrap: wrap; }
.turn-log { margin-top: 10px; max-height: 96px; overflow-y: auto; line-height: 1.5; }

/* ---------- region picker dialog ---------- */
dialog#picker { background: var(--panel); color: var(--ink); border: 1px solid var(--line); border-radius: 14px; padding: 14px; max-width: min(92vw, 430px); }
dialog#picker::backdrop { background: rgba(5,8,12,.7); }
dialog#picker h3 { font-size: .95rem; color: var(--gold); }
dialog#picker svg { width: 100%; height: auto; cursor: crosshair; }
dialog#picker .dlg-actions { display: flex; justify-content: space-between; margin-top: 8px; }

/* ---------- calibrate ---------- */
.profile-card { display: flex; gap: 16px; flex-wrap: wrap; background: var(--panel-2); border: 1px solid var(--line); border-radius: 10px; padding: 12px 14px; margin-bottom: 12px; }
.profile-card .pc-main { font-size: 1.05rem; }
.profile-card .pc-main b { color: var(--gold); font-family: var(--mono); }
.profile-card .pc-stats { display: flex; gap: 10px; flex-wrap: wrap; }
.profile-card .chip, .chips .chip { background: var(--panel); border: 1px solid var(--line); border-radius: 999px; padding: 3px 12px; font-size: .82rem; color: var(--muted); }
.profile-card .chip b { color: var(--ink); font-family: var(--mono); font-weight: 650; }
.chips { display: flex; gap: 8px; flex-wrap: wrap; margin: 10px 0; }
.chips .chip { cursor: pointer; }
.chips .chip:hover { border-color: var(--gold); color: var(--gold); }
.chips .chip.active { background: var(--gold); border-color: var(--gold); color: #241c02; font-weight: 650; }
.mpr-row { display: flex; gap: 10px; align-items: center; flex-wrap: wrap; margin-top: 6px; }
.mpr-row label { font-size: .85rem; color: var(--muted); display: inline-flex; gap: 8px; align-items: center; }
.mpr-row input { width: 74px; background: var(--bg); color: var(--ink); border: 1px solid var(--line); border-radius: 6px; padding: 5px 6px; font-family: var(--mono); font-size: 16px; }

.tally-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(190px, 1fr)); gap: 10px; margin: 12px 0; }
.tally-box { background: var(--panel-2); border: 1px solid var(--line); border-radius: 10px; padding: 10px 12px; }
.tally-box .t-lbl { font-size: .82rem; color: var(--muted); min-height: 34px; }
.tally-box .t-lbl b { color: var(--ink); }
.tally-box .stepper button { width: 44px; height: 44px; font-size: 1.3rem; }
.tally-box .stepper input { width: 64px; font-size: 1.15rem; }
.fit-out { margin-top: 6px; font-size: .92rem; }
.fit-out .fit-sigma { font-size: 1.25rem; font-family: var(--mono); color: var(--gold); font-weight: 750; }
.fit-out .lr { margin-top: 4px; }
.cal-actions { display: flex; gap: 10px; margin-top: 12px; align-items: center; }

.tbl-scroll { overflow-x: auto; }
.dict-table { width: 100%; border-collapse: collapse; font-size: .85rem; min-width: 560px; }
.dict-table th { font-size: .72rem; text-transform: uppercase; letter-spacing: .05em; color: #6f7d8c; font-weight: 600; text-align: right; padding: 6px 10px; border-bottom: 1px solid var(--line); }
.dict-table td { padding: 6px 10px; border-bottom: 1px solid var(--line); text-align: right; font-family: var(--mono); font-variant-numeric: tabular-nums; }
.dict-table th:nth-child(2), .dict-table td:nth-child(2) { text-align: left; font-family: inherit; }
.dict-table tr.you td { background: rgba(240,196,25,.08); }

/* ---------- footer ---------- */
footer { max-width: 1180px; margin: 22px auto 0; padding: 0 16px; }
footer details { background: var(--panel); border: 1px solid var(--line); border-radius: 12px; padding: 12px 16px; }
footer summary { cursor: pointer; color: var(--muted); font-weight: 650; }
.foot-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 18px; margin-top: 12px; font-size: .9rem; }
.foot-grid h4 { color: var(--gold); font-size: .88rem; }

/* ---------- responsive ---------- */
@media (max-width: 900px) {
  main#lab-main, .match-wrap { grid-template-columns: 1fr; }
  .foot-grid { grid-template-columns: 1fr; }
  /* at the oche: advice reads first, scoreboard second */
  .match-wrap { display: flex; flex-direction: column; }
  .advice-panel { order: 0; }
  .score-panel { order: 1; }
  .rec-buttons button { flex: 1 1 30%; min-height: 52px; font-size: 1.05rem; }
}

/* phone-sized: thumb-first layout */
@media (max-width: 640px) {
  /* Match advice panel reorders: verdict → record buttons → the play list.
     Recording a dart must never require a scroll. */
  .advice-panel { display: flex; flex-direction: column; }
  .advice-panel > h2 { order: 0; }
  .advice-panel .gauge-row { order: 1; }
  .advice-panel .recorder { order: 2; border-top: none; border-bottom: 1px dashed var(--line); padding: 0 0 12px; margin-bottom: 10px; }
  .advice-panel .cand-table { order: 3; margin-bottom: 4px; }
  /* gauge + mini-board share the first row; verdict gets the full width below */
  .gauge-row { align-items: center; }
  .gauge-row .gauge { order: 1; }
  #mini-board { order: 2; width: 96px; min-width: 96px; height: 96px; }
  .big-verdict { order: 3; flex-basis: 100%; min-width: 0; }
  /* declutter: numbers carry the info, drop the bars */
  .cand-table .c-bar, .aims-table .a-bar { display: none; }
  .cand-table td, .aims-table td { padding: 8px 6px; }
  /* reclaim vertical space so the record buttons sit above the fold:
     the pips carry the dart count and the hints are boilerplate */
  #match-verdict .sub, .recorder .rec-note { display: none; }
  .mode-tabs { margin-top: 10px; }
  .gauge .g-num { font-size: 1.45rem; }
  .gauge { padding: 7px 6px; width: 104px; min-width: 104px; }
  /* fat-finger targets */
  .mark-cell { height: 48px; font-size: 1.3rem; }
  .stepper button { width: 44px; height: 44px; }
  .segmented button { padding: 7px 10px; font-size: .85rem; }
  .mode-tabs button { padding: 9px 14px; }
  .panel { padding: 14px 12px; }
  .board-legend { gap: 10px; }
}
