:root {
  --bg: #17110b;
  --bg2: #1e160e;
  --surface: #261b11;
  --surface2: #322417;
  --line: #4a3522;
  --text: #f6ead9;
  --muted: #bba68c;
  --amber: #eaa53f;
  --amber2: #c8781c;
  --gold: #ffd28a;
  --green: #86c46f;
  --red: #e5705d;
  --radius: 16px;
  --safe-b: env(safe-area-inset-bottom, 0px);
  --safe-t: env(safe-area-inset-top, 0px);
  color-scheme: dark;
}
* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
[hidden] { display: none !important; }
html, body { margin: 0; }
body {
  font-family: -apple-system, BlinkMacSystemFont, "SF Pro Text", "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  background: radial-gradient(120% 60% at 50% -10%, #3a2512 0%, var(--bg) 60%) fixed, var(--bg);
  color: var(--text);
  min-height: 100vh;
  min-height: 100dvh;
  -webkit-font-smoothing: antialiased;
  -webkit-text-size-adjust: 100%;
}
button, input, select { font: inherit; color: inherit; }
button { cursor: pointer; }
.sr-only { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); }
.muted { color: var(--muted); }
.small { font-size: 14px; }

/* Top bar */
.topbar {
  position: sticky; top: 0; z-index: 10;
  display: flex; align-items: center; justify-content: space-between;
  padding: calc(10px + var(--safe-t)) 16px 10px;
  background: rgba(23, 17, 11, .86);
  backdrop-filter: blur(14px); -webkit-backdrop-filter: blur(14px);
  border-bottom: 1px solid rgba(74, 53, 34, .6);
}
.brand { display: flex; align-items: center; gap: 12px; }
.brand img { border-radius: 10px; }
.brand h1 { font-size: 22px; margin: 0; letter-spacing: -.2px; }
.stats { margin: 2px 0 0; font-size: 13px; color: var(--muted); }
.icon-btn {
  width: 44px; height: 44px; border-radius: 12px; border: 1px solid var(--line);
  background: var(--surface); color: var(--muted); display: grid; place-items: center;
}
.icon-btn.small { width: 44px; height: 44px; font-size: 20px; }
.icon-btn:active { background: var(--surface2); }

