:root {
  --wine: #5b1a2b;
  --wine-dark: #3d0f1c;
  --wine-light: #8a2f47;
  --bg: #faf7f5;
  --surface: #ffffff;
  --text: #221417;
  --muted: #7a6a6d;
  --border: #e8dfe1;
  --danger: #b3261e;
  --radius: 12px;
  --shadow: 0 1px 3px rgba(0,0,0,.08), 0 1px 2px rgba(0,0,0,.06);
  color-scheme: light dark;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #161112;
    --surface: #221a1c;
    --text: #f2e9ea;
    --muted: #a8969a;
    --border: #3a2c2f;
    --wine-light: #c1476a;
  }
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  min-height: 100vh;
}

.screen { min-height: 100vh; }
.screen[hidden] { display: none !important; }

/* ---------- Sign-in screen ---------- */
#signin-screen {
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 24px;
}
.signin-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 40px 32px;
  text-align: center;
  max-width: 360px;
  width: 100%;
}
.signin-logo { font-size: 48px; margin-bottom: 4px; }
.signin-card h1 { margin: 0 0 4px; }
.error-text { color: var(--danger); font-size: 14px; }

/* ---------- Top bar ---------- */
.topbar {
  display: flex;
  align-items: center;
  gap: 16px;
  padding: 10px 16px;
  /* iOS "Add to Home Screen" uses a translucent status bar (see
     apple-mobile-web-app-status-bar-style in index.html), which draws the
     clock/wifi/battery ON TOP of our content instead of pushing it down —
     without this, a sticky header at top:0 renders straight underneath
     them. env(...) is 0 on browsers that don't need it, so this is a no-op
     everywhere except notched/status-bar-overlay devices. */
  padding-top: calc(10px + env(safe-area-inset-top));
  padding-left: calc(16px + env(safe-area-inset-left));
  padding-right: calc(16px + env(safe-area-inset-right));
  background: var(--wine);
  color: #fff;
  flex-wrap: wrap;
  position: sticky;
  top: 0;
  z-index: 10;
}
.brand { font-weight: 700; font-size: 18px; display: flex; align-items: center; gap: 6px; }
.tabs { display: flex; gap: 4px; flex: 1; flex-wrap: wrap; }
.tab-btn {
  background: transparent;
  border: none;
  color: rgba(255,255,255,.8);
  padding: 8px 14px;
  border-radius: 999px;
  cursor: pointer;
  font-size: 14px;
}
.tab-btn:hover { background: rgba(255,255,255,.1); }
.tab-btn.active { background: rgba(255,255,255,.18); color: #fff; font-weight: 600; }
.topbar-right { display: flex; align-items: center; gap: 10px; margin-left: auto; }

/* On narrow phones, 4 full-text tab buttons don't fit next to the brand and
   sign-out button — instead of wrapping into a tall, messy vertical stack,
   drop the tabs to their own full-width row and let it scroll horizontally
   like a normal mobile tab strip. */
@media (max-width: 640px) {
  .topbar { gap: 8px; }
  /* Keep brand + Sign out locked together on their own line, so Sign out
     always sits level with "Wine Cellar" instead of dropping below it. */
  .brand, .topbar-right { flex-shrink: 0; }
  .tabs {
    order: 3;
    flex-basis: 100%;
    flex-wrap: nowrap;
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
    scrollbar-width: none;
    margin-top: 2px;
  }
  .tabs::-webkit-scrollbar { display: none; }
  .tab-btn { flex: 0 0 auto; white-space: nowrap; }
}

.tab-panel { display: none; padding: 20px 16px 60px; max-width: 1000px; margin: 0 auto; }
.tab-panel.active { display: block; }

/* ---------- Buttons ---------- */
.btn {
  font: inherit;
  border-radius: 8px;
  border: 1px solid transparent;
  padding: 9px 16px;
  cursor: pointer;
  font-weight: 600;
  font-size: 14px;
}
.btn-primary { background: var(--wine); color: #fff; }
.btn-primary:hover { background: var(--wine-dark); }
.btn-ghost { background: transparent; border-color: var(--border); color: var(--text); }
.btn-ghost:hover { background: rgba(0,0,0,.04); }
.btn-danger { background: transparent; border-color: var(--danger); color: var(--danger); }
.btn-danger:hover { background: rgba(179,38,30,.08); }
/* .topbar's background is always dark maroon, regardless of light/dark theme —
   .btn-ghost's color is var(--text), which flips to near-black in light mode,
   making a plain .btn-ghost (e.g. Sign out) invisible against it. Force
   light text/border here instead, same idea as .tab-btn right next to it. */
.topbar .btn-ghost { color: rgba(255,255,255,.85); border-color: rgba(255,255,255,.4); }
.topbar .btn-ghost:hover { background: rgba(255,255,255,.12); color: #fff; }
.btn-lg { padding: 12px 24px; font-size: 16px; margin-top: 16px; }
.btn-sm { padding: 5px 10px; font-size: 12px; }

/* ---------- Chat search ---------- */
.chat-search {
  display: flex;
  gap: 8px;
  margin-bottom: 6px;
}
.chat-search input {
  flex: 1;
  min-width: 0;
}

/* ---------- Toolbar / filters ---------- */
.toolbar {
  display: flex;
  gap: 10px;
  flex-wrap: wrap;
  margin-bottom: 14px;
}
.toolbar input[type="search"] { flex: 1; min-width: 200px; }
.toolbar input[type="text"] { flex: 1; min-width: 140px; }
.toggle-label {
  display: flex;
  align-items: center;
  gap: 6px;
  font-size: 14px;
  color: var(--muted);
  cursor: pointer;
  user-select: none;
}
.toggle-label input[type="checkbox"] { width: 16px; height: 16px; cursor: pointer; }
input, select, textarea {
  font: inherit;
  padding: 9px 10px;
  border-radius: 8px;
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text);
}
input:focus, select:focus, textarea:focus { outline: 2px solid var(--wine-light); outline-offset: 1px; }

.stats-bar {
  display: flex;
  gap: 20px;
  flex-wrap: wrap;
  margin-bottom: 16px;
  font-size: 13px;
  color: var(--muted);
}
.stats-bar strong { color: var(--text); }
.value-toggle-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  vertical-align: -3px;
  background: none;
  border: none;
  padding: 2px;
  margin: 0;
  color: var(--muted);
  cursor: pointer;
  border-radius: 4px;
}
.value-toggle-btn:hover { color: var(--text); background: rgba(0,0,0,.05); }
@media (prefers-color-scheme: dark) {
  .value-toggle-btn:hover { background: rgba(255,255,255,.1); }
}

/* ---------- Wine grid / cards ---------- */
.wine-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(240px, 1fr));
  gap: 14px;
  margin-bottom: 24px;
}
.group-header {
  margin: 0 0 12px;
  padding-bottom: 6px;
  border-bottom: 2px solid var(--wine-light);
  color: var(--wine);
  font-size: 16px;
}
@media (prefers-color-scheme: dark) {
  .group-header { color: var(--wine-light); }
}
.wine-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 14px;
  box-shadow: var(--shadow);
  cursor: pointer;
  display: flex;
  flex-direction: column;
  gap: 4px;
}
.wine-card:hover { border-color: var(--wine-light); }
.wine-card h3 { margin: 0; font-size: 16px; }
.wine-card .sub { color: var(--muted); font-size: 13px; }
.wine-card .badges { display: flex; gap: 6px; flex-wrap: wrap; margin-top: 6px; }
.badge {
  font-size: 11px;
  padding: 2px 8px;
  border-radius: 999px;
  background: rgba(91,26,43,.08);
  color: var(--wine);
}
@media (prefers-color-scheme: dark) {
  .badge { background: rgba(193,71,106,.18); color: #eab8c6; }
}
.badge.consumed { background: #e8e8e8; color: #666; }
.wine-card .rating { font-size: 13px; }
.wine-card .price-line, .wine-card .drink-window-line { font-size: 12px; }

/* Two stacked star rows: a muted background row and a gold foreground row
   clipped to `rating/5` width, so fractional ratings (e.g. 3.5) clip
   partway through a star instead of needing a dedicated half-star glyph. */
.star-rating { position: relative; display: inline-block; line-height: 1; white-space: nowrap; }
.star-rating .star-bg, .star-rating .star-fill { display: block; white-space: nowrap; }
.star-rating .star-bg { color: var(--muted); }
.star-rating .star-fill { position: absolute; top: 0; left: 0; overflow: hidden; color: #c99a2e; }
.star-rating.interactive { cursor: pointer; user-select: none; }

.detail-rating { margin: 4px 0 8px; }
.detail-rating .star-rating { font-size: 26px; }

.empty-state { color: var(--muted); text-align: center; padding: 40px 0; }

/* ---------- Form ---------- */
.wine-form { display: flex; flex-direction: column; gap: 14px; max-width: 640px; }
.wine-form label { display: flex; flex-direction: column; gap: 4px; font-size: 13px; color: var(--muted); }
.grid-2 { display: grid; grid-template-columns: 1fr 1fr; gap: 14px; }
.grid-3 { display: grid; grid-template-columns: 1fr 1fr 1fr; gap: 14px; }
@media (max-width: 520px) {
  .grid-2, .grid-3 { grid-template-columns: 1fr; }
}
.form-actions { display: flex; gap: 10px; flex-wrap: wrap; margin-top: 4px; }

/* ---------- Autocomplete (e.g. Wine Merchant) ---------- */
.autocomplete-wrap { position: relative; }
.autocomplete-list {
  position: absolute;
  top: calc(100% + 4px);
  left: 0;
  right: 0;
  z-index: 20;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 8px;
  box-shadow: var(--shadow);
  max-height: 220px;
  overflow-y: auto;
}
.autocomplete-item { padding: 8px 10px; font-size: 14px; cursor: pointer; color: var(--text); }
.autocomplete-item:hover, .autocomplete-item.active { background: rgba(91,26,43,.08); }
@media (prefers-color-scheme: dark) {
  .autocomplete-item:hover, .autocomplete-item.active { background: rgba(193,71,106,.18); }
}

/* ---------- Import tabs (PDF / CSV) ---------- */
.import-controls {
  display: flex;
  flex-wrap: wrap;
  gap: 14px;
  align-items: flex-end;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 16px;
  margin-bottom: 16px;
}
.import-controls label { display: flex; flex-direction: column; gap: 4px; font-size: 13px; color: var(--muted); flex: 1; min-width: 220px; }
.import-controls label.inline { flex: none; min-width: 120px; }
.import-results { display: flex; flex-direction: column; gap: 10px; }
.import-result-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 14px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 12px;
  flex-wrap: wrap;
}
.import-result-card .meta { font-size: 13px; color: var(--muted); }
.import-result-card .subject { font-weight: 600; }
.import-result-card .status-tag { font-size: 12px; padding: 2px 8px; border-radius: 999px; background: #e8e8e8; color: #555; }
.import-result-card .status-tag.imported { background: #d6f0da; color: #1c6b2c; }
.import-result-card .status-tag.dismissed { background: #f0e0e0; color: #7a3030; }

/* ---------- Bottles / history ---------- */
#bottles-section { max-width: 640px; margin-top: 8px; }
#bottles-section h3 { margin: 20px 0 10px; }
.bottle-history { display: flex; flex-direction: column; gap: 10px; }
.bottle-history-item {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 12px 14px;
  display: flex;
  justify-content: space-between;
  gap: 12px;
  cursor: pointer;
}
.bottle-history-item:hover { border-color: var(--wine-light); }
.tx-detail-list { display: grid; grid-template-columns: auto 1fr; gap: 6px 14px; margin: 14px 0; }
.tx-detail-list dt { color: var(--muted); font-size: 13px; }
.tx-detail-list dd { margin: 0; }
.bottle-history-item .tx-label { font-weight: 600; }
.bottle-history-item .tx-label.add { color: #1c6b2c; }
.bottle-history-item .tx-label.remove { color: var(--text); }
.bottle-history-item .tx-note { color: var(--muted); font-size: 13px; margin-top: 2px; }
.bottle-history-item .tx-when { color: var(--muted); font-size: 12px; white-space: nowrap; }
.bottle-history-item .tx-right {
  display: flex;
  flex-direction: column;
  align-items: flex-end;
  gap: 6px;
  flex-shrink: 0;
}
.tx-delete-btn {
  font: inherit;
  font-size: 12px;
  background: transparent;
  border: none;
  color: var(--muted);
  cursor: pointer;
  padding: 0;
  text-decoration: underline;
}
.tx-delete-btn:hover { color: var(--danger); }

/* ---------- CSV import ---------- */
.import-table-wrap {
  overflow-x: auto;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--surface);
}
.import-table { width: 100%; border-collapse: collapse; font-size: 13px; white-space: nowrap; }
.import-table th, .import-table td { text-align: left; padding: 8px 10px; border-bottom: 1px solid var(--border); }
.import-table th { color: var(--muted); font-weight: 600; position: sticky; top: 0; background: var(--surface); }
.import-table tr.import-row-dup { opacity: .55; }
#import-summary { align-items: center; margin-bottom: 10px; }
#import-selected-count { white-space: nowrap; }

/* ---------- Modal ---------- */
.modal-backdrop {
  position: fixed; inset: 0; background: rgba(0,0,0,.5);
  display: flex; align-items: center; justify-content: center;
  z-index: 100; padding: 16px;
}
.modal-backdrop[hidden] { display: none; }
.modal {
  background: var(--surface);
  border-radius: var(--radius);
  padding: 20px;
  max-width: 560px;
  width: 100%;
  max-height: 88vh;
  overflow-y: auto;
}
#review-fields { display: flex; flex-direction: column; gap: 12px; margin: 14px 0; }
#review-fields label { display: flex; flex-direction: column; gap: 4px; font-size: 13px; color: var(--muted); }

/* ---------- Toast ---------- */
.toast {
  position: fixed;
  bottom: 20px;
  left: 50%;
  transform: translateX(-50%);
  background: var(--text);
  color: var(--bg);
  padding: 10px 18px;
  border-radius: 999px;
  font-size: 14px;
  z-index: 200;
  box-shadow: var(--shadow);
}
.toast[hidden] { display: none; }

.muted { color: var(--muted); }
.small { font-size: 12px; }
