/* Motorstorm leaderboards — base stylesheet.
 *
 * One sheet, themed via CSS custom properties. Game themes set the
 * colour variables; light/dark mode is layered on top via prefers-color-scheme.
 *
 * Replaces the legacy joban123.css / msa.css / msmv.css trio that was inlined
 * into every page by util.php:701.
 */

/* ---- Theme tokens ---- */

:root {
  --c-bg: #ffffff;
  --c-fg: #173e03;
  --c-accent: #9ad87f;
  --c-accent-soft: #caffaf;
  --c-recent-bg: #e6f99c;
  --c-vip-bg: #def0c0;
  --c-link: #375e23;
  --c-link-visited: #476e33;
  --c-table-stripe: #f4faec;
  --c-border: #173e0322;
  --gap: 0.6rem;
  --radius: 6px;
}

body.theme-pr {
  /* Lush green like the legacy joban123.css. */
  --c-bg: #ffffff;
  --c-fg: #173e03;
  --c-accent: #9ad87f;
}

body.theme-mv {
  /* Light variant of the grey/olive Monument Valley look. */
  --c-bg: #fafaf5;
  --c-fg: #33331f;
  --c-accent: #c9c9a4;
  --c-accent-soft: #e2e2c8;
  --c-table-stripe: #f0f0e6;
  --c-recent-bg: #e8eab8;
  --c-vip-bg: #e3e3d0;
  --c-link: #5a5a20;
  --c-link-visited: #4a4a28;
  --c-border: #33331f22;
}

body.theme-a {
  /* Light variant of the amber Apocalypse look: cream and dark amber,
     recent rows in a pale version of legacy's red. */
  --c-bg: #fffaf0;
  --c-fg: #4a2800;
  --c-accent: #e8b84a;
  --c-accent-soft: #f5dfa0;
  --c-table-stripe: #fbf2de;
  --c-recent-bg: #ffd9c2;
  --c-vip-bg: #f6e3b0;
  --c-link: #8a4b00;
  --c-link-visited: #6e3c00;
  --c-border: #4a280022;
}

/* Dark-mode tokens. Applied in two cases:
 *   1. User explicitly chose dark via the 🌓 toggle
 *      → <html data-theme="dark">
 *   2. User left the theme on auto AND the OS prefers dark
 *      → <html data-theme="auto"> + @media (prefers-color-scheme: dark)
 *
 * The toggle JS sets data-theme to one of {auto, dark, light}, so the
 * @media rule has to be scoped to data-theme="auto" — otherwise picking
 * "light" wouldn't override an OS-dark preference. Every dark theme sets
 * every row token itself (tests/test_style.py): an unset one falls back
 * to the light value.
 *
 * Pacific Rift's legacy look is light; Apocalypse (msa.css) and Monument
 * Valley (msmv.css) were dark only — their dark tokens are that look.
 */
:root { color-scheme: light dark; }
html[data-theme="dark"]  { color-scheme: dark;  }
html[data-theme="light"] { color-scheme: light; }

html[data-theme="dark"] body.theme-pr {
  --c-bg: #0c1409;
  --c-fg: #cdebbb;
  --c-accent: #2e5f1c;
  --c-accent-soft: #1f3f12;
  --c-table-stripe: #11190d;
  --c-recent-bg: #3b4214;
  --c-vip-bg: #263d1c;
  --c-link: #a6db82;
  --c-link-visited: #88bf64;
  --c-border: #cdebbb22;
}
html[data-theme="dark"] body.theme-mv {
  /* Dark like msmv.css, in grey/olive; the stripe is one small step
     above the background, as on Apocalypse. */
  --c-bg: #1a1a1a;
  --c-fg: #d8d8c4;
  --c-accent: #5c5c40;
  --c-accent-soft: #3a3a2a;
  --c-table-stripe: #242422;
  --c-recent-bg: #46461f;
  --c-vip-bg: #33332a;
  --c-link: #e6e6b8;
  --c-link-visited: #c8c89a;
  --c-border: #d8d8c422;
}
html[data-theme="dark"] body.theme-a {
  /* Dark amber like msa.css. */
  --c-bg: #100800;
  --c-fg: #f8d135;
  --c-accent: #540d09;
  --c-accent-soft: #624900;
  --c-table-stripe: #1a1004;
  --c-recent-bg: #620700;
  --c-vip-bg: #3a2a06;
  --c-link: #f8d135;
  --c-link-visited: #d4ad15;
  --c-border: #f8d13522;
}
@media (prefers-color-scheme: dark) {
  html[data-theme="auto"] body.theme-pr {
    --c-bg: #0c1409;
    --c-fg: #cdebbb;
    --c-accent: #2e5f1c;
    --c-accent-soft: #1f3f12;
    --c-table-stripe: #11190d;
    --c-recent-bg: #3b4214;
    --c-vip-bg: #263d1c;
    --c-link: #a6db82;
    --c-link-visited: #88bf64;
    --c-border: #cdebbb22;
  }
  html[data-theme="auto"] body.theme-mv {
    --c-bg: #1a1a1a;
    --c-fg: #d8d8c4;
    --c-accent: #5c5c40;
    --c-accent-soft: #3a3a2a;
    --c-table-stripe: #242422;
    --c-recent-bg: #46461f;
    --c-vip-bg: #33332a;
    --c-link: #e6e6b8;
    --c-link-visited: #c8c89a;
    --c-border: #d8d8c422;
  }
  html[data-theme="auto"] body.theme-a {
    --c-bg: #100800;
    --c-fg: #f8d135;
    --c-accent: #540d09;
    --c-accent-soft: #624900;
    --c-table-stripe: #1a1004;
    --c-recent-bg: #620700;
    --c-vip-bg: #3a2a06;
    --c-link: #f8d135;
    --c-link-visited: #d4ad15;
    --c-border: #f8d13522;
  }
}

