:root {
  --bg: #0e0f12;
  --panel: #16181d;
  --panel-2: #1c1f26;
  --stroke: #2a2e38;
  --text: #e7e4dc;
  --muted: #8b8a84;
  --accent: #d4a574;
  --accent-2: #f0c090;
  --danger: #c45c4a;
  --board: #000000;
  --safe-t: env(safe-area-inset-top, 0px);
  --safe-b: env(safe-area-inset-bottom, 0px);
  --safe-l: env(safe-area-inset-left, 0px);
  --safe-r: env(safe-area-inset-right, 0px);
  --mobile-tools-h: 64px;
  --font: "IBM Plex Sans", "Helvetica Neue", sans-serif;
  --mono: "IBM Plex Mono", ui-monospace, monospace;
}

* {
  box-sizing: border-box;
  -webkit-tap-highlight-color: transparent;
}

html,
body {
  margin: 0;
  height: 100%;
  background: var(--bg);
  color: var(--text);
  font-family: var(--font);
  overflow: hidden;
  overscroll-behavior: none;
  touch-action: none;
}

#app {
  height: 100%;
  height: 100dvh;
  display: flex;
  flex-direction: column;
  padding: var(--safe-t) var(--safe-r) 0 var(--safe-l);
}

.chip,
.icon-btn,
.text-btn {
  appearance: none;
  border: 1px solid var(--stroke);
  background: var(--panel-2);
  color: var(--text);
  font-family: var(--font);
  cursor: pointer;
  border-radius: 8px;
}

.chip,
.text-btn {
  height: 34px;
  padding: 0 10px;
  font-size: 12px;
  font-weight: 500;
}

.chip {
  font-family: var(--mono);
  color: var(--accent-2);
}

.icon-btn {
  width: 34px;
  height: 34px;
  display: grid;
  place-items: center;
  padding: 0;
  flex-shrink: 0;
  font-size: 16px;
  line-height: 1;
}

.icon-btn svg {
  width: 18px;
  height: 18px;
  fill: none;
  stroke: currentColor;
  stroke-width: 1.75;
  stroke-linecap: round;
  stroke-linejoin: round;
}

.icon-btn svg circle {
  fill: none;
}

.chip:active,
.icon-btn:active,
.text-btn:active,
.tool-btn.active,
.led-thumb.active {
  border-color: var(--accent);
  color: var(--accent-2);
}

.text-btn.accent {
  background: var(--accent);
  color: #1a140e;
  border-color: var(--accent);
}

.text-btn.danger {
  color: var(--danger);
  border-color: #5a2e28;
}

.workspace {
  flex: 1;
  min-height: 0;
  display: grid;
  grid-template-columns: 220px 1fr 260px;
}

.rail-head {
  display: flex;
  flex-direction: column;
  gap: 8px;
  padding-bottom: 4px;
}

.status {
  font-family: var(--mono);
  font-size: 11px;
  color: var(--muted);
}

.file-row {
  display: flex;
  flex-wrap: nowrap;
  gap: 4px;
}

.file-row .icon-btn {
  width: 30px;
  height: 30px;
}

.file-row .icon-btn svg {
  width: 16px;
  height: 16px;
}

.chip {
  width: 100%;
}

