:root {
  color-scheme: dark;
  --bg: #15171c;
  --panel: #1d2026;
  --panel-border: #2a2e37;
  --text: #e6e8eb;
  --muted: #8b93a1;
  --accent: #5b8cff;
  --accent-hover: #7aa2ff;
  --danger: #ff6b6b;
  --start: #f2c14e;
  --connector: #7f8db0;
  --radius: 8px;
}

* {
  box-sizing: border-box;
}

/* Component display rules (e.g. .btn's inline-flex) must not undo `hidden`. */
[hidden] {
  display: none !important;
}

html,
body {
  margin: 0;
  height: 100%;
}

body {
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
}

a {
  color: var(--accent);
}

button {
  font-family: inherit;
  cursor: pointer;
}

/* Login page */
.login-body {
  display: flex;
  align-items: center;
  justify-content: center;
}

.login-card {
  background: var(--panel);
  border: 1px solid var(--panel-border);
  border-radius: var(--radius);
  padding: 2rem 2.5rem;
  width: 320px;
}

.login-card h1 {
  font-size: 1.25rem;
  margin: 0 0 1.5rem;
  text-align: center;
}

.login-card form {
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
}

.login-card label {
  font-size: 0.85rem;
  color: var(--muted);
}

.login-card input {
  background: var(--bg);
  border: 1px solid var(--panel-border);
  border-radius: var(--radius);
  color: var(--text);
  padding: 0.5rem 0.75rem;
  font-size: 1rem;
}

.login-card input:focus {
  outline: 2px solid var(--accent);
  outline-offset: 1px;
}

.login-card button {
  margin-top: 0.5rem;
  background: var(--accent);
  border: none;
  border-radius: var(--radius);
  color: white;
  padding: 0.6rem 0.75rem;
  font-size: 1rem;
  font-weight: 600;
}

.login-card button:hover {
  background: var(--accent-hover);
}

.login-card button:disabled {
  opacity: 0.6;
  cursor: default;
}

.error {
  color: var(--danger);
  font-size: 0.85rem;
  min-height: 1.1em;
  margin: 0;
}

/* App shell (map list, editor top bar, etc.) */
.app-shell {
  display: flex;
  flex-direction: column;
  min-height: 100vh;
}

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 1rem;
  padding: 0.75rem 1.25rem;
  border-bottom: 1px solid var(--panel-border);
  background: var(--panel);
}

.topbar h1 {
  font-size: 1.05rem;
  margin: 0;
}

.topbar-left {
  display: flex;
  align-items: center;
  gap: 0.75rem;
}

.topbar-center {
  flex: 1;
  display: flex;
  justify-content: center;
  align-items: center;
  gap: 0.5rem;
}

.topbar-right {
  display: flex;
  align-items: center;
  gap: 0.5rem;
}

.map-title {
  cursor: text;
}

.map-title:hover {
  color: var(--accent);
}

.layer-switcher {
  display: flex;
  align-items: center;
  gap: 0.4rem;
  font-size: 0.85rem;
  color: var(--muted);
}

.layer-switcher select {
  background: var(--bg);
  border: 1px solid var(--panel-border);
  border-radius: var(--radius);
  color: var(--text);
  padding: 0.3rem 0.5rem;
  font-size: 0.85rem;
}

.main-content {
  flex: 1;
  padding: 1.5rem;
}

/* Editor 3D stage */
.editor-shell {
  height: 100vh;
}

.stage-wrap {
  position: relative;
  flex: 1;
  overflow: hidden;
}

