/* ============================================================
   Keto Fasting Tracker — Mobile-first, dark-mode stylesheet
   ============================================================ */

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

:root {
  --bg: #0a0a0a;
  --bg-card: #1a1a1a;
  --bg-input: #222;
  --border: #333;
  --text: #f0f0f0;
  --text-muted: #888;
  --accent: #e07b39;
  --accent-dim: #a05520;
  --green: #4caf50;
  --red: #f44336;
  --blue: #2196f3;
  --orange: #e07b39;
  --radius: 12px;
  --radius-sm: 8px;
  --tab-height: 60px;
  --header-height: 56px;
}

html, body {
  height: 100%;
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
  background: var(--bg);
  color: var(--text);
  font-size: 16px;
  line-height: 1.5;
  -webkit-text-size-adjust: 100%;
}

/* ---- Layout ---- */

#app {
  display: flex;
  flex-direction: column;
  height: 100dvh;
  max-width: 480px;
  margin: 0 auto;
}

.app-header {
  height: calc(var(--header-height) + env(safe-area-inset-top));
  padding-top: env(safe-area-inset-top);
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding-left: 16px;
  padding-right: 16px;
  background: var(--bg-card);
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
}

.app-header h1 {
  font-size: 20px;
  font-weight: 800;
  color: var(--accent);
  letter-spacing: -0.3px;
}

.api-key-indicator {
  font-size: 18px;
  opacity: 0.2;
  transition: opacity 0.2s;
}
.api-key-indicator.active {
  opacity: 1;
}

.tab-content {
  flex: 1;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  padding: 16px;
  padding-bottom: calc(var(--tab-height) + 16px);
}

.tab-bar {
  height: var(--tab-height);
  display: flex;
  background: var(--bg-card);
  border-top: 1px solid var(--border);
  flex-shrink: 0;
  position: sticky;
  bottom: 0;
}

.tab-btn {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 3px;
  background: none;
  border: none;
  color: var(--text-muted);
  font-size: 10px;
  font-weight: 500;
  cursor: pointer;
  padding: 6px 0;
  transition: color 0.15s;
  position: relative;
}

.tab-btn svg { width: 22px; height: 22px; fill: currentColor; }
.tab-btn.active { color: var(--accent); }
.tab-btn.active::after {
  content: '';
  position: absolute;
  top: 0;
  left: 25%;
  right: 25%;
  height: 2px;
  background: var(--accent);
  border-radius: 0 0 3px 3px;
}

.tab-section { display: none; }
.tab-section.active { display: block; }

/* ---- Cards ---- */

.card {
  background: var(--bg-card);
  border-radius: var(--radius);
  padding: 18px 16px;
  margin-bottom: 14px;
  border: 1px solid var(--border);
}

.card-title {
  font-size: 11px;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.1em;
  color: var(--text-muted);
  margin-bottom: 14px;
  padding-left: 10px;
  border-left: 3px solid var(--accent);
  line-height: 1.2;
}

/* ---- Buttons ---- */

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  padding: 10px 18px;
  border-radius: var(--radius-sm);
  border: none;
  font-size: 15px;
  font-weight: 600;
  cursor: pointer;
  transition: opacity 0.15s;
  text-decoration: none;
}

