:root {
  --ink: #1a1d1f;
  --muted: #6b7280;
  --line: #e5e7eb;
  --bg: #ffffff;
  --row-alt: #fafafa;
  --accent: #2563eb;
  --accent-soft: #eff6ff;
  --rank-gold: #b8860b;
  --rank-gold-soft: #fdf6e3;
  --rank-silver: #6b7280;
  --rank-silver-soft: #f3f4f6;
  --rank-bronze: #b0631e;
  --rank-bronze-soft: #fdf1e7;
}

* { box-sizing: border-box; }

body {
  margin: 0;
  padding: 24px 32px 48px;
  background: var(--bg);
  color: var(--ink);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Helvetica, Arial, sans-serif;
  font-size: 14px;
}

.page-header {
  display: flex;
  align-items: flex-end;
  gap: 14px;
  margin-bottom: 20px;
}

.header-text {
  flex: 1;
  min-width: 0;
}

.site-logo {
  height: 40px;
  width: auto;
  flex-shrink: 0;
}

h1 {
  font-size: 20px;
  font-weight: 600;
  margin: 0 0 4px 0;
}

.subtitle {
  color: var(--muted);
  margin: 0;
  font-size: 13px;
}

.race-meta {
  color: var(--muted);
}

.archives-link {
  flex-shrink: 0;
  color: var(--accent);
  font-size: 13px;
  font-weight: 500;
  text-decoration: none;
  white-space: nowrap;
  padding-bottom: 1px; /* optical alignment with the subtitle baseline */
}

.archives-link:hover {
  text-decoration: underline;
}

.link-btn {
  appearance: none;
  border: none;
  background: none;
  color: var(--accent);
  font-size: 13px;
  cursor: pointer;
  padding: 0;
}

.link-btn:hover {
  text-decoration: underline;
}

/* ---- Archived Races tab ---- */

.archive-race-list {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.archive-race-item {
  appearance: none;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--bg);
  padding: 12px 16px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  cursor: pointer;
  text-align: left;
  font-size: 14px;
}

.archive-race-item:hover {
  background: var(--accent-soft);
  border-color: var(--accent);
}

.archive-race-name {
  font-weight: 600;
}

.archive-race-meta {
  color: var(--muted);
  font-size: 12px;
  white-space: nowrap;
}

.archive-table-header {
  position: sticky;
  top: 0;
  z-index: 3;
  background: var(--bg);
  border-bottom: 1px solid var(--line);
  padding: 10px 14px;
  display: flex;
  align-items: center;
  gap: 14px;
}

.archive-table-header h3 {
  margin: 0;
  font-size: 14px;
  font-weight: 600;
}

code {
  background: #f3f4f6;
  padding: 1px 5px;
  border-radius: 4px;
  font-size: 12px;
}

button:focus-visible,
input:focus-visible,
select:focus-visible,
.bib-link:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}

/* ---- Tabs ---- */

.tab-bar {
  display: flex;
  gap: 4px;
  border-bottom: 1px solid var(--line);
  margin-bottom: 20px;
  overflow-x: auto;
}

.tab-btn {
  appearance: none;
  border: none;
  background: none;
  padding: 10px 16px;
  font-size: 14px;
  font-weight: 500;
  color: var(--muted);
  cursor: pointer;
  border-bottom: 2px solid transparent;
  white-space: nowrap;
  display: flex;
  align-items: center;
  gap: 8px;
}

.tab-btn:hover {
  color: var(--ink);
}

.tab-btn.active {
  color: var(--accent);
  border-bottom-color: var(--accent);
}

.tab-panel {
  display: none;
}

.tab-panel.active {
  display: block;
}

/* Lap Updates is not one of the switchable tabs on desktop — hide its button
   there; it's re-shown in the mobile media query below. */
.lap-updates-tab-btn {
  display: none;
}

/* ---- Layout: main column + persistent desktop sidebar ---- */