.rail,
.inspector {
  background: var(--panel);
  border-right: 1px solid var(--stroke);
  overflow: auto;
  min-height: 0;
  padding: 10px;
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.inspector {
  border-right: 0;
  border-left: 1px solid var(--stroke);
}

.section-label {
  font-size: 10px;
  letter-spacing: 0.12em;
  text-transform: uppercase;
  color: var(--muted);
  margin-bottom: 6px;
}

.tool-group,
.mobile-tools {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 6px;
}

.tool-btn {
  appearance: none;
  border: 1px solid var(--stroke);
  background: var(--panel-2);
  color: var(--text);
  border-radius: 8px;
  min-height: 44px;
  font-family: var(--font);
  font-size: 12px;
  font-weight: 500;
  cursor: pointer;
}

.layers-wrap {
  min-height: 0;
  flex: 1;
  display: flex;
  flex-direction: column;
}

.led-strip {
  display: grid;
  grid-template-columns: repeat(5, 1fr);
  gap: 4px;
}

.led-thumb {
  appearance: none;
  border: 1px solid var(--stroke);
  background: #0a0a0a;
  border-radius: 6px;
  aspect-ratio: 1;
  width: 100%;
  height: auto;
  min-width: 0;
  padding: 3px;
  cursor: pointer;
  display: grid;
  place-items: center;
  color: var(--muted);
}

.led-thumb svg,
.led-thumb img {
  width: 100%;
  height: 100%;
  display: block;
}

.led-thumb span {
  display: none;
}

.layers {
  flex: 1;
  overflow: auto;
  display: flex;
  flex-direction: column;
  gap: 2px;
}

.layer-item {
  border: 1px solid var(--stroke);
  background: var(--panel-2);
  color: var(--text);
  border-radius: 6px;
  min-height: 34px;
  padding: 0 8px 0 2px;
  display: flex;
  align-items: center;
  gap: 0;
  text-align: left;
  font-family: var(--font);
  font-size: 11px;
  line-height: 1;
  cursor: grab;
  user-select: none;
}

.layer-item.active {
  border-color: var(--accent);
}

.layer-item.dragging {
  background: transparent;
  border-style: dashed;
  color: transparent;
  cursor: grabbing;
  touch-action: none;
}

.layer-item.dragging > * {
  visibility: hidden;
}

.layer-ghost {
  position: fixed;
  left: 0;
  top: 0;
  z-index: 80;
  margin: 0;
  pointer-events: none;
  box-shadow: 0 10px 28px rgb(0 0 0 / 45%);
  border-color: var(--accent);
  background: var(--panel-2);
  color: var(--text);
  transform: rotate(1.5deg);
  opacity: 0.95;
}

.layer-ghost.live {
  z-index: 90;
}

.layer-main {
  appearance: none;
  border: 0;
  background: transparent;
  color: inherit;
  flex: 1;
  min-height: 0;
  display: flex;
  align-items: center;
  text-align: left;
  font: inherit;
  line-height: 1;
  cursor: grab;
  padding: 0 6px;
}

.layer-item .meta {
  margin-left: auto;
  font-family: var(--mono);
  font-size: 10px;
  color: var(--muted);
}

.layer-item.hidden {
  opacity: 0.45;
}

.eye-btn {
  appearance: none;
  border: 0;
  background: transparent;
  color: var(--muted);
  cursor: pointer;
  display: grid;
  place-items: center;
  flex-shrink: 0;
}

.eye-btn {
  width: 28px;
  height: 28px;
  border-radius: 6px;
}

.eye-btn svg {
  width: 14px;
  height: 14px;
  fill: none;
  stroke: currentColor;
  stroke-width: 1.75;
  stroke-linecap: round;
  stroke-linejoin: round;
}

.eye-btn:active,
.layer-main:active {
  color: var(--accent-2);
}

.fold {
  border: 1px solid var(--stroke);
  border-radius: 8px;
  padding: 0 8px 8px;
  margin-bottom: 10px;
  background: var(--panel-2);
  min-width: 0;
}

.fold summary {
  cursor: pointer;
  list-style: none;
  height: 36px;
  display: flex;
  align-items: center;
  font-size: 12px;
  color: var(--text);
}

.fold summary::-webkit-details-marker {
  display: none;
}

.fold summary::after {
  content: "+";
  margin-left: auto;
  color: var(--muted);
}

.fold[open] summary::after {
  content: "−";
}

.fold .field-row {
  margin-top: 4px;
}

.stage {
  position: relative;
  min-width: 0;
  min-height: 0;
  background: var(--panel);
}

.preview-check {
  display: flex;
  font-size: 12px;
  color: var(--text);
  margin: 0;
  user-select: none;
}

.preview-check input {
  accent-color: var(--accent);
}

#board {
  width: 100%;
  height: 100%;
  display: block;
  touch-action: none;
  cursor: crosshair;
}

.zoom-cluster {
  display: flex;
  align-items: center;
  gap: 4px;
  width: 100%;
}

.zoom-cluster .text-btn {
  height: 32px;
  padding: 0 8px;
  flex: 1;
}

.zoom-cluster .icon-btn {
  width: 32px;
  height: 32px;
}

.zoom-readout {
  font-family: var(--mono);
  font-size: 11px;
  color: var(--muted);
  min-width: 40px;
  text-align: center;
}

#displaceRand {
  width: 100%;
  margin-bottom: 10px;
}

.inspector h2,
.modal h2 {
  margin: 0 0 10px;
  font-size: 13px;
  font-weight: 600;
  letter-spacing: 0.04em;
}

#imgPick {
  width: 100%;
  margin-bottom: 10px;
}

.inspector label,
.modal label {
  display: flex;
  flex-direction: column;
  gap: 4px;
  font-size: 11px;
  color: var(--muted);
  margin-bottom: 10px;
}

.rot-snaps {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 4px;
  margin-bottom: 10px;
}

