/* ---------------------------------------------------------------------------
   F1 PIT STRATEGY — round 3: timing tower / telemetria. Carbonio, rosso F1
   (#e10600) come unico accento caldo, giallo timing (#ffd400) solo per
   best/PB, hairline bianche, angoli smussati (clip-path) al posto dei raggi,
   tick dei tre settori (viola/verde/giallo), filo rosso sotto l'header.
   Barlow Condensed (titoli) + IBM Plex Mono (numeri) + Titillium (testo).
   Le regole "ROUND 3" in fondo al file sovrascrivono la base.
--------------------------------------------------------------------------- */
:root {
  --bg: #0b0c0f;
  --panel: #141519;
  --panel-2: #1b1d22;
  --grid: #26282f;
  --axis: #565b66;
  --text: #e8e9ec;
  --text-dim: #8b8f99;
  --live: #e10600;
  --live-text: #ff5a52;
  /* mescole = team colors funzionali */
  --soft: #ff1744;
  --medium: #ffc107;
  --hard: #9e9e9e;
  --inter: #4caf50;
  --green: #2ecc71;
  --red: #e74c3c;
  --purple: #9b59b6;
  --radius: 4px;
  --font-disp: 'Barlow Condensed', 'Titillium Web', system-ui, sans-serif;
}

* { box-sizing: border-box; margin: 0; padding: 0; }

body {
  background: var(--bg);
  color: var(--text);
  font-family: 'Titillium Web', system-ui, sans-serif;
  font-size: 14px;
  line-height: 1.5;
  font-variant-numeric: tabular-nums;
  min-height: 100vh;
  min-height: 100dvh;
  display: flex;
  flex-direction: column;
}
.mono, code { font-family: 'IBM Plex Mono', monospace; }
::selection { background: var(--text); color: var(--bg); }

/* -------------------------------------------------------------- navbar --- */
.navbar {
  display: flex;
  align-items: center;
  gap: 24px;
  /* Pinned alla colonna contenuto: .layout ha 20px di padding laterale,
     percio' si aggiunge +20px per allineare brand/tab al contenuto. */
  padding: 0 max(20px, calc((100% - 1440px) / 2 + 20px));
  min-height: 52px;
  background: var(--panel);
  border-bottom: 1px solid var(--grid);
  position: sticky;
  top: 0;
  z-index: 20;
}
.nav-brand {
  font-family: 'IBM Plex Mono', monospace;
  font-weight: 600;
  letter-spacing: 0.22em;
  font-size: 15px;
  color: var(--text);
  text-decoration: none;
  white-space: nowrap;
  flex-shrink: 0;
}
.nav-back {
  display: inline-flex; align-items: center; justify-content: center;
  width: 26px; height: 26px; flex-shrink: 0;
  color: var(--text-dim);
  border: 1px solid var(--grid);
  border-radius: 4px;
  background: transparent;
  text-decoration: none;
  transition: color .15s, border-color .15s, background .15s;
}
.nav-back svg { display: block; }
.nav-back:hover { color: var(--text); border-color: var(--text-dim); background: var(--panel-2); }
.nav-tabs { display: flex; align-self: stretch; }
.nav-tab {
  background: none;
  border: none;
  border-bottom: 2px solid transparent;
  color: var(--text-dim);
  font-family: 'IBM Plex Mono', monospace;
  font-size: 12px;
  letter-spacing: 0.14em;
  padding: 0 16px;
  min-height: 44px;
  cursor: pointer;
  text-decoration: none;
  display: flex;
  align-items: center;
  white-space: nowrap;
}
.nav-tab.on { color: var(--text); border-bottom-color: var(--text); }
.nav-tab:hover { color: var(--text); }
.live-badge {
  margin-left: auto;
  font-family: 'IBM Plex Mono', monospace;
  font-size: 11px;
  letter-spacing: 0.12em;
  color: var(--live-text);
  white-space: nowrap;
  flex-shrink: 0;
}
.live-badge.off { color: var(--text-dim); }
.nv-red { color: var(--live-text); }

/* -------------------------------------------------------------- layout --- */
/* Pagina a scorrimento naturale: la sidebar è sticky, niente scroll innestati. */
.layout {
  display: grid;
  grid-template-columns: 300px minmax(0, 1fr);
  gap: 28px;
  align-items: start;
  width: 100%;
  max-width: 1440px;
  margin: 0 auto;
  padding: 32px 20px 20px;
  flex: 1;
}
.layout.single { display: block; max-width: 1024px; }

.main { min-width: 0; }

