:root {
  color-scheme: light dark;
  --bg: #f5efe6;
  --surface: #fffaf3;
  --surface-2: #efe5d7;
  --ink: #2a1c1f;
  --muted: #74635f;
  --line: #e0d3c2;
  --accent: #7a1f3d;
  --accent-strong: #5e1730;
  --on-accent: #fff8f2;
  --gold: #8f6a2c;
  --gold-soft: #f3e6cc;
  --ok: #2f6b40;
  --warn: #8c5a10;
  --bad: #9a3324;
  --hold: #3f5f80;
  --shadow: 0 1px 2px rgb(60 30 30 / 6%), 0 8px 24px rgb(60 30 30 / 8%);
  --serif: "Iowan Old Style", "Palatino Linotype", Palatino, "Book Antiqua", Georgia, serif;
  --sans: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  --radius: 14px;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #140e10;
    --surface: #1f1719;
    --surface-2: #2b2023;
    --ink: #f1e8df;
    --muted: #ab9a9c;
    --line: #382a2e;
    --accent: #a12d53;
    --accent-strong: #c2476a;
    --on-accent: #fff8f2;
    --gold: #d5ab63;
    --gold-soft: #3a2e1c;
    --ok: #7dc491;
    --warn: #e2b05c;
    --bad: #ec8f7d;
    --hold: #94b6d8;
    --shadow: 0 1px 2px rgb(0 0 0 / 30%), 0 10px 30px rgb(0 0 0 / 35%);
  }
}

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

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

body {
  margin: 0;
  background: var(--bg);
  color: var(--ink);
  font: 16px/1.5 var(--sans);
  -webkit-font-smoothing: antialiased;
  overscroll-behavior-y: none;
}

h1, h2, h3, h4, p, dl, dd, ul { margin: 0; }
h1, h2 { font-family: var(--serif); font-weight: 600; letter-spacing: -0.01em; text-wrap: balance; }
h1 { font-size: 2rem; line-height: 1.15; }
h2 { font-size: 1.35rem; line-height: 1.2; }
a { color: var(--accent-strong); }
button, input, select, textarea { font: inherit; color: inherit; }
button { cursor: pointer; }
:focus-visible { outline: 2px solid var(--accent-strong); outline-offset: 2px; }

.muted { color: var(--muted); }
.icon { display: inline-flex; width: 1.25em; height: 1.25em; flex: none; }
.icon svg { width: 100%; height: 100%; }

/* ---------- shell ---------- */

.shell { min-height: 100dvh; }

main {
  max-width: 880px;
  margin: 0 auto;
  padding: calc(20px + env(safe-area-inset-top)) 16px calc(96px + env(safe-area-inset-bottom));
}

.nav {
  position: fixed;
  inset: auto 0 0;
  z-index: 5;
  background: color-mix(in srgb, var(--surface) 92%, transparent);
  backdrop-filter: blur(12px);
  border-top: 1px solid var(--line);
  padding-bottom: env(safe-area-inset-bottom);
}
.nav-brand { display: none; }
.nav-items { display: grid; grid-template-columns: repeat(4, 1fr); }
.nav-item {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 2px;
  padding: 9px 4px 8px;
  border: 0;
  background: none;
  color: var(--muted);
  font-size: 0.72rem;
  font-weight: 500;
}
.nav-item .icon { width: 24px; height: 24px; }
.nav-item.active { color: var(--accent-strong); }

@media (min-width: 900px) {
  .shell { display: grid; grid-template-columns: 232px 1fr; }
  .nav {
    position: sticky;
    inset: 0 auto auto;
    height: 100dvh;
    border-top: 0;
    border-right: 1px solid var(--line);
    background: var(--surface);
    backdrop-filter: none;
    padding: 24px 14px;
  }
  .nav-brand {
    display: flex;
    align-items: center;
    gap: 10px;
    padding: 0 8px 22px;
    font-family: var(--serif);
    font-size: 1.15rem;
    font-weight: 600;
  }
  .nav-brand img { border-radius: 9px; }
  .nav-items { display: flex; flex-direction: column; gap: 2px; }
  .nav-item {
    flex-direction: row;
    gap: 12px;
    padding: 10px 12px;
    border-radius: 10px;
    font-size: 0.95rem;
  }
  .nav-item .icon { width: 20px; height: 20px; }
  .nav-item:hover { background: var(--surface-2); color: var(--ink); }
  .nav-item.active { background: var(--surface-2); }
  main { padding: 40px 40px 64px; width: 100%; }
  .eyebrow.brand-eyebrow { display: none; }
}

