:root {
  --bg: #101418;
  --panel: #18202a;
  --panel2: #222b38;
  --ink: #f8fafc;
  --muted: #aab4c3;
  --line: #364254;

  --void: #d9dde3;
  --wall: #8b5a2b;
  --floor: #ffffff;
  --grid: #c9ced6;

  --blue: #1515f1;
  --red: #a8141b;
  --button: #eab308;
  --goal: #22c55e;
  --door: #f59e0b;
  --door-open: #fcd34d;
  --accent: #60a5fa;

  --red-btn: #dc2626;
  --red-door: #b91c1c;
  --red-door-border: #7f1d1d;
  --red-door-open: #fca5a5;

  --blue-btn: #2563eb;
  --blue-door: #1d4ed8;
  --blue-door-border: #1e3a5f;
  --blue-door-open: #93c5fd;

  --radius: 18px;
}

* { box-sizing: border-box; }

body {
  margin: 0;
  min-height: 100vh;
  background:
    linear-gradient(90deg, rgba(255,255,255,.035) 1px, transparent 1px),
    linear-gradient(rgba(255,255,255,.035) 1px, transparent 1px),
    var(--bg);
  background-size: 32px 32px;
  color: var(--ink);
  font-family: system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", "Microsoft YaHei", sans-serif;
}

.app {
  width: min(1420px, 96vw);
  margin: 18px auto;
  display: grid;
  gap: 14px;
}

.topbar, .card {
  background: rgba(24, 32, 42, .95);
  border: 1px solid var(--line);
  border-radius: 22px;
  box-shadow: 0 20px 50px rgba(0,0,0,.22);
}

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  padding: 16px 18px;
}

h1 {
  margin: 0 0 6px;
  font-size: clamp(24px, 3vw, 36px);
  line-height: 1.05;
}

h2 {
  margin: 0;
  font-size: 20px;
}

p {
  margin: 0;
  color: var(--muted);
  line-height: 1.55;
}

.legend {
  display: flex;
  flex-wrap: wrap;
  justify-content: flex-end;
  gap: 8px;
  min-width: 340px;
  color: var(--muted);
  font-size: 12px;
}

.legend span {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  background: rgba(255,255,255,.04);
  border: 1px solid var(--line);
  border-radius: 999px;
  padding: 6px 8px;
}

.swatch {
  width: 14px;
  height: 14px;
  display: inline-block;
  border-radius: 3px;
  border: 1px solid rgba(255,255,255,.35);
}
.swatch.void { background: var(--void); }
.swatch.wall { background: var(--wall); }
.swatch.floor { background: var(--floor); }
.swatch.player { background: var(--blue); }
.swatch.goal { background: var(--goal); }

.main {
  display: grid;
  grid-template-columns: minmax(460px, .95fr) minmax(560px, 1.05fr);
  gap: 14px;
  align-items: start;
}

.card { padding: 16px; }

.section-title {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: 12px;
}

.game-area {
  display: grid;
  gap: 12px;
  position: sticky;
  top: 12px;
}

.status {
  background: #13233d;
  border: 1px solid #27518d;
  color: #dbeafe;
  border-radius: 14px;
  padding: 12px 14px;
  line-height: 1.5;
  font-weight: 700;
}

.board-wrap {
  overflow: auto;
  padding: 16px;
  border-radius: 18px;
  background: #0b0f15;
  border: 1px solid var(--line);
}

.board {
  --cols: 8;
  --rows: 3;
  --cell: 56px;
  position: relative;
  width: calc(var(--cols) * var(--cell));
  height: calc(var(--rows) * var(--cell));
  display: grid;
  grid-template-columns: repeat(var(--cols), var(--cell));
  grid-template-rows: repeat(var(--rows), var(--cell));
  margin: auto;
  background: transparent;
  overflow: visible;
}

.tile {
  position: relative;
  width: var(--cell);
  height: var(--cell);
  border: none;
  overflow: visible;
}

.tile-ground,
.tile-object,
.tile-structure {
  position: absolute;
  inset: 0;
  background-size: 100% 100%;
  background-repeat: no-repeat;
  background-position: center;
  image-rendering: pixelated;
  image-rendering: crisp-edges;
  pointer-events: none;
}

.tile-ground { z-index: var(--z-base, 0); }
.tile-object { z-index: calc(var(--z-base, 0) + 2); }

.tile-structure {
  z-index: calc(var(--z-base, 0) + 2);
}

