:root {
  --bg: #f5f7f4;
  --surface: #ffffff;
  --surface-2: #eef2ec;
  --text: #17211b;
  --muted: #5a6b60;
  --line: #d6ded8;
  --accent: #1f6f4a;
  --accent-text: #ffffff;
  --accent-soft: #dcefe4;
  --danger: #b3261e;
  --danger-soft: #fbe3e1;
  --ok: #1f6f4a;
  --focus: #2f7de1;
  --radius: 12px;
  --shadow: 0 1px 2px rgb(0 0 0 / 0.06), 0 2px 8px rgb(0 0 0 / 0.05);
  --nav-height: 64px;
  color-scheme: light dark;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #101512;
    --surface: #18201b;
    --surface-2: #212b25;
    --text: #e6ece8;
    --muted: #9db0a4;
    --line: #2e3a33;
    --accent: #5ec28f;
    --accent-text: #08130d;
    --accent-soft: #1d3a2b;
    --danger: #ff8a80;
    --danger-soft: #3b1c1a;
    --ok: #5ec28f;
    --shadow: none;
  }
}

*, *::before, *::after { box-sizing: border-box; }

html { -webkit-text-size-adjust: 100%; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 16px/1.5 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  padding-bottom: calc(var(--nav-height) + env(safe-area-inset-bottom));
}

a { color: var(--accent); }
h1 { font-size: 1.6rem; line-height: 1.2; margin: 0.25rem 0; }
h2 { font-size: 1rem; margin: 1.25rem 0 0.5rem; color: var(--muted); text-transform: uppercase; letter-spacing: 0.04em; }
p { margin: 0.5rem 0; }

:focus-visible { outline: 3px solid var(--focus); outline-offset: 2px; }

.skip-link { position: absolute; left: -999px; top: 0; }
.skip-link:focus { left: 8px; top: 8px; z-index: 10; background: var(--surface); padding: 8px 12px; }
.visually-hidden { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; }

.connection { margin: 0; padding: 8px 16px; background: var(--danger-soft); color: var(--danger); text-align: center; }

main { display: block; max-width: 720px; margin: 0 auto; padding: 16px; outline: none; }
main.narrow { max-width: 420px; }

/* Header on wide screens, bottom bar on phones. */
.topbar { display: flex; align-items: center; justify-content: space-between; padding: 8px 16px; background: var(--surface); border-bottom: 1px solid var(--line); }
.brand { display: flex; align-items: center; gap: 8px; color: var(--text); text-decoration: none; font-weight: 700; font-size: 1.1rem; }
.topbar nav { position: fixed; left: 0; right: 0; bottom: 0; z-index: 5; display: flex; justify-content: space-around; background: var(--surface); border-top: 1px solid var(--line); padding-bottom: env(safe-area-inset-bottom); }
.topbar nav a, .navbutton { flex: 1; display: flex; flex-direction: column; align-items: center; gap: 2px; padding: 8px 4px; min-height: var(--nav-height); color: var(--muted); text-decoration: none; font: inherit; font-size: 0.8rem; background: none; border: 0; cursor: pointer; }
.topbar nav a[aria-current="page"] { color: var(--accent); font-weight: 600; }

@media (min-width: 720px) {
  body { padding-bottom: 0; }
  .topbar nav { position: static; border: 0; gap: 4px; padding: 0; }
  .topbar nav a, .navbutton { flex-direction: row; min-height: 0; padding: 8px 12px; font-size: 0.95rem; border-radius: 8px; }
  .topbar nav a:hover, .navbutton:hover { background: var(--surface-2); }
}

.icon { width: 22px; height: 22px; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; flex: none; }