.stage {
  position: absolute;
  inset: 0;
  perspective: 1200px;
  overflow: hidden;
  background: radial-gradient(circle at 50% 35%, #1b1e25 0%, #0f1115 75%);
  user-select: none;
  touch-action: none;
}

.world {
  position: absolute;
  top: 50%;
  left: 50%;
  transform-style: preserve-3d;
  will-change: transform;
}

.layer {
  position: absolute;
  top: 0;
  left: 0;
  transform-style: preserve-3d;
  transition: opacity 300ms ease;
}

.layer.active {
  opacity: 1;
  pointer-events: auto;
}

.layer.ghost {
  opacity: 0.5;
  pointer-events: none;
}

.plane {
  display: block;
  overflow: visible;
}

.plane-bg {
  fill: rgba(91, 140, 255, 0.04);
  stroke: rgba(139, 147, 161, 0.25);
  stroke-width: 1;
}

.grid-dot {
  fill: var(--muted);
}

.stage-empty-state {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  margin: 0;
  color: var(--muted);
  font-size: 0.9rem;
  pointer-events: none;
}

/* Buttons */
.btn {
  background: transparent;
  border: 1px solid var(--panel-border);
  border-radius: var(--radius);
  color: var(--text);
  padding: 0.4rem 0.8rem;
  font-size: 0.85rem;
  text-decoration: none;
  display: inline-flex;
  align-items: center;
}

.btn:hover {
  border-color: var(--accent);
  color: var(--accent);
}

.btn:disabled {
  opacity: 0.4;
  cursor: default;
}

.btn-primary {
  background: var(--accent);
  border-color: var(--accent);
  color: white;
}

.btn-primary:hover {
  background: var(--accent-hover);
  border-color: var(--accent-hover);
  color: white;
}

.btn-danger:hover {
  border-color: var(--danger);
  color: var(--danger);
}

/* Inline forms (new map, new layer) */
.inline-form {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  margin-bottom: 1rem;
}

.inline-form input {
  background: var(--panel);
  border: 1px solid var(--panel-border);
  border-radius: var(--radius);
  color: var(--text);
  padding: 0.5rem 0.75rem;
  font-size: 0.95rem;
  min-width: 220px;
}

.inline-form input:focus {
  outline: 2px solid var(--accent);
  outline-offset: 1px;
}

.rename-input {
  background: var(--bg);
  border: 1px solid var(--accent);
  border-radius: var(--radius);
  color: var(--text);
  font-size: inherit;
  padding: 0.2rem 0.4rem;
}

.empty-state {
  color: var(--muted);
}

/* Map list */
.map-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(220px, 1fr));
  gap: 1rem;
}

.map-card {
  background: var(--panel);
  border: 1px solid var(--panel-border);
  border-radius: var(--radius);
  padding: 1rem;
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
}

.map-card-name {
  margin: 0;
  font-size: 1.05rem;
  cursor: text;
}

.map-card-name:hover {
  color: var(--accent);
}

.map-card-meta {
  margin: 0;
  color: var(--muted);
  font-size: 0.8rem;
}

.map-card-actions {
  display: flex;
  gap: 0.5rem;
  margin-top: auto;
}

/* Editor layer panel */
.layer-panel {
  background: var(--panel);
  border: 1px solid var(--panel-border);
  border-radius: var(--radius);
  padding: 1rem 1.25rem;
}

.layer-panel.floating {
  position: absolute;
  top: 1rem;
  left: 1rem;
  z-index: 10;
  width: 320px;
  max-height: calc(100% - 2rem);
  overflow: auto;
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.4);
}

.layer-panel .inline-form {
  margin: 0.75rem 0 0.25rem;
}

.layer-panel .inline-form input {
  min-width: 0;
  flex: 1;
}

.layer-panel-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 1rem;
  flex-wrap: wrap;
}

.layer-panel-header h2 {
  margin: 0;
  font-size: 1rem;
}

.layer-panel-header .inline-form {
  margin-bottom: 0;
}

.layer-list {
  list-style: none;
  margin: 0.75rem 0 0;
  padding: 0;
  display: flex;
  flex-direction: column;
  gap: 0.4rem;
}

.layer-row {
  display: flex;
  align-items: center;
  gap: 0.4rem;
  padding: 0.4rem 0.5rem;
  border: 1px solid var(--panel-border);
  border-radius: var(--radius);
}

