/* Subtype Filter */
.subtype-search-container {
  position: relative;
  width: 100%;
}

.subtype-search-bar {
  display: flex;
  align-items: center;
  gap: 10px;
  background: #111;
  border: 1px solid #333;
  border-radius: 4px;
  padding: 8px 12px;
  min-height: 44px;
  cursor: text;
  transition: border-color 0.2s;
}

.subtype-search-bar:hover {
  border-color: #555;
}

.subtype-search-bar:focus-within {
  border-color: #fff;
}

.search-icon {
  width: 18px;
  height: 18px;
  color: #666;
  flex-shrink: 0;
}

.selected-subtypes {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  flex: 1;
}

.search-placeholder {
  color: #666;
  font-size: 0.95rem;
}

.subtype-pill {
  background: #2a2a2a;
  color: #fff;
  padding: 4px 10px;
  border-radius: 100px;
  font-size: 0.85rem;
  display: flex;
  align-items: center;
  gap: 6px;
  border: 1px solid #444;
}

.subtype-pill button {
  background: transparent;
  border: none;
  color: #888;
  cursor: pointer;
  font-size: 16px;
  padding: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  line-height: 1;
}

.subtype-pill button:hover {
  color: #fff;
}

.subtype-dropdown {
  position: absolute;
  top: calc(100% + 5px);
  left: 0;
  right: 0;
  background: #111;
  border: 1px solid #333;
  border-radius: 4px;
  z-index: 100;
  box-shadow: 0 10px 30px rgba(0,0,0,0.5);
  display: flex;
  flex-direction: column;
  overflow: hidden;
  max-height: 450px;
}

.dropdown-search-header {
  position: sticky;
  top: 0;
  z-index: 20;
  background: #111;
  border-bottom: 1px solid #333;
}

.dropdown-search-input {
  width: 100%;
  background: #1a1a1a;
  border: none;
  color: #fff;
  padding: 14px 16px;
  font-size: 1rem;
}

.subtype-results-count {
  padding: 8px 16px;
  font-size: 0.75rem;
  color: #666;
  background: #0a0a0a;
  border-bottom: 1px solid #222;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.05em;
}

.subtype-search-bar.disabled {
  opacity: 0.5;
  cursor: default;
  background: #0a0a0a;
  border-color: #222;
  pointer-events: none;
}

.subtype-search-bar.disabled:hover {
  border-color: #222;
}

.subtype-group-header {
  padding: 0 16px;
  font-size: 0.75rem;
  font-weight: 700;
  color: #666;
  text-transform: uppercase;
  letter-spacing: 0.1em;
  background: #0a0a0a;
  border-top: 1px solid #222;
  z-index: 10;
  height: 44px; /* Matches ITEM_HEIGHT */
  display: flex;
  align-items: center;
  box-sizing: border-box;
  gap: 8px;
}

.subtype-common-tag {
  font-size: 12px;
  font-weight: 400;
  text-transform: none;
  letter-spacing: normal;
  background: #222;
  color: #888;
  padding: 2px 6px;
  border-radius: 3px;
  line-height: 1;
}

.subtype-group-header.floating-header {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  z-index: 30;
  border-top: none;
  background: #0a0a0a;
  border-bottom: 1px solid #222;
  pointer-events: none;
  width: 100%;
}

.subtype-group-header:first-of-type {
  border-top: none;
}

/* Virtual List */
.virtual-list-viewport {
  height: 300px;
  overflow-y: auto;
  position: relative;
  background: #111;
}

.virtual-list-spacer {
  width: 100%;
  pointer-events: none;
}

.virtual-list-content {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
}

.subtype-item {
  padding: 12px 16px;
  cursor: pointer;
  transition: background 0.2s;
  display: flex;
  align-items: center;
  justify-content: space-between;
  height: 44px; /* Fixed height for virtualization */
}

.subtype-item:hover {
  background: #222;
}

.subtype-item.selected {
  color: #fff;
  font-weight: 600;
  background: #1a1a1a;
}

.subtype-item .check-icon {
  color: #fff;
  display: none;
}

.subtype-item.selected .check-icon {
  display: block;
}

.empty-state-msg {
  padding: 40px 20px;
  text-align: center;
  color: #888;
  font-size: 0.9rem;
}

/* Subtype Loading State */
.subtype-loading {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 12px;
  padding: 60px 20px;
  color: #888;
  font-size: 0.9rem;
}

/* Mobile Drawer */
@media (max-width: 768px) {
  .subtype-dropdown.mobile-active {
    position: fixed;
    top: 0;
    bottom: 0;
    left: 0;
    right: 0;
    height: 100vh;
    width: 100vw;
    border-radius: 0;
    z-index: 2000;
    background: #000;
    max-height: none;
  }
  
  .dropdown-search-header {
    background: #111;
    padding-top: env(safe-area-inset-top);
  }

  .virtual-list-viewport {
    flex: 1;
    height: auto;
  }
  
  .subtype-dropdown-overlay {
    position: fixed;
    inset: 0;
    background: rgba(0,0,0,0.8);
    z-index: 1999;
  }
  
  .mobile-close-btn {
    display: block;
    width: 100%;
    padding: 16px;
    background: #222;
    border: none;
    border-top: 1px solid #333;
    color: #fff;
    font-weight: 700;
    text-transform: uppercase;
    letter-spacing: 0.1em;
    text-align: center;
    cursor: pointer;
    padding-bottom: calc(16px + env(safe-area-inset-bottom));
  }
}

@media (min-width: 769px) {
  .mobile-close-btn {
    display: none;
  }
}

