:root {
  --bg: #0a0a0a;
  --fg: #e8e8e8;
  --panel: #141414;
  --border: #2a2a2a;
  --accent: #7cf;
  --warn: #f88;
}

* { box-sizing: border-box; }

html, body {
  margin: 0; padding: 0; height: 100%;
  background: var(--bg); color: var(--fg);
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
  overflow: hidden;
}

/* ---------------------------------------------------------------- stage */
#stage {
  position: absolute; inset: 0;
  display: flex; align-items: center; justify-content: center;
  padding: 24px 360px 48px 24px;
}

#wrap {
  position: relative;
  width: min(100%, calc(100vh - 72px));
  aspect-ratio: 1 / 1;
  background: #050505;
  border: 1px solid var(--border);
}

canvas {
  position: absolute; inset: 0;
  width: 100%; height: 100%;
  image-rendering: pixelated;
}

#overlay { position: absolute; inset: 0; pointer-events: none; }

/* layers UI overlay (selection/handles) */
#layers-overlay {
  position: absolute; inset: 0;
  pointer-events: none;
}
#layers-overlay .layer-box {
  position: absolute;
  border: 1px dashed rgba(124, 204, 255, 0.85);
  pointer-events: auto;
  cursor: move;
  background: rgba(124, 204, 255, 0.05);
}
#layers-overlay .layer-box.selected {
  border-style: solid;
  border-color: var(--accent);
  background: rgba(124, 204, 255, 0.08);
}
#layers-overlay .handle {
  position: absolute;
  width: 10px; height: 10px;
  background: var(--accent);
  border: 1px solid #000;
  pointer-events: auto;
}
#layers-overlay .handle.tl { top: -6px; left: -6px; cursor: nwse-resize; }
#layers-overlay .handle.tr { top: -6px; right: -6px; cursor: nesw-resize; }
#layers-overlay .handle.bl { bottom: -6px; left: -6px; cursor: nesw-resize; }
#layers-overlay .handle.br { bottom: -6px; right: -6px; cursor: nwse-resize; }
#layers-overlay .handle.t  { top: -6px; left: 50%; margin-left: -5px; cursor: ns-resize; }
#layers-overlay .handle.b  { bottom: -6px; left: 50%; margin-left: -5px; cursor: ns-resize; }
#layers-overlay .handle.l  { left: -6px; top: 50%; margin-top: -5px; cursor: ew-resize; }
#layers-overlay .handle.r  { right: -6px; top: 50%; margin-top: -5px; cursor: ew-resize; }

/* rotation handle */
#layers-overlay .rot-stem {
  position: absolute;
  width: 1px;
  height: 20px;
  background: var(--accent);
  top: -20px;
  left: 50%;
  pointer-events: none;
}
#layers-overlay .handle.rot {
  position: absolute;
  width: 12px; height: 12px;
  border-radius: 50%;
  background: var(--accent);
  border: 1px solid #000;
  pointer-events: auto;
  cursor: grab;
  top: -36px;
  left: 50%;
  margin-left: -6px;
}
#layers-overlay .handle.rot:active { cursor: grabbing; }

/* crop mode */
#layers-overlay .layer-box.crop-mode {
  border-color: #f90;
  border-style: dashed;
  cursor: default;
}
#layers-overlay .crop-dim {
  position: absolute;
  background: rgba(0, 0, 0, 0.50);
  pointer-events: none;
}
#layers-overlay .handle.crop-edge {
  background: #f90;
  border: none;
  pointer-events: auto;
}
#layers-overlay .handle.crop-edge.crop-l,
#layers-overlay .handle.crop-edge.crop-r {
  width: 4px; height: auto;
  margin-left: -2px;
  cursor: ew-resize;
}
#layers-overlay .handle.crop-edge.crop-r {
  margin-left: 0; margin-right: -2px;
}
#layers-overlay .handle.crop-edge.crop-t,
#layers-overlay .handle.crop-edge.crop-b {
  height: 4px; width: auto;
  margin-top: -2px;
  cursor: ns-resize;
}
#layers-overlay .handle.crop-edge.crop-b {
  margin-top: 0; margin-bottom: -2px;
}

