/* Kart Club — mobile-first styles. One file, no framework. */

:root {
  --bg: #f4f2ee;
  --surface: #ffffff;
  --surface-2: #f0ede7;
  --text: #1b1d22;
  --muted: #667085;
  --line: #e2ddd3;
  --accent: #c81e2a;
  --accent-ink: #ffffff;
  --ok: #138a4b;
  --ok-bg: #e5f6ec;
  --warn: #a35a00;
  --warn-bg: #fff3dc;
  --bad: #c0262d;
  --gold: #e8a400;
  --radius: 14px;
  --tap: 46px;
  --shadow: 0 1px 2px rgb(0 0 0 / 0.06), 0 4px 14px rgb(0 0 0 / 0.05);
  --font: system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #111318;
    --surface: #1a1d24;
    --surface-2: #232731;
    --text: #eceef3;
    --muted: #98a0b0;
    --line: #2d323d;
    --accent: #ef4250;
    --ok: #3ccf82;
    --ok-bg: #12301f;
    --warn: #f5b04a;
    --warn-bg: #33270f;
    --bad: #ff6b70;
    --shadow: none;
    color-scheme: dark;
  }
}

*, *::before, *::after { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 16px/1.5 var(--font);
  padding-bottom: calc(72px + var(--safe-bottom));
  overscroll-behavior-y: none;
}
img, canvas { max-width: 100%; height: auto; display: block; }
a { color: var(--accent); }
h1, h2, h3 { line-height: 1.2; margin: 0 0 0.5rem; }
h2 { font-size: 1.1rem; margin-top: 1.5rem; }
h3 { font-size: 1rem; }
p { margin: 0.4rem 0; }
code { background: var(--surface-2); padding: 0.1em 0.35em; border-radius: 6px; font-size: 0.9em; }

.skip { position: absolute; left: -999px; }
.skip:focus { left: 8px; top: 8px; z-index: 50; background: var(--surface); padding: 8px 12px; }
.visually-hidden { position: absolute !important; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; }

/* ---------- layout ---------- */
.topbar {
  position: sticky; top: 0; z-index: 20;
  display: flex; align-items: center; gap: 0.5rem;
  padding: calc(10px + env(safe-area-inset-top, 0px)) 16px 10px;
  background: var(--accent); color: var(--accent-ink);
}
.brand { color: inherit; text-decoration: none; font-weight: 800; letter-spacing: 0.01em; font-size: 1.1rem; }
main { max-width: 760px; margin: 0 auto; padding: 16px; outline: none; }
.page-title { font-size: 1.5rem; margin: 0.25rem 0 0.75rem; outline: none; }

.tabbar {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 30;
  display: flex; justify-content: space-around;
  background: var(--surface); border-top: 1px solid var(--line);
  padding-bottom: var(--safe-bottom);
}
.tabbar a {
  flex: 1; min-height: 60px; display: flex; flex-direction: column; align-items: center; justify-content: center;
  color: var(--muted); text-decoration: none; font-size: 0.75rem; font-weight: 600;
}
.tabbar a.active { color: var(--accent); }
.tab-icon { font-size: 1.3rem; line-height: 1.2; }

@media (min-width: 900px) {
  body { padding-bottom: 0; }
  .tabbar { position: sticky; top: 0; bottom: auto; max-width: 760px; margin: 0 auto; border: 0; background: transparent; justify-content: flex-start; gap: 4px; }
  .tabbar a { flex: 0 0 auto; flex-direction: row; gap: 6px; min-height: 44px; padding: 0 14px; font-size: 0.9rem; }
}