/* Rest of existing styles... */
/* Reset and Base Styles */
* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

body {
  margin: 0;
  background: #000;
  color: #fff;
  font-family: 'Montserrat', sans-serif;
  line-height: 1.6;
  overflow-x: hidden;
}

/* Force Montserrat for all elements except headlines */
button, input, select, textarea, label {
  font-family: 'Montserrat', sans-serif;
}

h1, h2, h3 {
  font-family: 'Cinzel', serif;
  font-weight: 400;
}

#app {
  min-height: 100vh;
  display: flex;
  flex-direction: column;
}

/* View Container */
.view-container {
  max-width: 1200px;
  margin: 0 auto;
  padding: 20px;
  width: 100%;
}

/* Header */
.view-header {
  display: grid;
  grid-template-columns: 1fr auto 1fr;
  align-items: center;
  gap: 12px;
  padding: 10px 0;
  margin-bottom: 25px;
  min-height: 56px;
  position: relative;
}

.header-left {
  display: flex;
  align-items: center;
  justify-content: flex-start;
}

.header-right {
  display: flex;
  align-items: center;
  justify-content: flex-end;
}

.header-center {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  text-align: center;
  min-width: 0;
  max-width: 100%;
}

.view-header h1 {
  font-size: clamp(1.2rem, 5vw, 1.8rem);
  font-weight: 500;
  margin: 0;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  max-width: 60vw;
}

.stack-title {
  font-size: clamp(1rem, 4vw, 1.3rem);
  margin: 0;
  max-width: 60vw;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  font-family: 'Cinzel', serif;
}

.view-header .subtitle {
  color: #888;
  font-size: 0.85rem;
}

/* Compact Back Button */
.btn-back {
  background: transparent;
  border: 1px solid #333;
  color: #fff;
  padding: 8px 12px;
  border-radius: 4px;
  cursor: pointer;
  font-size: 0.9rem;
  transition: all 0.2s;
  display: flex;
  align-items: center;
  gap: 4px;
  white-space: nowrap;
}

.icon-back {
  width: 18px;
  height: 18px;
}

.btn-back:hover {
  background: #222;
  border-color: #555;
}

/* Subtle Undo Button */
.btn-undo-subtle {
  background: transparent;
  border: none;
  color: #666;
  font-size: 0.7rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  display: flex;
  align-items: center;
  gap: 4px;
  cursor: pointer;
  padding: 4px 8px;
  border-radius: 4px;
  transition: all 0.2s;
  font-family: 'Montserrat', sans-serif;
}

.btn-undo-subtle:hover:not(:disabled) {
  color: #fff;
  background: rgba(255, 255, 255, 0.05);
}

.btn-undo-subtle:disabled {
  opacity: 0;
  pointer-events: none;
}

.btn-undo-subtle svg {
  opacity: 0.7;
}

/* Subtle Icon Button */
.btn-icon-subtle {
  background: transparent;
  border: 1px solid #333;
  color: #666;
  width: 32px;
  height: 32px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  font-size: 14px;
  transition: all 0.2s;
}

.btn-icon-subtle:hover {
  border-color: #555;
  color: #fff;
}

.btn-back:hover {
  background: #222;
  border-color: #555;
}

.btn-back:focus-visible {
  outline: 2px solid #fff;
  outline-offset: 2px;
}

.btn-primary {
  background: #ddd;
  color: #000;
  border: none;
  padding: 11px 24px;
  border-radius: 4px;
  cursor: pointer;
  font-size: 1rem;
  font-weight: 600;
  transition: all 0.2s;
}

.btn-primary:hover:not([data-is-valid="false"]) {
  background: #fff;
}

.btn-primary:active:not([data-is-valid="false"]) {
  background: #bbb;
}

.btn-primary[data-is-valid="false"] {
  cursor: pointer;
}

.btn-primary:focus-visible {
  outline: 2px solid #fff;
  outline-offset: 2px;
}

.btn-secondary {
  background: #212121;
  color: #fff;
  border: none;
  padding: 11px 24px;
  border-radius: 4px;
  cursor: pointer;
  font-size: 1rem;
  font-weight: 600;
  transition: all 0.2s;
}

.btn-secondary:hover {
  background: #2a2a2a;
  color: #fff;
}

.btn-secondary:active {
  transform: translateY(0);
}

.btn-secondary:focus-visible {
  outline: 2px solid #fff;
  outline-offset: 2px;
}

.btn-success {
  background: #2e7d32 !important;
  color: #fff !important;
}

.btn-icon {
  background: transparent;
  border: 1px solid #333;
  color: #fff;
  padding: 9px 14px;
  border-radius: 4px;
  cursor: pointer;
  font-size: 15px;
  transition: all 0.2s;
}

.btn-icon:hover:not(:disabled) {
  background: #222;
  border-color: #555;
}

.btn-icon:focus-visible {
  outline: 2px solid #fff;
  outline-offset: 2px;
}

button:disabled {
  opacity: 0.3;
  cursor: default;
  pointer-events: none;
}

.view-header .btn-icon {
  position: absolute;
  right: 0;
  top: 0;
  font-size: 15px;
}

.stacks-list-header {
  display: flex;
  justify-content: flex-end;
  margin-bottom: 15px;
}

.sort-control {
  display: flex;
  align-items: center;
  gap: 10px;
}

.sort-control label {
  font-size: 0.85rem;
  color: #888;
  font-weight: 600;
}

