/* ---------- Design tokens ---------- */
:root[data-theme="dark"] {
  --bg: #131312;
  --bg-glow: radial-gradient(1200px 700px at 50% -10%, #1d1d1a 0%, #131312 60%);
  --panel: #1b1b19;
  --panel-2: #222220;
  --topbar: #0e0e0d;
  --border: rgba(255, 255, 255, 0.08);
  --border-strong: rgba(255, 255, 255, 0.14);
  --text: #f2f1ed;
  --muted: #8f8e87;
  --accent: #f36b16;
  --accent-hover: #ff7d2b;
  --accent-soft: rgba(243, 107, 22, 0.14);
  --track: rgba(255, 255, 255, 0.14);
  --canvas-bg: #0c0c0b;
  --floating-bg: rgba(14, 14, 13, 0.5);
  --floating-bg-hover: rgba(14, 14, 13, 0.8);
  --shadow: 0 10px 40px rgba(0, 0, 0, 0.45);
  --toast-bg: #262624;
}
:root[data-theme="light"] {
  --bg: #f2f1ec;
  --bg-glow: radial-gradient(1200px 700px at 50% -10%, #faf9f5 0%, #f2f1ec 60%);
  --panel: #ffffff;
  --panel-2: #f6f5f1;
  --topbar: #ffffff;
  --border: rgba(25, 25, 15, 0.09);
  --border-strong: rgba(25, 25, 15, 0.18);
  --text: #1a1913;
  --muted: #71706a;
  --accent: #ec6410;
  --accent-hover: #f97822;
  --accent-soft: rgba(236, 100, 16, 0.12);
  --track: rgba(25, 25, 15, 0.15);
  --canvas-bg: #e7e6e0;
  --floating-bg: rgba(255, 255, 255, 0.5);
  --floating-bg-hover: rgba(255, 255, 255, 0.8);
  --shadow: 0 10px 32px rgba(30, 25, 15, 0.12);
  --toast-bg: #1a1913;
}
:root[data-theme="light"] .toast { color: #fff; }

/* ---------- Base ---------- */
* { box-sizing: border-box; }
[hidden] { display: none !important; }
:root {
  --font-ui: "Geist", sans-serif;
  --font-accent: "Instrument Serif", serif;
  --ease: cubic-bezier(0.22, 0.8, 0.22, 1);
  --spring: cubic-bezier(0.34, 1.56, 0.64, 1);
}
html, body {
  width: 100%;
  height: 100%;
  overflow: hidden;
  overscroll-behavior: none;
}
body {
  margin: 0;
  font-family: var(--font-ui);
  font-size: 14px;
  line-height: 1.45;
  color: var(--text);
  background: var(--bg);
  background-image: var(--bg-glow);
  -webkit-font-smoothing: antialiased;
}
button { font: inherit; color: inherit; }
:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; border-radius: 8px; }

.app {
  width: 100%;
  height: 100dvh;
  overflow: hidden;
  display: grid;
  grid-template-rows: auto 1fr;
  gap: 12px;
  padding: 12px;
  max-width: 1720px;
  margin: 0 auto;
}

/* ---------- Top bar ---------- */
.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  min-width: 0;
  height: 58px;
  padding: 0 12px 0 18px;
  background: var(--topbar);
  border: 1px solid var(--border);
  border-radius: 18px;
}
.brand { display: flex; align-items: center; gap: 10px; min-width: 0; }
.brand-name { font-weight: 650; font-size: 15px; letter-spacing: -0.01em; }
.brand-logo { display: block; height: 22px; width: auto; color: var(--text); }
.topbar-actions { display: flex; align-items: center; gap: 8px; }

/* ---------- UI kit: buttons ---------- */
.btn {
  --btn-h: 36px;
  --btn-w: auto;
  --btn-px: 16px;
  --btn-gap: 7px;
  --btn-radius: 999px;
  --btn-bg: transparent;
  --btn-bg-hover: var(--panel-2);
  --btn-border: transparent;
  --btn-border-hover: var(--btn-border);
  --btn-color: var(--text);
  --btn-color-hover: var(--btn-color);
  --btn-font-size: 13px;
  --btn-font-weight: 550;
  --btn-active-scale: 0.96;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: var(--btn-gap);
  width: var(--btn-w);
  height: var(--btn-h);
  padding: 0 var(--btn-px);
  border-radius: var(--btn-radius);
  border: 1px solid var(--btn-border);
  background: var(--btn-bg);
  color: var(--btn-color);
  cursor: pointer;
  font-size: var(--btn-font-size);
  font-weight: var(--btn-font-weight);
  white-space: nowrap;
  transition: background .22s var(--ease), border-color .22s var(--ease),
              color .22s var(--ease), opacity .22s var(--ease),
              transform .18s var(--ease);
}
.btn:hover:not(:disabled) {
  background: var(--btn-bg-hover);
  border-color: var(--btn-border-hover);
  color: var(--btn-color-hover);
}
.btn:active:not(:disabled) { transform: scale(var(--btn-active-scale)); }
.btn:disabled { opacity: .45; cursor: not-allowed; }
.btn svg { flex: none; }
.btn-primary {
  --btn-bg: var(--accent);
  --btn-bg-hover: var(--accent-hover);
  --btn-color: #fff;
  --btn-color-hover: #fff;
}
.btn-ghost {
  --btn-border: var(--border-strong);
  --btn-color: var(--text);
}
.btn-plain {
  --btn-color: var(--muted);
  --btn-color-hover: var(--text);
}
.btn-soft {
  --btn-bg: var(--accent-soft);
  --btn-bg-hover: var(--accent-soft);
  --btn-color: var(--accent);
  --btn-color-hover: var(--accent);
}
.btn-sm {
  --btn-h: 30px;
  --btn-px: 12px;
  --btn-font-size: 12.5px;
}
.btn-xs {
  --btn-h: 26px;
  --btn-px: 10px;
  --btn-font-size: 11.5px;
  --btn-font-weight: 600;
}
.btn-icon {
  --btn-w: var(--btn-h);
  --btn-px: 0;
  --btn-border: var(--border-strong);
}
.btn-square {
  --btn-w: var(--btn-h);
  --btn-px: 0;
  --btn-radius: 9px;
}
.btn-wide {
  --btn-w: 100%;
  justify-self: stretch;
}
.btn-tab {
  --btn-h: 26px;
  --btn-px: 11px;
  --btn-border: transparent;
  --btn-color: var(--muted);
  --btn-color-hover: var(--text);
  --btn-font-size: 12px;
  --btn-font-weight: 600;
}
.btn-tab[aria-selected="true"] {
  --btn-bg: var(--panel-2);
  --btn-color: var(--text);
}
.btn-toolbar {
  --btn-h: 30px;
  --btn-px: 12px;
  --btn-border: transparent;
  --btn-font-size: 12.5px;
}
.btn-menu {
  --btn-h: 34px;
  --btn-px: 12px;
  --btn-radius: 9px;
  --btn-border: transparent;
  --btn-font-size: 13px;
  width: 100%;
  justify-content: space-between;
  text-align: left;
}

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

/* Export spinner */
.spinner {
  width: 14px; height: 14px;
  border-radius: 50%;
  border: 2px solid rgba(255,255,255,.35);
  border-top-color: #fff;
  animation: spin .7s linear infinite;
  display: none;
}
.btn.busy .spinner { display: inline-block; }
.btn.busy .btn-svg { display: none; }
@keyframes spin { to { transform: rotate(360deg); } }

/* ---------- Layout ---------- */
.main {
  display: grid;
  grid-template-columns: var(--cat-w, 232px) 200px minmax(0, 1fr) var(--insp-w, 320px);
  gap: 12px;
  min-width: 0;
  min-height: 0;
  transition: grid-template-columns .38s var(--ease);
}
body.resizing .main { transition: none; }
body.resizing { cursor: col-resize; user-select: none; }

/* ---------- Side panels (catalog / layers / inspector) ---------- */
.side {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 18px;
  padding: 12px;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  gap: 6px;
  min-width: 0;
  min-height: 0;
  scrollbar-width: thin;
  scrollbar-color: var(--track) transparent;
}
.side::-webkit-scrollbar { width: 6px; }
.side::-webkit-scrollbar-thumb { background: var(--track); border-radius: 3px; }
.catalog,
.layers,
.catalog-list {
  scrollbar-width: none;
}
.catalog::-webkit-scrollbar,
.layers::-webkit-scrollbar,
.catalog-list::-webkit-scrollbar {
  display: none;
}

.side-title {
  font-size: 10.5px;
  font-weight: 650;
  letter-spacing: 0.09em;
  text-transform: uppercase;
  color: var(--muted);
  margin: 8px 4px 4px;
}
.side-title:first-child { margin-top: 2px; }

/* Catalog: search, tabs, tile palette */
.catalog-top { display: flex; align-items: center; gap: 6px; }
.search-wrap {
  flex: 1;
  min-width: 0;
  display: flex;
  align-items: center;
  gap: 7px;
  height: 32px;
  padding: 0 10px;
  border: 1px solid var(--border-strong);
  border-radius: 9px;
  color: var(--muted);
  transition: border-color .2s var(--ease);
}
.search-wrap:focus-within { border-color: var(--accent); }
.search-wrap input {
  flex: 1;
  min-width: 0;
  border: none;
  background: none;
  color: var(--text);
  font: inherit;
  font-size: 12.5px;
  outline: none;
}
.search-wrap input::-webkit-search-cancel-button { -webkit-appearance: none; }
.collapse-panel-btn {
  --btn-h: 32px;
  --btn-w: 26px;
  --btn-radius: 8px;
  flex: none;
}
.collapse-panel-btn svg { transition: transform .35s var(--ease); }
.catalog.collapsed .collapse-panel-btn svg { transform: rotate(180deg); }

.catalog-tabs {
  display: flex;
  gap: 2px;
  overflow-x: auto;
  scrollbar-width: none;
  -webkit-mask-image: linear-gradient(90deg, #000 86%, transparent);
  mask-image: linear-gradient(90deg, #000 86%, transparent);
  padding: 2px 0;
  flex: none;
}
.catalog-tabs::-webkit-scrollbar { display: none; }
.catalog-tabs .btn-tab { flex: none; }

.catalog-list {
  flex: 1;
  min-height: 0;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  gap: 8px;
  scrollbar-width: none;
}
.catalog-list::-webkit-scrollbar { display: none; }
.cat-header {
  font-family: var(--font-accent);
  font-size: 18px;
  font-weight: 400;
  letter-spacing: 0;
  line-height: 0.95;
  text-transform: uppercase;
  color: var(--muted);
  margin: 8px 2px 0;
}
.cat-header:first-child { margin-top: 2px; }
.tile-grid + .cat-header { margin-top: 20px; }
.tile-grid {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 10px 8px;
}
.tile {
  position: relative;
  border: none;
  background: none;
  padding: 0;
  cursor: pointer;
  text-align: left;
  display: flex;
  flex-direction: column;
  gap: 5px;
}
.tile-thumb {
  display: block;
  aspect-ratio: 1;
  border-radius: 9px;
  overflow: hidden;
  background: var(--panel-2);
  border: 1px solid var(--border);
  transition: border-color .22s var(--ease), transform .22s var(--ease);
}
.tile-thumb img { width: 100%; height: 100%; object-fit: cover; display: block; }
.tile:hover .tile-thumb { border-color: var(--border-strong); }
.tile:active .tile-thumb { transform: scale(0.97); }
.tile.active .tile-thumb { border-color: var(--accent); box-shadow: 0 0 0 1px var(--accent); }
.tile-name {
  font-size: 11.5px;
  color: var(--muted);
  line-height: 1.25;
  transition: color .2s var(--ease);
}
.tile:hover .tile-name, .tile.active .tile-name { color: var(--text); }
.tile-badge {
  position: absolute;
  top: 5px;
  left: 5px;
  min-width: 17px;
  height: 17px;
  padding: 0 4px;
  border-radius: 6px;
  background: var(--accent);
  color: #fff;
  font-size: 10.5px;
  font-weight: 650;
  display: grid;
  place-items: center;
}
.tile-fav {
  position: absolute;
  top: 3px;
  right: 6px;
  font-size: 13px;
  color: #fff;
  text-shadow: 0 1px 3px rgba(0, 0, 0, 0.65);
  opacity: 0;
  cursor: pointer;
  transition: opacity .2s var(--ease), color .2s var(--ease), transform .18s var(--spring);
}
.tile:hover .tile-fav { opacity: 0.75; }
.tile-fav:hover { transform: scale(1.2); }
.tile-fav.faved { opacity: 1; color: #ffcf40; }
.tile-x {
  position: absolute;
  top: 21px;
  right: 6px;
  width: 24px;
  height: 24px;
  color: #fff;
  text-shadow: 0 1px 3px rgba(0, 0, 0, 0.65);
  opacity: 0;
  cursor: pointer;
  display: grid;
  place-items: center;
  transition: opacity .2s var(--ease);
}
.tile-x svg { display: block; }
.tile:hover .tile-x { opacity: 0.75; }
.tile-x:hover { opacity: 1; }
.cat-save {
  --btn-h: 32px;
  --btn-radius: 9px;
  --btn-color: var(--muted);
  --btn-color-hover: var(--text);
  --btn-bg-hover: transparent;
  --btn-border-hover: var(--accent);
  border: 1px dashed var(--border-strong);
}
.cat-empty { color: var(--muted); font-size: 12px; padding: 10px 4px; }

/* Collapsed catalog: single column of square thumbnails */
.catalog.collapsed { padding: 10px 8px; }
.catalog.collapsed .search-wrap,
.catalog.collapsed .catalog-tabs { display: none; }
.catalog.collapsed .catalog-top { justify-content: center; }
.catalog.collapsed .collapse-panel-btn { width: 100%; }
.catalog.collapsed .tile-grid { grid-template-columns: 1fr; gap: 8px; }
.catalog.collapsed .tile-name { display: none; }

/* Layer stack */
.layer-list { display: flex; flex-direction: column; gap: 6px; }
.layer-item {
  display: flex;
  align-items: center;
  gap: 4px;
  height: 38px;
  padding: 0 6px 0 11px;
  border-radius: 10px;
  border: 1px solid var(--border);
  background: var(--panel-2);
  font-size: 13px;
  cursor: pointer;
  transition: border-color .2s var(--ease), background .2s var(--ease);
}
.layer-item:hover { border-color: var(--border-strong); }
.layer-item.selected { border-color: var(--accent); background: var(--accent-soft); }
.layer-item.off .layer-name { opacity: 0.4; }
.layer-preset {
  padding: 8px;
  border: 1px solid var(--border-strong);
  border-radius: 14px;
  background: color-mix(in srgb, var(--panel-2) 38%, transparent);
  display: flex;
  flex-direction: column;
  gap: 6px;
}
.layer-preset-head {
  min-width: 0;
  height: 30px;
  padding: 0 4px 0 7px;
  display: flex;
  align-items: center;
  gap: 7px;
  color: var(--muted);
}
.layer-preset-tag {
  flex: none;
  height: 20px;
  padding: 0 7px;
  display: inline-flex;
  align-items: center;
  border: 1px solid var(--border);
  border-radius: 999px;
  background: var(--panel);
  color: var(--muted);
  font-size: 10px;
  font-weight: 700;
  letter-spacing: 0.07em;
  text-transform: uppercase;
}
.layer-preset-name {
  flex: 1;
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  font-size: 12.5px;
  color: var(--text);
}
.layer-preset-remove {
  flex: none;
  color: var(--muted);
}
.layer-item.layer-child {
  border-color: var(--border);
  background: color-mix(in srgb, var(--panel-2) 72%, transparent);
  padding-left: 11px;
}
.layer-item.layer-child.selected {
  border-color: var(--accent);
  background: var(--accent-soft);
}
.layer-name { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.layer-reset {
  color: var(--muted);
  --btn-bg-hover: var(--panel);
}
.layer-btn {
  --btn-h: 26px;
  border-radius: 7px;
  font-size: 14px;
  --btn-bg-hover: var(--panel);
}
.layer-btn svg { display: block; }

/* Inspector */
.inspector { position: relative; }
.inspector-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  font-size: 13px;
  font-weight: 650;
  padding: 4px 4px 12px;
  border-bottom: 1px solid var(--border);
  margin-bottom: 10px;
  flex: none;
}
.insp-shuffle {
  --btn-h: 26px;
  border-radius: 7px;
  --btn-color-hover: var(--accent);
  --btn-bg-hover: var(--panel-2);
  transition-timing-function: var(--spring);
}
.insp-shuffle:active { transform: rotate(-12deg) scale(0.92); }
.inspector-body { display: flex; flex-direction: column; }
.inspector-rows { display: grid; gap: 13px; padding: 2px; }
.photo-dims { color: var(--muted); font-size: 12.5px; margin: 0; }

/* Drag handle on the inspector's left edge */
.inspector-resizer {
  position: absolute;
  left: -8px;
  top: 0;
  bottom: 0;
  width: 14px;
  cursor: col-resize;
  z-index: 5;
  touch-action: none;
}
.inspector-resizer::after {
  content: '';
  position: absolute;
  left: 6px;
  top: 0;
  bottom: 0;
  width: 2px;
  border-radius: 1px;
  background: transparent;
  transition: background .2s var(--ease);
}
.inspector-resizer:hover::after,
body.resizing .inspector-resizer::after { background: var(--accent); }

/* Layer thumbnails */
.layer-thumb {
  width: 26px;
  height: 26px;
  flex: none;
  border-radius: 6px;
  overflow: hidden;
  border: 1px solid var(--border);
  background: var(--panel);
  margin-right: 4px;
}
.layer-thumb img { width: 100%; height: 100%; object-fit: cover; display: block; }
.layer-item.base { padding-left: 6px; }

/* ---------- Viewport ---------- */
.viewport {
  position: relative;
  display: grid;
  place-items: center;
  background: var(--canvas-bg);
  border: 1px solid var(--border);
  border-radius: 18px;
  overflow: hidden;
  min-width: 0;
  min-height: 0;
  contain: layout;
}
#glcanvas {
  border-radius: 10px;
  box-shadow: var(--shadow);
  flex: none;
  touch-action: none;
  cursor: grab;
}
#glcanvas.panning { cursor: grabbing; }

/* Sharp-area overlay (motion blur editor) */
#sharpOverlay {
  position: absolute;
  overflow: visible;
  pointer-events: none;
  z-index: 5;
}
#sharpEllipse {
  fill: rgba(243, 107, 22, 0.06);
  stroke: var(--accent);
  stroke-width: 1.5;
  stroke-dasharray: 6 5;
  pointer-events: stroke;
  cursor: move;
}
#sharpAxisX, #sharpAxisY {
  stroke: var(--accent);
  stroke-width: 1;
  opacity: 0.4;
}
.sharp-handle {
  fill: var(--topbar);
  stroke: var(--accent);
  stroke-width: 2;
  pointer-events: all;
  cursor: grab;
  transition: r .15s var(--ease);
}
.sharp-handle:hover { r: 7; }
.sharp-handle[data-handle="center"] { fill: var(--accent); cursor: move; }
.app[data-state="ready"] #glcanvas { animation: canvas-in .5s var(--ease); }
@keyframes canvas-in {
  from { opacity: 0; }
  to { opacity: 1; }
}

