/* ===== Tokens ===================================================== */
:root {
  color-scheme: light;
  --bg:             #f4f3ef;
  --surface-1:      #fcfcfb;
  --surface-2:      #f0efeb;
  --border:         #e2e0da;
  --text-primary:   #0b0b0b;
  --text-secondary: #52514e;
  --text-muted:     #7a7975;
  --grid:           #e9e7e2;

  /* categorical slots 1-3 (validated all-pairs) */
  --series-1: #2a78d6;  /* Club Championship / primary */
  --series-2: #eb6834;  /* Up and Running / upcoming */
  --series-3: #1baf7a;  /* Other races */
  --series-4: #eda100;  /* parkrun */

  --accent:        #2a78d6;
  --accent-ink:    #ffffff;
  --accent-soft:   #e3eefb;
  --warn-bg:       #fff4dc;
  --warn-ink:      #6b4a00;
  --danger:        #c43b3a;
  --good:          #0d7a3e;

  --radius: 12px;
  --shadow: 0 1px 2px rgb(0 0 0 / .04), 0 2px 8px rgb(0 0 0 / .04);
  --font: ui-sans-serif, system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  --mono: ui-monospace, "SF Mono", Menlo, Consolas, monospace;
}

/* Dark theme: follows the OS unless the user picks Light/Dark in Account → Appearance */
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    color-scheme: dark;
    --bg:             #121211;
    --surface-1:      #1a1a19;
    --surface-2:      #242422;
    --border:         #33332f;
    --text-primary:   #f0efec;
    --text-secondary: #c3c2b7;
    --text-muted:     #92918a;
    --grid:           #2b2b28;
    --series-1: #3987e5;
    --series-2: #d95926;
    --series-3: #199e70;
    --series-4: #c98500;
    --accent:        #3987e5;
    --accent-soft:   #1d2d42;
    --warn-bg:       #3a2f12;
    --warn-ink:      #f3d68c;
    --danger:        #e66767;
    --good:          #4fc27f;
    --shadow: none;
  }
}
:root[data-theme="dark"] {
  color-scheme: dark;
  --bg:             #121211;
  --surface-1:      #1a1a19;
  --surface-2:      #242422;
  --border:         #33332f;
  --text-primary:   #f0efec;
  --text-secondary: #c3c2b7;
  --text-muted:     #92918a;
  --grid:           #2b2b28;
  --series-1: #3987e5;
  --series-2: #d95926;
  --series-3: #199e70;
  --series-4: #c98500;
  --accent:        #3987e5;
  --accent-soft:   #1d2d42;
  --warn-bg:       #3a2f12;
  --warn-ink:      #f3d68c;
  --danger:        #e66767;
  --good:          #4fc27f;
  --shadow: none;
}

/* ===== Base ======================================================= */
* { box-sizing: border-box; }
html, body { margin: 0; }
/* Safety net: nothing can make the page wider than the screen (no sideways sliding on phones). */
html, body { overflow-x: clip; max-width: 100%; }
body {
  font: 15px/1.5 var(--font);
  color: var(--text-primary);
  background: var(--bg);
  -webkit-font-smoothing: antialiased;
}
h1, h2 { margin: 0; line-height: 1.25; }
h1 { font-size: 1.15rem; font-weight: 700; letter-spacing: -.01em; }
h2 { font-size: 1rem; font-weight: 650; }
a { color: var(--accent); }
.muted { color: var(--text-secondary); margin: 2px 0 0; font-size: .875rem; }
.small { font-size: .8rem; }
.sr-only { position: absolute; top: 0; left: 0; width: 1px; height: 1px; margin: -1px; padding: 0; border: 0; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; }
.grow { flex: 1; }
[hidden] { display: none !important; }
body:not(.is-admin) .admin-only { display: none !important; }

