:root {
  --bg: #111318;
  --panel: #1b1f27;
  --panel2: #242a34;
  --text: #f4f5f7;
  --muted: #adb5c2;
  --accent: #f4b400;
  --danger: #e05252;
  --ok: #4fc38a;
  --border: #343b47;
}

* {
  box-sizing: border-box;
}

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

button,
input,
select {
  font: inherit;
}

button {
  border: 0;
  border-radius: 9px;
  padding: 0.65rem 0.85rem;
  background: var(--accent);
  color: #171717;
  font-weight: 750;
  cursor: pointer;
}

button.secondary {
  background: var(--panel2);
  color: var(--text);
  border: 1px solid var(--border);
}

button.danger {
  background: transparent;
  color: #ff8b8b;
  border: 1px solid #784044;
}

button:disabled {
  opacity: 0.45;
  cursor: not-allowed;
}

a {
  color: #ffd45a;
  text-decoration-thickness: 2px;
  text-underline-offset: 3px;
}

a:hover {
  color: #fff0ad;
}

a:focus-visible {
  outline: 3px solid rgba(244, 180, 0, 0.4);
  outline-offset: 3px;
  border-radius: 4px;
}

.official-link {
  display: inline-flex;
  align-items: center;
  gap: 0.4rem;
  margin-top: 0.5rem;
  padding: 0.65rem 0.9rem;
  border: 1px solid #ffd45a;
  border-radius: 9px;
  background: var(--accent);
  color: #171717;
  font-weight: 800;
  text-decoration: none;
  box-shadow: 0 3px 10px rgba(244, 180, 0, 0.22);
  transition:
    transform 0.15s ease,
    box-shadow 0.15s ease,
    background 0.15s ease,
    color 0.15s ease;
}

.official-link:hover {
  background: #ffd04a;
  color: #111;
  transform: translateY(-1px);
  box-shadow: 0 6px 16px rgba(244, 180, 0, 0.32);
}

.official-link:focus-visible {
  outline: 3px solid rgba(244, 180, 0, 0.4);
  outline-offset: 3px;
}

.official-link:active {
  transform: translateY(0);
}

.topbar {
  display: flex;
  justify-content: space-between;
  gap: 1rem;
  align-items: center;
  padding: 1rem 1.25rem;
  border-bottom: 1px solid var(--border);
  position: sticky;
  top: 0;
  background: rgba(17, 19, 24, 0.96);
  backdrop-filter: blur(10px);
  z-index: 5;
}

h1,
h2,
p {
  margin-top: 0;
}

.topbar h1 {
  margin: 0;
  font-size: 1.4rem;
}

.eyebrow {
  margin: 0 0 0.2rem;
  color: var(--accent);
  font-size: 0.72rem;
  letter-spacing: 0.12em;
}

.header-actions,
.button-row {
  display: flex;
  gap: 0.5rem;
  flex-wrap: wrap;
}

.notice {
  padding: 0.65rem 1.25rem;
  background: #322a13;
  color: #ffe291;
  border-bottom: 1px solid #5b4b1d;
}

main {
  display: grid;
  grid-template-columns: 240px minmax(0, 1fr) 330px;
  gap: 1rem;
  padding: 1rem;
  align-items: start;
}

.panel {
  padding: 1rem;
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 14px;
}

.filters,
.deck-panel {
  position: sticky;
  top: 88px;
  max-height: calc(100vh - 105px);
  overflow: auto;
}

label {
  display: block;
  margin: 0.8rem 0;
  color: var(--muted);
}

input,
select {
  width: 100%;
  margin-top: 0.3rem;
  padding: 0.62rem;
  background: #12151b;
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 8px;
}

.check {
  display: flex;
  align-items: center;
  gap: 0.5rem;
}

.check input {
  width: auto;
  margin: 0;
}

.section-head {
  display: flex;
  justify-content: space-between;
  align-items: end;
}

.section-head p {
  color: var(--muted);
}

.card-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(180px, 1fr));
  gap: 0.75rem;
}

.card-tile {
  display: flex;
  flex-direction: column;
  overflow: hidden;
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 12px;
}