/* Buttons */
.button { display: inline-flex; align-items: center; justify-content: center; gap: 6px; min-height: 44px; padding: 8px 16px; border-radius: 10px; border: 1px solid var(--line); background: var(--surface); color: var(--text); font: inherit; font-weight: 600; text-decoration: none; cursor: pointer; }
.button:hover { background: var(--surface-2); }
.button.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-text); }
.button.primary:hover { filter: brightness(1.08); }
.button.danger { color: var(--danger); }
.button.small { min-height: 36px; padding: 4px 12px; font-size: 0.9rem; }
.button:disabled { opacity: 0.6; cursor: default; }
.iconbutton { display: inline-flex; align-items: center; justify-content: center; width: 44px; height: 44px; border: 0; border-radius: 10px; background: none; color: var(--muted); cursor: pointer; }
.iconbutton:hover:not(:disabled) { background: var(--surface-2); color: var(--text); }
.iconbutton:disabled { opacity: 0.3; cursor: default; }

/* Forms */
.field { display: flex; flex-direction: column; gap: 4px; margin: 0 0 12px; min-width: 0; }
.field label, legend { font-weight: 600; font-size: 0.9rem; }
.field small { color: var(--muted); }
input, select, textarea { font: inherit; color: var(--text); background: var(--surface); border: 1px solid var(--line); border-radius: 10px; padding: 10px 12px; min-height: 44px; width: 100%; }
input[type="checkbox"] { width: 24px; height: 24px; min-height: 0; accent-color: var(--accent); flex: none; margin: 0; cursor: pointer; }
.row { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }
.row > .field:last-child:nth-child(3) { grid-column: 1 / -1; }
@media (min-width: 560px) { .row { grid-template-columns: 1fr 1fr 2fr; } .row > .field:last-child:nth-child(3) { grid-column: auto; } }
.inline-form { display: flex; gap: 8px; margin: 12px 0; }
.inline-form input { flex: 1; }
.actions { display: flex; gap: 8px; justify-content: flex-end; flex-wrap: wrap; margin-top: 12px; }
.welcome .actions { justify-content: center; }

.notice { padding: 10px 12px; border-radius: 10px; background: var(--surface-2); }
.notice.error { background: var(--danger-soft); color: var(--danger); }
.notice.success { background: var(--accent-soft); }
.error-text { color: var(--danger); }
.muted { color: var(--muted); }
.empty { color: var(--muted); padding: 24px 0; text-align: center; }
.alt { text-align: center; margin-top: 16px; }
.lead { font-size: 1.1rem; }
.welcome { text-align: center; padding-top: 48px; }
.back { margin: 0; }
.back a { text-decoration: none; }

.page-head { display: flex; align-items: center; justify-content: space-between; gap: 8px; flex-wrap: wrap; }
.head-tools { display: flex; gap: 6px; flex-wrap: wrap; }

/* Cards for lists */
.cards { list-style: none; margin: 0; padding: 0; display: grid; gap: 8px; }
.card { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); box-shadow: var(--shadow); }
.list-card { display: flex; align-items: center; gap: 4px; padding-right: 4px; }
.card-link { flex: 1; min-width: 0; display: flex; flex-direction: column; padding: 12px 16px; color: var(--text); text-decoration: none; }
.card-title { font-weight: 600; font-size: 1.05rem; overflow-wrap: anywhere; }
.card-tools { display: flex; align-items: center; }
.archived { margin-top: 24px; }
.archived summary { cursor: pointer; color: var(--muted); padding: 8px 0; }

.menu { position: relative; }
.menu summary { list-style: none; display: inline-flex; align-items: center; justify-content: center; width: 44px; height: 44px; border-radius: 10px; cursor: pointer; color: var(--muted); }
.menu summary::-webkit-details-marker { display: none; }
.menu-panel { position: absolute; right: 0; top: 46px; z-index: 4; min-width: 150px; background: var(--surface); border: 1px solid var(--line); border-radius: 10px; box-shadow: 0 6px 24px rgb(0 0 0 / 0.15); padding: 4px; display: flex; flex-direction: column; }
.menu-item { text-align: left; padding: 10px 12px; border: 0; background: none; font: inherit; color: var(--text); border-radius: 8px; cursor: pointer; }
.menu-item:hover { background: var(--surface-2); }
.menu-item.danger { color: var(--danger); }