/* ---------- surfaces ---------- */
.card {
  background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius);
  padding: 16px; margin: 12px 0; box-shadow: var(--shadow);
}
.card.tight { padding: 10px 12px; }
.card > h2:first-child { margin-top: 0; }
section { margin: 12px 0; }
.stack > * + * { margin-top: 10px; }
.grid-2 { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.row-between { display: flex; justify-content: space-between; gap: 8px; flex-wrap: wrap; }
.btn-row { display: flex; flex-wrap: wrap; gap: 8px; margin-top: 8px; }
.btn-row.tight { margin: 0; flex-wrap: nowrap; }

.muted { color: var(--muted); }
.small { font-size: 0.875rem; }
.ok-text { color: var(--ok); }
.warn-text { color: var(--warn); }

/* ---------- forms ---------- */
label { display: block; font-weight: 600; font-size: 0.9rem; margin: 10px 0 4px; }
label.check { display: flex; align-items: center; gap: 8px; font-weight: 500; }
input, select, textarea {
  width: 100%; min-height: var(--tap);
  font: inherit; font-size: 16px; /* ≥16px stops iOS zoom-on-focus */
  color: var(--text); background: var(--surface);
  border: 1.5px solid var(--line); border-radius: 10px; padding: 10px 12px;
}
input[type="checkbox"] { width: 22px; min-height: 22px; accent-color: var(--accent); }
input:focus-visible, select:focus-visible, button:focus-visible, a:focus-visible, summary:focus-visible {
  outline: 3px solid color-mix(in srgb, var(--accent) 55%, transparent); outline-offset: 2px;
}
input:disabled { opacity: 0.7; }
.code-input { font-size: 2rem; letter-spacing: 0.4em; text-align: center; font-variant-numeric: tabular-nums; }

/* ---------- buttons ---------- */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  min-height: var(--tap); padding: 0 16px;
  font: inherit; font-weight: 650; text-decoration: none;
  color: var(--text); background: var(--surface-2);
  border: 1.5px solid var(--line); border-radius: 12px; cursor: pointer;
  -webkit-tap-highlight-color: transparent; touch-action: manipulation;
}
.btn.primary { background: var(--accent); color: var(--accent-ink); border-color: transparent; }
.btn.block { display: flex; width: 100%; margin-top: 12px; }
.btn.big { min-height: 54px; font-size: 1.05rem; }
.btn.small { min-height: 38px; padding: 0 10px; font-size: 0.85rem; }
.btn.danger { color: var(--bad); }
.btn.link { background: none; border: 0; padding: 0; min-height: 36px; color: var(--accent); text-decoration: underline; font-weight: 600; }
.btn.danger-link { background: none; border: 0; color: var(--bad); margin-top: 24px; padding: 0; text-decoration: underline; }
.btn:disabled { opacity: 0.45; cursor: not-allowed; }
.btn.is-busy { position: relative; color: transparent !important; }
.btn.is-busy::after {
  content: ""; position: absolute; width: 18px; height: 18px; border-radius: 50%;
  border: 2.5px solid var(--accent-ink); border-right-color: transparent; animation: spin 0.8s linear infinite;
}
.btn:not(.primary).is-busy::after { border-color: var(--accent); border-right-color: transparent; }
.icon-btn {
  flex: 0 0 var(--tap); height: var(--tap); border-radius: 10px; border: 1.5px solid var(--line);
  background: var(--surface); color: var(--muted); font-size: 1rem; cursor: pointer;
}
details > summary { cursor: pointer; font-weight: 650; list-style: none; min-height: 36px; display: flex; align-items: center; }
details > summary::-webkit-details-marker { display: none; }
details > summary::after { content: "▾"; margin-left: 6px; color: var(--muted); }
details[open] > summary::after { content: "▴"; }
.collapsible details > summary.btn::after { content: ""; }
.collapsible details[open] > summary { margin-bottom: 8px; }

/* ---------- segmented control ---------- */
.segmented {
  display: grid; grid-auto-flow: column; grid-auto-columns: 1fr; gap: 4px;
  background: var(--surface-2); border-radius: 12px; padding: 4px; margin: 8px 0 12px;
}
.segmented button {
  min-height: 42px; border: 0; border-radius: 9px; background: transparent; color: var(--muted);
  font: inherit; font-weight: 650; font-size: 0.9rem; padding: 0 6px; cursor: pointer;
}
.segmented button.on { background: var(--surface); color: var(--text); box-shadow: var(--shadow); }
.segmented.small button { font-size: 0.85rem; }

/* ---------- lists ---------- */
.list { display: grid; gap: 8px; }
.list-item {
  display: flex; align-items: center; justify-content: space-between; gap: 12px;
  min-height: 64px; padding: 12px 14px; color: inherit; text-decoration: none;
  background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius);
}
.list-item:not(.static):active { transform: scale(0.99); }
.li-title { font-weight: 700; overflow-wrap: anywhere; }
.li-sub { color: var(--muted); font-size: 0.85rem; }
.pill { flex: 0 0 auto; font-size: 0.75rem; font-weight: 700; padding: 4px 10px; border-radius: 999px; background: var(--surface-2); }
.pill.ok { background: var(--ok-bg); color: var(--ok); }
.pill.warn { background: var(--warn-bg); color: var(--warn); }
.tag { margin-left: 6px; font-size: 0.7rem; font-weight: 700; padding: 1px 6px; border-radius: 6px; background: var(--surface-2); color: var(--muted); }

