:root {
  --radius: 10px;
  --font: -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
}

/* Dark theme (default) */
[data-theme="dark"], :root {
  --bg: #0f1117;
  --surface: #1a1d27;
  --surface2: #22263a;
  --border: #2e3347;
  --text: #e8eaf0;
  --text-muted: #7c82a0;
  --blue: #4f8ef7;
  --red: #f7564f;
  --green: #4fca8f;
  --orange: #f7a94f;
}

/* Light theme */
[data-theme="light"] {
  --bg: #f5f6f8;
  --surface: #ffffff;
  --surface2: #eef0f4;
  --border: #d8dbe5;
  --text: #1a1d27;
  --text-muted: #6b7085;
  --blue: #3b7de8;
  --red: #e5453e;
  --green: #2da872;
  --orange: #e89320;
}

* { box-sizing: border-box; margin: 0; padding: 0; }

body {
  background: var(--bg);
  color: var(--text);
  font-family: var(--font);
  font-size: 14px;
  line-height: 1.5;
  min-height: 100vh;
}

.hidden { display: none !important; }

/* ── Login ─────────────────────────────────────────── */
#passcode-screen {
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: 100vh;
}
.passcode-inputs {
  display: flex;
  gap: 10px;
  justify-content: center;
  margin-bottom: 24px;
}
.passcode-digit {
  width: 48px;
  height: 56px;
  text-align: center;
  font-size: 1.5rem;
  font-weight: 700;
  background: var(--surface2);
  border: 2px solid var(--border);
  border-radius: 10px;
  color: var(--text);
  outline: none;
  transition: border-color 0.15s;
  -webkit-appearance: none;
}
.passcode-digit:focus {
  border-color: var(--blue);
}
.passcode-digit.error {
  border-color: #ef4444;
  animation: shake 0.4s;
}
@keyframes shake {
  0%, 100% { transform: translateX(0); }
  25% { transform: translateX(-6px); }
  75% { transform: translateX(6px); }
}

#login-screen {
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: 100vh;
}
.login-box {
  text-align: center;
  padding: 48px;
}
.login-logo {
  width: 80px; height: 80px;
  background: linear-gradient(135deg, var(--blue), #6366f1);
  border-radius: 20px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 2rem;
  font-weight: 800;
  margin: 0 auto 24px;
  color: white;
  letter-spacing: -1px;
}
.login-box h1 { font-size: 2.5rem; margin-bottom: 8px; }
.login-box p  { color: var(--text-muted); margin-bottom: 32px; font-size: 1rem; }
.login-error {
  background: rgba(239,68,68,0.12);
  border: 1px solid rgba(239,68,68,0.3);
  color: #f87171;
  padding: 12px 20px;
  border-radius: 8px;
  margin-bottom: 24px;
  font-size: 0.875rem;
}

.btn-strava {
  display: inline-block;
  background: #fc4c02;
  color: white;
  padding: 14px 32px;
  border-radius: 6px;
  text-decoration: none;
  font-weight: 600;
  font-size: 1rem;
  transition: opacity 0.15s;
}
.btn-strava:hover { opacity: 0.85; }

/* ── Header ─────────────────────────────────────────── */
header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 24px;
  height: 56px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  position: sticky;
  top: 0;
  z-index: 100;
}
.logo { font-weight: 700; font-size: 1.1rem; color: var(--blue); }
nav { display: flex; gap: 4px; }

.nav-btn {
  background: none;
  border: none;
  color: var(--text-muted);
  padding: 6px 14px;
  border-radius: 6px;
  cursor: pointer;
  font-size: 0.875rem;
  font-weight: 500;
  transition: all 0.15s;
}
.nav-btn:hover, .nav-btn.active {
  background: var(--surface2);
  color: var(--text);
}
.nav-btn.active { color: var(--blue); }

