* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

:root {
  --bg: #0f172a;
  --surface: #1e293b;
  --surface-hover: #334155;
  --border: #475569;
  --text: #f1f5f9;
  --text-muted: #94a3b8;
  --accent: #3b82f6;
  --accent-hover: #2563eb;
  --danger: #ef4444;
  --danger-hover: #dc2626;
  --card-width: 2.5in;
  --card-height: 3.5in;
}

body {
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
  background: var(--bg);
  color: var(--text);
  min-height: 100vh;
}

header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0.75rem 1.5rem;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
}

header h1 {
  font-size: 1.25rem;
  font-weight: 600;
}

.header-actions {
  display: flex;
  gap: 0.5rem;
}

button {
  padding: 0.5rem 1rem;
  border: none;
  border-radius: 6px;
  background: var(--accent);
  color: white;
  font-size: 0.875rem;
  cursor: pointer;
  font-weight: 500;
  transition: background 0.15s;
}

button:hover {
  background: var(--accent-hover);
}

.btn-danger {
  background: var(--danger);
}

.btn-danger:hover {
  background: var(--danger-hover);
}

.btn-small {
  padding: 0.25rem 0.75rem;
  font-size: 0.8rem;
  margin-top: 0.5rem;
}

main {
  display: grid;
  grid-template-columns: 240px 1fr 320px;
  height: calc(100vh - 53px);
  overflow: hidden;
}

/* Card list panel */
#card-list-panel {
  background: var(--surface);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

.panel-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0.75rem;
  border-bottom: 1px solid var(--border);
}

.panel-header h2 {
  font-size: 0.95rem;
  font-weight: 600;
}

.panel-header button {
  padding: 0.3rem 0.6rem;
  font-size: 0.8rem;
}

#card-list {
  flex: 1;
  overflow-y: auto;
  padding: 0.5rem;
}

.card-list-item {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  padding: 0.5rem;
  border-radius: 6px;
  cursor: pointer;
  transition: background 0.15s;
  margin-bottom: 2px;
}

.card-list-item:hover {
  background: var(--surface-hover);
}

.card-list-item.active {
  background: var(--accent);
}

.card-list-item .card-swatch {
  width: 24px;
  height: 32px;
  border-radius: 3px;
  flex-shrink: 0;
  border: 1px solid rgba(255, 255, 255, 0.1);
}

.card-list-item .card-list-info {
  flex: 1;
  min-width: 0;
}

.card-list-item .card-list-name {
  font-size: 0.85rem;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.card-list-item .card-list-meta {
  font-size: 0.7rem;
  color: var(--text-muted);
}

/* Preview panel */
#preview-panel {
  display: flex;
  flex-direction: column;
  align-items: center;
  padding: 1.5rem;
  overflow-y: auto;
  gap: 1.5rem;
}

#preview-panel h2,
#preview-panel h3 {
  font-size: 0.95rem;
  font-weight: 600;
  color: var(--text-muted);
}

/* Card rendering */
.card-face {
  width: var(--card-width);
  height: var(--card-height);
  border-radius: 8px;
  overflow: hidden;
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.4);
  position: relative;
  flex-shrink: 0;
}

.card-front .card-inner {
  width: 100%;
  height: 100%;
  display: flex;
  flex-direction: column;
  background: #1a1a2e;
  color: white;
}

.card-header-bar {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 4px 8px;
  font-size: 0.65rem;
  font-weight: 700;
  text-transform: uppercase;
}