.layout {
  display: flex;
  gap: 24px;
  align-items: flex-start;
}

.main-col {
  flex: 1;
  min-width: 0;
}

.lap-updates-section {
  width: 360px;
  flex-shrink: 0;
  border: 1px solid var(--line);
  border-radius: 8px;
  display: flex;
  flex-direction: column;
  max-height: 85vh;
  position: sticky;
  top: 20px;
  overflow: hidden;
}

.ticker-header {
  padding: 12px 16px;
  border-bottom: 1px solid var(--line);
  font-weight: 600;
  font-size: 13px;
  display: flex;
  align-items: center;
  gap: 8px;
  flex-shrink: 0;
}

.lap-updates-section .ticker-list {
  overflow-y: auto;
  padding: 10px;
}

/* ---- Shared controls ---- */

.controls {
  display: flex;
  gap: 12px;
  align-items: center;
  margin-bottom: 16px;
  flex-wrap: wrap;
}

#search {
  flex: 1;
  min-width: 220px;
  padding: 8px 12px;
  border: 1px solid var(--line);
  border-radius: 6px;
  font-size: 14px;
  outline: none;
}

#search:focus {
  border-color: var(--accent);
}

#clear-filters {
  padding: 8px 14px;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: var(--bg);
  color: var(--muted);
  font-size: 13px;
  cursor: pointer;
  white-space: nowrap;
}

#clear-filters:hover {
  background: var(--accent-soft);
  color: var(--accent);
  border-color: var(--accent);
}

.status-text,
#status {
  color: var(--muted);
  font-size: 13px;
  white-space: nowrap;
}

/* ---- Shared table shell ---- */

.table-wrap {
  border: 1px solid var(--line);
  border-radius: 8px;
  overflow: auto;
  max-height: 75vh;
}

table {
  border-collapse: collapse;
  width: 100%;
  min-width: 900px;
}

thead th {
  position: sticky;
  top: 0;
  background: var(--bg);
  border-bottom: 1px solid var(--line);
  text-align: left;
  padding: 10px 14px 6px 14px;
  font-weight: 600;
  font-size: 12px;
  text-transform: uppercase;
  letter-spacing: 0.03em;
  color: var(--muted);
  cursor: pointer;
  user-select: none;
  white-space: nowrap;
  z-index: 2;
}

thead th:hover {
  color: var(--ink);
  background: var(--accent-soft);
}

thead th .arrow {
  display: inline-block;
  width: 12px;
  color: var(--accent);
}

thead tr.filter-row th {
  position: sticky;
  top: 33px;
  padding: 4px 8px 8px 8px;
  cursor: default;
  background: var(--bg);
  z-index: 1;
}

thead tr.filter-row th:hover {
  background: var(--bg);
}

.col-filter {
  width: 100%;
  padding: 4px 6px;
  border: 1px solid var(--line);
  border-radius: 4px;
  font-size: 12px;
  font-weight: 400;
  text-transform: none;
  letter-spacing: normal;
  color: var(--ink);
  outline: none;
}

.col-filter:focus {
  border-color: var(--accent);
}

tbody td {
  padding: 8px 14px;
  border-bottom: 1px solid var(--line);
  white-space: nowrap;
}

tbody tr:nth-child(even) {
  background: var(--row-alt);
}

/* Inactive riders: a light grey background + muted text, but nothing is
   hidden — bib links and the favorite star keep their normal colors since
   they set their own `color` explicitly, so the row stays fully readable
   and interactive. */
tbody tr.row-inactive {
  background: #f3f4f6;
  color: var(--muted);
}

tbody tr:hover {
  background: var(--accent-soft);
}

.empty, .error {
  padding: 40px;
  text-align: center;
  color: var(--muted);
}

.error {
  color: #b91c1c;
}

.bib-link {
  color: var(--accent);
  cursor: pointer;
  text-decoration: underline;
  text-decoration-color: transparent;
  transition: text-decoration-color 0.15s;
}