.empty { text-align: center; padding: 32px 16px; color: var(--muted); }
.empty-title { font-weight: 700; color: var(--text); font-size: 1.05rem; }

/* ---------- banners & issues ---------- */
.banner { border-radius: 12px; padding: 10px 12px; margin: 8px 0; font-size: 0.92rem; }
.banner.ok { background: var(--ok-bg); color: var(--ok); }
.banner.warn { background: var(--warn-bg); color: var(--warn); }
.issues { margin: 8px 0; padding-left: 1.2rem; color: var(--bad); font-size: 0.9rem; }

/* ---------- karts / score entry ---------- */
.kart { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); padding: 10px; margin: 8px 0; }
.kart-head { display: flex; align-items: center; gap: 8px; margin-bottom: 8px; }
.kart-no { flex: 0 0 28px; height: 28px; border-radius: 50%; background: var(--surface-2); display: grid; place-items: center; font-weight: 800; font-size: 0.85rem; }
.icon-crop canvas { width: 36px; height: 36px; border-radius: 8px; }
.hint { font-size: 0.8rem; color: var(--muted); margin: 6px 0 0; }

.totals { display: grid; gap: 6px; margin-top: 8px; }
.total-row {
  display: grid; grid-template-columns: 1fr minmax(120px, 46%); align-items: center; gap: 10px;
  margin: 0; font-weight: 600; padding: 4px 0; border-bottom: 1px solid var(--line);
}
.total-row span { overflow-wrap: anywhere; }
.total-row input { text-align: right; font-variant-numeric: tabular-nums; }
.total-row.bad select { border-color: var(--bad); }

.summary { margin-top: 16px; }
.field-strength { margin: 10px 0 0; }
.field-strength .muted { display: block; margin-top: 2px; }
.check-panel { border-left: 4px solid var(--accent); scroll-margin-top: 70px; }
.check-panel h2 { margin-top: 0; }
.check-status { margin: 8px 0 0; font-weight: 700; }
.check-status.todo { color: var(--bad); }
.check-status.ok { color: var(--ok); }
.confirm-hint {
  display: block; width: 100%; margin: 0 0 6px; padding: 8px 10px; border: 0; border-radius: 10px;
  background: var(--surface); color: var(--bad); font: inherit; font-size: 0.88rem; font-weight: 700; text-align: left;
  box-shadow: 0 0 0 1px var(--line); cursor: pointer;
}
.confirm-hint[hidden] { display: none; }
input.needs { border-color: var(--bad); box-shadow: 0 0 0 2px color-mix(in srgb, var(--bad) 25%, transparent); }
.sticky-action {
  position: sticky; bottom: calc(68px + var(--safe-bottom)); z-index: 10;
  padding: 8px 0; background: linear-gradient(to top, var(--bg) 70%, transparent);
}
@media (min-width: 900px) { .sticky-action { bottom: 0; } }

/* ---------- photo upload ---------- */
.photo-grid { display: grid; gap: 10px; grid-template-columns: 1fr; }
.photo-grid.n4 { grid-template-columns: 1fr 1fr; }
.photo-slot {
  position: relative; display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 6px;
  min-height: 140px; margin: 0; padding: 10px; text-align: center; cursor: pointer;
  border: 2px dashed var(--line); border-radius: var(--radius); background: var(--surface);
  font-weight: 600; font-size: 0.85rem; color: var(--muted); overflow: hidden;
}
.photo-slot.has { border-style: solid; border-color: var(--ok); color: var(--ok); }
.photo-slot img { width: 100%; max-height: 160px; object-fit: cover; border-radius: 8px; }
.photo-slot:focus-within { outline: 3px solid color-mix(in srgb, var(--accent) 55%, transparent); }
.slot-plus { font-size: 2.4rem; line-height: 1; color: var(--accent); }
.progress { position: relative; height: 34px; margin-top: 10px; border-radius: 10px; background: var(--surface-2); overflow: hidden; }
.progress .bar { position: absolute; inset: 0 auto 0 0; width: 0; background: color-mix(in srgb, var(--accent) 35%, transparent); transition: width 0.2s; }
.progress-label { position: relative; display: block; line-height: 34px; padding: 0 10px; font-size: 0.85rem; font-weight: 600; }
.analysis-canvas { width: 100%; border-radius: 8px; margin: 8px 0; }
.photo-strip { display: flex; gap: 8px; overflow-x: auto; scroll-snap-type: x mandatory; padding-bottom: 6px; }
.photo-strip a { flex: 0 0 80%; scroll-snap-align: start; }
.photo-strip img { border-radius: 10px; }