/* Zoom controls */
.zoom-bar {
  position: absolute;
  right: 14px;
  bottom: 16px;
  height: 44px;
  display: flex;
  align-items: center;
  gap: 4px;
  padding: 6px 8px;
  background: var(--floating-bg);
  backdrop-filter: blur(6px);
  -webkit-backdrop-filter: blur(6px);
  border: 1px solid var(--border);
  border-radius: 999px;
  box-shadow: var(--shadow);
  z-index: 6;
  transition: background .22s var(--ease), border-radius .28s var(--ease);
}
.zoom-bar:hover { background: var(--floating-bg-hover); }
.zoom-btn {
  --btn-h: 30px;
  --btn-radius: 9px;
  --btn-color: var(--text);
  --btn-active-scale: 0.92;
}
.zoom-bar select {
  width: 64px;
  height: 30px;
  padding: 0 18px 0 7px;
  font-size: 11px;
  border-radius: 8px;
  background-position: right 6px center;
}
.viewport.controls-stacked .zoom-bar {
  bottom: 68px;
  flex-direction: column;
  width: 48px;
  height: auto;
  padding: 7px 6px;
  border-radius: 18px;
}
.viewport.controls-stacked .zoom-bar select {
  width: 36px;
  padding: 0 5px;
  background-image: none;
  text-align: center;
  text-align-last: center;
}