/* Adding items */
.add-item { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); padding: 12px; margin: 12px 0; box-shadow: var(--shadow); }
.add-item .button { width: 100%; }
.add-message { min-height: 1.5em; margin-top: 6px; color: var(--muted); font-size: 0.9rem; }

/* Items */
.items { list-style: none; margin: 0; padding: 0; background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); overflow: hidden; }
.item { display: flex; align-items: center; gap: 4px; padding: 4px 4px 4px 12px; border-top: 1px solid var(--line); }
.item:first-child { border-top: 0; }
.item-text { flex: 1; min-width: 0; display: flex; flex-wrap: wrap; align-items: baseline; column-gap: 8px; padding: 10px 4px 10px 8px; cursor: pointer; }
.item-name { font-weight: 600; overflow-wrap: anywhere; }
.item-qty { color: var(--muted); }
.item-note, .item-sources { flex-basis: 100%; color: var(--muted); font-size: 0.85rem; }
.item.purchased .item-name { text-decoration: line-through; color: var(--muted); font-weight: 400; }
.badge { font-size: 0.75rem; font-weight: 600; padding: 1px 8px; border-radius: 999px; background: var(--accent-soft); color: var(--accent); }
.group.bought h2 { margin-top: 2rem; }

/* Combined view filter */
.filters { border: 0; padding: 0; margin: 12px 0; }
.chips { display: flex; flex-wrap: wrap; gap: 8px; margin-top: 6px; }
.chip { display: inline-flex; align-items: center; gap: 6px; padding: 6px 12px; border: 1px solid var(--line); border-radius: 999px; background: var(--surface); }
.chip label { cursor: pointer; }
.chip input { width: 18px; height: 18px; }

/* Dialogs */
dialog.popup { border: 0; border-radius: 16px; padding: 20px; width: min(92vw, 420px); background: var(--surface); color: var(--text); box-shadow: 0 12px 48px rgb(0 0 0 / 0.3); }
dialog.popup h2 { margin-top: 0; color: var(--text); text-transform: none; letter-spacing: 0; font-size: 1.2rem; }
dialog::backdrop { background: rgb(0 0 0 / 0.4); }

.noscript { padding: 16px; text-align: center; }

@media (prefers-reduced-motion: reduce) {
  * { scroll-behavior: auto !important; transition: none !important; }
}

/* Prices */
.item-price { flex-basis: 100%; font-size: 0.85rem; color: var(--accent); font-weight: 600; }
.item .button.small { flex: none; }
dialog.popup.wide { width: min(96vw, 560px); max-height: 90vh; }
.shop-prices, .results { list-style: none; margin: 0; padding: 0; }
.shop-prices > li, .results > li { padding: 12px 0; border-top: 1px solid var(--line); }
.shop-prices > li:first-child, .results > li:first-child { border-top: 0; }
.shop-prices h3 { margin: 0 0 4px; font-size: 1rem; }
.offer { display: flex; flex-direction: column; gap: 2px; margin-bottom: 6px; }
.offer-name { font-weight: 600; }
.was { color: var(--danger); font-size: 0.9rem; }
.offer-tools { display: flex; flex-wrap: wrap; gap: 6px; }
.results .button { margin-top: 4px; }
.estimate { padding: 12px 16px; margin: 12px 0; }
.estimate h2 { margin-top: 0; }
.mix { margin: 4px 0 8px; }
.estimate-shops { list-style: none; margin: 0; padding: 0; }
.estimate-shops li { display: grid; grid-template-columns: 1fr auto; column-gap: 12px; padding: 6px 0; border-top: 1px solid var(--line); }
.estimate-shops li .muted { grid-column: 1 / -1; font-size: 0.85rem; }
.small { font-size: 0.85rem; }

/* Settings */
fieldset.plain { border: 0; padding: 0; margin: 0 0 16px; }
.shop { padding: 12px 16px; }
.shop-head { display: flex; align-items: center; gap: 12px; }
.shop-head label { display: flex; flex-direction: column; cursor: pointer; }
.shop-details { margin-top: 12px; }
