:root {
  --bg: #f6f4ef;
  --surface: #ffffff;
  --surface-2: #efece5;
  --text: #1d1c1a;
  --muted: #6b675f;
  --line: #ddd8cd;
  --accent: #d9581c;
  --accent-ink: #ffffff;
  --ok: #2b8a3e;
  --warn: #c98a00;
  --danger: #b42318;
  --radius: 10px;
  --shadow: 0 1px 2px rgba(0, 0, 0, 0.06), 0 4px 16px rgba(0, 0, 0, 0.04);
  --swatch-edge: rgba(0, 0, 0, 0.45);   /* keeps white visible on white */
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  color-scheme: light;
}
.dark {
  --bg: #0f1115;
  --surface: #171a21;
  --surface-2: #1f232c;
  --text: #f2f2f0;
  --muted: #9a9ca3;
  --line: #2a2f3a;
  --accent: #ff7a3d;
  --accent-ink: #14110f;
  --swatch-edge: rgba(255, 255, 255, 0.55);   /* keeps black visible on black */
  color-scheme: dark;
}
* { box-sizing: border-box; }
html, body { margin: 0; padding: 0; }
body {
  background: var(--bg);
  color: var(--text);
  line-height: 1.45;
  -webkit-font-smoothing: antialiased;
  min-height: 100vh;
}
a { color: inherit; }
h1, h2, h3 { margin: 0 0 0.5rem; line-height: 1.2; }
h1 { font-size: 1.6rem; }
h2 { font-size: 1.2rem; }
h3 { font-size: 1rem; }
p { margin: 0.4rem 0; }
.muted { color: var(--muted); }
.small { font-size: 0.85rem; }
.mono { font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace; letter-spacing: 0.06em; }
.right { text-align: right; }
.center { text-align: center; }
.nowrap { white-space: nowrap; }
.hidden { display: none !important; }

.container { max-width: 1100px; margin: 0 auto; padding: 16px; }
.narrow { max-width: 520px; }

.topbar {
  display: flex; align-items: center; justify-content: space-between; gap: 12px;
  padding: 10px 16px; background: var(--surface); border-bottom: 1px solid var(--line);
  position: sticky; top: 0; z-index: 5;
}
.topbar .brand { font-weight: 700; text-decoration: none; }
.topbar nav a { margin-left: 12px; text-decoration: none; color: var(--muted); }
.topbar nav a.active, .topbar nav a:hover { color: var(--text); }

.card {
  background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius);
  padding: 16px; box-shadow: var(--shadow);
}
.card + .card { margin-top: 12px; }
.grid { display: grid; gap: 12px; }
.grid.cols-2 { grid-template-columns: repeat(2, minmax(0, 1fr)); }
.grid.cols-3 { grid-template-columns: repeat(3, minmax(0, 1fr)); }
.grid.cols-4 { grid-template-columns: repeat(4, minmax(0, 1fr)); }
@media (max-width: 720px) {
  .grid.cols-2, .grid.cols-3, .grid.cols-4 { grid-template-columns: 1fr 1fr; }
  .grid.cols-2.stack-mobile { grid-template-columns: 1fr; }
}
.row { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }
.row.between { justify-content: space-between; }
.spacer { flex: 1; }

label { display: block; font-size: 0.85rem; color: var(--muted); margin: 8px 0 4px; }
input, select, textarea, button {
  font: inherit; color: inherit;
}
input[type=text], input[type=number], input[type=password], input[type=date], select, textarea {
  width: 100%; padding: 10px 12px; border: 1px solid var(--line); border-radius: 8px;
  background: var(--surface); outline: none;
}
input:focus, select:focus, textarea:focus { border-color: var(--accent); box-shadow: 0 0 0 3px rgba(217, 88, 28, 0.15); }
textarea { min-height: 120px; resize: vertical; }
input.code {
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  font-size: 1.6rem; letter-spacing: 0.25em; text-transform: uppercase; text-align: center; padding: 14px;
}

button, .btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  padding: 10px 14px; border-radius: 8px; border: 1px solid var(--line);
  background: var(--surface-2); cursor: pointer; text-decoration: none; white-space: nowrap;
}
button:hover, .btn:hover { filter: brightness(0.97); }
button:disabled { opacity: 0.5; cursor: default; }
.btn-primary { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); font-weight: 600; }
.btn-danger { color: var(--danger); }
.btn-sm { padding: 6px 10px; font-size: 0.85rem; }
.btn-lg { padding: 14px 18px; font-size: 1.05rem; width: 100%; }
.btn-ghost { background: transparent; }

table { width: 100%; border-collapse: collapse; }
th, td { text-align: left; padding: 8px 10px; border-bottom: 1px solid var(--line); vertical-align: middle; }
th { font-size: 0.8rem; text-transform: uppercase; letter-spacing: 0.04em; color: var(--muted); font-weight: 600; }
tr:last-child td { border-bottom: none; }
td.num, th.num { text-align: right; font-variant-numeric: tabular-nums; }
.table-wrap { overflow-x: auto; }