/* Unsupported state */
.unsupported {
  display: flex;
  flex-direction: column;
  align-items: center;
  text-align: center;
  gap: 6px;
  padding: 32px;
  max-width: 420px;
}
.unsupported h1 {
  margin: 0;
  font-size: 26px;
  font-weight: 650;
  letter-spacing: -0.02em;
}
.unsupported p { margin: 0 0 18px; color: var(--muted); font-size: 13.5px; }

/* Canvas toolbar */
.canvas-bar {
  position: absolute;
  bottom: 16px;
  left: 50%;
  transform: translateX(-50%);
  height: 44px;
  display: flex;
  align-items: center;
  gap: 4px;
  padding: 6px 8px;
  background: var(--floating-bg);
  backdrop-filter: blur(6px);
  -webkit-backdrop-filter: blur(6px);
  border: 1px solid var(--border);
  border-radius: 999px;
  box-shadow: var(--shadow);
  transition: background .22s var(--ease);
}
.canvas-bar:hover { background: var(--floating-bg-hover); }
.dims { color: var(--muted); font-size: 12px; padding: 0 8px; font-variant-numeric: tabular-nums; white-space: nowrap; }
.bar-sep { width: 1px; height: 16px; background: var(--border-strong); margin: 0 2px; }
.bar-btn {
  --btn-active-scale: 0.94;
}
.bar-btn:active, .bar-btn.held { background: var(--accent-soft); color: var(--accent); }