/* ---- Page chrome ---- */

* { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
  font-family: system-ui, -apple-system, "Segoe UI", Arial, sans-serif;
  font-size: 14px;
  background: var(--c-bg);
  color: var(--c-fg);
  line-height: 1.4;
}

a {
  color: var(--c-link);
  text-decoration: none;
}
a:visited { color: var(--c-link-visited); }
a:hover, a:focus { text-decoration: underline; }

main {
  max-width: 1100px;
  margin: 0 auto;
  padding: var(--gap);
}

.site-header {
  display: flex;
  align-items: center;
  gap: var(--gap);
  padding: var(--gap);
  border-bottom: 1px solid var(--c-border);
  flex-wrap: wrap;
}
.site-header h1 {
  margin: 0;
  font-size: 1.1rem;
  flex: 1 1 auto;
}
.site-header h1 a { color: inherit; }
.site-title {
  display: inline-flex;
  align-items: center;
  gap: 0.5rem;
  text-decoration: none;
}
.site-logo {
  width: 1.8rem;
  height: 1.8rem;
  color: var(--c-accent);
  vertical-align: middle;
  flex-shrink: 0;
}

/* Nav dropdowns: game + view + sub-view + player-view all sit on a
   single row below the header. Compact and tap-friendly on mobile;
   wrap to extra rows on narrow viewports. */
.view-dropdowns {
  display: flex;
  flex-wrap: wrap;
  gap: 0.4rem;
  align-items: center;
  padding: 0.4rem var(--gap);
  border-bottom: 1px solid var(--c-border);
}
.nav-select {
  font: inherit;
  padding: 0.3em 1.8em 0.3em 0.6em;
  border: 1px solid var(--c-border);
  border-radius: var(--radius);
  background: var(--c-bg);
  color: inherit;
  cursor: pointer;
  /* Native arrow + a generous touch target. */
  -webkit-appearance: none;
  -moz-appearance: none;
  appearance: none;
  background-image: linear-gradient(45deg, transparent 50%, currentColor 50%),
                    linear-gradient(135deg, currentColor 50%, transparent 50%);
  background-position: calc(100% - 0.95em) center, calc(100% - 0.55em) center;
  background-size: 0.4em 0.4em, 0.4em 0.4em;
  background-repeat: no-repeat;
}
.nav-select:focus { outline: 2px solid var(--c-accent); outline-offset: 1px; }

.theme-toggle {
  background: transparent;
  border: 1px solid var(--c-border);
  border-radius: var(--radius);
  padding: 0.3rem 0.6rem;
  cursor: pointer;
  color: inherit;
}

.help-link {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 1.9rem;
  height: 1.9rem;
  border: 1px solid var(--c-border);
  border-radius: 50%;
  font-weight: 700;
  color: inherit;
}
.help-link:visited { color: inherit; }
.help-link:hover, .help-link:focus { text-decoration: none; background: var(--c-accent-soft); }
.lang-form { margin: 0; }
.lang-select {
  font: inherit;
  padding: 0.3em 0.4em;
  border: 1px solid var(--c-border);
  border-radius: var(--radius);
  background: var(--c-bg);
  color: inherit;
}
.visually-hidden {
  position: absolute;
  width: 1px;
  height: 1px;
  overflow: hidden;
  clip: rect(0 0 0 0);
  white-space: nowrap;
}

