:root {
  --bg: #f5f3ec;
  --surface: #ffffff;
  --surface-2: #efece2;
  --ink: #1c231f;
  --muted: #5e675f;
  --line: #dcd7c8;
  --green: #1f5130;
  --green-ink: #ffffff;
  --green-soft: #e3eee5;
  --sand: #c9ad62;
  --sand-soft: #f6edcf;
  --red: #b3261e;
  --red-soft: #fbe4e1;
  --amber: #8a5a00;
  --amber-soft: #fdf0cf;
  --focus: #2f7a4a;
  --radius: 10px;
  --shadow: 0 1px 2px rgba(20, 30, 20, .06), 0 2px 8px rgba(20, 30, 20, .05);
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #121714;
    --surface: #1a211c;
    --surface-2: #222b25;
    --ink: #e9eee9;
    --muted: #9aa69d;
    --line: #2f3a33;
    --green: #3f9a60;
    --green-ink: #08130c;
    --green-soft: #1c3324;
    --sand: #d8bf78;
    --sand-soft: #3a3320;
    --red: #ff8a80;
    --red-soft: #3d1f1c;
    --amber: #f3c35b;
    --amber-soft: #3a2f14;
    --focus: #6fd08f;
    --shadow: none;
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--ink);
  font: 16px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  font-variant-numeric: tabular-nums;
}
main { max-width: 1100px; margin: 0 auto; padding: 20px 16px 80px; outline: none; }
a { color: var(--green); }
h1, h2, h3 { line-height: 1.2; margin: 0 0 .5em; }
h1 { font-size: 1.6rem; }
h2 { font-size: 1.2rem; }
h3 { font-size: 1rem; }
p { margin: 0 0 1em; }
.muted { color: var(--muted); }
.small { font-size: .875rem; }

