:root {
  --bg: #f6f7f5;
  --surface: #ffffff;
  --text: #1c2321;
  --muted: #5f6b67;
  --border: #dde3e0;
  --accent: #0f766e;
  --accent-text: #ffffff;
  --ok-bg: #e7f5ee; --ok-fg: #1d6b43;
  --soon-bg: #fff3d6; --soon-fg: #8a5a00;
  --exp-bg: #fde8e7; --exp-fg: #a3271f;
  --radius: 12px;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #111615;
    --surface: #1a2120;
    --text: #e6ece9;
    --muted: #9aa8a3;
    --border: #2c3634;
    --accent: #2dd4bf;
    --accent-text: #062a26;
    --ok-bg: #143323; --ok-fg: #8ee0b2;
    --soon-bg: #3a2c0b; --soon-fg: #f5c96a;
    --exp-bg: #3d1614; --exp-fg: #ff9d95;
    color-scheme: dark;
  }
}
:root[data-theme="dark"] {
  --bg: #111615; --surface: #1a2120; --text: #e6ece9; --muted: #9aa8a3; --border: #2c3634;
  --accent: #2dd4bf; --accent-text: #062a26;
  --ok-bg: #143323; --ok-fg: #8ee0b2; --soon-bg: #3a2c0b; --soon-fg: #f5c96a; --exp-bg: #3d1614; --exp-fg: #ff9d95;
  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.5 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
}
a { color: var(--accent); }
h1 { font-size: 1.35rem; margin: 0 0 .75rem; }
h2 { font-size: 1.1rem; margin: 1.25rem 0 .5rem; }
code { font-size: .9em; }

.topbar {
  position: sticky; top: 0; z-index: 1;
  display: flex; align-items: center; justify-content: space-between; gap: 1rem;
  padding: calc(.6rem + env(safe-area-inset-top)) 16px .6rem;
  background: var(--surface); border-bottom: 1px solid var(--border);
}
.brand { font-weight: 700; color: var(--text); text-decoration: none; }
.topbar nav { display: flex; gap: .25rem; }
.topbar nav a {
  padding: .4rem .65rem; border-radius: 8px; color: var(--muted); text-decoration: none; font-weight: 500;
}
.topbar nav a[aria-current="page"] { background: var(--bg); color: var(--text); }
.topbar nav .logout { margin: 0; }
.topbar nav .logout button { background: transparent; color: var(--muted); padding: .4rem .65rem; min-height: 0; }

main { max-width: 720px; margin: 0 auto; padding: 16px 16px calc(32px + env(safe-area-inset-bottom)); }
.pull-refresh { display: none; }

.card {
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  padding: 16px; margin-bottom: 16px;
}
.add-panel summary {
  display: flex; align-items: center; justify-content: space-between; gap: 1rem;
  list-style: none; cursor: pointer; font-weight: 700;
  min-height: 44px; color: var(--accent);
}
.add-panel summary::-webkit-details-marker { display: none; }
.add-panel summary span { font-size: 1.4rem; line-height: 1; }
.add-panel[open] summary { border-bottom: 1px solid var(--border); padding-bottom: .55rem; margin-bottom: .75rem; }
.add-panel[open] summary span { transform: rotate(45deg); }
.add-panel summary:focus-visible { outline: 2px solid var(--accent); outline-offset: 3px; }
.row { display: flex; align-items: center; justify-content: space-between; gap: 1rem; flex-wrap: wrap; }
.row h1 { margin: 0; }

label { display: flex; flex-direction: column; gap: .3rem; font-weight: 500; font-size: .95rem; }
.hint { color: var(--muted); font-weight: 400; font-size: .85rem; }
input, textarea, select {
  font: inherit; color: var(--text); background: var(--bg);
  border: 1px solid var(--border); border-radius: 8px; padding: .6rem .7rem; width: 100%; min-height: 44px;
}
input:focus, textarea:focus, select:focus { outline: 2px solid var(--accent); outline-offset: 1px; }

