/* ==========================================================================
   Race Tracker — shared styles
   Light by default (readable in sun glare on snow), dark via system setting.
   Big tap targets for gloves.
   ========================================================================== */

:root {
  --bg:        #f3f4f6;
  --surface:   #ffffff;
  --surface-2: #f8f9fb;
  --border:    #d9dde3;
  --text:      #14171c;
  --muted:     #5b6472;
  --accent:    #e2461b;   /* race orange */
  --accent-2:  #b8360f;
  --accent-bg: #fdece6;
  --ok:        #177a3b;
  --ok-bg:     #e3f4e9;
  --bad:       #b42318;
  --bad-bg:    #fdeceb;
  --changed:   #fff4c2;
  --changed-b: #d9a800;
  --radius:    12px;
  --shadow:    0 1px 2px rgba(16, 24, 40, .06), 0 1px 3px rgba(16, 24, 40, .08);
  --nav-h:     64px;
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg:        #0f1216;
    --surface:   #181c22;
    --surface-2: #1f242b;
    --border:    #2e343d;
    --text:      #eef0f3;
    --muted:     #9aa3b0;
    --accent:    #ff6a3d;
    --accent-2:  #ff8a63;
    --accent-bg: #3a1d13;
    --ok:        #4cc47a;
    --ok-bg:     #13301f;
    --bad:       #ff6b60;
    --bad-bg:    #3a1715;
    --changed:   #3d3410;
    --changed-b: #d9a800;
    --shadow:    none;
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }

html, body { margin: 0; }
body {
  background: var(--bg);
  color: var(--text);
  font: 16px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  padding-bottom: calc(var(--nav-h) + env(safe-area-inset-bottom) + 16px);
}

a { color: var(--accent); text-decoration: none; }
a:hover { text-decoration: underline; }

h1, h2, h3 { line-height: 1.2; margin: 0 0 .5rem; }
h1 { font-size: 1.6rem; }
h2 { font-size: 1.2rem; }
h3 { font-size: 1rem; }

.muted { color: var(--muted); }
.small { font-size: .875rem; }
.num   { font-variant-numeric: tabular-nums; }
.hidden { display: none !important; }

/* ── Top bar ─────────────────────────────────────────────────────────────── */
.topbar {
  position: sticky; top: 0; z-index: 20;
  display: flex; align-items: center; gap: 12px;
  padding: 10px 16px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
}
.brand {
  display: flex; align-items: center; gap: 8px;
  font-weight: 800; letter-spacing: .04em; text-transform: uppercase;
  color: var(--text);
}
.brand:hover { text-decoration: none; }
.brand .flag {
  width: 28px; height: 28px; border-radius: 6px;
  background:
    conic-gradient(var(--text) 25%, var(--surface) 0 50%, var(--text) 0 75%, var(--surface) 0) 0 0 / 14px 14px;
  border: 2px solid var(--accent);
}
.topbar .team { margin-left: auto; color: var(--muted); font-size: .9rem; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }

/* ── Nav: bottom bar on phones, inline in the top bar on wide screens ────── */
.nav {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 30;
  display: grid; grid-template-columns: repeat(4, 1fr);
  height: calc(var(--nav-h) + env(safe-area-inset-bottom));
  padding-bottom: env(safe-area-inset-bottom);
  background: var(--surface);
  border-top: 1px solid var(--border);
}
.nav a {
  display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 2px;
  color: var(--muted); font-size: .75rem; font-weight: 600;
}
.nav a:hover { text-decoration: none; color: var(--text); }
.nav a svg { width: 24px; height: 24px; }
.nav a.active { color: var(--accent); }

@media (min-width: 820px) {
  body { padding-bottom: 32px; }
  .nav {
    position: static; display: flex; height: auto; padding: 0;
    background: none; border: 0; gap: 4px;
  }
  .nav a { flex-direction: row; gap: 6px; font-size: .95rem; padding: 8px 12px; border-radius: 8px; }
  .nav a svg { width: 18px; height: 18px; }
  .nav a.active { background: var(--accent-bg); }
  .topbar .team { margin-left: auto; }
}

/* ── Layout ──────────────────────────────────────────────────────────────── */
main { max-width: 1100px; margin: 0 auto; padding: 16px; }

