/* Cellarium — sober, mobile-first stylesheet. No framework. */
:root {
  --wine: #5b1a32;
  --wine-2: #7b2a48;
  --gold: #c9a96e;
  --bg: #faf7f4;
  --surface: #ffffff;
  --text: #2a2226;
  --muted: #7a6f74;
  --line: #e7dfd9;
  --ok: #2f7d4f;
  --warn: #b7791f;
  --danger: #b3261e;
  --radius: 10px;
  --shadow: 0 1px 2px rgba(60, 20, 35, .06), 0 2px 8px rgba(60, 20, 35, .05);
  --phase-keep: #6b8fb5;
  --phase-drink: #4f9a6a;
  --phase-peak: #c9a227;
  --phase-decline: #c0612b;
  --phase-unknown: #b9b0b4;
  --tabbar-h: 0px;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #17121a; --surface: #211a23; --text: #efe7ea; --muted: #a99da3; --line: #3a3036;
    --wine: #c55b83; --wine-2: #d97a9d; --shadow: none; color-scheme: dark;
  }
}
* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0; background: var(--bg); color: var(--text);
  font: 15px/1.5 system-ui, -apple-system, "Segoe UI", Roboto, Ubuntu, sans-serif;
  padding-bottom: calc(var(--tabbar-h) + env(safe-area-inset-bottom));
}
a { color: var(--wine); }
h1 { font-size: 1.5rem; margin: .2em 0 .6em; line-height: 1.2; }
h2 { font-size: 1.1rem; margin: 0 0 .6em; }
h3 { font-size: .95rem; margin: 1.2em 0 .5em; color: var(--muted); text-transform: uppercase; letter-spacing: .04em; }
code { font-size: .9em; background: color-mix(in srgb, var(--line) 50%, transparent); padding: .1em .35em; border-radius: 4px; word-break: break-all; }
.skip { position: absolute; left: -999px; }
.skip:focus { left: 8px; top: 8px; z-index: 99; background: var(--surface); padding: 6px; }
.muted { color: var(--muted); }
.small { font-size: .85em; }
.block { display: block; }
.num { text-align: right; font-variant-numeric: tabular-nums; }
.pre { white-space: pre-line; }
.grow { flex: 1; min-width: 0; }
[hidden] { display: none !important; }

