:root {
  --bg: #F5F6EF;
  --bg-soft: #EEEFE5;
  --surface: #FFFFFF;
  --border: #E2E3D6;
  --ink: #15160D;
  --ink-muted: #63665A;
  --ink-faint: #9A9C8D;
  --accent: #C9DA1E;
  --accent-solid: #DFF314;
  --accent-ink: #15160D;

  --radius-card: 14px;
  --shadow-card: 0 1px 2px rgba(0, 0, 0, 0.06), 0 8px 24px -12px rgba(0, 0, 0, 0.18);

  /* the topbar is a constant dark chrome bar, independent of light/dark theme */
  --topbar-bg: #15160D;
  --topbar-ink: #F5F6EF;
  --topbar-border: rgba(245, 246, 239, 0.18);
  --topbar-glow: none;

  --font-heading: 'Piazzolla', Georgia, 'Times New Roman', serif;
  --font-body: 'DM Sans', system-ui, sans-serif;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #171717;
    --bg-soft: #1D1D1B;
    --surface: #1F1F1D;
    --border: #333330;
    --ink: #F5F5F0;
    --ink-muted: #9C9C93;
    --ink-faint: #6E6E66;
    --accent: #DFF314;
    --accent-solid: #DFF314;
    --accent-ink: #14150D;
    --shadow-card: 0 1px 2px rgba(0, 0, 0, 0.4), 0 8px 24px -12px rgba(0, 0, 0, 0.6);
    --topbar-glow: radial-gradient(ellipse 520px 240px at 20% -30%, color-mix(in srgb, var(--accent-solid) 15%, transparent), transparent 70%);
  }
}

:root[data-theme="dark"] {
  --bg: #171717;
  --bg-soft: #1D1D1B;
  --surface: #1F1F1D;
  --border: #333330;
  --ink: #F5F5F0;
  --ink-muted: #9C9C93;
  --ink-faint: #6E6E66;
  --accent: #DFF314;
  --accent-solid: #DFF314;
  --accent-ink: #14150D;
  --shadow-card: 0 1px 2px rgba(0, 0, 0, 0.4), 0 8px 24px -12px rgba(0, 0, 0, 0.6);
  --topbar-glow: radial-gradient(ellipse 520px 240px at 20% -30%, color-mix(in srgb, var(--accent-solid) 15%, transparent), transparent 70%);
}

* {
  box-sizing: border-box;
}

html, body {
  margin: 0;
  height: 100%;
  font-family: var(--font-body);
  background: var(--bg);
  color: var(--ink);
}

h1, h2, h3, h4 {
  font-family: var(--font-heading);
}

button {
  cursor: pointer;
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--ink);
  border-radius: 8px;
  padding: 6px 12px;
  font-size: 13px;
  font-family: var(--font-body);
}

button:hover {
  background: var(--bg-soft);
}

button:focus-visible,
input:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}

button:focus:not(:focus-visible) {
  outline: none;
}

input, select {
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--ink);
  border-radius: 6px;
  padding: 6px 10px;
  font-size: 13px;
  font-family: var(--font-body);
}

.mobile-banner {
  display: none;
  text-align: center;
  font-size: 11px;
  font-weight: 700;
  padding: 10px 12px;
  background: var(--accent-solid);
  color: var(--accent-ink);
  max-height: 100px;
  overflow: hidden;
  transition: opacity 0.4s ease, max-height 0.4s ease, padding 0.4s ease;
}

.mobile-banner.is-dismissed {
  opacity: 0;
  max-height: 0;
  padding-top: 0;
  padding-bottom: 0;
}

.topbar {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 10px 20px;
  background-color: var(--topbar-bg);
  background-image: var(--topbar-glow);
  background-repeat: no-repeat;
  color: var(--topbar-ink);
  height: 56px;
  position: relative;
}

.topbar-brand {
  display: flex;
  align-items: center;
  gap: 10px;
  order: 0;
}

.brand-mark {
  width: 26px;
  height: 26px;
  flex-shrink: 0;
  display: block;
}

.brand-word {
  font-family: var(--font-heading);
  font-weight: 600;
  font-size: 17px;
  letter-spacing: 0.01em;
  color: var(--topbar-ink);
}

/* Desktop visual order: brand, then the project controls hugged to the
   right (via margin-left: auto on the first one), theme/help last. On
   mobile these are reset to `initial` so DOM order (see index.html) takes
   over and groups them into rows instead. */