/* Drop overlay */
.drop-overlay {
  position: absolute;
  inset: 10px;
  display: grid;
  place-items: center;
  border: 2px dashed var(--accent);
  border-radius: 14px;
  background: var(--accent-soft);
  backdrop-filter: blur(2px);
  z-index: 10;
  pointer-events: none;
  font-size: 16px;
  font-weight: 600;
  color: var(--accent);
}

/* ---------- Control panel ---------- */
.panel {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 18px;
  padding: 14px;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  gap: 12px;
  scrollbar-width: none;
}
.panel::-webkit-scrollbar { display: none; }

/* Presets */
.presets { display: flex; flex-wrap: wrap; gap: 6px; }
.chip {
  --btn-h: 30px;
  --btn-px: 13px;
  --btn-border: var(--border-strong);
  --btn-color: var(--muted);
  --btn-color-hover: var(--text);
  --btn-active-scale: 0.95;
}
.chip.active {
  background: var(--accent-soft);
  border-color: var(--accent);
  color: var(--accent);
}

/* Effect groups */
.groups { display: flex; flex-direction: column; gap: 10px; }
.group {
  background: var(--panel-2);
  border: 1px solid var(--border);
  border-radius: 14px;
  overflow: clip;
}
.group-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  padding: 11px 14px;
  cursor: pointer;
  user-select: none;
}
.group-head.static { cursor: default; }
.group-title {
  font-family: var(--font-accent);
  font-size: 18px;
  font-weight: 400;
  letter-spacing: 0;
  line-height: 0.95;
  text-transform: uppercase;
  transition: color .25s var(--ease);
}
.group[data-on="false"] .group-title { color: var(--muted); }
.collapse-btn {
  --btn-h: 28px;
  --btn-bg-hover: var(--panel);
}
.collapse-btn svg { transition: transform .28s var(--ease); }
.group[data-collapsed="false"] .collapse-btn svg { transform: rotate(180deg); }

