:root {
  --bg: #f3f5f2;
  --card: #ffffff;
  --card-2: #f7f8f6;
  --text: #17201b;
  --muted: #66716b;
  --line: #e3e7e2;
  --accent: #1f9d6b;
  --accent-ink: #ffffff;
  --accent-soft: #e2f4ec;
  --carb: #e6a23c;
  --protein: #3f7fd9;
  --fat: #d9577a;
  --warn: #d9534f;
  --track: #e8ece7;
  --shadow: 0 1px 2px rgb(0 0 0 / 0.04), 0 4px 16px rgb(0 0 0 / 0.04);
  --radius: 18px;
  --tabbar-h: 58px;
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #101412;
    --card: #1a201d;
    --card-2: #222924;
    --text: #eef2ef;
    --muted: #9aa59f;
    --line: #2c3430;
    --accent: #35c08a;
    --accent-ink: #062016;
    --accent-soft: #173a2b;
    --carb: #f0b65a;
    --protein: #6aa3f0;
    --fat: #ef7d9a;
    --warn: #f07470;
    --track: #2a322e;
    --shadow: none;
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 16px/1.4 -apple-system, BlinkMacSystemFont, "SF Pro Text", "Segoe UI", Roboto, sans-serif;
  -webkit-tap-highlight-color: transparent;
  min-height: 100dvh;
}
body.locked { overflow: hidden; }
button, input, select, textarea { font: inherit; color: inherit; }
button { cursor: pointer; }
h1, h2, h3 { margin: 0; line-height: 1.2; }
svg { display: block; }

/* Topp og navigasjon */
.topbar {
  position: sticky; top: 0; z-index: 5;
  display: flex; align-items: center; justify-content: space-between; gap: 8px;
  padding: calc(env(safe-area-inset-top) + 10px) 16px 10px;
  background: color-mix(in srgb, var(--bg) 88%, transparent);
  backdrop-filter: blur(12px); -webkit-backdrop-filter: blur(12px);
  min-height: 56px;
}
.topbar h1 { font-size: 22px; font-weight: 700; }
.datenav { display: flex; align-items: center; justify-content: center; gap: 4px; width: 100%; }
.datenav .label { font-weight: 700; font-size: 18px; min-width: 130px; text-align: center; border: 0; background: none; padding: 6px; }
.icon-btn {
  display: inline-grid; place-items: center;
  width: 40px; height: 40px; border-radius: 50%;
  border: 0; background: transparent; color: var(--text);
}
.icon-btn:active { background: var(--track); }
.icon-btn svg { width: 22px; height: 22px; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }

.view {
  max-width: 720px; margin: 0 auto;
  padding: 4px 16px calc(var(--tabbar-h) + env(safe-area-inset-bottom) + 24px);
}

.tabbar {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 10;
  display: grid; grid-template-columns: repeat(4, 1fr);
  padding: 6px 8px calc(env(safe-area-inset-bottom) + 4px);
  background: color-mix(in srgb, var(--card) 92%, transparent);
  backdrop-filter: blur(14px); -webkit-backdrop-filter: blur(14px);
  border-top: 1px solid var(--line);
}
.tabbar button {
  display: grid; justify-items: center; gap: 2px;
  border: 0; background: none; color: var(--muted);
  font-size: 11px; font-weight: 600; padding: 4px 0;
}
.tabbar svg { width: 24px; height: 24px; fill: none; stroke: currentColor; stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round; }
.tabbar button[aria-current="page"] { color: var(--accent); }

/* Kort */
.card {
  background: var(--card); border-radius: var(--radius);
  box-shadow: var(--shadow); padding: 16px; margin-bottom: 12px;
}
.card h2 { font-size: 17px; }
.card-head { display: flex; align-items: center; justify-content: space-between; gap: 8px; }
.muted { color: var(--muted); }
.small { font-size: 13px; }
.num { font-variant-numeric: tabular-nums; }