.layer-name {
  flex: 1;
  cursor: text;
}

.layer-name:hover {
  color: var(--accent);
}

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

.layer-activate {
  background: transparent;
  border: none;
  color: var(--accent);
  font-size: 0.95rem;
  padding: 0 0.25rem;
}

.layer-activate:disabled {
  cursor: default;
}

.layer-row .btn {
  padding: 0.25rem 0.5rem;
}

/* Grid points, nodes and previews */
.grid-hit {
  fill: transparent;
  cursor: pointer;
}

.grid-dot {
  pointer-events: none;
}

.layer.active .grid-hit:hover + .grid-dot {
  fill: var(--accent);
  r: 6px;
}

.node {
  cursor: pointer;
}

.node-body {
  fill: #2a3552;
  stroke: var(--accent);
  stroke-width: 2.5;
}

.node.start .node-body {
  stroke: var(--start);
  stroke-width: 3.5;
}

.node.selected .node-body {
  fill: #3a4a74;
  stroke: #ffffff;
  stroke-width: 4;
}

.preview {
  pointer-events: none;
}

/* Live session controls */
.transition-setting {
  display: flex;
  align-items: center;
  gap: 0.3rem;
  font-size: 0.85rem;
  color: var(--muted);
}

.transition-setting input {
  width: 3.6rem;
  background: var(--bg);
  border: 1px solid var(--panel-border);
  border-radius: var(--radius);
  color: var(--text);
  padding: 0.3rem 0.4rem;
  font: inherit;
}

.btn-play {
  background: #2f8f5b;
  border-color: #2f8f5b;
  color: white;
  font-weight: 600;
}

.btn-play:hover {
  background: #37a86a;
  border-color: #37a86a;
  color: white;
}

.btn-stop {
  background: var(--danger);
  border-color: var(--danger);
  color: #1a0b0b;
  font-weight: 600;
}

.btn-stop:hover {
  background: #ff8585;
  border-color: #ff8585;
  color: #1a0b0b;
}

.toolbar-divider {
  width: 1px;
  height: 1.4rem;
  margin: 0 0.25rem;
  background: var(--panel-border);
}

/* During play: only reachable neighbours respond to clicks. */
.stage.live .grid-hit,
.stage.live .connector,
.stage.live .riser,
.stage.live .thumb {
  pointer-events: none;
}

.stage.live .node {
  cursor: default;
}

.stage.live .node.target {
  cursor: pointer;
}

.stage.live .layer.ghost .node.target {
  pointer-events: auto;
}

.node.visited .node-body {
  fill: #33405f;
  stroke: #9aa8c7;
}

.node.target .node-body {
  fill: #1f5a3c;
  stroke: #5be39a;
  stroke-width: 4;
}

.node.current .node-body {
  fill: #5be39a;
  stroke: #ffffff;
  stroke-width: 4;
}

.live-preview {
  position: absolute;
  left: 1rem;
  bottom: 1rem;
  z-index: 10;
  width: 240px;
  padding: 0.6rem;
  background: var(--panel);
  border: 1px solid var(--panel-border);
  border-radius: var(--radius);
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.4);
}

.live-preview-head {
  display: flex;
  align-items: center;
  gap: 0.4rem;
  margin-bottom: 0.45rem;
  color: var(--muted);
  font-size: 0.75rem;
  text-transform: uppercase;
  letter-spacing: 0.05em;
}

.live-dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: #5be39a;
}

.live-preview-frame {
  aspect-ratio: 16 / 9;
  background: #000;
  border-radius: 4px;
  overflow: hidden;
}

.live-preview-frame img {
  display: block;
  width: 100%;
  height: 100%;
  object-fit: contain;
}

.live-preview-caption {
  margin-top: 0.4rem;
  font-size: 0.85rem;
}