#layers-overlay .layer-label {
  position: absolute;
  top: -44px; left: 0;
  font-size: 10px;
  background: var(--panel);
  border: 1px solid var(--border);
  padding: 2px 6px;
  white-space: nowrap;
  pointer-events: none;
}

/* ------------------------------------------------------------- HUD/loupe */
#wrap.loupe-on { cursor: crosshair; }

#loupe {
  position: absolute;
  pointer-events: none;
  border-radius: 50%;
  border: 2px solid rgba(124, 255, 255, 0.9);
  box-shadow: 0 0 0 1px rgba(0,0,0,0.8), 0 6px 24px rgba(0,0,0,0.6);
  background: #000;
  display: none;
  image-rendering: pixelated;
  overflow: hidden;
}
#loupe canvas {
  position: absolute; inset: 0;
  width: 100%; height: 100%;
  image-rendering: pixelated;
}

#loupeReadout {
  position: absolute; left: 12px; bottom: 12px;
  font-size: 11px; color: #8a8a8a;
  background: rgba(0,0,0,0.55);
  padding: 6px 8px; border: 1px solid var(--border);
  display: none;
}
#loupeReadout b { color: var(--fg); font-weight: normal; }

#hud {
  position: absolute; left: 12px; top: 12px;
  font-size: 11px; color: #8a8a8a;
  background: rgba(0,0,0,0.55);
  padding: 6px 8px; border: 1px solid var(--border);
}
#hud b { color: var(--fg); font-weight: normal; }

/* ----------------------------------------------------------------- menu */
#menu {
  position: absolute; top: 0; right: 0; bottom: 0;
  width: 340px;
  background: var(--panel);
  border-left: 1px solid var(--border);
  transform: translateX(0);
  transition: transform .2s ease;
  display: flex; flex-direction: column;
}
#menu.collapsed { transform: translateX(320px); }
#menu header {
  display: flex; align-items: center; justify-content: space-between;
  padding: 12px 14px; border-bottom: 1px solid var(--border);
}
#menu header h1 {
  margin: 0; font-size: 12px; font-weight: 600; letter-spacing: .08em;
  text-transform: uppercase; color: #cfcfcf;
}
#toggle {
  background: none; border: 1px solid var(--border); color: var(--fg);
  width: 28px; height: 24px; cursor: pointer; font-size: 14px; line-height: 1;
}
#toggle:hover { border-color: var(--accent); color: var(--accent); }

#menu-body { flex: 1; overflow-y: auto; }

