:root {
  --bg: #f1f5f9; --card: #fff; --ink: #0f172a; --muted: #64748b; --line: #e2e8f0;
  --brand: #2563eb; --ok: #16a34a; --warn: #d97706; --bad: #dc2626;
  font-family: -apple-system, BlinkMacSystemFont, "SF Pro Text", "Segoe UI", Roboto, sans-serif;
  color: var(--ink); background: var(--bg);
}
* { box-sizing: border-box; }
body { margin: 0; }
a { color: var(--brand); }
.muted { color: var(--muted); }
h1, h2 { margin: 0; }

/* ---------- race page ---------- */
body.race { display: flex; flex-direction: column; height: 100dvh; overflow: hidden; background: #f7f7fa; }
.top {
  display: flex; align-items: center; gap: 10px; padding: 10px 14px;
  padding-top: max(10px, env(safe-area-inset-top));
  background: #fff; color: #242428; z-index: 500; border-bottom: 1px solid #ececf0;
}
.top .back { color: #242428; text-decoration: none; font-size: 30px; line-height: 1; padding-right: 2px; }
.top .title { flex: 1; min-width: 0; }
.top h1 { font-size: 17px; font-weight: 800; letter-spacing: -.01em; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.top .muted { color: #6d6d78; font-size: 12px; text-transform: capitalize; }
.clock { font-variant-numeric: tabular-nums; font-weight: 800; font-size: 18px; background: #FC4C02; color: #fff; padding: 6px 11px; border-radius: 10px; }
.clock.pre { font-size: 13px; background: #fff1ea; color: #c33a00; }
#mapbox { position: relative; flex: 1 1 55%; min-height: 240px; }
#map { position: absolute; inset: 0; }
.sheet {
  flex: 1 1 45%; overflow-y: auto; padding: 12px 12px calc(12px + env(safe-area-inset-bottom));
  background: #f7f7fa; border-top: 1px solid #ececf0;
}
@media (min-width: 900px) {
  body.race { display: grid; grid-template: auto 1fr / 1fr 420px; }
  .top { grid-column: 1 / -1; }
  #mapbox { grid-row: 2; }
  .sheet { grid-row: 2; border-top: 0; border-left: 1px solid #ececf0; }
}

/* controles del mapa */
.mapctl { position: absolute; z-index: 2; display: flex; background: #fff; border-radius: 10px;
  box-shadow: 0 2px 10px rgba(0,0,0,.18); overflow: hidden; }
.mapctl button { background: #fff; color: #242428; border: 0; border-radius: 0; padding: 8px 12px;
  font-size: 13px; font-weight: 700; cursor: pointer; }
.mapctl button + button { border-left: 1px solid #ececf0; }
.mapctl button.on { background: #FC4C02; color: #fff; }
.mapctl.styles { top: 10px; left: 10px; }
.mapctl.tools { bottom: 28px; right: 10px; flex-direction: column; }
.mapctl.tools button { width: 40px; height: 40px; padding: 0; font-size: 15px; }
.mapctl.tools button + button { border-left: 0; border-top: 1px solid #ececf0; }
.maplibregl-ctrl-group { border-radius: 10px !important; box-shadow: 0 2px 10px rgba(0,0,0,.18) !important; }
.maplibregl-ctrl-group button { width: 40px; height: 40px; }

.banner { background: #fff1ea; color: #9a3412; padding: 10px 12px; border-radius: 12px; margin-bottom: 10px; }
.runners { display: grid; gap: 10px; }
.card {
  background: #fff; border-radius: 16px; padding: 14px 16px; cursor: pointer;
  box-shadow: 0 1px 3px rgba(0,0,0,.07); transition: box-shadow .2s, transform .2s;
}
.card.following { box-shadow: 0 0 0 2px var(--c), 0 4px 14px rgba(0,0,0,.08); }
.card-head { display: flex; align-items: center; gap: 10px; margin-bottom: 10px; }
.card-head h2 { font-size: 16px; font-weight: 800; flex: 1; }
.card-head small { color: var(--muted); font-weight: 500; }
.avatar { width: 34px; height: 34px; border-radius: 50%; background: var(--c); color: #fff; display: grid; place-items: center;
  font-weight: 800; font-size: 12px; flex: none; }
.dot { width: 12px; height: 12px; border-radius: 50%; background: var(--c); }
.badge { font-size: 11px; font-weight: 700; text-transform: uppercase; padding: 3px 8px; border-radius: 99px; background: #ececf0; color: var(--muted); }
.badge.live { background: #fff1ea; color: #FC4C02; }
.badge.live::before { content: "● "; animation: blink 1.4s infinite; }
.badge.ok { background: #dcfce7; color: var(--ok); }
.badge.warn { background: #fef3c7; color: var(--warn); }
@keyframes blink { 50% { opacity: .2; } }
.big { font-size: 30px; font-weight: 800; font-variant-numeric: tabular-nums; }
.big small { font-size: 15px; color: var(--muted); font-weight: 600; }
.stats { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; }
.stats div { display: flex; flex-direction: column; gap: 1px; }
.stats div + div { border-left: 1px solid #ececf0; padding-left: 10px; }
.stats span { font-size: 11px; color: var(--muted); }
.stats b { font-size: 22px; font-weight: 800; font-variant-numeric: tabular-nums; letter-spacing: -.02em; }
.stats b small { font-size: 12px; color: var(--muted); font-weight: 600; }
.substats { font-size: 13px; margin-top: 8px; }
.substats b { color: var(--ink); }
.bar { height: 6px; background: #ececf0; border-radius: 99px; overflow: hidden; margin: 10px 0 8px; }
.bar i { display: block; height: 100%; background: var(--c); border-radius: 99px; transition: width .6s; }
.meta { display: flex; flex-wrap: wrap; gap: 12px; font-size: 12px; }
.warn-t { color: var(--warn); font-weight: 600; }
.profile-wrap { background: #fff; border-radius: 16px; padding: 12px 14px; margin-top: 10px; box-shadow: 0 1px 3px rgba(0,0,0,.07); }
.profile-head { display: flex; justify-content: space-between; font-size: 13px; font-weight: 700; margin-bottom: 6px; }
#profile { width: 100%; height: 110px; display: block; }
#profile .ridge { fill: none; stroke: #FC4C02; stroke-width: 2; vector-effect: non-scaling-stroke; stroke-linejoin: round; }
#profile .grid { stroke: #ececf0; stroke-width: 1; vector-effect: non-scaling-stroke; }
#profile .gl { font-size: 11px; fill: #9a9aa5; }
.foot { font-size: 12px; text-align: center; padding: 12px 0 4px; }
.foot a { color: #FC4C02; }

/* marcadores del mapa */
.pinwrap { cursor: pointer; }  /* sin position: MapLibre lo coloca con position:absolute */
.pin {
  width: 34px; height: 34px; border-radius: 50%; background: var(--c); color: #fff; border: 3px solid #fff;
  display: grid; place-items: center; font-weight: 800; font-size: 12px; box-shadow: 0 2px 8px rgba(0,0,0,.35);
  position: relative;
}
.pin::after {
  content: ""; position: absolute; inset: -3px; border-radius: 50%; border: 3px solid var(--c);
  animation: pulse 2s infinite; opacity: .6;
}
.pin.stale { filter: grayscale(.7); opacity: .75; }
.pin.stale::after { display: none; }
@keyframes pulse { to { transform: scale(2.1); opacity: 0; } }
.pinlabel { position: absolute; left: 50%; top: -26px; transform: translateX(-50%); white-space: nowrap;
  background: #242428; color: #fff; font-size: 11px; font-weight: 700; padding: 3px 7px; border-radius: 6px;
  box-shadow: 0 1px 4px rgba(0,0,0,.3); pointer-events: none; }
.kmk {
  width: 22px; height: 22px; background: #fff; border-radius: 50%; color: #242428;
  font-size: 10px; font-weight: 800; display: grid; place-items: center;
  box-shadow: 0 1px 4px rgba(0,0,0,.35); pointer-events: none;
}
.lowzoom .kmk.minor { display: none; }
.flag { display: flex; align-items: center; gap: 5px; background: #fff; color: #242428; font-weight: 800; font-size: 11px;
  text-transform: uppercase; letter-spacing: .03em; padding: 4px 8px 4px 5px; border-radius: 99px;
  box-shadow: 0 2px 6px rgba(0,0,0,.3); white-space: nowrap; }
.flag i { width: 14px; height: 14px; border-radius: 50%; display: block; }
.flag.start i { background: #16a34a; box-shadow: inset 0 0 0 3px #fff, 0 0 0 1px #16a34a; }
.flag.finish i { background: conic-gradient(#242428 0 25%, #fff 0 50%, #242428 0 75%, #fff 0) 0 0 / 7px 7px; box-shadow: 0 0 0 1px #242428; }

/* ---------- generic pages ---------- */
.page { max-width: 900px; margin: 0 auto; padding: 24px 16px 60px; }
.page header h1 { font-size: 26px; font-weight: 800; letter-spacing: -.01em; }
.page header p { margin: 4px 0 20px; }
.list { display: grid; gap: 10px; }
.item {
  display: flex; justify-content: space-between; align-items: center; gap: 12px; text-decoration: none; color: inherit;
  background: var(--card); padding: 14px 16px; border-radius: 14px; box-shadow: 0 1px 2px rgba(0,0,0,.06);
}
.item h3 { margin: 0 0 3px; font-size: 16px; }
.pill { font-size: 11px; font-weight: 700; padding: 3px 9px; border-radius: 99px; background: var(--line); white-space: nowrap; }
.pill.live { background: #fff1ea; color: #FC4C02; }
.panel { background: var(--card); border-radius: 16px; padding: 18px 20px; margin-bottom: 16px; box-shadow: 0 1px 3px rgba(0,0,0,.07); }
.panel h2 { font-size: 18px; font-weight: 800; margin-bottom: 12px; }
.panel h3 { font-size: 14px; font-weight: 700; margin: 18px 0 8px; }
.panel > .row:first-child h2 { margin-bottom: 0; }

/* formularios: todos los controles con la misma altura para que alineen */
form.grid { display: grid; gap: 12px; grid-template-columns: repeat(auto-fit, minmax(190px, 1fr)); align-items: end; }
label { display: flex; flex-direction: column; gap: 5px; font-size: 12px; font-weight: 600; color: var(--muted); }
input, select, textarea, button { font: inherit; font-size: 14px; color: var(--ink); }
input, select, textarea {
  width: 100%; height: 42px; padding: 0 12px; border-radius: 10px; border: 1px solid #d5dbe3; background: #fff;
}
input:focus, select:focus, textarea:focus { outline: 2px solid #FC4C02; outline-offset: -1px; border-color: transparent; }
input[type=file] { padding: 0 6px; line-height: 40px; font-size: 13px; color: var(--muted); }
input[type=file]::file-selector-button {
  height: 30px; margin-right: 10px; padding: 0 12px; border: 0; border-radius: 8px; background: #eef1f5; color: var(--ink);
  font: inherit; font-weight: 600; cursor: pointer;
}
input[type=color] { padding: 4px; cursor: pointer; }
input[type=color]::-webkit-color-swatch-wrapper { padding: 0; }
input[type=color]::-webkit-color-swatch { border: 0; border-radius: 7px; }
input[type=checkbox] { width: 18px; height: 18px; padding: 0; accent-color: #FC4C02; }
button {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  height: 42px; padding: 0 18px; border-radius: 10px; border: 1px solid transparent;
  background: #FC4C02; color: #fff; font-weight: 700; cursor: pointer; white-space: nowrap; transition: filter .15s;
}
button:hover { filter: brightness(.95); }
button.ghost { background: #fff; color: var(--ink); border-color: #d5dbe3; }
button.danger { background: #fff; color: var(--bad); border-color: #fecaca; }
button.danger:hover { background: #fef2f2; }
button.sm { height: 32px; padding: 0 12px; font-size: 13px; border-radius: 8px; }
.row a { display: inline-flex; text-decoration: none; }
table { width: 100%; border-collapse: collapse; font-size: 14px; }
td, th { text-align: left; padding: 10px 8px; border-bottom: 1px solid var(--line); vertical-align: middle; }
th { font-size: 11px; text-transform: uppercase; color: var(--muted); }
.table-wrap { overflow-x: auto; }
code { background: #f1f5f9; padding: 3px 7px; border-radius: 6px; font-size: 13px; word-break: break-all; }
.row { display: flex; gap: 8px; flex-wrap: wrap; align-items: center; }
.help { background: #f8fafc; border: 1px solid var(--line); border-radius: 12px; padding: 12px 16px; font-size: 14px; line-height: 1.7; }
.help ol { margin: 0; padding-left: 20px; }
.help .sm { vertical-align: middle; }
.swatch { display: inline-block; width: 12px; height: 12px; border-radius: 50%; vertical-align: middle; margin-right: 6px; }
.toast { position: fixed; bottom: 20px; left: 50%; transform: translateX(-50%); background: #242428; color: #fff; padding: 10px 16px; border-radius: 10px; z-index: 99; }

/* sin señal */
.lost { background: #fff7ed; border: 1px solid #fed7aa; color: #9a3412; border-radius: 12px; padding: 10px 12px;
  margin-bottom: 10px; font-size: 13px; line-height: 1.4; display: flex; flex-direction: column; gap: 3px; }
.ghost-pin { width: 30px; height: 30px; border-radius: 50%; border: 3px dashed var(--c); background: rgba(255,255,255,.6);
  pointer-events: none; animation: ghostfade 1.6s ease-in-out infinite alternate; }
.ghost-pin .pinlabel { background: #fff; color: #242428; border: 1px dashed #9a9aa5; box-shadow: none; }
@keyframes ghostfade { from { opacity: .45; } to { opacity: 1; } }

/* parciales */
.card details { margin-top: 10px; border-top: 1px solid #ececf0; padding-top: 8px; }
.card summary { font-size: 13px; font-weight: 700; cursor: pointer; color: #FC4C02; list-style: none; }
.card summary::-webkit-details-marker { display: none; }
.card summary::after { content: " ▾"; }
.card details[open] summary::after { content: " ▴"; }
.splits { display: grid; gap: 4px; margin-top: 8px; font-variant-numeric: tabular-nums; }
.split { display: grid; grid-template-columns: 44px 48px 1fr 52px; align-items: center; gap: 8px; font-size: 13px; }
.split.head { font-size: 11px; color: var(--muted); text-transform: uppercase; font-weight: 700; }
.split .k { font-weight: 700; }
.split .p { font-weight: 700; }
.split .e { text-align: right; color: var(--muted); font-size: 12px; }
.split .b { height: 14px; }
.split .b i { display: block; height: 100%; background: #ffc4a8; border-radius: 4px; }
.split.best .b i { background: #FC4C02; }
.split.best .p { color: #FC4C02; }

/* pantalla de llegada */
.btn-summary { margin-top: 10px; width: 100%; background: #FC4C02; border-radius: 12px; padding: 11px; }
#summary { border: 0; border-radius: 20px; padding: 14px; max-width: min(440px, calc(100vw - 32px)); width: 100%;
  box-shadow: 0 20px 60px rgba(0,0,0,.35); }
#summary::backdrop { background: rgba(20,20,25,.6); backdrop-filter: blur(3px); }
#summary img { width: 100%; display: block; border-radius: 12px; box-shadow: 0 0 0 1px #ececf0; }
.row-btns { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; margin-top: 12px; }
.row-btns button { border-radius: 12px; padding: 12px; }
#summaryShare { background: #FC4C02; }

/* fotos de corredores */
.pin.photo { background: #fff; overflow: visible; width: 44px; height: 44px; border: 3px solid var(--c); }
.pin.photo img { width: 100%; height: 100%; border-radius: 50%; object-fit: cover; display: block; border: 2px solid #fff; }
.pin.photo::after { inset: -6px; }
.pin.photo + .pinlabel { top: -28px; }
img.avatar { object-fit: cover; box-shadow: 0 0 0 2px var(--c); }
.av { width: var(--s); height: var(--s); border-radius: 50%; flex: none; object-fit: cover; display: inline-grid; place-items: center;
  background: var(--c); color: #fff; font-weight: 800; font-size: calc(var(--s) * .36); box-shadow: 0 0 0 2px #fff, 0 0 0 4px var(--c); }
.profiles { display: grid; gap: 10px; margin-bottom: 4px; }
.profile { display: flex; align-items: center; gap: 14px; padding: 12px 14px; border: 1px solid var(--line); border-radius: 14px; flex-wrap: wrap; }
.profile .pinfo { flex: 1; min-width: 220px; display: flex; flex-direction: column; gap: 4px; }
.profile .pinfo b { font-size: 15px; }
.profile .pinfo .small { display: flex; align-items: center; gap: 6px; flex-wrap: wrap; }
.small { font-size: 12px; }
form.profile-form { grid-template-columns: 56px minmax(140px, 1.2fr) 72px minmax(160px, 1.4fr) auto; }
@media (max-width: 720px) { form.profile-form { grid-template-columns: 56px 1fr 72px; } form.profile-form > label:nth-of-type(4),
  form.profile-form > button { grid-column: 1 / -1; } }
.photo-pick { cursor: pointer; }
.photo-pick > span:not(.photo-prev) { display: none; }
.photo-prev { width: 56px; height: 56px; border-radius: 50%; background: #f1f5f9; border: 2px dashed #cbd5e1; display: grid;
  place-items: center; font-size: 20px; overflow: hidden; margin-bottom: -7px; transition: border-color .15s; }
.photo-pick:hover .photo-prev { border-color: #FC4C02; }
.photo-prev img { width: 100%; height: 100%; object-fit: cover; }
.help-wrap { margin-top: 16px; }
.help-wrap summary { cursor: pointer; font-weight: 700; font-size: 14px; margin-bottom: 8px; }
.edit-dlg { border: 0; border-radius: 18px; padding: 22px; width: min(640px, calc(100vw - 32px)); box-shadow: 0 20px 60px rgba(0,0,0,.3); }
.edit-dlg::backdrop { background: rgba(15,23,42,.5); }
.edit-dlg form.profile-form { grid-template-columns: 56px 1fr 72px; }
.edit-dlg form.profile-form > label:nth-of-type(4) { grid-column: 1 / -1; }
label.inline { flex-direction: row; align-items: center; gap: 8px; grid-column: 1 / -1; font-size: 13px; color: var(--ink); }
.mapctl.styles button { height: 36px; }

/* repetición */
.replaybar { position: absolute; z-index: 3; left: 10px; right: 60px; bottom: 28px; display: flex; align-items: center; gap: 8px;
  background: #fff; border-radius: 12px; padding: 6px 8px; box-shadow: 0 2px 10px rgba(0,0,0,.2); }
.replaybar button { height: 34px; min-width: 34px; padding: 0 8px; border-radius: 8px; font-size: 13px; }
.replaybar #rpClose { background: #fff; color: #242428; border-color: #d5dbe3; }
.replaybar .rpspeed { background: #fff1ea; color: #c33a00; }
.replaybar input[type=range] { flex: 1; min-width: 60px; height: 34px; padding: 0; border: 0; accent-color: #FC4C02; background: none; }
.rptime { font-variant-numeric: tabular-nums; font-weight: 800; font-size: 13px; min-width: 52px; text-align: right; }
.replaying .mapctl.styles { display: none; }
.replay-cta { width: 100%; margin-bottom: 10px; border-radius: 12px; }

/* Simulador (solo admin) */
[hidden] { display: none !important; }  /* que ningún display: flex/grid enseñe algo oculto */
.simbar { background: #f5f3ff; border: 1px solid #ddd6fe; border-radius: 14px; padding: 10px 12px; margin-bottom: 12px; display: grid; gap: 8px; }
.simhead { display: flex; flex-wrap: wrap; gap: 4px 10px; align-items: baseline; }
.simhead b { color: #5b21b6; }
.simctl { display: flex; align-items: center; gap: 8px; }
.simctl button { height: 34px; min-width: 34px; padding: 0 8px; border-radius: 8px; font-size: 13px; }
.simctl input[type=range] { flex: 1; min-width: 60px; height: 34px; padding: 0; border: 0; accent-color: #7c3aed; background: none; }
.simpaces { display: flex; flex-wrap: wrap; gap: 8px 14px; }
.simpaces label { display: flex; align-items: center; gap: 6px; font-size: 14px; font-weight: 600; }
.simpaces input { width: 64px; padding: 6px 8px; text-align: center; }
.simopts { display: flex; flex-wrap: wrap; gap: 8px 14px; align-items: center; justify-content: space-between; }
body.simulating .clock { background: #7c3aed; }
body.simulating .clock.pre { background: #f5f3ff; color: #5b21b6; }