/* Sammendrag */
.summary { display: grid; grid-template-columns: 1fr auto 1fr; align-items: center; gap: 8px; text-align: center; }
.summary .side strong { display: block; font-size: 20px; }
.summary .side span { font-size: 13px; color: var(--muted); }
.ring { position: relative; width: 148px; height: 148px; }
.ring svg { width: 100%; height: 100%; transform: rotate(-90deg); }
.ring circle { fill: none; stroke-width: 12; }
.ring .track { stroke: var(--track); }
.ring .bar { stroke: var(--accent); stroke-linecap: round; transition: stroke-dashoffset 0.5s ease; }
.ring.over .bar { stroke: var(--warn); }
.ring .center { position: absolute; inset: 0; display: grid; place-content: center; text-align: center; }
.ring .center strong { font-size: 30px; line-height: 1; }
.ring .center span { font-size: 13px; color: var(--muted); margin-top: 4px; }

.macros { display: grid; grid-template-columns: repeat(3, 1fr); gap: 14px; margin-top: 16px; }
.macro .name { font-size: 13px; font-weight: 600; }
.macro .val { font-size: 13px; color: var(--muted); }
.meter { height: 6px; border-radius: 3px; background: var(--track); overflow: hidden; margin: 6px 0 4px; }
.meter i { display: block; height: 100%; border-radius: 3px; background: var(--c, var(--accent)); max-width: 100%; }
.meter.over i { background: var(--warn); }

.notice {
  display: flex; align-items: center; gap: 12px;
  background: var(--accent-soft); border-radius: var(--radius); padding: 12px 14px; margin-bottom: 12px;
  font-size: 14px;
}
.notice button { margin-left: auto; flex-shrink: 0; }

/* Måltider */
.meal .card-head { margin-bottom: 4px; }
.meal .kcal { color: var(--muted); font-size: 14px; }
.add-btn {
  display: inline-grid; place-items: center; width: 34px; height: 34px; border-radius: 50%;
  border: 0; background: var(--accent); color: var(--accent-ink); flex-shrink: 0;
}
.add-btn svg { width: 18px; height: 18px; stroke: currentColor; stroke-width: 2.5; stroke-linecap: round; fill: none; }

.list { list-style: none; margin: 0; padding: 0; }
.row {
  display: flex; align-items: center; gap: 12px; width: 100%;
  padding: 10px 0; border: 0; border-top: 1px solid var(--line); background: none; text-align: left;
}
.list > li:first-child > .row { border-top: 0; }
.row .main { flex: 1; min-width: 0; }
.row .title { display: block; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-weight: 500; }
.row .sub { display: block; font-size: 13px; color: var(--muted); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.row .end { font-size: 14px; color: var(--muted); white-space: nowrap; }
.empty { color: var(--muted); font-size: 14px; padding: 6px 0 2px; }

/* Knapper og skjema */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  min-height: 46px; padding: 0 18px; border-radius: 14px; border: 0;
  background: var(--accent); color: var(--accent-ink); font-weight: 650;
}
.btn.block { width: 100%; }
.btn.secondary { background: var(--card-2); color: var(--text); border: 1px solid var(--line); }
.btn.danger { background: transparent; color: var(--warn); border: 1px solid var(--line); }
.btn:disabled { opacity: 0.5; }
.link { border: 0; background: none; color: var(--accent); font-weight: 600; padding: 8px 4px; }
.pill {
  border: 0; border-radius: 999px; padding: 8px 14px; font-weight: 600;
  background: var(--accent-soft); color: var(--text);
}