.page-help { max-width: 46rem; }
.page-help p, .page-help dd { line-height: 1.55; }
.help-terms dt { font-weight: 700; margin-top: 0.6em; }
.help-terms dd { margin: 0.1em 0 0 1.2em; }
.help-topics { columns: 2 12rem; padding-left: 1.2em; }

/* ---- News banner, feedback ---- */
.news-banner {
  display: flex;
  align-items: center;
  gap: var(--gap);
  padding: 0.4rem var(--gap);
  background: var(--c-vip-bg);
  border-bottom: 1px solid var(--c-border);
}
.news-banner a { color: inherit; flex: 1 1 auto; }
.news-dismiss {
  background: transparent;
  border: 0;
  font-size: 1.2em;
  line-height: 1;
  cursor: pointer;
  color: inherit;
}
.news-item h3 { margin-bottom: 0.2em; }
.news-item time { opacity: 0.7; font-weight: 400; margin-right: 0.4em; }
.page-feedback, .page-news { max-width: 46rem; }
.feedback-form { display: flex; flex-direction: column; gap: 0.8rem; }
.feedback-form label { display: flex; flex-direction: column; gap: 0.25rem; }
.feedback-form input, .feedback-form textarea {
  font: inherit;
  padding: 0.35em 0.5em;
  border: 1px solid var(--c-border);
  border-radius: var(--radius);
  background: var(--c-bg);
  color: inherit;
}
.feedback-form input[readonly] { opacity: 0.7; }
.feedback-form button { align-self: flex-start; }
.feedback-form .trap { position: absolute; left: -9999px; }
/* The flag app.js puts at the end of every result row. */
.report-row {
  margin-left: 0.4em;
  opacity: 0.25;
  text-decoration: none;
}
tr:hover .report-row, .report-row:focus { opacity: 1; }

.site-footer {
  text-align: center;
  padding: var(--gap);
  margin-top: 2rem;
  border-top: 1px solid var(--c-border);
}

/* ---- Filter form ---- */

.filters {
  display: flex;
  flex-wrap: wrap;
  gap: var(--gap);
  align-items: end;
  margin-bottom: var(--gap);
}
.filters label {
  display: flex;
  flex-direction: column;
  font-size: 0.85em;
}
.filters input[type="text"], .filters input[type="number"] {
  padding: 0.3rem;
  border: 1px solid var(--c-border);
  border-radius: var(--radius);
  background: var(--c-bg);
  color: var(--c-fg);
}
.filters button {
  padding: 0.4rem 0.8rem;
  border: 1px solid var(--c-border);
  background: var(--c-accent);
  color: var(--c-fg);
  border-radius: var(--radius);
  cursor: pointer;
}

/* ---- Results table ---- */

/* Wrap the results table in a horizontally-scrollable container so
   long rows (lots of columns / wide racer names) don't push the
   page chrome around on narrow screens. The <main> element gets the
   overflow so the table itself can stay width:max-content and never
   wrap individual cells. */
main {
  overflow-x: auto;
}

table.results {
  width: max-content;
  min-width: 100%;
  border-collapse: collapse;
  margin-top: var(--gap);
}
table.results th,
table.results td {
  padding: 0.25rem 0.5rem;
  border-bottom: 1px solid var(--c-border);
  text-align: left;
  vertical-align: top;
  /* Keep each cell on a single line — the user explicitly preferred
     horizontal scrolling over multi-line wrap. */
  white-space: nowrap;
}
table.results th {
  background: var(--c-accent);
  color: var(--c-fg);
  position: sticky;
  top: 0;
  cursor: pointer;
  user-select: none;
}
table.results th[data-sort]::after {
  content: " ⇅";
  opacity: 0.4;
}
table.results th[aria-sort="ascending"]::after { content: " ▲"; opacity: 1; }
table.results th[aria-sort="descending"]::after { content: " ▼"; opacity: 1; }

table.results td.num { text-align: right; font-variant-numeric: tabular-nums; }
table.results td.country { white-space: nowrap; text-align: center; }
table.results td.country .flag { font-size: 1.15em; line-height: 1; cursor: help; }

table.results tbody tr:nth-child(even) td { background: var(--c-table-stripe); }
table.results tbody tr.recent  td { background: var(--c-recent-bg); }
table.results tbody tr.vip     td { font-weight: 700; background: var(--c-vip-bg); }
table.results tbody tr.best    td { font-weight: 700; }

.empty {
  font-style: italic;
  opacity: 0.7;
  padding: 1rem;
  border: 1px dashed var(--c-border);
  border-radius: var(--radius);
  text-align: center;
}

/* ---- Filter form ---- */
/* (The player-jump form and the .controls wrapper are gone — the
   only remaining external nav is the row of dropdowns below the
   header.) */

