/* ── Reset & Base ── */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

:root {
  --primary: #1a1a2e;
  --accent: #e94560;
  --accent2: #f5a623;
  --card-bg: #0f3460;
  --card-text: #ffffff;
  --card-header-bg: #e94560;
  --card-footer-bg: #0a1628;
  --card-shine: rgba(255,255,255,0.08);
  --card-border: #e94560;
  --stat-bg: rgba(255,255,255,0.12);
  --badge-bg: #f5a623;
  --badge-text: #1a1a2e;
  --section-label-color: #f5a623;
  --ui-bg: #12121f;
  --ui-surface: #1c1c30;
  --ui-border: #2e2e4e;
  --ui-text: #e0e0f0;
  --ui-muted: #888aaa;
}

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

header {
  text-align: center;
  padding: 2rem 1rem 1rem;
}
header h1 {
  font-size: 2rem;
  font-weight: 800;
  letter-spacing: 0.04em;
  color: #fff;
}
header p { color: var(--ui-muted); margin-top: 0.25rem; }

.app { max-width: 1200px; margin: 0 auto; padding: 0 1rem 4rem; }

/* ── Card Deck ── */
.deck-section {
  margin-top: 1.5rem;
  background: var(--ui-surface);
  border: 1px solid var(--ui-border);
  border-radius: 16px;
  padding: 1rem 1.25rem;
}
.deck-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 0.85rem;
}
.deck-title {
  font-size: 0.78rem;
  font-weight: 700;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  color: var(--ui-muted);
}
.btn-new-card {
  background: var(--accent);
  color: #fff;
  border: none;
  border-radius: 8px;
  padding: 0.4rem 0.9rem;
  font-size: 0.82rem;
  font-weight: 700;
  cursor: pointer;
  transition: opacity 0.2s;
}
.btn-new-card:hover { opacity: 0.85; }

.deck-scroll { overflow-x: auto; padding-bottom: 0.5rem; }
.deck-scroll::-webkit-scrollbar { height: 4px; }
.deck-scroll::-webkit-scrollbar-thumb { background: var(--ui-border); border-radius: 4px; }

.card-deck {
  display: flex;
  gap: 0.75rem;
  align-items: flex-start;
  min-height: 116px;
}

/* Mini Card */
.mini-card {
  position: relative;
  width: 80px;
  min-width: 80px;
  height: 112px;
  border-radius: 8px;
  border: 2px solid var(--ui-border);
  background: var(--ui-bg);
  cursor: pointer;
  overflow: hidden;
  transition: border-color 0.2s, box-shadow 0.2s, transform 0.15s;
  flex-shrink: 0;
}
.mini-card:hover { transform: translateY(-3px); }
.mini-card.active {
  border-color: var(--accent);
  box-shadow: 0 0 0 2px rgba(233,69,96,0.3), 0 4px 16px rgba(0,0,0,0.4);
}
.mini-card-header {
  height: 28px;
  display: flex;
  align-items: center;
  padding: 0 6px;
  font-size: 0.6rem;
  font-weight: 800;
  letter-spacing: 0.02em;
  text-transform: uppercase;
  line-height: 1.1;
  color: #fff;
  background: var(--card-header-bg);
  word-break: break-word;
  overflow: hidden;
}
.mini-card-img {
  width: 100%;
  height: 54px;
  object-fit: cover;
  display: block;
}
.mini-card-placeholder {
  width: 100%;
  height: 54px;
  display: flex;
  align-items: center;
  justify-content: center;
  background: rgba(255,255,255,0.04);
  font-size: 0.6rem;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  opacity: 0.3;
}
.mini-card-footer {
  height: 20px;
  padding: 0 6px;
  display: flex;
  align-items: center;
  font-size: 0.55rem;
  opacity: 0.5;
  background: rgba(0,0,0,0.3);
  color: #fff;
  overflow: hidden;
  white-space: nowrap;
  text-overflow: ellipsis;
}
.mini-card-delete {
  position: absolute;
  top: 4px;
  right: 4px;
  width: 16px;
  height: 16px;
  background: rgba(0,0,0,0.7);
  color: #fff;
  border: none;
  border-radius: 50%;
  font-size: 0.7rem;
  line-height: 1;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  opacity: 0;
  transition: opacity 0.15s;
  z-index: 2;
}
.mini-card:hover .mini-card-delete { opacity: 1; }
.mini-card-delete:hover { background: var(--accent); }

