:root {
  --bg: #f4f1fb;
  --panel: #ffffff;
  --ink: #1f1a2e;
  --muted: #6b6480;
  --line: #e7e2f2;
  --brand: #ff5c8a;
  --brand-2: #7c5cff;
  --accent: #ffb703;
  --radius: 14px;
  --shadow: 0 10px 30px rgba(40, 20, 80, 0.12);
  font-family: "Source Sans 3", system-ui, -apple-system, Segoe UI, Roboto, sans-serif;
}

* { box-sizing: border-box; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--ink);
  min-height: 100vh;
}

/* ---------- Top bar ---------- */
.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 14px 22px;
  background: var(--panel);
  border-bottom: 1px solid var(--line);
  position: sticky;
  top: 0;
  z-index: 20;
}
.brand {
  display: flex;
  align-items: center;
  gap: 10px;
  font-weight: 800;
  font-size: 22px;
  letter-spacing: -0.02em;
}
.brand .logo {
  font-size: 26px;
  filter: drop-shadow(0 2px 2px rgba(0,0,0,.12));
}
.brand .name {
  background: linear-gradient(90deg, var(--brand), var(--brand-2));
  -webkit-background-clip: text;
  background-clip: text;
  color: transparent;
}
.brand .tag {
  font-size: 12px;
  font-weight: 600;
  color: var(--muted);
  margin-left: 6px;
}

.btn {
  border: 0;
  border-radius: 999px;
  padding: 10px 18px;
  font-size: 14px;
  font-weight: 700;
  cursor: pointer;
  font-family: inherit;
  transition: transform .06s ease, box-shadow .2s ease, opacity .2s ease;
}
.btn:active { transform: translateY(1px); }
.btn-ai {
  color: #fff;
  background: linear-gradient(90deg, var(--brand), var(--brand-2));
  box-shadow: 0 6px 18px rgba(124, 92, 255, .35);
}
.btn-ghost {
  background: var(--bg);
  color: var(--ink);
  border: 1px solid var(--line);
}

/* ---------- Toolbar (global tools; the card is the centerpiece) ---------- */
.toolbar {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 12px 22px;
  padding: 12px 22px;
  background: var(--panel);
  border-bottom: 1px solid var(--line);
  position: sticky;
  top: 56px;
  z-index: 15;
}
.tool-group { display: flex; align-items: center; gap: 8px; }
.tool-label {
  font-size: 11px;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: .06em;
  color: var(--muted);
}

/* ---------- Layout ---------- */
.layout {
  padding: 24px;
  max-width: 1200px;
  margin: 0 auto;
}

.field { margin-bottom: 14px; }
.field:last-child { margin-bottom: 0; }
.field label {
  display: block;
  font-size: 12px;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: .06em;
  color: var(--muted);
  margin-bottom: 6px;
}
.field select {
  width: 100%;
  border: 1px solid var(--line);
  border-radius: 10px;
  padding: 10px 12px;
  font-size: 15px;
  font-family: inherit;
  color: var(--ink);
  background: #fff;
}
.field select:focus {
  outline: 2px solid var(--brand-2);
  outline-offset: 1px;
  border-color: transparent;
}

/* ---------- Floating inspector (only shows when an item is selected) ---------- */
.inspector {
  position: fixed;
  z-index: 40;
  width: 280px;
  right: 22px;
  top: 124px;
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 14px;
  box-shadow: var(--shadow);
  padding: 14px 16px;
}
.inspector[hidden] { display: none; }
.inspector-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 12px;
}
.inspector-title {
  font-size: 12px;
  font-weight: 800;
  text-transform: uppercase;
  letter-spacing: .06em;
  color: var(--brand-2);
}
/* On phones, the inspector becomes an unobtrusive bottom sheet. */
@media (max-width: 720px) {
  .inspector {
    right: 0; left: 0; top: auto; bottom: 0;
    width: auto;
    border-radius: 16px 16px 0 0;
    border-bottom: none;
    box-shadow: 0 -8px 30px rgba(40,20,80,.18);
  }
}

/* Keldritch face/shadow rows (each is a palette swatch group + picker peer). */
.kel-row { display: flex; align-items: flex-start; gap: 8px; margin-bottom: 8px; }
.kel-row:last-child { margin-bottom: 0; }
.kel-tag {
  flex: 0 0 48px;
  font-size: 12px;
  font-weight: 700;
  color: var(--muted);
  padding-top: 6px;
}

.swatches { display: flex; flex-wrap: wrap; gap: 8px; }

