/* ============ Design tokens ============ */
/* Dark is the default (static HTML ships data-theme="dark"; also honors prefers-color-scheme via JS). */
:root, [data-theme="dark"] {
  --bg: #16181d;
  --surface: #1d2027;
  --surface-2: #232730;
  --border: #333947;
  --text: #eceef2;
  --text-muted: #a2a9b8;
  --accent: #4f9d69;
  --accent-contrast: #0b1f12;
  --danger: #e2695f;
  --radius: 12px;
  --shadow: 0 12px 32px rgba(0, 0, 0, .45);
}

/* Light: clean white, no gimmicks */
[data-theme="light"] {
  --bg: #f7f8fa;
  --surface: #ffffff;
  --surface-2: #eff1f4;
  --border: #d9dde4;
  --text: #1b1e24;
  --text-muted: #5d6675;
  --accent: #2e7d43;
  --accent-contrast: #ffffff;
  --danger: #b3423a;
  --shadow: 0 10px 28px rgba(25, 30, 40, .12);
}

/* ============ Base ============ */
* { box-sizing: border-box; }
html { height: 100%; }
body {
  margin: 0;
  min-height: 100%;
  background: var(--bg);
  color: var(--text);
  font: 16px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  padding-bottom: calc(76px + env(safe-area-inset-bottom));
  -webkit-tap-highlight-color: transparent;
}
h1, h2, h3 { line-height: 1.2; }
button { font: inherit; }
.muted { color: var(--text-muted); font-size: .9rem; }
.hidden { display: none !important; }

.skip-link {
  position: absolute;
  left: -9999px;
  background: var(--accent);
  color: var(--accent-contrast);
  padding: .5rem 1rem;
  border-radius: 0 0 8px 0;
  z-index: 100;
}
.skip-link:focus { left: 0; }

/* ============ Header ============ */
.app-header {
  padding: .9rem 1rem .4rem;
  display: flex;
  align-items: baseline;
  gap: .6rem;
  flex-wrap: wrap;
}
.app-title { font-size: 1.25rem; margin: 0; }
.mode-indicator { margin: 0; font-size: .8rem; }
.mode-indicator::before { content: "· "; }

/* ============ Views ============ */
main { padding: 0 .8rem; }
.view { display: none; }
.view.is-active { display: block; animation: fade .18s ease-out; }
@keyframes fade { from { opacity: 0; transform: translateY(4px); } to { opacity: 1; transform: none; } }

.view-head h2 { margin: .4rem 0 .1rem; font-size: 1.2rem; }
.view-head p { margin: .1rem 0 .6rem; }

/* ============ Buttons ============ */
.btn {
  display: inline-flex;
  align-items: center;
  gap: .4rem;
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text);
  border-radius: 10px;
  padding: .55rem .9rem;
  cursor: pointer;
  min-height: 44px;
}
.btn:hover { background: var(--surface-2); }
.btn:focus-visible, .tab:focus-visible, .slot:focus-visible,
.seg-btn:focus-visible, .recipe-pick:focus-visible, input:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}
.btn-primary {
  background: var(--accent);
  color: var(--accent-contrast);
  border-color: transparent;
  font-weight: 600;
}
.btn-primary:hover { filter: brightness(1.08); background: var(--accent); }
.btn-ghost { background: transparent; }
.btn-ghost:hover { background: var(--surface-2); }
.btn[disabled] { opacity: .55; cursor: not-allowed; }

.actions-row {
  display: flex;
  flex-wrap: wrap;
  gap: .5rem;
  margin: .4rem 0 .8rem;
}

/* ============ Forms ============ */
.goal-form, .settings-group {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: .9rem;
  margin: 0 0 .9rem;
}
.goal-form h3, .settings-group h3 { margin: 0 0 .6rem; font-size: 1rem; }
.form-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(150px, 1fr));
  gap: .7rem;
  margin-bottom: .7rem;
}
.goal-form label, .settings-row label { display: flex; flex-direction: column; gap: .25rem; font-size: .85rem; }
.goal-form input, .settings-row input {
  background: var(--surface-2);
  border: 1px solid var(--border);
  color: var(--text);
  border-radius: 8px;
  padding: .5rem;
  font: inherit;
  min-height: 44px;
}
.form-status { min-height: 1.2em; margin: .4rem 0 0; font-size: .9rem; color: var(--accent); }
.form-status.error { color: var(--danger); }