/* Smooth expand/collapse via animatable grid rows */
.group-body-wrap {
  display: grid;
  grid-template-rows: 1fr;
  transition: grid-template-rows .34s var(--ease);
}
.group[data-on="false"] .group-body-wrap,
.group[data-collapsed="true"] .group-body-wrap { grid-template-rows: 0fr; }
.group-body {
  padding: 2px 14px 14px;
  display: grid;
  gap: 13px;
  overflow: hidden;
  min-height: 0;
  transition: opacity .26s ease, transform .34s var(--ease), padding .34s var(--ease);
}
.group[data-on="false"] .group-body,
.group[data-collapsed="true"] .group-body {
  opacity: 0;
  transform: translateY(-6px);
  padding-top: 0;
  padding-bottom: 0;
  pointer-events: none;
}

/* Rows */
.row { display: grid; gap: 7px; }
.row-top { display: flex; align-items: baseline; justify-content: space-between; }
.row-label { font-size: 12px; color: var(--muted); }
.row-val { font-size: 12px; color: var(--text); font-variant-numeric: tabular-nums; }
.row-inline { display: flex; align-items: center; justify-content: space-between; }

/* Range */
input[type="range"] {
  -webkit-appearance: none;
  appearance: none;
  width: 100%;
  height: 18px;
  margin: 0;
  background: transparent;
  cursor: pointer;
  --fill: 50%;
}
input[type="range"]::-webkit-slider-runnable-track {
  height: 4px;
  border-radius: 2px;
  background: linear-gradient(90deg, var(--accent) var(--fill), var(--track) var(--fill));
}
input[type="range"]::-webkit-slider-thumb {
  -webkit-appearance: none;
  width: 14px; height: 14px;
  margin-top: -5px;
  border-radius: 50%;
  border: none;
  background: var(--text);
  box-shadow: 0 1px 4px rgba(0,0,0,.35);
  transition: transform .22s var(--spring), background .2s var(--ease);
}
input[type="range"]:hover::-webkit-slider-thumb { transform: scale(1.12); }
input[type="range"]:active::-webkit-slider-thumb { transform: scale(1.25); }
input[type="range"]::-moz-range-track {
  height: 4px;
  border-radius: 2px;
  background: linear-gradient(90deg, var(--accent) var(--fill), var(--track) var(--fill));
}
input[type="range"]::-moz-range-thumb {
  width: 14px; height: 14px;
  border-radius: 50%;
  border: none;
  background: var(--text);
  box-shadow: 0 1px 4px rgba(0,0,0,.35);
}

