:root {
  --bg: #0E1113;
  --surface: #16191C;
  --surface-raised: #1B1F22;
  --line: #262B2F;
  --text: #ECEFF0;
  --text-muted: #8B939A;
  --accent: #C6F135;
  --accent-dim: #7C8A2A;

  --z1: #4A5560; /* recovery */
  --z2: #3E7CB1; /* endurance */
  --z3: #4FA65B; /* tempo */
  --z4: #E0B03E; /* threshold */
  --z5: #E0743E; /* vo2 max */
  --z6: #C6453E; /* anaerobic */

  --font-display: "Oswald", sans-serif;
  --font-body: "Inter", sans-serif;
  --font-mono: "JetBrains Mono", monospace;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
  background: var(--bg);
  color: var(--text);
  font-family: var(--font-body);
  -webkit-font-smoothing: antialiased;
  height: 100%;
}

.hidden { display: none !important; }

/* ---------- Passcode screen ---------- */
.passcode-screen {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 24px;
}
.passcode-box {
  width: 100%;
  max-width: 320px;
  text-align: center;
}
.passcode-label {
  font-family: var(--font-display);
  letter-spacing: 0.12em;
  font-size: 14px;
  color: var(--text-muted);
  margin-bottom: 24px;
}
#passcode-input {
  width: 100%;
  background: var(--surface);
  border: 1px solid var(--line);
  color: var(--text);
  font-family: var(--font-mono);
  font-size: 18px;
  padding: 14px 16px;
  text-align: center;
  letter-spacing: 0.2em;
  border-radius: 2px;
  margin-bottom: 12px;
}
#passcode-input:focus { outline: 2px solid var(--accent); }
#passcode-submit {
  width: 100%;
  background: var(--accent);
  color: #0E1113;
  border: none;
  font-family: var(--font-display);
  font-weight: 600;
  letter-spacing: 0.08em;
  padding: 14px;
  border-radius: 2px;
  cursor: pointer;
}
.passcode-error { color: var(--z6); font-size: 13px; margin-top: 10px; min-height: 16px; }

/* ---------- App shell ---------- */
.app {
  max-width: 480px;
  margin: 0 auto;
  min-height: 100vh;
  padding-bottom: 120px;
  position: relative;
}

.topbar {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: calc(env(safe-area-inset-top) + 16px) 20px 12px;
  position: sticky;
  top: 0;
  background: linear-gradient(to bottom, var(--bg) 80%, transparent);
  z-index: 5;
}
.brand {
  font-family: var(--font-display);
  font-size: 18px;
  font-weight: 600;
  letter-spacing: 0.04em;
}
.brand span { color: var(--accent); }
.conn-status {
  font-family: var(--font-mono);
  font-size: 11px;
  color: var(--text-muted);
  letter-spacing: 0.05em;
  text-transform: uppercase;
}
.conn-status.ok { color: var(--z3); }
.conn-status.error { color: var(--z6); }

.main { padding: 8px 16px 0; }

.card {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 3px;
}

/* ---------- Hero / FTP gauge ---------- */
.hero {
  padding: 24px 20px;
  display: flex;
  flex-direction: column;
  align-items: center;
  margin-bottom: 20px;
}
.hero-gauge {
  position: relative;
  width: 220px;
  height: 220px;
}
.ftp-ring { width: 100%; height: 100%; }
.hero-center {
  position: absolute;
  top: 0; left: 0; right: 0; bottom: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
}
.ftp-value {
  font-family: var(--font-display);
  font-size: 56px;
  font-weight: 700;
  line-height: 1;
  color: var(--accent);
}
.ftp-unit {
  font-family: var(--font-mono);
  font-size: 10px;
  letter-spacing: 0.1em;
  color: var(--text-muted);
  margin-top: 6px;
}
.hero-meta {
  margin-top: 18px;
  text-align: center;
  width: 100%;
}
.ftp-confidence {
  font-family: var(--font-mono);
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  color: var(--text-muted);
  margin-bottom: 6px;
}
.ftp-note {
  font-size: 13px;
  color: var(--text-muted);
  line-height: 1.4;
  max-width: 320px;
  margin: 0 auto;
}
.zone-legend {
  display: flex;
  justify-content: center;
  gap: 10px;
  margin-top: 16px;
  flex-wrap: wrap;
}
.zone-chip {
  display: flex;
  align-items: center;
  gap: 5px;
  font-family: var(--font-mono);
  font-size: 10px;
  color: var(--text-muted);
}
.zone-dot { width: 8px; height: 8px; border-radius: 50%; display: inline-block; }