.form-select-sm {
  background: #111;
  border: 1px solid #333;
  color: #fff;
  padding: 6px 30px 6px 12px;
  border-radius: 4px;
  font-size: 0.85rem;
  appearance: none;
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='24' height='24' viewBox='0 0 24 24' fill='none' stroke='%23666' stroke-width='2' stroke-linecap='round' stroke-linejoin='round'%3E%3Cpolyline points='6 9 12 15 18 9'%3E%3C/polyline%3E%3C/svg%3E");
  background-repeat: no-repeat;
  background-position: right 8px center;
  background-size: 16px;
  cursor: pointer;
  transition: all 0.2s;
}

.form-select-sm:hover {
  border-color: #555;
  background-color: #1a1a1a;
}

.form-select-sm:focus {
  outline: none;
  border-color: #fff;
}

/* Stacks List */
.stacks-list {
  display: flex;
  flex-direction: column;
  gap: 20px;
  margin-bottom: 30px;
}

#stacks-container {
  display: flex;
  flex-direction: column;
  gap: 20px;
}

.stack-card {
  background: #111;
  border: 1px solid #333;
  border-radius: 8px;
  padding: 20px;
  cursor: pointer;
  transition: all 0.2s;
  position: relative;
}

.stack-card:hover {
  border-color: #555;
}

.stack-card:focus-visible {
  outline: 2px solid #fff;
  outline-offset: 2px;
}

.stack-card.new-stack {
  border-style: dashed;
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: 150px;
  background: transparent;
}

.stack-card.new-stack .stack-card-content {
  text-align: center;
  display: flex;
  flex-direction: column;
  gap: 0px;
}

.plus-icon {
  font-size: 3rem;
  margin-bottom: 0px;
}

.stack-card-content h3 {
  margin-bottom: 5px;
  font-size: 1.3rem;
}

.stack-filters {
  color: #666;
  font-size: 0.85rem;
  margin-bottom: 12px;
  font-weight: 400;
}

.stack-meta {
  color: #888;
  font-size: 0.9rem;
  margin-bottom: 10px;
}

.stack-categories {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  margin-top: 15px;
}

.category-tag {
  background: #1a1a1a;
  border: 1px solid #333;
  color: #fff;
  padding: 4px 12px;
  border-radius: 100px;
  font-size: 0.75rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.1em;
  display: flex;
  align-items: center;
  gap: 8px;
}

.tag-count {
  color: #666;
  font-size: 0.7rem;
  font-weight: 400;
}

.stack-source-tag {
  background: #1a1a1a;
  border: 1px solid #333;
  color: #666;
  padding: 2px 8px;
  border-radius: 4px;
  font-size: 0.75rem;
  font-weight: 400;
  display: inline-block;
  text-transform: none;
  letter-spacing: normal;
  margin-left: 6px;
}

.stack-actions {
  display: flex;
  gap: 10px;
  margin-top: 15px;
}

.stack-actions .btn-primary,
.stack-actions .btn-secondary {
  flex: 1;
}

.btn-delete {
  position: absolute;
  top: 10px;
  right: 10px;
  padding: 4px 8px;
  font-size: 12px;
  opacity: 0.4;
  transition: all 0.2s;
  z-index: 2;
}

.btn-delete:hover {
  opacity: 1;
  background: #c00;
  border-color: #f00;
}

/* Forms */
.form-container {
  max-width: 600px;
  margin: 0 auto;
}

.form-section {
  margin-bottom: 25px;
}

.form-label {
  display: block;
  margin-bottom: 8px;
  font-weight: 600;
  font-size: 0.95rem;
}

.form-label-primary {
  font-size: 18px;
}

.form-sublabel {
  display: block;
  margin-top: -4px;
  margin-bottom: 12px;
  padding-top: 25px;
  padding-bottom: 50px;
  font-size: 0.85rem;
  color: #888;
  font-weight: 400;
}

.form-input,
.form-select {
  width: 100%;
  background: #111;
  border: 1px solid #333;
  color: #fff;
  padding: 11px;
  border-radius: 4px;
  font-size: 0.9rem;
}

.form-select {
  appearance: none;
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='24' height='24' viewBox='0 0 24 24' fill='none' stroke='%23666' stroke-width='2' stroke-linecap='round' stroke-linejoin='round'%3E%3Cpolyline points='6 9 12 15 18 9'%3E%3C/polyline%3E%3C/svg%3E");
  background-repeat: no-repeat;
  background-position: right 10px center;
  background-size: 20px;
  cursor: pointer;
  padding-right: 40px;
}

.form-select:hover {
  background-color: #1a1a1a;
  border-color: #555;
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='24' height='24' viewBox='0 0 24 24' fill='none' stroke='%23fff' stroke-width='2' stroke-linecap='round' stroke-linejoin='round'%3E%3Cpolyline points='6 9 12 15 18 9'%3E%3C/polyline%3E%3C/svg%3E");
}

.form-input:focus,
.form-select:focus {
  outline: none;
  border-color: #fff;
  box-shadow: 0 0 0 2px rgba(255, 255, 255, 0.1);
}

.form-input:focus-visible,
.form-select:focus-visible {
  outline: 2px solid #fff;
  outline-offset: 2px;
}

.form-input.highlight-required {
  animation: borderPulse 0.6s ease-in-out !important;
}

.category-item.highlight-required {
  animation: borderPulse 0.6s ease-in-out !important;
}

@keyframes borderPulse {
  0% {
    border-color: #333;
  }
  50% {
    border-color: #fff;
  }
  100% {
    border-color: #333;
  }
}

.checkbox-group {
  display: grid;
  /* minmax(0, …) lets columns shrink below their longest word instead of overflowing */
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 12px;
}

