/* Bite – same look as Purser: ezWallet/iOS colours and card shapes, standard web
   interaction. Compact layout, centred modals, sidebar on wide screens, and
   master/detail where the detail panel is "parked" inline instead of a modal. */

:root {
  --radius: 20px;
  --radius-sm: 10px;
  --gap: 12px;
  --sidebar-w: 11.5rem;

  /* The background sits close to the card colour, so cards are separated by a
     soft border rather than contrast (as in Purser). */
  --bg: #fbfbfd;
  --card: #ffffff;
  --card-border: rgba(60, 60, 67, 0.10);
  --card-shadow: 0 1px 2px rgba(0, 0, 0, 0.03), 0 4px 12px rgba(0, 0, 0, 0.04);
  --shadow-lg: 0 12px 40px rgba(0, 0, 0, 0.18);

  --accent: #007aff;
  --accent-soft: rgba(0, 122, 255, 0.10);
  --green: #34c759;
  --green-soft: rgba(52, 199, 89, 0.12);
  --red: #ff3b30;
  --red-soft: rgba(255, 59, 48, 0.10);
  --orange: #ff9500;
  --orange-soft: rgba(255, 149, 0, 0.12);

  /* Macros: Apple system colours, always shown with a text label. */
  --protein: #5856d6;
  --carbs: #ff9500;
  --fat: #ff2d55;

  --text: #1c1c1e;
  --secondary: rgba(60, 60, 67, 0.65);
  --tertiary: rgba(60, 60, 67, 0.35);
  --separator: rgba(60, 60, 67, 0.12);
  --track: rgba(60, 60, 67, 0.10);
  --field: #ffffff;
  --hover: rgba(60, 60, 67, 0.05);
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #1c1c1e;
    --card: #2c2c2e;
    --card-border: rgba(235, 235, 245, 0.10);
    --card-shadow: none;
    --shadow-lg: 0 12px 40px rgba(0, 0, 0, 0.6);

    --accent: #0a84ff;
    --accent-soft: rgba(10, 132, 255, 0.18);
    --green: #30d158;
    --green-soft: rgba(48, 209, 88, 0.16);
    --red: #ff453a;
    --red-soft: rgba(255, 69, 58, 0.16);
    --orange: #ff9f0a;
    --orange-soft: rgba(255, 159, 10, 0.16);

    --protein: #5e5ce6;
    --carbs: #ff9f0a;
    --fat: #ff375f;

    --text: #f2f2f7;
    --secondary: rgba(235, 235, 245, 0.62);
    --tertiary: rgba(235, 235, 245, 0.35);
    --separator: rgba(235, 235, 245, 0.12);
    --track: rgba(235, 235, 245, 0.12);
    --field: #3a3a3c; /* lifted above --card, or fields vanish into their card */
    --hover: rgba(255, 255, 255, 0.05);
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }
[x-cloak] { display: none !important; }
[hidden] { display: none !important; }

html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0;
  min-height: 100vh;
  overscroll-behavior-y: contain;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", system-ui, sans-serif;
  font-size: 14px;
  line-height: 1.45;
  -webkit-font-smoothing: antialiased;
}

