:root {
  --bg: #f6f4f0;
  --surface: #ffffff;
  --surface-2: #f0ede7;
  --text: #24211d;
  --muted: #6f6a62;
  --border: #e3ded5;
  --accent: #2f6b4f;
  --accent-text: #ffffff;
  --accent-soft: #e2efe7;
  --good: #23784a;
  --good-soft: #dff2e6;
  --warn: #9a6500;
  --warn-soft: #fbefd5;
  --bad: #b23a2e;
  --bad-soft: #f8e1dd;
  --info: #3c5a9a;
  --info-soft: #e1e8f7;
  --shadow: 0 1px 2px rgba(40, 30, 10, 0.06), 0 4px 14px rgba(40, 30, 10, 0.06);
  --radius: 14px;
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #171614;
    --surface: #211f1c;
    --surface-2: #2b2925;
    --text: #ece8e1;
    --muted: #a39d93;
    --border: #36332e;
    --accent: #6fbf93;
    --accent-text: #10241a;
    --accent-soft: #233a2d;
    --good: #7fd3a1;
    --good-soft: #1f3529;
    --warn: #e6b45a;
    --warn-soft: #3a2f1a;
    --bad: #f08a7e;
    --bad-soft: #3d2320;
    --info: #9db5ea;
    --info-soft: #232c40;
    --shadow: 0 1px 2px rgba(0, 0, 0, 0.3), 0 4px 14px rgba(0, 0, 0, 0.25);
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }

body {
  margin: 0;
  font: 15px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  background: var(--bg);
  color: var(--text);
}

button, input, select, textarea { font: inherit; color: inherit; }

/* ---- Top bar ---- */

.topbar {
  position: sticky;
  top: 0;
  z-index: 10;
  display: flex;
  align-items: center;
  gap: 16px;
  padding: 12px 24px;
  background: color-mix(in srgb, var(--surface) 92%, transparent);
  backdrop-filter: blur(8px);
  border-bottom: 1px solid var(--border);
}
.brand { font-weight: 700; font-size: 18px; display: flex; align-items: center; gap: 8px; white-space: nowrap; }
.logo { font-size: 22px; }
.summary { color: var(--muted); font-size: 14px; flex: 1; }
.summary strong { color: var(--text); font-variant-numeric: tabular-nums; }
.top-actions { display: flex; gap: 8px; }

/* ---- Buttons ---- */

.btn {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  border: 1px solid transparent;
  border-radius: 10px;
  padding: 8px 14px;
  cursor: pointer;
  font-weight: 600;
  white-space: nowrap;
  transition: background 0.15s, border-color 0.15s, opacity 0.15s;
}
.btn.primary { background: var(--accent); color: var(--accent-text); }
.btn.primary:hover { filter: brightness(1.07); }
.btn.ghost { background: transparent; border-color: var(--border); }
.btn.ghost:hover { background: var(--surface-2); }
.btn.danger { color: var(--bad); }
.btn.small { padding: 6px 10px; font-size: 13px; }
.btn:disabled { opacity: 0.55; cursor: progress; }
.icon-btn {
  border: none;
  background: transparent;
  width: 32px;
  height: 32px;
  border-radius: 8px;
  cursor: pointer;
  color: var(--muted);
  font-size: 15px;
}
.icon-btn:hover { background: var(--surface-2); color: var(--text); }

.spinning .spin-icon, .card.loading .refresh-icon { display: inline-block; animation: spin 0.9s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }

/* ---- Layout ---- */

.layout { display: grid; grid-template-columns: 240px 1fr; min-height: calc(100vh - 61px); }

