:root {
  --bg: #f5efe3;
  --surface: #fffaf0;
  --ink: #23332b;
  --muted: #6c776e;
  --line: #e2d8c3;
  --green: #3a6a4e;
  --green-soft: #dfe9dc;
  --accent: #c4532d;
  --accent-ink: #fffaf0;
  --over: #b3412a;
  --serif: "Iowan Old Style", "Palatino Linotype", Palatino, Georgia, serif;
  --sans: -apple-system, BlinkMacSystemFont, "Segoe UI", system-ui, sans-serif;
  color-scheme: light dark;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #1b201c;
    --surface: #252c27;
    --ink: #efe9db;
    --muted: #9aa59c;
    --line: #36403a;
    --green: #86b794;
    --green-soft: #2c3a31;
    --accent: #e0805a;
    --accent-ink: #1b201c;
    --over: #e88e6d;
  }
}

* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--ink);
  font-family: var(--sans);
  font-size: 17px;
  line-height: 1.4;
  -webkit-tap-highlight-color: transparent;
}

#app {
  max-width: 520px;
  margin: 0 auto;
  padding: max(16px, env(safe-area-inset-top)) 18px max(40px, env(safe-area-inset-bottom));
}

.sr-only { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; }
[hidden] { display: none !important; }
button, input { font: inherit; color: inherit; }
.muted { color: var(--muted); margin: 0; }

.top { display: flex; justify-content: space-between; align-items: flex-end; padding: 8px 0 14px; }
.eyebrow { margin: 0; font-size: 14px; color: var(--muted); }
.eyebrow::first-letter { text-transform: uppercase; }
h1 { margin: 2px 0 0; font-family: var(--serif); font-weight: 700; font-size: 34px; letter-spacing: -0.01em; line-height: 1.1; }
h2 { margin: 0 0 6px; font-family: var(--serif); font-size: 24px; }

.icon-btn {
  width: 44px; height: 44px; border-radius: 50%; border: 1px solid var(--line);
  background: var(--surface); display: grid; place-items: center; color: var(--muted); cursor: pointer;
}

.week { display: grid; grid-template-columns: 28px repeat(7, 1fr) 28px; gap: 4px; align-items: end; margin-bottom: 16px; }
.week .nav {
  border: 0; background: none; color: var(--muted); font-size: 22px; height: 56px; cursor: pointer; padding: 0;
}
.day {
  border: 0; background: none; padding: 4px 0 6px; border-radius: 12px; cursor: pointer;
  display: flex; flex-direction: column; align-items: center; gap: 4px;
}
.day .bar { width: 10px; height: 26px; border-radius: 5px; background: var(--line); display: flex; align-items: flex-end; overflow: hidden; }
.day .bar i { display: block; width: 100%; background: var(--green); border-radius: 5px; }
.day .bar i.over { background: var(--over); }
.day .dow { font-size: 11px; color: var(--muted); text-transform: uppercase; letter-spacing: 0.04em; }
.day .num { font-size: 15px; font-weight: 600; width: 30px; height: 30px; display: grid; place-items: center; border-radius: 50%; }
.day[aria-current="date"] .num { box-shadow: inset 0 0 0 1.5px var(--green); }
.day[aria-pressed="true"] .num { background: var(--ink); color: var(--bg); box-shadow: none; }

.summary {
  background: var(--surface); border: 1px solid var(--line); border-radius: 24px; padding: 20px 22px 18px; margin-bottom: 14px;
}
.total { display: flex; align-items: baseline; gap: 8px; }
.total strong { font-family: var(--serif); font-size: 58px; line-height: 1; letter-spacing: -0.02em; font-variant-numeric: tabular-nums; }
.total span { color: var(--muted); font-size: 17px; }
.remain { margin: 10px 0 0; font-size: 16px; color: var(--muted); }
.remain b { color: var(--green); font-weight: 600; }
.remain.over b { color: var(--over); }
.meter { height: 8px; border-radius: 4px; background: var(--line); overflow: hidden; margin-top: 12px; }
.meter i { display: block; height: 100%; background: var(--green); border-radius: 4px; transition: width 0.3s ease; }
.meter.over i { background: var(--over); }
.link { background: none; border: 0; padding: 10px 0 0; color: var(--green); text-decoration: underline; text-underline-offset: 3px; cursor: pointer; font-size: 16px; }