.header-right {
  display: flex;
  align-items: center;
  gap: 12px;
}
.avatar {
  width: 32px; height: 32px;
  border-radius: 50%;
  object-fit: cover;
}
.sync-status {
  font-size: 0.65rem;
  color: var(--text-muted);
}
#user-name { font-size: 0.875rem; color: var(--text-muted); }

/* ── Buttons ─────────────────────────────────────────── */
button, .btn-primary, .btn-secondary, .btn-ghost {
  font-family: var(--font);
  font-size: 0.875rem;
  cursor: pointer;
  border-radius: 6px;
  border: none;
  transition: all 0.15s;
}
.btn-primary {
  background: var(--blue);
  color: white;
  padding: 10px 20px;
  font-weight: 600;
}
.btn-primary:hover { opacity: 0.85; }
.btn-secondary {
  background: var(--surface2);
  color: var(--text);
  padding: 6px 14px;
  border: 1px solid var(--border);
}
.btn-secondary:hover { border-color: var(--blue); color: var(--blue); }
.btn-ghost {
  background: none;
  color: var(--text-muted);
  padding: 6px 12px;
}
.btn-ghost:hover { color: var(--text); }

/* ── Views ─────────────────────────────────────────── */
.view {
  max-width: 1100px;
  margin: 0 auto;
  padding: 32px 24px;
}

/* ── Summary cards ─────────────────────────────────── */
.summary-cards {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 16px;
  margin-bottom: 32px;
}
@media (max-width: 700px) {
  .summary-cards { grid-template-columns: repeat(2, 1fr); }
}
.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 20px;
}
.card-label { font-size: 0.75rem; color: var(--text-muted); text-transform: uppercase; letter-spacing: 0.05em; }
.card-value { font-size: 2rem; font-weight: 700; margin-top: 4px; }

/* ── Chart ─────────────────────────────────────────── */
.chart-container {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 24px;
  margin-bottom: 32px;
}
.chart-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 20px;
}
.chart-header h2 { font-size: 1rem; font-weight: 600; }
.chart-controls { display: flex; gap: 4px; }
.range-btn {
  background: none;
  border: 1px solid var(--border);
  color: var(--text-muted);
  padding: 4px 10px;
  border-radius: 4px;
  font-size: 0.75rem;
  cursor: pointer;
}
.range-btn.active, .range-btn:hover {
  border-color: var(--blue);
  color: var(--blue);
  background: rgba(79,142,247,0.08);
}

/* ── Recent activities ─────────────────────────────── */
.recent-activities h2 { font-size: 1rem; font-weight: 600; margin-bottom: 16px; }
.activity-row {
  display: grid;
  grid-template-columns: 36px 1fr auto auto auto;
  align-items: center;
  gap: 12px;
  padding: 12px 16px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 8px;
  margin-bottom: 8px;
}
.activity-icon { font-size: 1.25rem; text-align: center; }
.activity-name { font-weight: 500; }
.activity-meta { font-size: 0.75rem; color: var(--text-muted); margin-top: 2px; }
.activity-stat { font-size: 0.875rem; color: var(--text-muted); }
.activity-tss { font-weight: 600; color: var(--orange); min-width: 60px; text-align: right; }