/* ---------- top bar & tab bar */
.topbar {
  position: sticky; top: 0; z-index: 20; display: flex; align-items: center; gap: 1rem;
  padding: .55rem 1rem; background: var(--wine); color: #fff;
}
.topbar a { color: #fff; text-decoration: none; }
.brand { display: flex; align-items: center; gap: .5rem; font-weight: 700; font-size: 1.1rem; letter-spacing: .02em; }
.brand img { width: 28px; height: 28px; border-radius: 6px; }
.top-nav { display: flex; gap: .25rem; flex: 1; }
.top-nav .nav-link { padding: .35rem .7rem; border-radius: 6px; opacity: .85; display: flex; gap: .4rem; align-items: center; }
.top-nav .nav-link.active, .top-nav .nav-link:hover { background: rgba(255, 255, 255, .15); opacity: 1; }
.top-tools { display: flex; align-items: center; gap: .6rem; margin-left: auto; }
.lang { background: transparent; color: inherit; border: 1px solid rgba(255, 255, 255, .35); border-radius: 6px; padding: .15rem .3rem; }
.lang option { color: #000; }
.user-chip { font-size: .9rem; opacity: .9; display: flex; gap: .4rem; align-items: center; }
.topbar .icon-btn { color: #fff; }
.tabbar { display: none; }
@media (max-width: 760px) {
  :root { --tabbar-h: 62px; }
  .top-nav, .user-chip { display: none; }
  .tabbar {
    display: flex; position: fixed; bottom: 0; left: 0; right: 0; z-index: 20;
    height: calc(var(--tabbar-h) + env(safe-area-inset-bottom)); padding-bottom: env(safe-area-inset-bottom);
    background: var(--surface); border-top: 1px solid var(--line);
  }
  .tabbar .nav-link {
    flex: 1; display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 2px;
    font-size: .72rem; color: var(--muted); text-decoration: none;
  }
  .tabbar .nav-link .icon { font-size: 1.25rem; line-height: 1; }
  .tabbar .nav-link.active { color: var(--wine); font-weight: 600; }
  .hide-sm { display: none; }
}

/* ---------- layout */
.page { max-width: 1100px; margin: 0 auto; padding: 1rem; outline: none; }
.page-head { display: flex; align-items: flex-start; justify-content: space-between; gap: 1rem; flex-wrap: wrap; }
.subtitle { margin: -.4em 0 .8em; color: var(--muted); }
.card {
  background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius);
  padding: 1rem; margin: 0 0 1rem; box-shadow: var(--shadow);
}
.card-head { display: flex; align-items: center; justify-content: space-between; gap: .75rem; flex-wrap: wrap; margin-bottom: .6rem; }
.card-head h2 { margin: 0; }
.columns { display: grid; gap: 1rem; grid-template-columns: repeat(auto-fit, minmax(300px, 1fr)); }
.columns .card { margin: 0; }
.columns { margin-bottom: 1rem; }
.stack > * + * { margin-top: .8rem; }
.grid-2 { display: grid; gap: .8rem; grid-template-columns: repeat(auto-fit, minmax(200px, 1fr)); align-items: end; }
.row-actions { display: flex; flex-wrap: wrap; gap: .4rem; align-items: center; }
.sticky-actions { position: sticky; bottom: calc(var(--tabbar-h) + .5rem); justify-content: flex-end; background: var(--bg); padding: .5rem 0; }
.back { display: inline-block; margin-bottom: .4rem; text-decoration: none; color: var(--muted); }
.loading, .empty { text-align: center; color: var(--muted); padding: 2.5rem 1rem; }
.empty .row-actions { justify-content: center; }

/* ---------- controls */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: .2rem;
  border: 1px solid var(--line); background: var(--surface); color: var(--text);
  padding: .5rem .9rem; border-radius: 8px; font: inherit; cursor: pointer; text-decoration: none; min-height: 40px;
}
.btn:hover { border-color: var(--wine); }
.btn.primary { background: var(--wine); border-color: var(--wine); color: #fff; }
.btn.primary:hover { background: var(--wine-2); }
.btn.danger { color: var(--danger); border-color: color-mix(in srgb, var(--danger) 40%, var(--line)); }
.btn.ghost { background: transparent; }
.btn.small { padding: .25rem .6rem; min-height: 32px; font-size: .88rem; }
.btn.block { width: 100%; }
.btn:disabled { opacity: .5; cursor: default; }
.icon-btn {
  border: 0; background: transparent; color: var(--muted); cursor: pointer; font: inherit;
  min-width: 36px; min-height: 36px; border-radius: 8px; display: inline-flex; align-items: center; justify-content: center;
}
.icon-btn:hover:not(:disabled) { background: color-mix(in srgb, var(--line) 60%, transparent); color: var(--text); }
.icon-btn:disabled { opacity: .3; }
.icon { display: inline-flex; align-items: center; justify-content: center; line-height: 0; }
.icon svg { display: block; }
input, select, textarea {
  font: inherit; color: var(--text); background: var(--surface); border: 1px solid var(--line);
  border-radius: 8px; padding: .5rem .6rem; width: 100%; min-height: 40px;
}
textarea { resize: vertical; }
input:focus, select:focus, textarea:focus, .btn:focus-visible, a:focus-visible, button:focus-visible {
  outline: 2px solid color-mix(in srgb, var(--wine) 60%, transparent); outline-offset: 1px;
}
input[type=checkbox], input[type=radio] { width: auto; min-height: 0; }
input[type=color] { padding: 2px; height: 40px; }
.field { display: block; }
.field-label { display: block; font-size: .85rem; font-weight: 600; margin-bottom: .25rem; color: var(--muted); }
.hint { display: block; color: var(--muted); font-size: .8rem; margin-top: .2rem; }
.check { display: inline-flex; gap: .4rem; align-items: center; white-space: nowrap; font-size: .9rem; }
.form-error { color: var(--danger); min-height: 1em; margin: 0; }
.toolbar { display: flex; gap: .6rem; flex-wrap: wrap; align-items: center; margin-bottom: .6rem; }
.toolbar .search { flex: 1 1 260px; }
.toolbar select { width: auto; }
.mobile-only { display: none; }
@media (max-width: 760px) { .mobile-only { display: block; } }

/* ---------- badges, dots */
.badge {
  display: inline-block; font-size: .72rem; padding: .1rem .45rem; border-radius: 999px; font-weight: 600;
  background: color-mix(in srgb, var(--line) 70%, transparent); color: var(--text); white-space: nowrap; vertical-align: middle;
}
.topbar .badge { background: rgba(255, 255, 255, .2); color: #fff; }
.type-dot { display: inline-block; width: .8em; height: .8em; border-radius: 50%; vertical-align: -.05em; border: 1px solid rgba(0, 0, 0, .15); flex: none; }
.phase-keep { --p: var(--phase-keep); } .phase-drink { --p: var(--phase-drink); } .phase-peak { --p: var(--phase-peak); }
.phase-decline { --p: var(--phase-decline); } .phase-unknown { --p: var(--phase-unknown); }
.badge[class*=phase-] { background: color-mix(in srgb, var(--p) 18%, transparent); color: color-mix(in srgb, var(--p) 70%, var(--text)); }
.swatch { display: inline-block; width: .75em; height: .75em; border-radius: 3px; background: var(--p); margin-right: .45em; }
.stock { font-weight: 700; font-variant-numeric: tabular-nums; }
.ai-badge { font-size: .7rem; font-weight: 700; color: #6a4fb3; background: #efe9fb; border-radius: 4px; padding: 0 .3rem; margin-left: .35rem; cursor: help; white-space: nowrap; }
.scope-write { background: #fbe9ec; color: var(--wine); }

/* ---------- tabs & chips */
.tabs { display: flex; gap: .25rem; border-bottom: 1px solid var(--line); margin-bottom: 1rem; overflow-x: auto; scrollbar-width: none; }
.tab { padding: .55rem .9rem; text-decoration: none; color: var(--muted); border-bottom: 2px solid transparent; white-space: nowrap; }
.tab.active { color: var(--wine); border-color: var(--wine); font-weight: 600; }
.chips { display: flex; gap: .35rem; flex-wrap: wrap; margin-bottom: .6rem; }
.chip {
  display: inline-flex; gap: .35rem; align-items: center; border: 1px solid var(--line); background: var(--surface);
  border-radius: 999px; padding: .25rem .7rem; font: inherit; font-size: .88rem; cursor: pointer; color: var(--text);
}
.chip.active { border-color: var(--wine); background: color-mix(in srgb, var(--wine) 10%, var(--surface)); color: var(--wine); font-weight: 600; }
.counter { margin: .2rem 0 .6rem; font-size: .9rem; }

/* ---------- summary */
.type-grid { display: grid; gap: .6rem; grid-template-columns: repeat(auto-fit, minmax(140px, 1fr)); margin-bottom: 1rem; }
.type-card {
  display: flex; flex-direction: column; gap: .2rem; padding: .9rem 1rem; text-decoration: none; color: var(--text);
  background: var(--surface); border: 1px solid var(--line); border-left: 5px solid var(--type-color); border-radius: var(--radius); box-shadow: var(--shadow);
}
.type-count { font-size: 1.8rem; font-weight: 700; line-height: 1.1; font-variant-numeric: tabular-nums; }
.type-name { display: flex; gap: .4rem; align-items: center; color: var(--muted); }
@media (max-width: 760px) { .type-grid { grid-template-columns: repeat(3, 1fr); } .type-card { padding: .6rem; } .type-count { font-size: 1.4rem; } .type-name { font-size: .8rem; } }
.kpis { display: flex; gap: .6rem; flex-wrap: wrap; margin-bottom: 1rem; }
.kpi { flex: 1 1 120px; background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); padding: .6rem .9rem; display: flex; flex-direction: column; text-decoration: none; color: var(--text); }
.kpi strong { font-size: 1.25rem; }
.kpi span { color: var(--muted); font-size: .85rem; }
.kpi.warn { border-color: color-mix(in srgb, var(--warn) 50%, var(--line)); }
.kpi.warn strong { color: var(--warn); }
.phase-bar { display: flex; height: 14px; border-radius: 7px; overflow: hidden; margin: .4rem 0 .8rem; background: var(--line); }
.phase-bar .seg { background: var(--p); }
.phase-list { list-style: none; padding: 0; margin: 0; display: grid; gap: .3rem; }
.phase-list li { display: grid; grid-template-columns: 1fr auto 3.5em; gap: .6rem; align-items: center; }
.phase-list a { text-decoration: none; color: var(--text); display: flex; align-items: center; }
.phase-list small { text-align: right; }
meter { width: 90px; height: 10px; }

/* ---------- lists */
.list { list-style: none; margin: 0; padding: 0; }
.list > li + li { border-top: 1px solid var(--line); }
.list-row { display: flex; align-items: center; gap: .7rem; padding: .6rem .2rem; text-decoration: none; color: var(--text); width: 100%; background: none; border: 0; font: inherit; text-align: left; }
a.list-row:hover, button.list-row:hover { background: color-mix(in srgb, var(--line) 35%, transparent); }
.list.cards > li { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); margin-bottom: .5rem; padding: 0 .6rem; }
.list.cards > li + li { border-top: 1px solid var(--line); }
.list .row-actions select { width: auto; }
.note { font-style: italic; color: var(--muted); }

/* ---------- cellar table (cards on mobile) */
.cellar-table, .data { width: 100%; border-collapse: collapse; background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); overflow: hidden; }
.cellar-table th, .data th { text-align: left; font-size: .8rem; text-transform: uppercase; letter-spacing: .03em; color: var(--muted); background: color-mix(in srgb, var(--line) 35%, var(--surface)); padding: 0; }
.data th { padding: .5rem .6rem; }
.cellar-table td, .data td { padding: .5rem .6rem; border-top: 1px solid var(--line); vertical-align: middle; }
.sort { all: unset; cursor: pointer; display: block; padding: .55rem .6rem; width: 100%; box-sizing: border-box; }
.sort:focus-visible { outline: 2px solid var(--wine); }
th[aria-sort] .sort { color: var(--wine); }
tr.clickable { cursor: pointer; }
tr.clickable:hover td { background: color-mix(in srgb, var(--wine) 5%, var(--surface)); }
.cellar-table td.col-name a { color: var(--text); font-weight: 600; text-decoration: none; }
.cellar-table td.col-year, .cellar-table td.col-volume_cl { white-space: nowrap; }
tr.finished { opacity: .55; }
.table-wrap { overflow-x: auto; }
.data.compact td { padding: .3rem .5rem; font-size: .88rem; }
.data td.actions { white-space: nowrap; text-align: right; }
.row-error td { background: color-mix(in srgb, var(--danger) 8%, var(--surface)); }
@media (max-width: 760px) {
  .cellar-table, .cellar-table tbody { display: block; border: 0; background: none; }
  .cellar-table thead { display: none; }
  .cellar-table tr {
    display: grid; grid-template-columns: 1fr auto; gap: 0 .6rem; background: var(--surface);
    border: 1px solid var(--line); border-radius: var(--radius); padding: .6rem .8rem; margin-bottom: .45rem;
  }
  .cellar-table td { border: 0; padding: 0; }
  .cellar-table td.col-name { grid-column: 1; grid-row: 1; font-size: 1rem; }
  .cellar-table td.col-stock { grid-column: 2; grid-row: 1 / span 2; align-self: center; font-size: 1.3rem; }
  .cellar-table td.col-appellation { grid-column: 1; grid-row: 2; color: var(--muted); font-size: .9rem; }
  .cellar-table td.col-year, .cellar-table td.col-type, .cellar-table td.col-volume_cl, .cellar-table td.col-price {
    grid-row: 3; display: inline; font-size: .85rem; color: var(--muted); text-align: left;
  }
  .cellar-table td.col-year { grid-column: 1; }
  .cellar-table tr { grid-template-areas: none; }
  .cellar-table td.col-type, .cellar-table td.col-volume_cl, .cellar-table td.col-price { display: none; }
  .cellar-table td.col-year::after { content: ""; }
  .data:not(.compact), .data:not(.compact) tbody, .data:not(.compact) tr, .data:not(.compact) td { display: block; }
  .data:not(.compact) thead { display: none; }
  .data:not(.compact) tr { border-top: 1px solid var(--line); padding: .4rem .2rem; }
  .data:not(.compact) td { border: 0; padding: .1rem .4rem; }
  .data:not(.compact) td[data-label]::before { content: attr(data-label) " : "; color: var(--muted); font-size: .8rem; }
  .data:not(.compact) { border: 0; }
}

/* ---------- plan (racks) */
.legend { display: flex; gap: .9rem; flex-wrap: wrap; font-size: .85rem; color: var(--muted); margin-bottom: .6rem; }
.legend span { display: inline-flex; align-items: center; gap: .35rem; }
.move-banner {
  position: sticky; top: 52px; z-index: 10; display: flex; gap: .6rem; align-items: center; justify-content: space-between;
  background: #fff6dd; color: #5a4300; border: 1px solid #ecd9a0; border-radius: var(--radius); padding: .5rem .8rem; margin-bottom: .8rem;
}
.storage h2 { color: var(--wine); text-align: center; }
.rack-scroll { overflow-x: auto; padding-bottom: .3rem; }
.rack-grid {
  display: grid; grid-template-columns: 1.6rem repeat(var(--cols), minmax(30px, 46px)); gap: 6px;
  justify-content: center; align-items: center; min-width: min-content; margin: 0 auto;
}
.col-head, .row-head { font-size: .72rem; color: var(--muted); text-align: center; font-weight: 600; }
.slot {
  aspect-ratio: 1; border-radius: 50%; border: 1.5px solid var(--line); background: var(--bg);
  cursor: pointer; padding: 0; position: relative; transition: transform .1s;
}
.slot.empty:hover { border-color: var(--wine); }
.slot.full { background: var(--c); border-color: color-mix(in srgb, var(--c) 70%, #000); box-shadow: inset 0 -3px 0 rgba(0, 0, 0, .12); }
.slot.full::after {
  content: ""; position: absolute; inset: 30%; border-radius: 50%; background: rgba(255, 255, 255, .35);
}
.slot.full:hover { transform: scale(1.08); }
.slot.selected, .bottle-chip.selected { outline: 3px solid var(--wine); outline-offset: 2px; }
.slot.focus, .bottle-chip.focus { animation: pulse 1.2s ease-in-out 3; outline: 3px solid var(--gold); outline-offset: 2px; }
.slot.match, .bottle-chip.match { outline: 3px solid var(--gold); outline-offset: 2px; }
.dim { opacity: .2; }
.over { outline: 2px dashed var(--wine) !important; }
@keyframes pulse { 50% { transform: scale(1.2); } }
.bottle-chips { display: flex; flex-wrap: wrap; gap: .35rem; }
.bottle-chip {
  display: inline-flex; align-items: center; gap: .4rem; border: 1px solid var(--line); background: var(--surface);
  border-radius: 999px; padding: .25rem .7rem .25rem .35rem; font: inherit; font-size: .85rem; cursor: pointer; color: var(--text);
}
.bottle-chip .dot { width: 14px; height: 14px; border-radius: 50%; background: var(--c); border: 1px solid rgba(0, 0, 0, .2); }
.storage.box h2::before { content: "▣ "; color: var(--muted); }
.unplaced { border-style: dashed; }
.rack-preview { display: grid; grid-template-columns: repeat(var(--cols), 14px); gap: 3px; margin: .3rem 0; }
.rack-preview span { width: 14px; height: 14px; border-radius: 50%; border: 1px solid var(--line); }

/* ---------- wine sheet */
.wine-head h1 { margin-bottom: .1em; }
.wine-top { display: grid; grid-template-columns: 220px 1fr; gap: 1.2rem; margin-bottom: 1rem; }
@media (max-width: 760px) { .wine-top { grid-template-columns: 1fr; } }
.wine-image { margin: 0; text-align: center; }
.wine-image img { max-width: 100%; max-height: 320px; border-radius: var(--radius); background: var(--surface); border: 1px solid var(--line); object-fit: contain; }
.wine-image .row-actions { justify-content: center; margin-top: .5rem; }
.wine-image figcaption { font-size: .8rem; margin-top: .3rem; }
.facts { display: grid; grid-template-columns: max-content 1fr; gap: .35rem 1rem; margin: 0; }
.facts dt { color: var(--muted); font-size: .9rem; }
.facts dd { margin: 0; }
.text-block h3 { margin-top: 1rem; }
.text-block p { margin: .2rem 0; }
.window-head { margin-bottom: .4rem; }
.window { display: grid; grid-template-columns: repeat(auto-fit, minmax(130px, 1fr)); gap: .4rem; margin: .2rem 0 .6rem; }
.window dt { font-size: .8rem; color: var(--muted); }
.window dd { margin: 0 0 .3rem; font-weight: 700; font-size: 1.1rem; }
.sources { margin-top: 1rem; font-size: .88rem; }
.sources ul { padding-left: 1.2rem; word-break: break-all; }
.suggestions { list-style: none; padding: 0; margin: 0 0 1rem; }
.suggestions li { padding: .2rem 0; }

/* ---------- history */
.history-day h3 { margin: 1rem 0 .3rem; }
.history-item { display: flex; align-items: flex-start; gap: .7rem; padding: .55rem .2rem; }
.kind { width: 28px; height: 28px; border-radius: 50%; display: inline-flex; align-items: center; justify-content: center; font-weight: 700; flex: none; }
.kind-in { background: #e3f1e8; color: var(--ok); }
.kind-out { background: #f8e4e8; color: var(--wine); }
.kind-move { background: #e7eef7; color: #36597e; }
.kind-restore { background: #fff3d6; color: var(--warn); }

/* ---------- auth */
.auth-card { max-width: 400px; margin: 6vh auto; background: var(--surface); border: 1px solid var(--line); border-radius: 14px; padding: 1.6rem; box-shadow: var(--shadow); text-align: center; }
.auth-card form { text-align: left; margin-top: 1rem; }
.auth-logo { width: 56px; height: 56px; border-radius: 12px; }

/* ---------- dialogs & toasts */
dialog.modal {
  border: 0; border-radius: 14px; padding: 0; width: min(560px, calc(100vw - 1.5rem)); max-height: calc(100dvh - 2rem);
  background: var(--surface); color: var(--text); box-shadow: 0 10px 40px rgba(0, 0, 0, .3);
}
dialog.modal::backdrop { background: rgba(25, 10, 18, .45); }
.modal-head { display: flex; align-items: center; justify-content: space-between; padding: .8rem 1rem; border-bottom: 1px solid var(--line); position: sticky; top: 0; background: var(--surface); }
.modal-head h2 { margin: 0; font-size: 1.05rem; }
.modal-body { padding: 1rem; overflow: auto; }
.modal-actions { display: flex; gap: .5rem; justify-content: flex-end; flex-wrap: wrap; padding: .8rem 1rem; border-top: 1px solid var(--line); position: sticky; bottom: 0; background: var(--surface); }
@media (max-width: 760px) {
  dialog.modal { margin: auto 0 0; width: 100vw; max-width: 100vw; border-radius: 14px 14px 0 0; }
  .modal-actions .btn { flex: 1; }
}
#toasts { position: fixed; z-index: 100; left: 50%; transform: translateX(-50%); bottom: calc(var(--tabbar-h) + 1rem); display: grid; gap: .4rem; width: min(420px, calc(100vw - 2rem)); }
.toast { background: #2a2226; color: #fff; padding: .65rem .9rem; border-radius: 8px; box-shadow: 0 4px 16px rgba(0, 0, 0, .25); transition: opacity .4s; }
.toast.success { background: var(--ok); }
.toast.error { background: var(--danger); }
.toast.hide { opacity: 0; }
.copy-box { display: flex; gap: .4rem; align-items: flex-start; }
.copy-box .code { font-family: ui-monospace, monospace; font-size: .82rem; }