button {
  font: inherit; font-weight: 600; cursor: pointer;
  background: var(--accent); color: var(--accent-text);
  border: 0; border-radius: 8px; padding: .6rem 1rem; min-height: 44px;
}
button.secondary { background: transparent; color: var(--text); border: 1px solid var(--border); }
button:active { transform: translateY(1px); }

.item-form, .item-fields { display: grid; gap: .85rem; }
.item-form { margin-top: 1rem; }
.item-fields { grid-template-columns: repeat(2, minmax(0, 1fr)); }
.item-fields label:first-child, .item-fields label:nth-child(2) { grid-column: span 1; }
.save-item { justify-self: start; min-width: 10rem; }
.photo-tools { margin: .25rem 0 1rem; }
.photo-actions { display: flex; gap: .6rem; flex-wrap: wrap; }
.photo-button {
  display: inline-flex; align-items: center; justify-content: center; cursor: pointer;
  padding: .65rem .9rem; min-height: 44px; border-radius: 8px;
  background: var(--accent); color: var(--accent-text); font-weight: 600;
}
.photo-button.secondary { background: var(--bg); color: var(--text); border: 1px solid var(--border); }
.photo-button:focus-within { outline: 2px solid var(--accent); outline-offset: 2px; }
.visually-hidden {
  position: absolute; width: 1px; height: 1px; padding: 0; margin: -1px;
  overflow: hidden; clip: rect(0, 0, 0, 0); white-space: nowrap; border: 0;
}
.photo-help { margin: .55rem 0 0; }
.photo-result { display: flex; align-items: flex-start; gap: .8rem; margin-top: .75rem; padding: .7rem; border: 1px solid var(--border); border-radius: 10px; }
.photo-result[hidden] { display: none; }
.photo-preview { width: 64px; height: 64px; object-fit: cover; border-radius: 8px; background: var(--bg); }
.photo-status { margin: 0; }
.brand-alternatives { display: flex; gap: .4rem; align-items: center; flex-wrap: wrap; margin-top: .45rem; font-size: .85rem; }
.brand-alternatives button { min-height: 36px; padding: .25rem .5rem; font-size: .85rem; }
.mobile-signout { display: none; }
.settings-form { display: grid; gap: .9rem; }
.settings-form button { justify-self: start; }
.login-card { max-width: 420px; margin: 8vh auto 0; }
.login-card form { display: grid; gap: .9rem; }
.login-card form button { margin-top: .3rem; }

.count {
  display: inline-block; min-width: 1.6em; padding: 0 .4em; margin-left: .25rem;
  font-size: .8rem; text-align: center; border-radius: 999px; background: var(--border); color: var(--muted);
}

.items { list-style: none; margin: 0; padding: 0; display: grid; gap: .5rem; }
.item {
  display: grid; grid-template-columns: 1fr auto; gap: .35rem .75rem; align-items: center;
  background: var(--surface); border: 1px solid var(--border); border-left: 4px solid var(--border);
  border-radius: var(--radius); padding: .75rem .9rem;
}
.item.soon { border-left-color: var(--soon-fg); }
.item.expired { border-left-color: var(--exp-fg); }
.item-name { font-weight: 600; }
.item-brand { color: var(--muted); margin-left: .4rem; font-size: .9rem; }
.item-qty { color: var(--muted); margin-left: .4rem; font-size: .9rem; }
.item-spot { display: block; color: var(--muted); font-size: .82rem; margin-top: .15rem; }
.location-group { margin-bottom: 1.35rem; }
.location-group h3 { font-size: 1rem; margin: 0 0 .5rem; }
.item-when { text-align: right; display: flex; flex-direction: column; align-items: flex-end; }
.date { color: var(--muted); font-size: .8rem; font-variant-numeric: tabular-nums; }
.item-actions { grid-column: 1 / -1; display: flex; gap: .5rem; align-items: center; justify-content: flex-end; }
.item-actions form { margin: 0; }
.item-actions button { padding: .35rem .75rem; min-height: 36px; font-size: .9rem; }
.btn-link { padding: .35rem .5rem; font-size: .9rem; }