.btn:disabled { opacity: 0.4; cursor: not-allowed; }
.btn-primary { background: var(--accent); color: #fff; }
.btn-danger { background: var(--red); color: #fff; }
.btn-ghost { background: var(--bg-input); color: var(--text); border: 1px solid var(--border); }
.btn-full { width: 100%; }
.btn-sm { padding: 6px 12px; font-size: 13px; }

/* ---- Forms ---- */

.form-group { margin-bottom: 14px; }
.form-label { display: block; font-size: 13px; color: var(--text-muted); margin-bottom: 6px; }

.form-input, .form-select, .form-textarea {
  width: 100%;
  background: var(--bg-input);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  color: var(--text);
  font-size: 15px;
  padding: 10px 12px;
  outline: none;
  transition: border-color 0.15s;
  -webkit-appearance: none;
}

.form-input:focus, .form-select:focus, .form-textarea:focus {
  border-color: var(--accent);
}

.form-textarea { resize: vertical; min-height: 70px; }

.form-row {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 10px;
}

.form-row-3 {
  display: grid;
  grid-template-columns: 1fr 1fr 1fr;
  gap: 8px;
}

/* ---- Macro grid ---- */

.macro-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 10px;
}

.macro-cell { display: flex; flex-direction: column; gap: 4px; }
.macro-label { font-size: 11px; color: var(--text-muted); }
.macro-value { font-size: 18px; font-weight: 700; font-variant-numeric: tabular-nums; }
.macro-unit { font-size: 11px; color: var(--text-muted); font-weight: 400; }

/* ---- Progress bars ---- */

.progress-bar {
  height: 8px;
  background: var(--border);
  border-radius: 4px;
  overflow: hidden;
  margin-top: 5px;
  margin-bottom: 10px;
}

.progress-fill {
  height: 100%;
  border-radius: 4px;
  transition: width 0.3s ease;
  background: var(--accent);
}

.progress-fill.good { background: var(--green); }
.progress-fill.over { background: var(--red); }

.progress-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  font-size: 13px;
  margin-bottom: 2px;
}

.progress-row .label { color: var(--text-muted); }
.progress-row .value { font-variant-numeric: tabular-nums; }

/* ---- Timer ---- */

.timer-display {
  font-size: 56px;
  font-weight: 800;
  font-variant-numeric: tabular-nums;
  letter-spacing: -1px;
  text-align: center;
  color: var(--accent);
  line-height: 1;
  padding: 8px 0;
}

.timer-label {
  text-align: center;
  color: var(--text-muted);
  font-size: 14px;
  margin-bottom: 16px;
}

/* ---- Progress ring ---- */

.ring-container {
  display: flex;
  justify-content: center;
  margin: 12px 0;
}

.ring-svg { transform: rotate(-90deg); }

.ring-bg { fill: none; stroke: var(--border); }
.ring-fill { fill: none; stroke: var(--accent); stroke-linecap: round; transition: stroke-dashoffset 1s linear; }

.ring-label {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  font-size: 18px;
  font-weight: 700;
  text-align: center;
}

.ring-wrapper { position: relative; display: inline-block; }

/* ---- Protocol picker ---- */

.protocol-grid {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 8px;
  margin-bottom: 16px;
}

.protocol-btn {
  padding: 12px 4px;
  border-radius: var(--radius-sm);
  border: 1px solid var(--border);
  background: var(--bg-input);
  color: var(--text-muted);
  font-size: 13px;
  font-weight: 700;
  cursor: pointer;
  text-align: center;
  transition: all 0.15s;
  letter-spacing: 0.02em;
}

.protocol-btn.selected {
  border-color: var(--accent);
  background: var(--accent-dim);
  color: #fff;
}

/* ---- Fast history list ---- */

.fast-item {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 10px 0;
  border-bottom: 1px solid var(--border);
}

.fast-item:last-child { border-bottom: none; }
.fast-date { font-weight: 600; }
.fast-protocol { font-size: 12px; color: var(--text-muted); }
.fast-duration { font-variant-numeric: tabular-nums; font-weight: 700; color: var(--accent); }

/* ---- Food log ---- */

.date-nav {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 16px;
  background: var(--bg-card);
  border-radius: var(--radius);
  padding: 12px 16px;
  border: 1px solid var(--border);
}

.date-nav-btn {
  background: none;
  border: none;
  color: var(--text);
  font-size: 22px;
  cursor: pointer;
  padding: 0 8px;
  line-height: 1;
}

.date-nav-btn:disabled { color: var(--border); cursor: not-allowed; }
.date-label { font-weight: 600; font-size: 15px; }

