/* Dark theme tokens, after shadcn/ui's neutral dark palette. */
:root {
  color-scheme: dark;
  --background: #171717;
  --surface: #171717;
  --muted: #262626;
  --muted-hover: #2e2e2e;
  --border: rgba(255, 255, 255, 0.1);
  --border-strong: rgba(255, 255, 255, 0.15);
  --foreground: #fafafa;
  --muted-foreground: #a1a1a1;
  --primary: #e5e5e5;
  --primary-hover: #fafafa;
  --primary-foreground: #171717;
  --ring: rgba(255, 255, 255, 0.3);
  --radius: 8px;
  --font-mono: 'SF Mono', Menlo, monospace;
}

* {
  box-sizing: border-box;
}

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

input[type='range'] {
  accent-color: var(--primary);
}

body {
  margin: 0;
  height: 100vh;
  display: flex;
  flex-direction: column;
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
  background: var(--background);
  color: var(--foreground);
}

.tabbar {
  flex-shrink: 0;
  display: flex;
  gap: 6px;
  align-items: center;
  padding: 10px 16px;
  background: var(--background);
}

.tab-btn {
  background: transparent;
  color: var(--muted-foreground);
  border: none;
  border-radius: var(--radius);
  padding: 8px 14px;
  font-size: 13px;
  cursor: pointer;
}

.tab-btn:hover {
  background: var(--muted);
  color: var(--foreground);
}

.tab-btn.active {
  background: var(--muted);
  color: var(--foreground);
}

.export-btn {
  margin-left: auto;
  display: flex;
  align-items: center;
  gap: 6px;
  background: transparent;
  color: var(--foreground);
  border: 1px solid var(--border-strong);
  border-radius: var(--radius);
  padding: 7px 12px;
  font-size: 13px;
  cursor: pointer;
}

.export-btn:hover,
.export-btn.active {
  background: var(--muted);
}

.view {
  display: none;
  flex: 1;
  min-height: 0;
}

.view.active {
  display: flex;
}

.canvas-wrap {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 24px;
  position: relative;
  overflow: auto;
}

.toolbar {
  position: fixed;
  left: 50%;
  bottom: calc(16px + env(safe-area-inset-bottom));
  transform: translateX(-50%);
  z-index: 30;
  display: flex;
  gap: 4px;
  max-width: calc(100vw - 32px);
  overflow-x: auto;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 999px;
  padding: 6px;
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.5);
}

.toolbar-btn {
  flex-shrink: 0;
  background: transparent;
  color: var(--muted-foreground);
  border: none;
  border-radius: 999px;
  padding: 10px 16px;
  font-size: 13px;
  cursor: pointer;
  white-space: nowrap;
}

.toolbar-btn:hover {
  background: var(--muted);
  color: var(--foreground);
}

.toolbar-btn.active {
  background: var(--primary);
  color: var(--primary-foreground);
}

.toolbar-icon-btn {
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 10px 12px;
}

.toolbar-btn.primary-action {
  background: var(--primary);
  color: var(--primary-foreground);
  font-weight: 500;
}

.toolbar-btn.primary-action:hover {
  background: var(--primary-hover);
}

.popover {
  position: fixed;
  left: 50%;
  bottom: calc(16px + env(safe-area-inset-bottom) + 56px);
  transform: translateX(-50%);
  z-index: 30;
  width: min(360px, calc(100vw - 32px));
  max-height: 33vh;
  overflow-y: auto;
  background: var(--surface);
  color: var(--foreground);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 16px;
  display: flex;
  flex-direction: column;
  gap: 16px;
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.5);
  opacity: 1;
  transition: opacity 0.1s ease;
}

/* Phone widths: tighten the bottom toolbar so all six controls fit
   without sideways scrolling. */
@media (max-width: 480px) {
  .toolbar {
    max-width: calc(100vw - 16px);
    gap: 2px;
    padding: 4px;
  }

  .toolbar-btn {
    padding: 9px 8px;
    font-size: 12px;
  }

  .toolbar-icon-btn {
    padding: 9px 8px;
  }
}

.popover.popover-top {
  top: 56px;
  bottom: auto;
  left: auto;
  right: 16px;
  transform: none;
  width: 220px;
  gap: 0;
}

.popover[hidden] {
  display: none;
}

#pattern-svg {
  max-width: 100%;
  max-height: 100%;
  /* drop-shadow (not box-shadow) follows the painted artwork, so it hugs
     the canvas rather than the letterboxed <svg> box around it. */
  filter: drop-shadow(0 18px 40px rgba(0, 0, 0, 0.7)) drop-shadow(0 4px 10px rgba(0, 0, 0, 0.45));
  cursor: pointer;
}