/* The Vehicles + Tracks checkbox walls collapse into a <details> so the
   results table sits right under the heading. The closed summary line
   shows the current selection ("All vehicles · Original 16") so the
   state is visible without opening the panel. */
.filters-details {
  border: 1px solid var(--c-border);
  border-radius: var(--radius);
  background: var(--c-card-bg, var(--c-bg));
  padding: 0.5rem 0.75rem;
  margin-bottom: 0.75rem;
  font-size: 0.95em;
}
.filters-details > summary {
  cursor: pointer;
  display: inline-flex;
  gap: 0.6rem;
  align-items: baseline;
  list-style: none;
  user-select: none;
}
.filters-details > summary::-webkit-details-marker { display: none; }
.filters-details > summary::before {
  content: "▸";
  display: inline-block;
  width: 0.8em;
  transition: transform 120ms ease;
  opacity: 0.7;
}
.filters-details[open] > summary::before { transform: rotate(90deg); }
.filters-summary-state { opacity: 0.85; font-size: 0.92em; }
.filters-summary-vip {
  display: inline-flex;
  align-items: baseline;
  gap: 0.25em;
}
.filters-summary-vip input[type="text"] {
  width: 9em;
  padding: 0.1em 0.4em;
  border: 1px solid var(--c-border);
  border-radius: var(--radius);
  background: var(--c-bg);
  color: inherit;
  /* iOS Safari auto-zooms when the focused input's font-size is < 16px.
     Pin to 16px so tapping the VIP field doesn't change the page zoom
     (which would survive the reload and disorient the user). */
  font-size: 16px;
}
.filters-summary-apply {
  padding: 0 0.5em;
  margin-left: 0.25em;
  background: var(--c-accent);
  color: var(--c-fg);
  border: 0;
  border-radius: var(--radius);
  cursor: pointer;
  font: inherit;
  font-size: 0.9em;
  line-height: 1.6;
}
.filters-details[open] > div.filters-checkboxes { margin-top: 0.75rem; }
.filters-body { margin-top: 0.75rem; display: flex; flex-direction: column; gap: 0.75rem; }

/* (The bottom Apply row is gone — the summary-line "Ok" button is the
   only submit affordance now.) */
form.filters fieldset {
  border: 1px solid var(--c-border);
  border-radius: var(--radius);
  padding: 0.5rem 0.75rem;
  margin: 0;
}
form.filters legend { padding: 0 0.5em; font-weight: 600; }
form.filters .filters-basic { display: flex; gap: 0.75rem; flex-wrap: wrap; }
form.filters label { display: inline-flex; gap: 0.4em; align-items: center; }
form.filters .filters-checkboxes {
  display: flex;
  flex-direction: column;
  gap: 0.75rem;
  flex: 1 1 auto;
}
form.filters .filters-tracks,
form.filters .filters-vehicles {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(11em, 1fr));
  gap: 0.15em 0.6em;
}
form.filters .filters-tracks legend,
form.filters .filters-vehicles legend { grid-column: 1 / -1; }
form.filters .filters-tracks label,
form.filters .filters-vehicles label {
  font-size: 0.92em;
  cursor: pointer;
}
/* Double-clicking a name selects all/one (app.js); don't also select the word. */
.filters-checkboxes label { user-select: none; }
.filters-race {
  display: flex;
  flex-wrap: wrap;
  gap: 0.5rem;
  align-items: center;
  border: 1px solid var(--c-border);
  border-radius: var(--radius);
  padding: 0.5rem 0.75rem;
  margin: 0;
}
.filters-race legend { padding: 0 0.5em; font-weight: 600; }
.filters-race input[type=number] { width: 4em; }
form.filters .dlc-divider {
  display: block;
  height: 0;
  border-bottom: 1px dotted var(--c-border);
  margin-top: 0.4em;
  /* Span the row so the next-row tracks visually become DLC. */
}
form.filters .hint { font-size: 0.85em; opacity: 0.7; margin: 0.25em 0 0; }
form.filters button[type=submit] {
  padding: 0.5em 1.2em;
  background: var(--c-accent);
  color: var(--c-fg);
  border: none;
  border-radius: var(--radius);
  cursor: pointer;
  font-weight: 600;
  align-self: flex-end;
}
form.filters button[type=submit]:hover { filter: brightness(1.1); }

/* Update forms (routers/forms.py) */
.notice { padding: .4rem .6rem; border-radius: 4px; }
.notice.ok { background: var(--c-vip-bg); }
.notice.error { border: 1px solid currentColor; font-weight: 700; }
.update-row input, .update-row select { font-size: 16px; } /* no iOS zoom */
.country-form { margin-top: 1rem; }
