:root {
  --bg: #f5f6f8;
  --card-bg: #ffffff;
  --text: #1c1f26;
  --muted: #6b7280;
  --border: #e3e5ea;
  --primary: #2563eb;
  --primary-dark: #1d4ed8;
  --danger: #dc2626;
  --dup: #eaf7ea;
  --dup-border: #b9e3b9;
  --radius: 10px;
  --shadow: 0 1px 3px rgba(0,0,0,0.08);
}

html.dark {
  --bg: #14161c;
  --card-bg: #1e2129;
  --text: #e7e9ee;
  --muted: #9099a8;
  --border: #2c303a;
  --primary: #3b82f6;
  --primary-dark: #2563eb;
  --danger: #f87171;
  --dup: #16261a;
  --dup-border: #234a2b;
  --shadow: 0 1px 3px rgba(0,0,0,0.4);
}
html.dark input[type="search"], html.dark input[type="text"], html.dark input[type="number"],
html.dark input[type="date"], html.dark input[type="password"], html.dark select, html.dark textarea {
  background: #262a33; color: var(--text); border-color: var(--border);
}
html.dark .game-meta .tag { background: #2a2e38; }
html.dark .secondary-btn, html.dark .icon-btn, html.dark .game-actions button { background: #262a33; color: var(--text); }
html.dark .close-x { background: #262a33; }

* { box-sizing: border-box; }

/* The .modal-overlay rule below sets display:flex, which (per the CSS
   cascade) overrides the browser's built-in "display:none" for [hidden]
   even though specificity looks equal — author styles always beat the
   user-agent stylesheet. Without this rule, "hidden" modals still render
   as full-screen overlays and swallow every click on the page. */
[hidden] { display: none !important; }

body {
  margin: 0;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  background: var(--bg);
  color: var(--text);
  -webkit-tap-highlight-color: transparent;
}

#app { max-width: 900px; margin: 0 auto; padding: 12px 12px 80px; }

.topbar {
  display: flex; align-items: center; justify-content: space-between;
  padding: 10px 4px;
}
.topbar h1 { font-size: 1.25rem; margin: 0; }
.logout-link { color: var(--muted); text-decoration: none; font-size: 0.9rem; }
.logout-link:hover { color: var(--text); }

.topbar-actions { display: flex; align-items: center; gap: 6px; }
.topbar-actions .icon-btn { padding: 6px 10px; font-size: 1rem; }

.menu-wrap { position: relative; }
.menu-dropdown {
  position: absolute; right: 0; top: calc(100% + 6px); background: var(--card-bg);
  border: 1px solid var(--border); border-radius: 10px; box-shadow: 0 6px 20px rgba(0,0,0,0.15);
  min-width: 180px; overflow: hidden; z-index: 40;
}
.menu-dropdown a, .menu-dropdown button {
  display: block; width: 100%; text-align: left; padding: 11px 14px; font-size: 0.9rem;
  color: var(--text); text-decoration: none; background: none; border: none; cursor: pointer;
}
.menu-dropdown a:hover, .menu-dropdown button:hover { background: var(--bg); }

.stats-panel {
  background: var(--card-bg); border: 1px solid var(--border); border-radius: var(--radius);
  padding: 14px; margin-bottom: 12px;
}
.stats-summary { display: flex; gap: 10px; margin-bottom: 8px; }
.stat-tile {
  flex: 1; background: var(--bg); border-radius: 8px; padding: 10px; text-align: center;
}
.stat-tile strong { display: block; font-size: 1.3rem; }
.stat-tile span { color: var(--muted); font-size: 0.8rem; }
.stat-tile-link { display: block; text-decoration: none; color: var(--text); cursor: pointer; }
.stat-tile-link:hover { outline: 1px solid var(--primary); }

/* ---- Sold games: profit/loss coloring ---- */
.profit-positive { color: #1a7a1a; font-weight: 600; }
.profit-negative { color: var(--danger); font-weight: 600; }
html.dark .profit-positive { color: #6fcf6f; }
span.profit-positive.tag, span.profit-negative.tag { background: var(--bg); }
.sold-stats-bar span { display: inline-block; }
.stats-panel h3 { font-size: 0.85rem; color: var(--muted); margin: 10px 0 4px; }
.stat-row { display: flex; justify-content: space-between; font-size: 0.9rem; padding: 3px 0; }

.form-success { color: #1a7a1a; font-size: 0.85rem; margin-top: 10px; }
html.dark .form-success { color: #6fcf6f; }

.tabs { display: flex; gap: 8px; margin: 4px 0 12px; }
.tab {
  flex: 1; padding: 10px; border: 1px solid var(--border); background: var(--card-bg);
  border-radius: var(--radius); font-size: 0.95rem; font-weight: 600; color: var(--muted);
  cursor: pointer;
}
.tab.active { background: var(--primary); border-color: var(--primary); color: #fff; }

.toolbar { display: flex; flex-direction: column; gap: 8px; margin-bottom: 10px; }
.toolbar-row { display: flex; gap: 8px; }
.toolbar-row > * { flex: 1; }

input[type="search"], input[type="text"], input[type="number"], input[type="date"],
select, textarea {
  width: 100%; padding: 10px; border: 1px solid var(--border); border-radius: 8px;
  font-size: 0.95rem; background: #fff; color: var(--text);
}
textarea { resize: vertical; font-family: inherit; }

.primary-btn, .secondary-btn, .icon-btn, .link-btn {
  padding: 10px 14px; border-radius: 8px; font-size: 0.95rem; font-weight: 600;
  border: 1px solid transparent; cursor: pointer;
}
.primary-btn { background: var(--primary); color: #fff; }
.primary-btn:hover { background: var(--primary-dark); }
.secondary-btn { background: #fff; border-color: var(--border); color: var(--text); }
.icon-btn { background: #fff; border-color: var(--border); color: var(--text); padding: 8px 12px; }
.link-btn { background: none; border: none; color: var(--primary); padding: 0; font-weight: 600; cursor: pointer; }

.tag-filter-bar {
  display: flex; align-items: center; justify-content: space-between; flex-wrap: wrap; gap: 6px;
  background: var(--card-bg); border: 1px solid var(--border); border-radius: 8px;
  padding: 8px 12px; margin: 0 4px 10px; font-size: 0.85rem;
}
.tag-filter-chips { display: flex; flex-wrap: wrap; gap: 6px; }

.tag-filter-wrap { position: relative; }
.tag-filter-dropdown {
  left: 0; right: 0; min-width: unset; max-height: 260px; overflow-y: auto; padding: 6px;
}
.tag-filter-option {
  display: flex; align-items: center; gap: 8px; padding: 8px 6px; font-size: 0.9rem;
  color: var(--text); cursor: pointer; border-radius: 6px;
}
.tag-filter-option:hover { background: var(--bg); }
.tag-filter-option input { flex-shrink: 0; }
.tag-filter-option .tag-manage-count { margin-left: auto; }

.totals-bar {
  font-size: 0.85rem; color: var(--muted); margin: 4px 4px 12px; min-height: 1.1em;
}
.totals-bar span { margin-right: 12px; }
.totals-bar .omr-total { color: var(--text); }
.totals-bar .unrated-note, .unrated-note { color: #a05a00; font-size: 0.8rem; }
html.dark .totals-bar .unrated-note, html.dark .unrated-note { color: #e0a350; }
.stat-row.omr-total { font-weight: 700; border-top: 1px solid var(--border); margin-top: 4px; padding-top: 6px; }
#ratesRows label { display: flex; align-items: center; justify-content: space-between; gap: 10px; }
#ratesRows input { max-width: 140px; }

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

.console-section-header {
  display: flex; align-items: center; gap: 6px;
  font: inherit; font-weight: 700; font-size: 0.8rem; color: var(--muted); text-transform: uppercase;
  letter-spacing: 0.03em; margin: 18px 4px 8px; padding: 0 0 5px; border: none; border-bottom: 2px solid var(--border);
  background: none; width: 100%; text-align: left; cursor: pointer;
}
.console-section-header:first-child { margin-top: 4px; }
.console-section-header:hover { color: var(--text); }
.console-section-header .section-arrow {
  display: inline-block; transition: transform 0.15s ease; font-size: 0.75em;
}
.console-section-header.collapsed .section-arrow { transform: rotate(-90deg); }
.game-seq { color: var(--muted); font-weight: 500; margin-right: 4px; }

.game-card {
  background: var(--card-bg); border: 1px solid var(--border); border-radius: var(--radius);
  box-shadow: var(--shadow); padding: 12px; margin-bottom: 10px;
}
.game-card.duplicate { background: var(--dup); border-color: var(--dup-border); }

.game-card-top { display: flex; justify-content: space-between; align-items: flex-start; gap: 8px; }
.game-name { font-weight: 700; font-size: 1.02rem; }
.dup-badge {
  font-size: 0.7rem; font-weight: 700; background: #4caf50; color: #fff;
  padding: 2px 6px; border-radius: 20px; margin-left: 6px; vertical-align: middle;
}
.game-price { font-weight: 700; white-space: nowrap; text-align: right; }
.price-breakdown { font-weight: 400; font-size: 0.72rem; color: var(--muted); white-space: nowrap; margin-top: 1px; }

.game-meta { color: var(--muted); font-size: 0.85rem; margin-top: 4px; display: flex; flex-wrap: wrap; gap: 4px 10px; }
.game-meta .tag { background: #eef0f4; border-radius: 6px; padding: 1px 7px; }
.book-with { color: #1a7a1a; }
.book-no { color: #a05a00; }
.tag-pill { color: var(--primary); }
html.dark .tag-pill { color: #8ab4f8; }

.condition-badge { font-weight: 600; }
.condition-badge.condition-poor { background: #fdecea; color: #b3261e; }
.condition-badge.condition-ok { background: #fff4de; color: #a05a00; }
.condition-badge.condition-great { background: #e7f6e9; color: #1a7a1a; }
html.dark .condition-badge.condition-poor { background: #3a1f1f; color: #f87171; }
html.dark .condition-badge.condition-ok { background: #3a2e1a; color: #f0b866; }
html.dark .condition-badge.condition-great { background: #16261a; color: #6fcf6f; }

.game-notes { font-size: 0.85rem; color: var(--muted); margin-top: 6px; font-style: italic; }

.game-actions { display: flex; gap: 8px; margin-top: 10px; flex-wrap: wrap; }
.game-actions button { padding: 7px 12px; font-size: 0.85rem; font-weight: 600; border-radius: 7px; border: 1px solid var(--border); background: #fff; cursor: pointer; }
.game-actions .delete-btn { color: var(--danger); border-color: #f3c6c6; }
.game-actions .photo-btn { color: var(--primary); border-color: #c6d7f3; }

.modal-overlay {
  position: fixed; inset: 0; background: rgba(0,0,0,0.45);
  display: flex; align-items: flex-end; justify-content: center; z-index: 50;
}
.modal {
  background: var(--card-bg); color: var(--text); width: 100%; max-width: 480px; max-height: 92vh; overflow-y: auto;
  border-radius: 16px 16px 0 0; padding: 18px;
}
@media (min-width: 620px) {
  .modal-overlay { align-items: center; }
  .modal { border-radius: 16px; }
}
.modal h2 { margin-top: 0; }
.modal form label { display: block; font-size: 0.85rem; color: var(--muted); font-weight: 600; margin: 10px 0 4px; }
.field-row { display: flex; gap: 10px; }
.field-row > label { flex: 1; }
.checkbox-label { font-weight: 500 !important; display: inline-flex !important; align-items: center; gap: 6px; margin: 0 !important; color: var(--text) !important; }
.current-photo-row { display: flex; align-items: center; justify-content: space-between; margin-top: 8px; }
.form-error { color: var(--danger); font-size: 0.85rem; margin-top: 10px; }
.modal-actions { display: flex; gap: 10px; justify-content: flex-end; margin-top: 18px; }

.tag-chips { display: flex; flex-wrap: wrap; gap: 6px; margin-bottom: 6px; }
.tag-chip {
  display: inline-flex; align-items: center; gap: 4px; background: #eef0f4;
  border-radius: 20px; padding: 3px 6px 3px 10px; font-size: 0.85rem; color: var(--text); font-weight: 500;
}
.tag-chip button {
  background: none; border: none; color: var(--muted); cursor: pointer; font-size: 1rem;
  line-height: 1; padding: 0 2px;
}
html.dark .tag-chip { background: #2a2e38; }

.tag-manage-row { padding: 8px 0; border-bottom: 1px solid var(--border); }
.tag-manage-main { display: flex; align-items: center; gap: 8px; }
.tag-manage-row input[type="text"] { flex: 1; }
.tag-manage-count { color: var(--muted); font-size: 0.8rem; min-width: 20px; text-align: center; }
.tag-manage-row .link-btn { white-space: nowrap; }
.tag-manage-row .delete-btn {
  padding: 6px 10px; font-size: 0.8rem; border-radius: 7px; border: 1px solid #f3c6c6;
  background: #fff; color: var(--danger); cursor: pointer; white-space: nowrap;
}
html.dark .tag-manage-row .delete-btn { background: #262a33; }

.tag-manage-photo-row { display: flex; align-items: center; gap: 10px; margin-top: 6px; }
.tag-manage-thumb { width: 36px; height: 36px; border-radius: 7px; object-fit: cover; border: 1px solid var(--border); }
.tag-photo-upload-label { font-size: 0.8rem; cursor: pointer; }
.tag-reorder-btn { padding: 4px 6px; font-size: 0.75rem; }

.cleanup-row { padding: 10px 0; border-bottom: 1px solid var(--border); }
.cleanup-row-label { font-size: 0.85rem; color: var(--muted); margin-bottom: 6px; }
.cleanup-row-action { display: flex; gap: 8px; }
.cleanup-row-action input { flex: 1; }

.spend-chart { display: flex; align-items: flex-end; gap: 8px; height: 100px; margin: 8px 0 4px; overflow-x: auto; padding-bottom: 2px; }
.spend-bar-wrap { display: flex; flex-direction: column; align-items: center; justify-content: flex-end; min-width: 24px; flex-shrink: 0; }
.spend-bar { width: 14px; background: var(--primary); border-radius: 3px 3px 0 0; }
.spend-bar-label { font-size: 0.65rem; color: var(--muted); margin-top: 4px; }

@keyframes surprisePulse {
  0% { box-shadow: 0 0 0 4px rgba(37,99,235,0.5); }
  100% { box-shadow: var(--shadow); }
}
.game-card.surprise-highlight { animation: surprisePulse 2.2s ease; }

.collections-grid {
  display: grid; grid-template-columns: repeat(auto-fill, minmax(140px, 1fr)); gap: 12px; margin-top: 14px;
}
.collection-card {
  background: var(--card-bg); border: 1px solid var(--border); border-radius: var(--radius);
  box-shadow: var(--shadow); padding: 18px 14px; text-align: center; text-decoration: none; color: var(--text);
  display: flex; flex-direction: column; align-items: center; gap: 6px;
}
.collection-card:hover { border-color: var(--primary); }
.collection-card-icon { font-size: 1.8rem; }
.collection-card-photo { width: 64px; height: 64px; border-radius: 10px; object-fit: cover; }
.collection-card-name { font-weight: 700; }
.collection-card-count { color: var(--muted); font-size: 0.8rem; }

.photo-modal { position: relative; padding: 10px; max-width: min(92vw, 560px); }
/* A bounded "stage" instead of a bare, unconstrained <img> — without this,
   a tall/portrait photo (very common for box-cover shots) just kept
   growing past the screen at width:100%;height:auto, forcing an awkward
   scrollbar inside the modal. Capping the stage height and letting the
   image scale down to fit inside it (object-fit: contain, so nothing gets
   cropped) keeps the whole photo on screen on any device, phone or
   desktop — the same approach shopping-site product galleries use. */
.photo-modal-stage {
  display: flex; align-items: center; justify-content: center;
  height: min(55vh, 420px); background: var(--bg); border-radius: 8px; overflow: hidden;
}
.photo-modal-stage img { max-width: 100%; max-height: 100%; width: auto; height: auto; object-fit: contain; display: block; }
.close-x { position: absolute; top: -14px; right: -14px; background: #fff; border-radius: 50%; width: 34px; height: 34px; font-size: 1.3rem; line-height: 1; }

/* ---- Extra photos (edit form gallery + photo viewer thumbs) ---- */
.extra-photos-field { margin-bottom: 14px; }
.extra-photos-label { font-weight: 600; font-size: 0.85rem; display: block; margin-bottom: 6px; }
.extra-photos-grid { display: flex; flex-wrap: wrap; gap: 8px; margin-bottom: 8px; }
.extra-photo-thumb { position: relative; width: 64px; height: 64px; }
.extra-photo-thumb img { width: 100%; height: 100%; object-fit: cover; border-radius: 8px; border: 1px solid var(--border); }
.extra-photo-thumb button {
  position: absolute; top: -6px; right: -6px; width: 20px; height: 20px; border-radius: 50%;
  background: var(--danger); color: #fff; border: none; font-size: 0.85rem; line-height: 1; cursor: pointer;
}
.extra-photo-upload-label { display: inline-block; }
.photo-modal-thumbs { display: flex; gap: 6px; margin-top: 10px; overflow-x: auto; padding-bottom: 2px; }
.photo-modal-thumb { width: 56px; height: 56px; flex-shrink: 0; object-fit: cover; border-radius: 6px; cursor: pointer; opacity: 0.6; border: 2px solid transparent; background: var(--bg); }
.photo-modal-thumb.active { opacity: 1; border-color: var(--primary); }

/* ---- Loan tracker badge ---- */
.loan-badge { background: #fff4e0; color: #9a5b00; }
html.dark .loan-badge { background: #3a2c10; color: #f0c56a; }

/* Wishlist item that matches something already in the Owned collection —
   deliberately a different color from the green ×N duplicate-within-list
   badge (.dup-badge), since it's a different kind of flag. */
.already-owned-badge { background: #fff4e0; color: #9a5b00; font-weight: 700; }
html.dark .already-owned-badge { background: #3a2c10; color: #f0c56a; }

/* ---- Wishlist priority reorder buttons (on the card, when sorting by Priority) ---- */
.wishlist-reorder { display: inline-flex; flex-direction: column; margin-right: 6px; vertical-align: middle; }
.wishlist-reorder button {
  background: none; border: 1px solid var(--border); color: var(--primary); cursor: pointer;
  font-size: 0.6rem; line-height: 1; padding: 2px 4px; border-radius: 4px; margin-bottom: 1px;
}

/* ---- Share collection modal ---- */
.share-link-row { display: flex; gap: 8px; margin: 10px 0; }
.share-link-row input { flex: 1; padding: 8px 10px; border: 1px solid var(--border); border-radius: 8px; font-size: 0.85rem; background: var(--card-bg); color: var(--text); }

@media (min-width: 700px) {
  .game-card-top { align-items: center; }
}

/* ---- Login page ---- */
.login-body {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px;
}
.login-card {
  background: var(--card-bg);
  border: 1px solid var(--border);
  border-radius: 16px;
  box-shadow: 0 4px 20px rgba(0,0,0,0.08);
  padding: 32px 28px;
  width: 100%;
  max-width: 360px;
  text-align: center;
}
.login-card h1 { font-size: 1.4rem; margin: 0 0 6px; }
.login-sub { color: var(--muted); font-size: 0.9rem; margin: 0 0 20px; }
.login-card form { display: flex; flex-direction: column; gap: 12px; }
.login-card input[type="password"] {
  width: 100%; padding: 12px; border: 1px solid var(--border); border-radius: 8px;
  font-size: 1rem; text-align: center;
}
.login-card button {
  padding: 12px; border-radius: 8px; border: none; background: var(--primary);
  color: #fff; font-size: 1rem; font-weight: 600; cursor: pointer;
}
.login-card button:hover { background: var(--primary-dark); }
.login-error {
  background: #fdecea; color: var(--danger); border: 1px solid #f5c6c2;
  border-radius: 8px; padding: 8px 12px; font-size: 0.85rem; margin: 0 0 16px;
}

/* ---- Import page ---- */
.import-page { padding-top: 8px; }
.page-hint { color: var(--muted); font-size: 0.9rem; line-height: 1.5; }
.import-form { display: flex; flex-direction: column; gap: 12px; max-width: 420px; margin-top: 12px; }
.import-form input[type="file"] {
  border: 1px dashed var(--border); border-radius: 8px; padding: 14px; background: var(--card-bg);
}
.import-preview-wrap { overflow-x: auto; margin: 14px 0; border: 1px solid var(--border); border-radius: var(--radius); }
.import-preview { border-collapse: collapse; width: 100%; font-size: 0.85rem; white-space: nowrap; }
.import-preview th, .import-preview td { padding: 8px 10px; border-bottom: 1px solid var(--border); text-align: left; }
.import-preview th { background: var(--card-bg); position: sticky; top: 0; }
.import-warnings { color: var(--muted); font-size: 0.85rem; }

/* ---- Hub (section-picker landing page) ---- */
.hub-grand-total {
  background: var(--card-bg); border: 1px solid var(--border); border-radius: var(--radius);
  box-shadow: var(--shadow); padding: 18px; text-align: center; margin: 18px 0 22px;
}
.hub-grand-total .hub-grand-label { color: var(--muted); font-size: 0.8rem; text-transform: uppercase; letter-spacing: 0.03em; }
.hub-grand-total .hub-grand-value { font-size: 1.9rem; font-weight: 800; color: var(--text); margin-top: 4px; }
.hub-grand-total .hub-grand-note { color: var(--muted); font-size: 0.78rem; margin-top: 6px; }
.hub-sections-grid {
  display: grid; grid-template-columns: repeat(auto-fill, minmax(160px, 1fr)); gap: 14px;
}
.hub-section-card {
  background: var(--card-bg); border: 1px solid var(--border); border-radius: var(--radius);
  box-shadow: var(--shadow); padding: 20px 16px; text-align: center; text-decoration: none; color: var(--text);
  display: flex; flex-direction: column; align-items: center; gap: 6px;
}
.hub-section-card:hover { border-color: var(--primary); }
.hub-section-icon { font-size: 2.1rem; }
.hub-section-name { font-weight: 700; font-size: 1.05rem; margin-top: 2px; }
.hub-section-count { color: var(--muted); font-size: 0.8rem; }
.hub-section-total { font-weight: 700; color: var(--primary); font-size: 0.95rem; margin-top: 2px; }
.hub-section-card.hub-empty { color: var(--muted); }
.hub-section-card.hub-empty .hub-section-total { color: var(--muted); }
