:root {
  --bg: #0a0a0a;
  --surface: #1a1a1a;
  --surface2: #252525;
  --border: #333;
  --text: #f0f0f0;
  --text2: #888;
  --accent: #e8ff00;
  --accent-dim: rgba(232,255,0,0.15);
  --red: #ff4444;
  --green: #44cc66;
  --radius: 12px;
  --tab-h: 64px;
}

@media (prefers-color-scheme: light) {
  :root {
    --bg: #f5f5f5;
    --surface: #ffffff;
    --surface2: #f0f0f0;
    --border: #ddd;
    --text: #111;
    --text2: #666;
    --accent: #1a1a1a;
    --accent-dim: rgba(0,0,0,0.08);
  }
}

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

html, body {
  height: 100%;
  font-family: -apple-system, BlinkMacSystemFont, 'Helvetica Neue', sans-serif;
  background: var(--bg);
  color: var(--text);
  overflow: hidden;
  -webkit-tap-highlight-color: transparent;
}

#app {
  display: flex;
  flex-direction: column;
  height: 100%;
  max-width: 430px;
  margin: 0 auto;
  position: relative;
}

/* ── Tabs ── */
#tab-bar {
  display: flex;
  background: var(--surface);
  border-top: 1px solid var(--border);
  height: var(--tab-h);
  position: fixed;
  bottom: 0;
  left: 50%;
  transform: translateX(-50%);
  width: 100%;
  max-width: 430px;
  z-index: 100;
  padding-bottom: env(safe-area-inset-bottom);
}

.tab-btn {
  flex: 1;
  background: none;
  border: none;
  color: var(--text2);
  font-size: 10px;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 4px;
  cursor: pointer;
  transition: color 0.2s;
  padding-bottom: 4px;
}
.tab-btn.active { color: var(--accent); }
.tab-btn svg { width: 24px; height: 24px; }

/* ── Screens ── */
.screen {
  display: none;
  flex-direction: column;
  flex: 1;
  overflow-y: auto;
  padding: 16px 16px calc(var(--tab-h) + env(safe-area-inset-bottom) + 8px);
  padding-top: calc(env(safe-area-inset-top) + 16px);
}
.screen.active { display: flex; }

/* ── Cards ── */
.card {
  background: var(--surface);
  border-radius: var(--radius);
  padding: 16px;
  margin-bottom: 12px;
}

.card-title {
  font-size: 11px;
  font-weight: 700;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  color: var(--text2);
  margin-bottom: 12px;
}

/* ── Stats grid ── */
.stats-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 10px;
  margin-bottom: 12px;
}
.stat-card {
  background: var(--surface);
  border-radius: var(--radius);
  padding: 14px;
}
.stat-label { font-size: 11px; color: var(--text2); margin-bottom: 4px; }
.stat-value { font-size: 26px; font-weight: 700; line-height: 1; }
.stat-unit { font-size: 13px; color: var(--text2); }
.stat-sub { font-size: 11px; color: var(--text2); margin-top: 4px; }

/* ── Day header ── */
#day-header {
  margin-bottom: 16px;
}
#day-label {
  font-size: 13px;
  color: var(--text2);
  margin-bottom: 2px;
}
#day-title {
  font-size: 28px;
  font-weight: 800;
  line-height: 1.1;
}

/* ── Checklist ── */
.check-item {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 12px 0;
  border-bottom: 1px solid var(--border);
  cursor: pointer;
  user-select: none;
}
.check-item:last-child { border-bottom: none; }

.check-box {
  width: 22px; height: 22px;
  border-radius: 50%;
  border: 2px solid var(--border);
  flex-shrink: 0;
  display: flex; align-items: center; justify-content: center;
  transition: all 0.2s;
}
.check-item.done .check-box {
  background: var(--green);
  border-color: var(--green);
}
.check-item.done .check-box::after {
  content: '✓';
  color: #000;
  font-size: 13px;
  font-weight: 700;
}
.check-label { font-size: 15px; flex: 1; }
.check-item.done .check-label { color: var(--text2); text-decoration: line-through; }

/* ── Progress bars ── */
.progress-item { margin-bottom: 14px; }
.progress-item:last-child { margin-bottom: 0; }
.progress-row {
  display: flex; justify-content: space-between;
  font-size: 13px; margin-bottom: 6px;
}
.progress-track {
  height: 6px; background: var(--surface2);
  border-radius: 3px; overflow: hidden;
}
.progress-fill {
  height: 100%; background: var(--accent);
  border-radius: 3px; transition: width 0.5s ease;
}

