/* ============================================================
   FoodTracker — Dark Theme, Mobile-First
   ============================================================ */

:root {
  --bg: #0a0a0a;
  --surface: #1a1a1a;
  --surface2: #252525;
  --border: #333;
  --text: #ffffff;
  --text-secondary: #a1a1aa;
  --accent: #8b5cf6;
  --accent-dim: rgba(139, 92, 246, 0.15);
  --green: #22c55e;
  --green-dim: rgba(34, 197, 94, 0.15);
  --red: #ef4444;
  --red-dim: rgba(239, 68, 68, 0.15);
  --yellow: #eab308;
  --yellow-dim: rgba(234, 179, 8, 0.15);
  --radius: 12px;
  --radius-sm: 8px;
  --nav-height: 64px;
  --safe-bottom: env(safe-area-inset-bottom, 0px);
}

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

html {
  font-size: 16px;
  -webkit-text-size-adjust: 100%;
  -webkit-tap-highlight-color: transparent;
}

body {
  font-family: 'Inter', -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
  background: var(--bg);
  color: var(--text);
  min-height: 100dvh;
  overflow-x: hidden;
  -webkit-font-smoothing: antialiased;
}

/* ---- App Shell ---- */
#app {
  max-width: 480px;
  margin: 0 auto;
  min-height: 100dvh;
  position: relative;
  overflow: hidden;
}

/* ---- Views ---- */
.view {
  display: none;
  padding: 16px 16px calc(var(--nav-height) + var(--safe-bottom) + 16px);
  animation: fadeIn .2s ease;
}
.view.active { display: block; }

@keyframes fadeIn {
  from { opacity: 0; transform: translateY(8px); }
  to { opacity: 1; transform: translateY(0); }
}

/* ---- Bottom Nav ---- */
#bottom-nav {
  position: fixed;
  bottom: 0;
  left: 50%;
  transform: translateX(-50%);
  width: 100%;
  max-width: 480px;
  height: var(--nav-height);
  background: #111;
  border-top: 1px solid var(--border);
  display: flex;
  align-items: center;
  justify-content: space-around;
  padding-bottom: var(--safe-bottom);
  z-index: 100;
}

.nav-btn {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 2px;
  background: none;
  border: none;
  color: var(--text-secondary);
  font-size: 11px;
  padding: 8px 12px;
  cursor: pointer;
  transition: color .15s;
  -webkit-tap-highlight-color: transparent;
  min-width: 48px;
  min-height: 48px;
  justify-content: center;
}
.nav-btn.active { color: var(--accent); }
.nav-btn svg { width: 24px; height: 24px; }

/* ---- Typography ---- */
h1 { font-size: 1.5rem; font-weight: 700; }
h2 { font-size: 1.15rem; font-weight: 600; }
h3 { font-size: 1rem; font-weight: 600; }
.text-secondary { color: var(--text-secondary); }
.text-sm { font-size: 0.85rem; }

