/* Mobile first. Dichte Zahlenkolonnen brauchen Tabellenziffern und ruhige Farben,
   damit die eine Zeile, die zaehlt, sofort ins Auge springt. */

:root {
  --bg: #0b0f14;
  --surface: #131a23;
  --surface-2: #1a232f;
  --line: #253141;
  --text: #e8eef6;
  --muted: #8fa3b8;
  --accent: #ffd21e;
  --accent-ink: #1a1400;
  --up: #3ddc84;
  --down: #ff6b6b;
  --safe-b: env(safe-area-inset-bottom, 0px);
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  /* Nichts darf die Seite breiter machen als den Bildschirm. */
  max-width: 100%; overflow-x: hidden;
  background: var(--bg);
  color: var(--text);
  font: 15px/1.4 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, system-ui, sans-serif;
  -webkit-text-size-adjust: 100%;
  overscroll-behavior-y: contain;
}

body { padding-bottom: calc(24px + var(--safe-b)); }
/* Waehrend das Sheet offen ist, wird die Seite eingefroren. overflow:hidden
   allein ignoriert iOS - position:fixed mit gemerkter Position wirkt. */
body.locked { position: fixed; left: 0; right: 0; width: 100%; overflow: hidden; }

.hidden { display: none !important; }
.muted { color: var(--muted); }
.small { font-size: 12px; }

/* --- Kopfbereich ---------------------------------------------------------- */
.top {
  position: sticky; top: 0; z-index: 20;
  background: linear-gradient(180deg, var(--surface) 0%, rgba(19,26,35,.96) 100%);
  border-bottom: 1px solid var(--line);
  padding: calc(8px + env(safe-area-inset-top, 0px)) 12px 8px;
  backdrop-filter: blur(8px);
}

.top-row { display: flex; align-items: center; gap: 10px; min-width: 0; }

