:root {
  --bg-app: #0c0e13;
  --bg-panel: #13161f;
  --bg-card: #191d28;
  --bg-card-hover: #1f2432;
  --bg-input: #0e1017;
  --border: #262b3a;
  --border-strong: #374056;
  --text-main: #edf0f7;
  --text-muted: #939bb0;
  --text-dim: #687086;
  --accent: #6366f1;
  --accent-hover: #7578ff;
  --cyan: #22d3ee;
  --amber: #f59e0b;
  --danger: #ef4444;
  --radius-sm: 6px;
  --radius-md: 9px;
  --font-sans: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Inter, sans-serif;
  --font-mono: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, monospace;
}

*,
*::before,
*::after {
  box-sizing: border-box;
}

html,
body {
  margin: 0;
  padding: 0;
  width: 100%;
  height: 100%;
  overflow: hidden;
  background: var(--bg-app);
  color: var(--text-main);
  font-family: var(--font-sans);
  font-size: 13px;
  -webkit-font-smoothing: antialiased;
}

.app-shell {
  display: flex;
  flex-direction: column;
  width: 100%;
  height: 100%;
}

/* Topbar */
.topbar {
  height: 46px;
  flex-shrink: 0;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 0 14px;
  background: var(--bg-panel);
  border-bottom: 1px solid var(--border);
}

.topbar-left,
.topbar-center,
.topbar-right {
  display: flex;
  align-items: center;
  gap: 10px;
}

.brand {
  display: flex;
  align-items: center;
  gap: 8px;
}

.brand-dot {
  width: 9px;
  height: 9px;
  border-radius: 50%;
  background: linear-gradient(135deg, var(--accent), var(--cyan));
  box-shadow: 0 0 10px rgba(99, 102, 241, 0.55);
}

.brand-title {
  font-weight: 650;
  font-size: 13.5px;
  letter-spacing: -0.01em;
}

.badge-dim {
  font-family: var(--font-mono);
  font-size: 11px;
  padding: 2px 6px;
  border-radius: 4px;
  background: rgba(255, 255, 255, 0.05);
  border: 1px solid var(--border);
  color: var(--text-muted);
}

.stage-meta {
  font-family: var(--font-mono);
  font-size: 11.5px;
  color: var(--text-dim);
  font-variant-numeric: tabular-nums;
}

.segmented {
  display: inline-flex;
  background: var(--bg-input);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 2px;
  gap: 2px;
}

.seg-btn {
  appearance: none;
  border: none;
  background: transparent;
  color: var(--text-muted);
  font-size: 12px;
  font-weight: 500;
  padding: 4px 10px;
  border-radius: 4px;
  cursor: pointer;
  transition: background 0.12s, color 0.12s;
}

.seg-btn:hover {
  color: var(--text-main);
}

.seg-btn.is-active {
  background: var(--accent);
  color: #fff;
}

.tool-btn {
  appearance: none;
  display: inline-flex;
  align-items: center;
  gap: 6px;
  background: var(--bg-card);
  border: 1px solid var(--border);
  color: var(--text-muted);
  font-size: 12px;
  font-weight: 500;
  padding: 5px 10px;
  border-radius: var(--radius-sm);
  cursor: pointer;
  transition: border-color 0.12s, background 0.12s, color 0.12s;
}

.tool-btn:hover {
  border-color: var(--border-strong);
  color: var(--text-main);
}

.tool-btn.is-active {
  background: rgba(99, 102, 241, 0.16);
  border-color: var(--accent);
  color: var(--text-main);
}

.tool-btn .dot-indicator {
  width: 7px;
  height: 7px;
  border-radius: 50%;
  background: var(--text-dim);
}

.tool-btn.is-active .dot-indicator {
  background: var(--cyan);
  box-shadow: 0 0 8px var(--cyan);
}

kbd {
  font-family: var(--font-mono);
  font-size: 10.5px;
  padding: 1px 4px;
  border-radius: 3px;
  background: rgba(255, 255, 255, 0.08);
  border: 1px solid rgba(255, 255, 255, 0.12);
  color: var(--text-muted);
}

/* Workspace Split */
.workspace {
  flex: 1;
  min-height: 0;
  display: flex;
  overflow: hidden;
}