.bib-link:hover {
  text-decoration-color: var(--accent);
}

/* ---- Leaderboard tab ---- */

.leaderboard-controls {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  margin-bottom: 16px;
  flex-wrap: wrap;
}

/* ---- Favorites ---- */

.fav-toggle {
  display: flex;
  align-items: center;
  gap: 6px;
  font-size: 13px;
  color: var(--muted);
  cursor: pointer;
  white-space: nowrap;
  user-select: none;
}

.fav-toggle input {
  cursor: pointer;
}

.fav-toggle .star-icon {
  color: var(--rank-gold);
}

.col-star {
  width: 32px;
  padding-left: 10px !important;
  padding-right: 4px !important;
}

.star-btn {
  appearance: none;
  border: none;
  background: none;
  padding: 0;
  font-size: 16px;
  line-height: 1;
  cursor: pointer;
  color: var(--line);
}

.star-btn:hover {
  color: var(--rank-gold);
}

.star-btn.favorited {
  color: var(--rank-gold);
}

.leaderboard-group {
  margin-bottom: 28px;
}

.leaderboard-group-title {
  display: flex;
  flex-wrap: wrap;
  align-items: baseline;
  gap: 8px;
  font-size: 15px;
  font-weight: 600;
  margin: 0 0 10px 0;
  padding-bottom: 8px;
  border-bottom: 1px solid var(--line);
}

.leaderboard-group-title .group-category {
  color: var(--ink);
}

.leaderboard-group-title .group-meta {
  color: var(--muted);
  font-weight: 500;
  font-size: 13px;
}

.leaderboard-table {
  min-width: 820px;
}

.leaderboard-table th:first-child,
.leaderboard-table td:first-child {
  width: 56px;
}

.rank-badge {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 24px;
  height: 24px;
  border-radius: 50%;
  font-size: 12px;
  font-weight: 700;
}

.rank-badge-1 {
  background: var(--rank-gold-soft);
  color: var(--rank-gold);
}

.rank-badge-2 {
  background: var(--rank-silver-soft);
  color: var(--rank-silver);
}

.rank-badge-3 {
  background: var(--rank-bronze-soft);
  color: var(--rank-bronze);
}

tr.rank-1 { border-left: 3px solid var(--rank-gold); }
tr.rank-2 { border-left: 3px solid var(--rank-silver); }
tr.rank-3 { border-left: 3px solid var(--rank-bronze); }

/* ---- Lap Updates tab ---- */

.ticker-list {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(280px, 1fr));
  gap: 10px;
  align-content: start;
}

.ticker-item {
  padding: 10px 12px;
  border: 1px solid var(--line);
  border-radius: 6px;
  font-size: 13px;
  line-height: 1.4;
  background: var(--bg);
}

.ticker-item.newest {
  animation: row-flash 1.2s ease;
}

.ticker-item .ticker-time {
  color: var(--accent);
  font-weight: 600;
}

.ticker-item .ticker-lap {
  font-weight: 600;
}

@keyframes row-flash {
  from { background: var(--accent-soft); }
  to { background: transparent; }
}

.ticker-empty {
  grid-column: 1 / -1;
  padding: 40px 16px;
  color: var(--muted);
  font-size: 13px;
  text-align: center;
}

.ticker-dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: #d1d5db;
  flex-shrink: 0;
}

.ticker-dot.live {
  background: #22c55e;
  box-shadow: 0 0 0 3px rgba(34, 197, 94, 0.2);
}

/* ---- Rider modal ---- */

.modal-overlay {
  display: none;
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.45);
  align-items: center;
  justify-content: center;
  z-index: 100;
  padding: 24px;
}

.modal-overlay.open {
  display: flex;
}