/* ── Workspace ── */
.workspace {
  display: grid;
  grid-template-columns: 420px 1fr;
  gap: 2rem;
  margin-top: 2rem;
  align-items: start;
}
@media (max-width: 900px) {
  .workspace { grid-template-columns: 1fr; }
}

/* ── Editor Panel ── */
.editor-panel {
  background: var(--ui-surface);
  border: 1px solid var(--ui-border);
  border-radius: 16px;
  padding: 1.5rem;
}
.editor-panel h2,
.preview-panel h2 {
  font-size: 1rem;
  font-weight: 700;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  color: var(--ui-muted);
  margin-bottom: 1.25rem;
}

.field-group { margin-bottom: 1.1rem; }
.field-group label {
  display: block;
  font-size: 0.78rem;
  font-weight: 600;
  letter-spacing: 0.06em;
  text-transform: uppercase;
  color: var(--ui-muted);
  margin-bottom: 0.4rem;
}
.two-col {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 0.75rem;
}
.two-col .field-group { margin-bottom: 0; }

input[type="text"],
input[type="number"],
textarea {
  width: 100%;
  background: var(--ui-bg);
  border: 1px solid var(--ui-border);
  border-radius: 8px;
  padding: 0.55rem 0.75rem;
  color: var(--ui-text);
  font-size: 0.9rem;
  font-family: inherit;
  transition: border-color 0.2s;
  resize: vertical;
}
input:focus, textarea:focus {
  outline: none;
  border-color: var(--accent);
}
.char-count {
  display: block;
  text-align: right;
  font-size: 0.72rem;
  color: var(--ui-muted);
  margin-top: 0.2rem;
}

/* ── Image Upload ── */
.image-upload-area {
  border: 2px dashed var(--ui-border);
  border-radius: 10px;
  transition: border-color 0.2s;
  overflow: hidden;
}
.image-upload-area.drover { border-color: var(--accent); }

.upload-placeholder {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 0.4rem;
  color: var(--ui-muted);
  font-size: 0.85rem;
  padding: 1.5rem;
  cursor: pointer;
}
.upload-placeholder:hover { color: var(--ui-text); }
.upload-icon { font-size: 2rem; line-height: 1; }

