:root {
  --bg: #f4f5f7;
  --card: #ffffff;
  --text: #15171c;
  --muted: #6b7280;
  --line: #e5e7eb;
  --accent: #2563eb;
  --kcal: #f97316;
  --protein: #e11d48;
  --carbs: #0ea5e9;
  --fat: #eab308;
  --good: #16a34a;
  --radius: 16px;
  color-scheme: light dark;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0f1115;
    --card: #1a1d24;
    --text: #f1f2f4;
    --muted: #9aa1ad;
    --line: #2a2e37;
    --accent: #60a5fa;
  }
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
[hidden] { display: none !important; }
html, body { margin: 0; }
body {
  font: 16px/1.4 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  background: var(--bg);
  color: var(--text);
  padding-bottom: calc(76px + env(safe-area-inset-bottom));
}
h2, h3 { margin: 0 0 10px; }
h3 { font-size: 15px; color: var(--muted); font-weight: 600; }
main { max-width: 640px; margin: 0 auto; padding: 12px 16px; }
.muted { color: var(--muted); }
.small { font-size: 13px; }

.top {
  position: sticky; top: 0; z-index: 5;
  display: flex; align-items: center; justify-content: space-between;
  padding: calc(8px + env(safe-area-inset-top)) 12px 8px;
  background: var(--bg);
  border-bottom: 1px solid var(--line);
}
.date-label { background: none; border: 0; color: var(--text); font-size: 17px; font-weight: 600; }
.icon-btn {
  background: none; border: 0; color: var(--text);
  font-size: 24px; width: 44px; height: 44px; border-radius: 12px; cursor: pointer;
}

.view { display: none; }
.view.active { display: block; }

.card {
  background: var(--card); border-radius: var(--radius);
  padding: 16px; margin-bottom: 12px;
  border: 1px solid var(--line);
}
.row { display: flex; gap: 8px; align-items: center; }
.row.space { justify-content: space-between; margin: 4px 0; }
.grow { flex: 1; }