.color-card, .type-card {
  /* Reset native button styles */
  font: inherit;
  color: inherit;
  text-align: center;
  appearance: none;
  -webkit-tap-highlight-color: transparent;
  min-width: 0;
  background: #111;
  border: 1px solid #333;
  border-radius: 4px;
  padding: 10px;
  height: 44px;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 10px;
  cursor: pointer;
  transition: all 0.2s;
  user-select: none;
}

.color-card:hover, .type-card:hover {
  border-color: #555;
  background: #1a1a1a;
}

.color-card.active, .type-card.active {
  border-color: #333;
  opacity: 1;
}

.color-card:not(.active), .type-card:not(.active) {
  opacity: 0.3;
}

.color-card:focus-visible, .type-card:focus-visible {
  outline: 2px solid #fff;
  outline-offset: 2px;
}

/* Keep focused-but-off tiles readable without looking selected */
.color-card:not(.active):focus-visible, .type-card:not(.active):focus-visible {
  opacity: 0.6;
}

.color-card .mana-symbol {
  width: 24px;
  height: 24px;
}

.color-card span, .type-card span {
  font-weight: 400;
  font-size: 0.9rem;
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

/* Phones: tighter tiles so "Planeswalkers" and "Enchantments" fit */
@media (max-width: 520px) {
  .checkbox-group {
    gap: 8px;
  }

  .color-card, .type-card {
    padding: 8px 4px;
    gap: 6px;
  }

  .color-card span, .type-card span {
    font-size: 0.78rem;
  }

  .color-card .mana-symbol {
    width: 20px;
    height: 20px;
  }
}

/* Very small phones: 2 columns so full labels fit */
@media (max-width: 360px) {
  .checkbox-group {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }
}

.checkbox-group label {
  display: flex;
  align-items: center;
  gap: 8px;
  cursor: pointer;
}

.mana-symbol {
  width: 20px;
  height: 20px;
  display: inline-block;
  vertical-align: middle;
}

.mini-mana-icon {
  width: 16px;
  height: 16px;
  vertical-align: middle;
  margin: 0 1px;
}

.mana-group {
  display: inline-flex;
  gap: 2px;
  align-items: center;
  margin-left: 5px;
}

.identity-label {
  font-weight: 400;
  font-size: 0.8rem;
  color: #888;
  margin-right: 4px;
  text-transform: capitalize;
}

.checkbox-group input[type="checkbox"] {
  width: 18px;
  height: 18px;
  cursor: pointer;
}

/* Category Configuration */
.category-config {
  display: flex;
  flex-direction: column;
  gap: 15px;
}

.category-item-card {
  background: #111;
  border: 1px solid #333;
  border-radius: 4px;
  padding: 10px;
  height: 44px;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: all 0.2s;
  position: relative;
}

.category-item-card.active {
  background: #111;
  border: 1px solid #333;
}

.category-add-btn {
  position: absolute;
  top: 50%;
  transform: translateY(-50%);
  right: 10px;
  background: transparent;
  border: none;
  color: #666;
  width: 24px;
  height: 24px;
  border-radius: 4px;
  cursor: pointer;
  font-size: 22px;
  line-height: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: all 0.2s;
  padding: 0;
  pointer-events: none;
}

.category-remove-btn {
  position: absolute;
  top: 50%;
  transform: translateY(-50%);
  right: 10px;
  background: transparent;
  border: none;
  color: #666;
  width: 24px;
  height: 24px;
  border-radius: 4px;
  cursor: pointer;
  font-size: 22px;
  line-height: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: all 0.2s;
  padding: 0;
}

.category-remove-btn:hover {
  background: #222;
  color: #fff;
}

.category-item-card.add-category {
  background: transparent;
  border: 1px dashed #333;
  cursor: pointer;
}

.category-item-card.add-category:hover {
  background: #0a0a0a;
  border-color: #555;
}

.category-item-card .form-input {
  width: 100%;
  height: 100%;
  color: #FFFFFF;
  background: transparent;
  border: none;
  padding: 0 10px;
  text-align: left;
  font-size: 0.9rem;
  line-height: normal;
}

.category-item-card.has-remove-btn .form-input {
  padding-right: 40px;
}

.category-item-card .form-input:focus {
  outline: none;
  box-shadow: none;
}

.add-category-content {
  display: flex;
  flex-direction: row;
  align-items: center;
  justify-content: flex-start;
  gap: 8px;
  color: #666;
  user-select: none;
  height: 100%;
  width: 100%;
  padding-left: 10px;
}

.add-category-content span {
  font-size: 0.9rem;
  font-weight: 600;
  line-height: 1;
  display: flex;
  align-items: center;
}

.category-max-hint {
  margin-top: 10px;
  color: #666;
  font-size: 0.85rem;
  text-align: center;
  font-style: italic;
}

/* Form Actions */
.form-actions {
  display: flex;
  gap: 15px;
  margin-top: 30px;
  padding-top: 25px;
}

.form-actions button {
  flex: 1;
}

.preview-result {
  margin-top: 15px;
  padding: 15px;
  background: #111;
  border: 1px solid #333;
  border-radius: 4px;
  text-align: center;
  display: none;
}

.preview-result.show {
  display: block;
}

/* Card Sorting View */
.sort-view {
  height: 100vh;
  display: flex;
  flex-direction: column;
  padding: 0;
  overflow: hidden;
  --card-max-width: 488px;
  --card-width-mobile: 85vw;
  --card-actual-width: min(var(--card-max-width), var(--card-width-mobile));
  --card-actual-height: calc(var(--card-actual-width) * 680 / 488);
  --card-aspect-ratio: 488 / 680;
}

.sort-view .view-header {
  padding: 20px 20px 0;
  margin-bottom: 0;
}

.sort-view .progress-bar {
  margin: 10px 20px;
}

.card-display-area {
  flex: 1;
  width: 100%;
  height: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
  position: relative;
  padding: 0;
}

.category-hints {
  position: absolute;
  inset: 0;
  pointer-events: none;
}

.hint {
  position: absolute;
  background: rgba(255, 255, 255, 0.1);
  color: #fff;
  padding: 20px;
  font-size: 1.5rem;
  font-weight: 700;
  opacity: 0;
  transition: opacity 0.2s;
  display: flex;
  align-items: center;
  justify-content: center;
}

.hint.active {
  opacity: 0;
}

.hint-left {
  left: 0;
  top: 0;
  bottom: 0;
  width: calc((100% - var(--card-actual-width)) / 2);
}

.hint-right {
  right: 0;
  top: 0;
  bottom: 0;
  width: calc((100% - var(--card-actual-width)) / 2);
}

.hint-up {
  top: 0;
  left: 0;
  right: 0;
  height: calc((100% - var(--card-actual-height)) / 2);
}

.hint-down {
  bottom: 0;
  left: 0;
  right: 0;
  height: calc((100% - var(--card-actual-height)) / 2);
}

/* Category Labels */
.category-labels {
  position: absolute;
  inset: 0;
  pointer-events: none;
}

.category-label {
  position: absolute;
  font-size: 0.75rem;
  font-weight: 600;
  letter-spacing: 0.1em;
  text-transform: uppercase;
  color: #3C3C3C;
  white-space: nowrap;
  user-select: none;
  pointer-events: auto;
  cursor: pointer;
  transition: color 0.2s;
  padding: 8px 12px;
}

body.is-dragging .category-label {
  pointer-events: none !important;
}

.category-label:hover {
  color: #fff;
}

.category-label.active {
  color: #fff;
}

/* Drag feedback: the label the card is heading toward */
.category-label {
  transition: color 0.12s ease, text-shadow 0.12s ease;
}

.category-label.leaning {
  color: #9a9a9a;
}

.category-label.armed {
  color: #fff;
  text-shadow: 0 0 12px rgba(255, 255, 255, 0.6);
}

#current-card.swipe-armed {
  box-shadow: 0 0 0 2px rgba(255, 255, 255, 0.85), 0 0 24px rgba(255, 255, 255, 0.25);
}