.card-name-display {
  flex: 1;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.card-rarity-display {
  font-size: 0.55rem;
  padding: 1px 6px;
  border-radius: 3px;
  background: rgba(255, 255, 255, 0.15);
  text-transform: uppercase;
  letter-spacing: 0.05em;
  flex-shrink: 0;
}

.card-rarity-display[data-rarity='common'] {
  background: #6b7280;
}
.card-rarity-display[data-rarity='uncommon'] {
  background: #059669;
}
.card-rarity-display[data-rarity='rare'] {
  background: #2563eb;
}
.card-rarity-display[data-rarity='epic'] {
  background: #7c3aed;
}
.card-rarity-display[data-rarity='legendary'] {
  background: linear-gradient(135deg, #f59e0b, #ef4444);
}

.card-image-area {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  background: rgba(0, 0, 0, 0.3);
  margin: 0 4px;
  border-radius: 4px;
  overflow: hidden;
  position: relative;
}

.card-image-area img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.placeholder-text {
  color: rgba(255, 255, 255, 0.3);
  font-size: 0.7rem;
}

.card-type-bar {
  padding: 2px 8px;
  font-size: 0.55rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  opacity: 0.8;
}

.card-stats {
  padding: 4px 6px 6px;
  font-size: 0.5rem;
}

.stat-row {
  display: flex;
  gap: 4px;
  margin-bottom: 1px;
}

.stat-label {
  font-weight: 700;
  flex-shrink: 0;
}

.card-powers {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 1px 6px;
  margin: 2px 0;
}

.power-row {
  display: flex;
  justify-content: space-between;
  padding: 1px 4px;
  background: rgba(255, 255, 255, 0.05);
  border-radius: 2px;
}

.power-name {
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.power-value {
  font-weight: 700;
  flex-shrink: 0;
  margin-left: 4px;
}

.help-row {
  margin-top: 2px;
  font-weight: 700;
}

/* Card back */
.card-back .card-back-inner {
  width: 100%;
  height: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
  background: #1e3a5f;
  color: white;
  font-weight: 700;
  font-size: 0.85rem;
  text-align: center;
  padding: 1rem;
}

.card-back-inner img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

/* Editor panel */
#editor-panel {
  background: var(--surface);
  border-left: 1px solid var(--border);
  overflow-y: auto;
  padding: 1rem;
}

#editor-panel h2 {
  font-size: 0.95rem;
  font-weight: 600;
  margin-bottom: 1rem;
}

#editor-empty {
  color: var(--text-muted);
  text-align: center;
  margin-top: 3rem;
  font-size: 0.9rem;
}

#card-editor fieldset,
#back-editor fieldset {
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 0.75rem;
  margin-bottom: 0.75rem;
}

#card-editor legend,
#back-editor legend {
  font-size: 0.75rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: var(--text-muted);
  padding: 0 0.25rem;
}

#card-editor label,
#back-editor label {
  display: block;
  font-size: 0.8rem;
  margin-bottom: 0.5rem;
  color: var(--text-muted);
}

#card-editor input[type='text'],
#card-editor input[type='number'],
#card-editor select,
#back-editor input[type='text'],
#back-editor input[type='number'],
#back-editor select {
  display: block;
  width: 100%;
  padding: 0.4rem 0.5rem;
  margin-top: 0.2rem;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 4px;
  color: var(--text);
  font-size: 0.85rem;
}

#card-editor input[type='color'],
#back-editor input[type='color'] {
  display: block;
  width: 100%;
  height: 32px;
  margin-top: 0.2rem;
  border: 1px solid var(--border);
  border-radius: 4px;
  background: var(--bg);
  cursor: pointer;
  padding: 2px;
}

#image-upload-area,
#back-upload-area {
  border: 2px dashed var(--border);
  border-radius: 6px;
  padding: 1.5rem;
  text-align: center;
  cursor: pointer;
  color: var(--text-muted);
  font-size: 0.8rem;
  transition:
    border-color 0.15s,
    background 0.15s;
}

#image-upload-area:hover,
#back-upload-area:hover {
  border-color: var(--accent);
  background: rgba(59, 130, 246, 0.05);
}

#image-upload-area.has-image,
#back-upload-area.has-image {
  border-style: solid;
  border-color: var(--accent);
  padding: 0.5rem;
}

