/* Mobile-first, RTL-aware via CSS logical properties. Auto dark/light. */

:root {
  --bg: #f8fafc;
  --surface: #ffffff;
  --surface-2: #f1f5f9;
  --border: #e2e8f0;
  --text: #0f172a;
  --muted: #64748b;
  --accent: #16a34a;
  --accent-2: #15803d;
  --warn: #f59e0b;
  --bad: #ef4444;
  --good: #22c55e;
  --header-bg: #0f172a;
  --header-fg: #f8fafc;
  --shadow: 0 1px 2px rgba(15, 23, 42, 0.06), 0 4px 12px rgba(15, 23, 42, 0.06);
  --radius: 14px;
  --radius-sm: 10px;
  --tap: 44px;
}

/* dark mode (auto) */
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0b1120;
    --surface: #111827;
    --surface-2: #1f2937;
    --border: #1f2937;
    --text: #f1f5f9;
    --muted: #94a3b8;
    --header-bg: #020617;
    --shadow: 0 1px 2px rgba(0,0,0,0.4), 0 6px 16px rgba(0,0,0,0.3);
  }
}

/* explicit dark mode (overrides auto) */
html[data-theme-mode="dark"] {
  --bg: #0b1120;
  --surface: #111827;
  --surface-2: #1f2937;
  --border: #1f2937;
  --text: #f1f5f9;
  --muted: #94a3b8;
  --header-bg: #020617;
  --shadow: 0 1px 2px rgba(0,0,0,0.4), 0 6px 16px rgba(0,0,0,0.3);
}

/* explicit light mode (overrides auto+dark) */
html[data-theme-mode="light"] {
  --bg: #f8fafc;
  --surface: #ffffff;
  --surface-2: #f1f5f9;
  --border: #e2e8f0;
  --text: #0f172a;
  --muted: #64748b;
  --header-bg: #0f172a;
  --header-fg: #f8fafc;
  --shadow: 0 1px 2px rgba(15, 23, 42, 0.06), 0 4px 12px rgba(15, 23, 42, 0.06);
}