.add { background: var(--green-soft); border-radius: 24px; padding: 14px; margin-bottom: 22px; }
.chips { display: grid; grid-template-columns: repeat(4, 1fr); gap: 4px; margin-bottom: 10px; }
.chip {
  border: 1px solid transparent; background: transparent; color: var(--muted); padding: 8px 2px; border-radius: 999px;
  font-size: 13.5px; cursor: pointer; white-space: nowrap;
}
.chip[aria-checked="true"] { background: var(--surface); color: var(--ink); border-color: var(--line); font-weight: 600; }
#addForm { display: flex; gap: 8px; }
input[type="text"], input[type="number"], input[type="search"], select {
  width: 100%; min-width: 0; background: var(--surface); border: 1px solid var(--line); border-radius: 14px;
  padding: 13px 14px; font-size: 17px; outline: none;
}
input:focus { border-color: var(--green); box-shadow: 0 0 0 3px color-mix(in srgb, var(--green) 25%, transparent); }
.primary {
  background: var(--accent); color: var(--accent-ink); border: 0; border-radius: 14px; padding: 0 18px; font-weight: 600;
  cursor: pointer; white-space: nowrap; min-height: 48px;
}
.primary:active { transform: translateY(1px); }
.danger { background: none; border: 1px solid var(--line); border-radius: 14px; padding: 0 16px; min-height: 48px; color: var(--over); cursor: pointer; }

.unknown { margin-top: 12px; background: var(--surface); border-radius: 16px; padding: 14px; border: 1px solid var(--line); }
.unknown p { margin: 0 0 10px; font-size: 15px; }
.unknown .q { display: grid; grid-template-columns: 1fr 104px; gap: 8px; align-items: center; margin-bottom: 8px; }
.unknown .q span { font-weight: 600; overflow-wrap: anywhere; }
.unknown .row { display: flex; gap: 8px; justify-content: flex-end; align-items: center; margin-top: 10px; }
.unknown .row .link { padding: 0 10px; }

.meal { margin-bottom: 18px; }
.meal h3 { display: flex; justify-content: space-between; margin: 0 4px 6px; font-size: 13px; font-weight: 600; letter-spacing: 0.06em; text-transform: uppercase; color: var(--muted); }
.meal ul { list-style: none; margin: 0; padding: 0; background: var(--surface); border: 1px solid var(--line); border-radius: 18px; overflow: hidden; }
.meal li + li { border-top: 1px solid var(--line); }
.entry {
  width: 100%; display: grid; grid-template-columns: 30px 1fr auto; gap: 10px; align-items: center; text-align: left;
  background: none; border: 0; padding: 12px 14px; cursor: pointer; min-height: 56px;
}
.entry .em { font-size: 22px; text-align: center; }
.entry .nm { display: block; font-weight: 600; overflow-wrap: anywhere; }
.entry .sub { display: block; font-size: 13px; color: var(--muted); overflow-wrap: anywhere; }
.entry .kc { font-family: var(--serif); font-size: 20px; font-weight: 700; font-variant-numeric: tabular-nums; }
.entry .kc small { font-family: var(--sans); font-size: 12px; font-weight: 400; color: var(--muted); margin-left: 2px; }

.empty { text-align: center; color: var(--muted); padding: 22px 12px; }
.empty b { color: var(--ink); }
.foot { text-align: center; color: var(--muted); font-size: 13px; margin: 28px 0 0; }

