:root {
  --green: #1D9E75;
  --amber: #BA7517;
  --red: #E24B4A;
  --purple: #534AB7;
  --blue: #378ADD;
  --bg: #0f1117;
  --surface: #181c27;
  --surface-2: #1f2433;
  --border: #2a3045;
  --text: #e8eaf0;
  --text-dim: #9099b0;
  --text-faint: #6b7288;
  --font-mono: ui-monospace, "SF Mono", Menlo, Consolas, monospace;
  --font-sans: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  --radius: 14px;
  --radius-sm: 8px;
}

* { box-sizing: border-box; }

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

body {
  padding-bottom: env(safe-area-inset-bottom);
}

button, input, select {
  font-family: inherit;
  color: inherit;
}

#app {
  min-height: 100vh;
  display: flex;
  flex-direction: column;
}

/* Header */
#app-header {
  position: sticky;
  top: 0;
  z-index: 20;
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 10px 16px;
  padding-top: max(10px, env(safe-area-inset-top));
  background: rgba(15,17,23,0.92);
  backdrop-filter: blur(8px);
  border-bottom: 1px solid var(--border);
}

.brand {
  font-weight: 700;
  font-size: 15px;
  letter-spacing: 0.02em;
  color: var(--purple);
  margin-right: auto;
}

#main-nav {
  display: flex;
  gap: 4px;
  background: var(--surface);
  border-radius: var(--radius-sm);
  padding: 3px;
}

.nav-btn {
  background: transparent;
  border: none;
  color: var(--text-dim);
  padding: 6px 12px;
  border-radius: 6px;
  font-size: 13px;
  cursor: pointer;
}

.nav-btn.active {
  background: var(--surface-2);
  color: var(--text);
}

.header-actions {
  display: flex;
  align-items: center;
  gap: 6px;
}

.sync-status {
  font-size: 11px;
  color: var(--text-faint);
  margin-right: 4px;
  white-space: nowrap;
}

.icon-btn {
  background: var(--surface);
  border: 1px solid var(--border);
  color: var(--text);
  width: 34px;
  height: 34px;
  border-radius: 50%;
  font-size: 16px;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
}

.icon-btn.spinning { animation: spin 0.8s linear infinite; }
@keyframes spin { from { transform: rotate(0deg); } to { transform: rotate(360deg); } }

/* Screens */
#screen-container {
  flex: 1;
  padding: 16px;
  padding-bottom: 96px;
  max-width: 1200px;
  width: 100%;
  margin: 0 auto;
}

.screen { display: none; }
.screen.active { display: block; }

/* Cards grid */
.metric-cards {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 10px;
  margin-bottom: 16px;
}

@media (min-width: 700px) {
  .metric-cards { grid-template-columns: repeat(4, 1fr); }
}

.metric-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 12px 14px;
}