.sidebar {
  padding: 18px 12px;
  border-right: 1px solid var(--border);
  position: sticky;
  top: 61px;
  align-self: start;
  max-height: calc(100vh - 61px);
  overflow-y: auto;
}
.room-list { list-style: none; margin: 0 0 12px; padding: 0; display: flex; flex-direction: column; gap: 2px; }
.room-list button {
  width: 100%;
  display: flex;
  align-items: center;
  gap: 10px;
  border: none;
  background: transparent;
  padding: 9px 12px;
  border-radius: 10px;
  cursor: pointer;
  text-align: left;
}
.room-list button:hover { background: var(--surface-2); }
.room-list button.active { background: var(--accent-soft); font-weight: 600; }
.room-list .r-icon { width: 22px; text-align: center; }
.room-list .r-name { flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.room-list .r-count { font-size: 12px; color: var(--muted); font-variant-numeric: tabular-nums; }
.add-room { margin-left: 4px; }

main { padding: 22px 28px 60px; min-width: 0; }

.toolbar { display: flex; flex-wrap: wrap; align-items: center; justify-content: space-between; gap: 12px; margin-bottom: 16px; }
.view-heading { display: flex; align-items: center; gap: 10px; }
.view-heading h1 { margin: 0; font-size: 24px; }
.view-heading .room-total { color: var(--muted); font-weight: 500; font-size: 15px; }
.room-actions { display: flex; gap: 2px; }
.filters { display: flex; gap: 8px; flex-wrap: wrap; }
.filters input, .filters select, .field input, .field select, .field textarea {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 8px 10px;
  outline: none;
}
.filters input:focus, .filters select:focus, .field input:focus, .field select:focus, .field textarea:focus {
  border-color: var(--accent);
  box-shadow: 0 0 0 3px var(--accent-soft);
}
.filters input[type="search"] { width: 200px; }

.status-line {
  margin: -4px 0 14px;
  font-size: 13px;
  color: var(--muted);
}

/* ---- Cards ---- */

.grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(240px, 1fr)); gap: 18px; }

.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  overflow: hidden;
  display: flex;
  flex-direction: column;
  transition: transform 0.15s, box-shadow 0.15s;
}
.card:hover { transform: translateY(-2px); }
.card.purchased { opacity: 0.6; }
.card.purchased .thumb img { filter: grayscale(0.7); }

.thumb {
  position: relative;
  display: block;
  aspect-ratio: 4 / 3;
  background: #fff;
  border-bottom: 1px solid var(--border);
}
.thumb { overflow: hidden; }
.thumb img { position: absolute; inset: 0; width: 100%; height: 100%; object-fit: contain; padding: 14px; }
.thumb .no-image {
  position: absolute;
  inset: 0;
  display: grid;
  place-items: center;
  font-size: 54px;
  background: var(--surface-2);
}
.thumb .pills { position: absolute; top: 10px; left: 10px; right: 10px; display: flex; justify-content: space-between; gap: 6px; }