#toast {
  position: fixed; left: 50%; bottom: max(24px, env(safe-area-inset-bottom)); transform: translateX(-50%);
  background: var(--ink); color: var(--bg); padding: 11px 16px; border-radius: 14px; font-size: 15px;
  display: flex; gap: 14px; align-items: center; max-width: calc(100% - 32px); z-index: 10;
}
#toast button { background: none; border: 0; color: inherit; text-decoration: underline; cursor: pointer; padding: 0; }

dialog {
  width: min(92vw, 440px); border: 0; border-radius: 24px; padding: 22px; background: var(--surface); color: var(--ink);
  box-shadow: 0 20px 60px rgba(0, 0, 0, 0.3); max-height: 88dvh; overflow-y: auto;
}
dialog::backdrop { background: rgba(20, 28, 22, 0.5); }
dialog label { display: block; margin: 14px 0 6px; font-size: 14px; color: var(--muted); }
dialog .row { display: flex; justify-content: space-between; gap: 10px; margin-top: 20px; }
dialog .link { display: block; margin: 12px auto 0; }
.help { margin-top: 18px; padding: 12px 14px; background: var(--bg); border-radius: 14px; font-size: 14px; color: var(--muted); }
.help p { margin: 0 0 8px; }
.help p:last-child { margin: 0; }
.help em { color: var(--ink); font-style: normal; background: var(--green-soft); padding: 0 4px; border-radius: 4px; }

.hint { background: var(--surface); border: 1px solid var(--line); border-radius: 18px; padding: 14px 16px; margin-bottom: 14px; font-size: 15px; }
.hint p { margin: 0; }
.hint svg { vertical-align: -4px; margin: 0 2px; color: var(--green); }
.hint .row { display: flex; align-items: center; gap: 12px; margin-top: 10px; }
.hint .link { padding-top: 8px; }

.recents { display: flex; gap: 6px; overflow-x: auto; margin: 10px -14px 0; padding: 0 14px 2px; scrollbar-width: none; }
.recents::-webkit-scrollbar { display: none; }
.recents button {
  flex: none; border: 1px solid var(--line); background: var(--surface); border-radius: 999px; padding: 7px 12px;
  font-size: 14px; cursor: pointer; white-space: nowrap;
}
.recents button small { color: var(--muted); margin-left: 4px; font-size: 12px; }

.backup { margin-top: 18px; display: grid; gap: 8px; }
.backup p { margin: 0; }
.backup .muted { font-size: 15px; }
.backup .actions { display: flex; flex-wrap: wrap; gap: 8px; margin-top: 4px; }
.ghost {
  display: inline-flex; align-items: center; justify-content: center; min-height: 48px; padding: 0 16px; border-radius: 14px;
  border: 1px solid var(--line); background: none; color: var(--ink); cursor: pointer; font-size: 16px;
}
.backup details { margin-top: 4px; }
.backup summary { cursor: pointer; color: var(--green); padding: 8px 0; }
.backup textarea {
  width: 100%; background: var(--surface); border: 1px solid var(--line); border-radius: 14px; padding: 12px; font-size: 16px;
  color: var(--ink); margin-bottom: 8px; resize: vertical;
}
.ghost:focus-within { outline: 2px solid var(--green); }
h2[tabindex="-1"]:focus { outline: none; }

