:root {
  --bg: #fafaf9;
  --bg-card: #ffffff;
  --bg-card-alt: #f5f5f4;
  --text: #0b0b0b;
  --text-secondary: #57534e;
  --text-muted: #a8a29e;
  --border: rgba(0,0,0,0.08);
  --accent: #2563eb;
  --green: #16a34a;
  --yellow: #d97706;
  --red: #dc2626;
  --green-bg: #f0fdf4;
  --yellow-bg: #fffbeb;
  --red-bg: #fef2f2;
  --green-text: #15803d;
  --yellow-text: #92400e;
  --red-text: #991b1b;
  --radius: 12px;
  --radius-sm: 8px;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0c0a09;
    --bg-card: #1c1917;
    --bg-card-alt: #292524;
    --text: #fafaf9;
    --text-secondary: #a8a29e;
    --text-muted: #78716c;
    --border: rgba(255,255,255,0.08);
    --green-bg: #052e16;
    --yellow-bg: #451a03;
    --red-bg: #450a0a;
    --green-text: #4ade80;
    --yellow-text: #fbbf24;
    --red-text: #f87171;
  }
}

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

body {
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', system-ui, sans-serif;
  background: var(--bg);
  color: var(--text);
  line-height: 1.5;
  -webkit-font-smoothing: antialiased;
}

#app {
  max-width: 720px;
  margin: 0 auto;
  padding: 0 16px 80px;
}

.hidden { display: none !important; }

.screen { min-height: 100vh; }

/* Auth */
.auth-card {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  min-height: 100vh;
  gap: 12px;
}

.auth-card h1 {
  font-size: 24px;
  font-weight: 600;
}

.auth-card p { color: var(--text-secondary); font-size: 14px; }

.auth-card input {
  width: 200px;
  padding: 12px 16px;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  font-size: 24px;
  text-align: center;
  letter-spacing: 8px;
  background: var(--bg-card);
  color: var(--text);
  outline: none;
}

.auth-card input:focus { border-color: var(--accent); }

.auth-card button {
  width: 200px;
  padding: 12px;
  background: var(--text);
  color: var(--bg);
  border: none;
  border-radius: var(--radius-sm);
  font-size: 15px;
  font-weight: 500;
  cursor: pointer;
}

.error { color: var(--red); font-size: 13px; }

/* Header */
header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 20px 0 16px;
  position: sticky;
  top: 0;
  background: var(--bg);
  z-index: 10;
}

header h1 { font-size: 22px; font-weight: 600; }

.muted { color: var(--text-muted); font-size: 12px; }

#refresh-btn {
  background: none;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 8px;
  cursor: pointer;
  color: var(--text-secondary);
  display: flex;
  align-items: center;
}

#refresh-btn:hover { background: var(--bg-card-alt); }
#refresh-btn.spinning svg { animation: spin 1s linear infinite; }

@keyframes spin { to { transform: rotate(360deg); } }

/* Tabs */
.tabs {
  display: flex;
  gap: 4px;
  background: var(--bg-card-alt);
  padding: 4px;
  border-radius: var(--radius-sm);
  margin-bottom: 20px;
  position: sticky;
  top: 68px;
  z-index: 10;
}

.tab {
  flex: 1;
  padding: 10px 16px;
  border: none;
  background: none;
  border-radius: 6px;
  font-size: 14px;
  font-weight: 500;
  cursor: pointer;
  color: var(--text-secondary);
  transition: all 0.2s;
}

.tab.active {
  background: var(--bg-card);
  color: var(--text);
  box-shadow: 0 1px 3px rgba(0,0,0,0.08);
}

/* Cards */
.card {
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 16px 20px;
  margin-bottom: 12px;
}

.card-header {
  font-size: 12px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.5px;
  color: var(--text-muted);
  margin-bottom: 12px;
}

/* Metric grid */
.metric-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(140px, 1fr));
  gap: 10px;
}

.metric {
  background: var(--bg-card-alt);
  border-radius: var(--radius-sm);
  padding: 14px 16px;
}

.metric-label {
  font-size: 12px;
  color: var(--text-muted);
  margin-bottom: 4px;
}

.metric-value {
  font-size: 24px;
  font-weight: 600;
  line-height: 1.2;
}

.metric-value .unit {
  font-size: 13px;
  font-weight: 400;
  color: var(--text-secondary);
}

.metric-sub {
  font-size: 12px;
  color: var(--text-secondary);
  margin-top: 2px;
}

/* Recovery badge */
.recovery-badge {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 6px 14px;
  border-radius: 20px;
  font-size: 14px;
  font-weight: 600;
}

.recovery-badge.green { background: var(--green-bg); color: var(--green-text); }
.recovery-badge.yellow { background: var(--yellow-bg); color: var(--yellow-text); }
.recovery-badge.red { background: var(--red-bg); color: var(--red-text); }

/* Recovery dots row */
.recovery-dots {
  display: flex;
  gap: 6px;
  justify-content: space-between;
}

.recovery-dot {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 4px;
  flex: 1;
}

.recovery-dot .dot {
  width: 36px;
  height: 36px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 13px;
  font-weight: 600;
  color: #fff;
}

.recovery-dot .day-label {
  font-size: 11px;
  color: var(--text-muted);
}