.metric-card .metric-label {
  font-size: 11px;
  color: var(--text-dim);
  text-transform: uppercase;
  letter-spacing: 0.04em;
  margin-bottom: 6px;
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.metric-card .metric-value {
  font-family: var(--font-mono);
  font-size: 26px;
  font-weight: 600;
  line-height: 1.1;
}

.metric-card .metric-value .unit {
  font-size: 13px;
  color: var(--text-dim);
  font-weight: 400;
  margin-left: 2px;
}

.metric-card .metric-sub {
  font-size: 11px;
  color: var(--text-faint);
  margin-top: 4px;
  font-family: var(--font-mono);
}

.badge {
  font-size: 10px;
  font-weight: 700;
  padding: 2px 7px;
  border-radius: 100px;
  text-transform: uppercase;
  letter-spacing: 0.03em;
}

.badge-green { background: rgba(29,158,117,0.18); color: var(--green); }
.badge-amber { background: rgba(186,117,23,0.18); color: var(--amber); }
.badge-red { background: rgba(226,75,74,0.18); color: var(--red); }

.spark-wrap { position: relative; margin-top: 8px; height: 26px; width: 100%; }

/* Training decision banner */
.decision-card {
  border-radius: var(--radius);
  padding: 22px 20px;
  margin-bottom: 16px;
  border: 1px solid var(--border);
  position: relative;
  overflow: hidden;
}

.decision-card.green { background: linear-gradient(135deg, rgba(29,158,117,0.35), rgba(29,158,117,0.12)); border-color: rgba(29,158,117,0.5); }
.decision-card.amber { background: linear-gradient(135deg, rgba(186,117,23,0.35), rgba(186,117,23,0.12)); border-color: rgba(186,117,23,0.5); }
.decision-card.red { background: linear-gradient(135deg, rgba(226,75,74,0.35), rgba(226,75,74,0.12)); border-color: rgba(226,75,74,0.5); }

.decision-eyebrow {
  font-size: 12px;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  opacity: 0.85;
  margin-bottom: 8px;
  font-weight: 600;
}

.decision-headline {
  font-size: 22px;
  font-weight: 700;
  line-height: 1.3;
  margin-bottom: 8px;
}

@media (min-width: 500px) {
  .decision-headline { font-size: 26px; }
}

.decision-sub {
  font-size: 13px;
  font-family: var(--font-mono);
  opacity: 0.85;
}

.decision-card.green .decision-eyebrow, .decision-card.green .decision-headline { color: #b6f2dc; }
.decision-card.amber .decision-eyebrow, .decision-card.amber .decision-headline { color: #ffdca8; }
.decision-card.red .decision-eyebrow, .decision-card.red .decision-headline { color: #ffc9c8; }
.decision-card .decision-sub { color: rgba(255,255,255,0.75); }

.decision-skeleton {
  height: 116px;
  border-radius: var(--radius);
  background: var(--surface);
  border: 1px solid var(--border);
  margin-bottom: 16px;
}

/* Charts row */
.charts-row {
  display: grid;
  grid-template-columns: 1fr;
  gap: 12px;
  margin-bottom: 16px;
}

@media (min-width: 900px) {
  .charts-row { grid-template-columns: 1.4fr 1fr; }
}

.chart-panel {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 14px;
}

.chart-panel h3 {
  margin: 0 0 10px 0;
  font-size: 13px;
  color: var(--text-dim);
  font-weight: 600;
}

.chart-legend {
  display: flex;
  gap: 14px;
  margin-top: 8px;
  flex-wrap: wrap;
  font-size: 11px;
  color: var(--text-dim);
}

.chart-legend .legend-item { display: flex; align-items: center; gap: 5px; }
.legend-dot { width: 9px; height: 9px; border-radius: 50%; display: inline-block; }
.legend-line { width: 14px; height: 2px; display: inline-block; }
.legend-dash { width: 14px; height: 0; border-top: 1px dashed; display: inline-block; }

.chart-canvas-wrap { position: relative; height: 220px; }

/* Three-column section */
.columns-3 {
  display: grid;
  grid-template-columns: 1fr;
  gap: 12px;
  margin-bottom: 16px;
}

@media (min-width: 900px) {
  .columns-3 { grid-template-columns: repeat(3, 1fr); }
}

.panel {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 14px;
}

.panel h3 {
  margin: 0 0 10px 0;
  font-size: 13px;
  color: var(--text-dim);
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.03em;
}

.run-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 8px 0;
  border-bottom: 1px solid var(--border);
  font-size: 13px;
}
.run-row:last-child { border-bottom: none; }
.run-name { font-weight: 600; }
.run-date { font-size: 11px; color: var(--text-faint); }
.run-stats { font-family: var(--font-mono); font-size: 12px; color: var(--text-dim); text-align: right; }

.stat-row {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 7px 0;
  font-size: 13px;
  border-bottom: 1px solid var(--border);
}
.stat-row:last-child { border-bottom: none; }
.stat-row .stat-value { font-family: var(--font-mono); color: var(--text); }
.stat-row .stat-value.over { color: var(--green); }
.stat-row .stat-value.under { color: var(--amber); }

.progress-track {
  height: 5px;
  background: var(--surface-2);
  border-radius: 100px;
  overflow: hidden;
  margin-top: 4px;
}
.progress-fill { height: 100%; border-radius: 100px; background: var(--purple); }

/* Goals + alerts */
.columns-2 {
  display: grid;
  grid-template-columns: 1fr;
  gap: 12px;
  margin-bottom: 16px;
}

@media (min-width: 900px) {
  .columns-2 { grid-template-columns: 1fr 1fr; }
}

.goal-row { margin-bottom: 14px; }
.goal-row:last-child { margin-bottom: 0; }
.goal-label { display: flex; justify-content: space-between; font-size: 13px; margin-bottom: 5px; }
.goal-label .goal-name { font-weight: 600; }
.goal-label .goal-nums { font-family: var(--font-mono); color: var(--text-dim); font-size: 12px; }

.alert-item {
  display: flex;
  gap: 10px;
  padding: 10px;
  border-radius: var(--radius-sm);
  margin-bottom: 8px;
  font-size: 13px;
  line-height: 1.4;
  border: 1px solid transparent;
}
.alert-item:last-child { margin-bottom: 0; }
.alert-item.warning { background: rgba(186,117,23,0.12); border-color: rgba(186,117,23,0.3); color: #ffdca8; }
.alert-item.danger { background: rgba(226,75,74,0.12); border-color: rgba(226,75,74,0.3); color: #ffc9c8; }
.alert-item.success { background: rgba(29,158,117,0.12); border-color: rgba(29,158,117,0.3); color: #b6f2dc; }
.alert-icon { font-size: 15px; line-height: 1; }
.empty-state { color: var(--text-faint); font-size: 13px; padding: 8px 0; }

/* Modal */
.modal-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.6);
  display: flex;
  align-items: flex-end;
  justify-content: center;
  z-index: 100;
}

@media (min-width: 600px) {
  .modal-overlay { align-items: center; }
}

.modal-box {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius) var(--radius) 0 0;
  padding: 20px;
  width: 100%;
  max-width: 420px;
  max-height: 90vh;
  overflow-y: auto;
}

@media (min-width: 600px) {
  .modal-box { border-radius: var(--radius); }
}

.modal-box h2 { margin-top: 0; font-size: 16px; }

.field {
  margin-bottom: 14px;
}
.field label {
  display: block;
  font-size: 12px;
  color: var(--text-dim);
  margin-bottom: 6px;
}
.field input, .field select {
  width: 100%;
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 10px 12px;
  font-size: 14px;
}
.field input:focus, .field select:focus { outline: 1px solid var(--purple); }

.btn {
  background: var(--purple);
  color: #fff;
  border: none;
  border-radius: var(--radius-sm);
  padding: 11px 16px;
  font-size: 14px;
  font-weight: 600;
  cursor: pointer;
  width: 100%;
}
.btn.secondary { background: var(--surface-2); color: var(--text); border: 1px solid var(--border); }
.btn.danger { background: var(--red); }
.btn-row { display: flex; gap: 8px; }
.btn-row .btn { width: auto; flex: 1; }

/* Settings screen */
.settings-section {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 16px;
  margin-bottom: 14px;
}
.settings-section h3 { margin: 0 0 12px 0; font-size: 14px; }
.settings-section .desc { font-size: 12px; color: var(--text-faint); margin-bottom: 12px; }

.const-table { width: 100%; font-size: 12px; font-family: var(--font-mono); border-collapse: collapse; }
.const-table td { padding: 5px 0; border-bottom: 1px solid var(--border); }
.const-table td:first-child { color: var(--text-dim); font-family: var(--font-sans); }
.const-table td:last-child { text-align: right; }

.notif-schedule-row {
  display: flex;
  justify-content: space-between;
  padding: 7px 0;
  border-bottom: 1px solid var(--border);
  font-size: 13px;
}
.notif-schedule-row:last-child { border-bottom: none; }
.notif-schedule-row .time { font-family: var(--font-mono); color: var(--purple); }

/* Trends screen */
.heatmap-grid {
  display: grid;
  grid-template-columns: repeat(13, 1fr);
  gap: 3px;
}
.heatmap-cell {
  aspect-ratio: 1;
  border-radius: 3px;
  background: var(--surface-2);
}
.heatmap-cell.good { background: var(--green); }
.heatmap-cell.ok { background: var(--amber); }
.heatmap-cell.hard { background: var(--red); }
.heatmap-cell.no-data { background: var(--surface-2); opacity: 0.4; }

.big-number {
  font-family: var(--font-mono);
  font-size: 44px;
  font-weight: 700;
}
.trend-arrow { font-size: 22px; margin-left: 6px; }
.trend-arrow.up { color: var(--green); }
.trend-arrow.down { color: var(--red); }
.trend-arrow.flat { color: var(--text-dim); }

table.month-table { width: 100%; border-collapse: collapse; font-size: 12px; }
table.month-table th, table.month-table td { text-align: right; padding: 8px 6px; border-bottom: 1px solid var(--border); }
table.month-table th:first-child, table.month-table td:first-child { text-align: left; }
table.month-table th { color: var(--text-dim); font-weight: 600; font-size: 11px; text-transform: uppercase; }
table.month-table td { font-family: var(--font-mono); }

/* Loading / error / offline */
.loading-row { display: flex; align-items: center; justify-content: center; padding: 40px 0; color: var(--text-faint); font-size: 13px; }
.error-banner {
  background: rgba(226,75,74,0.12);
  border: 1px solid rgba(226,75,74,0.3);
  color: #ffc9c8;
  border-radius: var(--radius-sm);
  padding: 10px 12px;
  font-size: 13px;
  margin-bottom: 14px;
}
.offline-banner {
  background: rgba(83,74,183,0.15);
  border: 1px solid rgba(83,74,183,0.35);
  color: #cfcbff;
  border-radius: var(--radius-sm);
  padding: 8px 12px;
  font-size: 12px;
  margin-bottom: 14px;
  text-align: center;
}
.skeleton {
  background: linear-gradient(90deg, var(--surface) 25%, var(--surface-2) 37%, var(--surface) 63%);
  background-size: 400% 100%;
  animation: skeleton-loading 1.4s ease infinite;
  border-radius: var(--radius-sm);
}
@keyframes skeleton-loading {
  0% { background-position: 100% 50%; }
  100% { background-position: 0 50%; }
}

.section-title {
  font-size: 12px;
  color: var(--text-faint);
  text-transform: uppercase;
  letter-spacing: 0.05em;
  margin: 20px 0 10px;
}
.section-title:first-child { margin-top: 0; }