.image-loaded { display: flex; flex-direction: column; }
.preview-img {
  width: 100%;
  height: 160px;
  object-fit: cover;
  display: block;
}
.image-btns {
  display: flex;
  gap: 0.5rem;
  padding: 0.5rem;
  background: rgba(0,0,0,0.3);
}
.img-action-btn {
  flex: 1;
  background: var(--ui-surface);
  border: 1px solid var(--ui-border);
  color: var(--ui-text);
  border-radius: 6px;
  padding: 0.3rem 0.5rem;
  font-size: 0.75rem;
  font-weight: 600;
  cursor: pointer;
  transition: border-color 0.2s;
}
.img-action-btn:hover { border-color: var(--accent); }
.img-action-btn.danger:hover { border-color: #e94560; color: #e94560; }
.hidden { display: none !important; }

/* ── Tags / Ideas ── */
.tag-list {
  display: flex;
  flex-wrap: wrap;
  gap: 0.4rem;
  margin-bottom: 0.5rem;
  min-height: 32px;
}
.tag {
  display: inline-flex;
  align-items: center;
  gap: 0.3rem;
  background: rgba(233,69,96,0.15);
  border: 1px solid rgba(233,69,96,0.4);
  color: #e94560;
  border-radius: 20px;
  padding: 0.2rem 0.6rem;
  font-size: 0.78rem;
  font-weight: 600;
}
.tag button {
  background: none;
  border: none;
  color: inherit;
  cursor: pointer;
  font-size: 1rem;
  line-height: 1;
  padding: 0;
  opacity: 0.6;
}
.tag button:hover { opacity: 1; }

.tag-input-row { display: flex; gap: 0.5rem; }
.tag-input-row input { flex: 1; }

/* ── Stats editor ── */
.stats-editor { margin-bottom: 0.5rem; }
.stat-row-item {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  margin-bottom: 0.35rem;
  font-size: 0.85rem;
}
.stat-row-item .stat-label-tag { font-weight: 700; color: var(--accent2); min-width: 80px; }
.stat-row-item .stat-val-tag { color: var(--ui-text); }
.stat-row-item button {
  background: none; border: none; color: var(--ui-muted);
  cursor: pointer; font-size: 1rem; margin-left: auto;
}
.stat-row-item button:hover { color: var(--accent); }
.stats-row input:first-child { max-width: 130px; }
.stats-row input:nth-child(2) { max-width: 100px; }

/* ── Buttons ── */
.add-btn {
  background: var(--accent);
  color: #fff;
  border: none;
  border-radius: 8px;
  padding: 0.55rem 1rem;
  font-size: 0.85rem;
  font-weight: 700;
  cursor: pointer;
  white-space: nowrap;
  transition: opacity 0.2s;
}
.add-btn:hover { opacity: 0.85; }

.actions {
  display: flex;
  gap: 0.75rem;
  margin-top: 1.5rem;
  flex-wrap: wrap;
}
.btn-randomize {
  flex: 1;
  background: linear-gradient(135deg, #6c3fd8, #e94560);
  color: #fff;
  border: none;
  border-radius: 10px;
  padding: 0.7rem 1rem;
  font-size: 0.9rem;
  font-weight: 700;
  cursor: pointer;
  transition: opacity 0.2s, transform 0.1s;
}
.btn-randomize:hover { opacity: 0.9; transform: scale(1.02); }

.btn-share {
  flex: 1;
  background: var(--ui-surface);
  color: var(--ui-text);
  border: 1px solid var(--ui-border);
  border-radius: 10px;
  padding: 0.7rem 1rem;
  font-size: 0.9rem;
  font-weight: 700;
  cursor: pointer;
  transition: border-color 0.2s, color 0.2s;
}
.btn-share:hover { border-color: var(--accent); color: #fff; }
.btn-share.copied {
  border-color: #4caf50;
  color: #4caf50;
}

.btn-print {
  flex: 1;
  background: var(--accent2);
  color: #1a1a2e;
  border: none;
  border-radius: 10px;
  padding: 0.7rem 1rem;
  font-size: 0.9rem;
  font-weight: 700;
  cursor: pointer;
  transition: opacity 0.2s, transform 0.1s;
}
.btn-print:hover { opacity: 0.9; transform: scale(1.02); }

/* ── Preview Panel ── */
.preview-panel {
  display: flex;
  flex-direction: column;
  align-items: center;
}
.card-wrapper {
  display: flex;
  justify-content: center;
  width: 100%;
}

/* ── Trading Card ── */
/*
  Fixed to 320×427px = 3:4 ratio.
  At print scale 1.125 this becomes 360×480px = 3.75in×5in,
  fitting 4 cards (2×2) on US Letter with 0.35in margins.
*/
.trading-card {
  width: 320px;
  height: 427px;
  overflow: hidden;
  border-radius: 18px;
  border: 3px solid var(--card-border);
  background: var(--card-bg);
  color: var(--card-text);
  box-shadow:
    0 0 0 1px rgba(255,255,255,0.05),
    0 8px 40px rgba(0,0,0,0.6),
    0 0 60px rgba(233,69,96,0.12);
  position: relative;
  font-size: 13px;
  transition: box-shadow 0.4s, border-color 0.4s, transform 0.2s;
}
.trading-card::before {
  content: '';
  position: absolute;
  inset: 0;
  background: linear-gradient(135deg, transparent 30%, var(--card-shine) 50%, transparent 70%);
  pointer-events: none;
  z-index: 10;
  border-radius: inherit;
}

.card-header {
  background: var(--card-header-bg);
  padding: 0.6rem 0.9rem 0.5rem;
}
.card-name {
  font-size: 1.1rem;
  font-weight: 900;
  letter-spacing: 0.03em;
  text-transform: uppercase;
  line-height: 1.1;
  text-shadow: 0 1px 4px rgba(0,0,0,0.4);
}
.card-meta {
  display: flex;
  gap: 0.5rem;
  margin-top: 0.2rem;
  font-size: 0.7rem;
  font-weight: 600;
  opacity: 0.9;
}
.card-position::after { content: ' •'; opacity: 0.5; margin-left: 0.25rem; }

.card-image-area {
  position: relative;
  height: 170px;
  background: linear-gradient(180deg, rgba(0,0,0,0.1) 0%, rgba(0,0,0,0.4) 100%);
  overflow: hidden;
}
.card-image-placeholder {
  width: 100%; height: 100%;
  display: flex; align-items: center; justify-content: center;
  font-size: 0.85rem;
  font-weight: 700;
  letter-spacing: 0.1em;
  opacity: 0.3;
  text-transform: uppercase;
  background: repeating-linear-gradient(
    45deg,
    rgba(255,255,255,0.03) 0px, rgba(255,255,255,0.03) 2px,
    transparent 2px, transparent 10px
  );
}
.card-photo {
  width: 100%;
  height: 100%;
  object-fit: cover;
}
.card-year-badge {
  position: absolute;
  top: 8px; right: 10px;
  background: var(--badge-bg);
  color: var(--badge-text);
  font-size: 0.65rem;
  font-weight: 900;
  padding: 2px 7px;
  border-radius: 20px;
  letter-spacing: 0.05em;
}

.card-body {
  padding: 0.7rem 0.9rem;
  display: flex;
  flex-direction: column;
  gap: 0.6rem;
}
.section-label {
  font-size: 0.6rem;
  font-weight: 900;
  letter-spacing: 0.12em;
  text-transform: uppercase;
  color: var(--section-label-color);
  margin-bottom: 0.25rem;
}
.card-bio p { font-size: 0.72rem; line-height: 1.5; opacity: 0.85; }

.card-ideas ul {
  list-style: none;
  display: flex;
  flex-wrap: wrap;
  gap: 0.3rem;
}
.card-ideas li {
  background: rgba(255,255,255,0.1);
  border-radius: 20px;
  padding: 0.15rem 0.55rem;
  font-size: 0.65rem;
  font-weight: 600;
  border: 1px solid rgba(255,255,255,0.15);
}

.card-funfact {
  background: rgba(255,255,255,0.06);
  border-left: 3px solid var(--section-label-color);
  border-radius: 0 6px 6px 0;
  padding: 0.4rem 0.6rem;
}
.card-funfact p { font-size: 0.7rem; line-height: 1.45; opacity: 0.85; font-style: italic; }

.stats-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(60px, 1fr));
  gap: 0.35rem;
}
.stat-cell {
  background: var(--stat-bg);
  border-radius: 6px;
  padding: 0.35rem 0.4rem;
  text-align: center;
  border: 1px solid rgba(255,255,255,0.08);
}
.stat-cell .s-value { font-size: 0.95rem; font-weight: 900; display: block; line-height: 1.1; }
.stat-cell .s-label { font-size: 0.55rem; letter-spacing: 0.08em; text-transform: uppercase; opacity: 0.65; display: block; }

.card-footer {
  background: var(--card-footer-bg);
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 0.4rem 0.9rem;
  font-size: 0.62rem;
  letter-spacing: 0.06em;
  opacity: 0.7;
  font-weight: 700;
  text-transform: uppercase;
}

/* ── Crop Modal ── */
.crop-modal {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.88);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 1000;
  padding: 1rem;
}
.crop-modal-inner {
  background: var(--ui-surface);
  border: 1px solid var(--ui-border);
  border-radius: 16px;
  padding: 1.5rem;
  width: 480px;
  max-width: 100%;
}
.crop-modal-inner h3 {
  font-size: 1rem;
  font-weight: 700;
  margin-bottom: 0.2rem;
  color: #fff;
}
.crop-hint {
  font-size: 0.78rem;
  color: var(--ui-muted);
  margin-bottom: 1rem;
}