.pill {
  display: inline-flex;
  align-items: center;
  gap: 5px;
  padding: 3px 9px;
  border-radius: 999px;
  font-size: 12px;
  font-weight: 600;
  white-space: nowrap;
}
.pill::before { content: ""; width: 7px; height: 7px; border-radius: 50%; background: currentColor; }
.pill.in_stock, .pill.limited { background: var(--good-soft); color: var(--good); }
.pill.limited { background: var(--warn-soft); color: var(--warn); }
.pill.preorder, .pill.backorder { background: var(--info-soft); color: var(--info); }
.pill.out_of_stock, .pill.discontinued { background: var(--bad-soft); color: var(--bad); }
.pill.unknown { background: var(--surface-2); color: var(--muted); }
.pill.bought { background: var(--accent); color: var(--accent-text); }
.pill.bought::before { display: none; }
.pill.drop { background: var(--good); color: #fff; }
.pill.drop::before { display: none; }

.card-body { padding: 12px 14px 6px; display: flex; flex-direction: column; gap: 6px; flex: 1; }
.meta-row { display: flex; align-items: center; gap: 8px; font-size: 12px; color: var(--muted); flex-wrap: wrap; }
.store { font-weight: 600; text-transform: lowercase; }
.prio { display: inline-flex; align-items: center; gap: 4px; }
.prio::before { content: ""; width: 8px; height: 8px; border-radius: 50%; }
.prio.high::before { background: var(--bad); }
.prio.medium::before { background: var(--warn); }
.prio.low::before { background: var(--muted); }
.room-tag { margin-left: auto; }

.card h3 { margin: 0; font-size: 15px; font-weight: 600; line-height: 1.3; }
.card h3 a {
  color: inherit;
  text-decoration: none;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
}
.card h3 a:hover { text-decoration: underline; }

.price-row { display: flex; align-items: baseline; gap: 8px; flex-wrap: wrap; }
.price { font-size: 22px; font-weight: 700; font-variant-numeric: tabular-nums; }
.price.none { font-size: 15px; font-weight: 500; color: var(--muted); }
.qty { color: var(--muted); font-size: 13px; }
.change { font-size: 13px; font-weight: 600; font-variant-numeric: tabular-nums; }
.change.down { color: var(--good); }
.change.up { color: var(--bad); }
.manual-tag { font-size: 11px; color: var(--muted); border: 1px solid var(--border); border-radius: 6px; padding: 0 5px; }

.sub { font-size: 12px; color: var(--muted); display: flex; gap: 10px; align-items: center; flex-wrap: wrap; }
.spark { color: var(--accent); }
.notes { margin: 2px 0 0; font-size: 13px; color: var(--muted); white-space: pre-wrap; overflow-wrap: anywhere; }
.card .error { margin: 2px 0 0; font-size: 12px; color: var(--warn); }

.card-actions { display: flex; justify-content: flex-end; gap: 2px; padding: 4px 8px 8px; }

.empty {
  text-align: center;
  padding: 70px 20px;
  color: var(--muted);
  border: 2px dashed var(--border);
  border-radius: var(--radius);
}
.empty .big { font-size: 46px; }
.empty h2 { color: var(--text); margin: 8px 0 4px; font-size: 19px; }
.empty p { margin: 0 0 16px; }

/* ---- Dialogs ---- */

/* Keep the page behind an open dialog still, so swipes scroll the form instead. */
html:has(dialog[open]) { overflow: hidden; }

.dialog {
  width: min(620px, calc(100vw - 32px));
  max-height: calc(100vh - 40px);
  max-height: calc(var(--vvh, 100dvh) - 40px);
  overscroll-behavior: contain;
  border: 1px solid var(--border);
  border-radius: 18px;
  padding: 0;
  background: var(--surface);
  color: var(--text);
  box-shadow: 0 20px 60px rgba(0, 0, 0, 0.3);
}
.dialog.narrow { width: min(420px, calc(100vw - 32px)); }
.dialog::backdrop { background: rgba(20, 16, 10, 0.45); backdrop-filter: blur(2px); }
.dialog form { padding: 18px 22px 20px; display: flex; flex-direction: column; gap: 14px; }
.dialog-head { display: flex; justify-content: space-between; align-items: center; }
.dialog-head h2 { margin: 0; font-size: 19px; }
.dialog-foot { display: flex; justify-content: flex-end; gap: 8px; margin-top: 4px; }

.field { display: flex; flex-direction: column; gap: 5px; font-size: 13px; font-weight: 600; }
.field em { font-weight: 400; font-style: normal; color: var(--muted); }
.field input, .field select, .field textarea { font-weight: 400; font-size: 15px; width: 100%; }
.field textarea { resize: vertical; }
.field-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }
.field-grid .span-2 { grid-column: span 2; }
.icon-field { max-width: 80px; }
#roomForm .field-grid { grid-template-columns: 80px 1fr; }
.icon-field input { text-align: center; font-size: 20px; padding: 5px; }
.url-row { display: flex; gap: 8px; }
.url-row input { flex: 1; }

.emoji-picks { display: flex; flex-wrap: wrap; gap: 4px; }
.emoji-picks button { border: 1px solid var(--border); background: var(--surface); border-radius: 8px; font-size: 18px; width: 36px; height: 36px; cursor: pointer; }
.emoji-picks button:hover { background: var(--surface-2); }