.dot.green { background: var(--green); }
.dot.yellow { background: var(--yellow); color: #451a03; }
.dot.red { background: var(--red); }

/* Alert box */
.alert {
  display: flex;
  gap: 10px;
  padding: 12px 16px;
  border-radius: var(--radius-sm);
  margin-bottom: 12px;
  align-items: flex-start;
}

.alert.danger { background: var(--red-bg); color: var(--red-text); }
.alert.warning { background: var(--yellow-bg); color: var(--yellow-text); }
.alert.success { background: var(--green-bg); color: var(--green-text); }

.alert-icon { font-size: 18px; flex-shrink: 0; margin-top: 1px; }
.alert-title { font-weight: 600; font-size: 14px; }
.alert-body { font-size: 13px; opacity: 0.85; margin-top: 2px; }

/* Training card */
.training-card {
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 14px 18px;
  margin-bottom: 8px;
}

.training-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.training-name { font-weight: 600; font-size: 15px; }
.training-date { font-size: 13px; color: var(--text-secondary); }
.training-stats { font-size: 13px; color: var(--text-secondary); margin-top: 4px; }

.strain-badge {
  font-size: 14px;
  font-weight: 600;
  padding: 4px 10px;
  border-radius: 6px;
}

.strain-low { background: var(--green-bg); color: var(--green-text); }
.strain-med { background: var(--yellow-bg); color: var(--yellow-text); }
.strain-high { background: var(--red-bg); color: var(--red-text); }

/* Sleep bar */
.sleep-bar-container { margin: 12px 0; }

.sleep-bar {
  display: flex;
  height: 16px;
  border-radius: 8px;
  overflow: hidden;
}

.sleep-bar .light { background: #93c5fd; }
.sleep-bar .rem { background: #2563eb; }
.sleep-bar .deep { background: #1e3a5f; }

.sleep-legend {
  display: flex;
  gap: 14px;
  margin-top: 8px;
  font-size: 12px;
  color: var(--text-secondary);
  flex-wrap: wrap;
}

.sleep-legend span {
  display: flex;
  align-items: center;
  gap: 4px;
}

.sleep-legend .swatch {
  width: 10px;
  height: 10px;
  border-radius: 2px;
}

/* Chart container */
.chart-container {
  position: relative;
  width: 100%;
  height: 220px;
  margin: 8px 0;
}

.chart-legend {
  display: flex;
  flex-wrap: wrap;
  gap: 14px;
  font-size: 12px;
  color: var(--text-secondary);
  margin-bottom: 8px;
}

.chart-legend span {
  display: flex;
  align-items: center;
  gap: 4px;
}

.chart-legend .swatch {
  width: 10px;
  height: 10px;
  border-radius: 2px;
}

/* Recommendation list */
.recs {
  display: grid;
  gap: 12px;
}

.rec {
  display: flex;
  gap: 12px;
  align-items: flex-start;
}

.rec-num {
  font-size: 18px;
  font-weight: 600;
  color: var(--accent);
  flex-shrink: 0;
  min-width: 24px;
}

.rec-title { font-weight: 600; font-size: 14px; }
.rec-body { font-size: 13px; color: var(--text-secondary); margin-top: 2px; }

/* Meal cards */
.meal-card {
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 14px 18px;
  margin-bottom: 8px;
}

.meal-header {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-bottom: 4px;
}

.meal-icon { font-size: 18px; }
.meal-name { font-weight: 600; font-size: 14px; }
.meal-desc { font-size: 14px; color: var(--text-secondary); }
.meal-macros { font-size: 12px; color: var(--text-muted); margin-top: 4px; }

/* Section title */
.section-title {
  font-size: 12px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.5px;
  color: var(--text-muted);
  margin: 20px 0 10px;
}

/* Loading */
#loading {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 60px 0;
  gap: 16px;
}

#loading p { color: var(--text-secondary); font-size: 14px; }

.spinner {
  width: 32px;
  height: 32px;
  border: 3px solid var(--border);
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
}

/* Error */
#error-banner {
  background: var(--red-bg);
  color: var(--red-text);
  padding: 14px 18px;
  border-radius: var(--radius-sm);
  margin-bottom: 16px;
  display: flex;
  justify-content: space-between;
  align-items: center;
}

#error-banner button {
  background: none;
  border: none;
  color: var(--red-text);
  font-weight: 600;
  cursor: pointer;
  font-size: 13px;
}

/* Goal assessment */
.goal-card {
  padding: 16px 20px;
  border-radius: var(--radius);
  margin-bottom: 12px;
}

.goal-card.on-track { background: var(--green-bg); border: 1px solid var(--green); }
.goal-card.needs-work { background: var(--yellow-bg); border: 1px solid var(--yellow); }
.goal-card.off-track { background: var(--red-bg); border: 1px solid var(--red); }

.goal-title {
  font-size: 16px;
  font-weight: 600;
  margin-bottom: 6px;
}

.goal-body { font-size: 14px; }

/* Responsive */
@media (max-width: 480px) {
  #app { padding: 0 12px 80px; }
  .metric-grid { grid-template-columns: repeat(2, 1fr); }
  .metric-value { font-size: 20px; }
  header h1 { font-size: 18px; }
  .recovery-dot .dot { width: 32px; height: 32px; font-size: 11px; }
}