/* Switch */
.switch { position: relative; width: 34px; height: 20px; flex: none; }
.switch input { position: absolute; inset: 0; margin: 0; opacity: 0; cursor: pointer; }
.switch .knob {
  position: absolute; inset: 0;
  border-radius: 999px;
  background: var(--track);
  transition: background .28s var(--ease);
  pointer-events: none;
}
.switch .knob::after {
  content: '';
  position: absolute;
  top: 2px; left: 2px;
  width: 16px; height: 16px;
  border-radius: 50%;
  background: #fff;
  box-shadow: 0 1px 3px rgba(0,0,0,.3);
  transition: transform .32s var(--spring);
}
.switch input:checked + .knob { background: var(--accent); }
.switch input:checked + .knob::after { transform: translateX(14px); }
.switch input:focus-visible + .knob { outline: 2px solid var(--accent); outline-offset: 2px; }

/* Select */
select {
  appearance: none;
  -webkit-appearance: none;
  width: 100%;
  height: 32px;
  padding: 0 30px 0 10px;
  border: 1px solid var(--border-strong);
  border-radius: 10px;
  background: transparent url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='10' height='6' viewBox='0 0 10 6'%3E%3Cpath d='M1 1l4 4 4-4' stroke='%23888' stroke-width='1.6' fill='none' stroke-linecap='round'/%3E%3C/svg%3E") no-repeat right 11px center;
  color: var(--text);
  font-size: 12.5px;
  cursor: pointer;
}
:root[data-theme="light"] select { background-color: var(--panel); }
:root[data-theme="dark"] select option { background: var(--panel-2); }