.field { display: grid; gap: 6px; margin-bottom: 12px; }
.field > span { font-size: 13px; font-weight: 600; color: var(--muted); }
.input, select.input {
  width: 100%; min-height: 46px; padding: 10px 12px;
  border: 1px solid var(--line); border-radius: 12px; background: var(--card-2);
  font-size: 16px; /* hindrer zoom på iOS */
}
.input:focus { outline: 2px solid var(--accent); outline-offset: -1px; }
.grid2 { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.grid3 { display: grid; grid-template-columns: repeat(3, 1fr); gap: 10px; }
.actions { display: grid; gap: 10px; margin-top: 8px; }

.seg { display: flex; background: var(--track); border-radius: 12px; padding: 3px; gap: 3px; margin-bottom: 12px; }
.seg button { flex: 1; border: 0; background: none; border-radius: 9px; padding: 8px 4px; font-size: 14px; font-weight: 600; color: var(--muted); }
.seg button[aria-selected="true"] { background: var(--card); color: var(--text); box-shadow: var(--shadow); }

/* Ark (bottom sheets) */
.sheet-wrap { position: fixed; inset: 0; z-index: 20; }
.sheet-backdrop { position: absolute; inset: 0; background: rgb(0 0 0 / 0.35); opacity: 0; transition: opacity 0.2s; }
.sheet {
  position: absolute; left: 0; right: 0; bottom: 0; margin: 0 auto; max-width: 640px;
  max-height: calc(100dvh - env(safe-area-inset-top) - 24px);
  display: flex; flex-direction: column;
  background: var(--bg); border-radius: 20px 20px 0 0;
  transform: translateY(100%); transition: transform 0.25s ease;
  padding-bottom: env(safe-area-inset-bottom);
}
.sheet.full { height: calc(100dvh - env(safe-area-inset-top) - 24px); }
.sheet-wrap.open .sheet { transform: none; }
.sheet-wrap.open .sheet-backdrop { opacity: 1; }
.sheet-head {
  display: grid; grid-template-columns: 1fr auto 1fr; align-items: center;
  padding: 10px 12px 6px;
}
.sheet-head h2 { font-size: 17px; text-align: center; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; max-width: 60vw; }
.sheet-head > :first-child { justify-self: start; }
.sheet-head > :last-child { justify-self: end; }
.sheet-body { overflow-y: auto; padding: 6px 16px 20px; -webkit-overflow-scrolling: touch; overscroll-behavior: contain; }

.searchbar { display: flex; gap: 8px; padding: 0 16px 10px; }
.searchbar .input { flex: 1; }
.searchbar .icon-btn { width: 46px; height: 46px; border-radius: 12px; background: var(--accent); color: var(--accent-ink); }
.section-label { font-size: 13px; font-weight: 700; color: var(--muted); text-transform: uppercase; letter-spacing: 0.04em; margin: 14px 0 4px; }
.src-tag { font-size: 11px; font-weight: 700; padding: 2px 6px; border-radius: 6px; background: var(--track); color: var(--muted); margin-right: 4px; }

.food-title { font-size: 20px; font-weight: 700; }
.nutri { display: grid; grid-template-columns: repeat(4, 1fr); gap: 8px; text-align: center; margin: 14px 0; }
.nutri div { background: var(--card); border-radius: 12px; padding: 10px 4px; }
.nutri strong { display: block; font-size: 18px; }
.nutri span { font-size: 12px; color: var(--muted); }
.star { color: var(--muted); }
.star[aria-pressed="true"] { color: #f0b429; }
.star[aria-pressed="true"] svg { fill: currentColor; }

/* Skanner */
.scanner { position: fixed; inset: 0; z-index: 40; background: #000; color: #fff; }
.scanner video { position: absolute; inset: 0; width: 100%; height: 100%; object-fit: cover; }
.scanner-frame {
  position: absolute; left: 50%; top: 42%; width: min(78vw, 360px); height: min(40vw, 190px);
  transform: translate(-50%, -50%); border: 3px solid rgb(255 255 255 / 0.9); border-radius: 16px;
  box-shadow: 0 0 0 100vmax rgb(0 0 0 / 0.45);
}
.scanner-top { position: absolute; top: calc(env(safe-area-inset-top) + 12px); right: 16px; }
.scanner-bottom {
  position: absolute; left: 0; right: 0; bottom: 0;
  padding: 16px 16px calc(env(safe-area-inset-bottom) + 20px); text-align: center;
}
.scanner-status { margin: 0 0 12px; }
.scanner-manual { display: flex; gap: 8px; max-width: 420px; margin: 0 auto; }
.scanner-manual input { flex: 1; min-height: 44px; border-radius: 12px; border: 0; padding: 0 12px; font-size: 16px; color: #000; background: #fff; }
.scanner .pill { background: rgb(255 255 255 / 0.9); color: #000; }

/* Grafer */
.chart { width: 100%; height: auto; margin-top: 8px; }
.chart .grid { stroke: var(--line); stroke-width: 1; }
.chart .axis { fill: var(--muted); font-size: 11px; }
.chart .line { fill: none; stroke: var(--accent); stroke-width: 2.5; stroke-linejoin: round; stroke-linecap: round; }
.chart .dot { fill: var(--card); stroke: var(--accent); stroke-width: 2; }
.chart .barfill { fill: var(--accent); }
.chart .barfill.over { fill: var(--warn); }
.chart .goal { stroke: var(--muted); stroke-dasharray: 4 4; stroke-width: 1.5; }
.stat-row { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; text-align: center; margin-top: 4px; }
.stat-row strong { display: block; font-size: 20px; }
.stat-row span { font-size: 12px; color: var(--muted); }

.toast {
  position: fixed; left: 50%; bottom: calc(env(safe-area-inset-bottom) + var(--tabbar-h) + 16px); z-index: 50;
  transform: translate(-50%, 20px); opacity: 0; pointer-events: none;
  background: var(--text); color: var(--bg); padding: 10px 16px; border-radius: 999px; font-size: 14px; font-weight: 600;
  transition: opacity 0.2s, transform 0.2s; white-space: nowrap;
}
.toast.show { opacity: 1; transform: translate(-50%, 0); }

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

@media (min-width: 700px) {
  .meals { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }
  .meals .card { margin-bottom: 0; }
}

/* Stemme */
.searchbar [data-voice] { background: var(--accent-soft); color: var(--accent); }
.voice-top { display: flex; align-items: center; gap: 14px; margin: 4px 0 14px; }
.voice-top p { margin: 0; }
.mic-btn {
  flex-shrink: 0; display: grid; place-items: center; width: 68px; height: 68px; border-radius: 50%;
  border: 0; background: var(--accent); color: var(--accent-ink);
}
.mic-btn svg { width: 30px; height: 30px; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }
.mic-btn.listening { background: var(--warn); animation: pulse 1.4s ease-out infinite; }
@keyframes pulse {
  0% { box-shadow: 0 0 0 0 color-mix(in srgb, var(--warn) 45%, transparent); }
  100% { box-shadow: 0 0 0 18px transparent; }
}
.voice-text { min-height: 88px; resize: vertical; line-height: 1.4; }
.row-main { flex: 1; min-width: 0; border: 0; background: none; padding: 0; text-align: left; }
.row .icon-btn { width: 34px; height: 34px; margin-right: -6px; color: var(--muted); }
.row .icon-btn svg { width: 18px; height: 18px; }
.warn { color: var(--warn); }

/* Matlagingsmodus */
.sheet-body { flex: 1; }
.sheet-foot {
  display: flex; align-items: center; gap: 12px;
  padding: 12px 16px; border-top: 1px solid var(--line); background: var(--card);
}
.sheet-foot .btn { min-width: 150px; }
.foot-total { flex: 1; display: grid; }
.foot-total strong { font-size: 22px; }
.cook-status { display: flex; align-items: center; gap: 14px; margin-bottom: 8px; }
.cook-text p { margin: 4px 0 0; }
.heard { min-height: 20px; margin: 0 4px 12px; color: var(--muted); font-style: italic; overflow-wrap: anywhere; }
.heard.hit { color: var(--accent); font-style: normal; font-weight: 600; }
.manual-add { display: flex; gap: 8px; margin-bottom: 4px; }
.manual-add .input { flex: 1; }
.list li.fresh { animation: fresh 2.5s ease-out; border-radius: 10px; }
@keyframes fresh { from { background: var(--accent-soft); } to { background: transparent; } }
.cook-pill { display: inline-flex; align-items: center; gap: 6px; margin-top: 6px; font-size: 13px; color: var(--accent); }
.cook-pill svg { width: 16px; height: 16px; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; }
.link.warn { color: var(--warn); }
.heard-log { margin-top: 18px; font-size: 13px; color: var(--muted); }
.heard-log summary { cursor: pointer; font-weight: 600; padding: 6px 0; }
.heard-log ol { margin: 4px 0 0; padding-left: 20px; display: grid; gap: 4px; }
.heard-log mark { background: var(--accent-soft); color: var(--text); border-radius: 4px; padding: 0 2px; }
