:root {
  color-scheme: dark;
  --bg: #0d1015;
  --panel: #161b22;
  --panel-2: #1c222b;
  --line: #2a323d;
  --text: #e9edf3;
  --muted: #8b96a5;
  --accent: #2f7bff;
  --accent-hi: #4b8fff;
  --accent-lo: #1f62d8;
  --danger: #d6453d;
  --yellow: #ffd400;
  --green: #18c964;
  --lift-top: inset 0 1px 0 rgb(255 255 255 / 0.07);
  --lift-bottom: inset 0 -1px 0 rgb(0 0 0 / 0.4);
  --lift-contact: 0 1px 2px rgb(0 0 0 / 0.5);
  --lift-glow: 0 3px 8px -2px rgb(0 0 0 / 0.5);
  font-family: system-ui, "Segoe UI", Roboto, Arial, sans-serif;
}

* { box-sizing: border-box; }
html, body { margin: 0; background: var(--bg); color: var(--text); }
body { min-height: 100vh; font-size: 15px; }
h2 { margin: 0 0 10px; font-size: 13px; letter-spacing: 0.08em; text-transform: uppercase; color: var(--muted); }
h2 + * { margin-top: 0; }
.muted { color: var(--muted); }
.error { color: #ff7b72; min-height: 1.2em; }
[hidden] { display: none !important; }

input, select, textarea {
  width: 100%;
  padding: 9px 10px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #0f1319;
  color: var(--text);
  font: inherit;
}
textarea { font-family: ui-monospace, Consolas, monospace; resize: vertical; }
input:focus, select:focus, textarea:focus { outline: 2px solid var(--accent); outline-offset: -1px; }
label { display: grid; gap: 5px; margin-bottom: 10px; color: var(--muted); font-size: 13px; }
label.check { display: flex; align-items: center; gap: 8px; }
label.check input { width: auto; }

/* ---------- Lit buttons ---------- */
.btn, .key {
  border: 1px solid #2c3540;
  border-radius: 8px;
  padding: 8px 14px;
  color: var(--text);
  font: 600 14px/1.1 inherit;
  font-family: inherit;
  cursor: pointer;
  background: linear-gradient(180deg, #252d38, #1b222b);
  box-shadow: var(--lift-top), var(--lift-bottom), var(--lift-contact), var(--lift-glow);
  transition: box-shadow 0.15s, transform 0.1s, filter 0.15s;
}
.btn:hover, .key:hover { filter: brightness(1.08); }
.btn:active:not(:disabled), .key:active, .key.is-hit {
  transform: translateY(1px);
  box-shadow: var(--lift-bottom), var(--lift-contact);
}
.btn:disabled { opacity: 0.45; cursor: default; box-shadow: var(--lift-contact); }
.btn.small { padding: 6px 10px; font-size: 13px; }
.btn.big { padding: 13px 18px; font-size: 17px; border-radius: 10px; }
.btn.ghost { background: transparent; border-color: transparent; box-shadow: none; color: var(--muted); }
.btn.primary, .key.enter {
  color: #fff;
  border-color: rgb(10 40 110 / 0.7);
  background: linear-gradient(180deg, var(--accent-hi) 0%, var(--accent) 55%, var(--accent-lo) 100%);
  text-shadow: 0 1px 0 rgb(0 0 0 / 0.3);
  box-shadow: inset 0 1px 0 rgb(255 255 255 / 0.3), inset 0 -2px 0 rgb(0 0 0 / 0.25), 0 1px 2px rgb(0 0 0 / 0.5), 0 6px 18px -4px rgb(47 123 255 / 0.55);
}
.btn.danger {
  border-color: rgb(120 20 20 / 0.7);
  background: linear-gradient(180deg, #3a1d1f, #2a1517);
  color: #ffb4ad;
  box-shadow: var(--lift-top), var(--lift-bottom), var(--lift-contact), 0 3px 10px -3px rgb(214 69 61 / 0.4);
}
.btn.is-on {
  transform: translateY(1px);
  color: #fff;
  border-color: var(--accent);
  background: linear-gradient(180deg, #1d3157, #18284a);
  box-shadow: inset 0 2px 4px rgb(0 0 0 / 0.45), 0 0 0 3px rgb(47 123 255 / 0.28);
}
.btn.flag-y.is-on { border-color: var(--yellow); background: linear-gradient(180deg, #4a4210, #3a330b); box-shadow: inset 0 2px 4px rgb(0 0 0 / .45), 0 0 0 3px rgb(255 212 0 / .25); }
.btn.flag-r.is-on { border-color: var(--danger); background: linear-gradient(180deg, #4a1714, #3a110f); box-shadow: inset 0 2px 4px rgb(0 0 0 / .45), 0 0 0 3px rgb(214 69 61 / .3); }

/* ---------- Gate ---------- */
.gate { display: grid; place-items: center; min-height: 100vh; padding: 16px; }
.gate-card { width: min(340px, 100%); display: grid; gap: 12px; padding: 26px; background: var(--panel); border: 1px solid var(--line); border-radius: 12px; }
.gate-card input { font-size: 28px; text-align: center; letter-spacing: 0.3em; }
.wordmark { font-weight: 900; letter-spacing: 0.06em; font-size: 18px; }
.wordmark span { color: var(--accent-hi); }
.gate-card .wordmark { font-size: 26px; text-align: center; }

/* ---------- Header ---------- */
.top {
  position: sticky; top: 0; z-index: 10;
  display: flex; align-items: center; gap: 16px; flex-wrap: wrap;
  padding: 10px 16px; background: #0a0d11; border-bottom: 1px solid var(--line);
}
.event-name { color: var(--muted); flex: 1; min-width: 120px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.tabs { display: flex; gap: 4px; }
.tab { border: 0; background: none; color: var(--muted); padding: 8px 12px; border-radius: 8px; font: 600 14px inherit; font-family: inherit; cursor: pointer; }
.tab.is-on { color: #fff; background: var(--panel-2); }
.conn { width: 10px; height: 10px; border-radius: 50%; background: var(--danger); }
.conn.is-live { background: var(--green); box-shadow: 0 0 8px var(--green); }

.tab-page { padding: 16px; }
.card { background: var(--panel); border: 1px solid var(--line); border-radius: 12px; padding: 16px; min-width: 0; }

/* ---------- Score ---------- */
.score-grid {
  display: grid; gap: 16px;
  grid-template-columns: minmax(280px, 1fr) minmax(300px, 380px) minmax(320px, 1.3fr);
  grid-template-areas: "status keypad order" "history history order";
  align-items: start;
}
.status-card { grid-area: status; }
.keypad-card { grid-area: keypad; }
.order-card { grid-area: order; }
.history-card { grid-area: history; }

.race-pick { display: flex; gap: 6px; }
.race-pick select { font-weight: 600; }
.ltg { text-align: center; margin: 14px 0 6px; }
.ltg-num { font-size: 96px; font-weight: 900; line-height: 1; font-variant-numeric: tabular-nums; }
.ltg-label { color: var(--muted); letter-spacing: 0.2em; font-size: 12px; }
.lapline { display: flex; justify-content: center; align-items: center; gap: 12px; margin-bottom: 14px; color: var(--muted); }
.flag-pill { padding: 3px 10px; border-radius: 999px; font-weight: 800; font-size: 12px; letter-spacing: 0.08em; text-transform: uppercase; background: #2a323d; color: #cfd6df; }
.flag-pill[data-flag="green"] { background: var(--green); color: #032; }
.flag-pill[data-flag="yellow"] { background: var(--yellow); color: #221d00; }
.flag-pill[data-flag="red"] { background: var(--danger); color: #fff; }
.flag-pill[data-flag="white"] { background: #fff; color: #111; }
.flag-pill[data-flag="checkered"] { background: repeating-conic-gradient(#111 0 25%, #eee 0 50%) 0 0 / 10px 10px; color: #fff; text-shadow: 0 0 3px #000, 0 0 3px #000; }

.entry-label { font-size: 12px; color: var(--muted); letter-spacing: 0.08em; text-transform: uppercase; }
.entry-strip { display: flex; flex-wrap: wrap; gap: 6px; margin: 6px 0 8px; min-height: 44px; }
.chip {
  width: 40px; height: 40px; display: grid; place-items: center;
  border-radius: 8px; font-weight: 900; font-size: 22px;
  background: #fff; color: #000;
}
.chip.empty { background: transparent; border: 1px dashed var(--line); color: var(--muted); font-size: 14px; }
.chip.new { animation: pop 0.25s ease; }
@keyframes pop { from { transform: scale(1.3); } to { transform: scale(1); } }
.message { min-height: 1.4em; font-weight: 600; color: var(--muted); }
.message.is-error { color: #ff7b72; }
.flag-row { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 10px; }

.keypad { display: grid; grid-template-columns: repeat(4, 1fr); grid-auto-rows: 64px; gap: 8px; }
.key { padding: 0; font-size: 28px; font-weight: 800; display: grid; place-content: center; line-height: 1; }
.key small { display: block; font-size: 10px; font-weight: 600; color: var(--muted); margin-top: 3px; letter-spacing: 0.04em; }
.key.enter small { color: #dbe7ff; }
.key.fn { font-size: 20px; }
.key.tall { grid-row: span 2; }
.key.wide { grid-column: span 2; }
.key.xk, .key.yk { color: #ffd88a; }
/* Numpad layout: the tall + sits in column 4 rows 2-3, Enter in column 4 rows 4-5. */
.keypad .yk { grid-column: 4; grid-row: 2 / span 2; }
.keypad .enter { grid-column: 4; grid-row: 4 / span 2; }
.hint { color: var(--muted); font-size: 12px; margin: 12px 0 0; line-height: 1.5; }

.order { width: 100%; border-collapse: collapse; font-variant-numeric: tabular-nums; }
.order th { text-align: left; font-size: 11px; color: var(--muted); font-weight: 600; padding: 4px 6px; border-bottom: 1px solid var(--line); }
.order td { padding: 5px 6px; border-bottom: 1px solid #1f262f; }
.order .pos { width: 34px; color: var(--muted); font-weight: 700; }
.order .car span { display: inline-grid; place-items: center; width: 30px; height: 26px; border-radius: 5px; background: #fff; color: #000; font-weight: 900; }
.order tr.is-punched .car span { background: var(--accent); color: #fff; }
.order tr.is-dnf { opacity: 0.45; }
.order .down { color: #ffb86b; font-weight: 700; }
.order td:last-child { text-align: right; }

.history { margin: 0; padding-left: 0; list-style: none; display: grid; gap: 4px; max-height: 360px; overflow: auto; font-family: ui-monospace, Consolas, monospace; }
.history li { display: flex; gap: 10px; align-items: baseline; }
.history .lapno { color: var(--muted); min-width: 64px; }
.history .cars { letter-spacing: 0.12em; font-weight: 700; }
.history .tag { font-size: 11px; color: #ffb86b; }

/* ---------- Races ---------- */
.races-grid { display: grid; gap: 16px; grid-template-columns: minmax(300px, 1fr) minmax(300px, 1fr); align-items: start; }
.races-grid h2 { margin-top: 18px; }
.races-grid h2:first-child { margin-top: 0; }
.row-form { display: flex; gap: 8px; margin-bottom: 8px; }
.two { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.laps-field { max-width: 140px; }
.form-actions { display: flex; gap: 8px; flex-wrap: wrap; }
.race-list { display: grid; gap: 14px; margin-bottom: 14px; }
.class-group h3 { margin: 0 0 6px; font-size: 15px; }
.race-item { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; padding: 8px 10px; border: 1px solid var(--line); border-radius: 8px; background: var(--panel-2); margin-bottom: 6px; }
.race-item.is-active { border-color: var(--accent); box-shadow: 0 0 0 2px rgb(47 123 255 / 0.25); }
.race-item .name { flex: 1; font-weight: 600; min-width: 120px; }
.race-item .meta { color: var(--muted); font-size: 12px; }
.badge { font-size: 11px; padding: 2px 7px; border-radius: 999px; background: #2a323d; color: #cfd6df; }
.badge.live { background: var(--green); color: #032; }
.badge.done { background: #444; }
.photo-race { border: 1px solid var(--line); border-radius: 8px; padding: 10px; margin-bottom: 10px; background: var(--panel-2); }

/* ---------- Graphics ---------- */
.gfx-grid { display: grid; gap: 16px; grid-template-columns: minmax(280px, 380px) 1fr; align-items: start; }
.gfx-buttons { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; margin-bottom: 16px; }
.gfx-buttons .btn { padding: 16px; font-size: 16px; }
.links { padding-left: 18px; margin: 0; display: grid; gap: 6px; word-break: break-all; }
.links a { color: var(--accent-hi); }
.preview-box { position: relative; width: 100%; aspect-ratio: 16 / 9; overflow: hidden; border-radius: 8px; border: 1px solid var(--line);
  background: repeating-conic-gradient(#20262e 0 25%, #181d24 0 50%) 0 0 / 24px 24px; }
.preview-box iframe { position: absolute; left: 0; top: 0; width: 1920px; height: 1080px; border: 0; transform-origin: 0 0; }

dialog { border: 1px solid var(--line); border-radius: 12px; background: var(--panel); color: var(--text); width: min(460px, calc(100vw - 32px)); padding: 20px; }
dialog::backdrop { background: rgb(0 0 0 / 0.6); }
#mf-order { font-size: 26px; letter-spacing: 0.2em; font-weight: 800; font-family: ui-monospace, Consolas, monospace; }

@media (max-width: 1150px) {
  .score-grid { grid-template-columns: 1fr 1fr; grid-template-areas: "status keypad" "order order" "history history"; }
}
@media (max-width: 760px) {
  .score-grid, .races-grid, .gfx-grid { grid-template-columns: 1fr; }
  .score-grid { grid-template-areas: "status" "keypad" "order" "history"; }
  .top { gap: 8px; }
  .event-name { display: none; }
  .ltg-num { font-size: 72px; }
}

/* ---------- Lap sheet actions ---------- */
.history li { flex-wrap: wrap; padding: 3px 0; border-bottom: 1px solid #1f262f; }
.lap-actions { margin-left: auto; display: flex; gap: 6px; opacity: 0.35; transition: opacity 0.15s; }
.history li:hover .lap-actions, .lap-actions:focus-within { opacity: 1; }
.lap-actions .btn { padding: 3px 8px; font-size: 11px; font-family: system-ui, sans-serif; }
@media (hover: none) { .lap-actions { opacity: 1; } }
#le-cars { font-size: 26px; letter-spacing: 0.2em; font-weight: 800; font-family: ui-monospace, Consolas, monospace; }

/* ---------- Results ---------- */
.results-grid { display: grid; gap: 16px; grid-template-columns: minmax(240px, 320px) 1fr; align-items: start; }
.results-list .race-item { width: 100%; text-align: left; color: var(--text); font: inherit; cursor: pointer; }
.results-head { display: flex; justify-content: space-between; gap: 12px; flex-wrap: wrap; margin-bottom: 12px; }
.results-head h2 { margin: 0; }
.res-title { font-size: 26px; font-weight: 800; }
.results-table td.gain { color: var(--green); font-weight: 700; }
.results-table td.loss { color: #ff7b72; font-weight: 700; }
.res-sheet-h { margin-top: 20px; }
#res-sheet { max-height: none; }
@media (max-width: 760px) { .results-grid { grid-template-columns: 1fr; } }

@media print {
  body { background: #fff; color: #000; }
  .top, .results-list, .results-head .form-actions, .lap-actions, #tab-score, #tab-races, #tab-graphics { display: none !important; }
  .card { border: 0; background: #fff; padding: 0; }
  .results-grid { display: block; }
  .order td, .order th { color: #000; border-color: #ccc; }
  .order .car span { border: 1px solid #000; }
  .res-title, .muted, h2 { color: #000; }
}
.results-head .btn { white-space: nowrap; }