#project-name { order: 1; margin-left: auto; }
#btn-new { order: 2; }
#btn-save { order: 3; }
#project-dropdown { order: 4; }
#btn-export { order: 5; }
#btn-export-image { order: 6; }
#btn-theme-toggle { order: 7; }
#btn-help { order: 8; }

.topbar-row-break {
  display: none;
}

.topbar #project-name {
  width: 200px;
}

.topbar input {
  background: rgba(245, 246, 239, 0.08);
  border-color: var(--topbar-border);
  color: var(--topbar-ink);
}

.topbar input::placeholder {
  color: rgba(245, 246, 239, 0.5);
}

.topbar input:-webkit-autofill,
.topbar input:-webkit-autofill:hover,
.topbar input:-webkit-autofill:focus {
  -webkit-text-fill-color: var(--topbar-ink);
  -webkit-box-shadow: 0 0 0 1000px var(--topbar-bg) inset;
  box-shadow: 0 0 0 1000px var(--topbar-bg) inset;
  caret-color: var(--topbar-ink);
}

.dropdown {
  position: relative;
}

.dropdown-toggle {
  display: flex;
  align-items: center;
  gap: 8px;
  background: rgba(245, 246, 239, 0.08);
  border-color: var(--topbar-border);
  color: var(--topbar-ink);
  min-width: 150px;
  justify-content: space-between;
}

.dropdown-toggle:hover {
  background: rgba(245, 246, 239, 0.16);
}

.dropdown-toggle svg {
  flex-shrink: 0;
  transition: transform 0.15s;
}

.dropdown.open .dropdown-toggle svg {
  transform: rotate(180deg);
}

.dropdown-menu {
  position: absolute;
  top: calc(100% + 6px);
  left: 0;
  min-width: 220px;
  max-height: 320px;
  overflow-y: auto;
  list-style: none;
  margin: 0;
  padding: 6px;
  background: var(--topbar-bg);
  border: 1px solid var(--topbar-border);
  border-radius: 10px;
  box-shadow: 0 12px 28px rgba(0, 0, 0, 0.35);
  z-index: 20;
}

.dropdown-menu li {
  padding: 9px 12px;
  border-radius: 6px;
  font-size: 13px;
  color: var(--topbar-ink);
  cursor: pointer;
  white-space: nowrap;
}

.dropdown-menu li:hover {
  background: rgba(245, 246, 239, 0.1);
}

.dropdown-menu li.selected {
  color: var(--accent-solid);
}

.dropdown-menu .dropdown-empty {
  color: rgba(245, 246, 239, 0.5);
  cursor: default;
}

.dropdown-menu .dropdown-empty:hover {
  background: none;
}

.btn {
  border-radius: 40px;
  padding: 9px 20px;
  font-weight: 700;
  font-size: 12.5px;
  text-transform: uppercase;
  letter-spacing: 0.04em;
}

.btn-icon {
  display: none;
}

.btn-primary {
  background: var(--accent-solid);
  color: var(--accent-ink);
  border-color: var(--accent-solid);
}

.btn-primary:hover {
  background: var(--accent);
  border-color: var(--accent);
}

.btn-ghost {
  background: transparent;
  color: var(--topbar-ink);
  border-color: var(--topbar-border);
}

.btn-ghost:hover {
  background: rgba(245, 246, 239, 0.08);
}

.theme-toggle {
  width: 32px;
  height: 32px;
  border-radius: 50%;
  padding: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  background: transparent;
  border: 1px solid var(--topbar-border);
  color: var(--topbar-ink);
}

.theme-toggle:hover {
  background: rgba(245, 246, 239, 0.08);
}

.theme-toggle .icon-sun {
  display: none;
}

:root[data-theme="dark"] .theme-toggle .icon-sun {
  display: block;
}

:root[data-theme="dark"] .theme-toggle .icon-moon {
  display: none;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) .theme-toggle .icon-sun {
    display: block;
  }

  :root:not([data-theme="light"]) .theme-toggle .icon-moon {
    display: none;
  }
}

.layout {
  display: grid;
  grid-template-columns: 220px 340px 1fr;
  height: calc(100vh - 56px);
}

.col {
  overflow-y: auto;
  padding: 16px;
}