.category-label-up {
  top: calc((100% - var(--card-actual-height)) / 4);
  left: 50%;
  transform: translate(-50%, -50%);
}

.category-label-down {
  bottom: calc((100% - var(--card-actual-height)) / 4);
  left: 50%;
  transform: translate(-50%, 50%);
}

.category-label-left {
  left: calc((100% - var(--card-actual-width)) / 4);
  top: 50%;
  transform: translate(-50%, -50%) rotate(-90deg);
  transform-origin: center;
}

.category-label-right {
  right: calc((100% - var(--card-actual-width)) / 4);
  top: 50%;
  transform: translate(50%, -50%) rotate(-90deg);
  transform-origin: center;
}

.card-container {
  position: relative;
  width: var(--card-actual-width);
  max-width: var(--card-max-width);
  max-height: 100%;
  aspect-ratio: var(--card-aspect-ratio);
  touch-action: none;
}

.card-container img {
  width: 100%;
  height: 100%;
  border-radius: 12px;
  box-shadow: 0 10px 40px rgba(0, 0, 0, 0.5);
  cursor: grab;
  user-select: none;
  transition: transform 0.3s ease-out;
  will-change: transform;
  animation: cardEnter 0.4s ease-out;
}

@keyframes cardEnter {
  from {
    opacity: 0;
    transform: scale(0.9) translateY(20px);
  }
  to {
    opacity: 1;
    transform: scale(1) translateY(0);
  }
}

.card-container img:active {
  cursor: grabbing;
}

.card-container img.exiting {
  transition: transform 0.5s ease-out, opacity 0.5s ease-out;
}

.card-container img.returning {
  transition: transform 0.5s ease-out, opacity 0.5s ease-out !important;
  animation: none !important;
}

.loading-indicator {
  text-align: center;
}

/* Card Shuffle Loader */
.card-loader {
  position: relative;
  width: 48px;
  height: 64px;
  margin: 0 auto 20px;
}

.loader-card {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background: #1a1a1a;
  border: 2px solid #333;
  border-radius: 6px;
  animation: shuffle 1.2s ease-in-out infinite;
  box-shadow: 0 4px 12px rgba(0,0,0,0.5);
  will-change: transform, opacity;
}