.card-art {
  display: grid;
  place-items: center;
  aspect-ratio: 5 / 7;
  padding: 1rem;
  background: linear-gradient(145deg, #303745, #171a20);
  color: var(--muted);
  text-align: center;
}

.card-art img {
  width: 100%;
  height: 100%;
  object-fit: contain;
}

.card-info {
  display: flex;
  flex: 1;
  flex-direction: column;
  gap: 0.35rem;
  padding: 0.75rem;
}

.card-title {
  font-weight: 800;
}

.meta {
  color: var(--muted);
  font-size: 0.82rem;
}

.tags {
  display: flex;
  flex-wrap: wrap;
  gap: 0.3rem;
}

.tag {
  padding: 0.15rem 0.4rem;
  background: var(--panel2);
  border-radius: 999px;
  font-size: 0.72rem;
}

.card-actions {
  display: flex;
  gap: 0.4rem;
  margin-top: auto;
}

.card-actions button {
  flex: 1;
  padding: 0.5rem;
}

.card-actions .details {
  background: var(--panel2);
  color: var(--text);
}

.deck-summary {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 0.5rem;
}

.deck-summary div {
  padding: 0.7rem;
  background: var(--panel2);
  border-radius: 9px;
}

.deck-summary span {
  display: block;
  color: var(--muted);
  font-size: 0.8rem;
}

.deck-summary strong {
  font-size: 1.1rem;
}

.validation {
  margin: 0.75rem 0;
  padding: 0.65rem;
  background: #2a1c1d;
  color: #ffb2b2;
  border-radius: 8px;
}

.validation.ok {
  background: #163126;
  color: #a8f0cd;
}

.deck-list {
  display: flex;
  flex-direction: column;
  gap: 0.35rem;
}

.deck-row {
  display: grid;
  grid-template-columns: 1fr auto auto;
  gap: 0.35rem;
  align-items: center;
  padding: 0.45rem;
  background: var(--panel2);
  border-radius: 8px;
}

.deck-row small {
  display: block;
  color: var(--muted);
}

.standard-soul-row {
  border: 1px solid rgba(244, 180, 0, 0.35);
}

.standard-soul-row .official-link {
  margin-top: 0;
}

.qty {
  min-width: 2rem;
  text-align: center;
}

.mini {
  padding: 0.3rem 0.5rem;
  border-radius: 6px;
}

dialog {
  width: min(650px, 92vw);
  padding: 1.2rem;
  background: var(--panel);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 14px;
}

dialog::backdrop {
  background: rgba(0, 0, 0, 0.72);
}

.dialog-close {
  float: right;
  padding: 0.1rem 0.5rem;
  background: transparent;
  color: var(--text);
  font-size: 1.5rem;
}

footer {
  padding: 1rem 1.25rem;
  color: var(--muted);
  border-top: 1px solid var(--border);
  font-size: 0.82rem;
}

@media (max-width: 1050px) {
  main {
    grid-template-columns: 220px 1fr;
  }

  .deck-panel {
    grid-column: 1 / -1;
    position: static;
    max-height: none;
  }

  .deck-panel .deck-list {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(260px, 1fr));
  }
}

@media (max-width: 700px) {
  .topbar {
    position: static;
    align-items: flex-start;
    flex-direction: column;
  }

  .notice {
    font-size: 0.85rem;
  }

  main {
    grid-template-columns: 1fr;
  }

  .filters {
    position: static;
    max-height: none;
  }

  .card-grid {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }

  .card-title {
    font-size: 0.9rem;
  }

  .card-actions {
    flex-direction: column;
  }

  .official-link {
    width: 100%;
    justify-content: center;
  }
}

/* Pre-built deck loader, kept beside Import deck in the header. */
.prebuilt-deck-controls {
  display: inline-flex;
  align-items: center;
  gap: 0.5rem;
  min-width: 0;
}

.prebuilt-deck-controls select {
  max-width: 260px;
  min-height: 42px;
  padding: 0.55rem 2.1rem 0.55rem 0.7rem;
  border: 1px solid var(--border);
  border-radius: 9px;
  background: var(--panel2);
  color: var(--text);
}

.sr-only {
  position: absolute !important;
  width: 1px !important;
  height: 1px !important;
  padding: 0 !important;
  margin: -1px !important;
  overflow: hidden !important;
  clip: rect(0, 0, 0, 0) !important;
  white-space: nowrap !important;
  border: 0 !important;
}