/* ============ Week grid ============ */
.table-wrap { overflow-x: auto; }
.week-grid {
  width: 100%;
  border-collapse: collapse;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  overflow: hidden;
}
.week-grid th, .week-grid td { border: 1px solid var(--border); }
.week-grid thead th {
  background: var(--surface-2);
  font-size: .85rem;
  padding: .5rem .4rem;
  text-align: left;
}
.week-grid .day-col { width: 92px; }
.day-name { font-size: .85rem; font-weight: 600; padding: .5rem; white-space: nowrap; }
.slot-btn {
  width: 100%;
  min-height: 64px;
  border: 0;
  background: transparent;
  color: var(--text);
  padding: .45rem;
  text-align: left;
  cursor: pointer;
  display: flex;
  flex-direction: column;
  gap: .15rem;
}
.slot-btn:hover { background: var(--surface-2); }
.slot-empty::before { content: "+"; font-size: 1.2rem; color: var(--text-muted); }
.slot-empty { color: var(--text-muted); font-size: .85rem; align-items: flex-start; }
.slot-title { font-weight: 600; font-size: .85rem; }
.slot-kcal { font-size: .75rem; color: var(--text-muted); }
.slot-kcal.macro-line { }
.totals-line { margin: .5rem 0; }

/* ============ Plan detail ============ */
.plan-day {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: .7rem .9rem;
  margin-bottom: .6rem;
}
.plan-day h3 { margin: 0 0 .4rem; font-size: .95rem; }
.plan-meal { display: flex; justify-content: space-between; gap: .6rem; font-size: .9rem; padding: .2rem 0; }
.plan-meal .kcal { color: var(--text-muted); white-space: nowrap; }
.plan-day-total {
  border-top: 1px dashed var(--border);
  margin-top: .4rem;
  padding-top: .4rem;
  font-size: .85rem;
  color: var(--text-muted);
  display: flex;
  justify-content: space-between;
}

/* ============ Shopping list ============ */
.aisle {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: .7rem .9rem;
  margin-bottom: .6rem;
}
.aisle h3 { margin: 0 0 .4rem; font-size: .95rem; display: flex; justify-content: space-between; align-items: baseline; gap: .5rem; }
.aisle-count { color: var(--text-muted); font-size: .8rem; font-weight: 400; }
.aisle ul { list-style: none; margin: 0; padding: 0; }
.item-row { display: flex; align-items: center; gap: .6rem; padding: .3rem 0; }
.item-row input[type="checkbox"] {
  width: 20px;
  height: 20px;
  accent-color: var(--accent);
  flex: none;
}
.item-label { flex: 1; }
.item-label .qty { color: var(--text-muted); font-size: .85rem; margin-left: .3rem; }
.item-row input[type="checkbox"]:checked ~ .item-label { text-decoration: line-through; opacity: .6; }
.check-head { display: flex; justify-content: space-between; align-items: center; gap: .5rem; }
.progress-line { font-size: .8rem; color: var(--text-muted); }

/* ============ Settings ============ */
.settings-row {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: .8rem;
  padding: .4rem 0;
  flex-wrap: wrap;
}
.settings-row label { flex-direction: row; align-items: center; }
.settings-row .hint-line { flex-basis: 100%; margin: 0; }
.seg {
  display: inline-flex;
  border: 1px solid var(--border);
  border-radius: 10px;
  overflow: hidden;
}
.seg-btn {
  border: 0;
  background: transparent;
  color: var(--text);
  padding: .45rem .9rem;
  min-height: 44px;
  cursor: pointer;
}
.seg-btn[aria-checked="true"] { background: var(--accent); color: var(--accent-contrast); font-weight: 600; }
.seg-btn + .seg-btn { border-left: 1px solid var(--border); }
#notif-time { min-width: 110px; }