.badge {
  display: inline-block; padding: .1rem .55rem; border-radius: 999px; font-size: .8rem; font-weight: 600;
  background: var(--ok-bg); color: var(--ok-fg);
}
.badge.soon, .badge.dry-run, .badge.partial, .badge.skipped { background: var(--soon-bg); color: var(--soon-fg); }
.badge.expired, .badge.failed { background: var(--exp-bg); color: var(--exp-fg); }
.badge.nothing { background: var(--border); color: var(--muted); }

.flash { padding: .7rem .9rem; border-radius: 8px; margin: 0 0 16px; font-weight: 500; }
.flash.ok { background: var(--ok-bg); color: var(--ok-fg); }
.flash.err { background: var(--exp-bg); color: var(--exp-fg); }
.notice { background: var(--soon-bg); color: var(--soon-fg); padding: .7rem .9rem; border-radius: 8px; }
.empty, .meta { color: var(--muted); }
.meta { font-size: .85rem; }

.idea h2 { margin-top: 0; }
.idea ol { padding-left: 1.25rem; margin-bottom: 0; }
.uses { color: var(--muted); font-size: .9rem; }

.status { padding-left: 1.1rem; }
.status li { margin-bottom: .35rem; }
.good { color: var(--ok-fg); }
.warn { color: var(--soon-fg); }

.log { list-style: none; padding: 0; margin: 0; display: grid; gap: .75rem; }
.log li { border-top: 1px solid var(--border); padding-top: .75rem; }
.log li:first-child { border-top: 0; padding-top: 0; }
.log pre {
  white-space: pre-wrap; word-break: break-word; margin: .4rem 0 0;
  background: var(--bg); padding: .6rem; border-radius: 8px; font-size: .85rem;
}

@media (max-width: 600px) {
  html { overscroll-behavior-y: contain; }
  .pull-refresh {
    position: fixed; top: calc(48px + env(safe-area-inset-top)); left: 0; right: 0;
    z-index: 2; display: flex; align-items: center; justify-content: center; gap: .5rem;
    height: 76px; opacity: 0; pointer-events: none; color: var(--accent);
    font-size: .85rem; font-weight: 600; transition: opacity .12s ease-out;
  }
  .pull-refresh.visible { opacity: 1; }
  .pull-refresh svg { width: 40px; height: 40px; transform: scale(.82); transition: transform .12s ease-out; }
  .pull-refresh.ready svg { transform: scale(1); }
  .pull-refresh.refreshing svg { animation: ramen-refresh .55s ease-in-out infinite alternate; }
  @keyframes ramen-refresh { to { transform: translateY(-5px); } }
  .topbar { padding: calc(.5rem + env(safe-area-inset-top)) 16px .5rem; }
  .topbar nav {
    position: fixed; bottom: 0; left: 0; right: 0; z-index: 3;
    display: flex; justify-content: space-around; gap: 0;
    padding: .35rem .5rem calc(.35rem + env(safe-area-inset-bottom));
    border-top: 1px solid var(--border); background: var(--surface);
  }
  .topbar nav a { flex: 1; text-align: center; min-height: 44px; display: grid; place-items: center; }
  .topbar nav .logout { display: none; }
  .mobile-signout { display: block; text-align: center; margin: 1.2rem 0; }
  main { padding: 12px 12px calc(95px + env(safe-area-inset-bottom)); }
  .card { padding: 14px; }
  .item-fields { grid-template-columns: 1fr; }
  .save-item { width: 100%; }
  .photo-actions > label { flex: 1 1 125px; text-align: center; }
  .item { grid-template-columns: minmax(0, 1fr) auto; }
  .item-main { min-width: 0; overflow-wrap: anywhere; }
  .item-when { min-width: 90px; }
}