input, select {
  width: 100%; font: inherit; color: var(--text);
  background: var(--bg); border: 1px solid var(--line);
  border-radius: 12px; padding: 12px; min-height: 44px;
}
input:focus, select:focus { outline: 2px solid var(--accent); outline-offset: -1px; }
label { display: grid; gap: 4px; font-size: 13px; color: var(--muted); }
.grid2 { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.span2 { grid-column: span 2; }

.btn {
  font: inherit; font-weight: 600; min-height: 44px; padding: 0 16px;
  border-radius: 12px; border: 1px solid var(--line);
  background: var(--card); color: var(--text); cursor: pointer; white-space: nowrap;
}
.btn.primary { background: var(--accent); border-color: var(--accent); color: #fff; }
.btn.ghost { background: transparent; }
.btn.full { width: 100%; margin-top: 8px; }
.btn:disabled { opacity: .5; }

/* Sammendrag */
.summary { display: flex; gap: 16px; align-items: center; }
.ring-wrap { position: relative; width: 120px; flex: none; }
.ring { width: 120px; height: 120px; transform: rotate(-90deg); }
.ring circle { fill: none; stroke-width: 10; }
.ring-bg { stroke: var(--line); }
.ring-fg { stroke: var(--kcal); stroke-linecap: round; stroke-dasharray: 326.7; stroke-dashoffset: 326.7; transition: stroke-dashoffset .4s; }
.ring-fg.over { stroke: var(--protein); }
.ring-text { position: absolute; inset: 0; display: grid; place-content: center; text-align: center; }
.big { font-size: 26px; font-weight: 700; }
.big2 { font-size: 20px; font-weight: 700; }
.ring-text .muted { font-size: 12px; }
.macros { flex: 1; display: grid; gap: 10px; min-width: 0; }
.macro .top-line { display: flex; justify-content: space-between; font-size: 13px; }
.bar { height: 8px; background: var(--line); border-radius: 4px; overflow: hidden; margin-top: 4px; }
.bar > div { height: 100%; border-radius: 4px; transition: width .4s; }

.health-mini { display: flex; justify-content: space-around; text-align: center; }
.health-mini .v { font-size: 20px; font-weight: 700; }

/* Måltider */
.meal h3 { display: flex; justify-content: space-between; color: var(--text); font-size: 16px; }
.meal h3 span { color: var(--muted); font-weight: 500; }
.list { list-style: none; margin: 0; padding: 0; }
.list li {
  display: flex; align-items: center; gap: 10px;
  padding: 10px 0; border-top: 1px solid var(--line);
}
.list li:first-child { border-top: 0; }
.list .main { flex: 1; min-width: 0; }
.list .name { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.list .sub { font-size: 12px; color: var(--muted); }
.list .kc { font-weight: 600; white-space: nowrap; }
.list .del { background: none; border: 0; color: var(--muted); font-size: 18px; width: 36px; height: 36px; cursor: pointer; }
.results li { cursor: pointer; }
.add-link { background: none; border: 0; color: var(--accent); font: inherit; font-weight: 600; padding: 8px 0 0; cursor: pointer; }

/* Statistikk-ruter */
.stats { display: grid; grid-template-columns: repeat(auto-fit, minmax(130px, 1fr)); gap: 10px; margin-bottom: 12px; }
.stat { background: var(--card); border: 1px solid var(--line); border-radius: var(--radius); padding: 12px 14px; }
.stat .l { font-size: 12px; color: var(--muted); }
.stat .v { font-size: 22px; font-weight: 700; }
.stat .d { font-size: 12px; color: var(--muted); }

/* Faner */
.tabs {
  position: fixed; bottom: 0; left: 0; right: 0; z-index: 10;
  display: flex; background: var(--card); border-top: 1px solid var(--line);
  padding-bottom: env(safe-area-inset-bottom);
}
.tabs button {
  flex: 1; background: none; border: 0; color: var(--muted);
  font: 12px/1.2 inherit; padding: 8px 0 10px; display: grid; gap: 2px; cursor: pointer;
}
.tabs svg {
  width: 24px; height: 24px; margin: 0 auto 2px; display: block;
  fill: none; stroke: currentColor; stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round;
}
.tabs button.active svg { stroke-width: 2.2; }
.tabs button.active { color: var(--accent); font-weight: 600; }

/* Ark (dialog) */
.sheet {
  width: 100%; max-width: 640px; max-height: 92vh;
  margin: auto auto 0; padding: 16px 16px calc(16px + env(safe-area-inset-bottom));
  border: 0; border-radius: 20px 20px 0 0;
  background: var(--card); color: var(--text);
}
.sheet::backdrop { background: rgba(0,0,0,.45); }
.sheet-head { display: flex; justify-content: space-between; align-items: center; margin-bottom: 8px; }
.sheet label { margin-top: 10px; }
.seg { display: flex; background: var(--bg); border-radius: 12px; padding: 3px; margin: 10px 0 4px; }
.seg button { flex: 1; border: 0; background: none; color: var(--muted); font: inherit; font-size: 14px; padding: 8px; border-radius: 10px; cursor: pointer; }
.seg button.active { background: var(--card); color: var(--text); font-weight: 600; }
.results { max-height: 46vh; overflow-y: auto; }
#scanPanel { margin-top: 10px; display: grid; gap: 8px; }
#scanner { border-radius: 12px; overflow: hidden; background: #000; min-height: 200px; }
#scanner video { width: 100% !important; height: auto; display: block; }
.center { text-align: center; margin: 0; }
.fab {
  position: fixed; z-index: 9; right: 16px;
  bottom: calc(76px + env(safe-area-inset-bottom));
  display: flex; align-items: center; gap: 8px;
  height: 56px; padding: 0 22px; border: 0; border-radius: 28px;
  background: var(--accent); color: #fff; font: inherit; font-weight: 700; font-size: 17px;
  box-shadow: 0 6px 20px rgba(0,0,0,.3); cursor: pointer;
}
.ico {
  width: 20px; height: 20px; flex: none; vertical-align: -4px;
  fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round;
}
.fab .ico { width: 22px; height: 22px; }
#photoScanBtn { display: inline-flex; align-items: center; justify-content: center; gap: 8px; }
.report-notes { list-style: none; margin: 10px 0 0; padding: 0; font-size: 14px; line-height: 1.45; }
.report-notes li { position: relative; padding: 3px 0 3px 16px; }
.report-notes li::before { content: ""; position: absolute; left: 2px; top: 10px; width: 7px; height: 7px; border-radius: 50%; background: var(--muted); }
.report-notes li.good::before { background: var(--good); }
.report-notes li.bad::before { background: var(--protein); }
.status-dot { display: inline-block; width: 9px; height: 9px; border-radius: 50%; background: var(--good); margin-right: 8px; }
#view-today { padding-bottom: 72px; }
.chips { display: flex; gap: 6px; flex-wrap: wrap; margin-top: 8px; }
.chips button { border: 1px solid var(--line); background: var(--bg); color: var(--text); border-radius: 999px; padding: 6px 12px; font: inherit; font-size: 14px; cursor: pointer; }
.calc { display: grid; grid-template-columns: repeat(4, 1fr); text-align: center; margin: 14px 0; }
.calc .v { font-weight: 700; font-size: 18px; }
.calc .l { font-size: 12px; color: var(--muted); }

.banner { display: block; width: 100%; text-align: left; font: inherit; color: var(--text); cursor: pointer; border-color: var(--accent); }

.backfill-note { background: var(--card); border: 1px solid var(--line); border-radius: 12px; padding: 10px 12px; margin: 6px 0 10px; }
.backfill-bar { height: 6px; background: var(--line); border-radius: 3px; overflow: hidden; margin-top: 8px; }
.backfill-bar > div { height: 100%; background: var(--accent); transition: width .4s; }

/* Profil */
.avatar {
  width: 48px; height: 48px; border-radius: 50%; flex: none; overflow: hidden;
  display: inline-grid; place-items: center; background: var(--accent); color: #fff;
  font-weight: 700; font-size: 18px; text-transform: uppercase;
}
.avatar img { width: 100%; height: 100%; object-fit: cover; display: block; }
.avatar-xl { width: 120px; height: 120px; font-size: 42px; }
.profile-link { display: flex; align-items: center; gap: 14px; width: 100%; text-align: left; font: inherit; color: var(--text); cursor: pointer; }
.profile-link .main { flex: 1; display: grid; gap: 2px; }
.profile-link .name { font-weight: 700; font-size: 17px; }
.profile-link .sub { color: var(--muted); font-size: 13px; }
.profile-card { text-align: center; display: grid; justify-items: center; gap: 4px; padding-top: 24px; }
.avatar-edit { position: relative; cursor: pointer; margin-bottom: 8px; }
.avatar-badge {
  position: absolute; right: 2px; bottom: 2px; width: 34px; height: 34px; border-radius: 50%;
  background: var(--card); border: 1px solid var(--line); display: grid; place-items: center; color: var(--text);
}
.avatar-badge .ico { width: 18px; height: 18px; }
.profile-name { font-size: 22px; font-weight: 700; }
.profile-bio { margin: 10px 0 0; white-space: pre-wrap; max-width: 34ch; line-height: 1.45; }
.profile-bio:empty { display: none; }
.back-link { padding: 4px 0 10px; font-size: 16px; }

/* Delte måltider */
.seg button { font-size: 13px; padding: 8px 4px; }
textarea { width: 100%; font: inherit; color: var(--text); background: var(--bg); border: 1px solid var(--line); border-radius: 12px; padding: 12px; resize: vertical; }
.folder { margin-top: 10px; }
.folder-head { display: flex; justify-content: space-between; align-items: center; gap: 8px; font-weight: 700; padding: 6px 0; }
.folder-head .icon-btn { font-size: 16px; width: 36px; height: 36px; }
.group-head { font-size: 12px; font-weight: 700; color: var(--muted); text-transform: uppercase; letter-spacing: .04em; padding: 12px 0 4px; border-top: 0 !important; cursor: default !important; }
.ing-grams { width: 84px; min-height: 36px; padding: 6px 8px; text-align: right; }
.meal-save { background: none; border: 0; color: var(--muted); font: inherit; font-size: 13px; padding: 8px 0 0 14px; cursor: pointer; }

/* Mål og kalkulator */
.section-lbl { margin: 12px 0 0; padding-top: 12px; border-top: 1px solid var(--line); }
.estimate { background: var(--bg); border-radius: 12px; padding: 12px 14px; font-size: 14px; }
.estimate:empty { display: none; }
.estimate .big-kcal { font-size: 26px; font-weight: 700; }
.estimate .macro-row { display: grid; grid-template-columns: repeat(3, 1fr); text-align: center; margin: 10px 0 6px; }
.estimate .macro-row b { display: block; font-size: 17px; }
.estimate .line { display: flex; justify-content: space-between; padding: 3px 0; color: var(--muted); }
.estimate .line span:last-child { color: var(--text); font-weight: 600; }
.estimate .warn { color: var(--protein); margin-top: 6px; }

/* Fremgang */
.report-head { display: flex; align-items: center; justify-content: space-between; text-align: center; margin-bottom: 6px; }
.report-title { font-weight: 700; font-size: 17px; }
.report-row { display: flex; justify-content: space-between; align-items: baseline; gap: 8px; padding: 9px 0; border-top: 1px solid var(--line); }
.report-row:first-child { border-top: 0; }
.report-row .lbl { color: var(--muted); font-size: 14px; }
.report-row .val { font-weight: 700; text-align: right; }
.report-row .cmp { display: block; font-size: 12px; font-weight: 500; color: var(--muted); }
.good-txt, .report-row .good-txt { color: var(--good); }
.bad-txt, .report-row .bad-txt { color: var(--protein); }
.report-note { margin: 10px 0 0; font-size: 14px; line-height: 1.45; }
.corr-list li { cursor: pointer; }
.corr-list li.active { background: var(--bg); border-radius: 10px; }
.corr-bar { width: 64px; height: 8px; background: var(--line); border-radius: 4px; position: relative; flex: none; }
.corr-bar > div { position: absolute; top: 0; bottom: 0; border-radius: 4px; }
.corr-bar::after { content: ""; position: absolute; left: 50%; top: -2px; bottom: -2px; width: 1px; background: var(--muted); }
.tabs button { font-size: 11px; }

/* Innlogging */
.auth {
  position: fixed; inset: 0; z-index: 100; background: var(--bg);
  display: grid; place-items: center; padding: 24px 16px; overflow-y: auto;
}
.auth-box { width: 100%; max-width: 360px; text-align: center; }
.auth-logo { width: 72px; height: 72px; border-radius: 18px; }
.auth h1 { margin: 8px 0 20px; }
.stack { display: grid; gap: 10px; }
.link { background: none; border: 0; color: var(--accent); font: inherit; font-size: 14px; padding: 8px; cursor: pointer; }
#view-more label { margin-top: 10px; }
#view-more .row input { min-width: 0; }
.garmin-row { display: flex; justify-content: space-between; align-items: center; gap: 8px; }

.toast {
  position: fixed; left: 50%; bottom: calc(90px + env(safe-area-inset-bottom));
  transform: translateX(-50%) translateY(20px); opacity: 0; pointer-events: none;
  background: var(--text); color: var(--bg); padding: 10px 16px; border-radius: 12px;
  transition: .25s; z-index: 50; max-width: calc(100% - 32px);
}
.toast.show { opacity: 1; transform: translateX(-50%) translateY(0); }

@media (max-width: 380px) {
  .summary { flex-direction: column; }
  .macros { width: 100%; }
}
