:root {
  --bg: #0f1115;
  --bg-elev: #161a22;
  --bg-elev2: #1d222d;
  --border: #2a3140;
  --text: #e7ecf3;
  --muted: #8a93a3;
  --accent: #ff6a00;
  --accent-2: #ffb145;
  --green: #2dd47d;
  --red: #ff5563;
  --ladies: #ff7ab6;
  --row-hover: #1a2030;
  --row-stripe: #14181f;
  font-family: 'Segoe UI', system-ui, sans-serif;
}

* { box-sizing: border-box; }
html, body {
  margin: 0; padding: 0; background: var(--bg); color: var(--text);
  height: 100vh; overflow: hidden;
  display: flex; flex-direction: column;
}
button { font: inherit; color: inherit; cursor: pointer; }
.hidden { display: none !important; }
.topbar, .tabs, .filters, .pull-indicator { flex: 0 0 auto; }
main { flex: 1 1 auto; min-height: 0; display: flex; flex-direction: column; }
.table-wrap {
  flex: 1 1 auto; min-height: 0;
  overflow: auto;
  -webkit-overflow-scrolling: touch;
  padding: 8px 0 60px;
}

/* Top bar */
.topbar {
  display: flex; align-items: center; justify-content: space-between;
  padding: 10px 16px;
  background: linear-gradient(180deg, #1a1f2b, #131722);
  border-bottom: 1px solid var(--border);
  z-index: 6;
}
.brand { display: flex; align-items: center; gap: 12px; }
.logo {
  width: 44px; height: 44px; border-radius: 10px;
  background: linear-gradient(135deg, var(--accent), var(--accent-2));
  display: grid; place-items: center; font-weight: 900; color: #1a1209;
  letter-spacing: 0.5px;
}
.titleblock h1 {
  margin: 0; font-size: 17px; font-weight: 700; line-height: 1.2;
}
.meta { font-size: 12px; color: var(--muted); margin-top: 2px; }
.meta .dot { margin: 0 6px; opacity: 0.5; }
.live::before {
  content: ""; display: inline-block; width: 8px; height: 8px;
  border-radius: 50%; background: var(--green); margin-right: 6px;
  animation: pulse 1.6s infinite ease-in-out; vertical-align: middle;
}
.live.stale::before { background: var(--red); animation: none; }
@keyframes pulse {
  0%, 100% { opacity: 1; transform: scale(1); }
  50% { opacity: 0.4; transform: scale(0.85); }
}

.actions button {
  background: var(--bg-elev2); border: 1px solid var(--border);
  width: 38px; height: 38px; border-radius: 9px; font-size: 18px;
  margin-left: 6px; transition: all .15s;
}
.actions button:hover { background: var(--accent); color: #1a1209; border-color: var(--accent); }

/* Tabs */
.tabs {
  display: flex; gap: 4px;
  background: var(--bg-elev); padding: 4px;
  border-bottom: 1px solid var(--border);
  overflow-x: auto;
  z-index: 5;
}
.tab {
  background: transparent; border: 0; padding: 10px 16px; color: var(--muted);
  font-weight: 600; border-radius: 8px; white-space: nowrap; font-size: 14px;
}
.tab:hover { color: var(--text); background: var(--bg-elev2); }
.tab.active { background: var(--accent); color: #1a1209; }

/* Filter row */
.filters {
  padding: 10px 16px; background: var(--bg-elev);
  border-bottom: 1px solid var(--border);
  display: flex; gap: 10px; align-items: center; flex-wrap: wrap;
  z-index: 4;
}
.filters input, .filters select {
  background: var(--bg-elev2); border: 1px solid var(--border);
  color: var(--text); border-radius: 8px; padding: 8px 12px; font: inherit;
  min-width: 220px;
}
.filters input:focus, .filters select:focus {
  outline: none; border-color: var(--accent);
}
.filters label { color: var(--muted); display: flex; gap: 8px; align-items: center; }
.hint { color: var(--muted); font-size: 13px; }

/* Table */
.loading, .empty {
  text-align: center; padding: 40px; color: var(--muted);
}
table {
  width: 100%; border-collapse: collapse; background: var(--bg-elev);
  font-size: 14px;
  min-width: 900px;     /* enough for all lap columns */
}
thead th {
  background: #1a1f2b; color: var(--muted); text-align: left;
  padding: 10px 10px; font-weight: 600; font-size: 12px;
  text-transform: uppercase; letter-spacing: 0.4px;
  border-bottom: 2px solid var(--accent);
  white-space: nowrap;
  position: sticky; top: 0; z-index: 3;     /* sticks to top of .table-wrap scroll container */
}
tbody td {
  padding: 9px 10px; border-bottom: 1px solid #20262f;
  vertical-align: middle;
}
tbody tr:nth-child(2n) td { background: var(--row-stripe); }
tbody tr:hover td { background: var(--row-hover); }
tbody tr.lady td { background: rgba(255, 122, 182, 0.10); }
tbody tr.lady:hover td { background: rgba(255, 122, 182, 0.18); }

.rank {
  font-weight: 800; font-variant-numeric: tabular-nums; text-align: right;
  min-width: 36px;
}
.rank.gold { color: gold; }
.rank.silver { color: silver; }
.rank.bronze { color: #cd7f32; }

.startnr {
  display: inline-block; min-width: 36px; padding: 2px 7px;
  background: #252b39; color: var(--accent-2);
  border-radius: 6px; text-align: center; font-weight: 700;
  font-variant-numeric: tabular-nums; font-size: 13px;
}
.name { font-weight: 600; }
.lady-badge {
  display: inline-block; margin-left: 6px;
  font-size: 10px; padding: 2px 6px; border-radius: 4px;
  background: var(--ladies); color: #1a1209; font-weight: 700;
  letter-spacing: 0.3px;
}
.vehicle { color: var(--muted); font-size: 13px; }
.cls {
  display: inline-block; padding: 2px 7px; border-radius: 5px;
  background: #1f2632; color: #cfd6e0; font-size: 12px;
  border: 1px solid var(--border);
}
.time { font-variant-numeric: tabular-nums; font-family: 'Consolas', 'SF Mono', monospace; white-space: nowrap; }
.time.best { color: var(--green); font-weight: 700; }
.time.dnf { color: var(--red); }
.fault {
  display: inline-block;
  margin-left: 4px; padding: 1px 5px;
  background: rgba(255, 85, 99, 0.18);
  color: var(--red);
  border-radius: 4px; font-size: 10px; font-weight: 700;
  font-family: 'Consolas', 'SF Mono', monospace;
  vertical-align: middle;
}
.fault.dnf {
  background: var(--red);
  color: #fff;
}
.gap { color: var(--accent-2); font-variant-numeric: tabular-nums; }
.gap.zero { color: var(--green); font-weight: 700; }

/* Prognose column */
.col-prog { white-space: nowrap; }
.prog-target {
  display: inline-block; padding: 3px 8px;
  background: rgba(45, 212, 125, 0.18);
  color: var(--green); font-weight: 700;
  border-radius: 5px; font-size: 12px;
  font-family: 'Consolas','SF Mono',monospace;
  font-variant-numeric: tabular-nums;
}
.prog-stretch {
  display: inline-block; padding: 3px 8px;
  background: rgba(255, 177, 69, 0.16);
  color: var(--accent-2); font-weight: 700;
  border-radius: 5px; font-size: 12px;
  font-family: 'Consolas','SF Mono',monospace;
  font-variant-numeric: tabular-nums;
}
.prog-ahead {
  display: inline-block; padding: 3px 8px;
  background: rgba(45, 212, 125, 0.18);
  color: var(--green); font-weight: 700;
  border-radius: 5px; font-size: 12px;
  font-family: 'Consolas','SF Mono',monospace;
  font-variant-numeric: tabular-nums;
}
.prog-tied {
  display: inline-block; padding: 3px 8px;
  background: var(--bg-elev2); color: var(--accent-2);
  font-weight: 700; border-radius: 5px; font-size: 12px;
}
.prog-fix {
  display: inline-block; padding: 3px 8px;
  background: rgba(255, 215, 0, 0.18);
  color: gold; font-weight: 700;
  border-radius: 5px; font-size: 12px;
}
.prog-threat {
  display: inline-block; padding: 3px 8px;
  background: rgba(255, 85, 99, 0.18);
  color: var(--red); font-weight: 700;
  border-radius: 5px; font-size: 12px;
}
.prog-locked { color: var(--muted); font-size: 12px; font-style: italic; }
.prog-bad { color: var(--red); font-size: 11px; }
.prog-pending { color: var(--muted); font-size: 11px; }

/* Class headers in 'by class' tab */
.class-header td {
  background: #232a37 !important;
  font-weight: 700; padding: 12px 10px; color: var(--accent-2);
  border-bottom: 2px solid var(--accent);
}

/* Modal */
.modal {
  position: fixed; inset: 0; background: rgba(0,0,0,0.7);
  display: grid; place-items: center; z-index: 100; padding: 12px;
}
.modal-content {
  background: var(--bg-elev); border: 1px solid var(--border);
  border-radius: 12px; max-width: 600px; width: 100%;
  max-height: 90vh; display: flex; flex-direction: column;
}
.modal-content header {
  display: flex; justify-content: space-between; align-items: center;
  padding: 14px 18px; border-bottom: 1px solid var(--border);
}
.modal-content header h2 { margin: 0; font-size: 17px; }
.modal-content header button {
  background: transparent; border: 0; font-size: 22px; color: var(--muted);
}
.modal-body { padding: 14px 18px; overflow-y: auto; flex: 1; }
.modal-actions { display: flex; gap: 8px; margin: 10px 0; flex-wrap: wrap; }
.modal-actions input { flex: 1; min-width: 180px; }
.modal-content footer {
  padding: 14px 18px; border-top: 1px solid var(--border);
  display: flex; justify-content: flex-end; gap: 8px;
}
.driver-list {
  display: grid; gap: 4px;
  max-height: 50vh; overflow-y: auto;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 8px; padding: 6px;
}
.driver-list label {
  display: flex; align-items: center; gap: 10px;
  padding: 8px 10px; border-radius: 6px; cursor: pointer;
  font-size: 14px;
}
.driver-list label:hover { background: var(--bg-elev2); }
.driver-list label.checked { background: rgba(255, 122, 182, 0.13); }
.driver-list input { transform: scale(1.15); accent-color: var(--ladies); }
.driver-list .dr-meta { color: var(--muted); font-size: 12px; }

/* Settings menu */
.modal-content.menu { max-width: 420px; }
.menu-item {
  display: flex; flex-direction: column; align-items: flex-start; gap: 2px;
  width: 100%; text-align: left; cursor: pointer;
  background: var(--bg-elev2); border: 1px solid var(--border);
  color: var(--text); padding: 14px 16px; border-radius: 10px; margin-bottom: 10px;
}
.menu-item:hover { background: var(--row-hover); }
.menu-item .mi-title { font-size: 16px; font-weight: 700; }
.menu-item .mi-sub { font-size: 12px; color: var(--muted); }

/* Cup override list */
.cup-list {
  display: grid; gap: 4px;
  max-height: 56vh; overflow-y: auto;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 8px; padding: 6px;
}
.cup-row {
  display: flex; align-items: center; justify-content: space-between; gap: 10px;
  padding: 8px 10px; border-radius: 6px; font-size: 14px;
}
.cup-row:hover { background: var(--bg-elev2); }
.cup-row .cup-info { min-width: 0; }
.cup-row .dr-meta { color: var(--muted); font-size: 12px; }
.cup-seg { display: inline-flex; flex: 0 0 auto; border-radius: 8px; overflow: hidden; border: 1px solid var(--border); }
.cup-seg button {
  background: var(--bg-elev2); color: var(--muted); border: 0;
  padding: 7px 11px; font-size: 13px; font-weight: 700; cursor: pointer;
  border-left: 1px solid var(--border);
}
.cup-seg button:first-child { border-left: 0; }
.cup-seg button.sel { background: var(--accent); color: #1a1209; }

/* Wertung-ändern rows: driver info on top, class+cup editor below */
.wertung-row { flex-direction: column; align-items: stretch; gap: 6px; }
.wertung-edit { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; }
.wertung-edit .ovr-class {
  flex: 1 1 auto; min-width: 0;
  background: var(--bg-elev2); border: 1px solid var(--border); color: var(--text);
  padding: 7px 8px; border-radius: 8px; font-size: 13px;
}

/* Help text */
.help-body h3 { margin: 16px 0 4px; font-size: 14px; color: var(--accent); }
.help-body h3:first-child { margin-top: 0; }
.help-body p { margin: 4px 0; font-size: 13px; line-height: 1.45; color: var(--text); }
.help-body b { color: var(--text); }

button.primary {
  background: var(--accent); color: #1a1209; font-weight: 700;
  border: 0; padding: 10px 18px; border-radius: 8px;
}
button.primary:hover { background: var(--accent-2); }
button.ghost {
  background: var(--bg-elev2); border: 1px solid var(--border);
  color: var(--text); padding: 8px 14px; border-radius: 8px;
}
button.ghost:hover { background: var(--row-hover); }

input[type="search"] {
  background: var(--bg-elev2); border: 1px solid var(--border);
  color: var(--text); border-radius: 8px; padding: 8px 12px; font: inherit;
}

/* Favorites */
.col-fav { width: 30px; text-align: center; }
button.star {
  background: none; border: 0; padding: 4px 6px;
  font-size: 18px; color: #2a3140; cursor: pointer;
  transition: transform .15s;
}
button.star:hover { transform: scale(1.2); }
button.star.on { color: var(--accent); text-shadow: 0 0 6px rgba(255,106,0,0.6); }
tbody tr.fav td { background: rgba(255, 178, 69, 0.10); }
tbody tr.fav.lady td { background: rgba(255, 122, 182, 0.16); }
tbody tr.fav:hover td { background: rgba(255, 178, 69, 0.18); }

/* Rank-change arrows */
.arrow {
  display: inline-block; font-size: 9px; margin-left: 3px;
  vertical-align: middle; font-weight: 400;
}
.arrow.up { color: var(--green); }
.arrow.down { color: var(--red); }

/* Pull-to-refresh */
.pull-indicator {
  height: 0; overflow: hidden;
  background: linear-gradient(180deg, var(--bg-elev2), transparent);
  color: var(--muted); text-align: center;
  display: flex; align-items: center; justify-content: center;
  transition: height .2s, background .2s;
  font-size: 13px;
}
.pull-indicator.ready {
  background: linear-gradient(180deg, rgba(255,106,0,0.3), transparent);
  color: var(--accent-2); font-weight: 700;
}

/* Responsive */
@media (max-width: 760px) {
  .topbar h1 { font-size: 14px; }
  .meta { font-size: 11px; }
  table { font-size: 12.5px; min-width: 760px; }
  thead th, tbody td { padding: 7px 6px; }
  .vehicle { font-size: 11.5px; }
  .col-vehicle, .col-class { display: none; }
}
@media (max-width: 460px) {
  table { min-width: 600px; }
}