.menu-section {
  border-bottom: 1px solid var(--border);
  padding: 12px 14px;
  display: flex; flex-direction: column; gap: 8px;
}
.menu-section h3 {
  margin: 0; font-size: 11px; font-weight: 600; letter-spacing: .06em;
  text-transform: uppercase; color: #cfcfcf;
}
.menu-section .row { display: flex; gap: 6px; }
.menu-section .opts { display: flex; flex-wrap: wrap; gap: 10px; font-size: 10px; color: #888; }
.menu-section .opts label { display: flex; align-items: center; gap: 4px; }
.menu-section .msg { font-size: 10px; color: #888; min-height: 12px; }
.menu-section .msg.err { color: var(--warn); }

label.file {
  display: block;
  border: 1px dashed var(--border);
  padding: 14px 8px; text-align: center;
  font-size: 11px; color: #9a9a9a; cursor: pointer;
}
label.file:hover { border-color: var(--accent); color: var(--accent); }
label.file.dragover { border-color: var(--accent); color: var(--accent); background: #0f1518; }
label.file input[type=file] { display: none; }

input[type=text], input[type=number] {
  flex: 1; background: #0a0a0a; border: 1px solid var(--border);
  color: var(--fg); padding: 6px 8px; font: inherit; font-size: 11px;
  outline: none; min-width: 0;
}
input[type=text]:focus, input[type=number]:focus { border-color: var(--accent); }

button {
  background: #0a0a0a; border: 1px solid var(--border); color: var(--fg);
  padding: 6px 10px; font: inherit; font-size: 11px; cursor: pointer;
}
button:hover { border-color: var(--accent); color: var(--accent); }
button:disabled { opacity: 0.4; cursor: not-allowed; }
button.danger:hover { border-color: var(--warn); color: var(--warn); }

/* maps list */
#maps { padding: 8px; display: flex; flex-direction: column; gap: 8px; }
.map {
  border: 1px solid var(--border);
  padding: 10px 12px;
  cursor: pointer; background: #0f0f0f;
}
.map:hover { border-color: var(--accent); }
.map.active { border-color: var(--accent); background: #151a1d; }
.map h2 { margin: 0 0 4px 0; font-size: 12px; color: #e0e0e0; font-weight: 600; }
.map p { margin: 0; font-size: 10px; color: #888; line-height: 1.4; }

/* layer list (composition) */
#layer-list { display: flex; flex-direction: column; gap: 4px; max-height: 320px; overflow-y: auto; }
.layer-item {
  display: flex; align-items: center; gap: 6px;
  padding: 6px 8px;
  border: 1px solid var(--border);
  background: #0f0f0f;
  font-size: 11px;
  cursor: pointer;
}
.layer-item:hover { border-color: var(--accent); }
.layer-item.selected { border-color: var(--accent); background: #151a1d; }
.layer-item .name { flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.layer-item .vis, .layer-item .up, .layer-item .down, .layer-item .del {
  width: 22px; height: 20px; padding: 0; font-size: 11px;
  display: inline-flex; align-items: center; justify-content: center;
}
.layer-item .vis { font-size: 12px; }
.layer-item.hidden .name { color: #555; text-decoration: line-through; }

#layer-list .empty {
  font-size: 10px; color: #666; padding: 12px; text-align: center;
}

/* selected layer detail */
#layer-detail {
  display: none;
  flex-direction: column; gap: 8px;
}
#layer-detail.active { display: flex; }
#layer-detail .grid {
  display: grid; grid-template-columns: auto 1fr auto 1fr;
  gap: 4px 6px; align-items: center;
  font-size: 10px; color: #aaa;
}
#layer-detail .grid input { padding: 4px 6px; font-size: 11px; }
#layer-detail .grid label { color: #888; }

/* footer */
#menu footer {
  padding: 10px 14px; border-top: 1px solid var(--border);
  font-size: 10px; color: #777;
}
#menu footer div { margin-bottom: 4px; }
#menu footer label { display: flex; gap: 8px; align-items: center; }
#menu footer input[type=range] { flex: 1; }

.tab {
  position: absolute; left: -28px; top: 14px;
  width: 28px; height: 28px;
  background: var(--panel); border: 1px solid var(--border);
  border-right: none;
  display: none; align-items: center; justify-content: center;
  cursor: pointer; color: var(--fg);
}
#menu.collapsed .tab { display: flex; }

/* output panel */
#output-panel select {
  flex: 1; background: #0a0a0a; border: 1px solid var(--border);
  color: var(--fg); padding: 6px 8px; font: inherit; font-size: 11px;
  outline: none;
}
#output-panel select:focus { border-color: var(--accent); }
#output-panel label.slider {
  display: grid; grid-template-columns: 80px 1fr 36px;
  align-items: center; gap: 8px;
  font-size: 10px; color: #888;
}
#output-panel label.slider input[type=range] { width: 100%; }
#output-panel label.slider span { text-align: right; color: #cfcfcf; }

/* generic small utilities */
.muted { color: #777; font-size: 10px; }
.divider { height: 1px; background: var(--border); margin: 4px 0; }