/* Custom-color picker styled as a peer swatch (rainbow + a hidden color input). */
.swatch-picker {
  position: relative;
  overflow: hidden;
  background: conic-gradient(#ff5c8a, #ffb703, #06d6a0, #118ab2, #7c5cff, #ff5c8a);
  display: inline-flex;
  align-items: center;
  justify-content: center;
}
.swatch-picker::after {
  content: "🎨";
  font-size: 13px;
  filter: drop-shadow(0 0 1px rgba(255,255,255,.9));
}
.swatch-picker input[type="color"] {
  position: absolute;
  inset: 0;
  opacity: 0;
  width: 100%;
  height: 100%;
  border: none;
  padding: 0;
  cursor: pointer;
}
.swatch {
  width: 30px; height: 30px;
  border-radius: 8px;
  border: 2px solid #fff;
  box-shadow: 0 0 0 1px var(--line);
  cursor: pointer;
  padding: 0;
}
.swatch.selected { box-shadow: 0 0 0 2px var(--brand-2); }

.slider { width: 100%; }
.hint { font-size: 12px; color: var(--muted); margin-top: 6px; }

/* ---------- Stage / card (the centerpiece) ---------- */
.stage {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 14px;
}
.card-frame {
  /* Fit the card to both width and available height so it stays fully visible
     — on phones especially, where vertical space is the constraint. */
  width: min(440px, 92vw, calc((100dvh - 240px) * 5 / 7));
  aspect-ratio: 5 / 7;
  border-radius: 18px;
  box-shadow: var(--shadow);
  position: relative;
  container-type: inline-size;
}
#card {
  width: 100%;
  height: 100%;
  position: relative;
  border-radius: 18px;
  overflow: hidden;
}
.stage-hint {
  font-size: 12px;
  color: var(--muted);
  text-align: center;
  margin: 0;
}
/* Free-form draggable items live in this layer. */
.card-items {
  position: absolute;
  inset: 0;
}
.item-emoji { cursor: move; }
.item-emoji .item-content { user-select: none; line-height: 1; }
.item {
  position: absolute;
  box-sizing: border-box;
  cursor: move;
  touch-action: none;
}
.item.dragging { cursor: grabbing; }
.item .item-content {
  outline: none;
  word-break: break-word;
  line-height: 1.15;
}
.item-text .item-content {
  text-align: center;
  white-space: pre-wrap;
}
.item-text.editing { cursor: text; }
.item-text.editing .item-content { cursor: text; }
.item-image .item-content {
  width: 100%;
  line-height: 0;
  position: relative;
  overflow: hidden;
}
.item-image img {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  object-fit: cover;
  object-position: center;
  transform-origin: center;
  display: block;
  -webkit-user-drag: none;
  user-select: none;
}

/* Selection chrome */
.item.selected { outline: 2px solid var(--brand-2); outline-offset: 2px; }
.item .handle {
  position: absolute;
  width: 14px; height: 14px;
  right: -8px; bottom: -8px;
  background: var(--brand-2);
  border: 2px solid #fff;
  border-radius: 50%;
  box-shadow: 0 1px 3px rgba(0,0,0,.3);
  cursor: nwse-resize;
  display: none;
}
.item.selected .handle { display: block; }