.crop-viewport {
  width: 100%;
  aspect-ratio: 8 / 5;
  max-height: 280px;
  overflow: hidden;
  position: relative;
  cursor: grab;
  border-radius: 8px;
  background: #000;
  margin-bottom: 1rem;
  user-select: none;
  touch-action: none;
}
.crop-viewport.dragging { cursor: grabbing; }
.crop-frame {
  position: absolute;
  inset: 0;
  border: 2px solid rgba(255,255,255,0.25);
  border-radius: 6px;
  pointer-events: none;
  z-index: 2;
  box-shadow: inset 0 0 0 1px rgba(0,0,0,0.3);
}
#cropImg {
  position: absolute;
  transform-origin: 0 0;
  user-select: none;
  pointer-events: none;
  max-width: none;
}

.crop-controls {
  display: flex;
  align-items: center;
  gap: 0.75rem;
  margin-bottom: 1rem;
}
.zoom-icon { font-size: 1.1rem; color: var(--ui-muted); }
.crop-controls input[type="range"] {
  flex: 1;
  accent-color: var(--accent);
  height: 4px;
}

.crop-actions { display: flex; gap: 0.75rem; justify-content: flex-end; }
.btn-cancel {
  background: var(--ui-bg);
  color: var(--ui-text);
  border: 1px solid var(--ui-border);
  border-radius: 8px;
  padding: 0.6rem 1.2rem;
  font-size: 0.9rem;
  font-weight: 700;
  cursor: pointer;
}
.btn-apply {
  background: var(--accent);
  color: #fff;
  border: none;
  border-radius: 8px;
  padding: 0.6rem 1.2rem;
  font-size: 0.9rem;
  font-weight: 700;
  cursor: pointer;
}
.btn-cancel:hover { border-color: var(--accent); }
.btn-apply:hover { opacity: 0.88; }