/* ---------- Overview ---------- */
.section-label {
  font-family: var(--font-mono);
  font-size: 11px;
  letter-spacing: 0.12em;
  color: var(--text-muted);
  margin: 0 4px 10px;
}
.overview { margin-bottom: 24px; }
.overview-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 10px;
}
.stat-card {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 3px;
  padding: 14px 16px;
}
.stat-card-label {
  font-family: var(--font-mono);
  font-size: 10px;
  letter-spacing: 0.08em;
  color: var(--text-muted);
  text-transform: uppercase;
  margin-bottom: 8px;
}
.stat-row {
  display: flex;
  justify-content: space-between;
  font-family: var(--font-mono);
  font-size: 13px;
  padding: 3px 0;
}
.stat-row .v { color: var(--text); }
.stat-row .k { color: var(--text-muted); }

/* ---------- Weekly chart ---------- */
.weekly {
  padding: 14px 16px 16px;
  margin-bottom: 20px;
}
.weekly-svg {
  width: 100%;
  height: auto;
  margin-top: 4px;
}
.weekly-bar { fill: var(--accent); }
.weekly-bar.empty { fill: var(--line); }
.weekly-avg-line { stroke: var(--text-muted); stroke-width: 1; stroke-dasharray: 4 3; }
.weekly-avg-label {
  fill: var(--text-muted);
  font-family: var(--font-mono);
  font-size: 8px;
}
.weekly-axis { stroke: var(--line); stroke-width: 1; }
.weekly-gridline { stroke: var(--line); stroke-width: 0.5; }
.weekly-tick {
  fill: var(--text-muted);
  font-family: var(--font-mono);
  font-size: 7px;
}

/* ---------- Recent rides ---------- */
.recent { margin-bottom: 12px; }
.recent-list {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 3px;
  overflow: hidden;
}
.ride-row {
  padding: 12px 16px;
  border-bottom: 1px solid var(--line);
  cursor: pointer;
  transition: background 0.15s ease;
}
.ride-row:last-child { border-bottom: none; }
.ride-row:active, .ride-row:hover { background: var(--surface-raised); }
.ride-row-top {
  display: flex;
  justify-content: space-between;
  align-items: center;
}
.ride-row-sub {
  display: flex;
  gap: 12px;
  margin-top: 6px;
  font-family: var(--font-mono);
  font-size: 11px;
  color: var(--text-muted);
}
.ride-date {
  font-family: var(--font-mono);
  font-size: 11px;
  color: var(--text-muted);
  width: 64px;
  flex-shrink: 0;
}
.ride-name { flex: 1; font-size: 13px; padding: 0 10px; }
.ride-power {
  font-family: var(--font-mono);
  font-size: 13px;
  color: var(--accent);
  text-align: right;
  width: 70px;
  flex-shrink: 0;
}
.empty-state {
  padding: 24px 16px;
  color: var(--text-muted);
  font-size: 13px;
  text-align: center;
}