/* accent swatches */
html[data-theme-accent="green"]  { --accent: #16a34a; --accent-2: #15803d; }
html[data-theme-accent="blue"]   { --accent: #2563eb; --accent-2: #1d4ed8; }
html[data-theme-accent="purple"] { --accent: #9333ea; --accent-2: #7e22ce; }
html[data-theme-accent="orange"] { --accent: #ea580c; --accent-2: #c2410c; }
html[data-theme-accent="pink"]   { --accent: #db2777; --accent-2: #be185d; }

* { box-sizing: border-box; }
html, body { margin: 0; padding: 0; }
body {
  font-family: system-ui, -apple-system, "Segoe UI", "Helvetica Neue", "Heebo", "Arial", sans-serif;
  background: var(--bg);
  color: var(--text);
  -webkit-font-smoothing: antialiased;
  padding-block-end: calc(72px + env(safe-area-inset-bottom));
}

/* header */
.app-header {
  position: sticky; top: 0; z-index: 5;
  background: var(--header-bg); color: var(--header-fg);
  padding: env(safe-area-inset-top) 16px 12px;
  text-align: center;
}
.app-header h1 { margin: 0; font-size: 18px; font-weight: 600; padding-block-start: 8px; }

main {
  padding: 8px;
  display: flex; flex-direction: column; gap: 6px;
  max-width: 600px; margin: 0 auto;
}

/* bottom nav */
.nav {
  position: fixed; inset-inline: 0; inset-block-end: 0;
  display: flex; justify-content: space-around;
  background: var(--surface); border-block-start: 1px solid var(--border);
  padding-block-end: env(safe-area-inset-bottom);
  z-index: 6;
}
.nav a {
  flex: 1; padding: 8px 4px;
  display: flex; flex-direction: column; align-items: center; gap: 2px;
  text-decoration: none; color: var(--muted);
  font-size: 11px;
  min-height: var(--tap);
}
.nav a.active { color: var(--accent); }
.nav a span[aria-hidden] { font-size: 20px; line-height: 1; }
.nav-label { font-size: 11px; }

/* cards & primitives */
.card {
  background: var(--surface);
  border-radius: var(--radius);
  padding: 10px 12px;
  box-shadow: var(--shadow);
}
.card h2 { font-size: 15px; margin: 0 0 8px; }
.card h3 { font-size: 13px; margin: 0 0 6px; color: var(--muted); font-weight: 600; }
.card canvas { cursor: zoom-in; }

/* dense chart grid: 2 columns of small charts */
.chart-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 6px;
}
.chart-card {
  padding: 6px 8px;
  display: flex;
  flex-direction: column;
  min-width: 0;
  height: 150px;
}
.chart-card h3 {
  font-size: 11px;
  margin: 0 0 4px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  flex: 0 0 auto;
}
.chart-card canvas {
  flex: 1;
  min-height: 0;
  width: 100% !important;
  max-width: 100%;
}
/* pies need a touch more room for legend */
.chart-card.pie { height: 170px; }
.card-head { display: flex; justify-content: space-between; align-items: center; gap: 8px; margin-block-end: 10px; }
.card-head h2 { margin: 0; }

.muted { color: var(--muted); }
.small { font-size: 12px; }
.big { font-size: 22px; font-weight: 700; }
.unit { font-size: 12px; color: var(--muted); font-weight: 500; }

/* day nav */
.day-nav {
  display: flex; align-items: center; gap: 6px; justify-content: space-between;
  background: var(--surface); border-radius: var(--radius); padding: 6px;
  box-shadow: var(--shadow);
}
.day-nav .icon-btn { font-size: 22px; }
.day-picker {
  flex: 1; background: transparent; border: none; color: var(--text);
  font-size: 16px; font-weight: 600; padding: 10px;
}

/* budget */
.budget .budget-row {
  display: flex; justify-content: space-between; align-items: flex-end; gap: 12px;
}
.budget .budget-row.small { font-size: 13px; padding-block-start: 6px; }
.balance.good { color: var(--good); }
.balance.bad { color: var(--bad); }
.bar {
  height: 8px; background: var(--surface-2); border-radius: 999px; overflow: hidden;
  margin-block: 12px 6px;
}
.bar-fill { height: 100%; background: var(--accent); transition: width .25s; }
.bar-fill.over { background: var(--bad); }

/* meal lists */
.meal-list { list-style: none; padding: 0; margin: 0; display: flex; flex-direction: column; gap: 6px; }
.meal-row {
  display: grid; grid-template-columns: 1fr auto auto; align-items: center; gap: 8px;
  padding: 10px; background: var(--surface-2); border-radius: var(--radius-sm);
}
.meal-name { font-weight: 600; font-size: 14px; }
.meal-meta { font-size: 12px; color: var(--muted); white-space: nowrap; }
.icon-btn {
  background: transparent; border: 0; color: var(--muted);
  font-size: 18px; min-width: var(--tap); min-height: var(--tap);
  cursor: pointer; border-radius: 8px;
}
.icon-btn:hover { background: var(--surface-2); color: var(--text); }

/* buttons */
.btn-primary, .btn-secondary {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  border: 0; border-radius: var(--radius-sm);
  padding: 10px 14px; font-size: 14px; font-weight: 600; cursor: pointer;
  min-height: var(--tap);
}
.btn-primary { background: var(--accent); color: white; }
.btn-primary:hover { background: var(--accent-2); }
.btn-secondary { background: var(--surface-2); color: var(--text); }
.btn-secondary:hover { background: var(--border); }
.full { width: 100%; margin-block-start: 10px; }
.row-actions { display: flex; gap: 8px; justify-content: flex-end; margin-block-start: 10px; }
.row-actions .btn-primary, .row-actions .btn-secondary { flex: 1; }

/* labelled input rows */
.row {
  display: flex; align-items: center; justify-content: space-between; gap: 12px;
  padding-block: 10px;
  border-block-end: 1px solid var(--border);
}
.row:last-child { border-block-end: 0; }
.row > span { color: var(--muted); font-size: 14px; }
.row input, .row select {
  flex: 1; max-width: 160px;
  background: var(--surface-2); border: 1px solid var(--border); color: var(--text);
  padding: 8px 10px; border-radius: var(--radius-sm);
  font-size: 14px; font-family: inherit;
  min-height: var(--tap);
}
.row input[type="checkbox"] { transform: scale(1.3); flex: 0; max-width: none; min-height: 0; }

/* tabs */
.tabs {
  display: flex; gap: 4px; padding: 4px;
  background: var(--surface); border-radius: var(--radius); box-shadow: var(--shadow);
}
.tabs a {
  flex: 1; text-align: center; padding: 10px;
  border-radius: var(--radius-sm); text-decoration: none;
  color: var(--muted); font-weight: 600; font-size: 14px;
}
.tabs a.active { background: var(--accent); color: white; }

/* stats grid */
.grid-2 {
  display: grid; grid-template-columns: 1fr 1fr; gap: 8px;
}
.stat {
  background: var(--surface-2); border-radius: var(--radius-sm); padding: 10px;
}
.stat.good .big { color: var(--good); }
.stat.bad .big { color: var(--bad); }

/* lang */
.lang-row { display: flex; gap: 6px; }
.lang-btn {
  flex: 1; padding: 10px; border-radius: var(--radius-sm);
  border: 1px solid var(--border); background: var(--surface-2); color: var(--text);
  font-weight: 600; cursor: pointer;
  min-height: var(--tap);
}
.lang-btn.active { background: var(--accent); color: white; border-color: var(--accent); }

/* modal */
.modal-backdrop {
  position: fixed; inset: 0; z-index: 20;
  background: rgba(15, 23, 42, 0.5);
  display: flex; align-items: flex-end; justify-content: center;
  padding: 0;
}
.modal {
  background: var(--surface); color: var(--text);
  width: 100%; max-width: 600px; max-height: 90vh; overflow-y: auto;
  border-start-start-radius: var(--radius); border-start-end-radius: var(--radius);
  padding: 16px; padding-block-end: calc(16px + env(safe-area-inset-bottom));
  box-shadow: 0 -10px 30px rgba(0, 0, 0, 0.2);
}
.modal h2 { margin-block: 0 12px; font-size: 18px; }
.modal input[type="text"], .modal input[type="number"], .modal input[type="search"], .modal input[type="time"], .modal input[type="date"], .modal input[type="password"], .modal select {
  width: 100%;
  background: var(--surface-2); border: 1px solid var(--border); color: var(--text);
  padding: 10px; border-radius: var(--radius-sm);
  font-size: 16px; font-family: inherit;
  margin-block: 4px;
  min-height: var(--tap);
}
.modal .grid-2 input { width: 100%; }
.modal form .grid-2 { gap: 8px; margin-block: 8px; }

/* food results */
.results { margin-block-start: 8px; max-height: 50vh; overflow-y: auto; display: flex; flex-direction: column; gap: 8px; }
.results-section { display: flex; flex-direction: column; gap: 4px; }
.food-pick {
  display: block; width: 100%; text-align: start;
  background: var(--surface-2); border: 1px solid var(--border);
  border-radius: var(--radius-sm); padding: 10px; cursor: pointer; color: var(--text);
}
.food-pick:hover { background: var(--border); }
.food-pick-name { font-weight: 600; font-size: 14px; }

/* manual entry collapsible */
.manual-entry { margin-block-start: 12px; }
.manual-entry summary { cursor: pointer; padding: 10px 0; color: var(--accent); font-weight: 600; }
.manual-entry input { width: 100%; }

/* file input wrapper */
.file-label { cursor: pointer; }

/* RTL fixes for chevrons */
[dir="rtl"] .day-nav .icon-btn[data-act="prev-day"]::before { content: "‹"; }
[dir="rtl"] .day-nav .icon-btn[data-act="next-day"]::before { content: "›"; }

/* meal-row layout updated to include thumbnail column */
.meal-row { grid-template-columns: 40px 1fr auto auto; }
.meal-thumb {
  width: 40px; height: 40px; border-radius: 8px; object-fit: cover;
  background: var(--surface);
  border: 1px solid var(--border);
}
.meal-thumb.hidden { display: none; }
.meal-row:has(.meal-thumb.hidden) { grid-template-columns: 1fr auto auto; }

/* barcode scanner */
.scanner-wrap {
  position: relative;
  background: black;
  border-radius: var(--radius-sm);
  overflow: hidden;
  aspect-ratio: 4 / 3;
  display: flex; align-items: center; justify-content: center;
  margin-block: 8px;
}
.scanner-wrap video { width: 100%; height: 100%; object-fit: cover; }
.scan-status {
  position: absolute; inset-block-end: 8px; inset-inline: 0;
  text-align: center; color: white; padding: 4px;
  background: rgba(0,0,0,0.4);
}

/* iOS Shortcut setup steps */
.setup-steps {
  padding-inline-start: 18px;
  display: flex; flex-direction: column; gap: 6px;
  margin-block: 8px;
}
.setup-steps li { color: var(--muted); }

/* RTL: lists indent on the right */
[dir="rtl"] .setup-steps { padding-inline-start: 0; padding-inline-end: 18px; }

.meal-meta.good { color: var(--good); }

/* accent picker */
.accent-row {
  display: flex; gap: 10px; padding-block: 6px;
}
.accent-swatch {
  width: 36px; height: 36px; border-radius: 50%;
  border: 3px solid transparent; cursor: pointer; padding: 0;
  transition: transform .15s, border-color .15s;
}
.accent-swatch.active { border-color: var(--text); transform: scale(1.1); }
.accent-swatch:hover { transform: scale(1.05); }
.accent-swatch.accent-green  { background: #16a34a; }
.accent-swatch.accent-blue   { background: #2563eb; }
.accent-swatch.accent-purple { background: #9333ea; }
.accent-swatch.accent-orange { background: #ea580c; }
.accent-swatch.accent-pink   { background: #db2777; }

/* chart zoom modal */
.chart-zoom-modal { align-items: center; padding: 12px; }
.modal.chart-zoom {
  max-width: 760px; width: 100%; height: 90vh; max-height: 90vh;
  border-radius: var(--radius);
  display: flex; flex-direction: column; padding: 12px;
}
.chart-zoom-head { display: flex; justify-content: space-between; align-items: center; padding-block-end: 8px; }
.chart-zoom-head h2 { margin: 0; font-size: 16px; }
.chart-zoom-body { flex: 1; min-height: 0; }
.chart-zoom-body canvas { width: 100% !important; height: 100% !important; cursor: default; }