.badge {
  display: inline-block; padding: 2px 8px; border-radius: 999px; font-size: 0.75rem;
  background: var(--surface-2); color: var(--muted); border: 1px solid var(--line);
}
.badge.ok { color: var(--ok); border-color: var(--ok); }
.badge.warn { color: var(--warn); border-color: var(--warn); }
.badge.danger { color: var(--danger); border-color: var(--danger); }
.badge.accent { color: var(--accent); border-color: var(--accent); }

.tabs { display: flex; gap: 4px; border-bottom: 1px solid var(--line); margin-bottom: 12px; overflow-x: auto; }
.tabs a { padding: 10px 14px; text-decoration: none; color: var(--muted); border-bottom: 2px solid transparent; white-space: nowrap; }
.tabs a.active { color: var(--text); border-bottom-color: var(--accent); font-weight: 600; }

.stat { padding: 12px 14px; background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); }
.stat .label { font-size: 0.75rem; text-transform: uppercase; letter-spacing: 0.05em; color: var(--muted); }
.stat .value { font-size: 1.6rem; font-weight: 700; font-variant-numeric: tabular-nums; line-height: 1.15; }

.alert { padding: 10px 12px; border-radius: 8px; border: 1px solid var(--line); background: var(--surface-2); margin: 8px 0; }
.alert.error { border-color: var(--danger); color: var(--danger); background: rgba(180, 35, 24, 0.06); }
.alert.ok { border-color: var(--ok); color: var(--ok); background: rgba(43, 138, 62, 0.06); }

.swatch { display: inline-block; width: 12px; height: 12px; border-radius: 3px; border: 1px solid var(--swatch-edge); vertical-align: -1px; margin-right: 6px; flex: none; }
.swatch-lg { width: 18px; height: 18px; border-radius: 4px; vertical-align: -3px; }

.modal-backdrop { position: fixed; inset: 0; background: rgba(0, 0, 0, 0.45); display: flex; align-items: center; justify-content: center; z-index: 50; padding: 16px; }
.modal { background: var(--surface); border-radius: 14px; padding: 20px; width: 100%; max-width: 520px; max-height: 92vh; overflow: auto; box-shadow: 0 20px 60px rgba(0,0,0,0.3); }
.modal.wide { max-width: 780px; }

.toast { position: fixed; left: 50%; bottom: 20px; transform: translateX(-50%); background: var(--text); color: var(--bg); padding: 10px 16px; border-radius: 999px; font-size: 0.9rem; z-index: 60; box-shadow: var(--shadow); }