@media (max-width: 900px) {
  .prebuilt-deck-controls {
    width: 100%;
  }

  .prebuilt-deck-controls select {
    flex: 1;
    max-width: none;
  }
}

/* Visual deck browser */
.deck-visual-tools {
  margin: 0.8rem 0 1rem;
  padding: 0.75rem;
  background: rgba(255,255,255,0.025);
  border: 1px solid var(--border);
  border-radius: 10px;
}
.deck-search-label { margin: 0; }
.deck-tool-row { display: flex; align-items: end; gap: 0.6rem; margin-top: 0.55rem; }
.deck-tool-row label { flex: 1; margin: 0; }
.deck-view-toggle { display: flex; gap: 0.35rem; padding-bottom: 1px; }
.deck-view-toggle .active { box-shadow: 0 0 0 2px rgba(255,255,255,0.16) inset; }
.deck-color-summary { display: flex; flex-wrap: wrap; gap: 0.35rem; margin-top: 0.7rem; }
.color-total { padding: 0.26rem 0.5rem; background: var(--panel2); border: 1px solid var(--border); border-radius: 999px; font-size: 0.75rem; }

.deck-list.visual-grid { display: block; }
.deck-color-group { margin-bottom: 0.85rem; }
.deck-color-heading { display: flex; justify-content: space-between; align-items: center; margin: 0 0 0.45rem; padding: 0.4rem 0.55rem; background: var(--panel2); border-radius: 8px; font-size: 0.9rem; }
.deck-color-heading small { color: var(--muted); font-weight: 500; }
.deck-card-group { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 0.5rem; }
.deck-list.visual-list { display: flex; flex-direction: column; gap: 0.45rem; }
.deck-list.visual-list .deck-card-group { display: flex; flex-direction: column; }
.deck-list.visual-list .deck-card { grid-template-columns: 58px minmax(0,1fr) auto; }
.deck-list.visual-list .deck-card-image { aspect-ratio: 5 / 7; }

