:root {
  --bg: #0f1419;
  --panel: #161d27;
  --panel-2: #1d2530;
  --border: #2a3440;
  --text: #e6edf3;
  --muted: #8b98a5;
  --accent: #4fa3ff;
  --accent-2: #3ddc97;
  --warn: #f5a623;
  --bad: #e5534b;
  --good: #3ddc97;
  --radius: 10px;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  background: var(--bg);
  color: var(--text);
  overflow-x: hidden; /* safety net: nothing should ever drag the whole page sideways */
  max-width: 100%;
}

.topbar {
  padding: 16px 24px 0;
  border-bottom: 1px solid var(--border);
  background: var(--panel);
}
.topbar h1 { margin: 0 0 12px; font-size: 1.3rem; }

.topbar-heading {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  gap: 12px;
}
.user-badge {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 0.82rem;
  color: var(--muted);
  white-space: nowrap;
}
.user-badge button {
  padding: 4px 10px;
  font-size: 0.78rem;
}

.tabs {
  display: flex;
  gap: 4px;
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
  scrollbar-width: thin;
}
.tab-btn {
  background: none;
  border: none;
  color: var(--muted);
  padding: 10px 16px;
  cursor: pointer;
  font-size: 0.95rem;
  border-bottom: 2px solid transparent;
  white-space: nowrap;
  flex-shrink: 0;
}
.tab-btn:hover { color: var(--text); }
.tab-btn.active { color: var(--accent); border-bottom-color: var(--accent); }

main { max-width: 900px; margin: 0 auto; padding: 20px; }

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

.card {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 18px;
  margin-bottom: 16px;
}
.card h3 { margin-top: 0; }

.row { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }

input, select, button {
  font-family: inherit;
  font-size: 0.95rem;
  background: var(--panel-2);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 8px 10px;
}

input[type="text"], input[type="number"], input[type="date"] { flex: 1 1 180px; min-width: 120px; }
#search-input { flex: 2 1 220px; min-width: 220px; }

button { cursor: pointer; }
button:hover { border-color: var(--accent); }

.primary-btn {
  background: var(--accent);
  color: #06121f;
  border: none;
  font-weight: 600;
  padding: 10px 18px;
}
.primary-btn:hover { opacity: 0.9; }

.muted { color: var(--muted); font-size: 0.88rem; }

.result-list { list-style: none; margin: 10px 0 0; padding: 0; }
.result-list li {
  padding: 10px 8px;
  border-bottom: 1px solid var(--border);
  cursor: pointer;
  display: flex;
  justify-content: space-between;
  gap: 8px;
}
.result-list li:hover { background: var(--panel-2); }
.result-list li:last-child { border-bottom: none; }
.result-name { font-weight: 500; }
.result-meta { color: var(--muted); font-size: 0.82rem; }

.ai-result-card {
  background: var(--panel-2);
  border-radius: 8px;
  padding: 12px 14px;
  margin-top: 10px;
}
.ai-result-header {
  display: flex;
  align-items: center;
  gap: 8px;
  font-weight: 500;
  margin-bottom: 4px;
}
.ai-type-badge {
  font-size: 0.7rem;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.03em;
  padding: 2px 7px;
  border-radius: 4px;
  flex-shrink: 0;
}
.ai-type-badge.food { background: rgba(79, 163, 255, 0.18); color: var(--accent); }
.ai-type-badge.workout { background: rgba(61, 220, 151, 0.18); color: var(--good); }
.ai-result-card .ai-add-btn {
  margin-top: 8px;
  background: var(--accent);
  color: #06121f;
  border: none;
  font-weight: 600;
  padding: 6px 14px;
  font-size: 0.85rem;
}

.modal {
  position: fixed; inset: 0;
  background: rgba(0,0,0,0.6);
  display: flex; align-items: center; justify-content: center;
  z-index: 50;
}
.modal.hidden { display: none; }
.modal-inner {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 24px;
  width: 90%; max-width: 480px;
  max-height: 85vh;
  overflow-y: auto;
  position: relative;
}
.close-btn {
  position: absolute; top: 12px; right: 12px;
  background: none; border: none; font-size: 1.4rem; color: var(--muted);
}