/* ── Print Container (screen: hidden) ── */
#printContainer {
  display: none;
}

/* ── Print Styles ── */
@media print {
  @page {
    size: letter;
    margin: 0.35in;
    /* Content area: 7.8in × 10.3in
       2×2 grid with 0.2in gap → each card: 3.8in × 5.05in
       Screen card 320×427 scaled ×1.1875 ≈ 380×507 ≈ 3.96in×5.28in
       Using scale(1.125): 360×480 = 3.75in×5in — fits with a little breathing room */
  }

  * {
    -webkit-print-color-adjust: exact !important;
    print-color-adjust: exact !important;
  }

  html, body {
    background: white !important;
    margin: 0;
    padding: 0;
  }

  /* Hide the whole app UI */
  .app,
  .crop-modal,
  #cropCanvas {
    display: none !important;
  }

  /* Show and lay out the print container */
  #printContainer {
    display: block !important;
  }

  .print-grid {
    display: grid;
    grid-template-columns: 1fr 1fr;
    grid-auto-rows: 5in;
    gap: 0.2in;
  }

  /* Each cell clips the scaled card to exactly 3.75in × 5in */
  .print-cell {
    width: 3.75in;
    height: 5in;
    overflow: hidden;
    break-inside: avoid;
    page-break-inside: avoid;
  }

  /*
    Scale the 320×427px screen card up to 360×480px = 3.75in×5in.
    transform-origin: top left so the card fills from the top-left corner.
  */
  .print-card-wrapper {
    width: 320px;
    transform: scale(1.125);
    transform-origin: top left;
  }

  .print-card-wrapper .trading-card {
    width: 320px !important;
    height: 427px !important;
    box-shadow: none !important;
    transform: none !important; /* remove any leftover tilt */
  }

  .print-card-wrapper .trading-card::before {
    display: none; /* skip the holographic overlay in print */
  }
}