/* Give cards slight color variations for depth */
.loader-card:nth-child(1) { animation-delay: -0.8s; background: #2a2a2a; border-color: #555; }
.loader-card:nth-child(2) { animation-delay: -0.4s; background: #1a1a1a; border-color: #444; }
.loader-card:nth-child(3) { animation-delay: 0s;    background: #0a0a0a; border-color: #333; }

@keyframes shuffle {
  0%, 100% { transform: translate(0, 0) rotate(0deg); z-index: 3; }
  25%  { transform: translate(-25px, -5px) rotate(-10deg) scale(0.95); z-index: 1; }
  50%  { transform: translate(0, 5px) rotate(0deg) scale(0.9); z-index: 1; }
  75%  { transform: translate(25px, -5px) rotate(10deg) scale(0.95); z-index: 2; }
}

/* Mini version for inline loaders (like subtype search) */
.card-loader.mini {
  width: 18px;
  height: 24px;
  margin: 0;
  display: inline-block;
  vertical-align: middle;
}

.card-loader.mini .loader-card {
  border-width: 1px;
  border-radius: 2px;
  animation-name: shuffle-mini;
}

@keyframes shuffle-mini {
  0%, 100% { transform: translate(0, 0); z-index: 3; }
  25%  { transform: translate(-10px, 0) scale(0.9); z-index: 1; }
  50%  { transform: translate(0, 0) scale(0.8); z-index: 1; }
  75%  { transform: translate(10px, 0) scale(0.9); z-index: 2; }
}

.no-cards-message {
  text-align: center;
}

.no-cards-message p {
  font-size: 1.5rem;
  margin-bottom: 20px;
  color: #888;
}

/* Progress Bar */
.progress-bar {
  margin: 20px 0;
  background: #111;
  border-radius: 4px;
  height: 30px;
  position: relative;
  overflow: hidden;
  border: 1px solid #333;
}

.progress-fill {
  height: 100%;
  background: #fff;
  transition: width 0.3s ease-out;
  width: 0;
}

.progress-text {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  font-weight: 400;
  color: #fff;
  mix-blend-mode: difference;
}

/* Mini Progress Bar (for stack cards) */
.mini-progress-bar {
  background: #111;
  border-radius: 4px;
  height: 24px;
  position: relative;
  overflow: hidden;
  border: 1px solid #333;
  width: 100%;
}

.mini-progress-fill {
  height: 100%;
  background: #fff;
  transition: width 0.3s ease-out;
  width: 0;
}

.mini-progress-text {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  font-weight: 400;
  font-size: 0.8rem;
  color: #fff;
  mix-blend-mode: difference;
  white-space: nowrap;
}

/* Footer */
.view-footer {
  text-align: center;
  padding: 20px 0;
  margin-top: 10px;
}

.btn-settings-icon {
  background: transparent;
  border: 1px solid #333;
  color: #888;
  width: 44px;
  height: 44px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  transition: all 0.2s;
  padding: 10px;
}

.btn-settings-icon:hover {
  background: #222;
  border-color: #555;
  color: #fff;
}

.btn-settings-icon svg {
  width: 24px;
  height: 24px;
}

/* Settings View */
.settings-content {
  max-width: 600px;
  margin: 0 auto;
}

.settings-section {
  background: #111;
  border: 1px solid #333;
  border-radius: 8px;
  padding: 25px;
  margin-bottom: 25px;
  position: relative;
}

.settings-section h2 {
  margin-bottom: 20px;
  font-size: 1.5rem;
  border-bottom: 1px solid #333;
  padding-bottom: 10px;
}

.credits-box {
  color: #aaa;
  font-size: 0.95rem;
}

.credits-box p {
  margin-bottom: 15px;
}

.credits-box strong {
  color: #fff;
}

.storage-info {
  color: #aaa;
}

.storage-usage {
  margin: 15px 0;
  display: flex;
  align-items: baseline;
  gap: 8px;
}

.storage-value {
  font-size: 22px;
  font-weight: 600;
  color: #fff;
  font-family: 'Montserrat', sans-serif;
}

.storage-unit {
  font-size: 1.2rem;
  color: #666;
  font-weight: 600;
}

.storage-details {
  font-size: 0.85rem;
  color: #666;
}

.backup-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
  margin: 20px 0 10px;
}

.backup-actions .btn-secondary {
  flex: 1 1 180px;
}

/* Stack Details View */
.stack-details-container {
  max-width: 800px;
  margin: 0 auto;
}

.category-section {
  background: #111;
  border: 1px solid #333;
  border-radius: 8px;
  padding: 20px;
  margin-bottom: 20px;
}

.category-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 15px;
  padding-bottom: 15px;
  border-bottom: 1px solid #333;
}

.category-actions {
  display: flex;
  gap: 8px;
}

.btn-sm {
  padding: 6px 12px;
  font-size: 0.85rem;
}

.category-export-view {
  margin-top: 15px;
  width: 100%;
}

.export-textarea {
  display: none;
}

.export-textarea:focus {
  outline: none;
  border-color: #444;
}

/* Visual Card List Styles */
.category-visual-view {
  --card-width: 60px;
  --card-overlap: 45px;
  display: flex;
  flex-wrap: wrap;
  gap: 15px 0;
  padding: 15px 10px;
  padding-right: calc(10px + var(--card-overlap));
  background: #0a0a0a;
  border: 1px solid #222;
  border-radius: 4px;
  margin-top: 15px;
  min-height: 100px;
}

.visual-card {
  position: relative;
  width: var(--card-width);
  aspect-ratio: 488 / 680;
  margin-right: calc(-1 * var(--card-overlap));
  z-index: 1;
  transition: transform 0.2s ease, z-index 0s;
}

.visual-card:hover {
  transform: translateY(-10px);
  z-index: 100;
}

.visual-card img {
  width: 100%;
  height: 100%;
  border-radius: 4px;
  box-shadow: 2px 0 8px rgba(0,0,0,0.8);
  border: 1px solid #333;
  display: block;
  background: #111;
}

.category-info h3 {
  margin-bottom: 5px;
  font-size: 1.3rem;
}

.category-count {
  color: #888;
  font-size: 0.9rem;
}

.category-cards-list {
  margin-top: 15px;
}

.cards-list {
  list-style: none;
  padding: 0;
  margin: 0;
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(250px, 1fr));
  gap: 10px;
}

.cards-list li {
  padding: 10px 15px;
  background: #0a0a0a;
  border: 1px solid #222;
  border-radius: 4px;
  color: #ddd;
  font-size: 0.95rem;
  transition: all 0.2s;
}

