:root {
  --bg: #f2f2f7; --card: #ffffff; --fg: #1c1c1e; --muted: #6e6e73; --grid: #e5e5ea; --line: #d1d1d6;
  --accent: #FF7A1A; --blue: #007AFF; --green: #2fa84f; --orange: #e67700; --red: #e5342b;
  --tab: rgba(249, 249, 249, .94);
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #000; --card: #1c1c1e; --fg: #f2f2f7; --muted: #98989f; --grid: #2c2c2e; --line: #38383a;
    --green: #32d74b; --orange: #ff9f0a; --red: #ff453a; --tab: rgba(22, 22, 24, .94);
    color-scheme: dark;
  }
}
:root[data-theme="dark"] {
  --bg: #000; --card: #1c1c1e; --fg: #f2f2f7; --muted: #98989f; --grid: #2c2c2e; --line: #38383a;
  --green: #32d74b; --orange: #ff9f0a; --red: #ff453a; --tab: rgba(22, 22, 24, .94);
  color-scheme: dark;
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0; background: var(--bg); color: var(--fg);
  font: 15px/1.35 -apple-system, system-ui, Roboto, "Segoe UI", sans-serif;
  padding: calc(56px + env(safe-area-inset-top)) 0 calc(64px + env(safe-area-inset-bottom));
  overflow-x: hidden;
}
a { color: inherit; text-decoration: none; }
b { font-weight: 650; }
h3 { font-size: 15px; margin: 0 0 10px; display: flex; align-items: baseline; gap: 6px; }
h3 .avg { margin-left: auto; font-size: 12px; font-weight: 400; color: var(--muted); }
h4 { font-size: 13px; margin: 14px 0 6px; color: var(--muted); text-transform: uppercase; letter-spacing: .02em; }
h4.sec { margin: 4px 16px 8px; }
.muted { color: var(--muted); font-weight: 400; }
.small { font-size: 12px; }
.center { text-align: center; padding: 24px 16px; }
.err { color: var(--red); }

/* Cabecera y pestañas */
#top {
  position: fixed; top: 0; left: 0; right: 0; z-index: 1000; display: flex; align-items: center; gap: 4px;
  padding: env(safe-area-inset-top) 8px 0; height: calc(52px + env(safe-area-inset-top));
  background: var(--tab); backdrop-filter: blur(16px); -webkit-backdrop-filter: blur(16px); border-bottom: 1px solid var(--line);
}
#top .ttl { flex: 1; min-width: 0; padding-left: 8px; }
#top h1 { font-size: 18px; margin: 0; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
#status { font-size: 11px; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
button.icon { background: none; border: 0; color: var(--accent); font-size: 26px; width: 44px; height: 44px; border-radius: 22px; cursor: pointer; }
button.icon:active { background: var(--grid); }
#refresh.spin { animation: spin 1s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }
#tabs {
  position: fixed; bottom: 0; left: 0; right: 0; z-index: 1000; display: flex;
  padding-bottom: env(safe-area-inset-bottom); background: var(--tab);
  backdrop-filter: blur(16px); -webkit-backdrop-filter: blur(16px); border-top: 1px solid var(--line);
}
#tabs a { flex: 1; display: flex; flex-direction: column; align-items: center; gap: 1px; padding: 6px 0 7px; font-size: 10.5px; color: var(--muted); }
#tabs a span { font-size: 20px; filter: grayscale(1); opacity: .7; }
#tabs a.on { color: var(--accent); font-weight: 600; }
#tabs a.on span { filter: none; opacity: 1; }

main { max-width: 760px; margin: 0 auto; padding: 10px 0 16px; }

/* Controles */
.bar { display: flex; gap: 8px; align-items: center; padding: 4px 16px 10px; }
.bar.wrap { flex-wrap: wrap; }
.bar > select, .bar > input[type=search] { flex: 1 1 140px; min-width: 0; }
select, input[type=search] {
  font: inherit; font-size: 14px; color: var(--fg); background: var(--card); border: 1px solid var(--line);
  border-radius: 10px; padding: 8px 10px; height: 38px; appearance: none; -webkit-appearance: none;
}
select { padding-right: 26px; background-image: linear-gradient(45deg, transparent 50%, var(--muted) 50%), linear-gradient(135deg, var(--muted) 50%, transparent 50%);
  background-position: calc(100% - 14px) 16px, calc(100% - 9px) 16px; background-size: 5px 5px; background-repeat: no-repeat; }