/* Tool toggle */
.tool-toggle {
  display: inline-flex;
  margin-right: 0.75rem;
}

.tool-toggle .btn {
  border-radius: 0;
}

.tool-toggle .btn:first-child {
  border-radius: var(--radius) 0 0 var(--radius);
}

.tool-toggle .btn:last-child {
  border-radius: 0 var(--radius) var(--radius) 0;
  margin-left: -1px;
}

.tool-toggle .btn.active {
  background: var(--accent);
  border-color: var(--accent);
  color: white;
}

.stage.tool-connect .node {
  cursor: crosshair;
}

.stage.tool-connect .grid-hit {
  cursor: default;
}

.stage.tool-connect .layer.active .grid-hit:hover + .grid-dot {
  fill: var(--muted);
  r: 3px;
}

/* During a connect drag, nodes on the ghosted neighbour layers become targets. */
.stage.connecting .layer.ghost .node {
  pointer-events: auto;
}

/* In-layer connectors */
.connector {
  cursor: pointer;
}

.connector-hit {
  stroke: transparent;
  stroke-width: 14;
}

.connector-line {
  stroke: var(--connector);
  stroke-width: 3;
  pointer-events: none;
}

.connector.selected .connector-line {
  stroke: #ffffff;
  stroke-width: 4;
}

/* Cross-layer connectors: a div laid along +x, then rotated in 3D. */
.riser {
  position: absolute;
  top: 0;
  left: 0;
  height: 17px;
  margin-top: -8.5px;
  transform-origin: 0 50%;
  cursor: pointer;
}

/* Drawn thicker than the in-layer SVG stroke: the riser is rasterised as a
   texture and then shrunk by zoom and tilt, and thinner lines break up. */
.riser::before {
  content: '';
  position: absolute;
  left: 0;
  right: 0;
  top: 6px;
  border-top: 5px solid var(--connector);
}

.riser.selected::before {
  top: 5.5px;
  border-top: 6px solid #ffffff;
}

.preview-riser {
  pointer-events: none;
}

.preview-riser::before {
  border-top: 5px dashed var(--accent);
}

.preview-line {
  stroke: var(--accent);
  stroke-width: 2;
  stroke-dasharray: 6 5;
}

.preview-node {
  fill: rgba(91, 140, 255, 0.15);
  stroke: var(--accent);
  stroke-width: 2;
  stroke-dasharray: 5 4;
}

/* Billboards: counter-rotated against the world so they face the viewer. */
.billboards {
  position: absolute;
  inset: 0;
  transform-style: preserve-3d;
  pointer-events: none;
}

.billboard {
  position: absolute;
  top: 0;
  left: 0;
  transform-origin: 0 0;
  transform: translate3d(var(--x), var(--y), var(--z, 0px)) rotateZ(calc(var(--rotate) * -1deg))
    rotateX(calc(var(--tilt) * -1deg)) translate(var(--offset-x, 0), var(--offset-y, 0));
}

.node-label {
  --offset-x: -50%;
  --offset-y: 22px;
  display: flex;
  align-items: center;
  gap: 0.4rem;
  padding: 0.2rem 0.55rem;
  border-radius: 5px;
  background: rgba(15, 17, 21, 0.8);
  color: var(--text);
  font-size: 20px;
  white-space: nowrap;
}

/* Connector-end thumbnails stand upright on their point (origin 50% 100%). */
.thumb {
  --offset-x: -50%;
  --offset-y: -100%;
  width: 48px;
  height: 32px;
  border: 2px solid var(--connector);
  border-radius: 4px;
  overflow: hidden;
  background: #0b0d10;
  pointer-events: auto;
  cursor: pointer;
}

.thumb img {
  display: block;
  width: 100%;
  height: 100%;
  object-fit: cover;
  pointer-events: none;
}

.thumb.blocked {
  border: 2px dashed var(--danger);
  background: rgba(255, 107, 107, 0.12);
}