.flag {
  width: 26px; height: 26px; border-radius: 6px; flex: none;
  border: 1px solid var(--line); background: #2a3646;
}
.flag[data-flag="green"]  { background: #21c85a; }
.flag[data-flag="yellow"] { background: #ffd21e; }
.flag[data-flag="red"]    { background: #ff4444; }
.flag[data-flag="purple"] { background: #a06bff; }
.flag[data-flag="checkered"] {
  background-color: #fff;
  background-image:
    linear-gradient(45deg, #000 25%, transparent 25%, transparent 75%, #000 75%),
    linear-gradient(45deg, #000 25%, transparent 25%, transparent 75%, #000 75%);
  background-size: 12px 12px;
  background-position: 0 0, 6px 6px;
}

.run { flex: 1; min-width: 0; }
.run-name {
  font-weight: 700; font-size: 16px;
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.run-event {
  font-size: 12px; color: var(--muted);
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}

.icon-btn {
  flex: none; width: 40px; height: 40px; border-radius: 10px;
  background: var(--surface-2); border: 1px solid var(--line);
  color: var(--text); font-size: 18px; cursor: pointer;
  /* Flex statt Textfluss: sonst sitzt das Zahnrad-Glyph nicht mittig. */
  display: flex; align-items: center; justify-content: center;
  padding: 0; line-height: 1;
}

.stats {
  display: grid; grid-template-columns: repeat(4, minmax(0, 1fr));
  gap: 6px; margin-top: 8px;
}
.stat { background: var(--surface-2); border-radius: 8px; padding: 5px 6px; text-align: center; min-width: 0; }
.stat .v {
  display: block; font-weight: 700; font-size: 14px;
  font-variant-numeric: tabular-nums;
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.stat .k { display: block; font-size: 10px; color: var(--muted); text-transform: uppercase; letter-spacing: .04em; }

/* --- Verbindungsstatus ---------------------------------------------------- */
.conn { display: flex; align-items: center; gap: 6px; padding: 5px 14px; font-size: 12px; }
.conn .dot { width: 7px; height: 7px; border-radius: 50%; background: var(--down); }
.conn.online { color: var(--muted); }
.conn.online .dot { background: var(--up); }
.conn.offline { color: var(--down); background: rgba(255,107,107,.08); }

/* --- Fokus: die beobachteten Fahrer --------------------------------------- */
.focus { padding: 4px 12px 0; display: grid; gap: 10px; }

.card {
  background: var(--surface); border: 1px solid var(--line);
  border-radius: 12px; overflow: hidden;
}
.card.mine { border-color: var(--accent); box-shadow: 0 0 0 1px rgba(255,210,30,.25); }

.card-line {
  display: grid; grid-template-columns: 44px minmax(0, 1fr) auto;
  align-items: center; gap: 8px; padding: 8px 10px;
  font-variant-numeric: tabular-nums;
}
.card-line + .card-line { border-top: 1px solid var(--line); }
.card-line.ctx { background: rgba(255,255,255,.02); color: var(--muted); font-size: 13px; }
.card-line.self { background: rgba(255,210,30,.07); font-size: 15px; font-weight: 600; }

.pos { font-weight: 700; text-align: center; font-size: 15px; }
.card-line.self .pos { color: var(--accent); font-size: 20px; }
.who { min-width: 0; }
.who .nm { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.who .sub { font-size: 11px; color: var(--muted); }
.gap { font-variant-numeric: tabular-nums; font-size: 13px; text-align: right; white-space: nowrap; }

/* --- Ergebnisliste -------------------------------------------------------- */
.list-head {
  display: flex; align-items: baseline; justify-content: space-between;
  padding: 16px 14px 6px;
}
.list-head h2 { font-size: 13px; text-transform: uppercase; letter-spacing: .06em; color: var(--muted); margin: 0; font-weight: 600; }

.rows { list-style: none; margin: 0; padding: 0 8px; }

.row-item {
  display: grid; grid-template-columns: 34px minmax(0, 1fr) auto 26px;
  align-items: center; gap: 8px;
  padding: 9px 6px; border-bottom: 1px solid var(--line);
  font-variant-numeric: tabular-nums;
  cursor: pointer; -webkit-tap-highlight-color: transparent;
  min-height: 48px; /* bequeme Trefferflaeche fuer den Daumen */
}
.row-item:active { background: var(--surface-2); }
.row-item.watched { background: rgba(255,210,30,.08); }

.row-item .p { font-weight: 700; text-align: center; }
.row-item.watched .p { color: var(--accent); }
.row-item .nm { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.row-item .meta { font-size: 11px; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.row-item .t { font-size: 13px; text-align: right; }
.row-item .t .d { font-size: 11px; color: var(--muted); }
.row-item .star { font-size: 17px; text-align: center; color: #3b4a5e; }
.row-item.watched .star { color: var(--accent); }

.delta-up { color: var(--up); }
.delta-down { color: var(--down); }

.empty, .hint { text-align: center; color: var(--muted); font-size: 12px; padding: 14px; }

/* --- Einstellungen -------------------------------------------------------- */
.backdrop {
  position: fixed; inset: 0; background: rgba(0,0,0,.6); z-index: 30;
  touch-action: none; overscroll-behavior: none;
}

.sheet {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 31;
  /* dvh statt vh: in der installierten App weicht vh von der echten Hoehe ab,
     dadurch ragte der obere Teil des Sheets unter die Statusleiste. */
  max-height: calc(100dvh - env(safe-area-inset-top, 0px) - 12px);
  overflow-y: auto; -webkit-overflow-scrolling: touch;
  /* none statt contain: verhindert zuverlaessig, dass die Geste am Ende des
     Sheets auf die Seite dahinter ueberspringt. */
  overscroll-behavior: none;
  touch-action: pan-y;
  background: var(--surface); border-top: 1px solid var(--line);
  border-radius: 16px 16px 0 0;
  animation: rise .22s ease-out;
}
@supports not (height: 100dvh) {
  .sheet { max-height: calc(100vh - env(safe-area-inset-top, 0px) - 60px); }
}
@keyframes rise { from { transform: translateY(100%); } to { transform: translateY(0); } }

.sheet-inner { padding: 8px 16px calc(20px + var(--safe-b)); }
/* Kopf bleibt beim Scrollen stehen: das Kreuz ist immer erreichbar, egal wie
   lang der Inhalt ist. Vorher lag der einzige Schliessen-Knopf ganz unten. */
.sheet-head {
  position: sticky; top: 0; z-index: 2;
  display: flex; align-items: center; justify-content: center;
  height: 46px; padding: 0 8px;
  background: var(--surface);
  border-bottom: 1px solid var(--line);
}
.grabber { width: 40px; height: 4px; border-radius: 2px; background: var(--line); }
.sheet-x {
  position: absolute; right: 8px; top: 6px;
  width: 34px; height: 34px; padding: 0; min-height: 0;
  display: flex; align-items: center; justify-content: center;
  background: var(--surface-2); border: 1px solid var(--line);
  color: var(--text); font-size: 15px; line-height: 1; border-radius: 9px;
}
.sheet-inner { padding-top: 14px; }

.steps em, .no-timing em { font-style: normal; font-weight: 600; color: var(--text); }

.sheet h3 { font-size: 12px; text-transform: uppercase; letter-spacing: .06em; color: var(--muted); margin: 20px 0 8px; }
.sheet h3:first-of-type { margin-top: 4px; }

button {
  font: inherit; border-radius: 10px; padding: 12px 14px;
  border: 1px solid var(--line); cursor: pointer; min-height: 46px;
}
.primary { background: var(--accent); color: var(--accent-ink); border-color: var(--accent); font-weight: 700; width: 100%; }
.ghost { background: var(--surface-2); color: var(--text); }
.danger { background: transparent; color: var(--down); border-color: rgba(255,107,107,.4); width: 100%; margin-top: 10px; }
.wide { width: 100%; margin-top: 8px; }

.row {
  display: flex; align-items: center; justify-content: space-between; gap: 10px;
  padding: 11px 0; border-bottom: 1px solid var(--line);
}
.row.indent { padding-left: 28px; }
.row input[type=checkbox] { width: 22px; height: 22px; accent-color: var(--accent); order: 2; }
.row span { flex: 1; }

.spin { display: flex; align-items: center; gap: 6px; flex: none !important; }
.spin input {
  width: 62px; padding: 8px; text-align: right;
  background: var(--surface-2); border: 1px solid var(--line);
  border-radius: 8px; color: var(--text); font: inherit;
  font-variant-numeric: tabular-nums;
}
.unit { font-size: 12px; color: var(--muted); flex: none !important; }

.text {
  width: 100%; padding: 12px; background: var(--surface-2);
  border: 1px solid var(--line); border-radius: 10px; color: var(--text); font: inherit;
}

.watchlist { list-style: none; margin: 0; padding: 0; }
.watchlist li {
  display: flex; align-items: center; justify-content: space-between;
  padding: 10px 0; border-bottom: 1px solid var(--line);
}
.watchlist button {
  min-height: 34px; padding: 6px 12px; background: transparent;
  color: var(--down); border-color: rgba(255,107,107,.35); font-size: 13px;
}

.sheet-actions { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; margin-top: 18px; }

/* --- Toast ---------------------------------------------------------------- */
.toast {
  position: fixed; left: 50%; bottom: calc(24px + var(--safe-b));
  transform: translateX(-50%) translateY(20px);
  background: var(--surface-2); border: 1px solid var(--line);
  color: var(--text); padding: 11px 18px; border-radius: 24px;
  font-size: 14px; opacity: 0; pointer-events: none;
  transition: opacity .2s, transform .2s; z-index: 40;
  max-width: 90vw; text-align: center;
}
.toast.show { opacity: 1; transform: translateX(-50%) translateY(0); }

@media (min-width: 640px) {
  main, .top { max-width: 640px; margin-inline: auto; }
  .sheet { max-width: 640px; margin-inline: auto; border-radius: 16px 16px 0 0; }
}

/* --- Startnummern-Eingabe ------------------------------------------------- */
.add-nr { display: flex; gap: 8px; margin-top: 10px; }
.add-nr .text { flex: 1; }
.add-nr button { flex: none; }

button:disabled { opacity: .45; cursor: not-allowed; }

/* --- Pull to refresh ------------------------------------------------------ */
.ptr {
  position: fixed; top: -56px; left: 0; right: 0; z-index: 25;
  display: flex; align-items: center; justify-content: center; gap: 8px;
  height: 56px;
  color: var(--muted); font-size: 13px;
  background: var(--bg);
  /* transform statt margin: bewegt sich ohne Layout-Neuberechnung */
  transform: translate3d(0, 0, 0);
  transition: transform .18s ease-out;
  will-change: transform;
  pointer-events: none;
}
.ptr.dragging { transition: none; }
.ptr.spinning { transform: translate3d(0, 56px, 0); }
.ptr #ptrIcon { display: inline-block; transition: transform .2s; font-size: 16px; }
.ptr.ready #ptrIcon { transform: rotate(180deg); }
.ptr.spinning #ptrIcon { animation: spin .8s linear infinite; }
.rows { will-change: auto; }
@keyframes spin { to { transform: rotate(360deg); } }

.reload-wrap { display: flex; justify-content: center; padding: 4px 14px 28px; }
.reload-wrap button { min-width: 180px; }

/* --- Kein Timing verfuegbar ----------------------------------------------- */
.no-timing {
  margin: 18px 14px; padding: 28px 20px; text-align: center;
  background: var(--surface); border: 1px solid var(--line); border-radius: 14px;
}
.no-timing h2 { font-size: 18px; margin: 14px 0 8px; }
.no-timing p { color: var(--muted); font-size: 14px; margin: 0 0 18px; line-height: 1.5; }

/* Kleine Zielflagge als Zeichen, dass hier gerade nichts laeuft. */
.nt-flag {
  width: 56px; height: 40px; margin: 0 auto; border-radius: 4px; opacity: .5;
  background-color: #fff;
  background-image:
    linear-gradient(45deg, #000 25%, transparent 25%, transparent 75%, #000 75%),
    linear-gradient(45deg, #000 25%, transparent 25%, transparent 75%, #000 75%);
  background-size: 14px 14px;
  background-position: 0 0, 7px 7px;
}

.nt-link {
  display: inline-block; padding: 12px 18px; border-radius: 10px;
  background: var(--accent); color: var(--accent-ink);
  font-weight: 700; text-decoration: none; font-size: 14px;
}

/* --- Startleiste unten ---------------------------------------------------- */
.start-bar {
  position: sticky; bottom: 0; z-index: 22;
  padding: 12px 14px calc(12px + var(--safe-b));
  background: linear-gradient(180deg, rgba(11,15,20,0) 0%, var(--bg) 28%);
  text-align: center;
}
.start-bar p { margin: 8px 0 0; }
@media (min-width: 640px) { .start-bar { max-width: 640px; margin-inline: auto; } }

.row.dimmed { opacity: .4; }
.row.dimmed input { pointer-events: none; }

/* --- Einrichtungsanleitung ------------------------------------------------ */
.steps { list-style: none; counter-reset: step; margin: 0 0 16px; padding: 0; }

.steps li {
  position: relative; counter-increment: step;
  padding: 0 0 16px 40px; font-size: 14px; line-height: 1.45;
}
/* Verbindungslinie zwischen den Schritten */
.steps li::after {
  content: ""; position: absolute; left: 13px; top: 28px; bottom: 2px;
  width: 2px; background: var(--line);
}
.steps li:last-child { padding-bottom: 4px; }
.steps li:last-child::after { display: none; }

.steps li::before {
  content: counter(step);
  position: absolute; left: 0; top: 0;
  width: 28px; height: 28px; border-radius: 50%;
  background: var(--surface-2); border: 1px solid var(--line);
  color: var(--muted); font-size: 13px; font-weight: 700;
  display: flex; align-items: center; justify-content: center;
}
.steps li.done::before {
  content: "✓"; background: var(--up); border-color: var(--up); color: #062313;
}
.steps li.done { color: var(--muted); }
.steps li.now::before { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); }

.steps b { color: var(--text); font-weight: 600; }
.steps .sub { display: block; color: var(--muted); font-size: 12.5px; margin-top: 2px; }

/* Inline-Symbole in der Anleitung, damit man sie am Gerät wiedererkennt */
.gl {
  display: inline-flex; align-items: center; justify-content: center;
  vertical-align: -4px; width: 20px; height: 20px; margin: 0 1px;
  border-radius: 5px; background: var(--surface-2); border: 1px solid var(--line);
}
.gl svg { width: 13px; height: 13px; fill: none; stroke: var(--text); stroke-width: 1.8;
          stroke-linecap: round; stroke-linejoin: round; }

/* --- Hinweis auf eine neue Fassung ---------------------------------------- */
.update-bar {
  position: sticky; top: 0; z-index: 26;
  width: 100%; border: 0; border-radius: 0;
  padding: calc(10px + env(safe-area-inset-top, 0px)) 14px 10px;
  background: var(--accent); color: var(--accent-ink);
  font-size: 13px; font-weight: 700; text-align: center;
}

/* --- Status pro Fahrer ---------------------------------------------------- */
.arrow { font-size: 12px; }
.m-ok    { color: var(--up); }
.m-warn  { color: var(--accent); font-weight: 700; }
.m-pause { color: var(--muted); font-size: 10px; letter-spacing: -1px; }
.m-fin   { color: var(--text); }

/* DNS/DNF/DSQ passen nicht als Zahl in die Spalte */
.p.status, .pos.status {
  font-size: 10.5px; letter-spacing: .02em; color: var(--muted); font-weight: 700;
}