/* Small button (shuffle) */
.mini-btn {
  justify-self: start;
  --btn-px: 13px;
  --btn-font-size: 12px;
  --btn-bg-hover: var(--panel);
}
.mini-btn:active { background: var(--accent-soft); color: var(--accent); }
.mini-btn.wide { --btn-w: 100%; justify-self: stretch; }

/* Icon segmented control (pixel type) */
.seg { display: flex; gap: 4px; }
.seg .btn {
  flex: 1;
  --btn-h: 32px;
  --btn-radius: 9px;
  --btn-color: var(--muted);
  --btn-bg-hover: var(--panel);
  --btn-active-scale: 0.92;
  transition-timing-function: var(--spring);
}
.seg .btn[aria-pressed="true"] {
  border-color: var(--accent);
  color: var(--accent);
  background: var(--accent-soft);
}

/* Radio group */
.radio-group {
  display: grid;
  gap: 10px;
  padding: 10px 12px;
  border: 1px solid var(--border-strong);
  border-radius: 10px;
  background: rgba(255, 255, 255, 0.03);
}
.radio-option {
  display: flex;
  align-items: center;
  gap: 10px;
  color: var(--text);
  font-size: 12.5px;
  font-weight: 550;
  cursor: pointer;
}
.radio-option input {
  position: absolute;
  opacity: 0;
  pointer-events: none;
}
.radio-mark {
  width: 16px;
  height: 16px;
  border: 2px solid var(--muted);
  border-radius: 999px;
  display: grid;
  place-items: center;
  transition: border-color .22s var(--ease), background .22s var(--ease);
}
.radio-mark::after {
  content: '';
  width: 6px;
  height: 6px;
  border-radius: inherit;
  background: transparent;
  transition: background .22s var(--ease);
}
.radio-option input:checked + .radio-mark { border-color: var(--text); }
.radio-option input:checked + .radio-mark::after { background: var(--text); }
.radio-option input:focus-visible + .radio-mark { outline: 2px solid var(--accent); outline-offset: 2px; }

/* Dual-thumb range */
.range2 { position: relative; height: 18px; }
.range2 .track {
  position: absolute;
  left: 0; right: 0; top: 7px;
  height: 4px;
  border-radius: 2px;
  background: var(--track);
}
.range2 .fill {
  position: absolute;
  top: 7px;
  height: 4px;
  border-radius: 2px;
  background: var(--accent);
  left: var(--lo, 0%);
  right: calc(100% - var(--hi, 100%));
}
.range2 input {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 18px;
  margin: 0;
  -webkit-appearance: none;
  appearance: none;
  background: none;
  pointer-events: none;
}
.range2 input::-webkit-slider-runnable-track { background: transparent; height: 4px; }
.range2 input::-webkit-slider-thumb {
  -webkit-appearance: none;
  pointer-events: auto;
  width: 14px; height: 14px;
  margin-top: -5px;
  border-radius: 50%;
  border: none;
  background: var(--text);
  box-shadow: 0 1px 4px rgba(0,0,0,.35);
}
.range2 input::-moz-range-track { background: transparent; height: 4px; }
.range2 input::-moz-range-thumb {
  pointer-events: auto;
  width: 14px; height: 14px;
  border-radius: 50%;
  border: none;
  background: var(--text);
  box-shadow: 0 1px 4px rgba(0,0,0,.35);
}

/* Export menu */
.export-wrap { position: relative; }
.menu {
  position: absolute;
  top: calc(100% + 8px);
  right: 0;
  width: max-content;
  min-width: 200px;
  max-width: 320px;
  display: flex;
  flex-direction: column;
  padding: 6px;
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 14px;
  box-shadow: var(--shadow);
  z-index: 60;
  transform-origin: top right;
  animation: menu-in .22s var(--ease);
}
@keyframes menu-in {
  from { opacity: 0; transform: translateY(-5px) scale(0.97); }
  to { opacity: 1; transform: none; }
}
.menu .btn-menu {
  gap: 14px;
}
.menu-size { color: var(--muted); font-size: 11.5px; font-variant-numeric: tabular-nums; }
.menu-sep { height: 1px; background: var(--border); margin: 4px 8px; }