.thumb.blocked::after {
  content: '\2715';
  display: flex;
  align-items: center;
  justify-content: center;
  height: 100%;
  color: var(--danger);
  font-size: 14px;
  font-weight: 700;
}

.thumb.selected {
  border-color: #ffffff;
}

.thumb.blocked.selected {
  border-color: var(--danger);
  outline: 2px solid #ffffff;
}

.thumb.drop-target {
  outline: 3px solid var(--accent);
}

.start-tag {
  color: var(--start);
  font-size: 15px;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.04em;
}

/* Selection side panel */
.side-panel {
  position: absolute;
  top: 1rem;
  right: 1rem;
  z-index: 10;
  width: 300px;
  max-height: calc(100% - 2rem);
  overflow: auto;
  background: var(--panel);
  border: 1px solid var(--panel-border);
  border-radius: var(--radius);
  padding: 1rem 1.25rem;
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.4);
}

.side-body {
  display: flex;
  flex-direction: column;
  gap: 0.75rem;
  margin-top: 0.75rem;
}

.field {
  display: flex;
  flex-direction: column;
  gap: 0.3rem;
}

.field-label {
  font-size: 0.8rem;
  color: var(--muted);
}

.field input,
.field textarea {
  background: var(--bg);
  border: 1px solid var(--panel-border);
  border-radius: var(--radius);
  color: var(--text);
  padding: 0.45rem 0.6rem;
  font: inherit;
  font-size: 0.9rem;
  resize: vertical;
}

.field input:focus,
.field textarea:focus {
  outline: 2px solid var(--accent);
  outline-offset: 1px;
}

.side-meta {
  margin: 0;
  color: var(--muted);
  font-size: 0.8rem;
}

.checkbox-field {
  display: flex;
  align-items: center;
  gap: 0.4rem;
  font-size: 0.9rem;
}

.side-body .btn-danger {
  align-self: flex-start;
}

.connector-summary {
  margin: 0;
  font-weight: 600;
}

.connector-end {
  display: flex;
  flex-direction: column;
  gap: 0.2rem;
  padding: 0.6rem 0.7rem;
  border: 1px solid var(--panel-border);
  border-radius: var(--radius);
}

.connector-end-title {
  font-size: 0.9rem;
}

.side-meta.blocked {
  color: var(--danger);
}

.start-image {
  display: flex;
  flex-direction: column;
  gap: 0.3rem;
}

.start-image:empty {
  display: none;
}

.image-slot {
  display: flex;
  flex-direction: column;
  gap: 0.4rem;
  margin-top: 0.3rem;
}

.image-preview {
  display: flex;
  align-items: center;
  justify-content: center;
  aspect-ratio: 3 / 2;
  border: 1px solid var(--panel-border);
  border-radius: 6px;
  overflow: hidden;
  background: #0b0d10;
}

.image-preview img {
  width: 100%;
  height: 100%;
  object-fit: contain;
}

.image-preview.empty {
  aspect-ratio: auto;
  min-height: 3rem;
  padding: 0.5rem;
  border: 2px dashed var(--danger);
  background: rgba(255, 107, 107, 0.08);
  color: var(--danger);
  font-size: 0.8rem;
  text-align: center;
}

.image-preview.drop-target {
  outline: 3px solid var(--accent);
  outline-offset: 2px;
}

.image-actions {
  display: flex;
  gap: 0.4rem;
}

/* Image picker: reuse an uploaded image or upload a new one. */
.image-picker {
  display: flex;
  flex-direction: column;
  gap: 0.45rem;
  margin-top: 0.3rem;
  padding: 0.6rem;
  border: 1px solid var(--accent);
  border-radius: var(--radius);
  background: var(--bg);
}

.picker-grid {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 0.4rem;
  max-height: 260px;
  overflow-y: auto;
}

.picker-tile {
  position: relative;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  aspect-ratio: 3 / 2;
  padding: 0;
  overflow: hidden;
  border: 2px solid var(--panel-border);
  border-radius: 6px;
  background: #0b0d10;
  color: var(--text);
}