.ground-floor { background-image: url("tiles/kenney/ground_06.png"); }
.ground-goal { background-image: url("tiles/kenney/ground_03.png"); }
.ground-void { background-image: url("tiles/dirt.png"); }

.sprite-wall { background-image: url("tiles/kenney/block_08.png"); }
.sprite-button,
.sprite-red-button,
.sprite-blue-button {
  width: 72%;
  height: 72%;
  inset: auto;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
  box-shadow: none;
  background-size: contain;
  background-repeat: no-repeat;
  background-position: center;
}
.sprite-button { background-image: url("tiles/kenney/crate_37.png"); }
.sprite-red-button { background-image: url("tiles/kenney/crate_38.png"); }
.sprite-blue-button { background-image: url("tiles/kenney/crate_39.png"); }

.tile.button.pressed .sprite-button { background-image: url("tiles/kenney/crate_32.png"); }
.tile.red-button.pressed .sprite-red-button { background-image: url("tiles/kenney/crate_33.png"); }
.tile.blue-button.pressed .sprite-blue-button { background-image: url("tiles/kenney/crate_34.png"); }

.tile.button.pressed .tile-object,
.tile.red-button.pressed .tile-object,
.tile.blue-button.pressed .tile-object {
  transform: translate(-50%, -50%) scale(.94);
}
.sprite-goal {
  background-image: url("tiles/kenney/environment_12.png");
  width: 100%;
  height: 100%;
  inset: 0;
  background-size: 100% 100%;
  background-repeat: no-repeat;
  background-position: center;
  transform: none;
  box-shadow: none;
}
.sprite-door { background-image: url("tiles/kenney/crate_17.png"); }
.sprite-red-door { background-image: url("tiles/kenney/crate_18.png"); }
.sprite-blue-door { background-image: url("tiles/kenney/crate_09.png"); }

.tile.wall .tile-structure {
  left: -1px;
  width: calc(100% + 2px);
}

.tile.door.open .tile-structure,
.tile.red-door.open .tile-structure,
.tile.blue-door.open .tile-structure {
  filter: brightness(1.18) saturate(1.05);
}

.tile.door.open::after,
.tile.red-door.open::after,
.tile.blue-door.open::after {
  content: "";
  position: absolute;
  inset: 0;
  border-radius: 2px;
  pointer-events: none;
  z-index: calc(var(--z-base, 0) + 3);
}
.tile.door.open::after     { background: rgba(252, 211, 77, .45); }
.tile.red-door.open::after { background: rgba(252, 165, 165, .45); }
.tile.blue-door.open::after { background: rgba(147, 197, 253, .45); }

.piece {
  position: absolute;
  width: calc(var(--cell) * .82);
  height: calc(var(--cell) * .82);
  top: calc(var(--cell) * .09);
  left: calc(var(--cell) * .09);
  border-radius: 0;
  transition: transform 130ms ease;
  z-index: 4;
  box-shadow: none;
  background-size: contain;
  background-repeat: no-repeat;
  background-position: center bottom;
  image-rendering: auto;
  transform-origin: center bottom;
  will-change: transform, scale, filter;
  animation: piece-idle 1.5s ease-in-out infinite;
}

.player {
  background-image: url("tiles/kenney/player_05.png");
}

.ghost {
  background-image: url("tiles/kenney/player_echo_05.png");
  z-index: 4;
  animation-duration: 1.8s;
  filter: saturate(.72);
}

@keyframes piece-idle {
  0%, 100% { scale: 1; filter: brightness(1); }
  50% { scale: .992; filter: brightness(1.015); }
}

.hidden { display: none !important; }

.hud {
  display: grid;
  grid-template-columns: repeat(5, minmax(0, 1fr));
  gap: 8px;
}

.stat {
  background: var(--panel2);
  border: 1px solid var(--line);
  border-radius: 14px;
  padding: 10px;
  color: var(--muted);
  min-height: 64px;
  font-size: 13px;
}

.stat b {
  display: block;
  color: var(--ink);
  font-size: 20px;
  margin-top: 4px;
}

.row {
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
  align-items: center;
}

button, input, textarea, summary {
  font: inherit;
}

button {
  border: 0;
  border-radius: 12px;
  padding: 10px 12px;
  font-weight: 800;
  cursor: pointer;
  color: var(--ink);
  background: #334155;
  transition: transform 90ms ease, filter 120ms ease, opacity 120ms ease;
}