/* Stage Viewport */
.stage-viewport {
  flex: 1;
  min-width: 0;
  min-height: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 18px;
  background: radial-gradient(circle at center, #141824 0%, #0a0c11 100%);
  position: relative;
  overflow: hidden;
}

/* Strict 16:9 container sized dynamically to fit viewport */
.stage-frame {
  position: relative;
  border-radius: 6px;
  overflow: hidden;
  box-shadow:
    0 0 0 1px rgba(255, 255, 255, 0.12),
    0 22px 60px rgba(0, 0, 0, 0.65);
  user-select: none;
  flex-shrink: 0;
}

/* Exact 2560 x 1440 coordinate space scaled via CSS transform */
.canvas-stage {
  width: 2560px;
  height: 1440px;
  position: relative;
  transform-origin: top left;
  overflow: hidden;
  outline: none;
  cursor: grab;
}

.canvas-stage.is-dragging {
  cursor: grabbing;
}

.checkerboard-bg {
  position: absolute;
  inset: 0;
  background-color: #161922;
  background-image:
    linear-gradient(45deg, #1d212d 25%, transparent 25%),
    linear-gradient(-45deg, #1d212d 25%, transparent 25%),
    linear-gradient(45deg, transparent 75%, #1d212d 75%),
    linear-gradient(-45deg, transparent 75%, #1d212d 75%);
  background-size: 64px 64px;
  background-position: 0 0, 0 32px, 32px -32px, -32px 0;
  pointer-events: none;
}

/* Layers */
.layer {
  position: absolute;
  inset: 0;
  width: 2560px;
  height: 1440px;
}

/* Layer 1 & Layer 3: fixed 2560x1440 16:9 ratio */
.layer-1 {
  z-index: 1;
  background-size: cover;
  background-position: center;
}

.layer-1.is-locked {
  pointer-events: none;
}

.layer-1 img,
.layer-3 img {
  width: 2560px;
  height: 1440px;
  object-fit: cover;
  display: block;
  user-select: none;
  -webkit-user-drag: none;
}

.layer-1.preset-dark {
  background: radial-gradient(circle at 35% 30%, #24293d 0%, #101219 70%, #090a0e 100%);
}

.layer-1.preset-amber {
  background: radial-gradient(circle at 28% 35%, #5b3a12 0%, #21150a 48%, #0c0d12 100%);
}

.layer-1.preset-cyan {
  background: radial-gradient(circle at 32% 35%, #0e4b5c 0%, #111f2d 50%, #0b0d13 100%);
}

/* Layer 2: Any resolution, natural size scaled & translated in 2560x1440 space */
.layer-2 {
  z-index: 2;
}

.layer-2-transform {
  position: absolute;
  top: 0;
  left: 0;
  transform-origin: top left;
  will-change: transform;
}

.layer-2-transform img {
  display: block;
  max-width: none;
  max-height: none;
  width: auto;
  height: auto;
  user-select: none;
  -webkit-user-drag: none;
}

.layer-2-bbox {
  position: absolute;
  inset: 0;
  border: 3px dashed rgba(34, 211, 238, 0.75);
  box-shadow: inset 0 0 0 1px rgba(0, 0, 0, 0.35);
  pointer-events: none;
}

/* Layer 3: Top UI Overlay Frame — pointer-events: none when locked! */
.layer-3 {
  z-index: 3;
}

.layer-3.is-locked {
  pointer-events: none;
}

.sample-ui-guide {
  position: absolute;
  inset: 0;
  width: 2560px;
  height: 1440px;
  pointer-events: none;
}

/* Guides Overlay */
.guides-overlay {
  position: absolute;
  inset: 0;
  z-index: 4;
  pointer-events: none;
}

.guides-overlay[hidden] {
  display: none;
}

.guide-v {
  position: absolute;
  left: 50%;
  top: 0;
  bottom: 0;
  width: 2px;
  background: rgba(34, 211, 238, 0.35);
}

.guide-h {
  position: absolute;
  top: 50%;
  left: 0;
  right: 0;
  height: 2px;
  background: rgba(34, 211, 238, 0.35);
}

.guide-thirds-v1,
.guide-thirds-v2 {
  position: absolute;
  top: 0;
  bottom: 0;
  width: 1px;
  background: rgba(255, 255, 255, 0.12);
}

.guide-thirds-v1 { left: 33.3333%; }
.guide-thirds-v2 { left: 66.6667%; }

.guide-thirds-h1,
.guide-thirds-h2 {
  position: absolute;
  left: 0;
  right: 0;
  height: 1px;
  background: rgba(255, 255, 255, 0.12);
}

.guide-thirds-h1 { top: 33.3333%; }
.guide-thirds-h2 { top: 66.6667%; }

/* Empty Hint */
.empty-hint {
  position: absolute;
  inset: 0;
  z-index: 5;
  display: flex;
  align-items: center;
  justify-content: center;
  pointer-events: none;
}

.empty-hint[hidden] {
  display: none;
}

.empty-hint-card {
  background: rgba(13, 15, 22, 0.86);
  border: 2px solid rgba(255, 255, 255, 0.14);
  border-radius: 28px;
  padding: 56px 76px;
  text-align: center;
  backdrop-filter: blur(10px);
}

.empty-title {
  margin: 0 0 18px;
  font-size: 46px;
  font-weight: 650;
  color: #fff;
}

.empty-sub {
  margin: 10px 0;
  font-size: 28px;
  color: var(--text-muted);
}

.empty-sub kbd {
  font-size: 24px;
  padding: 4px 12px;
  border-radius: 8px;
}

/* Screen-space Stage HUD */
.stage-hud {
  position: absolute;
  left: 10px;
  bottom: 10px;
  z-index: 10;
  padding: 5px 9px;
  border-radius: 5px;
  background: rgba(12, 14, 20, 0.78);
  border: 1px solid rgba(255, 255, 255, 0.12);
  font-family: var(--font-mono);
  font-size: 11.5px;
  color: var(--text-main);
  font-variant-numeric: tabular-nums;
  pointer-events: none;
  backdrop-filter: blur(6px);
}

/* Drop Overlay */
.drop-overlay {
  position: absolute;
  inset: 0;
  z-index: 20;
  background: rgba(10, 12, 18, 0.84);
  backdrop-filter: blur(6px);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 24px;
}

.drop-overlay[hidden] {
  display: none;
}

.drop-targets {
  display: grid;
  grid-template-columns: repeat(3, minmax(140px, 1fr));
  gap: 14px;
  width: 100%;
  max-width: 680px;
}

.drop-zone {
  border: 2px dashed var(--border-strong);
  border-radius: 10px;
  padding: 28px 16px;
  text-align: center;
  color: var(--text-muted);
  background: rgba(25, 29, 40, 0.75);
  transition: border-color 0.15s, background 0.15s, color 0.15s, transform 0.15s;
}

.drop-zone.is-primary {
  border-color: var(--accent);
  color: var(--text-main);
}

.drop-zone.is-hover {
  border-color: var(--cyan);
  background: rgba(34, 211, 238, 0.12);
  color: #fff;
  transform: scale(1.02);
}

/* Right Sidebar */
.sidebar {
  width: 368px;
  flex-shrink: 0;
  background: var(--bg-panel);
  border-left: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  overflow-y: auto;
  overflow-x: hidden;
}

.sidebar.is-collapsed {
  display: none;
}

.panel-section {
  padding: 12px 14px;
  border-bottom: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.section-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
}

.section-header h2 {
  margin: 0;
  font-size: 12px;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--text-muted);
  font-weight: 650;
}

.hint-text {
  font-size: 11px;
  color: var(--text-dim);
}

/* Layer Cards */
.layer-card {
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: var(--radius-md);
  padding: 10px;
  display: flex;
  flex-direction: column;
  gap: 8px;
  transition: border-color 0.12s, background 0.12s;
}

.layer-card.is-selected {
  border-color: var(--accent);
  box-shadow: 0 0 0 1px rgba(99, 102, 241, 0.25);
}

.layer-card-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
}

.layer-title-group {
  display: flex;
  align-items: center;
  gap: 8px;
  min-width: 0;
}

.layer-badge {
  font-family: var(--font-mono);
  font-size: 11px;
  font-weight: 700;
  padding: 2px 6px;
  border-radius: 4px;
  flex-shrink: 0;
}

.layer-badge.l3 { background: rgba(245, 158, 11, 0.16); color: #fbbf24; border: 1px solid rgba(245, 158, 11, 0.35); }
.layer-badge.l2 { background: rgba(34, 211, 238, 0.16); color: #67e8f9; border: 1px solid rgba(34, 211, 238, 0.35); }
.layer-badge.l1 { background: rgba(148, 163, 184, 0.15); color: #cbd5e1; border: 1px solid rgba(148, 163, 184, 0.3); }

.layer-name {
  font-size: 12.5px;
  font-weight: 600;
  line-height: 1.2;
}

.layer-sub {
  font-family: var(--font-mono);
  font-size: 10.5px;
  color: var(--text-dim);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.layer-actions {
  display: flex;
  align-items: center;
  gap: 4px;
  flex-shrink: 0;
}

.icon-btn {
  appearance: none;
  width: 26px;
  height: 24px;
  border-radius: 5px;
  border: 1px solid var(--border);
  background: var(--bg-input);
  color: var(--text-muted);
  font-size: 12px;
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  justify-content: center;
}

.icon-btn:hover {
  border-color: var(--border-strong);
  color: var(--text-main);
}

.icon-btn.is-off {
  opacity: 0.45;
}

.icon-btn.is-active {
  background: rgba(34, 211, 238, 0.16);
  border-color: var(--cyan);
  color: var(--cyan);
}

.icon-btn.is-unlocked {
  background: rgba(245, 158, 11, 0.16);
  border-color: var(--amber);
  color: #fbbf24;
}

.layer-controls-row {
  display: flex;
  align-items: center;
  gap: 6px;
  flex-wrap: wrap;
}

.file-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  padding: 5px 9px;
  border-radius: var(--radius-sm);
  border: 1px solid var(--border-strong);
  background: #232837;
  color: var(--text-main);
  font-size: 11.5px;
  font-weight: 500;
  cursor: pointer;
}

.file-btn:hover {
  background: #2b3144;
}

.file-btn.primary {
  background: var(--accent);
  border-color: var(--accent);
  color: #fff;
}

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

.mini-btn {
  appearance: none;
  padding: 5px 8px;
  border-radius: var(--radius-sm);
  border: 1px solid var(--border);
  background: var(--bg-input);
  color: var(--text-muted);
  font-size: 11.5px;
  cursor: pointer;
}

.mini-btn:hover {
  border-color: var(--border-strong);
  color: var(--text-main);
}

.mini-btn.is-active {
  border-color: var(--accent);
  background: rgba(99, 102, 241, 0.18);
  color: #fff;
}

.mini-btn.accent {
  background: rgba(34, 211, 238, 0.15);
  border-color: rgba(34, 211, 238, 0.45);
  color: #a5f3fc;
}

.mini-btn.accent:hover {
  background: rgba(34, 211, 238, 0.25);
}

.mini-btn.danger:hover {
  border-color: rgba(239, 68, 68, 0.5);
  color: #fca5a5;
}

.mini-select {
  background: var(--bg-input);
  border: 1px solid var(--border);
  color: var(--text-muted);
  border-radius: var(--radius-sm);
  padding: 4px 6px;
  font-size: 11.5px;
  flex: 1;
  min-width: 90px;
}

/* Nanoka Loader */
.nanoka-loader {
  display: flex;
  flex-direction: column;
  gap: 5px;
}

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

.input-row input,
.template-details input {
  flex: 1;
  min-width: 0;
  background: var(--bg-input);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 5px 8px;
  color: var(--text-main);
  font-family: var(--font-mono);
  font-size: 11.5px;
}

.input-row input:focus,
.template-details input:focus,
.num-field input:focus,
select:focus {
  outline: none;
  border-color: var(--accent);
}

.template-details {
  font-size: 11px;
  color: var(--text-dim);
}

.template-details summary {
  cursor: pointer;
  user-select: none;
}

.template-details input {
  margin-top: 5px;
  width: 100%;
  font-size: 10.5px;
}

/* Transform Inputs */
.transform-grid {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 6px;
}

.num-field {
  display: flex;
  flex-direction: column;
  gap: 3px;
}

.num-field span {
  font-family: var(--font-mono);
  font-size: 10.5px;
  color: var(--text-dim);
}

.num-field input {
  width: 100%;
  background: var(--bg-input);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 5px 6px;
  color: var(--text-main);
  font-family: var(--font-mono);
  font-size: 12px;
  font-variant-numeric: tabular-nums;
}

.layer-inline-sliders {
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.inline-slider {
  display: grid;
  grid-template-columns: 48px 1fr 44px;
  align-items: center;
  gap: 8px;
  font-size: 11px;
  color: var(--text-muted);
}

.inline-slider input[type="range"] {
  width: 100%;
  accent-color: var(--accent);
  cursor: pointer;
}

.mono-val {
  font-family: var(--font-mono);
  font-size: 11px;
  text-align: right;
  color: var(--text-main);
  font-variant-numeric: tabular-nums;
}

.inline-offset-note {
  font-size: 11px;
  color: var(--text-dim);
}

/* Export Section */
.export-section {
  flex: 1;
  border-bottom: none;
}

.tab-pills {
  display: inline-flex;
  background: var(--bg-input);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 2px;
  gap: 2px;
}

.tab-pill {
  appearance: none;
  border: none;
  background: transparent;
  color: var(--text-muted);
  font-size: 11px;
  font-weight: 500;
  padding: 3px 8px;
  border-radius: 4px;
  cursor: pointer;
}

.tab-pill.is-active {
  background: var(--bg-card-hover);
  color: var(--text-main);
}

.export-options-row {
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.compact-select {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 11.5px;
  color: var(--text-muted);
}

.compact-select select {
  flex: 1;
  background: var(--bg-input);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 4px 6px;
  color: var(--text-main);
  font-size: 11.5px;
}

.export-tab-body {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.export-tab-body[hidden] {
  display: none;
}

.json-preview {
  margin: 0;
  padding: 10px;
  border-radius: var(--radius-sm);
  background: var(--bg-input);
  border: 1px solid var(--border);
  font-family: var(--font-mono);
  font-size: 11.5px;
  line-height: 1.45;
  color: #a5f3fc;
  overflow-x: auto;
  max-height: 220px;
  font-variant-numeric: tabular-nums;
}

.export-actions {
  display: flex;
  gap: 6px;
  flex-wrap: wrap;
}

.action-btn {
  appearance: none;
  flex: 1;
  min-width: 84px;
  padding: 7px 10px;
  border-radius: var(--radius-sm);
  border: 1px solid var(--border-strong);
  background: var(--bg-card);
  color: var(--text-main);
  font-size: 12px;
  font-weight: 500;
  cursor: pointer;
  white-space: nowrap;
}

.action-btn:hover {
  background: var(--bg-card-hover);
}

.action-btn.primary {
  background: var(--accent);
  border-color: var(--accent);
  color: #fff;
}

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

.action-btn.danger-outline {
  flex: 0 0 auto;
  color: #fca5a5;
  border-color: rgba(239, 68, 68, 0.35);
}

.import-json-row {
  display: flex;
  justify-content: flex-end;
}

.text-link-btn {
  appearance: none;
  border: none;
  background: none;
  color: var(--text-dim);
  font-size: 11px;
  cursor: pointer;
  padding: 2px 0;
  text-decoration: underline;
}

.text-link-btn:hover {
  color: var(--text-main);
}

/* Batch List */
.batch-list {
  display: flex;
  flex-direction: column;
  gap: 6px;
  max-height: 220px;
  overflow-y: auto;
  padding: 4px;
  border-radius: var(--radius-sm);
  background: var(--bg-input);
  border: 1px solid var(--border);
}

.batch-empty {
  padding: 14px 10px;
  text-align: center;
  font-size: 11.5px;
  color: var(--text-dim);
}

.batch-item {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  padding: 6px 8px;
  border-radius: 5px;
  background: var(--bg-card);
  border: 1px solid var(--border);
}

.batch-item-info {
  min-width: 0;
  cursor: pointer;
  flex: 1;
}

.batch-item-id {
  font-family: var(--font-mono);
  font-size: 12px;
  font-weight: 600;
  color: var(--cyan);
}

.batch-item-vals {
  font-family: var(--font-mono);
  font-size: 10.5px;
  color: var(--text-muted);
}

.batch-item-del {
  appearance: none;
  border: none;
  background: transparent;
  color: var(--text-dim);
  cursor: pointer;
  font-size: 13px;
  padding: 2px 5px;
}

.batch-item-del:hover {
  color: #fca5a5;
}

/* Toast */
.toast {
  position: fixed;
  bottom: 18px;
  left: 50%;
  transform: translateX(-50%);
  z-index: 100;
  background: #1e2433;
  border: 1px solid var(--accent);
  color: #fff;
  padding: 8px 14px;
  border-radius: 7px;
  font-size: 12.5px;
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.5);
}

@media (max-width: 980px) {
  .workspace {
    flex-direction: column;
  }
  .sidebar {
    width: 100%;
    height: 44%;
    border-left: none;
    border-top: 1px solid var(--border);
  }
}