.modal-box {
  background: var(--bg);
  border-radius: 10px;
  max-width: 640px;
  width: 100%;
  max-height: 94vh;
  display: flex;
  flex-direction: column;
  box-shadow: 0 20px 50px rgba(0, 0, 0, 0.25);
}

.modal-header {
  padding: 16px 20px;
  border-bottom: 1px solid var(--line);
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.modal-header h2 {
  margin: 0;
  font-size: 17px;
  font-weight: 600;
}

.modal-close {
  border: none;
  background: none;
  font-size: 20px;
  line-height: 1;
  cursor: pointer;
  color: var(--muted);
  padding: 4px 8px;
  border-radius: 6px;
}

.modal-close:hover {
  background: var(--accent-soft);
  color: var(--accent);
}

.modal-body {
  padding: 20px;
  overflow-y: auto;
  overflow-x: hidden;
}

.rider-info-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 10px 20px;
  margin-bottom: 20px;
  font-size: 13px;
  min-width: 0;
}

.rider-info-grid .field-label {
  color: var(--muted);
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: 0.03em;
  display: block;
}

.rider-info-grid .field-value {
  font-weight: 500;
  overflow-wrap: break-word;
  word-break: break-word;
}

.modal-body h3 {
  font-size: 13px;
  text-transform: uppercase;
  letter-spacing: 0.03em;
  color: var(--muted);
  margin: 0 0 10px 0;
}

.rider-lap-table {
  width: auto;
  min-width: 0;
  border-collapse: collapse;
  font-size: 13px;
}

.rider-lap-table th,
.rider-lap-table td {
  white-space: nowrap;
}

.rider-lap-table th {
  text-align: left;
  padding: 6px 10px;
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: 0.03em;
  color: var(--muted);
  border-bottom: 1px solid var(--line);
}

.rider-lap-table td {
  padding: 6px 10px;
  border-bottom: 1px solid var(--line);
}

.rider-lap-table tbody tr:nth-child(even) {
  background: var(--row-alt);
}

.rider-lap-table th:last-child,
.rider-lap-table td:last-child {
  padding-right: 24px;
}

.no-laps {
  color: var(--muted);
  font-size: 13px;
  padding: 8px 0;
}

/* ---- Mobile ---- */