/* ---------- common pieces ---------- */

.view-head {
  display: flex;
  align-items: flex-end;
  justify-content: space-between;
  gap: 12px;
  margin-bottom: 20px;
}

.eyebrow {
  display: flex;
  align-items: center;
  gap: 7px;
  color: var(--gold);
  font-size: 0.74rem;
  font-weight: 600;
  letter-spacing: 0.09em;
  text-transform: uppercase;
}

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 7px;
  min-height: 44px;
  padding: 0 16px;
  border: 1px solid var(--line);
  border-radius: 999px;
  background: var(--surface);
  font-weight: 550;
  white-space: nowrap;
  text-decoration: none;
}
.btn:hover:not(:disabled) { border-color: var(--muted); }
.btn:disabled { opacity: 0.45; cursor: default; }
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--on-accent); }
.btn.primary:hover:not(:disabled) { background: var(--accent-strong); border-color: var(--accent-strong); }
.btn.accent { background: var(--gold-soft); border-color: transparent; color: var(--ink); }
.btn.danger { background: var(--bad); border-color: var(--bad); color: var(--bg); }
.btn.ghost { border-color: transparent; background: none; }
.btn.danger-text { color: var(--bad); }
.btn.small { min-height: 34px; padding: 0 12px; font-size: 0.88rem; }

.icon-btn {
  display: inline-grid;
  place-items: center;
  width: 44px;
  height: 44px;
  flex: none;
  border: 1px solid var(--line);
  border-radius: 50%;
  background: var(--surface);
}
.icon-btn:disabled { opacity: 0.4; cursor: default; }

.actions { display: flex; justify-content: flex-end; align-items: center; gap: 10px; margin-top: 20px; }
.actions.start { justify-content: flex-start; }
.actions.centre { justify-content: center; }
.actions.spread { justify-content: space-between; }
.actions.wrap, .actions.centre { flex-wrap: wrap; }
.actions .actions { margin-top: 0; }

.card {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 20px;
  margin-bottom: 16px;
}
.card > * + * { margin-top: 12px; }