/* ── Calendar (continuous scroll) ─────────────────── */
#view-calendar {
  max-width: 1200px;
}
.calendar-scroll-header {
  display: grid;
  grid-template-columns: 70px 1fr 70px;
  gap: 1px;
  margin-bottom: 2px;
}
.cal-col-label { }
.cal-day-headers {
  display: grid;
  grid-template-columns: repeat(7, 1fr);
  gap: 1px;
  text-align: center;
  font-size: 0.7rem;
  text-transform: uppercase;
  color: var(--text-muted);
  letter-spacing: 0.05em;
  padding: 8px 0;
}
.cal-week-summary-header {
  text-align: center;
  font-size: 0.7rem;
  text-transform: uppercase;
  color: var(--text-muted);
  letter-spacing: 0.05em;
  padding: 8px 0;
}
.calendar-scroll {
  max-height: calc(100vh - 160px);
  overflow-y: auto;
  border: 1px solid var(--border);
  border-radius: var(--radius);
}
.cal-week-row {
  display: grid;
  grid-template-columns: 70px 1fr 70px;
  gap: 1px;
  background: var(--border);
  border-bottom: 1px solid var(--border);
}
.cal-month-label {
  background: var(--surface);
  display: flex;
  align-items: flex-start;
  justify-content: center;
  padding: 8px 4px;
  font-size: 0.7rem;
  font-weight: 700;
  color: var(--text-muted);
  text-transform: uppercase;
  letter-spacing: 0.05em;
  writing-mode: vertical-lr;
  text-orientation: mixed;
  transform: rotate(180deg);
}
.cal-month-label.has-label {
  color: var(--blue);
}
.cal-week-days {
  display: grid;
  grid-template-columns: repeat(7, 1fr);
  gap: 1px;
  background: var(--border);
}
.cal-week-total {
  background: var(--surface);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: flex-start;
  padding: 8px 4px;
  font-size: 0.65rem;
  gap: 4px;
}
.cal-week-tss {
  font-weight: 700;
  color: var(--orange);
  font-size: 0.8rem;
}
.cal-week-ctl { color: var(--green); }
.cal-week-tsb { color: var(--blue); }
.cal-day {
  background: var(--surface);
  min-height: 110px;
  padding: 6px;
  vertical-align: top;
  overflow: hidden;
}
.cal-day.empty { background: var(--bg); }
.cal-day.today { background: rgba(79,142,247,0.06); }
.cal-day-num {
  font-size: 0.7rem;
  color: var(--text-muted);
  margin-bottom: 4px;
}
.cal-day.today .cal-day-num { color: var(--blue); font-weight: 700; }

/* Activity card */
.cal-workout {
  font-size: 0.65rem;
  background: var(--surface2);
  border-radius: 5px;
  padding: 5px 6px;
  margin-bottom: 4px;
  overflow: hidden;
  border-left: 3px solid var(--blue);
}
.cal-workout-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 2px;
  margin-bottom: 2px;
  font-weight: 600;
  color: var(--text);
}
.cal-workout-duration { white-space: nowrap; }
.cal-workout-dist { white-space: nowrap; }
.cal-workout-stats {
  display: flex;
  gap: 6px;
  color: var(--text-muted);
  font-size: 0.6rem;
  margin-bottom: 3px;
}
.cal-workout-stats .watts { color: var(--orange); }
.cal-workout-stats .hr { color: var(--red); }
.cal-workout-stats .tss-val { color: var(--green); font-weight: 600; }
.cal-workout-name {
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  color: var(--text-muted);
  font-size: 0.58rem;
  margin-top: 2px;
}
/* Mini power sparkline */
.cal-sparkline {
  width: 100%;
  height: 16px;
  margin-top: 3px;
  border-radius: 2px;
  overflow: hidden;
}

/* ── Coaching ─────────────────────────────────────── */
.coaching-container { max-width: 700px; }
.coaching-container h2 { margin-bottom: 8px; }
.coaching-subtitle { color: var(--text-muted); margin-bottom: 24px; }
.coaching-output {
  margin-top: 24px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 24px;
  white-space: pre-wrap;
  line-height: 1.7;
  font-size: 0.9rem;
}

/* ── Power Curve ──────────────────────────────────── */
.power-container { max-width: 1100px; }
.power-container .chart-container { height: 500px; }
.power-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 20px;
  flex-wrap: wrap;
  gap: 12px;
}
.power-header h2 { margin: 0; }
.power-controls {
  display: flex;
  align-items: center;
  gap: 10px;
  font-size: 0.875rem;
}
.power-controls label { color: var(--text-muted); }
.power-controls select {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 6px 12px;
  color: var(--text);
  font-size: 0.875rem;
  font-family: var(--font);
  cursor: pointer;
}
.power-hint {
  color: var(--text-muted);
  font-size: 0.85rem;
  margin-top: 16px;
}