/* ---------- tables ---------- */
.table-wrap { overflow-x: auto; -webkit-overflow-scrolling: touch; margin: 8px 0; border: 1px solid var(--line); border-radius: 12px; background: var(--surface); }
table { width: 100%; border-collapse: collapse; font-size: 0.9rem; font-variant-numeric: tabular-nums; }
th, td { padding: 9px 10px; text-align: left; white-space: nowrap; border-bottom: 1px solid var(--line); }
thead th { font-size: 0.75rem; text-transform: uppercase; letter-spacing: 0.04em; color: var(--muted); background: var(--surface-2); }
tbody tr:last-child > * { border-bottom: 0; }
table.sticky-first tbody th, table.sticky-first thead th:first-child {
  position: sticky; left: 0; background: var(--surface); z-index: 1; font-weight: 650;
  box-shadow: 1px 0 0 var(--line);
}
table.sticky-first thead th:first-child { background: var(--surface-2); }
.results td:last-child, .results th:last-child { text-align: right; }
.place { font-weight: 700; }
.track-list { color: var(--muted); font-size: 0.85rem; padding-left: 1.4rem; }

.char-badge { display: inline-flex; align-items: center; gap: 6px; }
.char-dot { width: 24px; height: 24px; border-radius: 50%; display: inline-grid; place-items: center; color: #fff; font-size: 0.65rem; font-weight: 800; }
@media (max-width: 420px) { .char-name { display: none; } }

/* ---------- stats ---------- */
.h2h { text-align: center; padding: 8px 0; }
.h2h-score { display: grid; grid-template-columns: 1fr auto 1fr; align-items: center; gap: 10px; font-weight: 700; }
.h2h-score strong { font-size: 2rem; color: var(--accent); }
.bw-grid { display: grid; gap: 10px; grid-template-columns: repeat(auto-fill, minmax(220px, 1fr)); }
.bw { background: var(--surface-2); border-radius: 12px; padding: 10px 12px; }

/* ---------- login ---------- */
.hero { text-align: center; padding: 28px 0 8px; }
.hero h1 { font-size: 2rem; }
.hero-mark { font-size: 3rem; }
.login { max-width: 420px; margin: 0 auto; }

/* ---------- feedback ---------- */
.loading { display: flex; align-items: center; gap: 10px; color: var(--muted); padding: 24px 0; }
.spin { width: 20px; height: 20px; border-radius: 50%; border: 3px solid var(--line); border-top-color: var(--accent); animation: spin 0.8s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }
/* Toasts drop in under the header, so they never cover the confirm button. */
.toasts { position: fixed; left: 12px; right: 12px; top: calc(60px + env(safe-area-inset-top, 0px)); z-index: 60; display: grid; gap: 8px; justify-items: center; pointer-events: none; }
.toast {
  max-width: 520px; padding: 12px 16px; border-radius: 12px; background: #1b1d22; color: #fff;
  font-weight: 600; font-size: 0.92rem; box-shadow: 0 6px 24px rgb(0 0 0 / 0.25); transition: opacity 0.4s, transform 0.4s;
}
.toast-error { background: var(--bad); }
.toast.out { opacity: 0; transform: translateY(-8px); }

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation-duration: 0.01ms !important; transition-duration: 0.01ms !important; }
}
@media (max-width: 360px) {
  main { padding: 12px; }
  .grid-2 { grid-template-columns: 1fr; }
  .photo-grid.n4 { grid-template-columns: 1fr; }
}
.subhead { margin: 18px 0 6px; font-size: 1rem; }
.pill.live { background: var(--accent); color: var(--accent-ink); position: relative; padding-left: 20px; }
.pill.live::before { content: ""; position: absolute; left: 8px; top: 50%; width: 7px; height: 7px; margin-top: -3.5px; border-radius: 50%; background: currentColor; animation: pulse 1.6s ease-in-out infinite; }
@keyframes pulse { 50% { opacity: 0.25; } }
.banner.live { background: color-mix(in srgb, var(--accent) 12%, var(--surface)); color: var(--text); }
.banner .btn.small { margin-left: 4px; vertical-align: middle; }
.live-card { border-color: color-mix(in srgb, var(--accent) 45%, var(--line)); }
.live-title { font-weight: 800; font-size: 1.1rem; color: inherit; text-decoration: none; }