.cell-editor-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.cell-editor-header h2 {
  font-size: 14px;
  margin: 0;
}

.cell-editor-close {
  background: transparent;
  border: none;
  color: var(--foreground);
  font-size: 20px;
  line-height: 1;
  cursor: pointer;
  padding: 2px 8px;
  border-radius: 4px;
}

.cell-editor-close:hover {
  background: var(--muted);
}

.cell-filled-toggle {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 13px;
  cursor: pointer;
}

.cell-filled-toggle input {
  width: 16px;
  height: 16px;
  cursor: pointer;
  accent-color: var(--primary);
}

#cell-editor-details {
  display: flex;
  flex-direction: column;
  gap: 16px;
}

.layer-group {
  display: flex;
  flex-direction: column;
  gap: 10px;
  padding: 14px;
  background: rgba(255, 255, 255, 0.03);
  border: 1px solid var(--border);
  border-radius: 8px;
}

.layer-group-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  font-size: 12px;
  font-weight: 600;
  color: var(--foreground);
}

.remove-layer-btn {
  background: transparent;
  border: none;
  color: var(--muted-foreground);
  font-size: 16px;
  line-height: 1;
  cursor: pointer;
  padding: 2px 6px;
  border-radius: 4px;
}

.remove-layer-btn:hover {
  background: var(--muted);
  color: var(--foreground);
}

.add-layer-btn {
  width: 100%;
  background: var(--muted);
  color: var(--foreground);
  border: 1px dashed var(--border-strong);
  border-radius: 6px;
  padding: 8px;
  font-size: 13px;
  cursor: pointer;
}

.add-layer-btn:hover {
  background: var(--muted-hover);
}

.add-layer-btn[hidden] {
  display: none;
}

.picker-card img {
  width: 100%;
  height: 100%;
  object-fit: cover;
  border-radius: 4px;
}

.section-label {
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--muted-foreground);
  margin: 0 0 8px;
}

.section-label-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.section-label-row .section-label {
  margin: 0 0 8px;
}

.icon-btn {
  position: relative;
  display: flex;
  align-items: center;
  justify-content: center;
  width: 22px;
  height: 22px;
  margin-bottom: 8px;
  background: var(--muted);
  color: var(--muted-foreground);
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 0;
  cursor: pointer;
}

.icon-btn:hover {
  background: var(--muted-hover);
  color: var(--foreground);
}

.icon-btn::after {
  content: attr(data-tooltip);
  position: absolute;
  bottom: calc(100% + 6px);
  right: 0;
  background: var(--surface);
  color: var(--foreground);
  font-size: 11px;
  line-height: 1;
  white-space: nowrap;
  padding: 4px 8px;
  border: 1px solid var(--border);
  border-radius: 4px;
  opacity: 0;
  visibility: hidden;
  transition: opacity 0.1s ease;
  pointer-events: none;
  z-index: 10;
}

.icon-btn:hover::after,
.icon-btn:focus-visible::after {
  opacity: 1;
  visibility: visible;
}

.button-group {
  display: flex;
  gap: 6px;
}

.button-group button,
.action-row button {
  flex: 1;
  background: var(--muted);
  color: var(--foreground);
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 8px 6px;
  font-size: 13px;
  cursor: pointer;
}

.button-group button:hover,
.action-row button:hover {
  background: var(--muted-hover);
}

.button-group button.active {
  background: var(--primary);
  color: var(--primary-foreground);
  border-color: var(--primary);
}

.slider-row {
  display: flex;
  align-items: center;
  gap: 10px;
}

.slider-row input[type='range'] {
  flex: 1;
}

.slider-row output {
  font-size: 12px;
  color: var(--muted-foreground);
  min-width: 56px;
  text-align: right;
  font-family: var(--font-mono);
}

.action-row {
  display: flex;
  gap: 6px;
}

.picker-grid {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 6px;
}

/* The Shapes popover packs all shapes into three rows so it never needs
   to scroll; icons scale with the card instead of staying a fixed 28px. */
.picker-grid-dense {
  grid-template-columns: repeat(7, minmax(0, 1fr));
  gap: 5px;
}

.picker-grid-dense .picker-card {
  padding: 5px;
}

.picker-grid-dense .picker-card svg {
  width: 100%;
  height: 100%;
}

.picker-card {
  aspect-ratio: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  background: var(--muted);
  border: 2px solid transparent;
  border-radius: 6px;
  padding: 6px;
  cursor: pointer;
}