/* ── Settings ─────────────────────────────────────── */
.settings-container { max-width: 480px; }
.settings-container h2 { margin-bottom: 8px; }
.settings-container > p { color: var(--text-muted); margin-bottom: 28px; }
#settings-form { display: flex; flex-direction: column; gap: 20px; }
#settings-form label {
  display: flex;
  flex-direction: column;
  gap: 6px;
  font-size: 0.875rem;
  color: var(--text-muted);
}
#settings-form input {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 10px 14px;
  color: var(--text);
  font-size: 1rem;
  font-family: var(--font);
  outline: none;
}
#settings-form input:focus { border-color: var(--blue); }
.saved-msg { color: var(--green); font-size: 0.875rem; }

/* ── Toast ─────────────────────────────────────────── */
/* ── Activity Detail Modal ────────────────────────── */
.modal {
  position: fixed;
  top: 0; left: 0; right: 0; bottom: 0;
  background: rgba(0, 0, 0, 0.7);
  z-index: 1000;
  display: flex;
  align-items: flex-start;
  justify-content: center;
  overflow-y: auto;
  padding: 40px 20px;
}
.modal-content {
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 12px;
  width: 100%;
  max-width: 1100px;
  padding: 24px;
  position: relative;
}
.modal-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 16px;
}
.modal-header h2 {
  font-size: 1.2rem;
  font-weight: 700;
}
.modal-close-btn {
  font-size: 1.5rem;
  width: 36px;
  height: 36px;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: 50%;
}
.modal-close-btn:hover {
  background: var(--surface2);
}
.activity-summary-bar {
  display: flex;
  flex-wrap: wrap;
  gap: 16px;
  margin-bottom: 20px;
  padding: 12px 16px;
  background: var(--surface);
  border-radius: 8px;
}
.summary-item {
  display: flex;
  flex-direction: column;
  gap: 2px;
}
.summary-item .label {
  font-size: 0.6rem;
  text-transform: uppercase;
  color: var(--text-muted);
  letter-spacing: 0.05em;
}
.summary-item .val {
  font-size: 0.95rem;
  font-weight: 600;
}
.map-peaks-row {
  display: grid;
  grid-template-columns: 1fr 280px;
  gap: 16px;
  margin-bottom: 20px;
}
.activity-map-container {
  min-width: 0;
}
.peaks-table-container {
  margin-bottom: 0;
}
.peaks-table-container h3 {
  font-size: 0.85rem;
  font-weight: 600;
  margin-bottom: 8px;
}
.peaks-table {
  width: 100%;
  border-collapse: collapse;
  font-size: 0.8rem;
}
.peaks-table th {
  text-align: left;
  padding: 6px 12px;
  font-size: 0.65rem;
  text-transform: uppercase;
  color: var(--text-muted);
  border-bottom: 1px solid var(--border);
}
.peaks-table td {
  padding: 6px 12px;
  border-bottom: 1px solid var(--border);
}
.peak-row:hover {
  background: var(--surface2);
}
.peak-row.active {
  background: var(--surface2);
  border-left: 3px solid #ef4444;
}
.laps-table-container {
  margin-bottom: 20px;
}
.laps-table-container h3 {
  font-size: 0.85rem;
  font-weight: 600;
  margin-bottom: 8px;
}
.laps-scroll {
  max-height: 185px;
  overflow-y: auto;
}
.laps-table {
  width: 100%;
  border-collapse: collapse;
  font-size: 0.75rem;
}
.laps-table thead {
  position: sticky;
  top: 0;
  background: var(--bg);
  z-index: 1;
}
.laps-table th {
  text-align: left;
  padding: 6px 10px;
  font-size: 0.6rem;
  text-transform: uppercase;
  color: var(--text-muted);
  border-bottom: 1px solid var(--border);
}
.laps-table td {
  padding: 6px 10px;
  border-bottom: 1px solid var(--border);
}
.lap-row {
  cursor: pointer;
}
.lap-row:hover {
  background: var(--surface2);
}
.lap-row.active {
  background: var(--surface2);
  border-left: 3px solid #ef4444;
}