/* ---- Buttons ---- */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  padding: 10px 20px;
  border-radius: var(--radius-sm);
  border: none;
  font-size: 0.9rem;
  font-weight: 600;
  cursor: pointer;
  transition: all .15s;
  min-height: 48px;
  -webkit-tap-highlight-color: transparent;
}
.btn-primary { background: var(--accent); color: #fff; }
.btn-primary:active { background: #7c3aed; }
.btn-ghost { background: transparent; color: var(--text-secondary); border: 1px solid var(--border); }
.btn-ghost:active { background: var(--surface); }
.btn-danger { background: var(--red); color: #fff; }
.btn-danger:active { background: #dc2626; }
.btn-sm { padding: 6px 12px; font-size: 0.8rem; min-height: 36px; }
.btn-block { width: 100%; }

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

/* ---- Date Navigation ---- */
.date-nav {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 16px;
}
.date-nav button {
  background: none;
  border: none;
  color: var(--text);
  font-size: 1.5rem;
  padding: 8px;
  cursor: pointer;
  min-width: 48px;
  min-height: 48px;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: var(--radius-sm);
}
.date-nav button:active { background: var(--surface); }

/* ---- Meal Section ---- */
.meal-section {
  margin-bottom: 20px;
}
.meal-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 8px;
}
.meal-header h3 { display: flex; align-items: center; gap: 8px; }
.meal-add-btn {
  width: 36px;
  height: 36px;
  border-radius: 50%;
  background: var(--accent);
  color: #fff;
  border: none;
  font-size: 1.3rem;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  min-width: 48px;
  min-height: 48px;
}
.meal-add-btn:active { background: #7c3aed; }

.meal-totals {
  display: flex;
  gap: 12px;
  font-size: 0.75rem;
  color: var(--text-secondary);
  margin-bottom: 8px;
  flex-wrap: wrap;
}
.meal-totals span { white-space: nowrap; }

/* ---- Log Entry ---- */
.log-entry {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 10px 12px;
  background: var(--surface);
  border-radius: var(--radius-sm);
  margin-bottom: 6px;
  position: relative;
  overflow: hidden;
  cursor: pointer;
  transition: background .15s;
}
.log-entry:active { background: var(--surface2); }
.log-entry-photo {
  width: 40px;
  height: 40px;
  border-radius: 8px;
  object-fit: cover;
  background: var(--surface2);
  flex-shrink: 0;
}
.log-entry-photo-placeholder {
  width: 40px;
  height: 40px;
  border-radius: 8px;
  background: var(--accent-dim);
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  font-size: 1.1rem;
}
.log-entry-info { flex: 1; min-width: 0; }
.log-entry-name { font-weight: 600; font-size: 0.9rem; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.log-entry-detail { font-size: 0.75rem; color: var(--text-secondary); margin-top: 2px; }
.log-entry-kcal { font-weight: 700; font-size: 0.9rem; color: var(--accent); white-space: nowrap; }
.log-entry-delete {
  position: absolute;
  right: 0;
  top: 0;
  bottom: 0;
  width: 60px;
  background: var(--red);
  color: #fff;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 0.8rem;
  font-weight: 600;
  transform: translateX(100%);
  transition: transform .2s;
}
.log-entry.swiped .log-entry-delete { transform: translateX(0); }

/* ---- Daily Summary ---- */
.daily-summary {
  background: var(--surface);
  border-radius: var(--radius);
  padding: 16px;
  margin-bottom: 20px;
}
.daily-summary h3 { margin-bottom: 12px; }
.macro-row {
  display: flex;
  align-items: center;
  gap: 10px;
  margin-bottom: 10px;
}
.macro-label {
  width: 90px;
  font-size: 0.8rem;
  color: var(--text-secondary);
  flex-shrink: 0;
}
.macro-bar {
  flex: 1;
  height: 8px;
  background: var(--surface2);
  border-radius: 4px;
  overflow: hidden;
}
.macro-bar-fill {
  height: 100%;
  border-radius: 4px;
  transition: width .3s ease, background .3s;
}
.macro-value {
  font-size: 0.8rem;
  font-weight: 600;
  width: 80px;
  text-align: right;
  flex-shrink: 0;
  white-space: nowrap;
}

/* ---- Food Grid (picker) ---- */
.food-picker-overlay {
  position: fixed;
  top: 0; left: 0; right: 0; bottom: 0;
  background: rgba(0,0,0,0.7);
  z-index: 200;
  display: none;
  align-items: flex-end;
  justify-content: center;
}
.food-picker-overlay.open { display: flex; }
.food-picker {
  background: var(--bg);
  border-radius: var(--radius) var(--radius) 0 0;
  width: 100%;
  max-width: 480px;
  max-height: 80dvh;
  overflow-y: auto;
  padding: 20px 16px;
  animation: slideUp .25s ease;
}
@keyframes slideUp {
  from { transform: translateY(100%); }
  to { transform: translateY(0); }
}
.food-picker-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 16px;
}
.food-picker-close {
  background: none; border: none; color: var(--text-secondary);
  font-size: 1.5rem; cursor: pointer; padding: 8px;
  min-width: 48px; min-height: 48px;
  display: flex; align-items: center; justify-content: center;
}
.food-grid {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 10px;
}
.food-card {
  background: var(--surface);
  border-radius: var(--radius-sm);
  padding: 8px;
  text-align: center;
  cursor: pointer;
  transition: all .15s;
  min-height: 48px;
}
.food-card:active { background: var(--accent-dim); transform: scale(0.96); }
.food-card img {
  width: 100%;
  aspect-ratio: 1;
  object-fit: cover;
  border-radius: 6px;
  background: var(--surface2);
  margin-bottom: 6px;
}
.food-card-placeholder {
  width: 100%;
  aspect-ratio: 1;
  background: var(--accent-dim);
  border-radius: 6px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 2rem;
  margin-bottom: 6px;
}
.food-card-name {
  font-size: 0.75rem;
  font-weight: 600;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.food-card-kcal {
  font-size: 0.65rem;
  color: var(--text-secondary);
  margin-top: 2px;
}

/* ---- Edit Log Modal ---- */
.modal-overlay {
  position: fixed;
  top: 0; left: 0; right: 0; bottom: 0;
  background: rgba(0,0,0,0.7);
  z-index: 300;
  display: none;
  align-items: center;
  justify-content: center;
}
.modal-overlay.open { display: flex; }
.modal {
  background: var(--surface);
  border-radius: var(--radius);
  padding: 24px;
  width: calc(100% - 32px);
  max-width: 400px;
  animation: fadeIn .2s ease;
}
.modal h3 { margin-bottom: 16px; }
.modal-actions {
  display: flex;
  gap: 8px;
  margin-top: 20px;
}
.modal-actions .btn { flex: 1; }

/* ---- Form Elements ---- */
.form-group {
  margin-bottom: 16px;
}
.form-group label {
  display: block;
  font-size: 0.8rem;
  color: var(--text-secondary);
  margin-bottom: 6px;
  font-weight: 500;
}
.form-group input,
.form-group select,
.form-group textarea {
  width: 100%;
  padding: 12px;
  background: var(--surface2);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  color: var(--text);
  font-size: 0.95rem;
  outline: none;
  transition: border-color .15s;
  min-height: 48px;
}
.form-group input:focus,
.form-group select:focus {
  border-color: var(--accent);
}
.form-group select { appearance: none; cursor: pointer; }

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

/* ---- Photo Upload ---- */
.photo-upload {
  width: 120px;
  height: 120px;
  border-radius: var(--radius);
  background: var(--surface2);
  border: 2px dashed var(--border);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  margin: 0 auto 16px;
  overflow: hidden;
  position: relative;
  transition: border-color .15s;
}
.photo-upload:active { border-color: var(--accent); }
.photo-upload img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}
.photo-upload-label {
  color: var(--text-secondary);
  font-size: 0.8rem;
  text-align: center;
}
.photo-upload svg { margin-bottom: 4px; }
.photo-upload input { display: none; }

/* ---- Item Card (in /items) ---- */
.item-list-card {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 12px;
  background: var(--surface);
  border-radius: var(--radius-sm);
  margin-bottom: 8px;
  cursor: pointer;
  transition: background .15s;
}
.item-list-card:active { background: var(--surface2); }
.item-list-photo {
  width: 50px;
  height: 50px;
  border-radius: 8px;
  object-fit: cover;
  background: var(--surface2);
  flex-shrink: 0;
}
.item-list-placeholder {
  width: 50px;
  height: 50px;
  border-radius: 8px;
  background: var(--accent-dim);
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  font-size: 1.4rem;
}
.item-list-info { flex: 1; min-width: 0; }
.item-list-name { font-weight: 600; font-size: 0.95rem; }
.item-list-meta { font-size: 0.75rem; color: var(--text-secondary); margin-top: 2px; }
.item-list-actions { display: flex; gap: 4px; }
.item-list-actions button {
  background: none; border: none; color: var(--text-secondary); padding: 8px; cursor: pointer;
  min-width: 44px; min-height: 44px; display: flex; align-items: center; justify-content: center;
  border-radius: var(--radius-sm);
}
.item-list-actions button:active { background: var(--surface2); }

/* ---- Progress Bars (Stats) ---- */
.progress-container { margin-bottom: 16px; }
.progress-label {
  display: flex;
  justify-content: space-between;
  font-size: 0.85rem;
  margin-bottom: 6px;
}
.progress-track {
  height: 12px;
  background: var(--surface2);
  border-radius: 6px;
  overflow: hidden;
}
.progress-fill {
  height: 100%;
  border-radius: 6px;
  transition: width .4s ease, background .3s;
}

/* ---- Stats Chart ---- */
.chart-container { margin-top: 20px; }
.chart-bars {
  display: flex;
  align-items: flex-end;
  gap: 6px;
  height: 160px;
  padding-top: 10px;
}
.chart-bar-wrapper {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  height: 100%;
  justify-content: flex-end;
}
.chart-bar {
  width: 100%;
  max-width: 40px;
  background: var(--accent);
  border-radius: 4px 4px 0 0;
  transition: height .3s ease;
  min-height: 2px;
}
.chart-bar-label {
  font-size: 0.65rem;
  color: var(--text-secondary);
  margin-top: 6px;
  text-align: center;
}
.chart-bar-value {
  font-size: 0.65rem;
  color: var(--text);
  margin-bottom: 4px;
  font-weight: 600;
}

/* ---- Deficit / Surplus ---- */
.deficit-indicator {
  text-align: center;
  padding: 16px;
  border-radius: var(--radius);
  font-size: 1.2rem;
  font-weight: 700;
  margin-top: 12px;
}
.deficit-indicator.deficit { background: var(--green-dim); color: var(--green); }
.deficit-indicator.surplus { background: var(--red-dim); color: var(--red); }
.deficit-indicator.balanced { background: var(--yellow-dim); color: var(--yellow); }

/* ---- Search ---- */
.search-bar {
  width: 100%;
  padding: 10px 14px;
  background: var(--surface2);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  color: var(--text);
  font-size: 0.9rem;
  margin-bottom: 16px;
  outline: none;
  min-height: 48px;
}
.search-bar:focus { border-color: var(--accent); }
.search-bar::placeholder { color: var(--text-secondary); }

/* ---- Empty State ---- */
.empty-state {
  text-align: center;
  padding: 40px 16px;
  color: var(--text-secondary);
}
.empty-state svg { margin-bottom: 12px; opacity: 0.4; }
.empty-state p { font-size: 0.9rem; }

/* ---- Range slider ---- */
input[type="range"] {
  -webkit-appearance: none;
  width: 100%;
  height: 6px;
  background: var(--surface2);
  border-radius: 3px;
  outline: none;
  border: none;
  min-height: auto;
  padding: 0;
}
input[type="range"]::-webkit-slider-thumb {
  -webkit-appearance: none;
  width: 24px;
  height: 24px;
  border-radius: 50%;
  background: var(--accent);
  cursor: pointer;
}

/* ---- Toast ---- */
.toast {
  position: fixed;
  bottom: calc(var(--nav-height) + var(--safe-bottom) + 16px);
  left: 50%;
  transform: translateX(-50%) translateY(80px);
  background: var(--surface);
  color: var(--text);
  padding: 12px 20px;
  border-radius: var(--radius-sm);
  font-size: 0.85rem;
  font-weight: 500;
  z-index: 400;
  opacity: 0;
  transition: all .3s ease;
  box-shadow: 0 4px 20px rgba(0,0,0,0.5);
}
.toast.show { opacity: 1; transform: translateX(-50%) translateY(0); }

/* Scrollbar */
::-webkit-scrollbar { width: 4px; }
::-webkit-scrollbar-track { background: transparent; }
::-webkit-scrollbar-thumb { background: var(--border); border-radius: 2px; }

/* Utilities */
.mt-8 { margin-top: 8px; }
.mt-16 { margin-top: 16px; }
.mb-8 { margin-bottom: 8px; }
.mb-16 { margin-bottom: 16px; }
.flex-between { display: flex; align-items: center; justify-content: space-between; }
.gap-8 { gap: 8px; }
.hidden { display: none !important; }

/* ---- Stats Sub-Tabs ---- */
.stats-tabs {
  display: flex;
  gap: 4px;
  background: var(--surface);
  border-radius: var(--radius);
  padding: 4px;
  margin-bottom: 16px;
}
.stats-tab {
  flex: 1;
  padding: 8px 4px;
  border: none;
  background: transparent;
  color: var(--text-secondary);
  font-size: 0.8rem;
  font-weight: 600;
  border-radius: var(--radius-sm);
  cursor: pointer;
  transition: all .15s;
  min-height: 40px;
  font-family: inherit;
}
.stats-tab.active {
  background: var(--accent);
  color: #fff;
}
.stats-panel {
  display: none;
}
.stats-panel.active {
  display: block;
}

/* ---- Range Toggle (14T/30T) ---- */
.stats-range-toggle {
  display: flex;
  gap: 4px;
}
.range-btn {
  padding: 4px 10px;
  border: 1px solid var(--border);
  background: transparent;
  color: var(--text-secondary);
  font-size: 0.75rem;
  font-weight: 600;
  border-radius: var(--radius-sm);
  cursor: pointer;
  min-height: 32px;
  font-family: inherit;
  transition: all .15s;
}
.range-btn.active {
  background: var(--accent);
  color: #fff;
  border-color: var(--accent);
}

/* ---- Canvas Charts ---- */
canvas {
  width: 100% !important;
  height: auto !important;
  display: block;
}

/* ---- Goal Cards ---- */
.goal-cards {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 8px;
}
.goal-card {
  background: var(--surface);
  border: 2px solid var(--border);
  border-radius: var(--radius);
  padding: 12px 8px;
  text-align: center;
  cursor: pointer;
  transition: all .15s;
}
.goal-card.active {
  border-color: var(--accent);
  background: var(--accent-dim);
}
.goal-card-icon {
  font-size: 1.5rem;
  margin-bottom: 4px;
}
.goal-card-title {
  font-size: 0.75rem;
  font-weight: 700;
  margin-bottom: 2px;
}
.goal-card-desc {
  font-size: 0.6rem;
  color: var(--text-secondary);
}

/* ---- Week Compare Cards ---- */
.week-compare {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 12px;
  margin-top: 12px;
}
.week-card {
  background: var(--surface);
  border-radius: var(--radius);
  padding: 16px;
}
.week-card h4 {
  font-size: 0.85rem;
  font-weight: 600;
  margin-bottom: 8px;
  color: var(--accent);
}
.week-card .week-stat {
  font-size: 0.8rem;
  color: var(--text-secondary);
  margin-bottom: 4px;
}
.week-card .week-stat strong {
  color: var(--text);
}

/* ---- Weight Summary ---- */
#weight-summary .weight-stat-row {
  display: flex;
  justify-content: space-between;
  padding: 6px 0;
  font-size: 0.85rem;
  border-bottom: 1px solid var(--border);
}
#weight-summary .weight-stat-row:last-child {
  border-bottom: none;
}
#weight-summary .weight-stat-label {
  color: var(--text-secondary);
}
#weight-summary .weight-stat-value {
  font-weight: 600;
}

/* ---- Month Stats ---- */
#month-stats .month-stat-row {
  display: flex;
  justify-content: space-between;
  padding: 6px 0;
  font-size: 0.85rem;
  border-bottom: 1px solid var(--border);
}
#month-stats .month-stat-row:last-child {
  border-bottom: none;
}
#month-stats .month-stat-label {
  color: var(--text-secondary);
}
#month-stats .month-stat-value {
  font-weight: 600;
}