/* Color input */
input[type="color"] {
  width: 36px;
  height: 24px;
  padding: 0;
  border: 1px solid var(--border-strong);
  border-radius: 7px;
  background: none;
  cursor: pointer;
}
input[type="color"]::-webkit-color-swatch-wrapper { padding: 2px; }
input[type="color"]::-webkit-color-swatch { border: none; border-radius: 4px; }
input[type="color"]::-moz-color-swatch { border: none; border-radius: 4px; }

/* User preset chips */
.chip .x {
  margin-left: 7px;
  opacity: 0.5;
  font-size: 13px;
}
.chip .x:hover { opacity: 1; color: var(--accent); }
.chip.save { border-style: dashed; }

/* Dialogs */
.dialog {
  width: min(430px, calc(100vw - 28px));
  padding: 0;
  border: none;
  border-radius: 18px;
  color: var(--text);
  background: transparent;
}
.dialog::backdrop {
  background: rgba(0, 0, 0, 0.56);
  backdrop-filter: blur(8px);
  -webkit-backdrop-filter: blur(8px);
}
.dialog-card {
  display: flex;
  flex-direction: column;
  gap: 16px;
  padding: 18px;
  background: color-mix(in srgb, var(--panel) 92%, transparent);
  border: 1px solid var(--border-strong);
  border-radius: 18px;
  box-shadow: var(--shadow);
}
.dialog-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
}
.dialog-head h2 {
  margin: 0;
  font-size: 18px;
  font-weight: 650;
}
.dialog-close {
  --btn-h: 32px;
  --btn-radius: 9px;
}
.dialog-field {
  display: flex;
  flex-direction: column;
  gap: 8px;
  color: var(--muted);
  font-size: 12px;
  font-weight: 600;
}
.dialog-field input {
  width: 100%;
  height: 42px;
  padding: 0 12px;
  border-radius: 11px;
  border: 1px solid var(--border-strong);
  background: var(--panel-2);
  color: var(--text);
  font: inherit;
}
.dialog-field input:focus {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}
.dialog-error {
  min-height: 18px;
  margin: -6px 0 0;
  color: var(--accent);
  font-size: 12px;
}
.dialog-actions {
  display: flex;
  justify-content: flex-end;
  gap: 8px;
}

/* Toast */
.toast {
  position: fixed;
  left: 50%;
  bottom: 26px;
  transform: translateX(-50%);
  background: var(--toast-bg);
  border: 1px solid var(--border);
  color: var(--text);
  padding: 10px 18px;
  border-radius: 999px;
  font-size: 13px;
  box-shadow: var(--shadow);
  z-index: 100;
  animation: toast-in .2s ease-out;
}
@keyframes toast-in {
  from { opacity: 0; transform: translate(-50%, 8px); }
  to { opacity: 1; transform: translate(-50%, 0); }
}

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

/* ---------- Responsive ---------- */
@media (max-width: 1280px) {
  .main { grid-template-columns: 190px 176px minmax(0, 1fr) 300px; }
}
@media (max-width: 1024px) {
  .app { height: 100dvh; min-height: 0; }
  .main {
    grid-template-columns: 1fr 1fr;
    grid-template-rows: minmax(340px, 55dvh) auto auto;
    overflow-x: hidden;
    overflow-y: auto;
  }
  .viewport { grid-column: 1 / -1; grid-row: 1; }
  .catalog { grid-row: 2; }
  .layers { grid-row: 2; }
  .inspector { grid-column: 1 / -1; grid-row: 3; }
  .side { overflow: visible; }
}
@media (max-width: 480px) {
  .main {
    grid-template-columns: minmax(0, 1fr);
    grid-template-rows: minmax(340px, 54dvh) auto auto auto;
  }
  .viewport { grid-column: 1; grid-row: 1; }
  .catalog { grid-column: 1; grid-row: 2; }
  .layers { grid-column: 1; grid-row: 3; }
  .inspector { grid-column: 1; grid-row: 4; }
  .topbar-actions .btn span:not(.spinner) { display: none; }
  .topbar-actions .btn-primary {
    --btn-w: 36px;
    width: 36px;
    padding: 0;
  }
  .topbar-actions .btn-primary > svg:last-of-type { display: none; }
}