.preview {
  display: grid;
  grid-template-columns: 96px 1fr;
  gap: 14px;
  align-items: center;
  padding: 12px;
  border-radius: 12px;
  background: var(--surface-2);
}
.preview.loading { grid-template-columns: 1fr; text-align: center; color: var(--muted); }
.preview .p-img { width: 96px; height: 96px; border-radius: 10px; background: #fff; display: grid; place-items: center; overflow: hidden; font-size: 34px; }
.preview .p-img img { width: 100%; height: 100%; object-fit: contain; }
.preview .p-title { font-weight: 600; line-height: 1.3; }
.preview .p-line { display: flex; gap: 10px; align-items: center; margin-top: 6px; flex-wrap: wrap; }
.preview .p-price { font-weight: 700; font-size: 18px; }
.preview .p-error { color: var(--warn); font-size: 13px; margin-top: 6px; }

.form-error { margin: 0; padding: 8px 12px; border-radius: 10px; background: var(--bad-soft); color: var(--bad); font-size: 14px; }

.toast {
  position: fixed;
  bottom: 24px;
  left: 50%;
  transform: translateX(-50%);
  background: var(--text);
  color: var(--bg);
  padding: 10px 18px;
  border-radius: 12px;
  box-shadow: var(--shadow);
  z-index: 100;
  font-size: 14px;
  max-width: calc(100vw - 32px);
}

/* ---- Small screens ---- */

@media (max-width: 760px) {
  .topbar { flex-wrap: wrap; padding: 10px 16px; gap: 8px; }
  .summary { order: 3; flex-basis: 100%; }
  .top-actions { margin-left: auto; }
  .top-actions .label { display: none; }
  .layout { grid-template-columns: 1fr; }
  .sidebar {
    position: static;
    max-height: none;
    border-right: none;
    border-bottom: 1px solid var(--border);
    padding: 10px 16px;
    display: flex;
    gap: 6px;
    overflow-x: auto;
  }
  .room-list { flex-direction: row; margin: 0; }
  .room-list button { white-space: nowrap; padding: 7px 12px; background: var(--surface); border: 1px solid var(--border); }
  .add-room { margin: 0; }
  main { padding: 16px 16px 50px; }
  .filters { width: 100%; }
  .filters input[type="search"] { flex: 1 1 100%; width: auto; min-width: 0; }
  .filters select { flex: 1; min-width: 0; }
  .grid { grid-template-columns: repeat(auto-fill, minmax(160px, 1fr)); gap: 12px; }
  .price { font-size: 19px; }
  .field-grid { grid-template-columns: 1fr; }
  .field-grid .span-2 { grid-column: auto; }

  /* 16px text stops iPhones zooming in (and cutting off the form) when a box is tapped. */
  .filters input, .filters select, .field input, .field select, .field textarea { font-size: 16px; }

  /* Dialogs fill the screen above the keyboard; the title and buttons stay put while the fields scroll. */
  .dialog, .dialog.narrow {
    width: 100%;
    max-width: none;
    height: var(--vvh, 100dvh);
    max-height: var(--vvh, 100dvh);
    margin: 0;
    top: var(--vv-top, 0px);
    border: none;
    border-radius: 0;
  }
  .dialog form { padding: 0 16px; min-height: 100%; gap: 14px; }
  .dialog-head, .dialog-foot {
    position: sticky;
    z-index: 1;
    background: var(--surface);
    margin: 0 -16px;
    padding: 12px 16px;
  }
  .dialog-head { top: 0; border-bottom: 1px solid var(--border); }
  .dialog-foot {
    bottom: 0;
    margin-top: auto;
    border-top: 1px solid var(--border);
    padding-bottom: max(12px, env(safe-area-inset-bottom));
  }
  .dialog-foot .btn { flex: 1; justify-content: center; padding: 11px 14px; }
  .dialog .icon-btn { width: 40px; height: 40px; font-size: 18px; }
  .url-row .btn { padding: 8px 16px; }
  .preview { grid-template-columns: 72px 1fr; gap: 12px; }
  .preview .p-img { width: 72px; height: 72px; }
}
.muted { color: var(--muted); }