/* Controls */
.controls { padding: 12px 16px 4px; display: grid; gap: 10px; }
.search-wrap { position: relative; }
.search-wrap svg { position: absolute; left: 14px; top: 50%; transform: translateY(-50%); color: var(--muted); }
#search, #b-search {
  width: 100%; height: 46px; border-radius: 14px; border: 1px solid var(--line);
  background: var(--surface); padding: 0 14px 0 40px; font-size: 16px; outline: none;
}
#search:focus, #b-search:focus { border-color: var(--amber2); }
.row { display: flex; align-items: center; justify-content: space-between; gap: 8px; }
.chips { display: flex; gap: 6px; overflow-x: auto; scrollbar-width: none; }
.chip {
  height: 38px; padding: 0 13px; border-radius: 999px; border: 1px solid var(--line);
  background: var(--surface); color: var(--muted); font-size: 14px; font-weight: 600; white-space: nowrap;
}
.chip.active, .chip[aria-pressed="true"] { background: var(--amber); border-color: var(--amber); color: #2a1706; }
.sort { display: flex; align-items: center; gap: 6px; }
.sort select {
  height: 38px; border-radius: 12px; border: 1px solid var(--line); background: var(--surface);
  padding: 0 10px; font-size: 15px; font-weight: 600; color: var(--text); -webkit-appearance: none; appearance: none;
  background-image: linear-gradient(45deg, transparent 50%, var(--muted) 50%), linear-gradient(135deg, var(--muted) 50%, transparent 50%);
  background-position: calc(100% - 14px) 16px, calc(100% - 9px) 16px; background-size: 5px 5px; background-repeat: no-repeat;
  padding-right: 24px;
}
.sort .icon-btn { width: 38px; height: 38px; border-radius: 12px; font-size: 18px; }
.sort .icon-btn.on { color: var(--amber); border-color: var(--amber2); }

/* List */
.list { list-style: none; margin: 0; padding: 8px 16px calc(110px + var(--safe-b)); display: grid; gap: 10px; }
.card {
  width: 100%; text-align: left; display: grid; grid-template-columns: 44px 1fr auto; gap: 12px; align-items: center;
  padding: 14px 14px 14px 12px; border-radius: var(--radius); border: 1px solid var(--line);
  background: linear-gradient(180deg, var(--surface2), var(--surface));
  box-shadow: 0 6px 18px rgba(0,0,0,.25);
}
.card:active { transform: scale(.985); }
.rank {
  width: 44px; height: 44px; border-radius: 12px; display: grid; place-items: center;
  font-weight: 800; font-size: 16px; color: var(--gold); background: rgba(234,165,63,.12); border: 1px solid rgba(234,165,63,.25);
}
.rank.r1 { background: linear-gradient(180deg, #f2b955, #c8781c); color: #2a1706; border: none; }
.rank.r2 { background: linear-gradient(180deg, #d9cbb7, #9b8a73); color: #241a10; border: none; }
.rank.r3 { background: linear-gradient(180deg, #c98a55, #8c5326); color: #24140a; border: none; }
.info { min-width: 0; }
.info .name { font-weight: 700; font-size: 17px; line-height: 1.25; overflow-wrap: anywhere; }
.info .sub { color: var(--muted); font-size: 14px; margin-top: 2px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.info .meta { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 7px; }
.tag { font-size: 12px; font-weight: 600; padding: 3px 8px; border-radius: 999px; background: rgba(255,255,255,.06); color: var(--muted); }
.tag.yes { background: rgba(134,196,111,.16); color: var(--green); }
.tag.no { background: rgba(229,112,93,.16); color: var(--red); }
.tag.value { background: rgba(234,165,63,.14); color: var(--gold); }
.info .comment { font-size: 13px; color: var(--muted); font-style: italic; margin-top: 6px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.score { text-align: center; min-width: 52px; }
.score b { display: block; font-size: 26px; font-weight: 800; color: var(--amber); line-height: 1; }
.score small { font-size: 11px; color: var(--muted); text-transform: uppercase; letter-spacing: .6px; }
.empty { text-align: center; padding: 40px 28px 120px; color: var(--muted); }
.empty .big { font-size: 48px; }

/* Add bar */
.add-bar {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 20;
  padding: 12px 16px calc(14px + var(--safe-b));
  background: linear-gradient(180deg, rgba(23,17,11,0), rgba(23,17,11,.95) 40%);
}
.add-btn { width: 100%; height: 58px; font-size: 19px; border-radius: 18px; box-shadow: 0 10px 28px rgba(200,120,28,.35); }

/* Buttons */
.btn {
  height: 48px; padding: 0 18px; border-radius: 14px; border: 1px solid var(--line);
  background: var(--surface2); color: var(--text); font-weight: 650; font-size: 16px;
}
.btn:active { filter: brightness(1.15); }
.btn.primary { background: linear-gradient(180deg, #f0b04f, var(--amber2)); border: none; color: #2a1706; font-weight: 800; }
.btn.ghost { background: transparent; border-color: transparent; color: var(--amber); padding: 0 8px; }
.btn.danger { background: rgba(229,112,93,.12); border-color: rgba(229,112,93,.45); color: var(--red); }
.btn.small { height: 40px; padding: 0 16px; }
.btn.block { width: 100%; margin-top: 10px; }
.link-btn { background: none; border: none; color: var(--muted); text-decoration: underline; font-size: 14px; padding: 8px; }

/* Sheets */
.sheet-backdrop {
  position: fixed; inset: 0; z-index: 50; background: rgba(0,0,0,.55);
  display: flex; align-items: flex-end; justify-content: center; animation: fade .18s ease;
}
.sheet {
  width: 100%; max-width: 560px; max-height: calc(100dvh - 24px - var(--safe-t)); margin-top: calc(24px + var(--safe-t));
  background: var(--bg2); border: 1px solid var(--line); border-bottom: none;
  border-radius: 22px 22px 0 0; display: flex; flex-direction: column; animation: up .22s ease;
}
.sheet-head {
  display: grid; grid-template-columns: 1fr auto 1fr; align-items: center; padding: 10px 12px;
  border-bottom: 1px solid var(--line);
}
.sheet-head > :first-child { justify-self: start; }
.sheet-head > :last-child { justify-self: end; }
.sheet-head h2 { font-size: 17px; margin: 0; }
.sheet-body { overflow-y: auto; -webkit-overflow-scrolling: touch; padding: 14px 16px calc(28px + var(--safe-b)); }
@keyframes up { from { transform: translateY(40px); opacity: .4; } to { transform: none; opacity: 1; } }
@keyframes fade { from { opacity: 0; } to { opacity: 1; } }

/* Form */
.field { display: block; margin-bottom: 16px; }
.field > span { display: flex; align-items: center; justify-content: space-between; font-size: 14px; font-weight: 650; color: var(--muted); margin-bottom: 7px; }
.field em { color: var(--amber); font-style: normal; }
.field input {
  width: 100%; height: 50px; border-radius: 14px; border: 1px solid var(--line); background: var(--surface);
  padding: 0 14px; font-size: 16px; outline: none; -webkit-appearance: none; appearance: none;
}
.field input[type="date"] { display: block; min-width: 0; line-height: 50px; }
.field input:focus { border-color: var(--amber2); box-shadow: 0 0 0 3px rgba(234,165,63,.15); }
.field input.invalid { border-color: var(--red); }
.error { color: var(--red); font-size: 13px; display: block; margin-top: 6px; }
.two { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }
.score-readout { color: var(--amber); font-size: 20px; }
.score-grid { display: grid; grid-template-columns: repeat(5, 1fr); gap: 8px; }
.score-grid button {
  height: 52px; border-radius: 14px; border: 1px solid var(--line); background: var(--surface);
  font-size: 19px; font-weight: 800; color: var(--text);
}
.score-grid button.sel { background: linear-gradient(180deg, #f0b04f, var(--amber2)); border-color: transparent; color: #2a1706; }
.score-extra { display: flex; justify-content: space-between; align-items: center; margin-top: 8px; }
.toggle { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }
.toggle button {
  height: 52px; border-radius: 14px; border: 1px solid var(--line); background: var(--surface); font-size: 17px; font-weight: 700;
}
.toggle button[data-buy="yes"].sel { background: rgba(134,196,111,.2); border-color: var(--green); color: var(--green); }
.toggle button[data-buy="no"].sel { background: rgba(229,112,93,.18); border-color: var(--red); color: var(--red); }
.value-preview { font-size: 14px; min-height: 18px; margin: 0 0 8px; }

/* Settings */
.panel { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); padding: 14px; margin-bottom: 14px; }
.panel h3 { margin: 0 0 6px; font-size: 16px; }
.panel p { margin: 0 0 4px; }

/* Login / loading */
.center-view { min-height: 70vh; display: grid; place-items: center; padding: 24px 20px; }
.login-card { width: 100%; max-width: 380px; text-align: center; }
.login-card img { border-radius: 18px; }
.login-card h2 { margin: 12px 0 4px; }
.login-card input {
  width: 100%; height: 52px; border-radius: 14px; border: 1px solid var(--line); background: var(--surface);
  padding: 0 14px; font-size: 17px; margin-top: 14px; outline: none; text-align: center;
}
.spinner { width: 38px; height: 38px; border-radius: 50%; border: 4px solid var(--surface2); border-top-color: var(--amber); animation: spin .8s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }

/* Dialog + toast */
.dialog-backdrop { position: fixed; inset: 0; z-index: 80; background: rgba(0,0,0,.6); display: grid; place-items: center; padding: 24px; animation: fade .15s; }
.dialog { width: 100%; max-width: 340px; background: var(--bg2); border: 1px solid var(--line); border-radius: 20px; padding: 20px; }
.dialog p { margin: 0 0 18px; font-size: 17px; line-height: 1.4; text-align: center; }
.dialog-actions { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.dialog-actions .btn.danger-ok { background: var(--red); color: #fff; border: none; }
.toast {
  position: fixed; left: 50%; bottom: calc(92px + var(--safe-b)); transform: translate(-50%, 20px); z-index: 90;
  background: #3b2a19; border: 1px solid var(--amber2); color: var(--text); padding: 11px 18px; border-radius: 999px;
  font-size: 15px; font-weight: 600; opacity: 0; pointer-events: none; transition: .2s; max-width: calc(100% - 32px); text-align: center;
}
.toast.show { opacity: 1; transform: translate(-50%, 0); }

/* Age filter row */
.age-wrap { gap: 8px; }
.age-label { font-size: 14px; font-weight: 650; color: var(--muted); }
.sort select#age-filter.on { border-color: var(--amber2); color: var(--amber); }
.tag.age { background: rgba(255,210,138,.10); color: var(--gold); }
.tag.age.nas { background: rgba(255,255,255,.06); color: var(--muted); }

/* Bottle thumbnail on cards (falls back to the rank badge when there's no image) */
.lead { position: relative; width: 44px; display: grid; place-items: center; }
.lead.has-img { height: 76px; }
.lead .thumb {
  width: 44px; height: 76px; object-fit: contain; object-position: center bottom;
  filter: drop-shadow(0 3px 6px rgba(0,0,0,.45));
}
.lead.has-img .rank {
  position: absolute; left: -6px; top: -6px; width: 26px; height: 22px; border-radius: 8px; font-size: 11px;
  background: rgba(23,17,11,.88); backdrop-filter: blur(4px); -webkit-backdrop-filter: blur(4px);
}
.lead.has-img .rank.r1 { background: linear-gradient(180deg, #f2b955, #c8781c); }
.lead.has-img .rank.r2 { background: linear-gradient(180deg, #d9cbb7, #9b8a73); }
.lead.has-img .rank.r3 { background: linear-gradient(180deg, #c98a55, #8c5326); }

/* Edit sheet: larger photo */
.photo-preview {
  display: grid; place-items: center; height: 190px; margin: 0 0 14px; border-radius: var(--radius);
  background: radial-gradient(60% 70% at 50% 60%, rgba(234,165,63,.14), transparent 70%), var(--surface);
  border: 1px solid var(--line);
}
.photo-preview img { max-height: 170px; max-width: 80%; object-fit: contain; filter: drop-shadow(0 6px 12px rgba(0,0,0,.5)); }
.photo-preview .photo-broken { display: none; }
.photo-preview.broken img { display: none; }
.photo-preview.broken .photo-broken { display: block; }

/* Age field */
.field-label { display: block; font-size: 14px; font-weight: 650; color: var(--muted); margin-bottom: 7px; }
.age-row { display: grid; grid-template-columns: 1fr auto; gap: 12px; align-items: center; }
.field .age-row input[type="number"] { -moz-appearance: textfield; }
.field input.dim { opacity: .55; }
.check { display: flex; align-items: center; gap: 8px; font-size: 15px; color: var(--text); white-space: nowrap; min-height: 44px; }
.field .check input { width: 22px; height: 22px; accent-color: var(--amber); -webkit-appearance: checkbox; appearance: auto; padding: 0; margin: 0; }

/* Advanced section */
.advanced { margin: 0 0 14px; border: 1px solid var(--line); border-radius: 14px; padding: 0 14px; background: rgba(255,255,255,.02); }
.advanced summary { cursor: pointer; padding: 13px 0; font-size: 14px; font-weight: 650; color: var(--muted); }
.advanced[open] summary { margin-bottom: 4px; }

/* "You might like" */
.recs { margin: 12px 16px 0; border: 1px solid var(--line); border-radius: var(--radius); background: linear-gradient(180deg, rgba(234,165,63,.08), rgba(38,27,17,.6)); }
.recs > summary {
  list-style: none; cursor: pointer; display: flex; align-items: center; justify-content: space-between; gap: 8px;
  padding: 12px 14px; min-height: 48px; font-weight: 750; font-size: 16px;
}
.recs > summary::-webkit-details-marker { display: none; }
.recs > summary::after { content: "▾"; color: var(--muted); transition: transform .15s; }
.recs:not([open]) > summary::after { transform: rotate(-90deg); }
.recs-title { flex: 1; }
.recs-count { color: var(--muted); font-weight: 600; font-size: 13px; }
.recs-body { padding: 0 10px 10px; }
.recs-group h3 { margin: 6px 4px 8px; font-size: 14px; color: var(--gold); letter-spacing: .2px; }
.recs-group h3 small { display: block; color: var(--muted); font-weight: 500; font-size: 12px; margin-top: 1px; }
.recs-foot, .recs-empty { margin: 6px 4px 2px; font-size: 12px; }
.picks { list-style: none; margin: 0 0 8px; padding: 0; display: grid; gap: 8px; }
.pick details { border: 1px solid var(--line); border-radius: 14px; background: var(--surface); }
.pick details[open] { border-color: var(--amber2); }
.pick summary { list-style: none; cursor: pointer; display: grid; grid-template-columns: 1fr auto; gap: 8px; padding: 10px 12px; align-items: start; }
.pick summary::-webkit-details-marker { display: none; }
.pick-main { min-width: 0; display: grid; gap: 2px; }
.pick-name { font-weight: 700; font-size: 15px; line-height: 1.25; overflow-wrap: anywhere; }
.pick-sub { color: var(--muted); font-size: 12.5px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.pick-facts { color: var(--text); font-size: 12.5px; opacity: .9; }
.pick-why { color: var(--gold); font-size: 12.5px; font-style: italic; }
.pick-badges { display: flex; flex-direction: column; align-items: flex-end; gap: 4px; }
.tag.avail { font-size: 11px; padding: 2px 7px; white-space: nowrap; }
.tag.nh-shelf { background: rgba(134,196,111,.16); color: var(--green); }
.tag.nh-limited { background: rgba(234,165,63,.16); color: var(--amber); }
.tag.nh-allocated { background: rgba(229,112,93,.16); color: var(--red); }
.tag.me { background: rgba(120,170,230,.16); color: #9cc3f0; }
.pick-more { padding: 0 12px 12px; font-size: 13px; color: var(--muted); }
.pick-more p { margin: 0 0 6px; }
.pick-add { margin-top: 4px; width: 100%; }
.similar { margin: 4px 0 14px; }
.similar h3 { margin: 0 0 8px; font-size: 14px; color: var(--muted); font-weight: 650; }

/* Tabs: My bottles | Browse */
.tabs { display: grid; grid-template-columns: 1fr 1fr; gap: 4px; margin: 12px 16px 0; padding: 4px; border-radius: 14px; background: var(--surface); border: 1px solid var(--line); }
.tab { height: 38px; border: none; border-radius: 10px; background: transparent; color: var(--muted); font-weight: 700; font-size: 15px; }
.tab small { font-weight: 600; font-size: 12px; opacity: .8; }
.tab.active { background: var(--surface2); color: var(--text); box-shadow: 0 1px 0 rgba(255,255,255,.06) inset, 0 2px 8px rgba(0,0,0,.25); }

/* Filter rows (age + where made; browse grid) */
.filter-row { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }
.filter-row .sort, .filter-grid .sort { display: block; min-width: 0; }
.filter-row select, .filter-grid select { width: 100%; min-width: 0; text-overflow: ellipsis; }
.sort select.on { border-color: var(--amber2); color: var(--amber); }
.filter-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }
.b-count { margin: 0 2px; }
.browse-list { padding: 4px 16px calc(110px + var(--safe-b)); margin: 0; }
.tag.maple { background: rgba(229,112,93,.18); color: var(--red); }
.tag.mine { background: rgba(134,196,111,.2); color: var(--green); }
.pick.is-mine details { border-color: rgba(134,196,111,.45); }
