/* Garmin Dashboard — dark-first design system.
   Tokens are overridden for light mode and again for paper (print). */

:root {
  --bg: #080a0e;
  --bg-grad: radial-gradient(1200px 700px at 15% -10%, #12261d 0%, #080a0e 55%);
  --panel: #11151b;
  --panel-2: #161b23;
  --line: rgba(255, 255, 255, 0.09);
  --line-strong: rgba(255, 255, 255, 0.18);
  --text: #e9eef5;
  --dim: #8b98ab;
  --accent: #3ddc97;
  --accent-2: #22d3ee;
  --danger: #ff6b6b;
  --grid: rgba(255, 255, 255, 0.07);
  --shadow: 0 18px 40px -24px rgba(0, 0, 0, 0.9);
  --radius: 16px;
  --radius-sm: 10px;
  --font: -apple-system, BlinkMacSystemFont, "Segoe UI", Inter, Roboto, "Helvetica Neue", "PingFang HK", "Noto Sans HK", sans-serif;
  --mono: ui-monospace, SFMono-Regular, "SF Mono", Menlo, Consolas, monospace;
  color-scheme: dark;
}

@media (prefers-color-scheme: light) {
  :root {
    --bg: #f6f8fb;
    --bg-grad: radial-gradient(1000px 600px at 12% -10%, #dff5ea 0%, #f6f8fb 60%);
    --panel: #ffffff;
    --panel-2: #f3f6fa;
    --line: rgba(12, 22, 38, 0.1);
    --line-strong: rgba(12, 22, 38, 0.22);
    --text: #0e1622;
    --dim: #5b6b80;
    --accent: #0f9d63;
    --accent-2: #0e7490;
    --danger: #d64545;
    --grid: rgba(12, 22, 38, 0.08);
    --shadow: 0 18px 40px -28px rgba(12, 22, 38, 0.45);
    color-scheme: light;
  }
}

* { box-sizing: border-box; }

html { -webkit-text-size-adjust: 100%; }

body {
  margin: 0;
  min-height: 100vh;
  background: var(--bg);
  background-image: var(--bg-grad);
  background-repeat: no-repeat;
  color: var(--text);
  font-family: var(--font);
  font-size: 15px;
  line-height: 1.55;
  -webkit-font-smoothing: antialiased;
}

[hidden] { display: none !important; }

h1, h2, h3 { margin: 0; line-height: 1.2; letter-spacing: -0.015em; font-weight: 650; }
h1 { font-size: clamp(1.35rem, 2.4vw, 1.7rem); }
h2 { font-size: 1rem; }
h3 { font-size: 0.95rem; margin-bottom: 0.5rem; }
p { margin: 0; }
a { color: var(--accent); }

.muted { color: var(--dim); }
.small { font-size: 0.8rem; }
.sr-only {
  position: absolute; width: 1px; height: 1px; padding: 0; margin: -1px;
  overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; border: 0;
}

.screen {
  max-width: 1180px;
  margin: 0 auto;
  padding: clamp(1rem, 2.4vw, 2rem) clamp(0.9rem, 2.4vw, 2rem) 3rem;
}

/* ── buttons & inputs ─────────────────────────────────────────────── */

.btn {
  display: inline-flex;
  align-items: center;
  gap: 0.45rem;
  padding: 0.55rem 0.95rem;
  border: 1px solid var(--line);
  border-radius: 999px;
  background: var(--panel-2);
  color: var(--text);
  font: inherit;
  font-size: 0.86rem;
  font-weight: 550;
  cursor: pointer;
  transition: background 0.15s ease, border-color 0.15s ease, transform 0.12s ease;
}
.btn svg { width: 15px; height: 15px; flex: none; }
.btn:hover { border-color: var(--line-strong); background: color-mix(in oklab, var(--panel-2) 80%, var(--text) 6%); }
.btn:active { transform: translateY(1px); }
.btn:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
.btn-primary {
  background: linear-gradient(180deg, color-mix(in oklab, var(--accent) 92%, white), var(--accent));
  border-color: transparent;
  color: #052016;
  font-weight: 700;
}
.btn-primary:hover { filter: brightness(1.06); background: var(--accent); }
.btn-ghost { background: transparent; }
.btn-quiet { background: transparent; }
.btn-small { padding: 0.35rem 0.7rem; font-size: 0.78rem; }
.btn-danger { background: transparent; border-color: color-mix(in oklab, var(--danger) 45%, transparent); color: var(--danger); }
.btn-danger:hover { background: color-mix(in oklab, var(--danger) 14%, transparent); }
.btn[disabled] { opacity: 0.6; cursor: progress; }

input, select {
  font: inherit;
  color: var(--text);
  background: var(--panel-2);
  border: 1px solid var(--line);
  border-radius: var(--radius-sm);
  padding: 0.6rem 0.75rem;
}
input:focus-visible, select:focus-visible { outline: 2px solid var(--accent); outline-offset: 1px; border-color: transparent; }
select { appearance: none; padding-right: 2rem;
  background-image: linear-gradient(45deg, transparent 50%, var(--dim) 50%), linear-gradient(135deg, var(--dim) 50%, transparent 50%);
  background-position: calc(100% - 18px) 52%, calc(100% - 13px) 52%;
  background-size: 5px 5px, 5px 5px;
  background-repeat: no-repeat;
}
.select-wrap { display: inline-flex; }

/* ── connect screen ───────────────────────────────────────────────── */

.connect-screen { display: flex; align-items: center; justify-content: center; min-height: 100vh; padding-top: 2rem; }
.connect-card {
  width: min(560px, 100%);
  background: color-mix(in oklab, var(--panel) 88%, transparent);
  border: 1px solid var(--line);
  border-radius: 22px;
  padding: clamp(1.4rem, 3vw, 2.4rem);
  box-shadow: var(--shadow);
  backdrop-filter: blur(14px);
}
.brand { display: flex; align-items: center; gap: 0.6rem; margin-bottom: 1.6rem; }
.brand-mark { width: 30px; height: 30px; color: var(--accent); }
.brand-name { font-weight: 700; letter-spacing: -0.02em; }
.brand-dim { color: var(--dim); font-weight: 500; margin-left: 0.3rem; }
.connect-card h1 { margin-bottom: 0.6rem; }
.lede { color: var(--dim); margin-bottom: 1.6rem; }

.steps { list-style: none; margin: 0 0 1.5rem; padding: 0; display: grid; gap: 0.7rem; }
.steps li { display: flex; gap: 0.7rem; align-items: flex-start; flex-wrap: wrap; font-size: 0.9rem; color: var(--text); }
.steps li > span:not(.step-num) { flex: 1 1 14rem; min-width: 0; }
.inline-link { flex-basis: 100%; margin-left: 1.85rem; font-weight: 600; }
.step-num {
  flex: none; width: 22px; height: 22px; border-radius: 50%;
  display: grid; place-items: center;
  background: color-mix(in oklab, var(--accent) 18%, transparent);
  color: var(--accent); font-size: 0.75rem; font-weight: 700; margin-top: 0.15rem;
}
.inline-link { flex-basis: 100%; margin-left: 1.85rem; font-weight: 600; }

.field { display: block; margin-bottom: 0.75rem; }
.field-label { display: block; font-size: 0.8rem; color: var(--dim); margin-bottom: 0.35rem; }
.field input { width: 100%; font-family: var(--mono); font-size: 0.86rem; letter-spacing: 0.02em; }
.checkbox { display: flex; align-items: center; gap: 0.5rem; font-size: 0.84rem; color: var(--dim); cursor: pointer; }
.checkbox input { accent-color: var(--accent); width: 15px; height: 15px; }
.error-msg {
  margin: 0.9rem 0 0; padding: 0.6rem 0.8rem; font-size: 0.84rem;
  border-radius: var(--radius-sm);
  background: color-mix(in oklab, var(--danger) 14%, transparent);
  border: 1px solid color-mix(in oklab, var(--danger) 40%, transparent);
  color: color-mix(in oklab, var(--danger) 88%, var(--text));
}
.connect-actions { display: flex; gap: 0.6rem; margin-top: 1.1rem; flex-wrap: wrap; }
.connect-actions .btn { padding: 0.65rem 1.2rem; }

.privacy-list { margin: 1.6rem 0 0; padding: 1rem 0 0; border-top: 1px solid var(--line); list-style: none; display: grid; gap: 0.45rem; }
.privacy-list li { font-size: 0.78rem; color: var(--dim); padding-left: 1.2rem; position: relative; }
.privacy-list li::before {
  content: ""; position: absolute; left: 0.25rem; top: 0.5rem;
  width: 5px; height: 5px; border-radius: 50%; background: var(--accent); opacity: 0.7;
}

/* ── dashboard chrome ─────────────────────────────────────────────── */

.banner {
  display: flex; flex-wrap: wrap; gap: 0.4rem; align-items: center;
  padding: 0.55rem 0.9rem; margin-bottom: 0.9rem;
  border-radius: var(--radius-sm); font-size: 0.82rem;
  border: 1px solid var(--line);
}
.banner-demo { background: color-mix(in oklab, #f59e0b 16%, var(--panel)); border-color: color-mix(in oklab, #f59e0b 40%, transparent); }
.banner-demo strong { color: #f59e0b; }
.banner-info { background: color-mix(in oklab, var(--accent-2) 12%, var(--panel)); border-color: color-mix(in oklab, var(--accent-2) 30%, transparent); color: var(--dim); }

.top-bar { display: flex; flex-wrap: wrap; gap: 1rem; align-items: center; justify-content: space-between; margin-bottom: 0.8rem; }
.athlete { display: flex; align-items: center; gap: 0.8rem; min-width: 0; }
.avatar {
  width: 46px; height: 46px; border-radius: 50%; flex: none;
  display: grid; place-items: center;
  background: var(--panel-2); background-size: cover; background-position: center;
  border: 1px solid var(--line-strong);
  font-weight: 700; color: var(--accent);
}
.athlete-text h1 { font-size: 1.2rem; }
.athlete-text p { font-size: 0.78rem; font-variant-numeric: tabular-nums; }
.controls { display: flex; flex-wrap: wrap; gap: 0.45rem; align-items: center; }

.sync-status { min-height: 1.2rem; font-size: 0.78rem; color: var(--dim); margin-bottom: 1rem; font-variant-numeric: tabular-nums; }
.sync-status.is-error { color: var(--danger); }

/* ── stats ────────────────────────────────────────────────────────── */

.stats-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(148px, 1fr));
  gap: 0.7rem;
  margin-bottom: 1.4rem;
}
.stat {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 0.85rem 0.95rem;
  position: relative;
  overflow: hidden;
}
.stat::after {
  content: ""; position: absolute; inset: 0 auto auto 0; height: 2px; width: 100%;
  background: linear-gradient(90deg, var(--accent), transparent 70%);
  opacity: 0.55;
}
.stat-label { font-size: 0.72rem; text-transform: uppercase; letter-spacing: 0.08em; color: var(--dim); }
.stat-value { font-size: clamp(1.2rem, 2.1vw, 1.55rem); font-weight: 700; margin-top: 0.15rem; font-variant-numeric: tabular-nums; letter-spacing: -0.02em; }
.stat-sub { font-size: 0.72rem; color: var(--dim); font-variant-numeric: tabular-nums; min-height: 1em; }

.highlights { display: grid; grid-template-columns: repeat(auto-fit, minmax(190px, 1fr)); gap: 0.7rem; margin-bottom: 1.4rem; }
.hl {
  display: flex; flex-direction: column; gap: 0.15rem;
  background: linear-gradient(180deg, color-mix(in oklab, var(--accent) 8%, var(--panel)), var(--panel));
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 0.8rem 1rem;
}
.hl-label { font-size: 0.72rem; text-transform: uppercase; letter-spacing: 0.08em; color: var(--dim); }
.hl strong { font-size: 1.12rem; font-weight: 700; font-variant-numeric: tabular-nums; letter-spacing: -0.01em; }
.hl-sub { font-size: 0.74rem; color: var(--dim); min-height: 1em; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

/* ── panels & charts ──────────────────────────────────────────────── */

.panel {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 1rem 1.05rem 1.1rem;
}
.panel-head { display: flex; flex-wrap: wrap; align-items: baseline; gap: 0.5rem 0.75rem; margin-bottom: 0.75rem; }
.panel-head h2 { font-size: 0.9rem; font-weight: 620; }
.panel-head .small { margin-left: auto; }

.charts-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 0.7rem; margin-bottom: 1.4rem; }
.chart-wide { grid-column: 1 / -1; }
.chart { width: 100%; height: 190px; display: block; overflow: visible; }
.chart-short { height: 90px; }

.chart .grid { stroke: var(--grid); stroke-width: 1; shape-rendering: crispEdges; }
.chart .axis-label { fill: var(--dim); font-size: 9.5px; font-family: var(--font); font-variant-numeric: tabular-nums; }
.chart .bar { fill: var(--accent); opacity: 0.92; }
.chart .bar:hover { opacity: 1; }
.chart .line { fill: none; stroke: var(--accent-2); stroke-width: 2; stroke-linejoin: round; stroke-linecap: round; }
.chart .avg-line { fill: none; stroke: var(--dim); stroke-width: 1.6; stroke-dasharray: 4 4; opacity: 0.9; }
.chart .dot { fill: var(--accent-2); }
.chart .zone-rect { opacity: 0.92; }
.chart .zone-pct { fill: #06121a; font-size: 11px; font-weight: 650; font-family: var(--font); }

.legend { display: flex; flex-wrap: wrap; gap: 0.4rem 1rem; font-size: 0.75rem; color: var(--dim); }
.legend-item { display: inline-flex; align-items: center; gap: 0.35rem; font-variant-numeric: tabular-nums; }
.legend-item i { width: 9px; height: 9px; border-radius: 3px; display: inline-block; }
.legend-item b { color: var(--text); font-weight: 600; }

/* ── map ──────────────────────────────────────────────────────────── */

.map-panel { padding-bottom: 1rem; margin-bottom: 1.4rem; }
.map-wrap { position: relative; height: clamp(300px, 42vh, 460px); border-radius: var(--radius-sm); overflow: hidden; border: 1px solid var(--line); background: var(--panel-2); }
#map { position: absolute; inset: 0; z-index: 1; background: var(--panel-2); }
.map-offline {
  position: absolute; inset: 0; z-index: 3; display: grid; place-items: center;
  background: color-mix(in oklab, var(--panel) 92%, transparent);
  color: var(--dim); font-size: 0.85rem;
}
.leaflet-container { font-family: var(--font); background: var(--panel-2); }
.leaflet-control-attribution { font-size: 9px !important; background: rgba(0, 0, 0, 0.45) !important; color: #cbd5e1 !important; }
.leaflet-control-attribution a { color: #7dd3fc !important; }
.leaflet-bar a { background: var(--panel) !important; color: var(--text) !important; border-color: var(--line) !important; }

/* ── table ────────────────────────────────────────────────────────── */

.table-panel { padding-bottom: 0.5rem; }
.table-tools { display: flex; flex-wrap: wrap; gap: 0.5rem; align-items: center; margin-left: auto; }
.table-tools input { min-width: 190px; font-size: 0.82rem; padding: 0.42rem 0.65rem; }
.chips { display: flex; flex-wrap: wrap; gap: 0.3rem; }
.chip {
  font: inherit; font-size: 0.75rem; padding: 0.28rem 0.65rem;
  border-radius: 999px; border: 1px solid var(--line);
  background: transparent; color: var(--dim); cursor: pointer;
}
.chip:hover { border-color: var(--line-strong); color: var(--text); }
.chip.is-active { background: color-mix(in oklab, var(--accent) 16%, transparent); border-color: color-mix(in oklab, var(--accent) 45%, transparent); color: var(--accent); font-weight: 600; }

.table-wrap { overflow: auto; max-height: 560px; border-radius: var(--radius-sm); }
table { width: 100%; border-collapse: collapse; font-size: 0.84rem; }
th, td { text-align: left; padding: 0.55rem 0.7rem; white-space: nowrap; }
thead th {
  position: sticky; top: 0; z-index: 2;
  background: var(--panel);
  border-bottom: 1px solid var(--line-strong);
  font-size: 0.72rem; font-weight: 600; color: var(--dim);
  text-transform: uppercase; letter-spacing: 0.06em;
}
thead th button {
  all: unset; cursor: pointer; display: inline-flex; align-items: center; gap: 0.25rem;
}
thead th button:hover { color: var(--text); }
thead th button:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; border-radius: 4px; }
th[aria-sort] button::after { content: "↓"; font-size: 0.7rem; opacity: 0.75; }
th[aria-sort="ascending"] button::after { content: "↑"; }
tbody tr { border-bottom: 1px solid var(--line); cursor: pointer; }
tbody tr:hover, tbody tr:focus-visible { background: var(--panel-2); outline: none; }
tbody tr:last-child { border-bottom: none; }
td.num, th.num { text-align: right; font-variant-numeric: tabular-nums; }
td.col-name { display: flex; align-items: center; gap: 0.45rem; }
.type-dot { width: 7px; height: 7px; border-radius: 50%; flex: none; }
.table-foot { padding: 0.5rem 0.2rem 0.3rem; }
.empty { padding: 1.4rem 0.7rem; color: var(--dim); font-size: 0.85rem; text-align: center; }

/* ── drawer ───────────────────────────────────────────────────────── */

.drawer {
  padding: 0; border: 1px solid var(--line); border-radius: 18px;
  background: var(--panel); color: var(--text);
  width: min(620px, 94vw); max-height: 88vh;
  box-shadow: 0 40px 80px -30px rgba(0, 0, 0, 0.8);
}
.drawer::backdrop { background: rgba(3, 6, 10, 0.66); backdrop-filter: blur(2px); }
.drawer-inner { padding: 1.4rem 1.4rem 1.2rem; overflow-y: auto; max-height: 88vh; overscroll-behavior: contain; }
.drawer-close-form { float: right; margin: -0.4rem -0.4rem 0 0; }
.drawer-close { padding: 0.4rem; }
.drawer-close svg { width: 18px; height: 18px; }
.drawer h2 { font-size: 1.15rem; margin-bottom: 0.2rem; }
.drawer-date { font-size: 0.8rem; margin-bottom: 1rem; font-variant-numeric: tabular-nums; }
.drawer-stats { display: grid; grid-template-columns: repeat(auto-fit, minmax(120px, 1fr)); gap: 0.6rem; margin-bottom: 1.2rem; }
.drawer-stats div { background: var(--panel-2); border: 1px solid var(--line); border-radius: var(--radius-sm); padding: 0.5rem 0.65rem; }
.drawer-stats span { display: block; font-size: 0.7rem; text-transform: uppercase; letter-spacing: 0.06em; color: var(--dim); }
.drawer-stats strong { font-size: 0.98rem; font-variant-numeric: tabular-nums; }
.drawer-table { max-height: none; overflow: visible; margin-bottom: 1rem; }
.mini-map { height: 220px; border-radius: var(--radius-sm); overflow: hidden; border: 1px solid var(--line); background: var(--panel-2); }

.page-foot { margin-top: 1.8rem; text-align: center; }

.print-head, .print-only { display: none; }

/* ── responsive ───────────────────────────────────────────────────── */

@media (max-width: 760px) {
  body { font-size: 14.5px; }
  .charts-grid { grid-template-columns: 1fr; }
  .chart { height: 170px; }
  .top-bar { align-items: flex-start; }
  .controls { width: 100%; }
  .controls .btn span { display: none; }
  .controls .btn { padding: 0.5rem 0.65rem; }
  .select-wrap { flex: 1; }
  .select-wrap select { width: 100%; }
  .table-tools { margin-left: 0; width: 100%; }
  .table-tools input { flex: 1; min-width: 120px; }
  .table-wrap { max-height: 460px; }
  thead th, tbody td { padding: 0.5rem 0.45rem; }
  .connect-card { padding: 1.3rem; }
  .drawer { width: 100vw; max-height: 92vh; border-radius: 18px 18px 0 0; }
}

@media (prefers-reduced-motion: reduce) {
  * { transition: none !important; animation: none !important; }
}

/* ── print / PDF ──────────────────────────────────────────────────── */

@page { size: A4 portrait; margin: 13mm 12mm; }

@media print {
  :root {
    --text: #0b1220;
    --dim: #4b5563;
    --line: #d7dce3;
    --line-strong: #b9c0ca;
    --panel: #ffffff;
    --panel-2: #f7f8fa;
    --accent: #0f9d63;
    --grid: #e5e8ed;
    color-scheme: light;
  }
  html, body {
    background: #fff !important;
    background-image: none !important;
    color: #0b1220;
    font-size: 10.5pt;
  }
  .screen { max-width: none; padding: 0; margin: 0; }

  .controls, .sync-status, .banner, .table-tools, .chips, .chip, .map-panel,
  #table-announcer, .page-foot, .btn, .drawer { display: none !important; }

  .print-head { display: block; margin-bottom: 8mm; }
  .print-head h1 { font-size: 20pt; letter-spacing: -0.02em; }
  .print-head p { color: #4b5563; font-size: 9.5pt; margin-top: 2mm; }

  .top-bar { margin-bottom: 6mm; }
  .avatar { width: 34px; height: 34px; font-size: 12pt; }
  .athlete-text h1 { font-size: 13pt; }

  .stats-grid { grid-template-columns: repeat(3, 1fr); gap: 3mm; margin-bottom: 6mm; }
  .stat { border: 1px solid #d7dce3; border-radius: 6px; padding: 3mm; break-inside: avoid; }
  .stat::after { display: none; }
  .stat-label { font-size: 7.5pt; color: #6b7280; }
  .stat-value { font-size: 13pt; }
  .stat-sub { font-size: 7.5pt; color: #6b7280; }

  .highlights { grid-template-columns: repeat(3, 1fr); gap: 3mm; margin-bottom: 6mm; }
  .hl { border: 1px solid #d7dce3; border-radius: 6px; padding: 3mm; background: #fff; }
  .hl-label { font-size: 7.5pt; color: #6b7280; }
  .hl strong { font-size: 11pt; }
  .hl-sub { font-size: 7.5pt; color: #6b7280; }

  .charts-grid { grid-template-columns: 1fr 1fr; gap: 4mm; margin-bottom: 0; }
  .panel { border: 1px solid #d7dce3; border-radius: 6px; padding: 3mm; break-inside: avoid; }
  .chart { height: 44mm; }
  .chart .bar { fill: #111827; }
  .chart .line { stroke: #0f172a; }
  .chart .avg-line { stroke: #6b7280; }
  .chart .dot { fill: #0f172a; }
  .chart .axis-label { fill: #4b5563; font-size: 7pt; }
  .chart .grid { stroke: #e5e8ed; }
  .chart .zone-pct { fill: #ffffff; }

  .table-panel { break-before: auto; break-inside: auto; border: none; padding: 0; }
  .print-types { break-before: page; margin-top: 0; }
  .table-panel .panel-head { margin-bottom: 3mm; }
  .table-wrap { max-height: none; overflow: visible; }
  table { font-size: 8.5pt; }
  thead { display: table-header-group; }
  thead th { position: static; background: #fff; border-bottom: 1pt solid #9aa3af; color: #374151; }
  tbody tr:nth-child(n + 25) { display: none; }
  tbody tr { break-inside: avoid; border-bottom: 0.4pt solid #e5e7eb; }
  td.col-name { display: table-cell; }
  .type-dot { display: inline-block; margin-right: 2mm; }
  .table-foot::after { content: " · continued rows omitted"; }
  .empty { display: none; }

  .print-only { display: block; }
  .print-route { break-before: page; border: none; padding: 0; }
  .print-route h2 { font-size: 12pt; margin-bottom: 3mm; }
  #print-map-canvas {
    display: block; width: 100%; height: auto;
    border: 1px solid #d7dce3; border-radius: 6px; background: #fff;
  }
  .print-route .legend { margin-top: 3mm; color: #4b5563; }
  .print-types { margin-top: 0; padding: 0; border: none; }
  .print-types h2 { font-size: 10pt; margin-bottom: 2mm; }
  .print-types table { font-size: 8.5pt; }
  .print-types thead th { border-bottom: 0.6pt solid #9aa3af; }
}