.cards-list li:hover {
  background: #1a1a1a;
  border-color: #444;
}

/* Modal / Dialog */
.modal {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.8);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 1000;
  animation: fadeIn 0.2s ease-out;
}

@keyframes fadeIn {
  from { opacity: 0; }
  to { opacity: 1; }
}

.modal-content {
  background: #111;
  border: 1px solid #333;
  border-radius: 8px;
  max-width: 500px;
  width: 90%;
  max-height: 80vh;
  overflow-y: auto;
  animation: slideUp 0.3s ease-out;
}

@keyframes slideUp {
  from {
    opacity: 0;
    transform: translateY(20px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

.modal-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 20px;
  border-bottom: 1px solid #333;
}

.modal-header h2 {
  margin: 0;
  font-size: 1.5rem;
}

.modal-body {
  padding: 20px;
}

.modal-body h3 {
  margin: 20px 0 10px 0;
  font-size: 1.1rem;
}

.modal-body h3:first-child {
  margin-top: 0;
}

.modal-body ul {
  list-style: none;
  padding: 0;
  margin: 0;
}

.modal-body li {
  padding: 8px 0;
  display: flex;
  align-items: center;
  gap: 10px;
}

.modal-body kbd {
  background: #222;
  border: 1px solid #444;
  border-radius: 4px;
  padding: 4px 8px;
  font-family: 'Montserrat', sans-serif;
  font-size: 0.9rem;
  box-shadow: 0 2px 0 #000;
}

/* Responsive Header */
@media (max-width: 768px) {
  .view-header {
    gap: 8px;
    margin-bottom: 15px;
    padding: 5px 0;
    grid-template-columns: auto 1fr auto;
  }

  .header-left, .header-right {
    flex: 0 0 auto;
  }

  .btn-back {
    padding: 8px 10px;
    font-size: 0.85rem;
  }

  .btn-back .btn-text {
    display: none;
  }

  .view-header h1 {
    font-size: 1.3rem;
  }

  .stack-title {
    font-size: 1.1rem;
  }
}

@media (max-width: 480px) {
  .view-header {
    gap: 6px;
  }
  
  .btn-undo-subtle {
    font-size: 0.65rem;
    padding: 2px 6px;
  }
}

/* Toast notifications */
.toast-region {
  position: fixed;
  left: 50%;
  bottom: calc(24px + env(safe-area-inset-bottom, 0px));
  transform: translateX(-50%);
  z-index: 2000;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 8px;
  width: min(420px, calc(100% - 32px));
  pointer-events: none;
}

.toast {
  pointer-events: auto;
  cursor: pointer;
  width: 100%;
  padding: 12px 16px;
  background: #111;
  color: #fff;
  border: 1px solid #333;
  border-left: 3px solid #888;
  border-radius: 6px;
  font-family: 'Montserrat', sans-serif;
  font-size: 0.9rem;
  line-height: 1.4;
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.5);
  animation: toast-in 0.2s ease-out;
}

.toast-success { border-left-color: #4caf50; }
.toast-error { border-left-color: #f44; }

.toast-leaving {
  opacity: 0;
  transform: translateY(8px);
  transition: opacity 0.2s ease, transform 0.2s ease;
}

@keyframes toast-in {
  from { opacity: 0; transform: translateY(8px); }
  to { opacity: 1; transform: translateY(0); }
}

/* Stack card header: name + ⋯ menu */
.stack-card-header {
  display: flex;
  align-items: flex-start;
  gap: 8px;
}

.stack-card-header .stack-name {
  flex: 1;
  min-width: 0;
  overflow-wrap: anywhere;
}

.stack-card.menu-open {
  z-index: 5;
}

.stack-menu {
  position: relative;
  flex-shrink: 0;
  /* The 32px button lines up with the title's first line (~33px tall) */
  margin-top: 0;
}

.btn-stack-menu {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 32px;
  height: 32px;
  padding: 0;
  background: transparent;
  border: 1px solid transparent;
  border-radius: 6px;
  color: #888;
  cursor: pointer;
  transition: color 0.15s, background 0.15s, border-color 0.15s;
}

.btn-stack-menu:hover,
.btn-stack-menu[aria-expanded="true"] {
  color: #fff;
  background: #1a1a1a;
  border-color: #333;
}

.btn-stack-menu:focus-visible {
  outline: 2px solid #fff;
  outline-offset: 2px;
  color: #fff;
}

.stack-menu-dropdown {
  position: absolute;
  top: calc(100% + 6px);
  right: 0;
  z-index: 10;
  min-width: 160px;
  max-width: calc(100vw - 32px);
  padding: 4px;
  background: #111;
  border: 1px solid #333;
  border-radius: 6px;
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.6);
}

.stack-menu-dropdown[hidden] {
  display: none;
}

.stack-menu-item {
  display: block;
  width: 100%;
  padding: 10px 12px;
  background: transparent;
  border: none;
  border-radius: 4px;
  color: #fff;
  font: inherit;
  font-size: 0.9rem;
  text-align: left;
  white-space: nowrap;
  cursor: pointer;
}

.stack-menu-item:hover,
.stack-menu-item:focus-visible {
  background: #222;
  outline: none;
}

.stack-menu-item-danger {
  color: #f44;
}

/* Inline rename field in place of the stack name */
.stack-rename-input {
  width: 100%;
  font-size: 1.1rem;
  padding: 6px 10px;
}

/* Range rows (mana value + price) */
.range-row {
  display: flex;
  align-items: center;
  gap: 12px;
  background: #111;
  border: 1px solid #333;
  padding: 10px;
  border-radius: 4px;
}

.range-value {
  font-family: 'Montserrat', sans-serif;
  font-weight: 400;
  min-width: 72px;
  text-align: right;
  white-space: nowrap;
}

.single-range {
  flex: 1;
  min-width: 0;
  cursor: pointer;
  accent-color: #fff;
}

/* Dual-handle slider: two stacked range inputs sharing one track */
.dual-range {
  position: relative;
  flex: 1;
  min-width: 0;
  height: 24px;
}

.dual-range-track,
.dual-range-fill {
  position: absolute;
  top: 50%;
  height: 4px;
  transform: translateY(-50%);
  border-radius: 2px;
  pointer-events: none;
}

.dual-range-track {
  left: 0;
  right: 0;
  background: #444;
}

.dual-range-fill {
  background: #fff;
}

.dual-range input[type="range"] {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  margin: 0;
  background: transparent;
  pointer-events: none;
  -webkit-appearance: none;
  appearance: none;
}

.dual-range input[type="range"].on-top {
  z-index: 2;
}

.dual-range input[type="range"]::-webkit-slider-runnable-track {
  background: transparent;
  height: 100%;
}

.dual-range input[type="range"]::-moz-range-track {
  background: transparent;
}

.dual-range input[type="range"]::-webkit-slider-thumb {
  -webkit-appearance: none;
  appearance: none;
  pointer-events: auto;
  width: 22px;
  height: 22px;
  margin-top: 1px;
  border-radius: 50%;
  background: #fff;
  border: 2px solid #111;
  cursor: pointer;
}

.dual-range input[type="range"]::-moz-range-thumb {
  pointer-events: auto;
  width: 18px;
  height: 18px;
  border-radius: 50%;
  background: #fff;
  border: 2px solid #111;
  cursor: pointer;
}

.dual-range input[type="range"]:focus-visible {
  outline: none;
}

.dual-range input[type="range"]:focus-visible::-webkit-slider-thumb {
  box-shadow: 0 0 0 2px #111, 0 0 0 4px #fff;
}

.dual-range input[type="range"]:focus-visible::-moz-range-thumb {
  box-shadow: 0 0 0 2px #111, 0 0 0 4px #fff;
}

/* Details header: primary "Sort stack" sits above the per-category actions */
.btn-sort-stack {
  padding: 10px 20px;
  font-size: 1rem;
}

.stack-menu-item:disabled {
  opacity: 0.4;
  cursor: default;
}

/* Category card viewer: zoom into a sorted list */
.visual-card { cursor: zoom-in; }

.card-viewer {
  --card-max-width: 488px;
  --card-actual-width: min(var(--card-max-width), 85vw, calc((100vh - 120px) * 488 / 680));
  position: fixed;
  inset: 0;
  z-index: 1000;
  display: flex;
  align-items: center;
  justify-content: center;
  overflow: hidden;
}

.card-viewer[hidden] { display: none; }

.viewer-backdrop {
  position: absolute;
  inset: 0;
  background: rgba(0, 0, 0, 0.88);
  animation: viewerFade 0.2s ease-out;
}

.viewer-track {
  position: relative;
  width: var(--card-actual-width);
  aspect-ratio: 488 / 680;
  pointer-events: none;
}

.viewer-card {
  --step: 55%;
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  border-radius: 12px;
  box-shadow: 0 10px 40px rgba(0, 0, 0, 0.6);
  user-select: none;
  background: #111;
  pointer-events: auto;
  cursor: pointer;
  transform: translateX(calc(var(--offset) * var(--step))) scale(var(--scale, 1));
  transition: transform 0.35s ease-out, opacity 0.35s ease-out, filter 0.35s ease-out;
}

.viewer-card[data-pos="0"] { --scale: 1;    z-index: 5; opacity: 1; cursor: grab; touch-action: none; }
.viewer-card[data-pos="1"] { --scale: 0.85; z-index: 4; opacity: 1; filter: brightness(0.6); }
.viewer-card[data-pos="2"] { --scale: 0.72; z-index: 3; opacity: 0.35; filter: brightness(0.5); }
.viewer-card[data-pos="3"] { --scale: 0.6;  z-index: 2; opacity: 0; pointer-events: none; }

.viewer-track { animation: viewerFade 0.2s ease-out; }

.viewer-close {
  position: absolute;
  top: 16px;
  right: 16px;
  z-index: 5;
  width: 40px;
  height: 40px;
  border: 1px solid #333;
  border-radius: 50%;
  background: #111;
  color: #ddd;
  font-size: 1.5rem;
  line-height: 1;
  cursor: pointer;
}

.viewer-close:hover { background: #222; }

.viewer-counter {
  position: absolute;
  bottom: 16px;
  left: 0;
  right: 0;
  z-index: 5;
  text-align: center;
  color: #888;
  font-size: 0.9rem;
}

@keyframes viewerFade { from { opacity: 0; } to { opacity: 1; } }

@media (max-width: 768px) {
  .viewer-card { --step: 38%; }
}

/* Reusable confirm modal (showModal) */
.confirm-modal .modal-content { max-width: 420px; }
.confirm-modal .modal-header h2 { font-size: 1.2rem; }
.modal-message { color: #ccc; line-height: 1.5; margin: 0; }

.modal-actions {
  display: flex;
  justify-content: flex-end;
  gap: 10px;
  margin-top: 24px;
}

.btn-primary.btn-danger { background: #c62828; border-color: #c62828; color: #fff; }
.btn-primary.btn-danger:hover { background: #e53935; border-color: #e53935; }

.stack-menu-item:disabled:hover { background: transparent; }