#scanner-camera-wrap {
  background: #000;
  border-radius: 8px;
  overflow: hidden;
  margin-top: 12px;
  aspect-ratio: 4 / 3;
}
#scanner-video {
  width: 100%; height: 100%;
  object-fit: cover;
  display: block;
}

.form-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(160px, 1fr));
  gap: 10px;
  margin: 12px 0;
}
.form-grid label {
  display: flex; flex-direction: column; gap: 4px;
  font-size: 0.82rem; color: var(--muted);
}

.nutrient-preview {
  background: var(--panel-2);
  border-radius: 8px;
  padding: 10px 14px;
  margin: 12px 0;
  font-size: 0.88rem;
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(140px, 1fr));
  gap: 6px 14px;
}
.nutrient-preview .np-row { display: flex; justify-content: space-between; }
.nutrient-preview .np-label { color: var(--muted); }

.totals-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(140px, 1fr));
  gap: 10px;
}
.stat-tile {
  background: var(--panel-2);
  border-radius: 8px;
  padding: 10px 12px;
}
.stat-tile .stat-value { font-size: 1.2rem; font-weight: 600; }
.stat-tile .stat-label { color: var(--muted); font-size: 0.78rem; }

.hero-stats {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 14px;
}
.hero-tile {
  background: var(--panel-2);
  border-radius: 10px;
  padding: 14px 16px;
}
.hero-value {
  font-size: 2rem;
  font-weight: 700;
  line-height: 1.1;
  color: var(--text);
}
.hero-value .hero-unit {
  font-size: 1rem;
  font-weight: 500;
  color: var(--muted);
  margin-left: 4px;
}
.hero-label {
  font-size: 0.85rem;
  color: var(--muted);
  margin: 2px 0 10px;
}
.hero-label .hero-target { color: var(--muted); }

.focus-bar { margin-bottom: 14px; }
.focus-bar-label { display: flex; justify-content: space-between; font-size: 0.9rem; margin-bottom: 4px; }
.focus-bar-track { background: var(--panel-2); border-radius: 6px; height: 10px; overflow: hidden; }
.focus-bar-fill { height: 100%; border-radius: 6px; transition: width 0.3s; }
.focus-bar-fill.good { background: var(--good); }
.focus-bar-fill.warn { background: var(--warn); }
.focus-bar-fill.bad { background: var(--bad); }

.meal-group { margin-bottom: 16px; }
.meal-group h4 { margin: 0 0 6px; text-transform: capitalize; color: var(--accent); }
.entry-row {
  display: flex; justify-content: space-between; align-items: center;
  padding: 8px 0; border-bottom: 1px solid var(--border);
  gap: 8px;
}
.entry-row .entry-meta { color: var(--muted); font-size: 0.8rem; }
.entry-actions button { padding: 4px 8px; font-size: 0.8rem; }
.entry-edit-form { width: 100%; }
.entry-edit-name { font-weight: 500; margin-bottom: 2px; }

.range-btn.active { border-color: var(--accent); color: var(--accent); }

.toast {
  position: fixed; bottom: 20px; left: 50%; transform: translateX(-50%);
  background: var(--panel-2); border: 1px solid var(--accent);
  padding: 10px 18px; border-radius: 8px; z-index: 100;
}
.toast.hidden { display: none; }
.toast.error { border-color: var(--bad); }

a { color: var(--accent); }
code { background: var(--panel-2); padding: 1px 5px; border-radius: 4px; }

@media (max-width: 600px) {
  /* Apple HIG / Material Design both call for >=44px touch targets; the compact desktop
     button padding renders a few px under that, fine with a mouse but easy to mis-tap
     with a thumb. */
  button, select, input[type="date"] {
    min-height: 44px;
  }
  .close-btn { min-height: 0; } /* icon-only corner button, not a normal tap target */
  .entry-actions button, .cf-delete-btn { min-height: 36px; } /* small inline row actions */
}