.deck-card { display: grid; grid-template-rows: auto auto auto; min-width: 0; overflow: hidden; background: var(--panel2); border: 1px solid var(--border); border-top-width: 3px; border-radius: 10px; cursor: pointer; transition: transform 0.12s ease, border-color 0.12s ease; }
.deck-card:hover, .deck-card:focus-visible { transform: translateY(-2px); border-color: #77839a; outline: none; }
.deck-card-image { position: relative; aspect-ratio: 5 / 7; overflow: hidden; background: #11151c; }
.deck-card-image img { width: 100%; height: 100%; object-fit: cover; display: block; }
.deck-card-placeholder { display: grid; place-items: center; width: 100%; height: 100%; color: var(--muted); font-size: 0.75rem; }
.deck-qty-badge { position: absolute; top: 0.35rem; right: 0.35rem; min-width: 2rem; padding: 0.18rem 0.38rem; background: rgba(0,0,0,0.82); color: #fff; border: 1px solid rgba(255,255,255,0.35); border-radius: 999px; text-align: center; font-weight: 800; font-size: 0.78rem; }
.deck-card-info { min-width: 0; padding: 0.48rem 0.5rem 0.35rem; }
.deck-card-info strong { display: -webkit-box; overflow: hidden; -webkit-line-clamp: 2; -webkit-box-orient: vertical; font-size: 0.78rem; line-height: 1.2; }
.deck-card-info small, .deck-card-meta { display: block; overflow: hidden; color: var(--muted); font-size: 0.67rem; white-space: nowrap; text-overflow: ellipsis; }
.deck-card-actions { display: grid; grid-template-columns: auto 1fr auto; align-items: center; gap: 0.25rem; padding: 0.35rem 0.45rem 0.45rem; }

.color-border-red { border-top-color: #d9534f; }
.color-border-blue { border-top-color: #4a90e2; }
.color-border-green { border-top-color: #55a868; }
.color-border-purple { border-top-color: #8e64c5; }
.color-border-yellow { border-top-color: #e0b83f; }
.color-border-colorless { border-top-color: #a7adb8; }
.color-red { color: #ff8a86; }
.color-blue { color: #82baff; }
.color-green { color: #8ed39d; }
.color-purple { color: #c0a0ec; }
.color-yellow { color: #f0d16e; }
.color-colorless { color: #d2d5dc; }

@media (max-width: 1050px) {
  .deck-panel .deck-list.visual-grid { display: block; }
  .deck-card-group { grid-template-columns: repeat(auto-fill, minmax(145px, 1fr)); }
}
@media (max-width: 700px) {
  .deck-tool-row { align-items: stretch; flex-direction: column; }
  .deck-view-toggle button { flex: 1; }
  .deck-card-group { grid-template-columns: repeat(2, minmax(0, 1fr)); }
}

/* Mobile usability pass: compact layout, touch-friendly controls, and smoother deck scrolling. */
html {
  overflow-x: hidden;
  scroll-behavior: smooth;
}

body {
  min-width: 0;
  overflow-x: hidden;
}

.filters,
.deck-panel {
  overscroll-behavior: contain;
  scrollbar-gutter: stable;
  -webkit-overflow-scrolling: touch;
}

@media (max-width: 700px) {
  body {
    font-size: 14px;
  }

  .topbar {
    gap: 0.65rem;
    padding: 0.7rem;
  }

  .topbar h1 {
    font-size: 1.15rem;
    line-height: 1.2;
  }

  .eyebrow {
    font-size: 0.61rem;
    line-height: 1.3;
  }

  .header-actions {
    display: grid;
    grid-template-columns: repeat(2, minmax(0, 1fr));
    width: 100%;
    gap: 0.4rem;
  }

  .header-actions > button {
    width: 100%;
    min-width: 0;
    min-height: 44px;
    padding: 0.55rem 0.45rem;
    font-size: 0.82rem;
  }

  .prebuilt-deck-controls {
    grid-column: 1 / -1;
    display: grid;
    grid-template-columns: minmax(0, 1fr) auto;
    gap: 0.4rem;
  }

  .prebuilt-deck-controls select,
  .prebuilt-deck-controls button {
    min-width: 0;
    min-height: 44px;
    margin: 0;
  }

  .notice {
    padding: 0.55rem 0.7rem;
    line-height: 1.35;
  }

  main {
    gap: 0.65rem;
    padding: 0.65rem;
  }

  .panel {
    padding: 0.7rem;
    border-radius: 11px;
  }

  /* Put the loaded deck near the top instead of below the full card catalog. */
  .deck-panel {
    order: -1;
    width: 100%;
    max-width: 100%;
    max-height: min(72svh, 690px);
    overflow-y: auto;
    overflow-x: hidden;
    padding-bottom: 1rem;
    scroll-padding-top: 0.65rem;
  }

  .filters {
    order: 0;
  }

  .catalog {
    order: 1;
    min-width: 0;
  }

  .deck-summary {
    gap: 0.4rem;
  }

  .deck-summary div {
    padding: 0.55rem;
  }

  .deck-summary strong {
    font-size: 1rem;
  }

  .validation {
    margin: 0.55rem 0;
    padding: 0.5rem;
    font-size: 0.78rem;
  }

  .deck-visual-tools {
    position: sticky;
    top: -1px;
    z-index: 3;
    margin: 0.55rem 0 0.75rem;
    padding: 0.55rem;
    background: rgba(27, 31, 39, 0.98);
    box-shadow: 0 8px 14px rgba(0, 0, 0, 0.25);
    backdrop-filter: blur(8px);
  }

  .deck-search-label input,
  .deck-tool-row select {
    min-height: 44px;
  }

  .deck-tool-row {
    gap: 0.45rem;
    margin-top: 0.4rem;
  }

  .deck-view-toggle {
    width: 100%;
  }

  .deck-view-toggle button,
  .deck-card-actions button,
  .mini {
    min-height: 40px;
  }

  .deck-color-summary {
    flex-wrap: nowrap;
    gap: 0.3rem;
    margin: 0.55rem -0.1rem 0;
    padding: 0 0.1rem 0.2rem;
    overflow-x: auto;
    overscroll-behavior-x: contain;
    scrollbar-width: thin;
  }

  .color-total {
    flex: 0 0 auto;
    white-space: nowrap;
  }

  .deck-color-group {
    margin-bottom: 0.7rem;
  }

  .deck-color-heading {
    position: sticky;
    top: 154px;
    z-index: 2;
    margin-bottom: 0.35rem;
    padding: 0.35rem 0.45rem;
    background: rgba(36, 42, 52, 0.97);
    backdrop-filter: blur(6px);
  }

  .deck-card-group {
    grid-template-columns: repeat(3, minmax(0, 1fr));
    gap: 0.38rem;
  }

  .deck-card {
    border-radius: 8px;
  }

  .deck-card:hover,
  .deck-card:focus-visible {
    transform: none;
  }

  .deck-card-image {
    aspect-ratio: 5 / 7;
  }

  .deck-card-image img {
    object-fit: contain;
  }

  .deck-qty-badge {
    top: 0.22rem;
    right: 0.22rem;
    min-width: 1.75rem;
    padding: 0.12rem 0.28rem;
    font-size: 0.7rem;
  }

  .deck-card-info {
    padding: 0.38rem 0.38rem 0.25rem;
  }

  .deck-card-info strong {
    font-size: 0.69rem;
  }

  .deck-card-info small,
  .deck-card-meta {
    font-size: 0.61rem;
  }

  .deck-card-actions {
    gap: 0.18rem;
    padding: 0.25rem 0.3rem 0.35rem;
  }

  .deck-card-actions .qty {
    min-width: 1.3rem;
    font-size: 0.75rem;
  }

  .deck-list.visual-list .deck-card {
    grid-template-columns: 48px minmax(0, 1fr) auto;
    grid-template-rows: auto;
  }

  .deck-list.visual-list .deck-card-actions {
    grid-template-columns: 38px 24px 38px;
  }

  .card-grid {
    gap: 0.5rem;
  }

  .card-art {
    padding: 0.35rem;
  }

  .card-info {
    gap: 0.25rem;
    padding: 0.55rem;
  }

  .card-actions button {
    min-height: 42px;
  }

  input,
  select,
  button {
    touch-action: manipulation;
  }

  dialog {
    width: calc(100vw - 1rem);
    max-height: calc(100svh - 1rem);
    padding: 0.8rem;
    overflow-y: auto;
    border-radius: 10px;
  }

  footer {
    padding: 0.75rem;
  }
}

@media (max-width: 420px) {
  .deck-card-group {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }

  .card-grid {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }

  .section-head {
    align-items: flex-start;
  }
}

@media (max-width: 340px) {
  .header-actions {
    grid-template-columns: 1fr;
  }

  .prebuilt-deck-controls {
    grid-column: 1;
    grid-template-columns: 1fr;
  }

  .deck-card-group,
  .card-grid {
    grid-template-columns: 1fr;
  }
}

/* Mobile scrolling fix: use the page as the only vertical scroll container. */
@media (max-width: 700px) {
  .filters,
  .deck-panel {
    max-height: none;
    overflow: visible;
    overscroll-behavior: auto;
    scrollbar-gutter: auto;
    -webkit-overflow-scrolling: auto;
  }

  .deck-panel {
    padding-bottom: 0.7rem;
    scroll-padding-top: 0;
  }

  /* Keep deck controls in normal document flow so they do not cover cards. */
  .deck-visual-tools,
  .deck-color-heading {
    position: static;
    top: auto;
    z-index: auto;
    backdrop-filter: none;
  }

  .deck-visual-tools {
    box-shadow: none;
  }

  /* Ensure the catalog and its card grid can grow naturally with page scrolling. */
  .catalog,
  .card-grid,
  .deck-list {
    min-height: 0;
    overflow: visible;
  }
}


/* Mobile quick navigation: jump from the card catalog to the deck. */
.mobile-deck-jump {
  display: none;
}

@media (max-width: 700px) {
  body {
    padding-bottom: calc(4.5rem + env(safe-area-inset-bottom));
  }

  .mobile-deck-jump {
    position: fixed;
    right: 0.85rem;
    bottom: calc(0.85rem + env(safe-area-inset-bottom));
    z-index: 30;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    min-height: 46px;
    padding: 0.7rem 1rem;
    border: 1px solid rgba(255, 255, 255, 0.18);
    border-radius: 999px;
    background: #f2b84b;
    color: #171a20;
    box-shadow: 0 8px 24px rgba(0, 0, 0, 0.35);
    font-weight: 800;
    line-height: 1;
    text-decoration: none;
    touch-action: manipulation;
  }

  .mobile-deck-jump:active {
    transform: translateY(1px);
  }

  #deckPanel {
    scroll-margin-top: 0.75rem;
  }

  #deckPanel:focus {
    outline: none;
  }
}