/* ---------- Climber view ---------- */
.climb-header { position: sticky; top: 0; z-index: 5; background: var(--surface); border-bottom: 1px solid var(--line); padding: 12px 16px; }
.climb-header .score { font-size: 2rem; font-weight: 800; font-variant-numeric: tabular-nums; line-height: 1; }
.problem-list { list-style: none; margin: 0; padding: 0; }
.problem {
  display: grid; grid-template-columns: 56px 1fr auto; gap: 10px; align-items: center;
  background: var(--surface); border: 1px solid var(--line); border-radius: 12px; padding: 10px 12px; margin-bottom: 8px;
}
.problem.ticked { border-color: var(--ok); background: rgba(43, 138, 62, 0.06); }
.problem .label { font-size: 1.3rem; font-weight: 800; text-align: center; font-variant-numeric: tabular-nums; }
.problem .meta { font-size: 0.82rem; color: var(--muted); }
.problem .place { font-size: 0.78rem; font-weight: 700; text-transform: uppercase; letter-spacing: 0.05em; color: var(--accent); }
.problem .value { font-weight: 700; font-variant-numeric: tabular-nums; }
.tick-btn {
  width: 84px; padding: 10px 0; border-radius: 10px; font-weight: 700; border: 1px solid var(--line); background: var(--surface-2);
}
.tick-btn.on { background: var(--ok); border-color: var(--ok); color: #fff; }
.filterbar { display: flex; gap: 8px; padding: 10px 16px; position: sticky; top: 92px; z-index: 4; background: var(--bg); }
.filterbar input, .filterbar select { padding: 8px 10px; }
.chips { display: flex; gap: 6px; flex-wrap: wrap; padding: 0 16px 8px; }
.chip { padding: 6px 12px; border-radius: 999px; border: 1px solid var(--line); background: var(--surface); font-size: 0.85rem; cursor: pointer; }
.chip.active { background: var(--text); color: var(--bg); border-color: var(--text); }

/* ---------- Live view ---------- */
.live { min-height: 100vh; display: grid; grid-template-rows: auto 1fr; }
.live-header { display: flex; align-items: baseline; justify-content: space-between; padding: 18px 28px 6px; }
.live-header h1 { font-size: 2rem; }
.live-grid { display: grid; grid-template-columns: 3fr 2fr; gap: 20px; padding: 12px 28px 28px; }
.live .card { padding: 18px 20px; }
.live table td, .live table th { padding: 10px 12px; font-size: 1.25rem; }
.live table th { font-size: 0.85rem; }
.live .rank { width: 48px; color: var(--muted); font-weight: 700; }
.live .rank-1 .rank, .live .rank-2 .rank, .live .rank-3 .rank { color: var(--accent); }
.live .score { font-weight: 800; }
.mvp { display: grid; grid-template-columns: minmax(110px, auto) 1fr; gap: 20px; align-items: center; }
.mvp .big { font-size: 4.2rem; font-weight: 900; line-height: 1; text-align: center; font-variant-numeric: tabular-nums; white-space: nowrap; }
.mvp .points { font-size: 2rem; font-weight: 800; color: var(--accent); font-variant-numeric: tabular-nums; }
.feed { list-style: none; margin: 0; padding: 0; }
.feed li { display: flex; justify-content: space-between; gap: 12px; padding: 8px 0; border-bottom: 1px solid var(--line); font-size: 1.1rem; }
.feed li:last-child { border-bottom: none; }
.feed .time { color: var(--muted); font-variant-numeric: tabular-nums; }
.live-stats { display: grid; grid-template-columns: repeat(4, 1fr); gap: 12px; }
.live .stat .value { font-size: 2rem; }
@media (max-width: 1000px) {
  .live-grid { grid-template-columns: 1fr; }
  .live-stats { grid-template-columns: repeat(2, 1fr); }
}
.pulse { animation: pulse 0.9s ease-out; }
@keyframes pulse { from { background: rgba(255, 122, 61, 0.35); } to { background: transparent; } }

/* ---------- Poster ---------- */
.poster { max-width: 760px; margin: 0 auto; padding: 40px 24px; text-align: center; }
.poster .qr { width: 360px; height: 360px; margin: 24px auto; }
.poster h1 { font-size: 2.6rem; }
.poster .url { font-size: 1.6rem; font-weight: 700; }
@media print { .no-print { display: none !important; } body { background: #fff; } }

/* ---------- Competition clock ---------- */
.countdown { font-variant-numeric: tabular-nums; font-weight: 800; letter-spacing: 0.02em; line-height: 1; }
.countdown .state { display: block; font-size: 0.34em; font-weight: 600; letter-spacing: 0.08em; text-transform: uppercase; color: var(--muted); }
.countdown.warn { color: var(--warn); }
.countdown.over { color: var(--danger); }
.countdown.idle { color: var(--muted); }
.live .countdown { font-size: 3.2rem; }

/* ---------- Charts (inline SVG, drawn in common.js) ---------- */
.chart { display: block; width: 100%; height: auto; }
.chart .track { fill: var(--accent); opacity: 0.16; }
.chart .fill { fill: var(--accent); }
.chart .axis { stroke: var(--line); stroke-width: 1; }
.chart text { font-size: 11px; }
.chart .key { fill: var(--muted); }
.chart .val { fill: var(--text); font-weight: 700; font-variant-numeric: tabular-nums; }
.chart g:hover .track { opacity: 0.28; }
.live .chart text { font-size: 13px; }
.chart-legend { display: flex; gap: 16px; color: var(--muted); font-size: 0.8rem; margin-top: 10px; }
.chart-legend i { display: inline-block; width: 10px; height: 10px; border-radius: 3px; margin-right: 6px; background: var(--accent); }
.chart-legend i.track { opacity: 0.16; }
.live .chart-legend { font-size: 1rem; }

/* ---------- Sortable table headings ---------- */
th.sortable { cursor: pointer; user-select: none; white-space: nowrap; }
th.sortable:hover, th.sortable:focus-visible { color: var(--text); }
th.sortable.sorted { color: var(--text); }
th.sortable .arrow { margin-left: 4px; font-size: 0.7em; color: var(--accent); }

/* ---------- Commentator ---------- */
.comm-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 14px; align-items: start; }
.comm-head { display: flex; justify-content: space-between; align-items: center; gap: 20px; flex-wrap: wrap; margin-bottom: 12px; }
.comm-head .countdown { font-size: 2.6rem; }
.chase { width: 100%; border-collapse: collapse; }
.chase td, .chase th { padding: 7px 8px; border-bottom: 1px solid var(--line); vertical-align: top; }
.chase .rank { width: 34px; color: var(--muted); font-weight: 800; }
.chase .gap { color: var(--muted); font-variant-numeric: tabular-nums; white-space: nowrap; }
.needs { margin: 2px 0 0; color: var(--muted); font-size: 0.9rem; }
.needs strong { color: var(--accent); }
.pill { display: inline-block; padding: 1px 7px; border: 1px solid var(--line); border-radius: 999px; font-weight: 700; margin-right: 4px; font-variant-numeric: tabular-nums; }
.pill.lead { border-color: var(--accent); color: var(--accent); }
@media (max-width: 900px) { .comm-grid { grid-template-columns: 1fr; } }