input[type=search] { flex-basis: 100%; }
.seg { display: flex; background: var(--grid); border-radius: 9px; padding: 2px; flex: 1 1 auto; }
.seg button { flex: 1; border: 0; background: none; font: inherit; font-size: 13px; color: var(--fg); padding: 6px 4px; border-radius: 7px; white-space: nowrap; }
.seg button.on { background: var(--card); font-weight: 600; box-shadow: 0 1px 3px rgba(0,0,0,.15); }
.chk { display: flex; align-items: center; gap: 6px; font-size: 14px; white-space: nowrap; }
.chk input { width: 18px; height: 18px; accent-color: var(--accent); }
.btn { font: inherit; font-size: 15px; border: 0; border-radius: 10px; padding: 10px 16px; background: var(--card); color: var(--accent); cursor: pointer; }
.btn:disabled { opacity: .35; }
.bar .btn { padding: 6px 14px; font-size: 22px; line-height: 1; }
.btn.wide { display: block; width: calc(100% - 32px); margin: 10px 16px; }
.card .btn.wide { width: 100%; margin: 10px 0 0; background: var(--bg); }
.btn.primary { background: var(--accent); color: #fff; font-weight: 600; }
.btn.ghost { color: var(--red); }

/* Tarjetas e indicadores */
.card { background: var(--card); border-radius: 14px; padding: 14px; margin: 0 16px 12px; }
.ch { position: relative; width: 100%; }
.kpis { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; padding: 0 16px 12px; }
.kpi { background: var(--card); border-radius: 14px; padding: 12px; min-width: 0; }
.kpi .k { font-size: 12px; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.kpi .v { font-size: 22px; font-weight: 700; font-variant-numeric: tabular-nums; letter-spacing: -.01em; margin: 2px 0; white-space: nowrap; }
.kpi .sub { font-size: 11.5px; color: var(--muted); }
.cmp { font-size: 11.5px; margin-top: 3px; }
.cmp.up, td.up { color: var(--green); } .cmp.down, td.down { color: var(--orange); }
.chips { display: grid; grid-template-columns: repeat(auto-fill, minmax(104px, 1fr)); gap: 8px; padding: 0 16px 12px; }
.chip { background: var(--card); border-radius: 12px; padding: 10px; }
.chip span { display: block; font-size: 11px; color: var(--muted); }
.chip b { font-size: 15px; font-variant-numeric: tabular-nums; white-space: nowrap; }
.chip.dim { opacity: .5; }
.readout { font-size: 12.5px; color: var(--muted); padding: 6px 2px 0; font-variant-numeric: tabular-nums; min-height: 22px; }
#trackArea > .readout { padding: 6px 18px 8px; }
.legend { display: flex; flex-wrap: wrap; gap: 10px; font-size: 12px; color: var(--muted); margin-top: 6px; }
.legend i { display: inline-block; width: 8px; height: 8px; border-radius: 4px; margin-right: 4px; }
.vol + .vol { margin-top: 10px; }
.vl { font-size: 12px; font-weight: 600; margin-bottom: 2px; }

/* Listas */
.list { background: var(--card); border-radius: 14px; margin: 0 16px 12px; overflow: hidden; }
.card .list { margin: 0; border-radius: 0; background: none; }
.row { display: flex; align-items: center; gap: 10px; padding: 10px 12px; border-bottom: 1px solid var(--grid); }
.row:last-child { border-bottom: 0; }
.row:active { background: var(--grid); }
.row .ico { font-size: 20px; width: 26px; text-align: center; flex: none; }
.row .l { flex: 1; min-width: 0; }
.row .t { font-weight: 600; font-size: 14.5px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.row .s { font-size: 11.5px; color: var(--muted); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; margin-top: 1px; }
.row .r { display: flex; flex-direction: column; align-items: flex-end; font-size: 12px; color: var(--muted); font-variant-numeric: tabular-nums; flex: none; }
.row .r b { color: var(--fg); font-size: 14px; }
.row .r .hr { color: var(--red); }
.row .chev { color: var(--muted); font-size: 22px; }
.mapi { font-size: 11px; }
.menu .row { padding: 14px 12px; }
.group .gh { display: flex; justify-content: space-between; gap: 8px; align-items: baseline; padding: 8px 18px 6px; font-size: 13px; }
.group .gh span { color: var(--muted); font-size: 11.5px; white-space: nowrap; }
.totals { display: grid; grid-template-columns: repeat(4, 1fr); gap: 4px; padding: 0 16px 8px; }
.totals div { display: flex; flex-direction: column; }
.totals span { font-size: 10.5px; color: var(--muted); }
.totals b { font-size: 14px; font-variant-numeric: tabular-nums; white-space: nowrap; }
.more { display: block; text-align: center; color: var(--accent); padding: 10px 0 0; }
.tag { display: inline-block; font-size: 10.5px; font-weight: 700; padding: 1px 6px; border-radius: 6px; vertical-align: 1px;
  color: var(--c); background: color-mix(in srgb, var(--c) 18%, transparent); white-space: nowrap; margin-left: 2px; }
.up { display: flex; justify-content: space-between; align-items: center; gap: 8px; padding: 7px 0; border-bottom: 1px solid var(--grid); }
.up:last-child { border-bottom: 0; }
.up .s { font-size: 12px; color: var(--muted); }
.up .r { display: flex; gap: 4px; align-items: center; flex-wrap: wrap; justify-content: flex-end; }
.up .days { font-size: 12px; color: var(--muted); white-space: nowrap; margin-left: 4px; }

/* Tablas */
.hscroll { overflow-x: auto; margin: 0 -14px; padding: 0 14px; -webkit-overflow-scrolling: touch; }
.tbl { width: 100%; border-collapse: collapse; font-size: 13px; font-variant-numeric: tabular-nums; }
.tbl th { font-size: 11px; color: var(--muted); font-weight: 600; text-align: right; padding: 4px 6px; white-space: nowrap; border-bottom: 1px solid var(--line); }
.tbl td { text-align: right; padding: 5px 6px; border-bottom: 1px solid var(--grid); white-space: nowrap; }
.tbl .l { text-align: left; }
.tbl td.l { white-space: normal; }
.tbl td.nowrap { white-space: nowrap; }
.tbl tr:last-child td { border-bottom: 0; }
.tbl tr.off td { color: var(--muted); }
.tbl tr.tap { cursor: pointer; }
.tbl tr.on td { background: color-mix(in srgb, var(--blue) 14%, transparent); }
.tbl tr.tot td { font-weight: 700; background: color-mix(in srgb, var(--red) 9%, transparent); }
.tbl tr.grp th { text-align: center; border-bottom: 0; padding-bottom: 0; }
.tbl .red { color: var(--red); }
.tbl a[data-country] { color: var(--blue); }
.tbl a.sel { text-decoration: underline; font-weight: 700; }
.barc { width: 30%; min-width: 60px; }
.barc i { display: block; height: 8px; border-radius: 2px; background: #FF9500b3; }

/* Detalle de actividad */
.dh { padding: 0 16px 12px; }
.dh .t { font-size: 21px; font-weight: 700; line-height: 1.2; }
.dh .tags { margin: 6px 0; }
.dh .s { font-size: 13px; color: var(--muted); margin-top: 2px; }
.metrics { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; padding: 0 16px 12px; }
.metrics .m { background: var(--card); border-radius: 12px; padding: 8px 10px; min-width: 0; }
.metrics span { display: block; font-size: 11px; color: var(--muted); }
.metrics b { font-size: 15px; font-variant-numeric: tabular-nums; white-space: nowrap; }
.mapwrap { isolation: isolate; position: relative; margin: 0 16px; border-radius: 14px; overflow: hidden; height: 300px; }
#map { height: 100%; background: var(--grid); }
.maplock { position: absolute; z-index: 500; top: 8px; right: 8px; font: inherit; font-size: 13px; border: 0; border-radius: 9px;
  padding: 7px 10px; background: rgba(255,255,255,.92); color: #1c1c1e; box-shadow: 0 1px 4px rgba(0,0,0,.25); }
.notes { white-space: pre-wrap; margin: 0; font-size: 14px; }

/* Zapatillas */
.shoe { display: flex; align-items: center; gap: 10px; padding: 11px 12px; border-bottom: 1px solid var(--grid); }
.shoe:last-child { border-bottom: 0; }
.shoe .l { flex: 1; min-width: 0; }
.shoe .s { font-size: 11.5px; color: var(--muted); margin: 2px 0 6px; }
.shoe .r { text-align: right; font-variant-numeric: tabular-nums; }
.shoe .r b { font-size: 18px; display: block; }
.shoe .r span { font-size: 11px; color: var(--muted); }
.shoe.retired { opacity: .6; }
.shoe.active { background: color-mix(in srgb, var(--accent) 9%, transparent); }
.prog { height: 6px; border-radius: 3px; background: var(--grid); overflow: hidden; }
.prog i { display: block; height: 100%; }

/* Carreras */
.pbs { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; padding: 0 16px 12px; }
.pb { background: var(--card); border-radius: 14px; padding: 10px 12px; min-width: 0; }
.pb .k { font-size: 11.5px; color: var(--muted); }
.pb b { font-size: 21px; font-variant-numeric: tabular-nums; }
.pb .s { font-size: 11px; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.race { display: flex; gap: 10px; align-items: center; padding: 8px 0; border-bottom: 1px solid var(--grid); }
.race:last-child { border-bottom: 0; }
.race > i { width: 8px; height: 8px; border-radius: 4px; flex: none; }
.race .l { flex: 1; min-width: 0; }
.race .l b { font-size: 14px; }
.race .s { font-size: 11.5px; color: var(--muted); }
.race .r { text-align: right; font-variant-numeric: tabular-nums; flex: none; }
.race .rk { width: 22px; text-align: center; font-weight: 700; color: var(--muted); flex: none; font-variant-numeric: tabular-nums; }
.race .s.good { color: var(--green); } .race .s.bad { color: var(--orange); }
.marks { display: grid; grid-template-columns: repeat(auto-fill, minmax(140px, 1fr)); gap: 8px; }
.mark { background: var(--bg); border-radius: 10px; padding: 8px 10px; display: flex; flex-direction: column; }
.mark span, .mark small { font-size: 11.5px; color: var(--muted); }
.mark b { font-size: 18px; font-variant-numeric: tabular-nums; }
details.place { border-bottom: 1px solid var(--grid); }
details.place summary { display: flex; justify-content: space-between; padding: 8px 0; cursor: pointer; list-style: none; }
details.place summary::-webkit-details-marker { display: none; }
details.place summary::before { content: '›'; color: var(--muted); margin-right: 6px; transition: transform .15s; display: inline-block; }
details.place[open] summary::before { transform: rotate(90deg); }
details.place summary b { flex: 1; }
.city { display: flex; justify-content: space-between; padding: 4px 0 4px 18px; font-size: 13.5px; }

/* Bienvenida */
.welcome { text-align: center; padding: 40px 24px; }
.welcome img { border-radius: 22px; }
.welcome p { color: var(--muted); }
.welcome .btn.wide { width: 100%; margin: 20px 0 0; }

@media (min-width: 620px) {
  .kpis { grid-template-columns: repeat(4, 1fr); }
  .metrics { grid-template-columns: repeat(5, 1fr); }
  .pbs { grid-template-columns: repeat(4, 1fr); }
}

/* Horizontal: cabecera y pestañas más bajas, y margen para la cámara en los laterales. */
main { padding-left: env(safe-area-inset-left); padding-right: env(safe-area-inset-right); }
#top { padding-left: calc(8px + env(safe-area-inset-left)); padding-right: calc(8px + env(safe-area-inset-right)); }
#tabs { padding-left: env(safe-area-inset-left); padding-right: env(safe-area-inset-right); }
@media (orientation: landscape) and (max-height: 500px) {
  body { padding-top: calc(42px + env(safe-area-inset-top)); padding-bottom: calc(40px + env(safe-area-inset-bottom)); }
  #top { height: calc(42px + env(safe-area-inset-top)); }
  #top .ttl { display: flex; align-items: baseline; gap: 10px; }
  #top h1 { font-size: 16px; }
  button.icon { width: 38px; height: 38px; font-size: 22px; }
  #tabs a { flex-direction: row; justify-content: center; gap: 6px; padding: 9px 0; font-size: 12px; }
  #tabs a span { font-size: 16px; }
  .mapwrap { height: 260px; }
}
