:root {
  --bg: #050508;
  --panel: #0c0e14;
  --panel2: #12151e;
  --line: #1e2433;
  --text: #eef1f6;
  --muted: #8b93a7;
  --green: #39ff14;
  --red: #ff1744;
  --cyan: #00e5ff;
  --purple: #7c5cff;
  --amber: #ffb300;
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
}
*, *::before, *::after { box-sizing: border-box; }
html, body {
  margin: 0; height: 100%;
  background: var(--bg); color: var(--text);
  font-family: "Segoe UI", system-ui, -apple-system, sans-serif;
  -webkit-tap-highlight-color: transparent;
}
body { padding-top: var(--safe-top); display: flex; flex-direction: column; min-height: 100%; }

.top {
  display: flex; flex-wrap: wrap; align-items: center; gap: 8px 12px;
  padding: 10px 12px; border-bottom: 1px solid var(--line);
  background: linear-gradient(180deg, #0a0c12, #080a10);
  position: sticky; top: 0; z-index: 5;
}
.brand { display: flex; align-items: center; gap: 10px; min-width: 0; }
.brand img {
  width: 40px; height: 40px; border-radius: 50%;
  box-shadow: 0 0 20px rgba(57,255,20,.2);
  border: 1px solid rgba(255,255,255,.12);
}
.brand strong { display: block; font-size: 13px; letter-spacing: .6px; }
.brand small { color: var(--muted); font-size: 11px; font-weight: 500; }

.phase {
  font-weight: 800; letter-spacing: .5px; padding: 6px 12px; border-radius: 8px;
  background: #222; font-size: 12px;
}
.phase.IDLE { color: var(--muted); }
.phase.GRID { color: var(--amber); background: #3a2a00; }
.phase.RACE, .phase.GREEN { color: var(--green); background: #0a2a10; }
.phase.FINISH { color: #fff; background: #4a1020; }

.meta {
  display: flex; flex-wrap: wrap; gap: 8px 14px; font-size: 12px; color: var(--muted);
  margin-left: auto;
}
.meta .online { color: var(--green); }
.meta .offline { color: var(--red); }

.actions { display: flex; flex-wrap: wrap; gap: 6px; }
.actions button {
  background: var(--panel2); border: 1px solid var(--line); color: var(--text);
  padding: 7px 10px; border-radius: 8px; font-size: 11px; cursor: pointer;
  min-height: 36px;
}
.actions button:active { transform: scale(0.97); }
.actions button:hover { border-color: var(--cyan); }

.grid {
  flex: 1; display: grid;
  grid-template-columns: minmax(280px, 1.05fr) minmax(320px, 1.25fr);
  grid-template-rows: 1fr auto;
  gap: 8px; padding: 8px; min-height: 0;
}
.panel {
  background: var(--panel); border: 1px solid var(--line); border-radius: 12px;
  padding: 8px 10px; display: flex; flex-direction: column; min-height: 0;
}
.map-panel { grid-row: 1 / 3; }
.pt {
  font-size: 10px; letter-spacing: 1px; color: var(--muted);
  margin-bottom: 6px; flex-shrink: 0;
}
#map {
  width: 100%; flex: 1; min-height: 220px; background: #050508;
  border-radius: 8px; touch-action: none;
}
.legend {
  display: flex; flex-wrap: wrap; gap: 6px 10px; margin-top: 6px;
  font-size: 11px; max-height: 48px; overflow: auto;
}
.legend span { display: inline-flex; align-items: center; gap: 4px; white-space: nowrap; }
.legend i { width: 10px; height: 10px; border-radius: 50%; display: inline-block; }

.head, .row {
  display: grid;
  grid-template-columns: 32px minmax(70px,1.3fr) .85fr .75fr .85fr .85fr 40px 52px 56px .9fr;
  gap: 4px; align-items: center; font-size: 12px;
}
.head {
  color: var(--muted); font-size: 9px; text-transform: uppercase;
  padding: 0 4px 6px; border-bottom: 1px solid var(--line); flex-shrink: 0;
}
.standings { overflow: auto; flex: 1; -webkit-overflow-scrolling: touch; }
.row {
  padding: 8px 4px; border-bottom: 1px solid #141824; border-radius: 6px;
}
.row.leader { background: linear-gradient(90deg, rgba(57,255,20,.08), transparent); }
.row.stale { opacity: .4; }
.row.dnf { opacity: .55; filter: grayscale(.35); }
.pos { font-weight: 800; font-size: 15px; color: var(--cyan); }
.psn { font-weight: 700; overflow: hidden; text-overflow: ellipsis; }
.team { display: block; font-size: 10px; color: var(--muted); font-weight: 400; }
.gap.lead { color: var(--green); font-weight: 700; }
.best { color: var(--purple); font-weight: 700; }
.tyre {
  display: inline-block; min-width: 40px; text-align: center;
  padding: 2px 4px; border-radius: 4px; font-size: 11px; font-weight: 700; color: #111;
}
.st {
  font-size: 10px; font-weight: 700; padding: 3px 5px; border-radius: 4px; text-align: center;
}
.st.IN_PISTA { background: #0a2a10; color: var(--green); }
.st.OFF_TRACK { background: #3a2000; color: var(--amber); }
.st.FERMO { background: #2a1020; color: var(--red); }
.st.DNF { background: #111; color: #888; }
.st.PAUSA { background: #1a1a2a; color: var(--cyan); }
.st.FINISH { background: #4a1020; color: #fff; }
.flash-up { animation: fu .7s ease; }
.flash-down { animation: fd .7s ease; }
@keyframes fu { from { background: rgba(57,255,20,.35); } to { background: transparent; } }
@keyframes fd { from { background: rgba(255,23,68,.35); } to { background: transparent; } }

.events { overflow: auto; max-height: 150px; font-size: 12px; -webkit-overflow-scrolling: touch; }
.ev { display: flex; gap: 8px; padding: 5px 0; border-bottom: 1px solid #141824; }
.ev time { color: var(--muted); font-size: 10px; min-width: 54px; flex-shrink: 0; }
.ev.POS_UP { color: var(--green); }
.ev.POS_DOWN { color: var(--red); }
.ev.GREEN, .ev.RACE { color: var(--green); font-weight: 700; }
.ev.FINISH { color: var(--red); font-weight: 700; }

.toast {
  position: fixed; bottom: calc(16px + var(--safe-bottom)); left: 50%;
  transform: translateX(-50%); background: #1b5e20; color: #fff;
  padding: 10px 18px; border-radius: 10px; font-size: 13px;
  display: none; z-index: 20; box-shadow: 0 8px 24px rgba(0,0,0,.45);
}
.toast.err { background: #b71c1c; }

.install-bar {
  display: none; position: fixed; left: 12px; right: 12px;
  bottom: calc(12px + var(--safe-bottom));
  background: var(--panel2); border: 1px solid var(--cyan);
  border-radius: 12px; padding: 12px 14px; z-index: 15;
  box-shadow: 0 12px 40px rgba(0,0,0,.5);
  align-items: center; gap: 12px;
}
.install-bar.show { display: flex; }
.install-bar p { margin: 0; flex: 1; font-size: 13px; }
.install-bar button {
  background: var(--green); color: #000; border: 0; font-weight: 700;
  padding: 8px 14px; border-radius: 8px; cursor: pointer;
}
.install-bar .x { background: transparent; color: var(--muted); border: 0; font-size: 18px; cursor: pointer; }

/* Desktop large */
@media (min-width: 1200px) {
  .grid { gap: 12px; padding: 12px; }
  .head, .row { font-size: 13px; }
}

/* Tablet / phone landscape */
@media (max-width: 960px) {
  .grid {
    grid-template-columns: 1fr;
    grid-template-rows: auto auto auto;
    overflow: auto;
  }
  .map-panel { grid-row: auto; min-height: 260px; }
  body { overflow: auto; }
}

/* Phone portrait: compact table */
@media (max-width: 640px) {
  .top { padding: 8px; gap: 6px; }
  .brand strong { font-size: 12px; }
  .meta { width: 100%; margin-left: 0; order: 5; }
  .actions { width: 100%; }
  .actions button { flex: 1; font-size: 10px; padding: 8px 4px; }
  .head, .row {
    grid-template-columns: 28px minmax(56px,1.2fr) .7fr .65fr .7fr 36px 44px .8fr;
    font-size: 11px;
  }
  /* hide last lap + one gap col on very small via CSS order - hide 4th gap ahead keeps leader gap */
  .head span:nth-child(4),
  .row > :nth-child(4),
  .head span:nth-child(6),
  .row > :nth-child(6) { display: none; }
}