/* Top bar */
.masthead { position: sticky; top: 0; z-index: 10; }
.topbar {
  display: flex; align-items: center; gap: 16px; flex-wrap: wrap;
  padding: 10px 16px;
  background: var(--green); color: var(--green-ink);
}
@media (prefers-color-scheme: dark) { .topbar { background: #173d24; color: #f3f6f2; } }
.wordmark {
  font-weight: 800; font-size: 1.35rem; letter-spacing: -.02em;
  color: inherit; text-decoration: none;
}
.wordmark::before { content: "⛳ "; font-weight: 400; }
.topbar { justify-content: space-between; }
.topbar-link { color: inherit; font-weight: 600; font-size: .95rem; text-decoration: none; opacity: .9; padding: 6px 12px; border-radius: 999px; }
.topbar-link:hover { background: rgba(255,255,255,.14); opacity: 1; }

/* Tournament context: which tournament you're in, and the steps */
.context { background: var(--surface); border-bottom: 1px solid var(--line); box-shadow: var(--shadow); }
.context-inner { max-width: 1100px; margin: 0 auto; padding: 10px 16px 0; }
.context-head { display: flex; flex-direction: column; gap: 2px; }
.back-link { font-size: .85rem; font-weight: 600; text-decoration: none; }
.back-link:hover { text-decoration: underline; }
.context-title { display: flex; align-items: baseline; gap: 4px 12px; flex-wrap: wrap; }
.t-name { font-size: 1.3rem; font-weight: 800; letter-spacing: -.01em; }
.t-meta { color: var(--muted); font-size: .95rem; }
.steps { display: flex; gap: 4px; margin-top: 8px; overflow-x: auto; scrollbar-width: none; }
.steps::-webkit-scrollbar { display: none; }
.step {
  display: flex; align-items: center; gap: 8px; padding: 8px 14px 10px; text-decoration: none; color: var(--muted);
  border-bottom: 3px solid transparent; white-space: nowrap; flex: none;
}
.step:hover { color: var(--ink); background: var(--surface-2); border-radius: 8px 8px 0 0; }
.step[aria-current="page"] { color: var(--ink); border-bottom-color: var(--green); }
.step-num {
  width: 26px; height: 26px; border-radius: 50%; display: inline-grid; place-items: center; flex: none;
  font-weight: 800; font-size: .85rem; background: var(--surface-2); color: var(--muted);
}
.step[aria-current="page"] .step-num { background: var(--green); color: var(--green-ink); }
.step.done .step-num { background: var(--green-soft); color: var(--green); }
.step[aria-current="page"].done .step-num { background: var(--green); color: var(--green-ink); }
.step-text { display: flex; flex-direction: column; line-height: 1.15; }
.step-text b { font-size: .95rem; }
.step-text small { font-size: .75rem; color: var(--muted); }
.step-tv { margin-left: auto; }
.step-tv .step-num { background: transparent; font-size: 1.1rem; }
.tabs { display: flex; gap: 4px; flex-wrap: wrap; }
.tabs a {
  color: inherit; text-decoration: none; opacity: .8;
  padding: 6px 12px; border-radius: 999px; font-weight: 600; font-size: .95rem;
}
.tabs a:hover { opacity: 1; background: rgba(255,255,255,.12); }
.tabs a[aria-current="page"] { opacity: 1; background: rgba(255,255,255,.2); }

/* Building blocks */
.card {
  background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius);
  padding: 18px; margin-bottom: 16px; box-shadow: var(--shadow);
}
.row { display: flex; gap: 12px; flex-wrap: wrap; align-items: center; }
.row.space { justify-content: space-between; }
.grid { display: grid; gap: 12px; grid-template-columns: repeat(auto-fit, minmax(180px, 1fr)); }
.stack > * + * { margin-top: 12px; }

label.field { display: flex; flex-direction: column; gap: 4px; font-size: .875rem; color: var(--muted); font-weight: 600; }
input, select, textarea {
  font: inherit; color: var(--ink); background: var(--surface);
  border: 1px solid var(--line); border-radius: 8px; padding: 8px 10px; min-height: 40px;
  width: 100%;
}
input:focus, select:focus, textarea:focus, button:focus-visible, a:focus-visible {
  outline: 3px solid var(--focus); outline-offset: 1px;
}
input[type="checkbox"] { width: 20px; height: 20px; min-height: 0; accent-color: var(--green); }

button, .btn {
  font: inherit; font-weight: 600; cursor: pointer;
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  min-height: 40px; padding: 8px 14px; border-radius: 8px;
  border: 1px solid var(--line); background: var(--surface); color: var(--ink);
  text-decoration: none; white-space: nowrap;
}
button:hover, .btn:hover { background: var(--surface-2); }
.btn-primary { background: var(--green); border-color: var(--green); color: var(--green-ink); }
.btn-primary:hover { background: var(--green); filter: brightness(1.08); }
.btn-danger { color: var(--red); }
.btn-ghost { background: transparent; border-color: transparent; }
.btn-sm { min-height: 32px; padding: 4px 10px; font-size: .875rem; }
button:disabled { opacity: .5; cursor: not-allowed; }

.pill {
  display: inline-flex; align-items: center; gap: 4px;
  padding: 2px 9px; border-radius: 999px; font-size: .8rem; font-weight: 700; white-space: nowrap;
  background: var(--surface-2); color: var(--muted);
}
.pill.ok { background: var(--green-soft); color: var(--green); }
.pill.warn { background: var(--amber-soft); color: var(--amber); }
.pill.bad { background: var(--red-soft); color: var(--red); }
.pill.gold { background: var(--sand-soft); color: var(--amber); }
@media (prefers-color-scheme: dark) { .pill.ok { color: #8fe0a9; } }

.notice { border-radius: 8px; padding: 10px 12px; margin: 0 0 12px; font-size: .95rem; }
.notice.info { background: var(--green-soft); }
.notice.warn { background: var(--amber-soft); color: var(--ink); }
.notice.bad { background: var(--red-soft); color: var(--ink); }
.notice ul { margin: 4px 0 0; padding-left: 20px; }

/* Tables */
.table-wrap { overflow-x: auto; -webkit-overflow-scrolling: touch; }
table { width: 100%; border-collapse: collapse; }
th, td { padding: 8px 10px; text-align: left; border-bottom: 1px solid var(--line); vertical-align: middle; }
th { font-size: .8rem; text-transform: uppercase; letter-spacing: .04em; color: var(--muted); font-weight: 700; }
td.num, th.num { text-align: right; }
tr.winner td { background: var(--sand-soft); }
tr.winner td:first-child { box-shadow: inset 4px 0 0 var(--sand); }

/* Home */
.tournament-list { list-style: none; padding: 0; margin: 0; }
.tournament-list li { display: flex; align-items: center; justify-content: space-between; gap: 12px; padding: 12px 0; border-bottom: 1px solid var(--line); flex-wrap: wrap; }
.tournament-list li:last-child { border-bottom: 0; }
.tournament-list a.name { font-weight: 700; font-size: 1.1rem; text-decoration: none; color: var(--ink); }
.empty { text-align: center; padding: 40px 16px; }
.empty .big { font-size: 3rem; }

/* Players table */
.players-table input { min-height: 36px; padding: 6px 8px; }
.players-table td { padding: 6px; }
.players-table .w-num { width: 90px; }
.players-table .w-chk { width: 56px; text-align: center; }
.flight-tag { font-weight: 800; padding: 2px 8px; border-radius: 6px; background: var(--surface-2); }
.flight-tag.B { background: var(--sand-soft); }

/* Scorecard */
.scorecard { width: 100%; border-collapse: separate; border-spacing: 0; }
.scorecard th, .scorecard td { text-align: center; padding: 4px 2px; border-bottom: 0; }
.scorecard th { font-size: .75rem; }
.scorecard .lbl { text-align: left; font-weight: 700; font-size: .8rem; color: var(--muted); padding-right: 8px; white-space: nowrap; }
.scorecard input {
  width: 40px; min-height: 44px; padding: 2px; text-align: center; font-size: 1.15rem; font-weight: 700;
  -moz-appearance: textfield;
}
.scorecard input::-webkit-outer-spin-button, .scorecard input::-webkit-inner-spin-button { -webkit-appearance: none; margin: 0; }
.scorecard .tot { font-weight: 800; font-size: 1.1rem; min-width: 48px; padding-left: 6px; }
.scorecard input.ocr { background: var(--amber-soft); }
.scorecard input.err { border-color: var(--red); box-shadow: 0 0 0 2px var(--red-soft); }
.scorecard input.hi { border-color: var(--amber); }
.scorecard input.under { color: var(--red); }
.scorecard .par-row td { color: var(--muted); font-size: .85rem; }
.nine { display: flex; gap: 16px; flex-wrap: wrap; }
.nine > div { flex: 1 1 480px; overflow-x: auto; }
.totals-bar { display: flex; gap: 12px; flex-wrap: wrap; align-items: end; }
.totals-bar .stat { background: var(--surface-2); border-radius: 8px; padding: 8px 14px; min-width: 90px; }
.totals-bar .stat b { display: block; font-size: 1.5rem; }
.totals-bar .stat span { font-size: .75rem; text-transform: uppercase; letter-spacing: .04em; color: var(--muted); font-weight: 700; }
.totals-bar .stat.match { background: var(--green-soft); }
.totals-bar .stat.mismatch { background: var(--red-soft); }

/* OCR */
.ocr-panel { display: grid; gap: 16px; grid-template-columns: minmax(0, 1fr); }
@media (min-width: 900px) { .ocr-panel.has-image { grid-template-columns: minmax(0, 1.1fr) minmax(0, 1fr); } }
.crop-box { position: relative; user-select: none; touch-action: none; border-radius: 8px; overflow: hidden; background: #000; cursor: crosshair; }
.crop-box img, .crop-box video { display: block; width: 100%; height: auto; }
.crop-rect { position: absolute; border: 3px solid #ffd34d; background: rgba(255, 211, 77, .15); box-shadow: 0 0 0 9999px rgba(0,0,0,.45); pointer-events: none; }
.progress { height: 8px; background: var(--surface-2); border-radius: 99px; overflow: hidden; }
.progress > div { height: 100%; background: var(--green); width: 0; transition: width .2s; }

/* Leaderboard */
.flights { display: grid; gap: 16px; grid-template-columns: repeat(auto-fit, minmax(min(100%, 420px), 1fr)); }
.pos { font-weight: 800; width: 44px; }
.net { font-weight: 800; font-size: 1.1rem; }
.trophy { color: var(--sand); }

/* Toast */
.toast {
  position: fixed; left: 50%; bottom: 20px; transform: translateX(-50%) translateY(150%);
  background: var(--ink); color: var(--bg); padding: 10px 16px; border-radius: 8px; font-weight: 600;
  transition: transform .2s, visibility .2s; z-index: 100; max-width: calc(100% - 32px); visibility: hidden;
}
.toast.show { transform: translateX(-50%) translateY(0); visibility: visible; }

/* Dialog */
dialog { border: 0; border-radius: 12px; padding: 0; max-width: min(720px, calc(100% - 32px)); width: 100%; background: var(--surface); color: var(--ink); }
dialog::backdrop { background: rgba(0,0,0,.5); }
dialog .dlg-body { padding: 18px; }

/* TV / clubhouse mode */
body.tv { background: #050806; color: #fff; overflow: hidden; }
body.tv .masthead { display: none; }
body.tv main { max-width: none; padding: 2.5vh 2.5vw; height: 100vh; display: flex; flex-direction: column; }
.tv-head { display: flex; align-items: baseline; justify-content: space-between; gap: 2vw; margin-bottom: 2vh; flex-wrap: wrap; }
.tv-head h1 { font-size: clamp(28px, 4.2vw, 80px); margin: 0; font-weight: 800; letter-spacing: -.02em; }
.tv-head .meta { font-size: clamp(16px, 1.8vw, 36px); color: #b9c7bd; }
.tv-brand { font-weight: 800; color: #6fd08f; }
.tv-flights { flex: 1; min-height: 0; display: grid; gap: 2.5vw; grid-template-columns: repeat(auto-fit, minmax(min(100%, 40vw), 1fr)); }
.tv-flight { min-height: 0; overflow: hidden; display: flex; flex-direction: column; }
.tv-flight h2 { font-size: clamp(20px, 2.6vw, 52px); color: #ffd34d; margin: 0 0 1vh; }
.tv-scroll { overflow: hidden; flex: 1; min-height: 0; }
.tv-table { width: 100%; border-collapse: collapse; font-size: clamp(18px, 2.3vw, 48px); }
.tv-table th { color: #9fb0a4; font-size: .5em; border-bottom: 2px solid #2b3a30; padding: .3em .4em; }
.tv-table td { padding: .28em .4em; border-bottom: 1px solid #1d2a21; color: #fff; }
.tv-table td.num, .tv-table th.num { text-align: right; }
.tv-table .pos { color: #9fb0a4; }
.tv-table .net { color: #6fd08f; font-size: 1em; }
.tv-table tr.winner td { background: #2a2408; }
.tv-table tr.winner td:first-child { box-shadow: inset .25em 0 0 #ffd34d; }
.tv-table tr.winner .name::after { content: " 🏆"; }
.tv-table tr.pending td { color: #66756b; }
.tv-foot { margin-top: 1.5vh; display: flex; justify-content: space-between; color: #7f9184; font-size: clamp(12px, 1.2vw, 24px); gap: 2vw; flex-wrap: wrap; }
.tv-exit { position: fixed; top: 12px; right: 12px; opacity: 0; transition: opacity .3s; z-index: 5; }
body.tv.cursor-active .tv-exit { opacity: 1; }
body.tv:not(.cursor-active) { cursor: none; }
.tv-exit button { background: #1d2a21; color: #fff; border-color: #2b3a30; }

/* Phones */
@media (max-width: 640px) {
  h1 { font-size: 1.35rem; }
  .card { padding: 14px; }
  .topbar { gap: 8px; padding: 8px 12px; }
  .t-name { font-size: 1.1rem; }
  .step { padding: 6px 8px 8px; gap: 6px; }
  .step-text small { display: none; }
  .step-tv { margin-left: 0; }
  .players-table thead { display: none; }
  .players-table tr { display: grid; grid-template-columns: 1fr 1fr 40px 1.1fr 32px; gap: 6px; align-items: end; padding: 10px 0; border-bottom: 1px solid var(--line); }
  .players-table select { padding: 2px 4px !important; font-size: .875rem; }
  .players-table td { border: 0; padding: 0; }
  .players-table td.name-cell { grid-column: 1 / -1; }
  .players-table td[data-label]::before { content: attr(data-label); display: block; font-size: .7rem; font-weight: 700; color: var(--muted); text-transform: uppercase; }
  .players-table .w-num, .players-table .w-chk { width: auto; text-align: left; }
  .scorecard .lbl { display: none; }
  .scorecard input { width: 30px; min-height: 40px; font-size: 1rem; padding: 0; }
  .scorecard th, .scorecard td { padding: 3px 1px; }
  .scorecard .tot { min-width: 34px; padding-left: 2px; }
  .hide-sm { display: none; }
}

@media print {
  .topbar, .no-print, .toast, .steps, .back-link { display: none !important; }
  .masthead { position: static; }
  body { background: #fff; color: #000; }
  .card { box-shadow: none; border: 0; }
}