/* ── Calendar dots ── */
#calendar-grid {
  display: grid;
  grid-template-columns: repeat(13, 1fr);
  gap: 4px;
  margin-top: 8px;
}
.cal-dot {
  aspect-ratio: 1;
  border-radius: 50%;
  background: var(--surface2);
}
.cal-dot.done { background: var(--green); }
.cal-dot.today { background: var(--accent); }
.cal-dot.future { background: var(--surface2); opacity: 0.4; }

/* ── Input controls ── */
.input-group {
  margin-bottom: 16px;
}
.input-label {
  font-size: 12px;
  font-weight: 600;
  color: var(--text2);
  text-transform: uppercase;
  letter-spacing: 0.06em;
  margin-bottom: 8px;
}
.stepper {
  display: flex;
  align-items: center;
  gap: 0;
  background: var(--surface2);
  border-radius: 10px;
  overflow: hidden;
}
.stepper-btn {
  width: 48px; height: 48px;
  background: none;
  border: none;
  color: var(--text);
  font-size: 22px;
  cursor: pointer;
  display: flex; align-items: center; justify-content: center;
  flex-shrink: 0;
}
.stepper-val {
  flex: 1;
  text-align: center;
  font-size: 22px;
  font-weight: 700;
  padding: 8px 0;
  background: none;
  border: none;
  color: var(--text);
}
.stepper-unit {
  padding-right: 12px;
  font-size: 13px;
  color: var(--text2);
}

.num-input {
  width: 100%;
  background: var(--surface2);
  border: none;
  border-radius: 10px;
  color: var(--text);
  font-size: 18px;
  font-weight: 600;
  padding: 12px 16px;
  -webkit-appearance: none;
}

.seg-control {
  display: flex;
  background: var(--surface2);
  border-radius: 10px;
  padding: 3px;
}
.seg-btn {
  flex: 1;
  border: none;
  background: none;
  color: var(--text2);
  font-size: 13px;
  font-weight: 600;
  padding: 8px 4px;
  border-radius: 8px;
  cursor: pointer;
  transition: all 0.2s;
}
.seg-btn.active {
  background: var(--surface);
  color: var(--text);
}

/* ── Save button ── */
.btn-primary {
  width: 100%;
  padding: 16px;
  background: var(--accent);
  color: #000;
  border: none;
  border-radius: var(--radius);
  font-size: 16px;
  font-weight: 800;
  cursor: pointer;
  margin-top: 8px;
  letter-spacing: 0.02em;
}
.btn-primary:active { opacity: 0.85; }

/* ── Coach tab ── */
.coach-feedback {
  font-size: 15px;
  line-height: 1.7;
  color: var(--text);
  white-space: pre-wrap;
}
.coach-loading {
  color: var(--text2);
  font-size: 14px;
  font-style: italic;
}

.chat-input-row {
  display: flex;
  gap: 8px;
  margin-top: 8px;
}
.chat-input {
  flex: 1;
  background: var(--surface2);
  border: none;
  border-radius: 10px;
  color: var(--text);
  font-size: 15px;
  padding: 12px 14px;
}
.chat-input::placeholder { color: var(--text2); }
.btn-send {
  width: 48px; height: 48px;
  border-radius: 10px;
  background: var(--accent);
  border: none;
  color: #000;
  font-size: 20px;
  cursor: pointer;
  flex-shrink: 0;
  display: flex; align-items: center; justify-content: center;
}

/* API Key setup */
.api-key-section {
  margin-bottom: 16px;
}
.api-key-input {
  width: 100%;
  background: var(--surface2);
  border: none;
  border-radius: 10px;
  color: var(--text);
  font-size: 14px;
  padding: 12px 14px;
  font-family: monospace;
}
.api-key-input::placeholder { color: var(--text2); }

/* Chart container */
.chart-wrap {
  position: relative;
  height: 160px;
}

/* Week summary */
.week-row {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 10px 0;
  border-bottom: 1px solid var(--border);
  font-size: 14px;
}
.week-row:last-child { border-bottom: none; }
.week-rate {
  font-weight: 700;
  color: var(--green);
}

/* Toast */
#toast {
  position: fixed;
  bottom: calc(var(--tab-h) + env(safe-area-inset-bottom) + 12px);
  left: 50%;
  transform: translateX(-50%) translateY(20px);
  background: var(--text);
  color: var(--bg);
  padding: 10px 20px;
  border-radius: 20px;
  font-size: 14px;
  font-weight: 600;
  opacity: 0;
  transition: all 0.3s;
  pointer-events: none;
  z-index: 200;
  white-space: nowrap;
}
#toast.show {
  opacity: 1;
  transform: translateX(-50%) translateY(0);
}