.picker-tile:hover {
  border-color: var(--accent);
}

.picker-tile img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.picker-tile.current {
  border-color: #ffffff;
}

.picker-tile.current::after {
  content: '\2713';
  position: absolute;
  top: 2px;
  right: 4px;
  color: #ffffff;
  font-size: 0.8rem;
  font-weight: 700;
  text-shadow: 0 0 3px #000;
}

.picker-uses {
  position: absolute;
  left: 0;
  right: 0;
  bottom: 0;
  padding: 1px 4px;
  background: rgba(0, 0, 0, 0.65);
  color: var(--muted);
  font-size: 0.65rem;
  text-align: left;
}

.picker-upload {
  border-style: dashed;
  gap: 0.1rem;
  color: var(--accent);
}

.picker-upload.drop-target {
  border-color: var(--accent);
  background: rgba(91, 140, 255, 0.12);
}

.picker-plus {
  font-size: 1.3rem;
  line-height: 1;
}

.picker-upload-label {
  font-size: 0.75rem;
}

.picker-cancel {
  align-self: flex-start;
}

.toast {
  position: absolute;
  bottom: 1.25rem;
  left: 50%;
  transform: translateX(-50%);
  z-index: 20;
  margin: 0;
  padding: 0.6rem 1rem;
  border: 1px solid var(--danger);
  border-radius: var(--radius);
  background: var(--panel);
  color: var(--danger);
  font-size: 0.9rem;
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.4);
}

.stage-hint {
  position: absolute;
  top: 1rem;
  left: 50%;
  transform: translateX(-50%);
  z-index: 5;
  margin: 0;
  padding: 0.45rem 0.9rem;
  border-radius: 999px;
  background: rgba(29, 32, 38, 0.85);
  border: 1px solid var(--panel-border);
  color: var(--muted);
  font-size: 0.85rem;
  pointer-events: none;
  white-space: nowrap;
}

.grid-form {
  display: flex;
  flex-direction: column;
  gap: 0.35rem;
  margin-top: 1rem;
  padding-top: 0.9rem;
  border-top: 1px solid var(--panel-border);
}

.grid-form-row {
  display: flex;
  align-items: center;
  gap: 0.4rem;
}

.grid-form-row input {
  width: 4rem;
  background: var(--bg);
  border: 1px solid var(--panel-border);
  border-radius: var(--radius);
  color: var(--text);
  padding: 0.35rem 0.5rem;
  font: inherit;
}

.shortcuts-backdrop {
  position: fixed;
  inset: 0;
  z-index: 50;
  display: flex;
  align-items: center;
  justify-content: center;
  background: rgba(0, 0, 0, 0.55);
}

.shortcuts {
  width: min(560px, calc(100vw - 2rem));
  max-height: calc(100vh - 2rem);
  overflow: auto;
  padding: 1.1rem 1.4rem;
  background: var(--panel);
  border: 1px solid var(--panel-border);
  border-radius: var(--radius);
  box-shadow: 0 16px 40px rgba(0, 0, 0, 0.5);
}

.shortcut-list {
  display: grid;
  grid-template-columns: max-content 1fr;
  gap: 0.45rem 1.25rem;
  margin: 1rem 0;
  font-size: 0.9rem;
}

.shortcut-list dt {
  color: var(--muted);
}

.shortcut-list dd {
  margin: 0;
}

kbd {
  display: inline-block;
  min-width: 1.4em;
  padding: 0.05rem 0.35rem;
  border: 1px solid var(--panel-border);
  border-bottom-width: 2px;
  border-radius: 4px;
  background: var(--bg);
  color: var(--text);
  font: 0.8rem ui-monospace, SFMono-Regular, Consolas, monospace;
  text-align: center;
}

.toast.info {
  border-color: var(--accent);
  color: var(--text);
}