.dot {
  display: inline-block;
  width: 12px;
  height: 12px;
  flex: none;
  border-radius: 50%;
  background: #8a8a8a;
  box-shadow: inset 0 0 0 1px rgb(0 0 0 / 14%);
}
.t-red { background: #7a1f3d; }
.t-white { background: #e6d48a; }
.t-rose { background: #eda1ac; }
.t-sparkling { background: radial-gradient(circle at 35% 35%, #fff 0 18%, #ecdc9c 20%); }
.t-sweet { background: #d2952b; }
.t-fortified { background: #4e2416; }
.t-orange { background: #dc8234; }

.pill {
  display: inline-block;
  padding: 2px 9px;
  border-radius: 999px;
  font-size: 0.76rem;
  font-weight: 600;
  white-space: nowrap;
  background: color-mix(in srgb, currentColor 13%, transparent);
}
.s-ready { color: var(--ok); }
.s-soon { color: var(--warn); }
.s-past { color: var(--bad); }
.s-young { color: var(--hold); }
.s-unknown { color: var(--muted); }

.stars { color: var(--gold); letter-spacing: 0.05em; }

.spinner {
  display: inline-block;
  width: 1em;
  height: 1em;
  margin-right: 8px;
  vertical-align: -0.15em;
  border: 2px solid color-mix(in srgb, currentColor 25%, transparent);
  border-top-color: currentColor;
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }
@media (prefers-reduced-motion: reduce) { .spinner { animation-duration: 3s; } }

.hint { color: var(--muted); font-size: 0.88rem; }
.hint:empty { display: none; }
.working { color: var(--gold); }
.error { color: var(--bad); }
.ok { color: var(--ok); }

/* ---------- cellar ---------- */

.stats {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(120px, 1fr));
  gap: 10px;
  margin-bottom: 18px;
}
.stat {
  display: flex;
  flex-direction: column;
  padding: 12px 14px;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: var(--surface);
  text-align: left;
}
.stat strong { font-family: var(--serif); font-size: 1.6rem; line-height: 1.15; font-variant-numeric: tabular-nums; }
.stat span { color: var(--muted); font-size: 0.82rem; }
.stat.link:hover { border-color: var(--muted); }

.toolbar { display: flex; gap: 10px; margin-bottom: 12px; }
.search {
  display: flex;
  align-items: center;
  gap: 8px;
  flex: 1;
  min-width: 0;
  padding: 0 14px;
  border: 1px solid var(--line);
  border-radius: 999px;
  background: var(--surface);
  color: var(--muted);
}
.search input {
  flex: 1;
  min-width: 0;
  height: 44px;
  border: 0;
  outline: 0;
  background: none;
  color: var(--ink);
}
.search:focus-within { outline: 2px solid var(--accent-strong); outline-offset: 1px; }
.toolbar select { max-width: 44%; border-radius: 999px; }

.chips { display: flex; gap: 8px; margin: 0 -16px 14px; padding: 2px 16px; overflow-x: auto; scrollbar-width: none; }
.chips::-webkit-scrollbar { display: none; }
.chip {
  display: inline-flex;
  align-items: center;
  gap: 7px;
  min-height: 36px;
  padding: 0 14px;
  border: 1px solid var(--line);
  border-radius: 999px;
  background: var(--surface);
  font-size: 0.9rem;
  white-space: nowrap;
}
.chip.active { background: var(--ink); border-color: var(--ink); color: var(--bg); }

.list {
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: var(--surface);
  overflow: hidden;
}
.list > * + * { border-top: 1px solid var(--line); }
.none-found { padding: 22px 16px; text-align: center; }

.wine, .entry {
  display: flex;
  align-items: flex-start;
  gap: 12px;
  width: 100%;
  padding: 14px 16px;
  border: 0;
  background: none;
  text-align: left;
}
.wine:hover { background: var(--surface-2); }
.wine > .dot, .entry > .dot { margin-top: 7px; }
.wine-main, .entry-main { display: flex; flex-direction: column; gap: 2px; flex: 1; min-width: 0; }
.wine-name { font-family: var(--serif); font-size: 1.08rem; font-weight: 600; line-height: 1.3; }
.vintage { color: var(--gold); font-variant-numeric: tabular-nums; }
.wine-sub { color: var(--muted); font-size: 0.9rem; }
.wine-meta { display: flex; flex-wrap: wrap; align-items: center; gap: 8px; margin-top: 5px; }
.loc { color: var(--muted); font-size: 0.82rem; }
.qty { font-family: var(--serif); font-size: 1.15rem; font-weight: 600; font-variant-numeric: tabular-nums; white-space: nowrap; }
.qty.none { color: var(--muted); font-family: var(--sans); font-size: 0.8rem; font-weight: 500; }
.entry { align-items: center; }
.entry-note { margin-top: 4px; font-size: 0.92rem; }

.empty { max-width: 420px; margin: 9vh auto 0; text-align: center; }
.empty > * + * { margin-top: 10px; }
.empty p { color: var(--muted); }
.empty-mark {
  display: inline-grid;
  place-items: center;
  width: 72px;
  height: 72px;
  border-radius: 50%;
  background: var(--gold-soft);
  color: var(--gold);
}
.empty-mark .icon { width: 34px; height: 34px; }

.lead { margin-bottom: 14px; }
.group-title { display: flex; align-items: baseline; gap: 10px; margin: 26px 0 2px; }
.group-title:first-of-type { margin-top: 4px; }
.count { color: var(--muted); font: 500 0.95rem var(--sans); }
.group-blurb { margin-bottom: 10px; font-size: 0.92rem; }

.fold { margin-top: 22px; }
.fold summary { padding: 8px 0; font-weight: 600; cursor: pointer; }
.fold > .list { margin-top: 8px; }
.fold.inset { margin-top: 12px; }
.fold.inset summary { font-weight: 500; font-size: 0.92rem; color: var(--muted); }

.steps { padding-left: 1.1em; color: var(--muted); }
.steps li + li { margin-top: 6px; }
.steps strong { color: var(--ink); }

/* ---------- forms ---------- */

.field { display: flex; flex-direction: column; gap: 5px; margin-top: 14px; }
.field-label { font-size: 0.84rem; font-weight: 600; color: var(--muted); }
input[type="text"], input[type="number"], input[type="date"], input[type="password"], select, textarea {
  width: 100%;
  min-height: 44px;
  padding: 9px 12px;
  border: 1px solid var(--line);
  border-radius: 10px;
  background: var(--bg);
}
textarea { resize: vertical; line-height: 1.45; }
input.narrow { width: 90px; }
.grid2 { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }
.paste { margin-top: 12px; font-size: 0.92rem; }

.quick {
  padding: 14px;
  border-radius: 12px;
  background: var(--gold-soft);
}
.quick-title { font-weight: 600; margin-bottom: 8px; }
.quick-row { display: flex; gap: 8px; }
.quick-row + .quick-row { margin-top: 8px; }
.quick-row input[type="text"] { background: var(--surface); }
.quick .btn.accent { background: var(--ink); color: var(--bg); }
.quick .hint { margin-top: 10px; }

/* ---------- dialogs ---------- */

dialog.sheet {
  width: 100%;
  max-width: 100%;
  max-height: 94dvh;
  margin: auto 0 0;
  padding: 0;
  border: 0;
  border-radius: 20px 20px 0 0;
  background: var(--surface);
  color: var(--ink);
  box-shadow: var(--shadow);
  overflow: hidden;
}
dialog.sheet[open] { display: flex; }
dialog.sheet::backdrop { background: rgb(20 10 12 / 55%); }
.sheet-body {
  width: 100%;
  padding: 20px 18px calc(20px + env(safe-area-inset-bottom));
  overflow-y: auto;
  overscroll-behavior: contain;
}
.sheet-body > p { margin-top: 10px; }
.sheet-head { display: flex; align-items: flex-start; justify-content: space-between; gap: 12px; margin-bottom: 14px; }
.sheet-head h2 { font-size: 1.55rem; margin-top: 4px; }
.sheet-head .sub { color: var(--muted); }
.sheet-head .sub:empty { display: none; }

@media (min-width: 640px) {
  dialog.sheet { width: min(640px, calc(100vw - 48px)); max-height: 88dvh; margin: auto; border-radius: 18px; }
  dialog.sheet.compact { width: min(440px, calc(100vw - 48px)); }
  .sheet-body { padding: 26px; }
}

.stock {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 12px;
  border-radius: var(--radius);
  background: var(--surface-2);
}
.stepper { display: flex; align-items: center; gap: 12px; }
.stock-count strong { font-family: var(--serif); font-size: 1.5rem; font-variant-numeric: tabular-nums; }

.facts, .research-parts {
  display: grid;
  grid-template-columns: minmax(96px, max-content) 1fr;
  gap: 8px 16px;
  margin-top: 18px;
}
.facts dt, .research-parts dt { color: var(--muted); font-size: 0.88rem; padding-top: 1px; }
.notes { margin-top: 16px; padding-left: 12px; border-left: 3px solid var(--line); white-space: pre-line; }

.research {
  margin-top: 22px;
  padding: 16px;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: var(--bg);
}
.research > * + * { margin-top: 12px; }
.research h3 { display: flex; align-items: center; gap: 8px; font-size: 0.82rem; letter-spacing: 0.09em; text-transform: uppercase; color: var(--gold); }
.research h4 { font-size: 0.82rem; color: var(--muted); margin-bottom: 4px; }
.summary { font-family: var(--serif); font-size: 1.1rem; line-height: 1.5; }
.suggest {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 10px 12px;
  border-radius: 10px;
  background: var(--gold-soft);
  font-size: 0.94rem;
}
.confidence { font-size: 0.9rem; color: var(--muted); }
.sources ul { padding-left: 1.1em; font-size: 0.92rem; }
.sources a { overflow-wrap: anywhere; }
.research-foot { display: flex; align-items: center; justify-content: space-between; gap: 12px; font-size: 0.88rem; }

@media (max-width: 480px) {
  .research-parts { grid-template-columns: 1fr; gap: 2px; }
  .research-parts dd { margin-bottom: 10px; }
}

.preview { max-height: 42dvh; overflow-y: auto; margin-top: 12px; }

/* ---------- toast ---------- */

.toast {
  position: fixed;
  inset: auto auto calc(84px + env(safe-area-inset-bottom)) 50%;
  transform: translateX(-50%);
  z-index: 10;
  width: max-content;
  max-width: min(520px, calc(100vw - 32px));
  margin: 0;
  padding: 11px 16px;
  border: 0;
  border-radius: 12px;
  background: var(--ink);
  color: var(--bg);
  font-size: 0.94rem;
  box-shadow: var(--shadow);
  pointer-events: none;
}
.toast:not([popover]):not(.show) { display: none; }
.toast.show { display: block; }
.toast.error { background: var(--bad); }
@media (min-width: 900px) { .toast { bottom: 32px; } }
