:root {
  --bg: #f6f7f9;
  --surface: #ffffff;
  --surface-2: #eef0f4;
  --text: #16181d;
  --muted: #5d6472;
  --border: #d9dde5;
  --accent: #2463eb;
  --accent-text: #ffffff;
  --danger: #c2372f;
  --warn-bg: #fff6dd;
  --warn-border: #e9c46a;
  --error-bg: #fde8e7;
  --info-bg: #e6efff;
  --foil: linear-gradient(90deg, #f7c3ff, #b4e1ff, #c8ffd4, #fff2a8);
  --radius: 10px;
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #0f1115;
    --surface: #181b21;
    --surface-2: #22262e;
    --text: #e8eaef;
    --muted: #9aa1ae;
    --border: #2e333d;
    --accent: #5b8cff;
    --accent-text: #0b0d12;
    --danger: #ff6b61;
    --warn-bg: #3a3016;
    --warn-border: #8a6d1f;
    --error-bg: #3d1a18;
    --info-bg: #182640;
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 15px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
}

h1, h2, h3, p { margin: 0; }

.topbar {
  display: flex;
  flex-wrap: wrap;
  gap: 12px 24px;
  align-items: center;
  justify-content: space-between;
  padding: 16px clamp(16px, 3vw, 32px);
  background: var(--surface);
  border-bottom: 1px solid var(--border);
}

.brand h1 { font-size: 20px; letter-spacing: -0.01em; }
.tagline { color: var(--muted); font-size: 13px; }
.actions { display: flex; flex-wrap: wrap; gap: 8px; }

.btn {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 8px 14px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--surface);
  color: var(--text);
  font: inherit;
  font-weight: 500;
  cursor: pointer;
}
.btn:hover:not(:disabled) { background: var(--surface-2); }
.btn:disabled { opacity: 0.5; cursor: not-allowed; }
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-text); }
.btn.primary:hover { filter: brightness(1.08); background: var(--accent); }
.btn.danger { color: var(--danger); }
.btn.small { padding: 4px 10px; font-size: 13px; }
.btn.more { display: block; margin: 16px auto 32px; }

.notice {
  display: flex;
  gap: 12px;
  align-items: flex-start;
  margin: 12px clamp(16px, 3vw, 32px) 0;
  padding: 10px 14px;
  border-radius: var(--radius);
  border: 1px solid var(--border);
  background: var(--info-bg);
}
.notice.warn { background: var(--warn-bg); border-color: var(--warn-border); }
.notice.error { background: var(--error-bg); border-color: var(--danger); }
.notice .close {
  margin-left: auto;
  border: 0;
  background: none;
  color: inherit;
  font-size: 18px;
  line-height: 1;
  cursor: pointer;
}

main { padding: 4px clamp(16px, 3vw, 32px) 0; }

.filters {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  align-items: center;
  padding: 14px 0;
}
.filters input[type="search"], .filters select {
  padding: 7px 10px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--surface);
  color: var(--text);
  font: inherit;
}
.filters input[type="search"] { flex: 1 1 220px; max-width: 360px; }
.filters select { max-width: 220px; text-overflow: ellipsis; }
.spacer { flex: 1; }
.muted { color: var(--muted); font-size: 13px; }

.empty-hint {
  margin-bottom: 14px;
  padding: 12px 14px;
  border: 1px dashed var(--border);
  border-radius: var(--radius);
  color: var(--muted);
}
.empty-hint strong { color: var(--text); }

.grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(168px, 1fr));
  gap: 14px;
}

.tile {
  display: flex;
  flex-direction: column;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  overflow: hidden;
}
.tile.unknown { border-style: dashed; }

.art {
  display: block;
  width: 100%;
  aspect-ratio: 5 / 7;
  object-fit: cover;
  background: var(--surface-2);
}
.art.landscape { aspect-ratio: 7 / 5; object-fit: contain; }
.art.missing {
  display: grid;
  place-items: center;
  color: var(--muted);
  font-size: 32px;
}

