:root {
  --bg: #1d1c1f;
  --panel: #262529;
  --panel-2: #2f2e33;
  --line: #3b3a40;
  --text: #e8e4dc;
  --muted: #9a958c;
  --accent: #e2a33a;
  --danger: #d0604c;
  --label-w: 180px;
  font: 13px/1.4 system-ui, -apple-system, "Segoe UI", sans-serif;
  color-scheme: dark;
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }

body {
  margin: 0;
  height: 100vh;
  display: grid;
  grid-template-columns: 268px 1fr 300px;
  grid-template-rows: 40px 1fr 230px;
  grid-template-areas:
    "top top top"
    "left view right"
    "tl tl tl";
  background: var(--bg);
  color: var(--text);
  overflow: hidden;
}

button {
  font: inherit;
  color: var(--text);
  background: var(--panel-2);
  border: 1px solid var(--line);
  border-radius: 4px;
  padding: 3px 9px;
  cursor: pointer;
}
button:hover { border-color: #55535b; }
button.primary { background: #5a4520; border-color: #8a6a30; }
button.active, .seg button.active { background: #4a3d24; border-color: var(--accent); color: #fff; }
button.danger { color: #f3b3a6; }

input, select, textarea {
  font: inherit;
  color: var(--text);
  background: #1a191c;
  border: 1px solid var(--line);
  border-radius: 4px;
  padding: 3px 6px;
  min-width: 0;
}
textarea { width: 100%; resize: vertical; min-height: 56px; }
input[type="number"] { width: 72px; }
input[type="checkbox"] { accent-color: var(--accent); }
input[type="range"] { accent-color: var(--accent); }

.sep { width: 1px; height: 20px; background: var(--line); margin: 0 4px; }
.spacer { flex: 1; }
.muted { color: var(--muted); }
.hint { color: var(--muted); font-size: 12px; }

/* ---- Top bar ---- */
.topbar {
  grid-area: top;
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 0 10px;
  background: var(--panel);
  border-bottom: 1px solid var(--line);
}
.brand { margin-right: 6px; }
.scene-name { color: var(--muted); }
.dirty { color: var(--accent); font-size: 12px; }

/* ---- Left ---- */
.left {
  grid-area: left;
  display: flex;
  flex-direction: column;
  background: var(--panel);
  border-right: 1px solid var(--line);
  min-height: 0;
}
.panel { padding: 10px; border-bottom: 1px solid var(--line); }
.panel h3 { margin: 0 0 8px; font-size: 11px; text-transform: uppercase; letter-spacing: 0.1em; color: var(--muted); }
.tools { display: grid; grid-template-columns: repeat(4, 1fr); gap: 4px; }
.tools button { padding: 6px 0; }
.tools kbd { display: block; font-size: 10px; color: var(--muted); }
.rotate-row { display: flex; align-items: center; justify-content: space-between; margin-top: 8px; }
.palette-panel { flex: 1; overflow-y: auto; }
.palette-cat { margin: 8px 0 4px; color: var(--muted); font-size: 12px; }
.palette-grid { display: grid; grid-template-columns: repeat(3, 1fr); gap: 4px; }
.swatch {
  display: flex;
  flex-direction: column;
  align-items: center;
  padding: 2px 2px 4px;
  border: 1px solid transparent;
  border-radius: 4px;
  background: #1f1e22;
  cursor: pointer;
}
.swatch:hover { border-color: #55535b; }
.swatch.active { border-color: var(--accent); background: #2d2618; }
.swatch canvas { image-rendering: pixelated; }
.swatch span { font-size: 10px; color: var(--muted); text-align: center; line-height: 1.2; }

/* ---- Viewport ---- */
.viewport-wrap { grid-area: view; display: flex; flex-direction: column; min-width: 0; min-height: 0; }
.viewport-bar {
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 6px 10px;
  background: var(--panel);
  border-bottom: 1px solid var(--line);
}
.seg { display: inline-flex; }
.seg button { border-radius: 0; }
.seg button:first-child { border-radius: 4px 0 0 4px; }
.seg button:last-child { border-radius: 0 4px 4px 0; }
.chk, .slider { display: inline-flex; align-items: center; gap: 6px; }
.viewport {
  flex: 1;
  overflow: auto;
  display: flex;
  padding: 40px;
  min-height: 0;
  background:
    radial-gradient(circle at 50% 40%, #2c2a30, #1b1a1d 70%);
}
.viewport.recording { box-shadow: inset 0 0 0 2px #d94a3a; }
.viewport.preview { background: radial-gradient(circle at 50% 40%, #ece5d8, #d9d0c1 80%); }
.iso-frame { position: relative; margin: auto; flex: none; }
.grid-canvas, .overlay-canvas { position: absolute; left: 0; top: 0; pointer-events: none; }
.iso-canvas { position: relative; }
.status {
  padding: 4px 10px;
  font-size: 12px;
  color: var(--muted);
  background: var(--panel);
  border-top: 1px solid var(--line);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

/* ---- Right ---- */
.right {
  grid-area: right;
  display: flex;
  flex-direction: column;
  background: var(--panel);
  border-left: 1px solid var(--line);
  min-height: 0;
}
.tabs { display: flex; border-bottom: 1px solid var(--line); }
.tabs button { flex: 1; border: 0; border-radius: 0; background: transparent; padding: 8px 0; color: var(--muted); font-size: 12px; }
.tabs button.active { color: var(--text); box-shadow: inset 0 -2px var(--accent); }
.inspector { flex: 1; overflow-y: auto; padding: 12px; }
.inspector h4 { margin: 14px 0 6px; font-size: 11px; text-transform: uppercase; letter-spacing: 0.1em; color: var(--muted); }
.inspector h4:first-child { margin-top: 0; }
.f { display: grid; grid-template-columns: 84px 1fr; align-items: center; gap: 6px; margin-bottom: 6px; }
.f > span { color: var(--muted); }
.f .row { display: flex; gap: 4px; align-items: center; }
.btn-row { display: flex; flex-wrap: wrap; gap: 4px; margin: 8px 0; }
.empty { color: var(--muted); padding: 8px 0; }
.list { display: flex; flex-direction: column; gap: 2px; }
.list-item {
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 4px 6px;
  border-radius: 4px;
  background: #1f1e22;
  border: 1px solid transparent;
  cursor: pointer;
}
.list-item.active { border-color: var(--accent); }
.list-item .name { flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.list-item .count { color: var(--muted); font-size: 11px; }
.list-item input[type="color"] { width: 18px; height: 18px; padding: 0; border: 0; background: none; }
.icon-btn { padding: 0 6px; border: 0; background: transparent; color: var(--muted); }
.icon-btn:hover { color: var(--text); }
.picking { color: var(--accent); }

/* ---- Timeline ---- */
.timeline {
  grid-area: tl;
  display: flex;
  flex-direction: column;
  background: var(--panel);
  border-top: 1px solid var(--line);
  min-height: 0;
}
.tl-toolbar { display: flex; align-items: center; gap: 6px; padding: 6px 10px; border-bottom: 1px solid var(--line); }
.tl-time { font-variant-numeric: tabular-nums; min-width: 48px; color: var(--muted); }
.tl-body { position: relative; flex: 1; overflow-y: auto; overflow-x: hidden; min-height: 0; }
.tl-ruler {
  position: sticky;
  top: 0;
  z-index: 2;
  height: 24px;
  margin-left: var(--label-w);
  background: #222125;
  border-bottom: 1px solid var(--line);
  cursor: ew-resize;
}
.tl-tick { position: absolute; top: 14px; bottom: 0; width: 1px; background: #4a4850; }
.tl-tick.major { top: 8px; background: #6a6870; }
.tl-tick span { position: absolute; top: -9px; left: 3px; font-size: 10px; color: var(--muted); }
.tl-marker {
  position: absolute;
  top: 0;
  bottom: 0;
  width: 2px;
  margin-left: -1px;
  background: #7fb0c8;
  cursor: grab;
}
.tl-marker span {
  position: absolute;
  top: 1px;
  left: 4px;
  font-size: 10px;
  padding: 0 4px;
  border-radius: 3px;
  background: #2d4450;
  color: #d4ecf6;
  white-space: nowrap;
}
.tl-row { display: flex; height: 24px; border-bottom: 1px solid #2b2a2f; }
.tl-row.section { height: 20px; background: #222125; color: var(--muted); font-size: 11px; text-transform: uppercase; letter-spacing: 0.1em; }
.tl-row.section .tl-label { cursor: default; }
.tl-label {
  flex: none;
  width: var(--label-w);
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 0 8px;
  overflow: hidden;
  white-space: nowrap;
  cursor: pointer;
  border-right: 1px solid var(--line);
}
.tl-label .dot { flex: none; width: 8px; height: 8px; border-radius: 2px; }
.tl-label .txt { overflow: hidden; text-overflow: ellipsis; }
.tl-label .sub { color: var(--muted); font-size: 11px; }
.tl-row.active .tl-label { background: #2d2618; }
.tl-track { position: relative; flex: 1; }
.tl-bar {
  position: absolute;
  top: 3px;
  bottom: 3px;
  min-width: 4px;
  border-radius: 3px;
  background: var(--c, #777);
  opacity: 0.85;
  cursor: grab;
  display: flex;
  align-items: center;
  overflow: hidden;
}
.tl-bar.active { opacity: 1; outline: 2px solid var(--accent); }
.tl-bar .lbl { font-size: 11px; padding: 0 8px; color: #141316; white-space: nowrap; pointer-events: none; mix-blend-mode: multiply; font-weight: 600; }
.tl-bar .h { position: absolute; top: 0; bottom: 0; width: 7px; cursor: ew-resize; }
.tl-bar .h.l { left: 0; }
.tl-bar .h.r { right: 0; }
.tl-key {
  position: absolute;
  top: 6px;
  width: 11px;
  height: 11px;
  margin-left: -5.5px;
  transform: rotate(45deg);
  background: #7fb0c8;
  border: 1px solid #d4ecf6;
  cursor: grab;
}
.tl-key.active { background: var(--accent); }
.tl-overlay { position: absolute; left: var(--label-w); right: 0; top: 0; bottom: 0; pointer-events: none; z-index: 3; }
.tl-playhead { position: absolute; top: 0; bottom: 0; width: 2px; margin-left: -1px; background: var(--accent); }
.tl-playhead::before {
  content: "";
  position: absolute;
  top: 0;
  left: -5px;
  border: 6px solid transparent;
  border-top-color: var(--accent);
}

/* ---- Block designer ---- */
dialog.bd {
  width: min(980px, 96vw);
  max-width: none;
  max-height: 92vh;
  padding: 0;
  display: flex;
  flex-direction: column;
}
dialog.bd:not([open]) { display: none; }
.bd-head, .bd-foot { display: flex; align-items: center; gap: 8px; padding: 10px 14px; border-bottom: 1px solid var(--line); }
.bd-foot { border-bottom: 0; border-top: 1px solid var(--line); }
.bd-body { display: grid; grid-template-columns: 330px 1fr; min-height: 0; overflow: hidden; }
.bd-preview { padding: 14px; border-right: 1px solid var(--line); }
.bd-canvas {
  display: grid;
  place-items: center;
  min-height: 300px;
  border-radius: 6px;
  background: radial-gradient(circle at 50% 45%, #ece5d8, #d4cab9 85%);
}
.bd-form { padding: 14px; overflow-y: auto; max-height: calc(92vh - 110px); }
.bd-form h4 { margin: 14px 0 6px; font-size: 11px; text-transform: uppercase; letter-spacing: 0.1em; color: var(--muted); }
.bd-form h4:first-child { margin-top: 0; }
.bd-form .f { grid-template-columns: 96px 1fr; }
.bd-form .f .row { flex-wrap: wrap; }
.bd-form input[type="number"] { width: 64px; }
.bd-parts { display: flex; flex-direction: column; gap: 2px; }
.bd-tex { display: flex; align-items: center; gap: 4px; flex-wrap: wrap; }
.bd-tex input[type="color"] { width: 30px; height: 24px; padding: 0 2px; }
.bd-accent { display: inline-flex; align-items: center; gap: 3px; color: var(--muted); font-size: 12px; }
.bd-face-slot { flex: 1; }
.bd-error { color: #f3b3a6; }
.bd-tex .bd-tex-btn { padding: 2px 8px; font-size: 12px; }

/* ---- Texture painter ---- */
dialog.tp { width: min(900px, 96vw); }
.tp-body { display: grid; grid-template-columns: 1fr 300px; min-height: 0; overflow: hidden; }
.tp-paint { padding: 14px; border-right: 1px solid var(--line); display: flex; flex-direction: column; gap: 8px; min-width: 0; }
.tp-stage { display: grid; place-items: center; min-height: 460px; background: repeating-conic-gradient(#2a292d 0 25%, #232226 0 50%) 0 0 / 16px 16px; border-radius: 6px; }
.tp-grid { image-rendering: pixelated; cursor: crosshair; touch-action: none; box-shadow: 0 0 0 1px #000; }
.tp-side { padding: 14px; overflow-y: auto; max-height: calc(92vh - 110px); }
.tp-side h4 { margin: 14px 0 6px; font-size: 11px; text-transform: uppercase; letter-spacing: 0.1em; color: var(--muted); }
.tp-side .f { grid-template-columns: 76px 1fr; }
.tp-side .f .row { flex-wrap: wrap; }
.tp-preview { min-height: 170px; }
.tp-colour { display: flex; gap: 4px; align-items: center; }
.tp-colour input[type="color"] { width: 44px; height: 28px; padding: 0 2px; }
.tp-swatches { display: flex; flex-wrap: wrap; gap: 3px; margin-top: 6px; }
.tp-swatch { width: 20px; height: 20px; padding: 0; border-radius: 3px; border: 1px solid #0006; }
.tp-swatch.active { outline: 2px solid var(--accent); outline-offset: 1px; }
.tp-custom { display: inline-flex; align-items: center; gap: 3px; }
.tp-custom input { width: 52px; }
.tp-tint { align-items: center; gap: 8px; flex-wrap: wrap; }
.tp-tint input[type="color"] { width: 36px; height: 24px; padding: 0 2px; }

.swatch.custom span { color: #e8c98a; }
.swatch.edited span::after { content: ' •'; color: #e8c98a; }

/* ---- Misc ---- */
dialog {
  color: var(--text);
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 8px;
  max-width: 560px;
}
dialog::backdrop { background: rgba(0, 0, 0, 0.5); }
.help-grid { display: grid; grid-template-columns: max-content 1fr; gap: 6px 12px; }
kbd {
  font: 11px ui-monospace, monospace;
  padding: 1px 6px;
  border: 1px solid var(--line);
  border-bottom-width: 2px;
  border-radius: 4px;
  background: #1a191c;
  white-space: nowrap;
  justify-self: start;
}
.toast {
  position: fixed;
  left: 50%;
  bottom: 250px;
  transform: translateX(-50%);
  padding: 8px 14px;
  border-radius: 6px;
  background: #3a3220;
  border: 1px solid #8a6a30;
  z-index: 10;
}

#btnRecord.recording { background: #d94a3a; border-color: #d94a3a; color: #fff; }
.rec-opts { display: inline-flex; align-items: center; gap: 4px; }
.rec-opts[hidden] { display: none; }