.meal-section { margin-bottom: 12px; }
.meal-header {
  font-size: 12px;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  color: var(--text-muted);
  padding: 6px 0;
  border-bottom: 1px solid var(--border);
  margin-bottom: 4px;
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.food-item {
  display: flex;
  align-items: center;
  padding: 8px 0;
  border-bottom: 1px solid var(--bg);
  gap: 8px;
}

.food-item-name { flex: 1; font-size: 14px; }
.food-item-cals { font-size: 13px; color: var(--text-muted); font-variant-numeric: tabular-nums; }

.food-delete-btn {
  background: none;
  border: none;
  color: var(--red);
  font-size: 18px;
  cursor: pointer;
  padding: 0 4px;
  line-height: 1;
  opacity: 0.6;
}

.food-delete-btn:hover { opacity: 1; }

.totals-row {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 6px;
  padding: 10px;
  background: var(--bg-input);
  border-radius: var(--radius-sm);
  margin-top: 8px;
}

.total-cell { text-align: center; }
.total-cell .lbl { font-size: 10px; color: var(--text-muted); }
.total-cell .val { font-size: 14px; font-weight: 700; font-variant-numeric: tabular-nums; }
.total-cell .val.over { color: var(--red); }

/* ---- Body stats grid ---- */

.stats-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 12px;
}

.stat-cell { display: flex; flex-direction: column; gap: 2px; }
.stat-label { font-size: 11px; color: var(--text-muted); }
.stat-value { font-size: 20px; font-weight: 700; }
.stat-value.highlight { color: var(--accent); }

/* ---- Modals ---- */

.modal-overlay {
  display: none;
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.75);
  z-index: 100;
  align-items: flex-end;
}

.modal-overlay.open { display: flex; }

.modal-sheet {
  background: var(--bg-card);
  border-radius: var(--radius) var(--radius) 0 0;
  padding: 20px 16px;
  padding-bottom: calc(20px + env(safe-area-inset-bottom));
  width: 100%;
  max-height: 90dvh;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
}

.modal-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 16px;
}

.modal-title { font-size: 18px; font-weight: 700; }
.modal-close {
  background: var(--bg-input);
  border: none;
  color: var(--text);
  width: 32px;
  height: 32px;
  border-radius: 50%;
  font-size: 18px;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
}

/* ---- AI input area ---- */

.ai-input-row {
  display: flex;
  gap: 8px;
  margin-bottom: 14px;
}

.ai-input-row .form-input { flex: 1; }

/* ---- Image preview ---- */

.image-preview {
  width: 100%;
  max-height: 200px;
  object-fit: contain;
  border-radius: var(--radius-sm);
  background: var(--bg-input);
  display: none;
  margin-bottom: 12px;
}

/* ---- Charts ---- */

.chart-container {
  position: relative;
  height: 180px;
  margin-top: 8px;
}

/* ---- Empty state ---- */

.empty-state {
  text-align: center;
  padding: 36px 20px;
  color: var(--text-muted);
  font-size: 14px;
  line-height: 1.7;
}

/* ---- Misc ---- */

.text-muted { color: var(--text-muted); }
.text-sm { font-size: 13px; }
.text-xs { font-size: 11px; }
.mt-8 { margin-top: 8px; }
.mt-12 { margin-top: 12px; }
.mb-8 { margin-bottom: 8px; }
.divider { height: 1px; background: var(--border); margin: 12px 0; }

.status-msg {
  font-size: 13px;
  padding: 8px 12px;
  border-radius: var(--radius-sm);
  margin-top: 8px;
}

.status-msg.error { background: rgba(244,67,54,0.15); color: var(--red); }
.status-msg.success { background: rgba(76,175,80,0.15); color: var(--green); }
.status-msg.info { background: rgba(33,150,243,0.15); color: var(--blue); }

.loading-spinner {
  display: inline-block;
  width: 18px;
  height: 18px;
  border: 2px solid var(--border);
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: spin 0.7s linear infinite;
}

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

/* Safe area on iPhone */
.tab-bar {
  height: calc(var(--tab-height) + env(safe-area-inset-bottom));
  padding-bottom: env(safe-area-inset-bottom);
}