/* ------------------------------------------------------------- control --- */
.control-panel {
  background: var(--panel);
  border: 1px solid var(--grid);
  border-radius: 4px;
  padding: 18px 16px;
  display: flex;
  flex-direction: column;
  gap: 14px;
  position: sticky;
  top: 72px;
}
.panel-title {
  font-family: 'IBM Plex Mono', monospace;
  font-size: 11px;
  letter-spacing: 0.22em;
  color: var(--text-dim);
  display: flex;
  align-items: center;
  gap: 10px;
}
.panel-title::after { content: ""; flex: 1; height: 1px; background: var(--grid); }
.setup-grid { display: grid; grid-template-columns: 1fr; gap: 14px; counter-reset: setup; }
.setup-grid .field { counter-increment: setup; }
.setup-grid .field::before {
  content: "0" counter(setup);
  font-family: 'IBM Plex Mono', monospace;
  font-size: 10px;
  letter-spacing: 0.2em;
  color: var(--text-dim);
}
.field {
  display: flex; flex-direction: column; gap: 6px;
  font-family: 'IBM Plex Mono', monospace;
  font-size: 11px;
  letter-spacing: 0.06em; text-transform: uppercase;
  color: var(--text-dim);
  min-width: 0;
}
.field select, .field input, .field textarea {
  background: var(--panel-2);
  color: var(--text);
  border: 1px solid var(--grid);
  border-radius: 4px;
  padding: 8px 10px;
  min-height: 44px;
  font-family: 'Titillium Web', system-ui, sans-serif;
  font-size: 14px;
  text-transform: none;
  letter-spacing: normal;
  width: 100%;
  outline: none;
}
.field select:focus-visible,
.start-btn:focus-visible,
.export-btn:focus-visible,
button:focus-visible,
a:focus-visible,
input:focus-visible,
textarea:focus-visible {
  outline: 2px solid var(--text);
  outline-offset: 1px;
}
.field textarea {
  resize: vertical;
  min-height: 90px;
  line-height: 1.55;
  font-size: 13px;
}
.hint { font-size: 12px; color: var(--text-dim); line-height: 1.5; }