/* ---------- Connect banner ---------- */
.connect-banner {
  position: fixed;
  top: 0; left: 0; right: 0;
  background: var(--z5);
  color: #14100A;
  padding: calc(env(safe-area-inset-top) + 10px) 16px 10px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  font-size: 13px;
  z-index: 20;
}
.connect-btn {
  background: #14100A;
  color: var(--z5);
  padding: 6px 12px;
  border-radius: 2px;
  text-decoration: none;
  font-family: var(--font-mono);
  font-size: 11px;
  font-weight: 700;
}

/* ---------- Ride detail overlay ---------- */
.ride-detail {
  position: fixed;
  inset: 0;
  background: var(--bg);
  z-index: 30;
  max-width: 480px;
  margin: 0 auto;
  overflow-y: auto;
}
.ride-detail-header {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: calc(env(safe-area-inset-top) + 16px) 16px 12px;
  position: sticky;
  top: 0;
  background: var(--bg);
  z-index: 2;
}
.ride-detail-close {
  background: none;
  border: none;
  color: var(--accent);
  font-family: var(--font-mono);
  font-size: 13px;
  cursor: pointer;
  padding: 4px 0;
}
.ride-detail-title {
  font-family: var(--font-display);
  font-size: 16px;
  font-weight: 600;
}
.ride-detail-map-wrap {
  margin: 0 16px 16px;
  border-radius: 3px;
  overflow: hidden;
  border: 1px solid var(--line);
}
.ride-map {
  width: 100%;
  height: 240px;
  background: var(--surface);
}
.ride-detail-stats {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 10px;
  padding: 0 16px 24px;
}
.ride-stat-card {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 3px;
  padding: 14px 16px;
  text-align: center;
}
.ride-stat-value {
  font-family: var(--font-mono);
  font-size: 20px;
  color: var(--accent);
}
.ride-stat-label {
  font-family: var(--font-mono);
  font-size: 10px;
  color: var(--text-muted);
  text-transform: uppercase;
  letter-spacing: 0.06em;
  margin-top: 4px;
}

/* ---------- Chat drawer ---------- */
.chat-drawer {
  position: fixed;
  bottom: 0; left: 0; right: 0;
  max-width: 480px;
  margin: 0 auto;
  background: var(--surface-raised);
  border-top: 1px solid var(--line);
  border-radius: 12px 12px 0 0;
  z-index: 15;
  transition: transform 0.25s ease;
  padding-bottom: env(safe-area-inset-bottom);
}
.chat-toggle {
  width: 100%;
  background: none;
  border: none;
  color: var(--accent);
  font-family: var(--font-mono);
  font-size: 11px;
  letter-spacing: 0.08em;
  padding: 14px;
  cursor: pointer;
}
.chat-body {
  height: 0;
  overflow: hidden;
  transition: height 0.25s ease;
  display: flex;
  flex-direction: column;
}
.chat-drawer.open .chat-body { height: 360px; }
.chat-log {
  flex: 1;
  overflow-y: auto;
  padding: 0 16px;
  display: flex;
  flex-direction: column;
  gap: 8px;
}
.chat-msg {
  font-size: 13px;
  line-height: 1.4;
  padding: 8px 12px;
  border-radius: 8px;
  max-width: 85%;
}
.chat-msg-assistant {
  background: var(--surface);
  color: var(--text);
  align-self: flex-start;
}
.chat-msg-user {
  background: var(--accent-dim);
  color: var(--text);
  align-self: flex-end;
}
.chat-form {
  display: flex;
  gap: 8px;
  padding: 12px 16px;
  border-top: 1px solid var(--line);
}
#chat-input {
  flex: 1;
  background: var(--surface);
  border: 1px solid var(--line);
  color: var(--text);
  padding: 10px 12px;
  border-radius: 6px;
  font-size: 13px;
}
.chat-form button {
  background: var(--accent);
  color: #0E1113;
  border: none;
  padding: 10px 16px;
  border-radius: 6px;
  font-weight: 600;
  font-size: 13px;
  cursor: pointer;
}

@media (prefers-reduced-motion: reduce) {
  .chat-drawer, .chat-body { transition: none; }
}