#image-upload-area.has-image img,
#back-upload-area.has-image img {
  max-width: 100%;
  max-height: 100px;
  border-radius: 4px;
}

.power-input-row {
  display: flex;
  gap: 0.5rem;
  margin-bottom: 0.5rem;
  align-items: stretch;
}

#card-editor .power-input-row input[type='text'] {
  flex: 1;
  min-width: 0;
  padding: 0.55rem 0.6rem;
  font-size: 0.9rem;
}

#card-editor .power-input-row input[type='number'] {
  width: 68px;
  flex: 0 0 68px;
  text-align: center;
  padding: 0.55rem 0.4rem;
  font-size: 0.9rem;
  -moz-appearance: textfield;
  appearance: textfield;
}

/* Drop the spinner so three digits fit comfortably. */
.power-input-row input[type='number']::-webkit-inner-spin-button,
.power-input-row input[type='number']::-webkit-outer-spin-button {
  -webkit-appearance: none;
  margin: 0;
}

#back-editor .hint {
  font-size: 0.8rem;
  color: var(--text-muted);
  margin-bottom: 0.75rem;
}

/* Special "Card Back" entry in the card list */
.card-list-item.back-item {
  margin-bottom: 0.5rem;
  padding-bottom: 0.75rem;
  border-bottom: 1px solid var(--border);
  border-radius: 6px 6px 0 0;
}

.editor-actions {
  display: flex;
  gap: 0.5rem;
  margin-top: 1rem;
}

.editor-actions button {
  flex: 1;
}

/* Print overlay */
#print-overlay {
  position: fixed;
  inset: 0;
  background: white;
  z-index: 1000;
  overflow-y: auto;
}

.print-controls {
  position: sticky;
  top: 0;
  z-index: 10;
  display: flex;
  gap: 0.5rem;
  padding: 1rem;
  background: #1e293b;
  justify-content: center;
}

#print-pages {
  padding: 0;
}

.print-page {
  width: 8.5in;
  height: 11in;
  margin: 0 auto;
  padding: 0.25in;
  display: grid;
  grid-template-columns: repeat(3, var(--card-width));
  grid-template-rows: repeat(2, var(--card-height));
  gap: 0.125in;
  justify-content: center;
  align-content: center;
  page-break-after: always;
  background: white;
  position: relative;
}

.print-page .card-face {
  box-shadow: none;
  border: 0.5px solid #ccc;
}

.print-page.back-page .card-face {
  transform: rotate(180deg);
}

/* Screen-only: show page borders */
@media screen {
  .print-page {
    border: 1px dashed #ccc;
    margin-bottom: 0.5rem;
  }
}

/* Print styles */
@media print {
  .no-print {
    display: none !important;
  }

  body {
    background: white;
  }

  #app {
    display: none;
  }

  #print-overlay {
    display: block !important;
    position: static;
  }

  .print-page {
    margin: 0 auto;
    page-break-after: always;
    border: none;
  }

  .print-page:last-child {
    page-break-after: avoid;
  }
}

/* Drag and drop highlight */
.drag-over {
  border-color: var(--accent) !important;
  background: rgba(59, 130, 246, 0.1) !important;
}

/* Scrollbar styling */
::-webkit-scrollbar {
  width: 6px;
}

::-webkit-scrollbar-track {
  background: transparent;
}

::-webkit-scrollbar-thumb {
  background: var(--border);
  border-radius: 3px;
}

::-webkit-scrollbar-thumb:hover {
  background: var(--text-muted);
}

/* Responsive */
@media (max-width: 900px) {
  main {
    grid-template-columns: 1fr;
    grid-template-rows: auto 1fr auto;
    height: auto;
    overflow: visible;
  }

  #card-list-panel {
    border-right: none;
    border-bottom: 1px solid var(--border);
    max-height: 200px;
  }

  #editor-panel {
    border-left: none;
    border-top: 1px solid var(--border);
  }
}