.picker-card:hover {
  background: var(--muted-hover);
}

.picker-card.selected {
  border-color: var(--primary);
  background: var(--muted-hover);
}

.palette-list {
  display: flex;
  flex-direction: column;
  gap: 8px;
  margin-bottom: 14px;
}

.palette-current {
  display: flex;
  align-items: center;
  gap: 10px;
  width: 100%;
  background: var(--muted);
  color: var(--foreground);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 8px 10px;
  font: inherit;
  font-size: 13px;
  text-align: left;
  cursor: pointer;
}

.palette-current:hover {
  background: var(--muted-hover);
}

.palette-current-name {
  flex: 1;
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.palette-chevron {
  width: 7px;
  height: 7px;
  margin: 0 2px 3px 2px;
  border-right: 1.5px solid var(--muted-foreground);
  border-bottom: 1.5px solid var(--muted-foreground);
  transform: rotate(45deg);
  transition: transform 0.15s ease;
}

.palette-current[aria-expanded='true'] .palette-chevron {
  margin-bottom: -3px;
  transform: rotate(-135deg);
}

.palette-grid {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 6px;
}

.palette-grid[hidden] {
  display: none;
}

.palette-option {
  display: flex;
  flex-direction: column;
  gap: 6px;
  min-width: 0;
  background: var(--muted);
  color: var(--foreground);
  border: 2px solid transparent;
  border-radius: 6px;
  padding: 6px;
  font: inherit;
  font-size: 11px;
  text-align: left;
  cursor: pointer;
}

.palette-option:hover {
  background: var(--muted-hover);
}

.palette-option.selected {
  border-color: var(--primary);
  background: var(--muted-hover);
}

.palette-option-name {
  line-height: 1.25;
  overflow-wrap: anywhere;
}

.palette-strip {
  display: flex;
  flex-shrink: 0;
  border-radius: 4px;
  overflow: hidden;
  border: 1px solid var(--border-strong);
}

.palette-strip span {
  flex: 1;
  min-width: 10px;
  height: 14px;
}

.palette-option .palette-strip span {
  min-width: 0;
  height: 18px;
}

.picker-color-swatch {
  width: 24px;
  height: 24px;
  border-radius: 50%;
  border: 1px solid var(--border-strong);
}

.reference {
  overflow-y: auto;
  justify-content: center;
}

.reference-inner {
  width: 100%;
  max-width: 900px;
  padding: 32px 24px 64px;
}

.reference-inner h2 {
  font-size: 18px;
  margin: 32px 0 16px;
}

.reference-inner section:first-child h2 {
  margin-top: 0;
}

.ref-hint {
  margin: -8px 0 16px;
  font-size: 12px;
  color: var(--muted-foreground);
}

.ref-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(180px, 1fr));
  gap: 12px;
}

.ref-shapes-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(220px, 1fr));
  gap: 12px;
}

.ref-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 16px;
  transition: opacity 0.15s;
}

.ref-card.is-disabled,
.ref-colour-chip.is-disabled {
  opacity: 0.4;
}

.ref-card-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
}

.ref-card h3 {
  font-size: 13px;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  color: var(--muted-foreground);
  margin: 0 0 10px;
}

.ref-card-header h3 {
  margin: 0;
}

.ref-toggle input {
  width: 16px;
  height: 16px;
  cursor: pointer;
  accent-color: var(--primary);
}

.ref-card ul {
  margin: 0;
  padding: 0;
  list-style: none;
}

.ref-card ul li {
  font-size: 13px;
  padding: 2px 0;
}

.ref-card p {
  margin: 0;
  font-size: 13px;
  color: var(--foreground);
}

.ref-rotations {
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
}

.ref-swatch {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 4px;
}

.ref-swatch svg {
  border: 1px solid var(--border);
  border-radius: 4px;
}

.ref-swatch span {
  font-size: 11px;
  color: var(--muted-foreground);
  font-family: var(--font-mono);
}

.ref-meta {
  margin-top: 10px;
  font-size: 12px;
  color: var(--muted-foreground);
}

.ref-colours-row {
  display: flex;
  gap: 16px;
  flex-wrap: wrap;
}

.ref-colour-chip {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 6px;
}

.ref-colour-swatch {
  width: 64px;
  height: 64px;
  border-radius: 8px;
  border: 1px solid var(--border);
}

.ref-colour-chip span {
  font-size: 12px;
  font-family: var(--font-mono);
  color: var(--muted-foreground);
}