.stream-chart-controls {
  display: flex;
  align-items: center;
  gap: 10px;
  margin-bottom: 10px;
  font-size: 0.75rem;
  color: var(--text-muted);
}
.smoothing-btns {
  display: flex;
  gap: 4px;
}
.smoothing-btn {
  padding: 4px 10px;
  font-size: 0.7rem;
  border: 1px solid var(--border);
  border-radius: 4px;
  background: var(--surface);
  color: var(--text);
  cursor: pointer;
}
.smoothing-btn.active {
  background: var(--blue);
  border-color: var(--blue);
  color: #fff;
}
.stream-chart-container {
  height: 300px;
  margin-bottom: 8px;
}
.selection-stats {
  display: flex;
  flex-wrap: wrap;
  gap: 16px;
  padding: 10px 16px;
  background: var(--surface2);
  border-radius: 8px;
  border: 1px solid var(--blue);
  margin-bottom: 16px;
}
.sel-stat {
  display: flex;
  flex-direction: column;
  gap: 2px;
}
.sel-stat .label {
  font-size: 0.6rem;
  text-transform: uppercase;
  color: var(--text-muted);
  letter-spacing: 0.05em;
}
.sel-stat .val {
  font-size: 0.9rem;
  font-weight: 600;
}

/* ── Activity Notes ──────────────────────────────── */
.activity-notes-container {
  display: flex;
  flex-direction: column;
}
.activity-notes-container h3 {
  font-size: 0.85rem;
  font-weight: 600;
  margin-bottom: 8px;
}
.activity-notes {
  width: 100%;
  flex: 1;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 8px;
  color: var(--text);
  padding: 10px 12px;
  font-size: 0.8rem;
  font-family: inherit;
  resize: none;
  line-height: 1.5;
}
.activity-notes:focus {
  outline: none;
  border-color: var(--blue);
}
.notes-actions {
  margin-top: 6px;
  display: flex;
  align-items: center;
  gap: 8px;
}
.notes-status {
  font-size: 0.65rem;
  color: var(--text-muted);
}
.color-override-row {
  margin-top: 10px;
  display: flex;
  align-items: center;
  gap: 6px;
}
.color-override-label {
  font-size: 0.7rem;
  color: var(--text-muted);
  margin-right: 4px;
}
.color-btn {
  background: var(--surface2);
  border: 2px solid var(--border);
  border-radius: 6px;
  padding: 3px 10px;
  cursor: pointer;
  font-size: 0.65rem;
  color: var(--text-muted);
  display: flex;
  align-items: center;
  gap: 4px;
  transition: border-color 0.15s;
}
.color-btn:hover { border-color: var(--text-muted); }
.color-btn.active { border-color: var(--blue); color: var(--text); }
.color-dot {
  display: inline-block;
  width: 10px;
  height: 10px;
  border-radius: 50%;
}
.notes-zones-row {
  display: grid;
  grid-template-columns: 1fr 380px;
  gap: 20px;
  margin-top: 20px;
  align-items: start;
}
.zones-chart-container h3 {
  font-size: 0.85rem;
  font-weight: 600;
  margin-bottom: 10px;
}
.zones-bars {
  display: flex;
  flex-direction: column;
  gap: 5px;
}
.zone-row {
  display: grid;
  grid-template-columns: 90px 1fr 45px;
  align-items: center;
  gap: 8px;
}
.zone-label {
  font-size: 0.65rem;
  text-align: right;
  line-height: 1.2;
}
.zone-name {
  font-weight: 600;
  color: var(--text);
}
.zone-range {
  color: var(--text-muted);
  font-size: 0.55rem;
}
.zone-bar-track {
  height: 14px;
  background: var(--surface2);
  border-radius: 3px;
  overflow: hidden;
}
.zone-bar-fill {
  height: 100%;
  border-radius: 3px;
  min-width: 0;
  transition: width 0.3s ease;
}
.zone-time {
  font-size: 0.6rem;
  color: var(--text-muted);
}

