:root {
  --bg: #faf7f2;
  --surface: #ffffff;
  --surface-2: #f3eee7;
  --text: #2b2420;
  --muted: #7a6e66;
  --line: #e7dfd5;
  --accent: #c2593a;
  --accent-text: #ffffff;
  --ok: #3f8f5a;
  --warn: #b7791f;
  --over: #c0392b;
  --radius: 14px;
  --shadow: 0 1px 2px rgb(0 0 0 / 6%), 0 4px 14px rgb(0 0 0 / 5%);
  color-scheme: light dark;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #1b1816;
    --surface: #262220;
    --surface-2: #312c29;
    --text: #f1ebe6;
    --muted: #a99d94;
    --line: #3b3532;
    --accent: #e07a5a;
    --accent-text: #1b1816;
    --ok: #6cc08a;
    --warn: #e0a84a;
    --over: #ef6a5b;
    --shadow: none;
  }
}

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

html, body { margin: 0; background: var(--bg); color: var(--text); }
body {
  font: 16px/1.45 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  -webkit-tap-highlight-color: transparent;
  -webkit-text-size-adjust: 100%;
}
h1, h2, h3 { line-height: 1.2; margin: 0; }
h2 { font-size: 1.05rem; margin: 1.4rem 0 .6rem; }
p { margin: 0; }
.muted { color: var(--muted); font-size: .9rem; }
.center { text-align: center; }
.tabular { font-variant-numeric: tabular-nums; }

/* Layout */
#app { min-height: 100dvh; display: flex; flex-direction: column; }
.topbar {
  display: flex; justify-content: space-between; align-items: center; gap: 1rem;
  padding: calc(env(safe-area-inset-top) + .8rem) 1rem .6rem;
  background: var(--bg); position: sticky; top: 0; z-index: 5;
}
.topbar h1 { font-size: 1.35rem; }
.view { flex: 1; width: 100%; max-width: 640px; margin: 0 auto; padding: .4rem 1rem calc(5.5rem + env(safe-area-inset-bottom)); }
.tabbar {
  position: fixed; inset: auto 0 0 0; z-index: 10;
  display: grid; grid-template-columns: repeat(4, 1fr);
  background: var(--surface); border-top: 1px solid var(--line);
  padding-bottom: env(safe-area-inset-bottom);
}
.tabbar a {
  display: flex; flex-direction: column; align-items: center; gap: 2px;
  padding: .5rem 0 .55rem; color: var(--muted); text-decoration: none; font-size: .75rem;
}
.tabbar a span { font-size: 1.25rem; filter: grayscale(1); opacity: .7; }
.tabbar a[aria-current="page"] { color: var(--accent); font-weight: 600; }
.tabbar a[aria-current="page"] span { filter: none; opacity: 1; }

.card { background: var(--surface); border-radius: var(--radius); box-shadow: var(--shadow); padding: 1rem; border: 1px solid var(--line); }
.stack { display: flex; flex-direction: column; gap: .8rem; }
.row { display: flex; gap: .5rem; align-items: center; }
.row > input { flex: 1; }
.grid-2 { display: grid; grid-template-columns: 1fr 1fr; gap: .6rem; }

/* Formulare */
label { display: flex; flex-direction: column; gap: .3rem; font-size: .9rem; color: var(--muted); }
input, select {
  font: inherit; color: var(--text); background: var(--surface);
  border: 1px solid var(--line); border-radius: 10px; padding: .7rem .8rem; min-height: 46px; width: 100%;
}
input:focus, select:focus { outline: 2px solid var(--accent); outline-offset: 1px; }
.form-error { color: var(--over); font-size: .9rem; min-height: 1.2em; }
.form-error:empty { display: none; }