.page-head {
  display: flex; flex-wrap: wrap; align-items: flex-end; justify-content: space-between; gap: 12px;
  margin: 8px 0 16px;
}
.page-head .sub { color: var(--muted); margin-top: 2px; }
.crumb { font-size: .9rem; color: var(--muted); display: inline-block; margin-bottom: 4px; }

.grid   { display: grid; gap: 12px; }
.grid-2 { grid-template-columns: repeat(auto-fill, minmax(280px, 1fr)); }
.grid-3 { grid-template-columns: repeat(auto-fill, minmax(220px, 1fr)); }
.stack  { display: flex; flex-direction: column; gap: 12px; }
.row    { display: flex; flex-wrap: wrap; gap: 8px; align-items: center; }
.spacer { flex: 1; }

.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 16px;
}
.card + .card { margin-top: 12px; }
.card-link { display: block; color: inherit; }
.card-link:hover { text-decoration: none; border-color: var(--accent); }
.card h2 { display: flex; align-items: center; gap: 8px; }

/* ── Stats ───────────────────────────────────────────────────────────────── */
.stats { display: grid; grid-template-columns: repeat(3, 1fr); gap: 12px; margin-bottom: 16px; }
.stat { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 12px 14px; }
.stat .v { font-size: 1.8rem; font-weight: 800; font-variant-numeric: tabular-nums; }
.stat .l { color: var(--muted); font-size: .85rem; }