.col-categories {
  background: var(--surface);
  border-right: 1px solid var(--border);
}

.col-categories #category-search {
  width: 100%;
  margin-bottom: 12px;
}

.category-select-label {
  display: none;
  font-size: 13px;
  font-weight: 600;
  color: var(--ink-muted);
  margin-bottom: 6px;
}

.category-select {
  display: none;
  width: 100%;
}

.category-list {
  list-style: none;
  margin: 0;
  padding: 0;
}

.category-list li {
  padding: 8px 10px;
  border-radius: 6px;
  cursor: pointer;
  font-size: 14px;
}

.category-list li:hover {
  background: var(--bg-soft);
}

.category-list li.active {
  background: var(--ink);
  color: var(--bg);
}

.category-list .count {
  color: var(--ink-faint);
  font-size: 12px;
  float: right;
}

.col-variants {
  background: var(--bg-soft);
  border-right: 1px solid var(--border);
}

.col-variants h2, .col-canvas h2 {
  font-size: 14px;
  text-transform: uppercase;
  letter-spacing: 0.5px;
  color: var(--ink-faint);
  margin: 0;
}

.variants-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 12px;
  gap: 12px;
}

.col-toggle {
  display: flex;
  gap: 4px;
  flex-shrink: 0;
}

.col-toggle button {
  width: 28px;
  height: 28px;
  padding: 0;
  border-radius: 6px;
  font-size: 12px;
}

.col-toggle button.active {
  background: var(--ink);
  color: var(--bg);
  border-color: var(--ink);
}

.variants-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 10px;
}

.variants-grid.cols-1 {
  grid-template-columns: 1fr;
}

.variants-grid.cols-2 {
  grid-template-columns: 1fr 1fr;
}

.variant-card {
  border: 1px solid var(--border);
  border-radius: var(--radius-card);
  overflow: hidden;
  background: var(--surface);
  box-shadow: var(--shadow-card);
  cursor: pointer;
  transition: box-shadow 0.15s, transform 0.15s;
}

.variant-card:hover {
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1), 0 14px 32px -14px rgba(0, 0, 0, 0.3);
  transform: translateY(-2px);
}

.variant-thumb {
  position: relative;
}

.variant-card img {
  width: 100%;
  display: block;
  background: var(--bg-soft);
}

.preview-btn {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%) scale(0.85);
  width: 42px;
  height: 42px;
  border-radius: 50%;
  padding: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  background: var(--surface);
  color: var(--ink);
  border: none;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.25);
  opacity: 0;
  transition: opacity 0.15s, transform 0.15s;
  pointer-events: none;
}

.variant-card:hover .preview-btn {
  opacity: 1;
  transform: translate(-50%, -50%) scale(1);
  pointer-events: auto;
}

.variant-card .variant-id {
  font-size: 11px;
  color: var(--ink-muted);
  padding: 4px 6px;
}

.variant-card.no-json .variant-id::after {
  content: " (no exportable)";
  color: #c0392b;
}

.lightbox {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.75);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 1000;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.15s;
}

.lightbox.visible {
  opacity: 1;
  pointer-events: auto;
}

.lightbox img {
  max-width: 85vw;
  max-height: 85vh;
  border-radius: var(--radius-card);
  box-shadow: 0 10px 40px rgba(0, 0, 0, 0.4);
  background: var(--surface);
}

.modal {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.55);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 1000;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.15s;
}

.modal.visible {
  opacity: 1;
  pointer-events: auto;
}

.modal-card {
  width: min(520px, 90vw);
  max-height: 80vh;
  overflow-y: auto;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-card);
  box-shadow: var(--shadow-card), 0 20px 50px rgba(0, 0, 0, 0.25);
  padding: 24px 28px;
}

.modal-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 14px;
}

.modal-header h3 {
  font-size: 19px;
}

.modal-close {
  width: 30px;
  height: 30px;
  border-radius: 50%;
  padding: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--ink);
}

.modal-close:hover {
  background: var(--bg-soft);
}

.help-list {
  margin: 0;
  padding-left: 20px;
  display: flex;
  flex-direction: column;
  gap: 12px;
  font-size: 14px;
  color: var(--ink-muted);
  line-height: 1.5;
}

.help-list li::marker {
  color: var(--ink-faint);
  font-weight: 700;
}

.help-list strong {
  color: var(--ink);
}