.info {
  display: flex;
  flex-direction: column;
  gap: 4px;
  padding: 8px 10px 10px;
  flex: 1;
}
.info h3 {
  font-size: 14px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.sub {
  color: var(--muted);
  font-size: 12px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.badges { display: flex; flex-wrap: wrap; gap: 4px; margin: 2px 0 6px; }
.badge {
  white-space: nowrap;
  padding: 1px 6px;
  border-radius: 5px;
  background: var(--surface-2);
  font-size: 11px;
  font-weight: 600;
}
.badge.foil { background: var(--foil); color: #1d1d1d; }
.badge.extra { background: var(--warn-bg); border: 1px solid var(--warn-border); cursor: help; }

.stepper {
  display: flex;
  margin-top: auto;
  border: 1px solid var(--border);
  border-radius: 8px;
  overflow: hidden;
}
.stepper button {
  width: 34px;
  border: 0;
  background: var(--surface-2);
  color: var(--text);
  font-size: 16px;
  cursor: pointer;
}
.stepper button:hover { filter: brightness(0.95); }
.stepper button:disabled { opacity: 0.35; cursor: default; filter: none; }
.stepper.zero input { color: var(--muted); }
.stepper input {
  flex: 1;
  min-width: 0;
  border: 0;
  background: var(--surface);
  color: var(--text);
  font: inherit;
  text-align: center;
  -moz-appearance: textfield;
}
.stepper input::-webkit-inner-spin-button { -webkit-appearance: none; }

/* ---------- stacked card tiles ---------- */

.stack-cover {
  display: block;
  padding: 0;
  border: 0;
  background: none;
  cursor: pointer;
}
.stack-head {
  display: flex;
  flex-direction: column;
  gap: 6px;
  width: 100%;
  padding: 0;
  border: 0;
  background: none;
  color: inherit;
  font: inherit;
  text-align: left;
  cursor: pointer;
}
.stack-title { display: flex; flex-direction: column; min-width: 0; }
.stack-name {
  font-size: 14px;
  font-weight: 600;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.stack-meta { display: flex; flex-wrap: wrap; gap: 6px; align-items: center; }
.stack-meta .muted { font-size: 12px; }
.owned-pill {
  padding: 1px 7px;
  border-radius: 999px;
  background: var(--accent);
  color: var(--accent-text);
  font-size: 12px;
  font-weight: 700;
}
.playset-pill {
  padding: 1px 7px;
  border-radius: 999px;
  border: 1px solid var(--warn-border);
  background: var(--warn-bg);
  font-size: 12px;
  font-weight: 700;
}
.playset-pill.done { border-color: var(--accent); background: none; color: var(--accent); }
.chev { margin-left: auto; color: var(--muted); }
.stack-head:hover .stack-name { text-decoration: underline; }

.tile.open {
  grid-column: 1 / -1;
  display: grid;
  grid-template-columns: minmax(180px, 300px) 1fr;
  gap: 4px 16px;
  padding: 12px;
}
.tile.open .preview { align-self: start; position: sticky; top: 12px; }
.tile.open .preview-img { border-radius: 8px; }
.tile.open .info { padding: 0; }
.tile.open .stack-name { font-size: 17px; white-space: normal; }

.variant-list { list-style: none; margin: 8px 0 0; padding: 0; display: flex; flex-direction: column; gap: 4px; }
.variant-row {
  display: grid;
  grid-template-columns: minmax(110px, 160px) 1fr auto;
  gap: 8px;
  align-items: center;
  padding: 6px 8px;
  border-radius: 8px;
  border: 1px solid transparent;
  cursor: default;
}
.variant-row[data-img] { cursor: pointer; }
.variant-row:hover, .variant-row:focus-visible, .variant-row.selected { background: var(--surface-2); border-color: var(--border); outline: none; }
.variant-name { font-weight: 600; font-size: 13px; }
.variant-name .badge.foil { font-size: 12px; padding: 2px 8px; }
.variant-row .badges { margin: 0; }
.variant-ctrl { display: flex; flex-direction: column; gap: 4px; min-width: 120px; }
.variant-ctrl .stepper { margin-top: 0; }

@media (max-width: 640px) {
  .tile.open { grid-template-columns: 1fr; }
  .tile.open .preview { position: static; max-width: 260px; margin: 0 auto; }
  .variant-row { grid-template-columns: 1fr auto; }
  .variant-row .badges { grid-column: 1 / -1; grid-row: 2; }
}

/* ---------- view switcher ---------- */

.view-switch {
  display: inline-flex;
  padding: 2px;
  border: 1px solid var(--border);
  border-radius: 9px;
  background: var(--surface);
}
.view-switch button {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 5px 10px;
  border: 0;
  border-radius: 7px;
  background: none;
  color: var(--muted);
  font: inherit;
  font-size: 13px;
  font-weight: 600;
  cursor: pointer;
}
.view-switch svg { width: 14px; height: 14px; fill: currentColor; }
.view-switch button[aria-checked="true"] { background: var(--accent); color: var(--accent-text); }
.view-switch button:not([aria-checked="true"]):hover { color: var(--text); background: var(--surface-2); }

/* ---------- owned-variant chips (Tiles and List) ---------- */

.chips { display: none; flex-wrap: wrap; gap: 4px; }
.chip {
  padding: 1px 7px;
  border-radius: 999px;
  border: 1px solid var(--accent);
  color: var(--text);
  font-size: 11px;
  font-weight: 600;
  white-space: nowrap;
}
.chip.foil { background: var(--foil); color: #1d1d1d; border-color: transparent; }
.chip b { font-weight: 800; }

/* ---------- Tiles: horizontal cards, several per row ---------- */

.grid.view-tiles { grid-template-columns: repeat(auto-fill, minmax(300px, 1fr)); gap: 10px; }
.view-tiles .tile.stack:not(.open) { flex-direction: row; align-items: stretch; }
.view-tiles .tile.stack:not(.open) .stack-cover { flex: 0 0 92px; }
.view-tiles .tile.stack:not(.open) .art { height: 100%; aspect-ratio: auto; min-height: 128px; object-fit: cover; }
.view-tiles .tile.stack:not(.open) .art.landscape { object-fit: cover; object-position: left center; }
.view-tiles .tile.stack:not(.open) .info { padding: 10px 12px; justify-content: center; }
.view-tiles .tile.stack:not(.open) .stack-name { white-space: normal; }
.view-tiles .tile.stack:not(.open) .chips { display: flex; margin-top: 2px; }

/* ---------- List: one slim row per card ---------- */

.grid.view-list { grid-template-columns: 1fr; gap: 6px; }
.view-list .tile.stack:not(.open) { flex-direction: row; align-items: center; }
.view-list .tile.stack:not(.open):hover { border-color: var(--accent); }
.view-list .tile.stack:not(.open) .stack-cover { flex: 0 0 46px; margin: 6px 0 6px 8px; }
.view-list .tile.stack:not(.open) .art {
  width: 46px;
  height: 64px;
  aspect-ratio: auto;
  object-fit: cover;
  border-radius: 5px;
}
.view-list .tile.stack:not(.open) .art.landscape { object-position: left center; }
.view-list .tile.stack:not(.open) .info { padding: 6px 12px; }
.view-list .tile.stack:not(.open) .stack-head {
  display: grid;
  grid-template-columns: minmax(170px, 1fr) minmax(0, 2fr) auto;
  grid-template-areas: "title chips meta";
  align-items: center;
  gap: 12px;
}
.view-list .tile.stack:not(.open) .stack-title { grid-area: title; }
.view-list .tile.stack:not(.open) .chips { grid-area: chips; display: flex; }
.view-list .tile.stack:not(.open) .stack-meta { grid-area: meta; flex-wrap: nowrap; }
.view-list .tile.stack:not(.open) .stack-meta .chev { margin-left: 4px; }

@media (max-width: 720px) {
  .view-list .tile.stack:not(.open) .stack-head {
    grid-template-columns: 1fr auto;
    grid-template-areas: "title meta" "chips chips";
    gap: 6px 10px;
  }
}

/* ---------- floating preview (hovering a card's picture) ---------- */

.hover-float {
  position: fixed;
  top: 0;
  left: 0;
  z-index: 50;
  width: 250px;
  pointer-events: none;
  border-radius: 12px;
  overflow: hidden;
  box-shadow: 0 18px 40px rgb(0 0 0 / 0.45), 0 0 0 1px var(--border);
  background: var(--surface-2);
}
.hover-float.landscape { width: 340px; }
.hover-float img { display: block; width: 100%; }
@media (hover: none) { .hover-float { display: none !important; } }

/* ---------- double-sided cards: hold Shift to flip ---------- */

kbd {
  padding: 0 4px;
  border: 1px solid currentColor;
  border-bottom-width: 2px;
  border-radius: 4px;
  font: inherit;
  font-size: 0.95em;
}
.flip-hint { display: none; }
.hover-float.has-back .flip-hint {
  display: block;
  position: absolute;
  right: 8px;
  bottom: 8px;
  padding: 2px 8px;
  border-radius: 999px;
  background: rgb(0 0 0 / 0.72);
  color: #fff;
  font-size: 11px;
  font-weight: 600;
}
.preview.has-back .flip-hint {
  display: block;
  margin-top: 8px;
  color: var(--muted);
  font-size: 12px;
  text-align: center;
}
.flipped .flip-hint { color: var(--accent); }
.hover-float.flipped .flip-hint { background: var(--accent); color: var(--accent-text); }
@media (hover: none) { .flip-hint { display: none !important; } }

dialog {
  max-width: min(440px, calc(100vw - 32px));
  padding: 20px;
  border: 1px solid var(--border);
  border-radius: 12px;
  background: var(--surface);
  color: var(--text);
}
dialog::backdrop { background: rgb(0 0 0 / 0.45); }
dialog h2 { font-size: 18px; margin-bottom: 8px; }
dialog p + p { margin-top: 6px; }
.dialog-actions { display: flex; flex-wrap: wrap; justify-content: flex-end; gap: 8px; margin-top: 16px; }

dialog.wide { max-width: min(760px, calc(100vw - 32px)); }
.situation { margin-bottom: 14px; }
.choices {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(260px, 1fr));
  gap: 12px;
}
.choice {
  display: flex;
  flex-direction: column;
  gap: 8px;
  padding: 14px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--surface-2);
}
.choice.recommended { border-color: var(--accent); box-shadow: 0 0 0 1px var(--accent) inset; }
.choice h3 { font-size: 15px; }
.choice p, .choice ol { font-size: 13px; }
.choice ol { margin: 0; padding-left: 18px; color: var(--muted); }
.choice ol li + li { margin-top: 3px; }
.choice .btn { margin-top: auto; align-self: flex-start; }
.choice-note {
  padding: 6px 8px;
  border-radius: 6px;
  background: var(--warn-bg);
  border: 1px solid var(--warn-border);
}
.badge.rec { background: var(--accent); color: var(--accent-text); vertical-align: middle; }
.done-row { display: flex; flex-wrap: wrap; gap: 8px; align-items: center; margin-top: 14px; font-size: 13px; }

.footer {
  padding: 24px clamp(16px, 3vw, 32px);
  border-top: 1px solid var(--border);
  margin-top: 24px;
}
.footer a { color: inherit; }

@media (max-width: 520px) {
  .grid { grid-template-columns: repeat(auto-fill, minmax(140px, 1fr)); gap: 10px; }
  .actions .btn { flex: 1 1 auto; justify-content: center; }
}