h1 { font-size: 20px; margin: 0; letter-spacing: -0.01em; }
h2 { font-size: 15px; margin: 0; font-weight: 600; }
h3 { font-size: 13px; margin: 0 0 8px; color: var(--secondary); font-weight: 600; }
a { color: var(--accent); text-decoration: none; }
a:hover { text-decoration: underline; }
code { font-family: ui-monospace, SFMono-Regular, Menlo, monospace; font-size: 12px; }
.muted { color: var(--secondary); }
.small { font-size: 12px; }
.num { font-variant-numeric: tabular-nums; }
.row { display: flex; align-items: center; gap: 8px; }
.row.between { justify-content: space-between; }
.row.wrap { flex-wrap: wrap; }
.grow { flex: 1; min-width: 0; }
.ellipsis { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.stack > * + * { margin-top: var(--gap); }
.stack-sm > * + * { margin-top: 6px; }

/* --- Layout: top bar, sidebar, content ------------------------------------ */

.topbar {
  position: sticky;
  top: 0;
  z-index: 20;
  display: flex;
  align-items: center;
  gap: 8px;
  min-height: 48px;
  padding: 6px 16px;
  padding-top: calc(6px + env(safe-area-inset-top));
  background: var(--card);
  border-bottom: 1px solid var(--separator);
}
.topbar .spacer { flex: 1; }
.topbar .title { font-size: 15px; font-weight: 700; letter-spacing: -0.01em; }
.topbar .date-label { font-weight: 600; min-width: 7rem; text-align: center; }
.topbar .date-label .date-sub { font-weight: 400; color: var(--secondary); margin-left: 6px; }
@media (max-width: 44rem) { .topbar .date-label .date-sub { display: none; } }

.sidebar {
  position: fixed;
  top: 0; left: 0; bottom: 0;
  z-index: 30;
  width: var(--sidebar-w);
  padding: 10px 8px;
  padding-top: calc(10px + env(safe-area-inset-top));
  background: var(--card);
  border-right: 1px solid var(--separator);
  transform: translateX(-100%);
  transition: transform 0.2s ease;
  display: flex;
  flex-direction: column;
  gap: 1px;
}
.sidebar.open { transform: none; }
.sidebar-head {
  display: flex; align-items: center; gap: 8px;
  font-size: 14px; font-weight: 700; letter-spacing: -0.01em;
  padding: 6px 8px 10px;
}
.nav-item {
  display: flex; align-items: center; gap: 8px;
  border-radius: 7px; padding: 7px 8px;
  font-size: 13px; font-weight: 500; color: var(--text);
}
.nav-item:hover { background: var(--hover); text-decoration: none; }
.nav-item svg { width: 16px; height: 16px; flex: none; opacity: 0.8; }
.nav-item.active { background: var(--accent); color: #fff; font-weight: 600; }
.nav-item.active svg { opacity: 1; }
.sidebar .foot { margin-top: auto; padding: 8px; font-size: 12px; color: var(--secondary); }

.scrim { position: fixed; inset: 0; z-index: 29; background: rgba(0, 0, 0, 0.35); }

.brand-icon {
  width: 22px; height: 22px; border-radius: 6px; background: var(--accent);
  color: #fff; display: inline-grid; place-items: center; font-weight: 800; font-size: 13px;
}

main {
  padding: 16px;
  padding-bottom: calc(24px + env(safe-area-inset-bottom));
  max-width: 44rem;
  margin: 0 auto;
}
main.wide { max-width: 80rem; }
main.narrow { max-width: 24rem; padding-top: 12vh; }

@media (min-width: 64rem) {
  .sidebar { transform: none; }
  .scrim { display: none !important; }
  .topbar, .page { margin-left: var(--sidebar-w); }
  .menu-btn { display: none; }
  main { padding: 20px 24px; }
}
@media (prefers-reduced-motion: reduce) { .sidebar { transition: none; } }

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

.card {
  background: var(--card);
  border: 1px solid var(--card-border);
  border-radius: var(--radius);
  box-shadow: var(--card-shadow);
  padding: 14px 16px;
}
.card + .card { margin-top: var(--gap); }
.card-head { display: flex; align-items: center; gap: 8px; margin-bottom: 8px; }
.card-head h2 { flex: 1; }
.card.flush { padding: 0; overflow: hidden; }
.card-nested { box-shadow: none; border: 1px solid var(--separator); background: transparent; }

/* --- Buttons, chips, fields -------------------------------------------------- */

button, .btn {
  font: inherit;
  font-size: 13px;
  font-weight: 500;
  color: var(--text);
  background: var(--card);
  border: 1px solid var(--separator);
  border-radius: 8px;
  padding: 5px 11px;
  min-height: 30px;
  cursor: pointer;
  white-space: nowrap;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  text-decoration: none;
}
button:hover, .btn:hover { background: var(--hover); text-decoration: none; }
button:focus-visible, .btn:focus-visible, input:focus-visible, select:focus-visible,
textarea:focus-visible, a:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 1px;
}
.btn-primary {
  background: var(--accent); border-color: var(--accent); color: #fff; font-weight: 600;
}
.btn-primary:hover { background: var(--accent); filter: brightness(1.08); }
.btn-ghost, .quiet { border-color: transparent; background: none; color: var(--secondary); }
.btn-ghost:hover, .quiet:hover { background: var(--hover); color: var(--text); }
.btn-danger { color: var(--red); }
.btn-block { width: 100%; }
.btn-lg { min-height: 38px; font-size: 14px; padding: 8px 14px; }
.btn-small { min-height: 26px; padding: 3px 9px; font-size: 12px; }
.icon-btn { min-width: 30px; padding: 4px 8px; font-size: 16px; line-height: 1; }
button:disabled, .btn.disabled { opacity: 0.5; cursor: default; pointer-events: none; }

.chip {
  display: inline-flex; align-items: center; gap: 6px;
  padding: 4px 10px; min-height: 28px;
  border-radius: 999px; border: 1px solid var(--separator);
  background: var(--card); font-size: 12px; font-weight: 500; cursor: pointer;
}
.chip.active { background: var(--accent-soft); border-color: var(--accent); color: var(--accent); }
.chips { display: flex; flex-wrap: wrap; gap: 6px; }
.badge {
  font-size: 11px; font-weight: 500; padding: 1px 7px; border-radius: 999px;
  background: var(--track); color: var(--secondary); white-space: nowrap;
}
.badge.ok { background: var(--green-soft); color: var(--green); }
.badge.warn { background: var(--orange-soft); color: var(--orange); }

.segmented {
  display: flex; gap: 2px; padding: 2px;
  background: var(--track); border-radius: 9px;
}
.segmented button {
  flex: 1; border: 0; background: transparent; min-height: 26px;
  font-size: 12px; border-radius: 7px; color: var(--secondary);
}
.segmented button.active {
  background: var(--card); color: var(--text); font-weight: 600;
  box-shadow: 0 1px 2px rgba(0, 0, 0, 0.12);
}

.field + .field, .fields-2 + .field, .field + .fields-2, .fields-2 + .fields-2 { margin-top: 10px; }
label, .label { display: block; font-size: 12px; color: var(--secondary); margin-bottom: 3px; }
input, select, textarea {
  font: inherit;
  width: 100%;
  padding: 6px 10px;
  min-height: 32px;
  border-radius: 8px;
  border: 1px solid var(--separator);
  background: var(--field);
  color: var(--text);
}
input[type="checkbox"] { width: auto; min-height: 0; accent-color: var(--accent); }
.fields-2 { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; align-items: end; }
.fields-3 { display: grid; grid-template-columns: repeat(3, 1fr); gap: 10px; align-items: end; }
.fields-2 > .field + .field, .fields-3 > .field + .field { margin-top: 0; }

/* Phones: 16px inputs stop iOS from zooming in on focus. */
@media (max-width: 44rem) {
  input, select, textarea { font-size: 16px; min-height: 38px; }
  button, .btn { min-height: 34px; }
}

.alert { padding: 8px 12px; border-radius: var(--radius-sm); font-size: 13px; }
.alert-error { background: var(--red-soft); color: var(--red); }
.alert-ok { background: var(--green-soft); color: var(--green); }
.alert-warn { background: var(--orange-soft); color: var(--orange); }

/* --- Lists and tables -------------------------------------------------------- */

.list { background: var(--card); border: 1px solid var(--card-border); border-radius: 14px; overflow: hidden; }
.list-item {
  display: flex; align-items: center; gap: 10px; width: 100%;
  padding: 8px 12px; min-height: 44px; border: 0; border-radius: 0;
  background: transparent; text-align: left; font-size: 13px; white-space: normal;
}
.list-item + .list-item { border-top: 1px solid var(--separator); }
.list-item:hover { background: var(--hover); }
.list-item.selected { background: var(--accent-soft); }
.list-item .sub { font-size: 12px; color: var(--secondary); }

.nutri-table { width: 100%; border-collapse: collapse; font-size: 13px; }
.nutri-table td { padding: 5px 0; border-bottom: 1px solid var(--separator); }
.nutri-table td:last-child { text-align: right; font-variant-numeric: tabular-nums; }
.nutri-table tr.sub td:first-child { padding-left: 14px; color: var(--secondary); }

/* --- Bars and day summary ------------------------------------------------------- */

.bar { height: 8px; background: var(--track); border-radius: 999px; overflow: hidden; }
.bar > span { display: block; height: 100%; background: var(--green); border-radius: 999px; transition: width 0.3s; }
.bar.over > span { background: var(--red); }
@media (prefers-reduced-motion: reduce) { .bar > span { transition: none; } }

.summary .kcal-label { color: var(--secondary); font-size: 12px; }
.macros { display: grid; grid-template-columns: repeat(3, 1fr); gap: 12px; margin-top: 14px; }
.macro .name { font-size: 12px; color: var(--secondary); }
.macro .val { font-weight: 600; font-size: 13px; }
.macro .bar { height: 6px; margin-top: 4px; }
.macro.protein .bar > span { background: var(--protein); }
.macro.carbs .bar > span { background: var(--carbs); }
.macro.fat .bar > span { background: var(--fat); }

.meal-head { display: flex; align-items: center; gap: 8px; padding: 10px 12px 10px 16px; }
.meal-head h2 { flex: 1; }
.meal-head .kcal { color: var(--secondary); font-size: 12px; }
.entry {
  display: flex; align-items: center; gap: 10px; width: 100%;
  padding: 8px 16px; border: 0; border-top: 1px solid var(--separator); border-radius: 0;
  background: transparent; text-align: left; min-height: 48px; white-space: normal;
}
.entry:hover { background: var(--hover); }
.entry.selected { background: var(--accent-soft); }
.entry .name { font-weight: 500; font-size: 13px; }
.entry .detail { font-size: 12px; color: var(--secondary); }
.entry .kcal { white-space: nowrap; font-variant-numeric: tabular-nums; font-size: 13px; }

/* --- Master/detail with modal fallback ---------------------------------------------
   `.detail-host` holds the panels (`.sheet`). On narrow screens it is a centred
   modal over a scrim while open; from 56rem it becomes the right-hand column and
   the panel is shown inline ("parked"), as in Purser. */

.split { display: flex; gap: 16px; align-items: flex-start; }
.split-master { flex: 1; min-width: 0; }
.split-master > * + * { margin-top: var(--gap); }

.detail-host {
  position: fixed; inset: 0; z-index: 40;
  background: rgba(0, 0, 0, 0.35);
  display: none; align-items: center; justify-content: center; padding: 16px;
}
.detail-host.open { display: flex; }

.sheet {
  background: var(--card);
  border: 1px solid var(--card-border);
  border-radius: var(--radius);
  box-shadow: var(--shadow-lg);
  width: 100%;
  max-width: 30rem;
  max-height: 86vh;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}
.sheet-head {
  display: flex; align-items: center; gap: 6px;
  padding: 10px 10px 10px 16px; border-bottom: 1px solid var(--separator);
}
.sheet-head h2 { flex: 1; }
.sheet-body { flex: 1; overflow-y: auto; padding: 14px 16px; -webkit-overflow-scrolling: touch; }
.sheet-foot {
  display: flex; gap: 8px; padding: 10px 16px;
  padding-bottom: calc(10px + env(safe-area-inset-bottom));
  border-top: 1px solid var(--separator);
}
.sheet-foot .grow { flex: 1; }

@media (min-width: 56rem) {
  .split-master.narrow { flex: 0 0 26%; min-width: 17rem; }
  .detail-host {
    position: sticky; top: 64px; inset: auto; z-index: auto;
    display: block; background: none; padding: 0;
    /* Fixed width (and min-width 0) so long names in a panel can't widen it and
       make the layout jump between panels. */
    flex: 0 0 auto; width: min(28rem, 42%); min-width: 0;
  }
  /* Next to a narrow list (Purser's 26 % master), the detail takes the rest. */
  .split-master.narrow + .detail-host { flex: 1 1 auto; width: auto; max-width: 42rem; }
  .detail-host.open { display: block; }
  .detail-host .sheet { box-shadow: none; max-width: none; max-height: calc(100vh - 88px); }
  .modal-only { display: none !important; }
}
@media (max-width: 55.99rem) {
  .inline-only { display: none !important; }
}

/* --- Small dropdown menu (⋯) ------------------------------------------------------ */

.menu { position: relative; }
.menu-list {
  position: absolute; right: 0; top: calc(100% + 4px); z-index: 15; min-width: 13rem;
  background: var(--card); border: 1px solid var(--card-border); border-radius: 12px;
  box-shadow: var(--shadow-lg); padding: 4px; display: flex; flex-direction: column;
}
.menu-list button {
  justify-content: flex-start; border: 0; background: none; border-radius: 8px;
  padding: 7px 10px; font-size: 13px; width: 100%;
}
.menu-list button:hover { background: var(--hover); }
.card.flush { overflow: visible; }
.card.flush > .entry:last-child { border-radius: 0 0 var(--radius) var(--radius); }

/* --- Scanner, spinner, toast ------------------------------------------------------ */

.scanner { position: relative; background: #000; border-radius: 14px; overflow: hidden; aspect-ratio: 4 / 3; }
.scanner video { width: 100%; height: 100%; object-fit: cover; display: block; }
.scanner .frame {
  position: absolute; left: 12%; right: 12%; top: 32%; bottom: 32%;
  border: 2px solid rgba(255, 255, 255, 0.85); border-radius: 10px;
  box-shadow: 0 0 0 999px rgba(0, 0, 0, 0.35);
}

.spinner {
  width: 14px; height: 14px; border-radius: 50%;
  border: 2px solid var(--separator); border-top-color: var(--accent);
  animation: spin 0.8s linear infinite; display: inline-block; flex: none;
}
@keyframes spin { to { transform: rotate(360deg); } }

.toast {
  position: fixed; left: 50%; transform: translateX(-50%);
  bottom: calc(20px + env(safe-area-inset-bottom)); z-index: 60;
  background: var(--text); color: var(--bg); padding: 8px 14px; border-radius: 999px;
  font-size: 13px; box-shadow: var(--shadow-lg); max-width: 90vw;
}

/* --- History ------------------------------------------------------------------------ */

.chart svg { width: 100%; height: auto; display: block; touch-action: pan-y; cursor: crosshair; }
.chart svg { height: 220px; }
.chart .axis { font-size: 11px; fill: var(--secondary); }
.stats { display: grid; grid-template-columns: repeat(2, 1fr); gap: 12px; }
.stat .val { font-size: 20px; font-weight: 700; }
.stat .lbl { font-size: 12px; color: var(--secondary); }
.history-grid { display: grid; gap: var(--gap); }
.history-grid > .card + .card { margin-top: 0; }
@media (min-width: 64rem) {
  .history-grid { grid-template-columns: 2fr 1fr; align-items: start; }
  .history-grid .span-all { grid-column: 1 / -1; }
}

/* --- Settings: cards in two columns on wide screens -------------------------------- */

.settings-grid > .card { break-inside: avoid; margin: 0 0 var(--gap); }
@media (min-width: 64rem) {
  .settings-grid { columns: 2; column-gap: var(--gap); }
}

/* --- Login / setup ------------------------------------------------------------------ */

.brand { display: flex; align-items: center; gap: 10px; margin-bottom: 16px; }
.brand .brand-icon { width: 36px; height: 36px; border-radius: 10px; font-size: 18px; }

@media (max-width: 520px) {
  .inline-form > strong { flex: 1; }
  .inline-form > .grow { order: 3; flex-basis: 100%; }
}
.kcal-total { font-size: 20px; font-weight: 700; letter-spacing: -0.01em; }
.kcal-total.neg { color: var(--red); }
.meal-toggle {
  display: inline-flex; align-items: center; gap: 6px; padding: 0; min-height: 0;
  border: 0; background: none; font: inherit; color: inherit;
}
.meal-toggle:hover { background: none; }
.meal-toggle .chevron { color: var(--secondary); font-size: 12px; width: 12px; }