/* ============ Slot picker dialog ============ */
.dialog-backdrop {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, .55);
  display: flex;
  align-items: flex-end;
  justify-content: center;
  z-index: 50;
  padding: 0;
}
.dialog {
  background: var(--surface);
  color: var(--text);
  width: 100%;
  max-height: 80vh;
  border-radius: 16px 16px 0 0;
  box-shadow: var(--shadow);
  display: flex;
  flex-direction: column;
}
.dialog-head {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: .5rem;
  padding: .8rem 1rem;
  border-bottom: 1px solid var(--border);
}
.dialog-head h3 { margin: 0; font-size: 1rem; }
.dialog-body { overflow-y: auto; padding: .5rem .6rem 1rem; }
.recipe-pick {
  width: 100%;
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: .6rem;
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: 10px;
  color: var(--text);
  padding: .6rem .8rem;
  margin: .35rem 0;
  cursor: pointer;
  text-align: left;
  min-height: 48px;
}
.recipe-pick:hover { border-color: var(--accent); }
.recipe-kcal { color: var(--text-muted); font-size: .85rem; white-space: nowrap; }

@media (min-width: 640px) {
  .dialog-backdrop { align-items: center; padding: 1rem; }
  .dialog { max-width: 460px; border-radius: 16px; }
}

/* ============ Tab nav ============ */
.tab-nav {
  position: fixed;
  left: 0;
  right: 0;
  bottom: 0;
  display: flex;
  background: var(--surface);
  border-top: 1px solid var(--border);
  padding-bottom: env(safe-area-inset-bottom);
  z-index: 40;
}
.tab {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 2px;
  padding: .45rem 0 .5rem;
  background: transparent;
  border: 0;
  color: var(--text-muted);
  cursor: pointer;
  min-height: 56px;
}
.tab-icon { font-size: 1.25rem; }
.tab-label { font-size: .7rem; }
.tab[aria-current="page"] { color: var(--accent); font-weight: 600; }
.tab:hover { color: var(--text); }
.tab[aria-current="page"]:hover { color: var(--accent); }

@media (prefers-reduced-motion: reduce) {
  * { animation: none !important; transition: none !important; }
}


/* ---- Profile wizard ---- */
.btn-accent { background: var(--accent, #4a7c59); color: #fff; border: none; padding: .55rem 1rem; border-radius: 10px; font-weight: 600; }
.wizard-overlay { position: fixed; inset: 0; background: rgba(0,0,0,.55); display: flex; align-items: flex-end; justify-content: center; z-index: 50; }
.wizard-card { background: var(--bg-card, var(--bg, #fff)); color: var(--text, inherit); width: 100%; border-radius: 16px 16px 0 0; padding: 1.25rem 1rem calc(1rem + env(safe-area-inset-bottom)); box-shadow: 0 -8px 30px rgba(0,0,0,.3); }
.wizard-card h3 { margin: 0 0 .75rem; }
.wizard-q p { font-weight: 600; margin: .5rem 0; }
.choices { display: flex; flex-wrap: wrap; gap: .5rem; }
.choices button { flex: 1 1 auto; min-width: 90px; padding: .6rem .8rem; border-radius: 10px; border: 1.5px solid var(--border, #ccc); background: transparent; color: inherit; font-size: .95rem; cursor: pointer; }
.choices button.is-selected { border-color: var(--accent, #4a7c59); background: var(--accent-soft, rgba(74,124,89,.15)); font-weight: 600; }
.choices button:focus-visible { outline: 2px solid var(--accent, #4a7c59); outline-offset: 2px; }
.wizard-nav { display: flex; align-items: center; justify-content: space-between; margin-top: 1rem; }
#wizard-step-label { opacity: .7; font-size: .85rem; }
@media (min-width: 640px) { .wizard-overlay { align-items: center; } .wizard-card { border-radius: 16px; max-width: 460px; } }