@media (max-width: 680px) {
  body {
    padding: 16px 12px 32px;
    font-size: 13px;
  }

  h1 {
    font-size: 17px;
  }

  .page-header {
    gap: 10px 14px;
    margin-bottom: 16px;
    flex-wrap: wrap;
  }

  .archives-link {
    flex-basis: 100%;
    text-align: right;
    padding: 4px 0 8px;
    border-bottom: 1px solid var(--line);
  }

  .site-logo {
    height: 30px;
  }

  .tab-btn {
    padding: 10px 12px;
    font-size: 13px;
    flex: 1;
    justify-content: center;
  }

  .controls {
    flex-direction: column;
    align-items: stretch;
    gap: 8px;
  }

  #search {
    min-width: 0;
  }

  .status-text,
  #status {
    text-align: right;
  }

  .table-wrap {
    max-height: 70vh;
    border-radius: 6px;
  }

  table {
    min-width: 0;
    table-layout: fixed;
  }

  thead th,
  tbody td {
    padding: 6px 8px;
    font-size: 12px;
  }

  /* Explicit widths for the frozen columns so their sticky left-offsets are
     predictable. Remaining columns (age/gender/category/mileage) share
     whatever width is left over. */
  .col-star {
    width: 26px;
    padding-left: 6px !important;
    padding-right: 2px !important;
  }

  .star-btn {
    font-size: 14px;
  }

  .col-bib_number { width: 48px; }
  .col-last_name,
  .col-first_name {
    width: 76px;
  }
  .col-age { width: 40px; }
  .col-gender { width: 56px; }
  .col-category { width: 70px; }
  .col-mileage { width: 60px; }

  #data-table th,
  #data-table td,
  #archive-data-table th,
  #archive-data-table td {
    overflow: hidden;
    text-overflow: ellipsis;
  }

  /* Freeze identity columns while scrolling right, in DOM order:
     star (0) -> bib_number (26) -> last_name (74) -> first_name (150) */
  .col-star,
  .col-bib_number,
  .col-last_name,
  .col-first_name {
    position: sticky;
    background: var(--bg);
  }

  .col-star       { left: 0; }
  .col-bib_number { left: 26px; }
  .col-last_name  { left: 74px; }
  .col-first_name {
    left: 150px;
    box-shadow: 4px 0 6px -4px rgba(0, 0, 0, 0.25);
  }

  /* The archive table has no star column, so its frozen columns start one
     slot earlier than the live table's. */
  #archive-data-table .col-bib_number { left: 0; }
  #archive-data-table .col-last_name  { left: 48px; }
  #archive-data-table .col-first_name { left: 124px; }

  /* Header cells are already position:sticky (top:0) for vertical freeze —
     bump the frozen ones above their non-frozen siblings so a horizontally
     scrolling header cell (e.g. Age) can't paint over a frozen one. */
  thead th.col-star,
  thead th.col-bib_number,
  thead th.col-last_name,
  thead th.col-first_name {
    z-index: 4;
  }

  /* Keep the frozen cells in step with row striping and hover, since their
     own opaque background would otherwise always show plain white. */
  tbody tr:nth-child(even) .col-star,
  tbody tr:nth-child(even) .col-bib_number,
  tbody tr:nth-child(even) .col-last_name,
  tbody tr:nth-child(even) .col-first_name {
    background: var(--row-alt);
  }

  tbody tr.row-inactive .col-star,
  tbody tr.row-inactive .col-bib_number,
  tbody tr.row-inactive .col-last_name,
  tbody tr.row-inactive .col-first_name {
    background: #f3f4f6;
  }

  tbody tr:hover .col-star,
  tbody tr:hover .col-bib_number,
  tbody tr:hover .col-last_name,
  tbody tr:hover .col-first_name {
    background: var(--accent-soft);
  }

  /* Per-column filter boxes are removed on mobile — Favorites only is the
     one filter kept, and it lives in .controls, not this row. */
  #filter-row {
    display: none;
  }

  #clear-filters {
    display: none;
  }

  .leaderboard-group-title {
    font-size: 14px;
  }

  .ticker-list {
    grid-template-columns: 1fr;
  }

  .modal-overlay {
    padding: 0;
  }

  .modal-box {
    max-width: 100%;
    width: 100%;
    height: 100%;
    max-height: 100%;
    border-radius: 0;
  }

  .rider-info-grid {
    grid-template-columns: 1fr 1fr;
    gap: 8px 14px;
  }

  /* Global search is hidden on mobile — the columns that used to be hidden
     here (City/State/Bike Type/Support/Long/Short Laps) are now simply not
     rendered at all on mobile (see MOBILE_COLUMN_KEYS in app.js), so there's
     no display:none list to maintain here anymore. */
  #search {
    display: none;
  }

  /* Lap Updates becomes a real third tab on mobile instead of a sidebar */
  .layout {
    flex-direction: column;
    /* align-items: flex-start (set for desktop, to stop the sidebar
       stretching tall) becomes a horizontal constraint once flex-direction
       flips to column — it was letting .main-col shrink-wrap to the table's
       full content width instead of the viewport width, which broke the
       internal horizontal scroll entirely. */
    align-items: stretch;
  }

  .lap-updates-tab-btn {
    display: flex;
  }

  .lap-updates-section {
    display: none;
    width: 100%;
    position: static;
    max-height: none;
    border: none;
    border-radius: 0;
  }

  .lap-updates-section.active {
    display: flex;
  }

  .lap-updates-section .ticker-list {
    padding: 0;
  }
}

@media (prefers-reduced-motion: reduce) {
  .ticker-item.newest {
    animation: none;
  }
}