.libopen {
  width: 100%; margin-top: 10px; background: none; color: var(--green); border-radius: 14px; padding: 11px 12px;
  border: 1px dashed color-mix(in srgb, var(--green) 55%, transparent); font-size: 15px; cursor: pointer;
}
dialog.lib { display: flex; flex-direction: column; gap: 10px; height: 86dvh; padding: 18px 18px 12px; overflow: hidden; }
dialog.lib:not([open]) { display: none; }
.lib-head { display: flex; justify-content: space-between; align-items: baseline; }
.lib-head h2 { margin: 0; }
.lib-head .link { padding: 0; }
.lib-body { display: flex; flex-direction: column; gap: 10px; min-height: 0; flex: 1; }
.lib-body[hidden] { display: none; }
#libResults { list-style: none; margin: 0; padding: 0; overflow-y: auto; flex: 1; border-top: 1px solid var(--line); overscroll-behavior: contain; }
#libResults .note { padding: 18px 4px; color: var(--muted); font-size: 15px; }
.lib-item {
  width: 100%; display: grid; grid-template-columns: 1fr auto; gap: 2px 12px; text-align: left; background: none; border: 0;
  border-bottom: 1px solid var(--line); padding: 11px 2px; cursor: pointer;
}
.lib-item .ln { font-weight: 600; overflow-wrap: anywhere; }
.lib-item .lg { grid-column: 1; font-size: 13px; color: var(--muted); }
.lib-item .lk { grid-row: 1 / span 2; grid-column: 2; align-self: center; text-align: right; font-family: var(--serif); font-size: 20px; font-weight: 700; font-variant-numeric: tabular-nums; }
.lib-item .lk small { display: block; font-family: var(--sans); font-size: 11px; font-weight: 400; color: var(--muted); }
.lib-body h3 { margin: 0; font-family: var(--serif); font-size: 22px; line-height: 1.2; overflow-wrap: anywhere; }
.lib-body .back { align-self: flex-start; padding: 0; }
.lib-body label { margin: 6px 0 0; font-size: 14px; color: var(--muted); }
.grams { display: flex; align-items: center; gap: 10px; }
.grams input { max-width: 150px; font-size: 22px; }
.grams span { font-size: 20px; color: var(--muted); }
.quick { display: flex; gap: 6px; flex-wrap: wrap; }
.quick button { border: 1px solid var(--line); background: var(--surface); border-radius: 999px; padding: 7px 13px; font-size: 15px; cursor: pointer; }
.lib-total { margin: 4px 0 0; color: var(--muted); }
.lib-total strong { font-family: var(--serif); font-size: 46px; line-height: 1; color: var(--ink); font-variant-numeric: tabular-nums; }
.primary.wide { width: 100%; margin-top: auto; }
.src { margin: 0; font-size: 12px; color: var(--muted); text-align: center; }
.unknown .q + .link { display: block; padding: 0 0 8px; font-size: 15px; }
.hint-line { font-size: 14px; min-height: 1.4em; }
#libPickView { overflow-y: auto; padding-bottom: 6px; }
.hint-line { margin: 0 0 8px; }
.lib-total { margin: 6px 0 4px; }
.src { flex: none; padding-top: 6px; }
.quick { flex-wrap: nowrap; overflow-x: auto; scrollbar-width: none; }
.quick::-webkit-scrollbar { display: none; }
.quick button { flex: none; padding: 6px 12px; }
.primary.wide { position: sticky; bottom: 0; box-shadow: 0 -12px 10px -2px var(--surface); }
#libPickView { gap: 6px; }

.suggest { list-style: none; margin: 8px 0 0; padding: 4px; background: var(--surface); border: 1px solid var(--line); border-radius: 16px; display: grid; }
.suggest[hidden] { display: none; }
.suggest li + li { border-top: 1px solid var(--line); }
.suggest button {
  width: 100%; display: grid; grid-template-columns: 28px 1fr auto; gap: 10px; align-items: center; text-align: left;
  background: none; border: 0; padding: 9px 8px; border-radius: 12px; cursor: pointer; min-height: 48px;
}
.suggest .se { font-size: 20px; text-align: center; }
.suggest .sn { font-weight: 600; font-size: 16px; overflow-wrap: anywhere; line-height: 1.25; }
.suggest .sk { font-family: var(--serif); font-weight: 700; font-size: 17px; text-align: right; font-variant-numeric: tabular-nums; }
.suggest .sk small { display: block; font-family: var(--sans); font-weight: 400; font-size: 11px; color: var(--muted); }
.add:has(.suggest:not([hidden])) .recents { display: none; }