.btn {
  font: inherit; font-weight: 600; border-radius: 12px; border: 1px solid var(--line);
  background: var(--surface); color: var(--text); padding: .75rem 1rem; min-height: 48px; cursor: pointer;
  display: inline-flex; align-items: center; justify-content: center; gap: .5rem; text-align: center;
}
.btn:active { transform: scale(.98); }
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-text); }
.btn.danger { color: var(--over); }
.btn.link { background: none; border: none; color: var(--accent); font-weight: 500; min-height: 40px; }
.btn.big { width: 100%; min-height: 60px; font-size: 1.1rem; }
.btn.small { min-height: 36px; padding: .35rem .7rem; font-size: .85rem; }
.icon-btn { font-size: 1.3rem; background: none; border: none; color: var(--muted); width: 44px; height: 44px; border-radius: 50%; cursor: pointer; }

/* Heute */
.goal { display: flex; align-items: center; gap: 1rem; }
.goal-ring { width: 108px; height: 108px; flex: none; }
.goal-ring circle { fill: none; stroke-width: 10; }
.goal-ring .track { stroke: var(--surface-2); }
.goal-ring .bar { stroke: var(--ok); stroke-linecap: round; transform: rotate(-90deg); transform-origin: 50% 50%; transition: stroke-dashoffset .4s; }
.goal-ring text { fill: var(--text); font-weight: 700; font-size: 26px; text-anchor: middle; dominant-baseline: central; }
.goal-ring .sub { font-size: 12px; font-weight: 500; fill: var(--muted); }
.goal.reached .bar { stroke: var(--warn); }
.goal.over .bar { stroke: var(--over); }
.goal-text strong { display: block; font-size: 1.15rem; }
.status-ok { color: var(--ok); }
.status-reached { color: var(--warn); }
.status-over { color: var(--over); }
.last { margin-top: .9rem; padding-top: .8rem; border-top: 1px solid var(--line); }
.last b { font-size: 1.05rem; }

.feed-actions { display: flex; flex-direction: column; gap: .6rem; margin-top: 1rem; }
.product-buttons { display: grid; grid-template-columns: 1fr 1fr; gap: .5rem; }
.product-btn {
  font: inherit; text-align: left; padding: .7rem .8rem; min-height: 64px; border-radius: 12px; cursor: pointer;
  background: var(--surface); color: var(--text); border: 1px solid var(--line); box-shadow: var(--shadow);
}
.product-btn small { display: block; color: var(--muted); font-size: .78rem; }
.product-btn:active { background: var(--surface-2); }

/* Listen */
.list { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: .5rem; }
.entry { background: var(--surface); border: 1px solid var(--line); border-radius: 12px; padding: .7rem .8rem; }
.entry-head { display: flex; justify-content: space-between; gap: .6rem; align-items: baseline; }
.entry-head .time { font-weight: 700; font-variant-numeric: tabular-nums; }
.entry button.entry-open { all: unset; display: block; width: 100%; cursor: pointer; }
.chips { display: flex; gap: .35rem; margin-top: .55rem; flex-wrap: wrap; }
.chip {
  font: inherit; font-size: .82rem; padding: .35rem .65rem; border-radius: 999px; cursor: pointer; min-height: 34px;
  border: 1px solid var(--line); background: var(--surface-2); color: var(--text);
}
.chip[aria-pressed="true"] { background: var(--accent); border-color: var(--accent); color: var(--accent-text); }
.day-head { display: flex; justify-content: space-between; align-items: baseline; margin: 1.2rem 0 .5rem; }
.day-head h2 { margin: 0; }
.badge { font-size: .8rem; padding: .15rem .55rem; border-radius: 999px; background: var(--surface-2); font-variant-numeric: tabular-nums; }
.badge.over { background: color-mix(in srgb, var(--over) 18%, transparent); color: var(--over); }
.badge.reached { background: color-mix(in srgb, var(--warn) 18%, transparent); color: var(--warn); }
.empty { text-align: center; padding: 1.5rem 1rem; color: var(--muted); }