button:hover { filter: brightness(1.08); }
button:active { transform: translateY(1px); }
button:disabled {
  cursor: not-allowed;
  opacity: .45;
  filter: grayscale(.25);
}
button.primary { background: var(--accent); color: #07111f; }
button.danger { background: #ef4444; color: white; }
button.good { background: #22c55e; color: #03120a; }

.checkline {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  color: var(--muted);
  font-size: 13px;
  background: var(--panel2);
  border: 1px solid var(--line);
  border-radius: 999px;
  padding: 8px 10px;
}
.checkline input { width: auto; }

.dpad {
  display: none;
  grid-template-columns: repeat(3, 44px);
  grid-template-areas:
    ". up ."
    "left down right";
  gap: 8px;
  justify-content: center;
}
.dpad button { min-height: 42px; padding: 0; }
.dpad [data-move="up"] { grid-area: up; }
.dpad [data-move="left"] { grid-area: left; }
.dpad [data-move="down"] { grid-area: down; }
.dpad [data-move="right"] { grid-area: right; }

.editor {
  display: grid;
  gap: 14px;
  overflow: hidden;
  transition: padding 0.3s ease;
}

.editor-header {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 12px;
}

.toggle-btn {
  width: 36px;
  height: 36px;
  border-radius: 50%;
  font-size: 20px;
  display: grid;
  place-items: center;
  padding: 0;
  background: var(--panel2);
  border: 1px solid var(--line);
  flex-shrink: 0;
  transition: transform 0.3s ease;
}

.toggle-btn.collapsed { transform: rotate(-90deg); }

.editor-body {
  display: grid;
  gap: 12px;
  overflow: hidden;
  transition: max-height 0.4s ease, opacity 0.3s ease;
  max-height: 2200px;
  opacity: 1;
}

.editor-body.collapsed {
  max-height: 0;
  opacity: 0;
  pointer-events: none;
}

.editor-toolbar {
  display: grid;
  grid-template-columns: repeat(2, minmax(86px, 1fr)) repeat(4, auto);
  gap: 8px;
  align-items: end;
}

label {
  color: var(--muted);
  font-size: 13px;
  display: grid;
  gap: 5px;
}

input {
  width: 100%;
  border: 1px solid var(--line);
  border-radius: 12px;
  background: #0f131a;
  color: var(--ink);
  padding: 10px;
}

.toolbox {
  display: grid;
  grid-template-columns: repeat(6, minmax(0, 1fr));
  gap: 8px;
}

.tool {
  min-height: 42px;
  padding: 8px 6px;
  background: var(--panel2);
  border: 1px solid var(--line);
  font-size: 13px;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 5px;
}

.tool kbd {
  min-width: 18px;
  height: 18px;
  padding: 0 4px;
  display: inline-grid;
  place-items: center;
  border-radius: 5px;
  background: rgba(255,255,255,.12);
  color: var(--muted);
  font-size: 11px;
  line-height: 1;
}

.tool.active {
  outline: 3px solid var(--accent);
  background: #1d3558;
}

.tool.tool-btn-yellow { border-left: 3px solid var(--button); }
.tool.tool-btn-red { border-left: 3px solid var(--red-btn); }
.tool.tool-btn-blue { border-left: 3px solid var(--blue-btn); }
.tool.tool-door-yellow { border-left: 3px solid var(--door); }
.tool.tool-door-red { border-left: 3px solid var(--red-door); }
.tool.tool-door-blue { border-left: 3px solid var(--blue-door); }

.editor-meta {
  display: flex;
  justify-content: space-between;
  gap: 10px;
  color: var(--muted);
  font-size: 12px;
}

.editor-shell {
  overflow: auto;
  padding: 10px;
  border-radius: 16px;
  background: #0b0f15;
  border: 1px solid var(--line);
}

.editor-grid {
  --cell: 28px;
  display: grid;
  grid-template-columns: repeat(16, var(--cell));
  grid-template-rows: repeat(16, var(--cell));
  width: calc(16 * var(--cell));
  height: calc(16 * var(--cell));
  border: 2px solid var(--line);
  background: var(--void);
  margin: 0 auto;
  touch-action: none;
  user-select: none;
}

.edit-cell {
  width: var(--cell);
  height: var(--cell);
  border: 1px solid var(--grid);
  position: relative;
  cursor: crosshair;
  transition: transform 0.08s ease, background 0.1s ease;
}

.edit-cell:hover {
  z-index: 2;
  outline: 2px solid var(--accent);
  outline-offset: -2px;
}

.edit-cell.void { background: var(--void); }
.edit-cell.wall { background: var(--wall); border-color: #5f3b18; }
.edit-cell.floor { background: var(--floor); }

.edit-cell.start,
.edit-cell.button,
.edit-cell.red-button,
.edit-cell.blue-button,
.edit-cell.goal,
.edit-cell.door,
.edit-cell.red-door,
.edit-cell.blue-door { background: var(--floor); }

.edit-cell.start::after {
  content: "";
  position: absolute;
  inset: 4px;
  background: var(--blue);
  border: 1px solid #9494ff;
  border-radius: 3px;
}

.edit-cell.button::after,
.edit-cell.red-button::after,
.edit-cell.blue-button::after {
  content: "";
  position: absolute;
  inset: 4px;
  border-radius: 3px;
}
.edit-cell.button::after { background: var(--button); }
.edit-cell.red-button::after { background: var(--red-btn); }
.edit-cell.blue-button::after { background: var(--blue-btn); }

.edit-cell.goal::after {
  content: "\2713";
  position: absolute;
  inset: 4px;
  display: grid;
  place-items: center;
  background: var(--goal);
  color: white;
  border-radius: 3px;
  font-size: 13px;
  font-weight: 900;
}

.edit-cell.door::after,
.edit-cell.red-door::after,
.edit-cell.blue-door::after {
  position: absolute;
  inset: 4px;
  display: grid;
  place-items: center;
  border-radius: 3px;
  font-size: 11px;
  font-weight: 900;
}

.edit-cell.door::after {
  content: "\95E8";
  background: var(--door);
  color: #111827;
  border: 2px solid #92400e;
}
.edit-cell.red-door::after {
  content: "\7EA2";
  background: var(--red-door);
  color: #111827;
  border: 2px solid var(--red-door-border);
}
.edit-cell.blue-door::after {
  content: "\84DD";
  background: var(--blue-door);
  color: #111827;
  border: 2px solid var(--blue-door-border);
}

.edit-cell.outside {
  box-shadow: inset 0 0 0 2px rgba(96, 165, 250, .25);
}

.editor-feedback {
  min-height: 44px;
  border: 1px solid var(--line);
  background: rgba(15, 19, 26, .82);
  border-radius: 14px;
  padding: 10px 12px;
  color: var(--muted);
  line-height: 1.5;
  font-size: 13px;
}
.editor-feedback.ok { border-color: rgba(34, 197, 94, .45); color: #bbf7d0; }
.editor-feedback.warn { border-color: rgba(245, 158, 11, .55); color: #fde68a; }
.editor-feedback.error { border-color: rgba(239, 68, 68, .55); color: #fecaca; }

.json-panel {
  border: 1px solid var(--line);
  border-radius: 14px;
  background: rgba(15, 19, 26, .55);
  overflow: hidden;
}

.json-panel summary {
  cursor: pointer;
  padding: 12px;
  font-weight: 800;
  color: var(--ink);
}

.json-actions { padding: 0 12px 12px; }

textarea {
  width: calc(100% - 24px);
  min-height: 160px;
  margin: 0 12px 12px;
  resize: vertical;
  border: 1px solid var(--line);
  border-radius: 14px;
  background: #0f131a;
  color: #d1fae5;
  padding: 12px;
  line-height: 1.45;
  font-family: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, "Liberation Mono", monospace;
  font-size: 12px;
}

.hint {
  font-size: 13px;
  color: var(--muted);
  line-height: 1.65;
}

.mini {
  font-size: 12px;
  color: var(--muted);
}

.toast {
  position: fixed;
  left: 50%;
  bottom: 20px;
  transform: translateX(-50%);
  z-index: 20;
  background: #0f172a;
  color: var(--ink);
  border: 1px solid var(--line);
  border-radius: 999px;
  padding: 10px 14px;
  box-shadow: 0 16px 40px rgba(0,0,0,.36);
  max-width: min(520px, 92vw);
  text-align: center;
}

@media (max-width: 1120px) {
  .topbar { align-items: flex-start; flex-direction: column; }
  .legend { min-width: 0; justify-content: flex-start; }
  .main { grid-template-columns: 1fr; }
  .game-area { position: static; }
  .hud { grid-template-columns: repeat(2, 1fr); }
}

@media (max-width: 720px) {
  .app { width: 100%; margin: 0; }
  .topbar, .card { border-radius: 0; }
  .board { --cell: 44px; }
  .editor-grid { --cell: 21px; }
  .toolbox { grid-template-columns: repeat(3, 1fr); }
  .editor-toolbar { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .editor-toolbar button { min-height: 40px; }
  .dpad { display: grid; }
}
