:root {
  --bg: #0f1115;
  --bg-2: #171a21;
  --panel: #1c2029;
  --panel-2: #232833;
  --line: #2e3440;
  --text: #e6e9ef;
  --muted: #9aa4b2;
  --accent: #5eead4;
  --accent-2: #7c9cff;
  --danger: #ff6b6b;
  --radius: 10px;
  --shadow: 0 4px 18px rgba(0,0,0,.28);
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }
html, body { margin: 0; height: 100%; }
body {
  background: radial-gradient(1000px 500px at 80% -10%, #1a2030 0%, var(--bg) 60%);
  color: var(--text);
  font-family: 'Inter', system-ui, sans-serif;
  display: flex;
  flex-direction: column;
  min-height: 100vh;
}

/* ---------- Top bar ---------- */
.topbar {
  display: flex; align-items: center; justify-content: space-between; gap: 10px;
  padding: 6px 14px; border-bottom: 1px solid var(--line);
  background: rgba(15,17,21,.75); backdrop-filter: blur(8px); flex-wrap: wrap;
}
.header-actions { display: flex; gap: 4px; }
.brand { display: flex; align-items: center; gap: 10px; }
.logo { font-size: 24px; color: var(--accent); text-shadow: 0 0 14px rgba(94,234,212,.4); }
.topbar h1 { font-size: 16px; margin: 0; letter-spacing: .2px; }
.tagline { margin: 0; color: var(--muted); font-size: 11px; }
.asset-name { display: flex; flex-direction: column; gap: 2px; }
.asset-name label { font-size: 10px; color: var(--muted); text-transform: uppercase; letter-spacing: .4px; }
.asset-name input {
  background: var(--panel); border: 1px solid var(--line); color: var(--text);
  border-radius: 6px; padding: 5px 8px; font-family: 'JetBrains Mono', monospace; font-size: 12px;
  width: 140px;
}

/* ---------- Steps ---------- */
.steps { display: flex; gap: 6px; padding: 6px 14px 0; flex-wrap: wrap; }
.step {
  display: flex; align-items: center; gap: 6px;
  background: var(--panel); color: var(--muted);
  border: 1px solid var(--line); border-radius: 999px;
  padding: 5px 12px; cursor: pointer; font-size: 12px; font-weight: 600;
  transition: .15s;
}
.step b {
  display: grid; place-items: center; width: 18px; height: 18px; border-radius: 50%;
  background: var(--panel-2); font-size: 11px;
}
.step:hover:not(:disabled) { border-color: var(--accent-2); color: var(--text); }
.step.active { color: #06131a; background: var(--accent); border-color: var(--accent); }
.step.active b { background: rgba(0,0,0,.2); color: #06131a; }
.step:disabled { opacity: .4; cursor: not-allowed; }

/* ---------- Layout ---------- */
.layout { flex: 1; padding: 8px 12px 16px; min-height: 0; }
.panel { display: grid; grid-template-columns: 248px 1fr; gap: 10px; align-items: stretch; min-height: calc(100vh - 108px); }
.controls {
  position: sticky; top: 6px; max-height: calc(100vh - 96px); overflow-y: auto;
  display: flex; flex-direction: column; gap: 8px;
}
.controls > .control-group,
.controls > .dropzone,
.controls > #kitPanel,
.controls > #sheetComposerPanel { margin-top: 0 !important; }
.editor-viewport, .sheet-viewport {
  flex: 1; min-height: 280px; max-height: calc(100vh - 160px); overflow: auto;
  border: 1px solid var(--line); border-radius: 6px; background: #141820;
}
.editor-card { flex: 1; min-width: 0; display: flex; flex-direction: column; }
.zoom-hint { font-size: 10px; margin: 4px 0 0; }
.project-library { display: flex; flex-direction: column; gap: 4px; max-height: 160px; overflow-y: auto; }
.lib-asset {
  display: flex; align-items: center; gap: 6px; padding: 4px 6px; border-radius: 6px;
  background: var(--panel-2); border: 1px solid var(--line); cursor: grab; font-size: 11px;
}
.lib-asset canvas { width: 28px; height: 28px; image-rendering: pixelated; flex-shrink: 0; }
.split-suggestions { display: flex; flex-direction: column; gap: 6px; margin-top: 6px; max-height: 140px; overflow-y: auto; }
.split-row {
  display: flex; align-items: center; gap: 6px; padding: 4px 6px; border-radius: 6px;
  background: var(--panel-2); border: 1px solid var(--line); font-size: 11px;
}
.split-row canvas { width: 28px; height: 28px; image-rendering: pixelated; flex-shrink: 0; }
.split-meta { flex: 1; display: flex; flex-direction: column; gap: 1px; }
.split-meta span { color: var(--muted); font-size: 10px; }
.split-row .btn { padding: 4px 6px; font-size: 10px; flex: 0; }
.seg-wrap { flex-wrap: wrap; }
.seg-wrap .seg-btn { flex: 1 1 30%; min-width: 48px; padding: 6px 4px; font-size: 11px; }
.kit-group { padding-bottom: 0; }
.kit-group h3 { margin-bottom: 4px; }
.glyph-grid {
  display: grid; grid-template-columns: repeat(5, 1fr); gap: 2px;
  max-height: min(38vh, 280px); overflow-y: auto;
}
.glyph-grid.type-grid { grid-template-columns: repeat(8, 1fr); }
.glyph-cell {
  display: flex; flex-direction: column; align-items: stretch;
  padding: 2px; margin: 0; border: 1px solid var(--line); border-radius: 4px;
  background: var(--panel-2); cursor: pointer; min-width: 0;
}
.glyph-cell.active { border-color: var(--accent); box-shadow: 0 0 0 1px var(--accent); }
.glyph-cell.empty { opacity: .55; }
.glyph-cell canvas { width: 100%; height: auto; image-rendering: pixelated; display: block; }
.glyph-cell span {
  font-size: 8px; line-height: 1.1; color: var(--muted); text-align: center;
  font-family: 'JetBrains Mono', monospace; overflow: hidden; text-overflow: ellipsis;
}
.kit-actions { margin-top: 4px; }
.kit-actions .btn { flex: 1; padding: 6px 4px; font-size: 11px; }
.type3d-row { display: flex; gap: 4px; align-items: flex-end; margin-bottom: 4px; }
.type3d-style { flex: 1; min-width: 0; font-size: 11px; padding: 5px 6px; }
.type3d-depth { flex: 1; min-width: 0; margin: 0; }
.type3d-depth span { font-size: 10px; }
.kit-preview-input { width: 100%; margin: 0 0 4px; font-size: 11px; padding: 5px 6px; }
.kit-type-preview { display: block; width: 100%; max-width: 100%; background: #141820; border-radius: 4px; }
.kit-stage .canvas-head { padding: 5px 8px; }
.kit-stage { padding-bottom: 6px; }
.lib-tag { font-size: 10px; padding: 3px 4px; max-width: 64px; background: var(--panel-2); color: var(--text); border: 1px solid var(--line); border-radius: 5px; }
.lib-tint { width: 24px; height: 24px; padding: 0; border: none; background: transparent; cursor: pointer; flex-shrink: 0; }
.lib-rename { flex: 1; min-width: 50px; font-size: 10px; padding: 3px 4px; }
.lib-desc { flex: 1 1 100%; font-size: 10px; padding: 3px 4px; margin-top: 2px; }
.lib-asset { flex-wrap: wrap; }
.sync-dot {
  width: 8px; height: 8px; border-radius: 50%; background: #64748b; flex-shrink: 0;
}
.sync-dot.online { background: #34d399; box-shadow: 0 0 8px rgba(52,211,153,.6); }
.sync-label { font-size: 11px; color: var(--muted); min-width: 72px; }
.cursor-bar { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; }
.cursor-fields { display: flex; flex-wrap: wrap; align-items: center; gap: 8px 12px; }
.cursor-fields label { font-size: 10px; color: var(--muted); margin: 0; }
.cursor-fields input { max-width: 140px; }
.cursor-fields .intent-label + input { max-width: 220px; flex: 1; min-width: 120px; }
#projectIntent { width: auto; min-height: unset; resize: none; padding: 6px 8px; }
.preset-row { flex-wrap: wrap; }
.preset-row .btn { flex: 1 1 30%; min-width: 56px; font-size: 11px; padding: 6px 6px; }
.tint-field { flex-direction: row; align-items: center; flex-wrap: wrap; gap: 6px; }
.tint-field input[type="color"] { width: 36px; height: 28px; padding: 0; border: 1px solid var(--line); border-radius: 5px; cursor: pointer; }
.modal {
  position: fixed; inset: 0; z-index: 1000; display: grid; place-items: center;
  background: rgba(0,0,0,.55); backdrop-filter: blur(4px);
}
.modal[hidden] { display: none !important; }
.modal-card {
  background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius);
  padding: 18px; max-width: 340px; width: 90%; box-shadow: var(--shadow);
}
.modal-card h3 { margin: 0 0 6px; font-size: 15px; }
.modal-card canvas { margin: 10px auto; display: block; }
.control-group, .dropzone {
  background: var(--panel); border: 1px solid var(--line);
  border-radius: var(--radius); padding: 10px; box-shadow: var(--shadow);
}
.control-group h3 { margin: 0 0 8px; font-size: 12px; letter-spacing: .25px; text-transform: uppercase; color: var(--muted); }
.control-group + .control-group, .dropzone + .control-group, .control-group + .dropzone { margin-top: 0; }

/* ---------- Dropzone ---------- */
.dropzone { cursor: pointer; text-align: center; border-style: dashed; transition: .15s; padding: 0; }
.dropzone.drag { border-color: var(--accent); background: var(--panel-2); }
.dz-inner { padding: 14px 8px; }
.dz-icon { font-size: 26px; color: var(--accent); }
.dropzone p { margin: 4px 0 0; font-size: 12px; }
.dropzone .hint { color: var(--muted); font-size: 11px; }

/* ---------- Fields ---------- */
.field { display: flex; flex-direction: column; gap: 4px; margin-bottom: 8px; font-size: 12px; }
.field.compact { margin-bottom: 6px; }
.field span { color: var(--muted); display: flex; justify-content: space-between; }
.field em { color: var(--accent); font-style: normal; font-family: 'JetBrains Mono', monospace; font-size: 11px; }
select, input[type="range"] { width: 100%; }
select {
  background: var(--panel-2); color: var(--text); border: 1px solid var(--line);
  border-radius: 6px; padding: 6px 8px; font-size: 12px;
}
input[type="range"] { accent-color: var(--accent); }
.checkbox { display: flex; align-items: center; gap: 7px; margin-bottom: 8px; font-size: 12px; color: var(--text); cursor: pointer; }
.checkbox input { accent-color: var(--accent); width: 14px; height: 14px; }

/* ---------- Buttons ---------- */
.btn {
  border: 1px solid var(--line); background: var(--panel-2); color: var(--text);
  border-radius: 6px; padding: 6px 10px; cursor: pointer; font-size: 12px; font-weight: 600;
  transition: .15s;
}
.btn:hover { border-color: var(--accent-2); }
.btn.primary { background: var(--accent); color: #06131a; border-color: var(--accent); }
.btn.primary:hover { filter: brightness(1.08); }
.btn.ghost { background: transparent; }
.btn.block { width: 100%; margin-top: 4px; }
.btn.tool.active { background: var(--accent-2); color: #06131a; border-color: var(--accent-2); }
.btn.active { border-color: var(--accent); color: var(--accent); background: rgba(94,234,212,.08); }
.tool-row, .crop-row { display: flex; gap: 6px; margin-bottom: 8px; flex-wrap: wrap; }
.tool-row .btn, .crop-row .btn { flex: 1; min-width: 0; padding: 6px 8px; font-size: 11px; }

/* ---------- Stage ---------- */
.stage { display: flex; gap: 10px; flex-wrap: nowrap; align-items: stretch; min-height: 0; flex: 1; }
.stage-import, .stage-desk { flex-direction: column; position: relative; flex: 1; min-height: calc(100vh - 118px); }
.center-stage { justify-content: center; flex-wrap: wrap; }
.stage-empty, .stage-dropzone {
  position: absolute; inset: 0; z-index: 3;
  border: 2px dashed var(--line); border-radius: var(--radius);
  display: grid; place-items: center; color: var(--muted); font-size: 14px;
  cursor: pointer; transition: border-color .15s, background .15s;
  background: rgba(12, 14, 18, .35);
}
.stage-empty .dz-inner { text-align: center; pointer-events: none; }
.stage-empty .dz-icon { font-size: 42px; color: var(--accent); margin-bottom: 8px; }
.stage-empty p { margin: 6px 0 0; }
.stage-empty .hint { color: var(--muted); font-size: 12px; }
.stage-empty.drag, .stage-dropzone.drag {
  border-color: var(--accent); background: rgba(94, 234, 212, .06);
}
.preview-desk, .stage-desk {
  position: absolute; inset: 0;
  background: #0c0e12; border: 1px solid var(--line); border-radius: var(--radius); overflow: hidden;
}
.panel-dock {
  position: absolute; left: 8px; bottom: 8px; z-index: 50;
  display: flex; gap: 6px; flex-wrap: wrap;
}
.panel-dock-btn {
  border: 1px dashed var(--accent-2); background: rgba(28,32,41,.92); color: var(--text);
  border-radius: 6px; padding: 4px 10px; font-size: 11px; cursor: pointer;
}
.panel-dock-btn:hover { border-color: var(--accent); color: var(--accent); }

.float-panel {
  position: absolute; display: flex; flex-direction: column;
  min-width: 140px; min-height: 100px;
  background: var(--panel); border: 1px solid var(--line); border-radius: 8px;
  box-shadow: var(--shadow); overflow: hidden;
}
.float-panel[hidden] { display: none !important; }
.fp-head {
  display: flex; align-items: center; gap: 8px; padding: 5px 8px;
  background: var(--panel-2); border-bottom: 1px solid var(--line);
  cursor: grab; user-select: none; flex-shrink: 0;
}
.fp-head:active { cursor: grabbing; }
.fp-title { font-size: 11px; font-weight: 700; text-transform: uppercase; letter-spacing: .35px; color: var(--text); }
.fp-hint { font-size: 10px; color: var(--muted); margin-right: auto; }
.fp-hint em { color: var(--accent); font-style: normal; }
.fp-btn {
  border: none; background: transparent; color: var(--muted);
  width: 22px; height: 22px; border-radius: 4px; cursor: pointer; font-size: 14px; line-height: 1;
}
.fp-btn:hover { background: rgba(255,255,255,.06); color: var(--text); }
.fp-body { flex: 1; min-height: 0; display: flex; flex-direction: column; }
.fp-resize {
  position: absolute; right: 0; bottom: 0; width: 14px; height: 14px;
  cursor: nwse-resize; background: linear-gradient(135deg, transparent 50%, rgba(94,234,212,.45) 50%);
}

.source-holder, .result-holder, .canvas-holder {
  flex: 1; display: flex; align-items: center; justify-content: center;
  min-height: 0; background: #141820; overflow: hidden; position: relative;
  touch-action: none;
}
.canvas-holder.pan-enabled { cursor: grab; }
.canvas-holder.panning { cursor: grabbing; }
.canvas-pan-layer {
  position: relative; flex-shrink: 0; will-change: transform;
}
.canvas-pan-layer canvas { display: block; }
.result-holder canvas, .canvas-holder[data-fit="cover"] canvas {
  max-width: none !important; max-height: none !important;
  flex-shrink: 0; image-rendering: pixelated; image-rendering: crisp-edges;
}
.source-holder canvas, .canvas-holder[data-fit="contain"] canvas {
  max-width: none; max-height: none; flex-shrink: 0;
}
.editor-viewport.canvas-holder { border: none; border-radius: 0; max-height: none; }
.canvas-card {
  background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius);
  padding: 10px; box-shadow: var(--shadow);
}
.canvas-card.wide, .sheet-stage { flex: 1; min-width: 0; display: flex; flex-direction: column; min-height: calc(100vh - 130px); }
.canvas-head { font-size: 11px; color: var(--muted); margin-bottom: 6px; text-transform: uppercase; letter-spacing: .4px; }
.canvas-head em { color: var(--accent); font-style: normal; }
canvas { display: block; max-width: 100%; border-radius: 4px; }
.pixelated { image-rendering: pixelated; image-rendering: crisp-edges; }
.checker {
  background-image:
    linear-gradient(45deg, #2a2f3a 25%, transparent 25%),
    linear-gradient(-45deg, #2a2f3a 25%, transparent 25%),
    linear-gradient(45deg, transparent 75%, #2a2f3a 75%),
    linear-gradient(-45deg, transparent 75%, #2a2f3a 75%);
  background-size: 16px 16px;
  background-position: 0 0, 0 8px, 8px -8px, -8px 0;
  background-color: #20242e;
}
#sourceCanvas { width: auto; height: auto; }

/* ---------- Crop ---------- */
.source-holder { position: relative; }
.crop-overlay, .crop-region {
  position: absolute; border: 2px dashed var(--accent); background: rgba(94,234,212,.1);
  pointer-events: none; z-index: 2;
}
.crop-region { border-color: var(--accent-2); background: rgba(124,156,255,.08); }
.crop-overlays { position: absolute; left: 0; top: 0; width: 100%; height: 100%; pointer-events: none; }

/* ---------- Animate ---------- */
.playbar { display: flex; align-items: center; gap: 8px; margin-top: 8px; }
.frame-count { color: var(--muted); font-size: 11px; font-family: 'JetBrains Mono', monospace; }
.frame-strip { display: flex; gap: 4px; flex-wrap: wrap; max-width: 340px; }
.frame-strip canvas { width: 44px; height: 44px; border: 1px solid var(--line); }
.note { color: var(--muted); font-size: 11px; margin: 6px 0 2px; line-height: 1.45; }
.note.compact-note { margin: 4px 0 0; }
.sheet-playback .tool-row { margin-bottom: 6px; }

/* ---------- Export ---------- */
.export-list { margin: 6px 0 10px; }
.meta {
  background: var(--bg-2); border: 1px solid var(--line); border-radius: 6px; padding: 10px;
  font-family: 'JetBrains Mono', monospace; font-size: 11px; color: var(--accent);
  max-height: 320px; overflow: auto; white-space: pre-wrap; margin: 0;
}
#sheetCanvas { max-width: 100%; }

/* ---------- Segmented control ---------- */
.seg { display: flex; background: var(--bg-2); border: 1px solid var(--line); border-radius: 8px; padding: 2px; gap: 2px; }
.seg-btn {
  flex: 1; border: none; background: transparent; color: var(--muted);
  padding: 6px 8px; border-radius: 6px; cursor: pointer; font-size: 11.5px; font-weight: 600;
}
.seg-btn.active { background: var(--accent); color: #06131a; }
.mode-group .note { margin-bottom: 0; }

/* ---------- Frame thumbnails (import) ---------- */
.frame-thumbs { display: flex; flex-wrap: wrap; gap: 6px; }
.frame-thumb { position: relative; }
.frame-thumb canvas {
  width: 48px; height: 48px; border: 1px solid var(--line); border-radius: 5px;
  image-rendering: pixelated; background: #20242e;
}
.frame-thumb .idx {
  position: absolute; top: 2px; left: 3px; font-size: 9px; color: var(--accent);
  font-family: 'JetBrains Mono', monospace;
}
.frame-thumb .del {
  position: absolute; top: -5px; right: -5px; width: 16px; height: 16px; border-radius: 50%;
  background: var(--danger); color: #fff; border: none; cursor: pointer; font-size: 10px; line-height: 1;
}

/* ---------- Parts list (rig) ---------- */
.parts-list { display: flex; flex-direction: column; gap: 4px; margin-bottom: 8px; }
.part-chip {
  display: flex; align-items: center; gap: 6px; padding: 6px 8px; border-radius: 6px;
  background: var(--panel-2); border: 1px solid var(--line); cursor: pointer; font-size: 12px;
}
.part-chip.active { border-color: var(--accent); }
.part-chip .swatch { width: 10px; height: 10px; border-radius: 3px; }
.part-chip .pmeta { margin-left: auto; color: var(--muted); font-size: 10px; font-family: 'JetBrains Mono', monospace; }
.sep { border: none; border-top: 1px solid var(--line); margin: 10px 0; }
.text-input {
  background: var(--panel-2); color: var(--text); border: 1px solid var(--line);
  border-radius: 6px; padding: 6px 8px; font-size: 12px;
}
#rigCanvas { max-width: 100%; }

/* ---------- Block face net (cube unwrap) ---------- */
.face-net {
  display: grid;
  grid-template-columns: repeat(4, 44px);
  grid-template-rows: repeat(3, 44px);
  gap: 3px;
  justify-content: center;
}
.face-slot {
  width: 44px; height: 44px; border: 1px solid var(--line); border-radius: 5px;
  background: var(--panel-2); cursor: pointer; position: relative; overflow: hidden;
  image-rendering: pixelated;
}
.face-slot.empty { background-image:
  linear-gradient(45deg, #2a2f3a 25%, transparent 25%),
  linear-gradient(-45deg, #2a2f3a 25%, transparent 25%),
  linear-gradient(45deg, transparent 75%, #2a2f3a 75%),
  linear-gradient(-45deg, transparent 75%, #2a2f3a 75%);
  background-size: 10px 10px; background-color: #20242e;
}
.face-slot.active { border-color: var(--accent); box-shadow: 0 0 0 2px rgba(94,234,212,.3); }
.face-slot .lbl {
  position: absolute; bottom: 0; left: 0; right: 0; text-align: center;
  font-size: 7px; color: var(--muted); background: rgba(0,0,0,.45); padding: 1px 0;
}
.face-slot[data-pos="top"] { grid-column: 2; grid-row: 1; }
.face-slot[data-pos="left"] { grid-column: 1; grid-row: 2; }
.face-slot[data-pos="front"] { grid-column: 2; grid-row: 2; }
.face-slot[data-pos="right"] { grid-column: 3; grid-row: 2; }
.face-slot[data-pos="back"] { grid-column: 4; grid-row: 2; }
.face-slot[data-pos="bottom"] { grid-column: 2; grid-row: 3; }
.block-faces { max-width: 400px; }

/* ---------- Shade picker ---------- */
.shade-picker { margin-bottom: 8px; }
.shade-label { display: block; color: var(--muted); font-size: 11px; margin-bottom: 4px; }
.swatches { display: flex; gap: 4px; flex-wrap: wrap; }
.swatch-btn {
  width: 26px; height: 26px; border-radius: 5px; border: 2px solid var(--line); cursor: pointer;
  background-image:
    linear-gradient(45deg, #2a2f3a 25%, transparent 25%),
    linear-gradient(-45deg, #2a2f3a 25%, transparent 25%),
    linear-gradient(45deg, transparent 75%, #2a2f3a 75%),
    linear-gradient(-45deg, transparent 75%, #2a2f3a 75%);
  background-size: 10px 10px; background-position: 0 0, 0 5px, 5px -5px, -5px 0; position: relative;
}
.swatch-btn span { position: absolute; inset: 0; border-radius: 2px; }
.swatch-btn.active { border-color: var(--accent); box-shadow: 0 0 0 2px rgba(94,234,212,.35); }

.checkbox.inline { margin: 0; font-size: 11px; gap: 4px; }
.checkbox.inline input { width: 13px; height: 13px; }

.frame-thumb { cursor: grab; }
.frame-thumb.dragging { opacity: .4; }
.frame-thumb.drop-target canvas { outline: 2px solid var(--accent); }
.frame-thumb .hold {
  position: absolute; bottom: 2px; right: 3px; font-size: 8px; color: var(--accent-2);
  font-family: 'JetBrains Mono', monospace; background: rgba(0,0,0,.5); padding: 0 2px; border-radius: 2px;
}

.toast {
  position: fixed; bottom: 20px; left: 50%; transform: translateX(-50%);
  background: var(--panel-2); border: 1px solid var(--accent-2); color: var(--text);
  padding: 10px 16px; border-radius: 8px; box-shadow: var(--shadow); font-size: 12px; z-index: 50;
}
.toast.error { border-color: var(--danger); }

@media (max-width: 900px) {
  .panel { grid-template-columns: 1fr; min-height: auto; }
  .controls { position: static; max-height: none; }
  .preview-desk, .stage-desk { min-height: 360px; }
}
