/* ============================================================
   Screenshot Beautifier — Material Design 3 Style System
   ------------------------------------------------------------
   Palette: "Iris & Plum" — an indigo-violet primary paired with
   a muted teal secondary and a rose-plum tertiary accent, tying
   the app chrome to the gradient presets it lets you paint with.

   Sections in this file:
     1. Design tokens (light + dark)
     2. Base reset & global elements (splash, top app bar, buttons)
     3. Canvas / preview panel
     4. Controls panel (desktop side panel + mobile bottom sheet)
     5. Shared form controls (sliders, switches, swatches, inputs)
     6. Overlays (toast, dialogs, export spinner)
     7. PWA install banner + mobile FAB
     8. Responsive rules
   ============================================================ */

@import url('https://fonts.googleapis.com/css2?family=Be+Vietnam+Pro:wght@300;400;500;600;700;800&family=JetBrains+Mono:wght@400;500&display=swap');

/* ── 1. Design tokens ───────────────────────────────────────── */
:root {
  /* Primary — Iris (indigo-violet) */
  --md-primary: #5457E5;
  --md-on-primary: #FFFFFF;
  --md-primary-container: #E1E0FF;
  --md-on-primary-container: #0E0B78;

  /* Secondary — muted teal, used for tonal chips/badges */
  --md-secondary: #386660;
  --md-on-secondary: #FFFFFF;
  --md-secondary-container: #BBECE3;
  --md-on-secondary-container: #00201C;

  /* Tertiary — rose/plum, used sparingly as a warm accent */
  --md-tertiary: #984061;
  --md-on-tertiary: #FFFFFF;
  --md-tertiary-container: #FFD9E3;
  --md-on-tertiary-container: #3E0021;

  /* Surfaces — near-white with a faint violet tint */
  --md-surface: #FCFBFF;
  --md-surface-1: #F3F1FE;
  --md-surface-2: #ECE9FD;
  --md-surface-3: #E4E1FB;
  --md-surface-variant: #E4E1EC;
  --md-on-surface: #1B1B21;
  --md-on-surface-variant: #46464F;
  --md-outline: #767680;
  --md-outline-variant: #C7C5D0;

  --md-background: #FCFBFF;
  --md-on-background: #1B1B21;

  --md-error: #BA1A1A;
  --md-on-error: #FFFFFF;
  --md-error-container: #FFDAD6;

  /* Brand gradient — reused for the logo mark, splash icon, install banner */
  --brand-gradient: linear-gradient(135deg, #5457E5 0%, #984061 100%);

  /* Elevation shadows */
  --elevation-1: 0 1px 2px rgba(20,10,60,.06), 0 1px 3px rgba(20,10,60,.1);
  --elevation-2: 0 2px 4px rgba(20,10,60,.08), 0 4px 10px rgba(20,10,60,.1);
  --elevation-3: 0 6px 10px rgba(20,10,60,.1), 0 10px 24px rgba(20,10,60,.14);

  /* Typography */
  --font-main: 'Be Vietnam Pro', sans-serif;
  --font-mono: 'JetBrains Mono', monospace;

  /* Radius scale */
  --radius-xs: 4px;
  --radius-sm: 8px;
  --radius-md: 12px;
  --radius-lg: 16px;
  --radius-xl: 24px;
  --radius-xxl: 28px;
  --radius-full: 9999px;

  /* Motion */
  --trans-fast: 150ms cubic-bezier(0.2, 0, 0, 1);
  --trans-std: 250ms cubic-bezier(0.2, 0, 0, 1);
  --trans-slow: 400ms cubic-bezier(0.2, 0, 0, 1);
  --trans-sheet: 340ms cubic-bezier(0.2, 0, 0, 1);

  /* Layout */
  --top-bar-height: 64px;
  --controls-width: 340px;
  --sheet-peek: 56px;
  --safe-b: env(safe-area-inset-bottom, 0px);
}

/* ── Dark theme ── */
[data-theme="dark"] {
  --md-primary: #C4C1FF;
  --md-on-primary: #24229C;
  --md-primary-container: #3B39B4;
  --md-on-primary-container: #E1E0FF;

  --md-secondary: #A0D0C7;
  --md-on-secondary: #033730;
  --md-secondary-container: #1F4D47;
  --md-on-secondary-container: #BBECE3;

  --md-tertiary: #FFB1CA;
  --md-on-tertiary: #5E1235;
  --md-tertiary-container: #7C2A4A;
  --md-on-tertiary-container: #FFD9E3;

  --md-surface: #131318;
  --md-surface-1: #1C1B25;
  --md-surface-2: #22212E;
  --md-surface-3: #282735;
  --md-surface-variant: #46464F;
  --md-on-surface: #E5E1E9;
  --md-on-surface-variant: #C8C5D0;
  --md-outline: #918F9A;
  --md-outline-variant: #46464F;

  --md-background: #131318;
  --md-on-background: #E5E1E9;

  --md-error: #FFB4AB;
  --md-on-error: #690005;
  --md-error-container: #93000A;

  --brand-gradient: linear-gradient(135deg, #6D70FF 0%, #B65478 100%);

  --elevation-1: 0 1px 2px rgba(0,0,0,.35), 0 1px 3px rgba(0,0,0,.4);
  --elevation-2: 0 2px 4px rgba(0,0,0,.4), 0 4px 10px rgba(0,0,0,.45);
  --elevation-3: 0 6px 10px rgba(0,0,0,.45), 0 10px 24px rgba(0,0,0,.5);
}

/* ── 2. Base reset & global elements ───────────────────────── */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

html {
  font-size: 16px;
  scroll-behavior: smooth;
  -webkit-text-size-adjust: 100%;
}

body {
  font-family: var(--font-main);
  background: var(--md-background);
  color: var(--md-on-background);
  line-height: 1.5;
  min-height: 100dvh;
  overflow-x: hidden;
  transition: background var(--trans-std), color var(--trans-std);
}

/* Focus visibility kept for keyboard users across every interactive control */
button:focus-visible,
input:focus-visible,
a:focus-visible,
[tabindex]:focus-visible {
  outline: 2px solid var(--md-primary);
  outline-offset: 2px;
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation-duration: 0.01ms !important; transition-duration: 0.01ms !important; }
}

/* ── Splash Screen ── */
.splash {
  position: fixed; inset: 0;
  background: var(--md-background);
  display: flex; align-items: center; justify-content: center;
  z-index: 9999;
  transition: opacity var(--trans-slow), visibility var(--trans-slow);
}
.splash.hidden { opacity: 0; visibility: hidden; pointer-events: none; }
.splash-inner { display: flex; flex-direction: column; align-items: center; gap: 16px; }
.splash-icon {
  width: 72px; height: 72px;
  background: var(--brand-gradient);
  color: #fff;
  border-radius: var(--radius-xl);
  display: flex; align-items: center; justify-content: center;
  font-size: 36px;
  box-shadow: var(--elevation-2);
  animation: splashPulse 1.2s ease-in-out infinite;
}
.splash-icon .material-icons-round { font-size: 36px; }
@keyframes splashPulse {
  0%, 100% { transform: scale(1); }
  50% { transform: scale(1.06); }
}
.splash-title { font-size: 20px; font-weight: 700; color: var(--md-on-background); }
.splash-loader {
  width: 200px; height: 4px;
  background: var(--md-surface-variant);
  border-radius: var(--radius-full);
  overflow: hidden;
}
.splash-bar {
  height: 100%; width: 40%;
  background: var(--brand-gradient);
  border-radius: var(--radius-full);
  animation: splashLoad 1s ease-in-out forwards;
}
@keyframes splashLoad {
  0% { width: 0%; }
  60% { width: 80%; }
  100% { width: 100%; }
}

/* ── Top App Bar ── */
.top-app-bar {
  position: fixed; top: 0; left: 0; right: 0;
  height: var(--top-bar-height);
  background: color-mix(in srgb, var(--md-surface) 88%, transparent);
  backdrop-filter: blur(14px) saturate(1.4);
  -webkit-backdrop-filter: blur(14px) saturate(1.4);
  border-bottom: 1px solid var(--md-outline-variant);
  z-index: 100;
  transition: background var(--trans-std), box-shadow var(--trans-std);
}
.top-app-bar.scrolled { box-shadow: var(--elevation-2); }
.top-app-bar-inner {
  max-width: 100%;
  height: 100%;
  padding: 0 16px;
  padding-left: max(16px, env(safe-area-inset-left));
  padding-right: max(16px, env(safe-area-inset-right));
  display: flex; align-items: center; gap: 12px;
}
.top-app-bar-left { display: flex; align-items: center; gap: 12px; flex: 1; min-width: 0; }
.top-app-bar-right { display: flex; align-items: center; gap: 2px; }
.app-logo {
  width: 40px; height: 40px;
  background: var(--brand-gradient);
  color: #fff;
  border-radius: var(--radius-md);
  display: flex; align-items: center; justify-content: center;
  flex-shrink: 0;
  box-shadow: var(--elevation-1);
}
.app-logo .material-icons-round { font-size: 20px; }
.app-title {
  font-size: 18px; font-weight: 700;
  color: var(--md-on-surface);
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
  letter-spacing: -0.01em;
}

/* ── Icon Button ── */
.icon-btn {
  width: 40px; height: 40px;
  border: none; background: transparent;
  color: var(--md-on-surface-variant);
  border-radius: var(--radius-full);
  display: flex; align-items: center; justify-content: center;
  cursor: pointer;
  transition: background var(--trans-fast), color var(--trans-fast), transform var(--trans-fast);
  position: relative; overflow: hidden;
  flex-shrink: 0;
}
.icon-btn[hidden] { display: none; }
.icon-btn:hover { background: color-mix(in srgb, var(--md-on-surface) 8%, transparent); }
.icon-btn:active { background: color-mix(in srgb, var(--md-on-surface) 12%, transparent); transform: scale(0.94); }
.icon-btn.sm { width: 32px; height: 32px; }
.icon-btn .material-icons-round { font-size: 20px; }
.danger-btn { color: var(--md-error) !important; }

/* ── MD Buttons ── */
.md-btn {
  height: 40px;
  padding: 0 24px;
  border: none; border-radius: var(--radius-full);
  font-family: var(--font-main); font-size: 14px; font-weight: 600;
  display: inline-flex; align-items: center; gap: 8px;
  cursor: pointer;
  transition: all var(--trans-fast);
  white-space: nowrap;
  position: relative; overflow: hidden;
  letter-spacing: 0.01em;
}
.md-btn .material-icons-round { font-size: 18px; }
.md-btn-filled {
  background: var(--md-primary);
  color: var(--md-on-primary);
  box-shadow: var(--elevation-1);
}
.md-btn-filled:hover {
  box-shadow: var(--elevation-2);
  background: color-mix(in srgb, var(--md-primary) 90%, black);
}
.md-btn-filled:active { transform: scale(0.98); }
.md-btn-tonal {
  background: var(--md-secondary-container);
  color: var(--md-on-secondary-container);
}
.md-btn-tonal:hover { background: color-mix(in srgb, var(--md-secondary-container) 85%, var(--md-on-secondary-container)); }
.md-btn-outlined {
  background: transparent;
  color: var(--md-primary);
  border: 1px solid var(--md-outline);
}
.md-btn-outlined:hover { background: color-mix(in srgb, var(--md-primary) 8%, transparent); }
.md-btn-text {
  background: transparent;
  color: var(--md-primary);
  padding: 0 12px;
}
.md-btn-text:hover { background: color-mix(in srgb, var(--md-primary) 8%, transparent); }
.md-btn.full-width { width: 100%; justify-content: center; }
.md-btn.mt8 { margin-top: 8px; }

/* ── 3. Main Layout & Canvas Panel ─────────────────────────── */
.main-layout {
  margin-top: var(--top-bar-height);
  display: flex;
  height: calc(100dvh - var(--top-bar-height));
  overflow: hidden;
}

.canvas-panel {
  flex: 1;
  min-width: 0;
  display: flex;
  flex-direction: column;
  background: var(--md-surface-1);
  position: relative;
  overflow: hidden;
}

/* ── Upload Zone ── */
.upload-zone {
  flex: 1;
  display: flex; align-items: center; justify-content: center;
  cursor: pointer;
  transition: background var(--trans-fast);
  padding: 24px;
}
.upload-zone:hover { background: color-mix(in srgb, var(--md-primary) 4%, transparent); }
.upload-zone.drag-over {
  background: color-mix(in srgb, var(--md-primary) 10%, transparent);
  outline: 2px dashed var(--md-primary);
  outline-offset: -8px;
}
.upload-content {
  display: flex; flex-direction: column; align-items: center; gap: 12px;
  text-align: center; max-width: 380px;
}
.upload-icon-wrap {
  width: 84px; height: 84px;
  background: var(--brand-gradient);
  color: #fff;
  border-radius: var(--radius-xl);
  display: flex; align-items: center; justify-content: center;
  margin-bottom: 8px;
  box-shadow: var(--elevation-2);
}
.upload-icon-wrap .material-icons-round { font-size: 42px; }
.upload-title { font-size: 20px; font-weight: 700; color: var(--md-on-surface); }
.upload-sub { font-size: 14px; color: var(--md-on-surface-variant); }
.upload-hint {
  font-size: 12px; color: var(--md-outline);
  font-family: var(--font-mono);
}

/* ── Canvas Wrap ── */
.canvas-wrap { flex: 1; display: flex; flex-direction: column; overflow: hidden; }
.canvas-toolbar {
  display: flex; align-items: center; gap: 8px;
  padding: 10px 16px;
  background: var(--md-surface);
  border-bottom: 1px solid var(--md-outline-variant);
  flex-wrap: wrap;
}
.chip-btn {
  height: 32px; padding: 0 16px;
  border: 1px solid var(--md-outline-variant);
  border-radius: var(--radius-full);
  background: transparent;
  color: var(--md-on-surface-variant);
  font-size: 13px; font-family: var(--font-main); font-weight: 500;
  cursor: pointer;
  transition: all var(--trans-fast);
}
.chip-btn:hover { background: var(--md-surface-variant); }
.chip-btn.active {
  background: var(--md-secondary-container);
  color: var(--md-on-secondary-container);
  border-color: transparent;
}
.canvas-viewport {
  flex: 1;
  overflow: auto;
  display: flex;
  align-items: flex-start;
  justify-content: center;
  padding: 40px 40px 60px;
  background: repeating-conic-gradient(var(--md-surface-variant) 0% 25%, transparent 0% 50%) 0 0 / 20px 20px;
  min-height: 0; /* keep content from being clipped at the top */
}
.canvas-viewport::-webkit-scrollbar { width: 6px; height: 6px; }
.canvas-viewport::-webkit-scrollbar-track { background: transparent; }
.canvas-viewport::-webkit-scrollbar-thumb {
  background: var(--md-outline-variant);
  border-radius: var(--radius-full);
}
.canvas-stage { display: inline-block; margin: auto; }
.output-wrapper { display: inline-block; transition: all var(--trans-std); }
.output-bg {
  border-radius: var(--radius-md);
  overflow: hidden;
  transition: all var(--trans-std);
}
.output-frame {
  display: flex; flex-direction: column;
  overflow: hidden;
  transition: all var(--trans-std);
}
.output-image-wrap { line-height: 0; }
.output-image-wrap img {
  display: block; max-width: 100%;
  pointer-events: none; user-select: none;
}

/* ── Browser Bar (macOS-style frame) ── */
.browser-bar {
  display: flex; align-items: center; gap: 8px;
  padding: 10px 14px;
  background: #e8e8e8;
  flex-shrink: 0;
}
.browser-bar.dark-frame { background: #2d2d2d; }
.browser-bar-dots { display: flex; gap: 6px; }
.dot { width: 12px; height: 12px; border-radius: 50%; }
.dot-red { background: #FF5F57; }
.dot-yellow { background: #FFBD2E; }
.dot-green { background: #28C840; }
.browser-bar-url {
  flex: 1; height: 26px;
  background: rgba(0,0,0,.08);
  border-radius: var(--radius-full);
  display: flex; align-items: center; gap: 6px;
  padding: 0 10px;
  font-size: 11px;
  color: rgba(0,0,0,.5);
  font-family: var(--font-mono);
  overflow: hidden; white-space: nowrap;
}
.browser-bar.dark-frame .browser-bar-url {
  background: rgba(255,255,255,.08);
  color: rgba(255,255,255,.5);
}
.browser-bar-actions { color: rgba(0,0,0,.4); display: flex; align-items: center; }
.browser-bar.dark-frame .browser-bar-actions { color: rgba(255,255,255,.4); }

/* ── Phone Bar (notch) ── */
.phone-bar {
  height: 36px;
  background: #1a1a1a;
  display: flex; align-items: center; justify-content: center;
  flex-shrink: 0;
}
.phone-bar.light-frame { background: #f0f0f0; }
.phone-notch {
  width: 120px; height: 28px;
  background: #000;
  border-radius: 0 0 20px 20px;
}
.phone-bar.light-frame .phone-notch { background: #1a1a1a; }

/* ── 4. Controls Panel (desktop side panel) ────────────────── */
.controls-panel {
  width: var(--controls-width);
  flex-shrink: 0;
  background: var(--md-surface);
  border-left: 1px solid var(--md-outline-variant);
  display: flex; flex-direction: column;
  overflow: hidden;
  transition: background var(--trans-std);
}
.controls-scroll { flex: 1; overflow-y: auto; padding: 16px; min-height: 0; }
.controls-scroll::-webkit-scrollbar { width: 4px; }
.controls-scroll::-webkit-scrollbar-track { background: transparent; }
.controls-scroll::-webkit-scrollbar-thumb {
  background: var(--md-outline-variant);
  border-radius: var(--radius-full);
}

/* Drag handle for the mobile bottom sheet — invisible/inert on desktop */
.sheet-handle {
  display: none;
  width: 100%;
  border: none;
  background: transparent;
  font-family: var(--font-main);
  cursor: pointer;
}

/* ── Skeleton Loading ── */
.skeleton {
  background: linear-gradient(90deg,
    var(--md-surface-variant) 0%,
    color-mix(in srgb, var(--md-surface-variant) 60%, var(--md-surface)) 50%,
    var(--md-surface-variant) 100%
  );
  background-size: 200% 100%;
  animation: shimmer 1.4s infinite;
  border-radius: var(--radius-sm);
}
@keyframes shimmer {
  0% { background-position: -200% 0; }
  100% { background-position: 200% 0; }
}
.sk-header { height: 20px; width: 40%; margin-bottom: 12px; }
.sk-row { height: 36px; width: 100%; margin-bottom: 8px; }
.sk-row.short { width: 70%; }
.sk-swatch-row { display: flex; gap: 8px; }
.sk-swatch { width: 40px; height: 40px; border-radius: var(--radius-sm); }
.mt { margin-top: 20px; }

/* ── Control Section ── */
.ctrl-section {
  background: var(--md-surface-1);
  border-radius: var(--radius-lg);
  padding: 16px;
  margin-bottom: 12px;
  border: 1px solid var(--md-outline-variant);
  transition: background var(--trans-std);
}
.ctrl-section-header {
  display: flex; align-items: center; gap: 8px;
  font-size: 14px; font-weight: 700;
  color: var(--md-on-surface);
  margin-bottom: 14px;
}
.ctrl-section-header .material-icons-round {
  font-size: 18px; color: var(--md-primary);
}
.ctrl-row {
  display: flex; align-items: center; gap: 10px;
  margin-bottom: 10px;
}
.ctrl-row:last-child { margin-bottom: 0; }
.ctrl-label {
  font-size: 13px; color: var(--md-on-surface-variant); font-weight: 500;
  flex-shrink: 0; min-width: 80px;
}
.ctrl-right { flex: 1; display: flex; align-items: center; gap: 8px; }
.mt8 { margin-top: 8px; }

/* ── Tab Group ── */
.tab-group {
  display: flex; gap: 4px;
  background: var(--md-surface-variant);
  border-radius: var(--radius-md);
  padding: 4px;
  margin-bottom: 14px;
  flex-wrap: wrap;
}
.tab-btn {
  flex: 1; height: 32px;
  border: none; border-radius: var(--radius-sm);
  background: transparent;
  color: var(--md-on-surface-variant);
  font-size: 12px; font-family: var(--font-main); font-weight: 600;
  cursor: pointer;
  transition: all var(--trans-fast);
  white-space: nowrap;
}
.tab-btn.active {
  background: var(--md-surface);
  color: var(--md-primary);
  box-shadow: var(--elevation-1);
}
.tab-btn:hover:not(.active) { background: color-mix(in srgb, var(--md-surface) 50%, transparent); }

/* ── Segmented Button Group ── */
.segmented-group {
  display: flex; border-radius: var(--radius-full);
  border: 1px solid var(--md-outline);
  overflow: hidden;
}
.seg-btn {
  flex: 1; height: 32px; padding: 0 12px;
  border: none; border-right: 1px solid var(--md-outline);
  background: transparent;
  color: var(--md-on-surface-variant);
  font-size: 12px; font-family: var(--font-main); font-weight: 600;
  cursor: pointer;
  transition: all var(--trans-fast);
}
.seg-btn:last-child { border-right: none; }
.seg-btn.active {
  background: var(--md-secondary-container);
  color: var(--md-on-secondary-container);
}
.seg-btn:hover:not(.active) { background: var(--md-surface-variant); }

/* ── 5. Shared form controls ────────────────────────────────── */
.md-slider {
  flex: 1; height: 4px;
  -webkit-appearance: none;
  appearance: none;
  background: var(--md-surface-variant);
  border-radius: var(--radius-full);
  outline: none;
  cursor: pointer;
  transition: background var(--trans-fast);
}
.md-slider::-webkit-slider-thumb {
  -webkit-appearance: none;
  width: 20px; height: 20px;
  background: var(--md-primary);
  border-radius: 50%;
  box-shadow: 0 0 0 4px color-mix(in srgb, var(--md-primary) 20%, transparent);
  cursor: grab;
  transition: all var(--trans-fast);
}
.md-slider::-webkit-slider-thumb:active { cursor: grabbing; }
.md-slider:hover::-webkit-slider-thumb {
  box-shadow: 0 0 0 8px color-mix(in srgb, var(--md-primary) 12%, transparent);
}
.md-slider::-moz-range-thumb {
  width: 20px; height: 20px; border: none;
  background: var(--md-primary);
  border-radius: 50%;
  cursor: grab;
}

.value-badge {
  background: var(--md-secondary-container);
  color: var(--md-on-secondary-container);
  font-size: 11px; font-family: var(--font-mono); font-weight: 600;
  padding: 2px 8px;
  border-radius: var(--radius-full);
  min-width: 44px; text-align: center;
  flex-shrink: 0;
}

/* ── Color Picker ── */
.color-picker {
  width: 36px; height: 36px;
  padding: 2px; border-radius: var(--radius-sm);
  border: 2px solid var(--md-outline-variant);
  cursor: pointer; background: transparent;
  transition: border-color var(--trans-fast);
}
.color-picker:hover { border-color: var(--md-primary); }

/* ── MD Input ── */
.md-input {
  flex: 1; height: 36px;
  padding: 0 12px;
  background: var(--md-surface-variant);
  border: 1px solid var(--md-outline-variant);
  border-radius: var(--radius-sm);
  color: var(--md-on-surface);
  font-size: 13px; font-family: var(--font-mono);
  transition: all var(--trans-fast);
  outline: none;
}
.md-input:focus {
  border-color: var(--md-primary);
  background: color-mix(in srgb, var(--md-primary) 5%, var(--md-surface-variant));
}

/* ── Switch ── */
.md-switch { display: flex; align-items: center; cursor: pointer; }
.md-switch input { display: none; }
.md-switch-track {
  width: 52px; height: 32px;
  background: var(--md-surface-variant);
  border: 2px solid var(--md-outline);
  border-radius: var(--radius-full);
  position: relative;
  transition: all var(--trans-std);
  flex-shrink: 0;
}
.md-switch-track::after {
  content: '';
  position: absolute;
  top: 50%; left: 4px;
  transform: translateY(-50%);
  width: 20px; height: 20px;
  background: var(--md-outline);
  border-radius: 50%;
  transition: all var(--trans-std);
}
.md-switch input:checked + .md-switch-track {
  background: var(--md-primary);
  border-color: var(--md-primary);
}
.md-switch input:checked + .md-switch-track::after {
  left: calc(100% - 24px);
  background: var(--md-on-primary);
}
.md-switch.sm .md-switch-track { width: 40px; height: 24px; }
.md-switch.sm .md-switch-track::after { width: 14px; height: 14px; }
.md-switch.sm input:checked + .md-switch-track::after { left: calc(100% - 18px); }

/* ── Swatch Grid ── */
.swatch-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(36px, 1fr));
  gap: 8px;
  margin-bottom: 12px;
}
.swatch-item {
  width: 100%; aspect-ratio: 1;
  border-radius: var(--radius-sm);
  cursor: pointer;
  border: 2px solid transparent;
  transition: all var(--trans-fast);
  position: relative;
}
.swatch-item:hover { transform: scale(1.08); }
.swatch-item.active {
  border-color: var(--md-primary);
  box-shadow: var(--elevation-2), 0 0 0 2px var(--md-surface-1);
}
.swatch-item.active::after {
  content: '';
  position: absolute; inset: 0;
  border-radius: calc(var(--radius-sm) - 2px);
  background: rgba(255,255,255,.25);
}

/* ── Background Panels ── */
.bg-panel { display: block; }
.bg-panel.hidden { display: none; }
.frame-panel.hidden { display: none; }
.hidden { display: none !important; }

/* ── BG Image Preview ── */
.bg-image-preview {
  display: flex; align-items: center; gap: 8px;
  margin: 8px 0;
  padding: 8px;
  background: var(--md-surface-variant);
  border-radius: var(--radius-md);
}
.bg-image-preview img { width: 48px; height: 48px; object-fit: cover; border-radius: var(--radius-sm); }

.spacer { flex: 1; }

/* ── 6. Overlays: Toast, Dialogs, Export spinner ───────────── */
.toast-container {
  position: fixed;
  bottom: calc(24px + var(--safe-b));
  left: 50%;
  transform: translateX(-50%);
  display: flex; flex-direction: column; align-items: center; gap: 8px;
  z-index: 9000;
  pointer-events: none;
}
.toast {
  background: var(--md-on-surface);
  color: var(--md-surface);
  padding: 12px 20px;
  border-radius: var(--radius-full);
  font-size: 14px; font-weight: 500;
  box-shadow: var(--elevation-3);
  display: flex; align-items: center; gap: 8px;
  animation: toastIn 300ms cubic-bezier(0.05, 0.7, 0.1, 1.0);
  pointer-events: all;
  white-space: nowrap;
}
.toast.toast-out { animation: toastOut 200ms ease-in forwards; }
@keyframes toastIn {
  from { opacity: 0; transform: translateY(16px) scale(.9); }
  to { opacity: 1; transform: translateY(0) scale(1); }
}
@keyframes toastOut {
  from { opacity: 1; transform: translateY(0) scale(1); }
  to { opacity: 0; transform: translateY(8px) scale(.95); }
}
.toast .material-icons-round { font-size: 18px; }

/* ── Dialog ── */
.dialog-overlay {
  position: fixed; inset: 0;
  background: rgba(15,10,35,.5);
  display: flex; align-items: center; justify-content: center;
  z-index: 500;
  padding: 16px;
}
.dialog {
  background: var(--md-surface);
  border-radius: var(--radius-xl);
  width: 100%; max-width: 480px;
  box-shadow: var(--elevation-3);
  overflow: hidden;
}
.dialog-header {
  display: flex; align-items: center; gap: 12px;
  padding: 20px 20px 0;
  font-size: 18px; font-weight: 700;
  color: var(--md-on-surface);
}
.dialog-header .material-icons-round { color: var(--md-primary); }
.ml-auto { margin-left: auto; }
.dialog-body { padding: 16px 20px 20px; }
.help-steps { display: flex; flex-direction: column; gap: 16px; margin-top: 8px; }
.help-step { display: flex; gap: 16px; align-items: flex-start; }
.help-step-num {
  width: 32px; height: 32px; flex-shrink: 0;
  background: var(--md-primary-container);
  color: var(--md-on-primary-container);
  border-radius: 50%;
  display: flex; align-items: center; justify-content: center;
  font-weight: 700; font-size: 14px;
}
.help-step-title { font-size: 14px; font-weight: 700; color: var(--md-on-surface); margin-bottom: 2px; }
.help-step-desc { font-size: 13px; color: var(--md-on-surface-variant); }

/* ── Export Overlay ── */
.export-overlay {
  position: fixed; inset: 0;
  background: rgba(15,10,35,.6);
  display: flex; align-items: center; justify-content: center;
  z-index: 600;
}
.export-progress {
  background: var(--md-surface);
  border-radius: var(--radius-xl);
  padding: 32px 48px;
  display: flex; flex-direction: column; align-items: center; gap: 16px;
  box-shadow: var(--elevation-3);
}
.export-spinner {
  width: 48px; height: 48px;
  border: 4px solid var(--md-surface-variant);
  border-top-color: var(--md-primary);
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }
.export-progress p { font-size: 16px; font-weight: 500; color: var(--md-on-surface); }

.danger-text { color: var(--md-error) !important; }

/* ── 7. PWA install banner + mobile FAB ─────────────────────── */
.install-banner {
  position: fixed;
  left: 16px; right: 16px;
  bottom: calc(16px + var(--safe-b));
  max-width: 440px;
  margin: 0 auto;
  background: var(--md-surface);
  border: 1px solid var(--md-outline-variant);
  border-radius: var(--radius-lg);
  box-shadow: var(--elevation-3);
  padding: 16px;
  display: flex; gap: 12px; align-items: flex-start;
  z-index: 400;
  animation: bannerIn var(--trans-slow);
}
.install-banner[hidden] { display: none; }
@keyframes bannerIn {
  from { opacity: 0; transform: translateY(16px); }
  to { opacity: 1; transform: translateY(0); }
}
.install-banner-icon {
  width: 44px; height: 44px; flex-shrink: 0;
  border-radius: var(--radius-md);
  background: var(--brand-gradient);
  color: #fff;
  display: flex; align-items: center; justify-content: center;
}
.install-banner-text { flex: 1; min-width: 0; }
.install-banner-title { font-size: 14px; font-weight: 700; color: var(--md-on-surface); margin-bottom: 2px; }
.install-banner-desc { font-size: 12.5px; color: var(--md-on-surface-variant); line-height: 1.4; }
.install-banner-actions { display: flex; flex-direction: column; gap: 4px; flex-shrink: 0; }
.install-banner-actions .md-btn { height: 32px; padding: 0 14px; font-size: 12.5px; }

/* Mobile-only floating quick-export button */
.mobile-fab {
  display: none;
  position: fixed;
  right: 16px;
  width: 56px; height: 56px;
  border: none; border-radius: var(--radius-lg);
  background: var(--md-primary);
  color: var(--md-on-primary);
  box-shadow: var(--elevation-3);
  align-items: center; justify-content: center;
  cursor: pointer;
  z-index: 190;
  transition: transform var(--trans-fast), opacity var(--trans-fast);
}
.mobile-fab .material-icons-round { font-size: 24px; }
.mobile-fab:active { transform: scale(0.92); }

/* Scrim behind the expanded mobile sheet */
.sheet-backdrop {
  display: none;
  position: fixed; inset: 0;
  background: rgba(10,8,25,.45);
  opacity: 0;
  pointer-events: none;
  transition: opacity var(--trans-sheet);
  z-index: 150;
}

/* ── Transition helpers ── */
.controls-inner { transition: opacity var(--trans-slow); }
.controls-inner.visible { opacity: 1 !important; }

/* ── 8. Responsive rules ─────────────────────────────────────
   Below 768px the controls panel turns into a Material bottom
   sheet: fixed to the viewport bottom, collapsed to a peek strip
   by default, and expanded by tapping/clicking its handle.
   ============================================================ */
@media (max-width: 768px) {
  .main-layout { flex-direction: column; }

  .canvas-panel {
    flex: 1;
    height: auto;
    padding-bottom: calc(var(--sheet-peek) + 16px + var(--safe-b));
  }
  .canvas-toolbar { gap: 4px; padding: 8px 12px; }
  .canvas-viewport { padding: 24px 20px 32px; }
  .md-btn { padding: 0 16px; height: 36px; font-size: 13px; }
  .app-title { font-size: 15px; }

  /* The controls panel becomes a fixed bottom sheet */
  .controls-panel {
    position: fixed;
    left: 0; right: 0; bottom: 0;
    width: 100%;
    max-height: var(--sheet-peek);
    border-left: none;
    border-top: 1px solid var(--md-outline-variant);
    border-top-left-radius: var(--radius-xxl);
    border-top-right-radius: var(--radius-xxl);
    box-shadow: var(--elevation-3);
    overflow: hidden;
    z-index: 200;
    transition: max-height var(--trans-sheet);
    padding-bottom: var(--safe-b);
  }
  .controls-panel.expanded { max-height: 82vh; }

  .sheet-handle {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 6px;
    padding: 10px 16px 14px;
    flex-shrink: 0;
  }
  .sheet-handle-grip {
    width: 36px; height: 4px;
    border-radius: var(--radius-full);
    background: var(--md-outline-variant);
  }
  .sheet-handle-row {
    display: flex; align-items: center; gap: 8px;
    font-size: 13px; font-weight: 700;
    color: var(--md-on-surface);
  }
  .sheet-handle-row .material-icons-round { font-size: 18px; color: var(--md-primary); }
  .sheet-handle-chevron { transition: transform var(--trans-sheet); }
  .controls-panel.expanded .sheet-handle-chevron { transform: rotate(180deg); }

  .controls-scroll { padding: 0 16px 16px; }

  /* Scrim + FAB only make sense once the sheet pattern is active */
  .sheet-backdrop { display: block; }
  body.sheet-open .sheet-backdrop { opacity: 1; pointer-events: auto; }

  .mobile-fab {
    display: flex;
    bottom: calc(var(--sheet-peek) + 16px + var(--safe-b));
  }
  body.sheet-open .mobile-fab { opacity: 0; transform: scale(0.7); pointer-events: none; }

  .install-banner { bottom: calc(var(--sheet-peek) + 12px + var(--safe-b)); }
}

@media (max-width: 480px) {
  .top-app-bar-inner { padding: 0 12px; }
  .canvas-viewport { padding: 20px 16px 28px; }
}

@media (min-width: 1400px) {
  :root { --controls-width: 372px; }
}