/* Floating trashcan on the selected item's outside border (top-right). */
.item .item-trash {
  position: absolute;
  top: -14px; right: -14px;
  width: 26px; height: 26px;
  display: none;
  align-items: center;
  justify-content: center;
  padding: 0;
  font-size: 13px;
  line-height: 1;
  border: 2px solid #fff;
  border-radius: 50%;
  background: #e5484d;
  color: #fff;
  cursor: pointer;
  box-shadow: 0 2px 6px rgba(0,0,0,.3);
}
.item .item-trash:hover { background: #c93b40; }
.item.selected .item-trash { display: flex; }

.stage-actions { display: flex; gap: 10px; }

/* ---------- Modal ---------- */
.modal-backdrop {
  position: fixed;
  inset: 0;
  background: rgba(25, 15, 45, .45);
  display: none;
  align-items: center;
  justify-content: center;
  z-index: 50;
  padding: 20px;
}
.modal-backdrop.open { display: flex; }
.modal {
  background: var(--panel);
  border-radius: 18px;
  box-shadow: var(--shadow);
  width: 100%;
  max-width: 460px;
  padding: 24px;
}
.modal h2 { margin: 0 0 4px; font-size: 22px; }
.modal p.sub { margin: 0 0 16px; color: var(--muted); font-size: 14px; }
.modal textarea {
  width: 100%;
  min-height: 96px;
  border: 1px solid var(--line);
  border-radius: 10px;
  padding: 12px;
  font-size: 15px;
  font-family: inherit;
  resize: vertical;
}
.credits-note {
  margin: 14px 0;
  padding: 12px 14px;
  border-radius: 10px;
  background: linear-gradient(90deg, rgba(255,92,138,.1), rgba(124,92,255,.1));
  border: 1px solid var(--line);
  font-size: 13px;
  color: var(--ink);
  display: flex;
  align-items: center;
  gap: 8px;
}
.modal-actions { display: flex; justify-content: flex-end; gap: 10px; margin-top: 8px; }
.btn[disabled] { opacity: .55; cursor: not-allowed; }

/* ---------- Palette picker ---------- */
.modal-palette { max-width: 580px; }
.palette-search {
  width: 100%;
  padding: 11px 14px;
  border: 1px solid var(--line);
  border-radius: 10px;
  font-size: 15px;
  font-family: inherit;
  margin-bottom: 12px;
}
.palette-search:focus { outline: 2px solid var(--brand-2); outline-offset: 1px; border-color: transparent; }
.palette-list {
  max-height: 52vh;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  gap: 6px;
}
.palette-row {
  display: flex;
  align-items: center;
  gap: 12px;
  width: 100%;
  padding: 8px 10px;
  border: 1px solid var(--line);
  border-radius: 10px;
  background: #fff;
  cursor: pointer;
  text-align: left;
  font-family: inherit;
}
.palette-row:hover { border-color: var(--brand-2); box-shadow: 0 2px 8px rgba(124,92,255,.15); }
.palette-chips { display: flex; border-radius: 6px; overflow: hidden; flex: 0 0 auto; box-shadow: 0 0 0 1px var(--line); }
.palette-chips span { width: 24px; height: 24px; display: block; }
.palette-meta { display: flex; flex-direction: column; min-width: 0; }
.palette-name { font-weight: 700; font-size: 14px; color: var(--ink); }
.palette-cat { font-size: 11px; color: var(--muted); text-transform: uppercase; letter-spacing: .05em; }
.palette-empty { color: var(--muted); font-size: 14px; padding: 20px; text-align: center; }
.palette-count { font-size: 12px; color: var(--muted); margin: 0 0 8px; }
.palette-chevron { margin-left: auto; color: var(--muted); font-size: 20px; line-height: 1; }

/* Palette browse nav (breadcrumb + back) */
.palette-nav { display: flex; align-items: center; gap: 12px; margin-bottom: 10px; min-height: 0; }
.palette-nav:empty { display: none; }
.palette-back {
  border: 1px solid var(--line);
  background: var(--bg);
  color: var(--ink);
  border-radius: 999px;
  padding: 6px 14px;
  font-size: 13px;
  font-weight: 700;
  font-family: inherit;
  cursor: pointer;
}
.palette-back:hover { border-color: var(--brand-2); }
.palette-crumbs { font-size: 13px; color: var(--muted); font-weight: 600; }

/* ---------- Custom confetti emoji picker ---------- */
.btn-mini { padding: 6px 12px; font-size: 13px; }
.emoji-input {
  width: 100%;
  border: 1px solid var(--line);
  border-radius: 10px;
  padding: 10px 12px;
  font-size: 20px;
  font-family: inherit;
  margin-bottom: 12px;
}
.emoji-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(40px, 1fr));
  gap: 4px;
  max-height: 40vh;
  overflow-y: auto;
  margin-bottom: 12px;
}
.emoji-cell {
  aspect-ratio: 1;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #fff;
  font-size: 22px;
  line-height: 1;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
}
.emoji-cell:hover { border-color: var(--brand-2); background: var(--bg); }

/* ---------- E-card link modal ---------- */
.ecard-link-row { display: flex; gap: 8px; margin: 8px 0 4px; }
.ecard-url {
  flex: 1;
  border: 1px solid var(--line);
  border-radius: 10px;
  padding: 10px 12px;
  font-size: 14px;
  font-family: monospace;
  color: var(--ink);
  background: var(--bg);
}

/* ---------- E-card viewer ---------- */
.viewer { display: flex; align-items: center; justify-content: center; min-height: 100vh; padding: 20px; }
.view-stage { display: flex; flex-direction: column; align-items: center; gap: 18px; }
.view-frame { width: min(440px, 92vw, calc((100dvh - 140px) * 5 / 7)); }
.view-cta { text-decoration: none; }
.view-msg { color: var(--muted); font-size: 16px; }

/* ---------- Print: only the card ---------- */
@media print {
  body { background: #fff; }
  .topbar, .toolbar, .inspector, .stage-actions, .stage-hint, .modal-backdrop { display: none !important; }
  .layout { display: block; padding: 0; }
  .card-frame { box-shadow: none; width: 100%; max-width: 100%; }
  .item { outline: none !important; }
  .item .handle, .item .item-trash { display: none !important; }
}