.help-list code {
  background: var(--bg-soft);
  border-radius: 4px;
  padding: 1px 6px;
  font-size: 13px;
}

.col-canvas {
  background: var(--bg-soft);
}

.canvas {
  display: flex;
  flex-direction: column;
  gap: 4px;
  max-width: 960px;
}

.canvas-empty {
  color: var(--ink-faint);
  font-size: 14px;
  padding: 40px 0;
  text-align: center;
}

.canvas-block {
  position: relative;
  background: var(--surface);
  cursor: grab;
}

.canvas-block .block-buttons button {
  cursor: pointer;
}

.canvas-block.dragging {
  opacity: 0.4;
}

.drop-indicator {
  height: 0;
  margin: 6px 0;
  border-top: 2px dashed var(--accent);
}

.canvas-block img {
  width: 100%;
  display: block;
}

.canvas-block .block-controls {
  position: absolute;
  top: 10px;
  right: 10px;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 6px;
  background: color-mix(in srgb, var(--surface) 92%, transparent);
  border-radius: 20px;
  padding: 8px 10px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.15);
  cursor: default;
}

.canvas-block:hover .block-controls {
  flex-direction: row;
}

.canvas-block .block-id {
  font-size: 11px;
  font-weight: 700;
  color: var(--ink);
}

.canvas-block .block-buttons {
  display: none;
  flex-direction: row;
  align-items: center;
  gap: 6px;
}

.canvas-block:hover .block-buttons {
  display: flex;
}

.canvas-block .block-buttons button {
  width: 26px;
  height: 26px;
  border-radius: 50%;
  padding: 0;
  line-height: 1;
  font-size: 14px;
}

.canvas-block.no-json {
  outline: 2px dashed #c0392b;
}

@media (max-width: 900px) {
  .mobile-banner {
    display: block;
  }

  .topbar {
    flex-wrap: wrap;
    height: auto;
    row-gap: 5px;
  }

  .topbar-brand,
  #project-name,
  #btn-new,
  #btn-save,
  #project-dropdown,
  #btn-export,
  #btn-export-image,
  #btn-theme-toggle,
  #btn-help {
    order: initial;
  }

  .topbar-row-break {
    display: block;
    flex-basis: 100%;
    width: 0;
    height: 0;
  }

  .topbar-brand {
    flex: 1 1 auto;
  }

  .topbar #project-name {
    width: auto;
    min-width: 0;
    margin-left: 0;
    flex: 1 1 auto;
  }

  #btn-new,
  #btn-save {
    flex: 0 0 auto;
    width: 40px;
    height: 40px;
    padding: 0;
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
  }

  #btn-new .btn-label,
  #btn-save .btn-label {
    display: none;
  }

  #btn-new .btn-icon,
  #btn-save .btn-icon {
    display: block;
    width: 16px;
    height: 16px;
  }

  /* The dropdown + the two export buttons need to share one row on a
     phone-width screen, so they shrink and stop growing to content size. */
  #project-dropdown,
  #btn-export,
  #btn-export-image {
    flex: 1 1 0;
    min-width: 0;
  }

  #project-dropdown .dropdown-toggle {
    min-width: 0;
    width: 100%;
    padding: 8px 4px;
    font-size: 10px;
  }

  #btn-export,
  #btn-export-image {
    padding: 8px 0px;
    font-size: 8.5px;
    white-space: normal;
    text-align: center;
  }

  .layout {
    grid-template-columns: 1fr;
    height: auto;
  }

  .col-categories,
  .col-variants {
    border-right: none;
    border-bottom: 1px solid var(--border);
  }

  .col-variants {
    max-height: none;
  }

  .col-canvas {
    max-height: 65vh;
  }

  #category-search,
  #category-list {
    display: none;
  }

  .category-select-label,
  .category-select {
    display: block;
  }

  .col-toggle {
    display: none;
  }

  .variants-grid,
  .variants-grid.cols-1,
  .variants-grid.cols-2 {
    display: flex;
    flex-wrap: nowrap;
    overflow-x: auto;
    scroll-snap-type: x proximity;
    padding-bottom: 8px;
  }

  .variant-card {
    flex: 0 0 150px;
    width: 150px;
    scroll-snap-align: start;
  }
}

@media (max-width: 480px) {
  .variants-grid.cols-2 {
    grid-template-columns: 1fr;
  }
}