.settings-integrations {
  margin-top: 24px;
  padding-top: 16px;
  border-top: 1px solid var(--border);
}
.settings-integrations h3 {
  font-size: 0.9rem;
  font-weight: 600;
  margin-bottom: 12px;
}

.cal-weight {
  font-size: 0.65rem;
  color: var(--text);
  background: var(--surface2);
  border: 1px solid var(--border);
  border-radius: 4px;
  padding: 2px 6px;
  margin-bottom: 3px;
  cursor: pointer;
  display: block;
  text-align: center;
  font-weight: 600;
  letter-spacing: 0.02em;
}
.cal-weight:hover {
  color: var(--blue);
  border-color: var(--blue);
  background: rgba(79,142,247,0.08);
}

.cal-note-icon {
  font-size: 0.6rem;
  margin-left: 2px;
}

.toast {
  position: fixed;
  bottom: 24px;
  left: 50%;
  transform: translateX(-50%);
  background: var(--surface2);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 12px 24px;
  font-size: 0.875rem;
  z-index: 999;
  transition: opacity 0.3s;
}

/* ── Mobile Responsive ──────────────────────────────── */
@media (max-width: 768px) {
  header {
    flex-wrap: wrap;
    height: auto;
    padding: 8px 12px;
    gap: 4px;
  }
  .header-left { order: 1; }
  nav {
    order: 3;
    width: 100%;
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
    padding: 4px 0;
  }
  .header-right {
    order: 2;
    gap: 6px;
  }
  .header-right .sync-status { display: none; }
  .nav-btn { font-size: 0.75rem; padding: 5px 10px; white-space: nowrap; }

  .summary-cards {
    grid-template-columns: repeat(2, 1fr);
    gap: 8px;
    padding: 12px;
  }
  .card { padding: 12px; }
  .card-value { font-size: 1.5rem; }

  .chart-container { padding: 12px; margin: 8px; }

  .recent-activities { padding: 12px; }

  /* Calendar */
  .calendar-scroll-header { display: none; }
  .cal-week-row {
    flex-direction: column;
    padding: 0;
  }
  .cal-month-label {
    width: 100%;
    text-align: left;
    padding: 8px 12px;
  }
  .cal-week-days {
    display: flex;
    flex-direction: column;
    gap: 2px;
  }
  .cal-day {
    min-height: auto;
    padding: 8px 12px;
    border-right: none;
    border-bottom: 1px solid var(--border);
  }
  .cal-day.empty { display: none; }
  .cal-day-num {
    font-size: 0.75rem;
  }
  .cal-week-total {
    width: 100%;
    border-left: none;
    border-top: 1px solid var(--border);
    padding: 8px 12px;
    flex-direction: row;
    gap: 12px;
    justify-content: center;
  }

  /* Activity modal */
  .modal-content {
    width: 100%;
    max-width: 100%;
    height: 100vh;
    max-height: 100vh;
    border-radius: 0;
    margin: 0;
  }
  .map-peaks-row {
    grid-template-columns: 1fr;
  }
  .notes-zones-row {
    grid-template-columns: 1fr;
  }
  .activity-summary-bar {
    flex-wrap: wrap;
    gap: 8px;
  }
  .laps-scroll { overflow-x: auto; }

  /* Power curve */
  .power-container { padding: 12px; }
  .power-header { flex-direction: column; gap: 8px; }

  /* Settings */
  .settings-container { padding: 16px; }

  /* Passcode */
  .passcode-digit { width: 40px; height: 48px; font-size: 1.2rem; }
  .passcode-inputs { gap: 8px; }
}