/* ── Pills ───────────────────────────────────────────────────────────────── */
.pill {
  display: inline-block; padding: 2px 10px; border-radius: 999px;
  font-size: .78rem; font-weight: 700; letter-spacing: .02em;
  background: var(--surface-2); border: 1px solid var(--border); color: var(--muted);
}
.pill.snow  { background: #e6f1fb; border-color: #b9d7f2; color: #1d5b8f; }
.pill.grass { background: #e7f5e4; border-color: #bfe3b7; color: #2f6b24; }
.pill.win   { background: var(--ok-bg); border-color: transparent; color: var(--ok); }
.pill.loss  { background: var(--bad-bg); border-color: transparent; color: var(--bad); }
@media (prefers-color-scheme: dark) {
  .pill.snow  { background: #14283a; border-color: #23445f; color: #8cc4f2; }
  .pill.grass { background: #16301a; border-color: #28532e; color: #8fd88a; }
}

/* ── Buttons ─────────────────────────────────────────────────────────────── */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  min-height: 44px; padding: 0 16px;
  border-radius: 10px; border: 1px solid var(--border);
  background: var(--surface); color: var(--text);
  font: inherit; font-weight: 650; cursor: pointer;
}
.btn:hover { text-decoration: none; border-color: var(--muted); }
.btn-primary { background: var(--accent); border-color: var(--accent); color: #fff; }
.btn-primary:hover { background: var(--accent-2); border-color: var(--accent-2); }
.btn-danger { color: var(--bad); }
.btn-danger:hover { border-color: var(--bad); }
.btn-lg { min-height: 52px; font-size: 1.05rem; padding: 0 22px; }
.btn-block { width: 100%; }
.btn:disabled { opacity: .6; cursor: default; }

/* ── Forms ───────────────────────────────────────────────────────────────── */
.form-grid { display: grid; gap: 12px; grid-template-columns: repeat(auto-fill, minmax(170px, 1fr)); }
.form-grid .wide { grid-column: 1 / -1; }

.field { display: flex; flex-direction: column; gap: 4px; min-width: 0; }
.field > span { font-size: .85rem; font-weight: 600; color: var(--muted); }
.field .unit { font-weight: 500; opacity: .8; }

input, select, textarea {
  width: 100%;
  min-height: 46px;
  padding: 10px 12px;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: var(--surface);
  color: var(--text);
  font: inherit;
  font-size: 16px; /* prevents iOS zoom on focus */
}
textarea { min-height: 90px; resize: vertical; }
input:focus, select:focus, textarea:focus {
  outline: 3px solid color-mix(in srgb, var(--accent) 35%, transparent);
  border-color: var(--accent);
}
.field.changed input, .field.changed select {
  background: var(--changed);
  border-color: var(--changed-b);
}
.field .was { font-size: .75rem; color: var(--muted); }

.check-list { display: grid; gap: 8px; grid-template-columns: repeat(auto-fill, minmax(200px, 1fr)); }
.check {
  display: flex; align-items: center; gap: 10px;
  min-height: 48px; padding: 8px 12px;
  border: 1px solid var(--border); border-radius: 10px; background: var(--surface);
  cursor: pointer;
}
.check input { width: 22px; height: 22px; min-height: 0; accent-color: var(--accent); }

.seg { display: inline-flex; border: 1px solid var(--border); border-radius: 10px; overflow: hidden; }
.seg label { position: relative; }
.seg input { position: absolute; opacity: 0; pointer-events: none; }
.seg span { display: inline-flex; align-items: center; min-height: 44px; padding: 0 16px; cursor: pointer; font-weight: 600; }
.seg input:checked + span { background: var(--accent); color: #fff; }
.seg label + label span { border-left: 1px solid var(--border); }

details.group {
  border: 1px solid var(--border); border-radius: var(--radius);
  background: var(--surface);
}
details.group + details.group { margin-top: 8px; }
details.group > summary {
  list-style: none; cursor: pointer;
  display: flex; align-items: center; gap: 8px;
  padding: 14px 16px; font-weight: 700;
}
details.group > summary::-webkit-details-marker { display: none; }
details.group > summary::after { content: "▸"; margin-left: auto; color: var(--muted); transition: transform .15s; }
details.group[open] > summary::after { transform: rotate(90deg); }
details.group > .body { padding: 0 16px 16px; }
.badge-changed {
  font-size: .72rem; font-weight: 700; padding: 1px 8px; border-radius: 999px;
  background: var(--changed); color: var(--text); border: 1px solid var(--changed-b);
}

.form-actions {
  position: sticky; bottom: calc(var(--nav-h) + env(safe-area-inset-bottom));
  display: flex; gap: 8px; padding: 12px 0;
  background: linear-gradient(to top, var(--bg) 70%, transparent);
}
@media (min-width: 820px) { .form-actions { bottom: 0; } }

.error {
  padding: 10px 12px; border-radius: 10px;
  background: var(--bad-bg); color: var(--bad); font-weight: 600;
}
.notice {
  padding: 10px 12px; border-radius: 10px;
  background: var(--accent-bg); color: var(--text);
}

/* ── Tables ──────────────────────────────────────────────────────────────── */
.table-wrap { overflow-x: auto; -webkit-overflow-scrolling: touch; border: 1px solid var(--border); border-radius: var(--radius); background: var(--surface); }
table.data { width: 100%; border-collapse: collapse; font-size: .92rem; }
table.data th, table.data td { padding: 10px 12px; text-align: left; white-space: nowrap; border-bottom: 1px solid var(--border); }
table.data th { font-size: .78rem; text-transform: uppercase; letter-spacing: .04em; color: var(--muted); background: var(--surface-2); position: sticky; top: 0; }
table.data td.num, table.data th.num { text-align: right; font-variant-numeric: tabular-nums; }
table.data tr:last-child td { border-bottom: 0; }
table.data tr.clickable { cursor: pointer; }
table.data tr.clickable:hover td { background: var(--surface-2); }
table.data .best { color: var(--ok); font-weight: 800; }

/* ── Key/value list ──────────────────────────────────────────────────────── */
.kv { display: grid; grid-template-columns: repeat(auto-fill, minmax(150px, 1fr)); gap: 10px 16px; margin: 0; }
.kv div { min-width: 0; }
.kv dt { font-size: .78rem; color: var(--muted); font-weight: 600; }
.kv dd { margin: 0; font-weight: 650; overflow-wrap: anywhere; }

/* ── Empty state ─────────────────────────────────────────────────────────── */
.empty { text-align: center; padding: 32px 16px; color: var(--muted); }
.empty .btn { margin-top: 12px; }

/* ── Auth pages ──────────────────────────────────────────────────────────── */
.auth { min-height: 100vh; display: grid; place-items: center; padding: 16px; }
.auth .card { width: 100%; max-width: 420px; padding: 24px; }
.auth .brand { justify-content: center; margin-bottom: 16px; font-size: 1.2rem; }
body.auth-body { padding-bottom: 0; }
.auth .card.install { max-width: 520px; }
.install .seg { align-self: center; }
.steps { margin: 0; padding-left: 1.4rem; display: flex; flex-direction: column; gap: 10px; }
.steps li { padding-left: 4px; }