/* Auswertung */
.chart { width: 100%; height: auto; display: block; }
.chart .bar { fill: var(--ok); }
.chart .bar.over { fill: var(--over); }
.chart .goal-line { stroke: var(--text); stroke-dasharray: 4 3; stroke-width: 1; opacity: .6; }
.chart .axis { fill: var(--muted); font-size: 10px; }
.chart .grid { stroke: var(--line); stroke-width: 1; }
.chart .value { fill: var(--muted); font-size: 9px; text-anchor: middle; }
.legend { display: flex; gap: 1rem; font-size: .8rem; color: var(--muted); margin-top: .4rem; flex-wrap: wrap; }
.legend i { display: inline-block; width: 10px; height: 10px; border-radius: 2px; margin-right: .3rem; vertical-align: -1px; }
.legend .l-bar { background: var(--ok); }
.legend .l-over { background: var(--over); }
.legend .l-goal { border-top: 2px dashed var(--text); height: 0; border-radius: 0; opacity: .6; }
.kpis { display: grid; grid-template-columns: repeat(3, 1fr); gap: .5rem; }
.kpi { background: var(--surface); border: 1px solid var(--line); border-radius: 12px; padding: .7rem; }
.kpi b { display: block; font-size: 1.3rem; font-variant-numeric: tabular-nums; }
.kpi span { font-size: .78rem; color: var(--muted); }
table { width: 100%; border-collapse: collapse; font-size: .92rem; }
th, td { text-align: left; padding: .5rem .3rem; border-bottom: 1px solid var(--line); vertical-align: top; }
th { color: var(--muted); font-weight: 500; font-size: .8rem; }
td.num, th.num { text-align: right; font-variant-numeric: tabular-nums; white-space: nowrap; }
.meter { height: 6px; border-radius: 3px; background: var(--surface-2); overflow: hidden; margin-top: .3rem; }
.meter > i { display: block; height: 100%; background: var(--ok); }

/* Dialoge */
dialog.sheet {
  border: none; padding: 1.1rem 1rem calc(1rem + env(safe-area-inset-bottom)); margin: auto auto 0; width: 100%; max-width: 640px;
  border-radius: 18px 18px 0 0; background: var(--surface); color: var(--text); max-height: 92dvh;
}
dialog.sheet::backdrop { background: rgb(0 0 0 / 45%); }
@media (min-width: 700px) { dialog.sheet { margin: auto; border-radius: 18px; } }
.sheet h2 { margin-top: 0; }
.sheet .actions { display: flex; flex-direction: column; gap: .5rem; margin-top: 1rem; }
.scanner { display: flex; flex-direction: column; gap: .7rem; }
.scanner:not([open]) { display: none; }
.scanner-frame { position: relative; border-radius: 14px; overflow: hidden; background: #000; aspect-ratio: 4 / 3; }
.scanner-frame video { width: 100%; height: 100%; object-fit: cover; display: block; }
.scanner-target { position: absolute; inset: 30% 12%; border: 3px solid rgb(255 255 255 / 85%); border-radius: 10px; box-shadow: 0 0 0 999px rgb(0 0 0 / 25%); }
.code-box { font: 700 2rem/1.2 ui-monospace, Menlo, monospace; letter-spacing: .12em; text-align: center; padding: 1rem; border-radius: 12px; background: var(--surface-2); user-select: all; }

/* Toast */
.toast {
  position: fixed; left: 50%; transform: translateX(-50%); bottom: calc(5.2rem + env(safe-area-inset-bottom)); z-index: 30;
  background: var(--text); color: var(--bg); border-radius: 12px; padding: .7rem 1rem; display: flex; gap: 1rem; align-items: center;
  max-width: calc(100% - 2rem); box-shadow: 0 6px 20px rgb(0 0 0 / 25%);
}
.toast button { all: unset; font-weight: 700; color: var(--accent); cursor: pointer; padding: .2rem .3rem; }

/* Login */
.auth { min-height: 100dvh; display: grid; place-items: center; padding: 1.5rem; }
.auth-card { width: 100%; max-width: 380px; display: flex; flex-direction: column; gap: 1rem; text-align: center; }
.auth-card form { text-align: left; }
.auth-logo { margin: 0 auto; border-radius: 18px; }
