:root {
  --bg: #0b1020;
  --panel: #151b30;
  --panel-2: #1b2340;
  --line: #26304d;
  --text: #e6e9f2;
  --muted: #98a2bf;
  --accent: #3b82f6;
  --good: #22c55e;
  --warn: #f59e0b;
  --bad: #ef4444;
  --purple: #a855f7;
  --radius: 12px;
  --mono: ui-monospace, "SF Mono", "Cascadia Mono", Consolas, monospace;
}

* { box-sizing: border-box; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 14px/1.5 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
}

.hidden { display: none !important; }

/* ---------------------------------------------------------------- */
/* Botões e controles                                                */
/* ---------------------------------------------------------------- */

.btn {
  background: var(--panel-2);
  color: var(--text);
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 9px 16px;
  font: inherit;
  font-weight: 600;
  cursor: pointer;
  transition: background .15s, border-color .15s;
}
.btn:hover { background: #232c50; }
.btn:disabled { opacity: .5; cursor: not-allowed; }
.btn.primary { background: var(--accent); border-color: var(--accent); color: #fff; }
.btn.primary:hover { background: #2f6fe0; }
.btn.small { padding: 6px 12px; font-size: 13px; }
.btn.ghost { background: transparent; }
.btn.danger { color: var(--bad); border-color: #4a2330; }
.btn.danger:hover { background: #2a1620; }
.btn.active { border-color: var(--accent); background: #1e2a4d; }

input, select {
  background: #0f1526;
  color: var(--text);
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 9px 11px;
  font: inherit;
  width: 100%;
}
input:focus, select:focus { outline: 2px solid var(--accent); outline-offset: -1px; }
label { display: block; font-size: 12px; color: var(--muted); font-weight: 600; }
label input, label select { margin-top: 5px; font-weight: 400; color: var(--text); }
label small { font-weight: 400; }

.row { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }
.hint { color: var(--muted); font-size: 12px; }
.error { color: var(--bad); font-size: 13px; margin: 12px 0 0; }

/* ---------------------------------------------------------------- */
/* Tela de acesso                                                    */
/* ---------------------------------------------------------------- */

.auth {
  position: relative; overflow: hidden;
  display: flex; align-items: center; justify-content: center;
  min-height: 100vh; padding: 24px;
}
/* Fundo dividido: motocross à esquerda, kart à direita, com escurecimento
   para o card ficar legível por cima. */
.auth-bg { position: absolute; inset: 0; display: flex; z-index: 0; }
.auth-bg .half { flex: 1; background-size: cover; background-position: center; }
.auth-bg .moto { background-image: url('/img/moto-hero.webp?v27'); }
.auth-bg .kart { background-image: url('/img/kart-hero.webp?v27'); }
.auth-bg::after {
  content: ''; position: absolute; inset: 0;
  background: linear-gradient(180deg, rgba(11, 16, 32, .66), rgba(11, 16, 32, .88));
}
.auth-card {
  position: relative; z-index: 1;
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 16px;
  padding: 36px;
  width: 100%;
  max-width: 400px;
  box-shadow: 0 24px 70px rgba(0, 0, 0, .55);
}
.auth-card h1 { margin: 0 0 6px; font-size: 24px; font-weight: 700; }
.auth-card h1 span, .brand span { color: var(--accent); }
.auth-card .sub { margin: 0 0 24px; color: var(--muted); font-size: 13px; }

.tabs { display: flex; gap: 4px; background: #0f1526; padding: 4px; border-radius: 10px; margin-bottom: 20px; }
.tab {
  flex: 1; background: transparent; border: 0; color: var(--muted);
  padding: 8px; border-radius: 7px; font: inherit; font-weight: 600; cursor: pointer;
}
.tab.active { background: var(--panel-2); color: var(--text); }

.auth-form { display: grid; gap: 14px; }

/* ---------------------------------------------------------------- */
/* Layout principal                                                  */
/* ---------------------------------------------------------------- */

.topbar {
  display: flex; justify-content: space-between; align-items: center;
  gap: 10px; flex-wrap: wrap;
  padding: 12px 20px; border-bottom: 1px solid var(--line); background: var(--panel);
  position: sticky; top: 0; z-index: 500;
}
.brand { font-weight: 700; font-size: 16px; }

/* Seletor de modalidade (Motocross | Kart) no topo. */
.modality-switch {
  display: flex; gap: 4px; background: #0f1526; padding: 4px;
  border-radius: 999px; border: 1px solid var(--line);
}
.modality-switch button {
  background: transparent; border: 0; color: var(--muted);
  padding: 6px 14px; border-radius: 999px; font: inherit; font-weight: 600; font-size: 13px;
  cursor: pointer; white-space: nowrap; line-height: 1;
}
.modality-switch button.active {
  background: var(--panel-2); color: var(--text); box-shadow: 0 1px 2px rgba(0, 0, 0, .3);
}
.topbar-right { display: flex; align-items: center; gap: 10px; flex-wrap: wrap; }
.user { color: var(--muted); font-size: 13px; }
.pill {
  font-size: 12px; padding: 4px 10px; border-radius: 999px;
  background: var(--panel-2); border: 1px solid var(--line); color: var(--muted);
}
.pill.ok { color: var(--good); border-color: #1e4030; }
.pill.off { color: var(--muted); }

.layout { display: grid; grid-template-columns: 330px 1fr; gap: 16px; padding: 16px; align-items: start; }
@media (max-width: 980px) { .layout { grid-template-columns: 1fr; } }

/* Item de grid nasce com min-width:auto, ou seja, nunca encolhe abaixo do
   min-content do conteúdo. Sem isto a largura mínima da tabela de voltas
   (colunas com white-space:nowrap) estica a coluna inteira e a PÁGINA passa a
   rolar na horizontal — em vez de a tabela rolar dentro do próprio quadro. */
.sidebar, .content { min-width: 0; }

.sidebar { display: grid; gap: 14px; position: sticky; top: 72px; }
@media (max-width: 980px) { .sidebar { position: static; } }

.panel { background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); padding: 16px; }
.panel.inline { margin-top: 16px; }
.panel h2 { margin: 0 0 12px; font-size: 13px; text-transform: uppercase; letter-spacing: .06em; color: var(--muted); }
.panel h3 { margin: 0; font-size: 14px; }

/* Guia "Como trazer seu treino" (recolhível nativo). */
.import-help { margin-top: 12px; border-top: 1px solid var(--line); padding-top: 10px; }
.import-help > summary {
  cursor: pointer; list-style: none; font-size: 13px; font-weight: 600; color: var(--accent);
  display: flex; align-items: center; gap: 6px;
}
.import-help > summary::-webkit-details-marker { display: none; }
.import-help > summary::before { content: '▸'; font-size: 10px; color: var(--muted); transition: transform .15s; }
.import-help[open] > summary::before { transform: rotate(90deg); }
.import-help .help-body { margin-top: 10px; font-size: 12.5px; color: var(--muted); line-height: 1.5; }
.import-help .help-body p { margin: 0 0 8px; }
.import-help .help-body b { color: var(--text); font-weight: 600; }
.import-help .dica { color: var(--warn); }
.wa-link {
  display: inline-flex; align-items: center; gap: 6px; margin-top: 4px;
  background: #25d366; color: #05320f; font-weight: 700; font-size: 13px;
  padding: 8px 14px; border-radius: 8px; text-decoration: none;
}
.wa-link:hover { filter: brightness(1.05); }
.wa-inline { color: var(--good); text-decoration: none; }
.wa-inline:hover { text-decoration: underline; }

/* Painel recolhível (cabeçalho clicável). */
.panel-toggle { cursor: pointer; user-select: none; display: flex; align-items: center; gap: 7px; }
.panel-toggle:hover { color: var(--text); }
.panel-toggle .chevron { font-size: 10px; color: var(--muted); transition: transform .15s ease; display: inline-block; }
.panel.collapsible:not(.collapsed) .panel-toggle .chevron { transform: rotate(90deg); }
.panel.collapsible.collapsed > h2 { margin-bottom: 0; }
.panel.collapsible.collapsed > .list { display: none; }
.panel-head { display: flex; justify-content: space-between; align-items: center; margin-bottom: 12px; gap: 12px; flex-wrap: wrap; }
.count { color: var(--muted); font-weight: 400; }

.drop {
  display: grid; place-items: center; gap: 4px; text-align: center;
  border: 2px dashed var(--line); border-radius: 10px; padding: 22px 14px;
  cursor: pointer; transition: border-color .15s, background .15s; margin-bottom: 10px;
}
.drop:hover, .drop.over { border-color: var(--accent); background: #10193166; }
.drop small { color: var(--muted); font-size: 12px; }

.log { margin-top: 10px; font-size: 12px; display: grid; gap: 4px; }
.log .ok { color: var(--good); }
.log .fail { color: var(--bad); }

.list { list-style: none; margin: 0; padding: 0; display: grid; gap: 6px; max-height: 46vh; overflow-y: auto; }
.list li {
  padding: 9px 11px; border-radius: 8px; border: 1px solid transparent;
  background: #0f1526; cursor: pointer; font-size: 13px;
}
.list li:hover { border-color: var(--line); }
.list li.active { border-color: var(--accent); background: #14203c; }
.list li .t { font-weight: 600; display: block; }
.list li .s { color: var(--muted); font-size: 11.5px; }
.list.compact li { display: flex; justify-content: space-between; align-items: center; gap: 8px; }
.list .del { color: var(--muted); background: none; border: 0; cursor: pointer; font-size: 15px; padding: 0 4px; }
.list .del:hover { color: var(--bad); }
.list .none { color: var(--muted); font-size: 12.5px; padding: 6px 2px; cursor: default; background: none; }

.empty { text-align: center; padding: 60px 24px; color: var(--muted); }
.empty h2 { color: var(--text); font-size: 18px; margin: 0 0 8px; }
/* Hero da modalidade ativa (troca com o seletor Motocross|Kart). */
.mode-hero {
  width: 100%; max-width: 620px; aspect-ratio: 16 / 7;
  margin: 0 auto 22px; border-radius: 14px;
  background-size: cover; background-position: center;
  border: 1px solid var(--line); box-shadow: 0 14px 34px rgba(0, 0, 0, .42);
}

.session-header { display: flex; justify-content: space-between; align-items: flex-start; gap: 16px; flex-wrap: wrap; margin-bottom: 12px; }
.session-title { display: flex; align-items: flex-start; gap: 12px; }
.btn-back { flex: 0 0 auto; }
.session-header h2 { margin: 0 0 4px; font-size: 19px; }
.session-header .meta { margin: 0; color: var(--muted); font-size: 12.5px; }
.session-actions { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }
.session-actions select { width: auto; min-width: 180px; }

/* ---------------------------------------------------------------- */
/* Mapa                                                              */
/* ---------------------------------------------------------------- */

.map-wrap { border: 1px solid var(--line); border-radius: var(--radius); overflow: hidden; background: var(--panel); }
#map { height: 460px; background: #0f1526; }
/* No celular 460px come quase a tela toda; 55vh deixa a tabela de voltas
   aparecer junto com o mapa. */
@media (max-width: 640px) { #map { height: 55vh; min-height: 300px; } }
#map.picking { cursor: crosshair; }
.map-tools { display: flex; gap: 8px; padding: 10px 12px; border-top: 1px solid var(--line); align-items: center; flex-wrap: wrap; }

.leaflet-container { background: #0f1526; }
/* Os tiles do OSM são claros demais para o tema escuro; escurece sem perder leitura. */
.leaflet-tile-pane { filter: brightness(.62) saturate(.7) hue-rotate(185deg) invert(0); }
.leaflet-popup-content-wrapper, .leaflet-popup-tip { background: var(--panel-2); color: var(--text); }
.leaflet-bar a { background: var(--panel-2); color: var(--text); border-color: var(--line); }
.leaflet-bar a:hover { background: #232c50; }

.map-legend {
  background: rgba(21,27,48,.92); border: 1px solid var(--line); border-radius: 8px;
  padding: 8px 10px; font-size: 11.5px; color: var(--muted); line-height: 1.7;
}
.map-legend .bar { height: 7px; border-radius: 4px; margin: 4px 0 3px;
  background: linear-gradient(90deg, #2563eb, #22c55e, #eab308, #ef4444); }
.map-legend .ends { display: flex; justify-content: space-between; }

/* ---------------------------------------------------------------- */
/* Resumo e tabela de voltas                                         */
/* ---------------------------------------------------------------- */

.cards { display: grid; grid-template-columns: repeat(auto-fit, minmax(148px, 1fr)); gap: 10px; margin-top: 16px; }
.card { background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); padding: 13px 15px; }
.card .k { font-size: 11px; text-transform: uppercase; letter-spacing: .05em; color: var(--muted); }
.card .v { font-size: 20px; font-weight: 700; margin-top: 3px; font-family: var(--mono); }
.card .n { font-size: 11.5px; color: var(--muted); margin-top: 2px; }
.card.best .v { color: var(--purple); }
.card.theo .v { color: var(--good); }

.grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(180px, 1fr)); gap: 12px; margin: 12px 0; }

.toggle { display: flex; gap: 3px; background: #0f1526; padding: 3px; border-radius: 8px; }
.tgl { background: transparent; border: 0; color: var(--muted); padding: 5px 12px; border-radius: 6px;
       font: inherit; font-size: 12.5px; font-weight: 600; cursor: pointer; }
.tgl.active { background: var(--panel-2); color: var(--text); }

.table-scroll { overflow-x: auto; max-height: 420px; overflow-y: auto; }
table { width: 100%; border-collapse: collapse; font-size: 13px; }
th, td { padding: 8px 10px; text-align: right; border-bottom: 1px solid var(--line); white-space: nowrap; }
th { position: sticky; top: 0; background: var(--panel); color: var(--muted); font-size: 11px;
     text-transform: uppercase; letter-spacing: .05em; z-index: 1; }
th:first-child, td:first-child { text-align: left; }
tbody tr { cursor: pointer; }
tbody tr:hover { background: #0f1526; }
tbody tr.sel { background: #14203c; }
td.time { font-family: var(--mono); font-weight: 600; }
tr.best td.time { color: var(--purple); }
td.pos { color: var(--bad); }
td.neg { color: var(--good); }
td.bestsec { color: var(--purple); font-weight: 600; }
td.dim { color: var(--muted); }

.notice { margin-top: 16px; padding: 14px 16px; border-radius: var(--radius);
          background: #1e1a10; border: 1px solid #4a3a18; color: #e8c98a; font-size: 13px; }

.toast {
  position: fixed; bottom: 22px; left: 50%; transform: translateX(-50%);
  background: var(--panel-2); border: 1px solid var(--line); border-radius: 10px;
  padding: 11px 20px; font-size: 13px; z-index: 9999; max-width: 90vw;
}
.toast.bad { border-color: #4a2330; color: #fca5a5; }
.toast.good { border-color: #1e4030; color: #86efac; }

/* ---------------------------------------------------------------- */
/* Marca                                                             */
/* ---------------------------------------------------------------- */

.auth-card .tagline {
  margin: -2px 0 14px;
  font-size: 12px;
  font-weight: 700;
  letter-spacing: .18em;
  text-transform: uppercase;
  color: var(--warn);
}
.auth-card .byline {
  margin: 22px 0 0;
  text-align: center;
  font-size: 11.5px;
  letter-spacing: .06em;
  color: var(--muted);
}
.brand em {
  font-style: normal;
  font-size: 11px;
  font-weight: 700;
  letter-spacing: .14em;
  text-transform: uppercase;
  color: var(--warn);
  vertical-align: 2px;
  margin-left: 4px;
}

/* ---------------------------------------------------------------- */
/* Participantes                                                     */
/* ---------------------------------------------------------------- */

.stack { display: block; margin: 10px 0 0; }
.stack select { margin-top: 5px; }
.stack-form { display: grid; gap: 7px; margin-top: 10px; }

.list .who { display: flex; flex-direction: column; min-width: 0; }
.list .who b { font-weight: 600; }
.list .who span { color: var(--muted); font-size: 11.5px; }
.list .tagme {
  font-size: 10px; font-weight: 700; letter-spacing: .08em; text-transform: uppercase;
  color: var(--accent); border: 1px solid #234070; border-radius: 4px; padding: 1px 5px; margin-left: 6px;
}
.list .acts { display: flex; gap: 2px; flex: 0 0 auto; }
.list .acts button { color: var(--muted); background: none; border: 0; cursor: pointer; font-size: 13px; padding: 2px 4px; }
.list .acts button:hover { color: var(--text); }
.list .acts button.del:hover { color: var(--bad); }

/* ---------------------------------------------------------------- */
/* Ranking                                                           */
/* ---------------------------------------------------------------- */

.filtros { gap: 10px; }
.inline-label { display: flex; align-items: center; gap: 6px; font-size: 12px; color: var(--muted); }
.inline-label input { width: auto; padding: 5px 8px; font-size: 12.5px; }

#ranking-table td.pos { font-weight: 700; font-size: 15px; width: 46px; }
#ranking-table tr.p1 td.pos { color: #fbbf24; }
#ranking-table tr.p2 td.pos { color: #cbd5e1; }
#ranking-table tr.p3 td.pos { color: #d97757; }
#ranking-table tr.p1 td.time { color: var(--purple); }
#ranking-table td.rider b { font-weight: 600; }
#ranking-table td.rider span { color: var(--muted); font-size: 11.5px; display: block; }
#ranking-sessions td.dim { color: var(--muted); }

/* Barra de filtros do ranking: os controles ficam juntos e cabem em coluna
   quando a tela é estreita. */
.filtro-bar { padding: 12px 16px; }
.filtro-bar .filtros { gap: 14px; }
.filtro-bar .inline-label { flex-direction: column; align-items: flex-start; gap: 4px; }
.filtro-bar .inline-label select { width: auto; min-width: 150px; padding: 6px 9px; font-size: 12.5px; }
@media (max-width: 640px) {
  .filtro-bar .filtros { flex-direction: column; align-items: stretch; }
  .filtro-bar .inline-label { width: 100%; }
  .filtro-bar .inline-label select, .filtro-bar .inline-label input { width: 100%; }
}

#ranking-cards { margin-top: 16px; }
#ranking-sessions tr.best td.time { color: var(--purple); font-weight: 700; }
#ranking-sessions td .tagme { color: var(--purple); border-color: #4a2a6b; }
#ranking-table tr.sel { background: #14203c; }
.session-actions #ranking-circuit-select { min-width: 200px; }

/* Modo de mesclagem de circuitos */
.merge-banner {
  background: #1e2a4d; border: 1px solid var(--accent); border-radius: 8px;
  padding: 9px 11px; margin-bottom: 10px; font-size: 12.5px; line-height: 1.6;
}
.merge-banner .btn { margin-left: 6px; }
.list li.merge-src { border-color: var(--warn); background: #241d10; }
.list li.merge-target { border-color: #2a3a63; }
.list li.merge-target:hover { border-color: var(--accent); background: #14203c; }

/* Área de administração */
.notice.info { background: #101c31; border-color: #24406b; color: #a9c4ea; }
.acts-cell { white-space: nowrap; }
.acts-cell .btn { margin-left: 4px; padding: 4px 9px; font-size: 12px; }
#admin-table td.rider .s { color: var(--muted); font-size: 11.5px; }
#admin-table tr.sel { background: #14203c; }

/* Circuito compartilhado (de outra conta) */
.tag-shared {
  font-size: 10px; font-weight: 700; letter-spacing: .05em; text-transform: uppercase;
  color: var(--good); border: 1px solid #1e4030; border-radius: 4px; padding: 1px 5px; margin-left: 6px;
}

/* Qualidade da gravação */
.card.warn .v { color: var(--warn); text-transform: capitalize; }
.warn-mark { color: var(--warn); margin-left: 5px; cursor: help; font-size: 12px; }
#laps-table tr.unreliable td.time { color: var(--warn); }
#ranking-sessions tr.lowq td:first-child::after,
#ranking-table tr.lowq td.time::after { content: ' ⚠'; color: var(--warn); font-size: 11px; }
/* Aviso de gravação limitada usa a cor de alerta */
#quality-notice.notice { background: #1e1a10; border-color: #4a3a18; color: #e8c98a; }

/* Links textuais nos formulários de acesso */
.linkish { background: none; border: 0; color: var(--accent); font: inherit; font-size: 13px;
  cursor: pointer; padding: 4px; text-align: center; }
.linkish:hover { text-decoration: underline; }
.okmsg { color: var(--good); font-size: 13px; margin: 12px 0 0; line-height: 1.5; }

/* Conexões entre pilotos */
.badge { display:inline-block; min-width:16px; padding:0 5px; margin-left:4px; border-radius:9px;
  background:var(--bad); color:#fff; font-size:11px; font-weight:700; text-align:center; }
.conn-list { display:grid; gap:8px; margin-top:10px; }
.conn-row { display:flex; justify-content:space-between; align-items:center; gap:12px; flex-wrap:wrap;
  background:#0f1526; border:1px solid var(--line); border-radius:8px; padding:10px 12px; font-size:13px; }
.conn-acts { display:flex; gap:6px; }
#connections-view .panel h3 { margin-bottom:2px; }
.tag-conn { font-size:10px; font-weight:700; letter-spacing:.05em; text-transform:uppercase;
  color:#a9c4ea; border:1px solid #24406b; border-radius:4px; padding:1px 5px; margin-left:6px; }
#conn-search { margin: 8px 0 4px; }

/* ---------------------------------------------------------------- */
/* PWA no iPhone: respeitar as áreas seguras (notch e barra inferior) */
/* ---------------------------------------------------------------- */
.topbar { padding-top: calc(12px + env(safe-area-inset-top)); }
.auth { padding-top: calc(24px + env(safe-area-inset-top)); }
.layout { padding-left: calc(16px + env(safe-area-inset-left));
          padding-right: calc(16px + env(safe-area-inset-right)); }
.toast { bottom: calc(22px + env(safe-area-inset-bottom)); }
/* fundo do tema atrás da status bar translúcida */
html { background: var(--bg); }

/* Esconde visualmente sem display:none — necessário para o input de arquivo
   disparar o "change" no Safari do iPhone. */
.sr-only {
  position: absolute !important; width: 1px; height: 1px;
  padding: 0; margin: -1px; overflow: hidden; clip: rect(0 0 0 0);
  white-space: nowrap; border: 0; opacity: 0;
}