/* ===== Buttons & inputs =========================================== */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  font: inherit; font-weight: 600; font-size: .875rem;
  padding: 8px 14px; border-radius: 8px; border: 1px solid transparent;
  cursor: pointer; text-decoration: none; white-space: nowrap;
  transition: background .15s, border-color .15s;
}
.btn:disabled { opacity: .6; cursor: progress; }
.btn-primary { background: var(--accent); color: var(--accent-ink); }
.btn-primary:hover { filter: brightness(1.08); }
.btn-ghost { background: transparent; color: var(--text-primary); border-color: var(--border); }
.btn-ghost:hover { background: var(--surface-2); }
.btn-danger-ghost { background: transparent; color: var(--danger); border-color: var(--border); }
.btn-danger-ghost:hover { background: color-mix(in srgb, var(--danger) 10%, transparent); }
.btn-block { width: 100%; }
.btn-danger { background: color-mix(in srgb, var(--danger) 82%, #000); color: #fff; }
.btn-danger:hover { filter: brightness(1.08); }
.danger-zone { border-color: color-mix(in srgb, var(--danger) 45%, var(--border)); }
.icon-btn {
  background: none; border: 0; font-size: 1.5rem; line-height: 1; cursor: pointer;
  color: var(--text-secondary); padding: 4px 8px; border-radius: 6px;
}
.icon-btn:hover { background: var(--surface-2); }

input, select, textarea {
  font: inherit; color: var(--text-primary);
  background: var(--surface-1);
  border: 1px solid var(--border); border-radius: 8px;
  padding: 8px 10px; width: 100%;
}
input:focus-visible, select:focus-visible, textarea:focus-visible, .btn:focus-visible, .tab:focus-visible, .chip:focus-visible {
  outline: 2px solid var(--accent); outline-offset: 1px;
}
label { display: flex; flex-direction: column; gap: 4px; font-size: .8rem; font-weight: 600; color: var(--text-secondary); }
label > input, label > select, label > textarea { font-weight: 400; font-size: .9rem; }
.form-error { color: var(--danger); font-size: .85rem; margin: 8px 0 0; }
.form-msg { font-size: .85rem; margin: 0; min-height: 1.2em; }
.form-msg.ok { color: var(--good); }
.form-msg.err { color: var(--danger); }
.stack { display: flex; flex-direction: column; gap: 12px; }
.row { display: flex; gap: 10px; align-items: end; flex-wrap: wrap; }

/* ===== Brand ====================================================== */
.brand { display: flex; align-items: center; gap: 10px; }
.brand-mark {
  width: 28px; height: 28px; border-radius: 8px; flex: none;
  background:
    linear-gradient(135deg, transparent 46%, var(--surface-1) 46% 54%, transparent 54%),
    var(--accent);
}

/* ===== Top bar ==================================================== */
.topbar {
  position: sticky; top: 0; z-index: 10;
  background: color-mix(in srgb, var(--surface-1) 92%, transparent);
  backdrop-filter: blur(8px);
  border-bottom: 1px solid var(--border);
}
.topbar-inner {
  max-width: 1200px; margin: 0 auto; padding: 10px 20px;
  display: flex; align-items: center; gap: 20px; flex-wrap: wrap;
}
.tabs { display: flex; gap: 2px; flex: 1; }
.tab {
  font: inherit; font-size: .9rem; font-weight: 600; color: var(--text-secondary);
  background: none; border: 0; padding: 8px 12px; border-radius: 8px; cursor: pointer;
}
.tab:hover { color: var(--text-primary); background: var(--surface-2); }
.tab[aria-selected="true"] { color: var(--accent); background: var(--accent-soft); }
.topbar-actions { display: flex; gap: 8px; align-items: center; }
.season-select select { width: auto; padding: 7px 10px; font-weight: 600; }

/* ===== Layout ===================================================== */
.page { max-width: 1200px; margin: 0 auto; padding: 20px; }
.panel { display: flex; flex-direction: column; gap: 16px; }
.grid-2 { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 16px; }
.card {
  background: var(--surface-1); border: 1px solid var(--border);
  border-radius: var(--radius); box-shadow: var(--shadow);
  padding: 18px 20px;
}
.card-head { display: flex; justify-content: space-between; align-items: flex-start; gap: 12px; margin-bottom: 12px; flex-wrap: wrap; }
.chart-box { position: relative; width: 100%; }
.h-220 { height: 220px; } .h-260 { height: 260px; } .h-300 { height: 300px; }

/* ===== KPI tiles ================================================== */
.kpis { display: grid; grid-template-columns: repeat(5, minmax(0, 1fr)); gap: 12px; }
.kpi {
  background: var(--surface-1); border: 1px solid var(--border); border-radius: var(--radius);
  padding: 14px 16px; box-shadow: var(--shadow);
}
.kpi-label { font-size: .78rem; font-weight: 600; color: var(--text-secondary); text-transform: uppercase; letter-spacing: .04em; }
.kpi-value { font-size: 1.9rem; font-weight: 700; letter-spacing: -.02em; line-height: 1.15; margin-top: 4px; font-variant-numeric: tabular-nums; }
.kpi-sub { font-size: .8rem; color: var(--text-muted); margin-top: 2px; }

/* ===== Personal records ========================================= */
.records { display: grid; grid-template-columns: repeat(5, minmax(0, 1fr)); gap: 10px; }
.record {
  border: 1px solid var(--border); border-radius: 10px; padding: 12px 14px;
  background: var(--surface-2); display: flex; flex-direction: column; gap: 2px; min-width: 0;
}
.record-dist { font-size: .78rem; font-weight: 650; color: var(--text-secondary); text-transform: uppercase; letter-spacing: .04em; }
.record-time { font-size: 1.6rem; font-weight: 700; letter-spacing: -.02em; font-variant-numeric: tabular-nums; line-height: 1.2; }
.record-meta { font-size: .78rem; color: var(--text-secondary); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.record-meta.pace { color: var(--text-muted); }
.record.is-empty .record-time { color: var(--text-muted); font-size: 1.1rem; font-weight: 600; padding: 6px 0 4px; }
.record .new-badge {
  align-self: flex-start; margin-top: 4px; font-size: .68rem; font-weight: 700; padding: 1px 7px; border-radius: 999px;
  background: var(--accent-soft); color: var(--accent);
}

/* ===== Legend ===================================================== */
.legend { display: flex; gap: 14px; flex-wrap: wrap; font-size: .8rem; color: var(--text-secondary); }
.legend-item { display: inline-flex; align-items: center; gap: 6px; }
.swatch { width: 10px; height: 10px; border-radius: 50%; flex: none; }
.swatch.hollow { background: transparent !important; border: 2px solid; }
.swatch.square { border-radius: 2px; }
.swatch.tri { border-radius: 0; clip-path: polygon(50% 0, 100% 100%, 0 100%); }
.swatch.diamond { border-radius: 1px; transform: rotate(45deg) scale(.85); }

/* ===== Alerts ===================================================== */
.alert {
  background: var(--warn-bg); color: var(--warn-ink);
  border-radius: var(--radius); padding: 12px 16px; font-size: .9rem;
  display: flex; gap: 10px; align-items: flex-start;
}
.alert::before { content: "⚠"; font-weight: 700; }
.alert a { color: inherit; font-weight: 600; }

/* ===== Chips ====================================================== */
.chips { display: flex; flex-wrap: wrap; gap: 6px; margin-bottom: 14px; }
.chip {
  font: inherit; font-size: .82rem; font-weight: 600;
  padding: 5px 12px; border-radius: 999px; cursor: pointer;
  background: var(--surface-2); color: var(--text-secondary); border: 1px solid transparent;
}
.chip[aria-pressed="true"] { background: var(--accent-soft); color: var(--accent); border-color: var(--accent); }
.chip .count { opacity: .7; font-weight: 500; margin-left: 4px; }

.empty {
  text-align: center; color: var(--text-secondary); padding: 28px 16px;
  border: 1px dashed var(--border); border-radius: 10px; margin: 0;
}
.empty p { margin: 6px auto 0; max-width: 520px; }

/* ===== Tables ===================================================== */
.filters { display: flex; gap: 8px; flex-wrap: wrap; margin-bottom: 12px; }
.filters input[type="search"] { flex: 1; min-width: 200px; }
.filters select { width: auto; }
.table-wrap { overflow-x: auto; position: relative; }
.data-table { width: 100%; border-collapse: collapse; font-size: .875rem; }
.data-table th {
  text-align: left; font-size: .75rem; font-weight: 650; text-transform: uppercase; letter-spacing: .04em;
  color: var(--text-secondary); padding: 8px 10px; border-bottom: 1px solid var(--border); white-space: nowrap;
}
.data-table td { padding: 9px 10px; border-bottom: 1px solid var(--grid); vertical-align: middle; }
.data-table tbody tr:hover { background: var(--surface-2); }
.data-table .num { text-align: right; font-variant-numeric: tabular-nums; }
.data-table tr.is-upcoming td { color: var(--text-secondary); }
.data-table td.event { font-weight: 600; }
.data-table .nowrap { white-space: nowrap; }
.data-table .muted.small { margin: 0; }
.data-table .gain-pos { color: var(--good); font-weight: 650; }
.cat-dot { display: inline-flex; align-items: center; gap: 6px; white-space: nowrap; }
.cat-dot::before { content: ""; width: 8px; height: 8px; border-radius: 50%; background: var(--dot); }
.badge {
  display: inline-block; font-size: .72rem; font-weight: 650; padding: 2px 8px; border-radius: 999px;
  background: var(--surface-2); color: var(--text-secondary); white-space: nowrap;
}
.badge.done { background: color-mix(in srgb, var(--good) 14%, transparent); color: var(--good); }
.badge.next { background: var(--accent-soft); color: var(--accent); }
.badge.warn { background: var(--warn-bg); color: var(--warn-ink); }
.pb-flag { font-size: .7rem; font-weight: 700; color: var(--accent); margin-left: 4px; }
.add-time {
  font: inherit; font-size: .78rem; color: var(--accent); background: none; border: 1px dashed var(--border);
  border-radius: 6px; padding: 1px 8px; cursor: pointer; white-space: nowrap;
}
.row-edit { font: inherit; font-size: .8rem; background: none; border: 0; color: var(--accent); cursor: pointer; font-weight: 600; }

/* ===== Users ====================================================== */
.user-list { list-style: none; margin: 0 0 16px; padding: 0; }
.user-list li { display: flex; align-items: center; gap: 10px; padding: 10px 0; border-bottom: 1px solid var(--grid); }
.user-list .user-main { flex: 1; min-width: 0; }
.user-list .user-main .muted { margin: 2px 0 0; }
.user-list [title] { cursor: help; text-decoration: underline dotted var(--border); text-underline-offset: 3px; }

/* ===== Dialog ===================================================== */
dialog {
  border: 0; padding: 0; border-radius: 14px; width: min(620px, calc(100vw - 24px));
  background: var(--surface-1); color: var(--text-primary);
  box-shadow: 0 20px 60px rgb(0 0 0 / .25);
}
dialog::backdrop { background: rgb(0 0 0 / .4); }
.dialog-head, .dialog-foot { display: flex; align-items: center; gap: 8px; padding: 14px 20px; }
.dialog-head { justify-content: space-between; border-bottom: 1px solid var(--border); }
.dialog-foot { border-top: 1px solid var(--border); }
.dialog-body { padding: 16px 20px; max-height: 70vh; overflow-y: auto; }
.form-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 12px 14px; }
.form-grid .span-2 { grid-column: span 2; }
fieldset { border: 0; padding: 0; margin: 0; display: flex; gap: 6px; flex-wrap: wrap; }
legend { font-size: .8rem; font-weight: 600; color: var(--text-secondary); margin-bottom: 4px; width: 100%; }
.radio {
  flex-direction: row; align-items: center; gap: 6px; font-weight: 500; font-size: .85rem; color: var(--text-primary);
  border: 1px solid var(--border); border-radius: 8px; padding: 7px 12px; cursor: pointer;
}
.radio input { width: auto; margin: 0; accent-color: var(--accent); }
.radio:has(input:checked) { border-color: var(--accent); background: var(--accent-soft); }

/* ===== Toast ====================================================== */
.toast {
  position: fixed; bottom: 20px; left: 50%; transform: translateX(-50%);
  background: var(--text-primary); color: var(--surface-1);
  padding: 10px 16px; border-radius: 10px; font-size: .875rem; font-weight: 600; z-index: 50;
}

/* ===== Login ====================================================== */
.login-page { min-height: 100vh; display: grid; place-items: center; padding: 20px; }
.login-wrap { display: flex; gap: 20px; align-items: flex-start; justify-content: center; width: min(1100px, 100%); }
.login-wrap .login-card { flex: none; }
.public-records { flex: 1; min-width: 0; }
.public-records .records { grid-template-columns: repeat(5, minmax(0, 1fr)); }
.public-records .record-time { font-size: 1.4rem; }
@media (max-width: 860px) {
  .login-wrap { flex-direction: column; align-items: center; }
  .login-wrap .login-card, .public-records { width: min(520px, 100%); }
  .public-records .records { grid-template-columns: repeat(2, minmax(0, 1fr)); }
}
.login-card {
  width: min(380px, 100%); align-self: flex-start; background: var(--surface-1); border: 1px solid var(--border);
  border-radius: 16px; padding: 28px; box-shadow: var(--shadow);
}
.login-card .brand { margin-bottom: 22px; }
.login-card form { display: flex; flex-direction: column; gap: 14px; }

/* ===== Responsive ================================================= */
@media (max-width: 900px) {
  .kpis { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .records { grid-template-columns: repeat(3, minmax(0, 1fr)); }
  .grid-2 { grid-template-columns: 1fr; }
  .tabs { order: 3; flex-basis: 100%; overflow-x: auto; }
  .topbar-actions { margin-left: auto; }
}
@media (max-width: 560px) {
  .page { padding: 12px; }
  .form-grid { grid-template-columns: 1fr; }
  .form-grid .span-2 { grid-column: auto; }
  .kpi-value { font-size: 1.5rem; }
  .records { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .record-time { font-size: 1.35rem; }
  .card { padding: 14px; }
  /* taller plots so vertical axis labels don't eat the drawing area */
  .h-220 { height: 270px; } .h-260 { height: 300px; } .h-300 { height: 330px; }
}
#pb-table td, #pb-table th { white-space: nowrap; }

/* ===== Appearance switch ========================================= */
.segmented { display: inline-flex; border: 1px solid var(--border); border-radius: 10px; padding: 3px; gap: 2px; background: var(--surface-2); }
.segmented label {
  flex-direction: row; align-items: center; gap: 6px; cursor: pointer;
  font-size: .875rem; font-weight: 600; color: var(--text-secondary);
  padding: 7px 14px; border-radius: 8px;
}
.segmented input { position: absolute; opacity: 0; pointer-events: none; width: auto; }
.segmented label:has(input:checked) { background: var(--accent-soft); color: var(--accent); box-shadow: inset 0 0 0 1px var(--accent); }
.segmented label:has(input:focus-visible) { outline: 2px solid var(--accent); outline-offset: 1px; }
@media (max-width: 560px) {
  #clear-form > label { flex-basis: 100%; }
  #clear-form > .btn { flex: 1; }
}

.check { flex-direction: row; align-items: center; gap: 10px; font-weight: 500; font-size: .9rem; color: var(--text-primary); cursor: pointer; }
.check input { width: 18px; height: 18px; margin: 0; accent-color: var(--accent); flex: none; }
.check small { display: block; color: var(--text-secondary); font-size: .8rem; }
.check.is-disabled { opacity: .5; }

/* legend key for line charts: short line with the marker on it */
.line-key { position: relative; display: inline-flex; align-items: center; justify-content: center; width: 40px; height: 12px; }
.line-key::before { content: ""; position: absolute; left: 0; right: 0; top: 50%; border-top: 2px solid var(--series-4); transform: translateY(-50%); }
.line-key.dashed::before { border-top-style: dashed; }
.line-key .swatch { position: relative; box-shadow: 0 0 0 2px var(--surface-1); }

/* ===== Races list as stacked cards on phones ====================== */
@media (max-width: 640px) {
  #race-table thead { display: none; }
  #race-table, #race-table tbody { display: block; width: 100%; }
  #race-table tr {
    display: flex; flex-wrap: wrap; align-items: center; gap: 4px 10px;
    padding: 12px 2px; border-bottom: 1px solid var(--grid);
  }
  #race-table tbody tr:hover { background: none; }
  #race-table td { border: 0; padding: 0; text-align: left; }
  #race-table td:empty { display: none; }
  #race-table .c-date   { order: 1; flex: 1; font-size: .8rem; color: var(--text-secondary); }
  #race-table .c-status { order: 2; }
  #race-table .c-event  { order: 3; flex-basis: 100%; font-size: .95rem; }
  #race-table .c-loc    { order: 4; font-size: .82rem; color: var(--text-secondary); }
  #race-table .c-type   { order: 5; font-size: .82rem; }
  #race-table .c-dist   { order: 6; font-size: .82rem; }
  #race-table .c-dist .muted.small { display: inline; margin-left: 4px; }
  #race-table .c-time   { order: 7; font-weight: 650; }
  #race-table .c-pos    { order: 8; font-weight: 650; }
  #race-table .c-act    { order: 9; margin-left: auto; }
  #race-table .c-act .row-edit { padding: 4px 8px; }
  #race-table td[colspan] { flex-basis: 100%; }
}