.start-btn {
  padding: 12px;
  min-height: 50px;
  font-family: 'IBM Plex Mono', monospace;
  font-size: 13px;
  letter-spacing: 0.12em;
  background: var(--text);
  color: var(--bg);
  border: none;
  border-radius: 4px;
  cursor: pointer;
  font-weight: 600;
  text-transform: uppercase;
}
.start-btn:hover:not(:disabled) { filter: brightness(1.08); }
.start-btn:active:not(:disabled) { transform: translateY(1px); }
.start-btn:disabled { opacity: 0.3; cursor: not-allowed; }
.start-btn.live { background: var(--live); color: #fff; }

/* ---------------------------------------------------------------- main --- */
/* AI notice: resta il componente condiviso, qui solo un ritocco mono */
.ai-notice { font-family: 'IBM Plex Mono', monospace; }

.page-head { margin-bottom: 20px; }
.page-head .kicker {
  font-family: 'IBM Plex Mono', monospace;
  font-size: 10px;
  letter-spacing: 0.28em;
  color: var(--text-dim);
  margin-bottom: 8px;
  display: flex;
  align-items: center;
  gap: 12px;
}
.page-head .kicker::after { content: ""; height: 1px; width: 72px; background: var(--grid); }
.page-head h1 {
  font-size: clamp(26px, 3.4vw, 36px);
  font-weight: 700;
  letter-spacing: -0.01em;
  line-height: 1.05;
}
.page-head .sub { color: var(--text-dim); font-size: 14px; margin-top: 8px; max-width: 62ch; }

.res-title {
  font-family: 'IBM Plex Mono', monospace;
  font-size: 13px;
  letter-spacing: 0.12em;
  text-transform: uppercase;
  margin-bottom: 12px;
}
.res-title::before {
  content: "";
  display: inline-block;
  width: 8px; height: 8px;
  border-radius: 50%;
  background: var(--green);
  margin-right: 8px;
}

/* error box (predictor) */
.error-box {
  display: none;
  font-family: 'IBM Plex Mono', monospace;
  font-size: 11px;
  background: var(--panel);
  border: 1px solid var(--grid);
  border-left: 3px solid var(--live);
  border-radius: 4px;
  padding: 10px 12px;
  color: var(--text);
  margin-bottom: 12px;
}

/* ------------------------------------------------------------ results --- */
#results.show { animation: rise .5s ease both; }
@keyframes rise {
  from { opacity: 0; transform: translateY(10px); }
  to { opacity: 1; transform: none; }
}
.charts { display: flex; flex-direction: column; gap: 12px; }
.chart-block {
  background: var(--panel);
  border: 1px solid var(--grid);
  border-radius: var(--radius);
  padding: 16px 16px 18px;
  min-width: 0;
}
.chart-label {
  font-family: 'IBM Plex Mono', monospace;
  font-size: 10px;
  letter-spacing: 0.24em;
  color: var(--text-dim);
  padding: 2px 4px 12px;
  text-transform: uppercase;
}

/* stint timeline (piano stint): barre proporzionali ai giri */
.stint-timeline { display: flex; gap: 6px; padding: 2px 6px 4px; }
.stint-seg {
  flex: 0 1 auto;
  min-width: 0;
  overflow: hidden;
  display: flex; flex-direction: column; align-items: center; justify-content: center;
  gap: 2px;
  padding: 10px 8px;
  background: var(--panel-2);
  border-radius: 4px;
  border-top: 3px solid var(--team, var(--grid));
}
.stint-seg .dot {
  width: 8px; height: 8px; border-radius: 50%;
  background: var(--team, var(--grid));
  flex-shrink: 0;
}
.stint-seg .cmp {
  font-family: 'IBM Plex Mono', monospace; font-size: 12px; font-weight: 600;
  color: var(--team, var(--text-dim));
  white-space: nowrap;
}
.stint-seg .laps {
  font-family: 'IBM Plex Mono', monospace; font-size: 10px; color: var(--text-dim);
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis; max-width: 100%;
}
.stint-seg:hover { filter: brightness(1.18); }
.giro-range {
  display: flex; justify-content: space-between;
  padding: 6px 8px 2px;
  font-family: 'IBM Plex Mono', monospace; font-size: 10px; color: var(--text-dim);
}

/* team color ridondante: bordo + lettera + dot (mai più tinta sbagliata) */
.stint-seg.seg-S { --team: #ff1744; }
.stint-seg.seg-M { --team: #ffc107; }
.stint-seg.seg-H { --team: #9e9e9e; }
.stint-seg.seg-I { --team: #4caf50; }
.stint-seg.seg-U { --team: #565b66; }

/* indicatori a tutta larghezza */
.kpi-grid { display: grid; grid-template-columns: repeat(4, 1fr); gap: 8px; padding: 2px 6px 4px; }
.stat {
  background: var(--panel);
  border: 1px solid var(--grid);
  border-radius: 4px;
  padding: 10px 12px;
  display: flex; flex-direction: column; gap: 3px;
  min-width: 0;
}
.kpi-grid .stat { background: transparent; }
.stat-label, .stat .k {
  font-family: 'IBM Plex Mono', monospace; font-size: 10px;
  letter-spacing: 0.2em; color: var(--text-dim); text-transform: uppercase;
}
.stat-value, .stat .v {
  font-family: var(--font-disp);
  font-size: 26px; font-weight: 600; letter-spacing: 0.02em; line-height: 1;
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}
.stat-sub { font-size: 10px; color: var(--text-dim); }
/* etichette su una riga sola: stesse altezze tra card sorelle */
.stat-grid .stat-label { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; display: block; }
.stat-label .lbl-short { display: none; }

/* card statistiche (grafico + tabelle): padding stretto, grafico a tutta card */
.chart-block.chart-tight { padding: 14px 14px 12px; }
.chart-tight .chart-label { padding: 0 0 10px; }

/* tabella scrollabile con header fisso */
.tbl-wrap { overflow-x: auto; padding: 2px 6px 4px; }
.lap-scroll { max-height: 420px; overflow-y: auto; }
.lap-scroll thead th { position: sticky; top: 0; z-index: 2; }
.lap-tbl { border-collapse: collapse; width: 100%; }
.lap-tbl th {
  font-size: 10px; font-weight: 600;
  letter-spacing: 0.14em; text-transform: uppercase; color: var(--text-dim);
  background: var(--panel); padding: 8px 10px; text-align: center;
  border-bottom: 1px solid var(--grid);
  font-family: 'Titillium Web', sans-serif;
  white-space: nowrap;
}
.lap-tbl th:first-child { text-align: left; }
.lap-tbl td {
  font-family: 'IBM Plex Mono', monospace; text-align: center;
  font-size: 12px; white-space: nowrap;
  padding: 7px 10px;
  border-bottom: 1px solid #1d1f26;
}
.lap-tbl tbody tr:last-child td { border-bottom: none; }
.lap-tbl td:first-child { text-align: left; font-weight: 600; }
.lap-tbl tbody tr:hover td { background: rgba(255, 255, 255, 0.025); }
.cmp {
  display: inline-block; min-width: 56px;
  font-size: 10px; font-weight: 600; letter-spacing: 0.1em;
  padding: 2px 8px; border-radius: 3px;
  border: 1px solid currentColor;
  border-left-width: 3px;
}
.cmp-S { background: rgba(255,23,68,.14); color: #ff5c7a; }
.cmp-M { background: rgba(255,193,7,.13); color: #ffd54d; }
.cmp-H { background: rgba(158,158,158,.14); color: #cfcfcf; }
.cmp-I { background: rgba(76,175,80,.14); color: #6fdc73; }
.cmp-U { background: rgba(86,91,102,.16); color: #8b8f99; }
.ev-real {
  display: inline-block; font-size: 10px; font-weight: 600; letter-spacing: 0.08em;
  padding: 1px 6px; border-radius: 3px; margin-right: 4px;
  background: rgba(232, 233, 236, 0.14); color: #e8e9ec;
}
.ev-pred {
  display: inline-block; font-size: 10px; font-weight: 600; letter-spacing: 0.08em;
  padding: 1px 6px; border-radius: 3px; margin-right: 4px;
  background: rgba(95, 217, 255, 0.13); color: #5fd9ff;
}
tr.is-real-pit td { box-shadow: inset 3px 0 0 var(--green); }
td.prob-high { color: var(--red); font-weight: 600; box-shadow: inset 3px 0 0 var(--red); }
td.prob-med { color: var(--medium); }

/* download finali, evidenti */
.dl-bar { display: none; flex-direction: column; gap: 8px; margin: 4px 0 8px; }
.dl-bar .start-btn { width: 100%; }
.dl-bar .export-btn { align-self: center; min-height: 44px; }

/* export buttons */
.export-btn {
  font-family: 'IBM Plex Mono', monospace;
  font-size: 11px;
  letter-spacing: 0.08em;
  color: var(--text-dim);
  background: var(--panel-2);
  border: 1px solid var(--grid);
  border-radius: 4px;
  padding: 8px 12px;
  min-height: 44px;
  cursor: pointer;
  text-transform: uppercase;
}
.export-btn:hover { color: var(--text); border-color: var(--text-dim); }
.export-btn.danger:hover { border-color: var(--red); color: #ff6b5b; }

/* loader = macchinina che gira sulla mini pista */
.sim-loader { display: none; margin-bottom: 12px; }
.sim-loader.show { display: block; }
.race-loader {
  background: var(--panel);
  border: 1px solid var(--grid);
  border-radius: var(--radius);
  padding: 18px 16px 14px;
  max-width: 560px;
  margin: 0 auto;
}
/* loader sobrio: riga di avanzamento indeterminata + messaggi di fase */
.rl-kicker {
  font-family: 'IBM Plex Mono', monospace; font-size: 10px;
  letter-spacing: 0.26em; color: var(--text-dim); text-align: center;
}
.rl-bar {
  height: 2px; background: var(--grid); border-radius: 1px;
  overflow: hidden; margin: 14px 4px 4px;
}
.rl-bar > span {
  display: block; height: 100%; width: 30%;
  background: var(--text); border-radius: 1px;
  animation: rl-slide 1.2s ease-in-out infinite;
}
@keyframes rl-slide {
  0% { transform: translateX(-110%); }
  100% { transform: translateX(360%); }
}
.sk-note {
  text-align: center; color: var(--text-dim); font-size: 12px;
  font-family: 'IBM Plex Mono', monospace; margin-top: 10px;
}

/* empty state */
.empty-state {
  display: flex; flex-direction: column; align-items: center;
  justify-content: center; gap: 10px; min-height: 280px; padding: 40px 20px;
  text-align: center; color: var(--text-dim);
}
.empty-icon { color: var(--text-dim); }
.empty-title { font-size: 16px; font-weight: 600; color: var(--text); letter-spacing: 0.02em; }
.empty-hint { font-size: 13px; color: var(--text-dim); max-width: 420px; line-height: 1.5; }

/* ------------------------------------------------------------- drivers --- */
/* (tab Stats: riuso stat-grid + history-table del template) */
.stat-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(150px, 1fr));
  gap: 10px;
  margin-bottom: 16px;
}
/* righe sempre complete: 4 card → 4 o 2+2 · 6 card → 3+3 o 2+2+2 */
.stat-grid.cols-4 { grid-template-columns: repeat(4, 1fr); }
.stat-grid.cols-6 { grid-template-columns: repeat(3, 1fr); }
.history-table { width: 100%; border-collapse: collapse; font-size: 13px; }
.history-table th {
  font-family: 'IBM Plex Mono', monospace; font-size: 10px;
  letter-spacing: 0.18em; color: var(--text-dim); text-align: left;
  padding: 8px 12px; border-bottom: 1px solid var(--grid);
  text-transform: uppercase; white-space: nowrap;
}
.history-table { font-variant-numeric: tabular-nums; }
.history-table td { padding: 9px 12px; border-bottom: 1px solid var(--grid); vertical-align: middle; }
.history-table td, .history-table th { white-space: nowrap; }
.history-table .col-date { width: 1%; min-width: 128px; }
.history-table th.num, .history-table td.num { text-align: right; min-width: 72px; }
.history-table td.mono { font-size: 12px; }
.history-table tr:hover td { background: var(--panel); }
.history-table td.num, .history-table th.num { text-align: right; font-family: 'IBM Plex Mono', monospace; }
.paid { color: var(--green); font-family: 'IBM Plex Mono', monospace; font-size: 12px; }
.unpaid { color: var(--medium); font-family: 'IBM Plex Mono', monospace; font-size: 12px; }
#sc { max-width: 100%; height: auto; display: block; margin: 0 auto; }
#info-ai { scroll-margin-top: 96px; }

/* --------------------------------------------------------------- about --- */
.about-view { max-width: 880px; margin: 0 auto; width: 100%; }
.about-block { margin-bottom: 32px; border-top: 1px solid var(--grid); padding-top: 20px; }
.about-block:first-child { border-top: none; padding-top: 0; }
.about-block h2 { font-size: 20px; font-weight: 700; margin-bottom: 12px; letter-spacing: 0.02em; }
.about-block h3 {
  font-family: 'IBM Plex Mono', monospace; font-size: 11px; font-weight: 600;
  letter-spacing: 0.22em; text-transform: uppercase;
  color: var(--text-dim); margin: 18px 0 8px;
}
.about-block p { font-size: 14px; line-height: 1.65; color: var(--text-dim); margin-bottom: 12px; }
.about-block p strong { color: var(--text); }
.about-block a { color: var(--text); border-bottom: 1px solid var(--grid); text-decoration: none; }
.about-block a:hover { border-bottom-color: var(--text); }
.stack-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.stack-item {
  background: var(--panel-2); border: 1px solid var(--grid);
  border-radius: 4px; padding: 10px 14px;
  display: flex; flex-direction: column; gap: 2px;
  min-width: 0;
}
.stack-name { font-family: 'IBM Plex Mono', monospace; font-size: 13px; font-weight: 600; color: var(--text); }
.stack-role { font-size: 12px; color: var(--text-dim); }
/* guida rapida: step numerati editoriali */
.steps { list-style: none; counter-reset: step; display: flex; flex-direction: column; }
.steps li {
  counter-increment: step;
  display: flex; gap: 8px; align-items: baseline;
  padding: 10px 0; border-bottom: 1px solid #1d1f26;
  font-size: 14px; line-height: 1.6; color: var(--text-dim);
}
.steps li:last-child { border-bottom: none; }
.steps li::before {
  content: "0" counter(step);
  font-family: 'IBM Plex Mono', monospace; font-size: 11px;
  color: var(--text); flex-shrink: 0; min-width: 20px;
}
.steps li strong { color: var(--text); font-weight: 600; }
.about-note { font-style: italic; }
/* blocco AI in evidenza: giallino + parole rosse F1 */
.about-block.warn {
  background: rgba(255, 193, 7, 0.05);
  border: 1px solid rgba(255, 193, 7, 0.25);
  border-radius: var(--radius);
  padding: 16px 18px;
}
.about-block.warn h2 { color: var(--medium); }
.about-block.warn .stack-item { background: rgba(255, 193, 7, 0.04); }
.f1red { color: #ff5c5c; font-weight: 600; }
.res-links { display: grid; grid-template-columns: repeat(4, 1fr); gap: 6px; margin-top: 4px; }
.res-links > a, .res-links > span {
  font-family: 'IBM Plex Mono', monospace; font-size: 11px; letter-spacing: 0.08em;
  color: var(--text-dim); background: var(--panel-2);
  border: 1px solid var(--grid); border-radius: 4px; padding: 8px 12px;
  min-height: 44px;
  display: inline-flex; align-items: center; justify-content: center; text-align: center;
  text-decoration: none;
}
.res-links > a:hover { color: var(--text); border-color: var(--text-dim); }
.res-links > span { opacity: 0.45; cursor: not-allowed; }

/* -------------------------------------------------------------- testing --- */
.test-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; margin-bottom: 10px; }
.status-bar {
  font-family: 'IBM Plex Mono', monospace;
  font-size: 12px;
  color: var(--text-dim);
  padding: 10px 12px;
  background: var(--panel);
  border: 1px solid var(--grid);
  border-radius: 4px;
  margin-bottom: 12px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  flex-wrap: wrap;
}
.test-msgs { display: flex; flex-direction: column; gap: 8px; margin-top: 10px; }
.test-msg {
  background: var(--panel-2); border: 1px solid var(--grid);
  border-left: 3px solid var(--axis); border-radius: 4px;
  padding: 10px 12px;
  min-width: 0;
}
.test-msg .phase {
  font-family: 'IBM Plex Mono', monospace; font-size: 10px;
  letter-spacing: 0.14em; color: var(--text-dim); margin-bottom: 6px;
  text-transform: uppercase;
}
.test-msg pre {
  margin: 0; white-space: pre-wrap; word-break: break-word;
  font-size: 11px; font-family: 'IBM Plex Mono', monospace;
  color: var(--text);
}
.radio-row input:focus-visible { outline: 2px solid var(--text); outline-offset: 2px; }
.radio-row { display: flex; gap: 12px; align-items: center; flex-wrap: wrap; font-size: 12px; }
.radio-row label { display: inline-flex; align-items: center; gap: 6px; cursor: pointer; min-height: 44px; padding-right: 6px; }
.radio-row input[type="radio"] { width: 18px; height: 18px; accent-color: var(--text); flex-shrink: 0; }
.endpoint-line { display: flex; gap: 10px; align-items: center; margin-bottom: 10px; flex-wrap: wrap; }
.endpoint-line .k { font-family: 'IBM Plex Mono', monospace; font-size: 10px; letter-spacing: 0.18em; color: var(--text-dim); min-width: 70px; }
.endpoint-line code { word-break: break-all; }

/* reveal (mantenuto dal progetto, ammorbidito) */
.reveal { opacity: 0; transform: translateY(14px); transition: opacity .5s ease, transform .5s ease; }
.reveal.visible { opacity: 1; transform: translateY(0); }

@media (prefers-reduced-motion: reduce) {
  .reveal { transition: none; }
  .rl-bar > span { animation: none; }
  #results.show { animation: none; }
}

/* ============================ schermi medi ============================ */
@media (max-width: 1020px) {
  /* Sidebar impilata, ma titolo + banner AI restano SEMPRE sopra la card:
     il main si "apre" (display:contents) così i suoi figli diventano item del
     layout e possiamo ordinarli (titolo/AI = 1, card = 2, risultati = 3). */
  /* align-items: stretch obbligatorio: la regola base .layout ha
     align-items: start, che in colonna fa restringere i blocchi al contenuto
     e lascia una colonna vuota a destra. */
  .layout:not(.single) { display: flex; flex-direction: column; gap: 16px; align-items: stretch; }
  .layout:not(.single) > * { width: 100%; }
  .layout:not(.single) .control-panel { position: static; order: 2; }
  .layout:not(.single) .main { display: contents; }
  .layout:not(.single) .main > .page-head { order: 1; }
  .layout:not(.single) .main > .ai-notice { order: 1; }
  .layout:not(.single) .main > :not(.page-head):not(.ai-notice) { order: 3; }
  .setup-grid { grid-template-columns: 1fr 1fr; }
  .setup-grid .span-all { grid-column: 1 / -1; }
}

/* ============================ schermi piccoli ============================ */
@media (max-width: 640px) {
  body { font-size: 14px; }
  .layout, .layout.single { padding: 14px 12px 20px; }

  /* navbar su due righe: brand + badge sopra, tab scrollabili sotto */
  .navbar { flex-wrap: wrap; gap: 6px 10px; padding: 8px 12px; }
  .nav-brand { font-size: 19px; }
  .nav-tabs {
    order: 3; flex-basis: 100%;
    align-self: auto;
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
  }
  .nav-tab { font-size: 11px; letter-spacing: 0.08em; padding: 0 14px; min-height: 44px; flex-shrink: 0; }
  .live-badge { font-size: 10px; letter-spacing: 0.06em; }

  .page-head h1 { font-size: 24px; }
  .page-head .sub { font-size: 13px; }

  /* setup compatto: pilota intero, stagione + GP affiancati */
  .control-panel { padding: 16px 14px; }
  .field select { font-size: 16px; } /* niente auto-zoom su iOS */
  .start-btn { width: 100%; }

  /* timeline in verticale: una card per stint, niente più schiacciamento */
  .stint-timeline { flex-direction: column; }
  .stint-seg {
    width: 100% !important;
    flex-direction: row;
    align-items: center;
    text-align: left;
    gap: 10px;
    padding: 10px 12px;
    border-top: none;
    border-left: 3px solid var(--team, var(--grid));
  }
  .stint-seg .dot { margin: 0; }
  .stint-seg .laps { margin-left: auto; text-align: right; }

  .kpi-grid { grid-template-columns: 1fr 1fr; }

  /* tabella giri → cards: una card per giro, header nascosto */
  .lap-scroll { max-height: none; overflow: visible; }
  .lap-tbl, .lap-tbl tbody { display: block; width: 100%; }
  .lap-tbl { border-spacing: 0; }
  .lap-tbl thead { display: none; }
  .lap-tbl tbody tr {
    display: block;
    background: var(--panel-2);
    border: 1px solid var(--grid);
    border-radius: 4px;
    margin-bottom: 8px;
    padding: 4px 6px;
  }
  .lap-tbl tbody tr.is-real-pit { border-left: 3px solid var(--green); }
  .lap-tbl tbody tr.is-real-pit td { box-shadow: none; }
  .lap-tbl td, .lap-tbl td:first-child {
    display: flex;
    justify-content: space-between;
    align-items: center;
    gap: 12px;
    background: none;
    border-radius: 0;
    text-align: right;
    font-size: 13px;
    padding: 7px 8px;
    white-space: normal;
  }
  .lap-tbl td::before {
    content: attr(data-label);
    font-family: 'Titillium Web', sans-serif;
    font-size: 10px; font-weight: 600;
    letter-spacing: 0.12em; text-transform: uppercase;
    color: var(--text-dim);
    text-align: left;
    flex-shrink: 0;
  }
  td.prob-high { box-shadow: none; }
  td.prob-high::after { content: ""; }

  .stat-value, .stat .v { font-size: 22px; }
  .stat-grid.cols-4, .stat-grid.cols-6 { grid-template-columns: 1fr 1fr; }
  .res-links { grid-template-columns: 1fr 1fr; }
  .history-table { font-size: 12px; }
  .history-table th, .history-table td { padding: 8px 6px; }
  .history-table .col-date { min-width: 118px; }
  .history-table th.num, .history-table td.num { min-width: 56px; }
  .stat-label .lbl-long { display: none; }
  .stat-label .lbl-short { display: inline; }
  #info-ai { scroll-margin-top: 120px; }

  .stack-grid { grid-template-columns: 1fr; }
  .test-grid { grid-template-columns: 1fr; }
  .empty-state { min-height: 200px; padding: 24px 16px; }
  .about-block h2 { font-size: 18px; }
}

.nav-back { position: relative; }
.nav-back::after { content: ""; position: absolute; inset: -9px; }

/* =====================================================================
   ROUND 3 — timing tower / telemetry
   Palette: carbon #0a0b0d / #111216 / #17181d · red #e10600 (#ff4b43 on dark)
   · timing yellow #ffd400 · hairline rgba(255,255,255,.14)
   ===================================================================== */
:root {
  --bg: #0a0b0d;
  --panel: #111216;
  --panel-2: #17181d;
  --grid: rgba(255, 255, 255, 0.14);
  --live-text: #ff4b43;
  --timing: #ffd400;
  --sec-p: #b46bff;
  --sec-g: #2ecc71;
  --cut: 8px;
}

/* chamfer: angolo alto-destro + basso-sinistro tagliati */
.control-panel, .chart-block, .stat, .status-bar, .race-loader, .test-msg, .about-block.warn {
  clip-path: polygon(0 0, calc(100% - var(--cut)) 0, 100% var(--cut), 100% 100%, var(--cut) 100%, 0 calc(100% - var(--cut)));
}
.start-btn, .export-btn, .res-links > a, .res-links > span, .stack-item {
  --cut: 6px;
  clip-path: polygon(0 0, calc(100% - var(--cut)) 0, 100% var(--cut), 100% 100%, var(--cut) 100%, 0 calc(100% - var(--cut)));
  border-radius: 0;
}
.control-panel, .chart-block, .stat, .status-bar, .race-loader, .test-msg, .stint-seg, .lap-tbl tbody tr { border-radius: 0; }
/* focus ring non tagliato dal clip-path */
.start-btn:focus-visible, .export-btn:focus-visible, .res-links > a:focus-visible { outline-offset: -3px; }

/* ------------------------------------------------------------- header --- */
.navbar {
  background: var(--bg);
  border-bottom: 1px solid var(--live);
  box-shadow: 0 1px 0 var(--live);
}
.nav-brand {
  font-family: 'DM Serif Display', Georgia, 'Times New Roman', serif;
  font-weight: 400;
  font-size: 23px;
  line-height: 1;
  letter-spacing: .005em;
  color: var(--text);
  transition: color .18s ease;
  display: inline-flex; align-items: center; min-height: 44px;
}
.nav-brand .nv-red { color: var(--live-text); }
.nav-brand:hover, .nav-brand:focus-visible, .nav-brand:active { color: var(--live-text); }
.nav-back { border-radius: 0; }
.nav-tab { position: relative; border-bottom: none; transition: color .15s, background .15s; }
.nav-tab::before {
  content: ""; position: absolute; left: 0; right: 0; top: 0; height: 2px;
  background: transparent; transition: background .15s;
}
.nav-tab.on { background: var(--panel-2); border-bottom: none; }
.nav-tab.on::before { background: var(--live); }
.nav-tab:hover::before { background: var(--grid); }
.nav-tab.on:hover::before { background: var(--live); }
.live-badge::before {
  content: ""; display: inline-block; width: 6px; height: 6px; margin-right: 7px;
  background: currentColor; vertical-align: 1px;
}

/* -------------------------------------------------------- hero / titles --- */
.page-head { padding-bottom: 14px; border-bottom: 1px solid var(--grid); position: relative; }
/* tick rosso sotto la hero */
.page-head::after {
  content: ""; position: absolute; left: 0; bottom: -1px; width: 72px; height: 3px;
  background: var(--live);
}
.page-head .kicker { color: var(--live-text); }
.page-head .kicker::after { background: var(--live); width: 40px; }
.page-head h1 {
  font-family: var(--font-disp);
  text-transform: uppercase;
  font-weight: 700;
  letter-spacing: 0.01em;
  font-size: clamp(28px, 3.2vw, 42px);
  line-height: 0.98;
}
.res-title { font-family: var(--font-disp); font-size: 18px; font-weight: 600; letter-spacing: 0.06em; }
.res-title::before { border-radius: 0; background: var(--timing); width: 8px; height: 14px; vertical-align: -1px; }
.about-block h2 { font-family: var(--font-disp); text-transform: uppercase; font-size: 24px; letter-spacing: 0.04em; }

/* ---------------------------------------------------------- form card --- */
.control-panel { border-top: 2px solid var(--live); padding-top: 16px; }
.panel-title { color: var(--text); }
.setup-grid .field::before { color: var(--live-text); }
.field select, .field input, .field textarea { border-radius: 0; background: var(--bg); }
.field select:hover, .field input:hover, .field textarea:hover { border-color: rgba(255,255,255,.34); }
.start-btn { background: var(--live); color: #fff; font-family: var(--font-disp); font-size: 16px; letter-spacing: 0.14em; transition: filter .15s, transform .1s; }
.start-btn:hover:not(:disabled) { filter: brightness(1.14); }
.export-btn { background: transparent; }
.export-btn:hover { border-color: var(--live); }

/* ------------------------------------------------------------ results --- */
.chart-block, .stat { position: relative; }
.chart-block::before {
  content: ""; position: absolute; left: 0; top: 0; width: 28px; height: 2px; background: var(--live);
}
.chart-label { color: var(--text); font-size: 11px; display: flex; align-items: center; gap: 10px; }
.chart-label::after { content: ""; flex: 1; height: 1px; background: var(--grid); }
.chart-label > * { flex-shrink: 0; }
.stint-seg { border-top-width: 4px; }
.stint-seg .dot { border-radius: 0; }
.stint-seg:hover { filter: brightness(1.25); }
.stat-value, .stat .v { font-family: 'IBM Plex Mono', monospace; font-weight: 600; font-size: 22px; letter-spacing: -0.02em; }
.cmp, .ev-real, .ev-pred { border-radius: 0; }
.ev-real { background: rgba(255, 212, 0, 0.14); color: var(--timing); }
.lap-tbl th, .history-table th { border-bottom: 1px solid rgba(255,255,255,.28); }
.lap-tbl td { border-bottom: 1px solid rgba(255,255,255,.07); }
.lap-tbl tbody tr:nth-child(even) td { background: rgba(255,255,255,.018); }
.lap-tbl tbody tr:hover td { background: rgba(255,255,255,.05); }
.lap-tbl tbody tr:not(.is-real-pit):hover td:first-child { box-shadow: inset 2px 0 0 var(--live); }
.history-table td { border-bottom: 1px solid rgba(255,255,255,.07); }
.history-table tr:hover td { background: rgba(255,255,255,.04); }

/* loader: barra rossa */
.rl-bar { border-radius: 0; height: 3px; background: rgba(255,255,255,.1); }
.rl-bar > span { background: var(--live); border-radius: 0; }
.rl-kicker { color: var(--live-text); }
.empty-title { font-family: var(--font-disp); font-size: 22px; text-transform: uppercase; letter-spacing: 0.05em; }
.empty-icon { color: var(--live-text); }

/* ----------------------------------------------------------- info/test --- */
.steps li::before { color: var(--timing); }
.about-block h3 { color: var(--live-text); }
.about-block a { border-bottom-color: var(--live); }
.about-block a:hover { color: var(--live-text); }
.about-block.warn { border: 1px solid rgba(255, 212, 0, .3); background: rgba(255, 212, 0, .04); }
.about-block.warn h2 { color: var(--timing); }
.test-msg { border-left-color: var(--live); }
.status-bar { border-left: 2px solid var(--live); }
.radio-row input[type="radio"] { accent-color: var(--live); }
.error-box { border-radius: 0; }

/* ------------------------------------------------------------- mobile --- */
@media (max-width: 640px) {
  .nav-brand { font-size: 20px; }
  .page-head h1 { font-size: 32px; }
  .stint-seg, .lap-tbl tbody tr { clip-path: none; }
  .nav-tab.on { background: transparent; }
}
@media (prefers-reduced-motion: reduce) {
  .nav-brand, .nav-brand .nv-red, .nav-tab, .nav-tab::before, .start-btn { transition: none; }
}