.section-gap { height: 8px; }

/* ── Week summary ── */
.week-summary-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 10px;
}
.week-stat {
  background: var(--surface2);
  border-radius: 10px;
  padding: 12px;
  text-align: center;
}
.week-stat-val {
  font-size: 22px;
  font-weight: 800;
  margin-bottom: 4px;
}
.week-stat-label {
  font-size: 11px;
  color: var(--text2);
}

/* ── Meal photo analysis ── */
.meal-photo-section {
  margin-bottom: 4px;
}

.btn-camera {
  width: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 10px;
  padding: 16px;
  background: var(--accent-dim);
  border: 2px dashed var(--accent);
  border-radius: var(--radius);
  color: var(--accent);
  font-size: 15px;
  font-weight: 700;
  cursor: pointer;
  transition: opacity 0.2s;
}
.btn-camera:active { opacity: 0.7; }
.camera-icon { font-size: 22px; }

.meal-preview-wrap {
  position: relative;
  margin-bottom: 12px;
}
.meal-preview-img {
  width: 100%;
  border-radius: var(--radius);
  max-height: 220px;
  object-fit: cover;
  display: block;
}
.btn-retake {
  position: absolute;
  top: 8px;
  right: 8px;
  background: rgba(0,0,0,0.6);
  color: #fff;
  border: none;
  border-radius: 8px;
  padding: 6px 12px;
  font-size: 12px;
  font-weight: 600;
  cursor: pointer;
}

.meal-analyzing {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 14px 0;
  color: var(--text2);
  font-size: 14px;
}
.analyzing-spinner {
  width: 18px; height: 18px;
  border: 2px solid var(--border);
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
  flex-shrink: 0;
}
@keyframes spin { to { transform: rotate(360deg); } }

.meal-analysis-card {
  background: var(--surface2);
  border-radius: var(--radius);
  padding: 14px;
  margin-bottom: 4px;
}
.meal-foods {
  font-size: 14px;
  color: var(--text2);
  margin-bottom: 12px;
  line-height: 1.6;
}
.meal-nums {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 10px;
  margin-bottom: 12px;
}
.meal-num-item {
  background: var(--surface);
  border-radius: 8px;
  padding: 10px 12px;
}
.meal-num-label {
  font-size: 10px;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--text2);
  margin-bottom: 4px;
}
.meal-num-value {
  font-size: 22px;
  font-weight: 700;
}
.meal-num-unit {
  font-size: 12px;
  color: var(--text2);
}
.meal-coach-comment {
  font-size: 13px;
  color: var(--text2);
  line-height: 1.6;
  margin-bottom: 12px;
  padding: 10px 12px;
  border-left: 2px solid var(--accent);
}
.btn-apply {
  width: 100%;
  padding: 13px;
  background: var(--green);
  color: #000;
  border: none;
  border-radius: 10px;
  font-size: 15px;
  font-weight: 800;
  cursor: pointer;
}
.btn-apply:active { opacity: 0.8; }

.meal-divider {
  height: 1px;
  background: var(--border);
  margin: 16px 0;
}

/* ── Scale reader ── */
.btn-scale {
  margin-bottom: 0;
}
.scale-preview-wrap {
  position: relative;
  margin: 12px 0;
}
.scale-analysis-card {
  background: var(--surface2);
  border-radius: var(--radius);
  padding: 14px;
  margin-bottom: 4px;
}
.scale-detected-nums {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 6px;
  margin-bottom: 12px;
}
.scale-num {
  background: var(--surface);
  border-radius: 8px;
  padding: 10px 8px;
  text-align: center;
}
.scale-num-label {
  font-size: 10px;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: var(--text2);
  margin-bottom: 4px;
}
.scale-num-value {
  font-size: 15px;
  font-weight: 700;
}
.scale-num-value.detected {
  color: var(--green);
}
.scale-num-value.not-detected {
  color: var(--text2);
  font-size: 14px;
}
.scale-note {
  font-size: 12px;
  color: var(--text2);
  margin-bottom: 12px;
  line-height: 1.5;
}

/* Streak badge */
.streak-badge {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  background: var(--accent-dim);
  color: var(--accent);
  border-radius: 8px;
  padding: 4px 10px;
  font-size: 13px;
  font-weight: 700;
  margin-top: 6px;
}