.rot-snap {
  appearance: none;
  min-height: 32px;
  border: 1px solid var(--stroke);
  border-radius: 8px;
  background: var(--panel-2);
  color: var(--text);
  font-family: var(--font);
  font-size: 12px;
  cursor: pointer;
}

.rot-snap.active {
  border-color: var(--accent);
  color: var(--accent-2);
}

.inspector input[type="number"],
.inspector input[type="text"],
.modal input[type="number"] {
  width: 100%;
  min-width: 0;
  height: 36px;
  border-radius: 8px;
  border: 1px solid var(--stroke);
  background: var(--panel-2);
  color: var(--text);
  padding: 0 8px;
  font-family: var(--mono);
  font-size: 12px;
  appearance: textfield;
  -moz-appearance: textfield;
}

.inspector input[type="number"]::-webkit-outer-spin-button,
.inspector input[type="number"]::-webkit-inner-spin-button,
.modal input[type="number"]::-webkit-outer-spin-button,
.modal input[type="number"]::-webkit-inner-spin-button {
  appearance: none;
  margin: 0;
}

.inspector input[type="range"] {
  width: 100%;
  height: 14px;
  margin: 0;
  padding: 0;
  accent-color: var(--accent);
}

.inspector label:has(input[type="range"]) {
  margin-bottom: 0;
}

.inspector input[type="range"] + .density-read,
.inspector label:has(input[type="range"]) + .density-read {
  margin-top: -10px;
  margin-bottom: 0;
}

.field-row {
  display: grid;
  grid-template-columns: minmax(0, 1fr) minmax(0, 1fr);
  gap: 8px;
  min-width: 0;
}

.field-row > * {
  min-width: 0;
}

.size-grid {
  display: grid;
  grid-template-columns: minmax(0, 1fr) minmax(0, 1fr);
  gap: 8px;
  min-width: 0;
}

.size-grid label {
  margin-bottom: 0;
  min-width: 0;
}

.check {
  flex-direction: row !important;
  align-items: center;
  gap: 8px !important;
  color: var(--text) !important;
}

.layers-mobile {
  display: none;
  margin-top: 12px;
}

.help,
.empty {
  color: var(--muted);
  font-size: 12px;
  line-height: 1.45;
}

.density-read {
  font-family: var(--mono);
  font-size: 11px;
  color: var(--accent-2);
  margin-top: 0;
  margin-bottom: 0;
}

.mobile-tools {
  display: none;
  grid-template-columns: repeat(6, 1fr);
  gap: 6px;
  padding: 8px 8px calc(8px + var(--safe-b));
  background: var(--panel);
  border-top: 1px solid var(--stroke);
  flex-shrink: 0;
}

.mobile-tools .tool-btn {
  font-size: 10px;
  min-height: 40px;
  padding: 0 2px;
}

.modal-backdrop {
  position: fixed;
  inset: 0;
  background: rgb(0 0 0 / 55%);
  display: grid;
  place-items: center;
  padding: 20px;
  z-index: 20;
}

.modal-backdrop[hidden] {
  display: none;
}

.modal {
  width: min(360px, 100%);
  background: var(--panel);
  border: 1px solid var(--stroke);
  border-radius: 12px;
  padding: 16px;
}

.modal-actions {
  display: flex;
  justify-content: flex-end;
  gap: 8px;
}

@media (max-width: 860px) {
  .workspace {
    position: relative;
    grid-template-columns: 1fr;
    grid-template-rows: 1fr auto;
  }

  .rail {
    position: absolute;
    left: 8px;
    top: 8px;
    right: auto;
    bottom: auto;
    width: auto;
    height: auto;
    z-index: 6;
    overflow: visible;
    background: transparent;
    border: 0;
    padding: 0;
    gap: 0;
  }

  .rail-head {
    flex-direction: row;
    flex-wrap: wrap;
    align-items: center;
    gap: 6px;
    padding: 6px;
    border: 1px solid var(--stroke);
    border-radius: 10px;
    background: color-mix(in srgb, var(--panel) 92%, transparent);
  }

  .rail .tool-group,
  .rail .layers-wrap,
  .rail-head .status {
    display: none;
  }

  .preview-check {
    width: 100%;
    flex-basis: 100%;
    height: 32px;
  }

  .rail-head .chip {
    width: auto;
    height: 32px;
  }

  .zoom-cluster {
    width: 100%;
    flex-basis: 100%;
  }

  .inspector {
    border-left: 0;
    border-top: 1px solid var(--stroke);
    max-height: 34dvh;
  }

  .mobile-tools {
    display: grid;
  }

  .layers-mobile {
    display: block;
  }
}
