:root {
  --bg: #15122b;
  --panel: #241f45;
  --panel-2: #2e2757;
  --edge: #0b0917;
  --hi: #5a52a0;
  --ink: #f4f1de;
  --muted: #a7a3c9;
  --accent: #ffd23f;
  --accent-ink: #2a1d00;
  --good: #7ef0c4;
  --warn: #ff8a7a;
  --btn: #3b3470;
  --pixel: 'Press Start 2P', 'Courier New', monospace;
  --ui: system-ui, -apple-system, 'Segoe UI', Roboto, sans-serif;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  height: 100%;
  background: var(--bg);
  color: var(--ink);
  font: 14px/1.4 var(--ui);
  overflow: hidden;
}

[hidden] { display: none !important; }

#app { height: 100%; }

.screen {
  position: fixed;
  inset: 0;
  display: flex;
  flex-direction: column;
  background: radial-gradient(ellipse at 50% 0%, #2b2456 0%, var(--bg) 70%);
}

/* ---- Buttons and fields ------------------------------------------------ */

.btn {
  font: 10px/1 var(--pixel);
  color: var(--ink);
  background: var(--btn);
  border: 3px solid var(--edge);
  box-shadow: inset 0 -3px 0 #221c4f, inset 0 3px 0 #5a52a0;
  padding: 12px 14px;
  cursor: pointer;
  letter-spacing: 0.5px;
  white-space: nowrap;
  touch-action: manipulation;
}

.btn:hover { background: #4a4290; }
.btn:active { transform: translateY(2px); box-shadow: inset 0 -1px 0 #221c4f; }
.btn:focus-visible, .field:focus-visible, .pal-btn:focus-visible { outline: 3px solid var(--accent); outline-offset: 2px; }
.btn:disabled { opacity: 0.4; cursor: default; }
.btn.big { font-size: 16px; padding: 18px 26px; min-width: 300px; }
.btn.small { font-size: 9px; padding: 9px 10px; }
.btn.primary { background: var(--accent); color: var(--accent-ink); box-shadow: inset 0 -3px 0 #b8900f, inset 0 3px 0 #fff1a6; }
.btn.primary:hover { background: #ffdd66; }
.btn.danger { background: #7a2f3d; }
.btn.danger:hover { background: #963a4c; }

.field {
  font: 13px var(--ui);
  color: var(--ink);
  background: #120f26;
  border: 2px solid var(--edge);
  padding: 7px 9px;
  min-width: 0;
}

.name-field { width: 220px; }

kbd {
  font: 11px var(--ui);
  background: #100d24;
  border: 1px solid #4a4290;
  border-bottom-width: 3px;
  border-radius: 4px;
  padding: 1px 5px;
  color: var(--ink);
}

.hint, .meta { color: var(--muted); font-size: 12px; margin: 0; }

/* ---- Title ------------------------------------------------------------- */

.title-wrap {
  margin: auto;
  padding: 24px 16px;
  text-align: center;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 18px;
}

.logo {
  margin: 0;
  font: 34px/1.15 var(--pixel);
  color: var(--accent);
  text-shadow: 4px 4px 0 #b8432b, 8px 8px 0 #2a1d00;
}

.logo span { display: block; }
.logo span + span { color: #7ef0c4; text-shadow: 4px 4px 0 #1f6b58, 8px 8px 0 #0a2a22; }

.tagline { color: var(--muted); margin: 0; font-size: 15px; }

.menu { display: flex; flex-direction: column; align-items: center; gap: 14px; }

.controls { color: var(--muted); max-width: 620px; font-size: 13px; line-height: 2; }

/* ---- Top bars ---------------------------------------------------------- */

.topbar, .ed-top, .game-top {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 10px 14px;
  background: #1b1738;
  border-bottom: 3px solid var(--edge);
  flex-wrap: wrap;
}

.topbar h2 { flex: 1; margin: 0; font: 12px var(--pixel); color: var(--accent); text-align: center; }

.scroll-area { flex: 1; overflow: auto; padding: 18px; }

/* ---- Level select ------------------------------------------------------ */

.card-grid {
  display: grid;
  justify-content: center;
  grid-template-columns: repeat(auto-fill, minmax(230px, 260px));
  gap: 16px;
  max-width: 1100px;
  margin: 0 auto;
}

.card {
  background: var(--panel);
  border: 3px solid var(--edge);
  box-shadow: inset 0 3px 0 var(--hi);
  padding: 16px;
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.card-num { font: 9px var(--pixel); color: var(--good); }
.card h3 { margin: 0; font: 12px/1.5 var(--pixel); color: var(--ink); }
.card-actions { display: flex; gap: 8px; margin-top: auto; padding-top: 8px; flex-wrap: wrap; }
.scroll-area > .hint { text-align: center; margin-top: 22px; }

/* ---- Game -------------------------------------------------------------- */

#screen-game { background: #0a0816; }

.game-top { justify-content: space-between; }
.game-title { font: 10px var(--pixel); color: var(--muted); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

.game-wrap {
  flex: 1;
  min-height: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 8px;
}

#game-canvas {
  image-rendering: pixelated;
  image-rendering: crisp-edges;
  background: #000;
  box-shadow: 0 0 0 4px var(--edge), 0 0 0 7px #2e2757;
  max-width: 100%;
  max-height: 100%;
}

.overlay {
  position: fixed;
  inset: 0;
  z-index: 10;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 16px;
  background: rgba(8, 6, 18, 0.72);
}

.overlay .panel {
  background: var(--panel);
  border: 4px solid var(--edge);
  box-shadow: inset 0 4px 0 var(--hi), 0 10px 0 rgba(0, 0, 0, 0.35);
  padding: 26px 28px;
  text-align: center;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 18px;
  max-width: 460px;
  width: 100%;
}

.overlay h2 { margin: 0; font: 16px/1.4 var(--pixel); color: var(--accent); }
.stats { margin: 0; font: 10px/1.8 var(--pixel); color: var(--good); }

/* ---- Editor ------------------------------------------------------------ */

.ed-top .name-field { flex: 0 1 220px; }

.ed-body { flex: 1; display: flex; min-height: 0; }

.ed-side {
  width: 236px;
  flex: none;
  overflow-y: auto;
  background: #1b1738;
  border-right: 3px solid var(--edge);
  padding: 12px;
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.side-block { display: flex; flex-direction: column; gap: 8px; }
.side-block h3 { margin: 0; font: 9px var(--pixel); color: var(--muted); }
.side-block h3 + * { margin-top: 2px; }

.tool-row { display: grid; grid-template-columns: 1fr 1fr; gap: 6px; }
.tool-btn {
  font: 9px var(--pixel);
  color: var(--ink);
  background: var(--btn);
  border: 2px solid var(--edge);
  padding: 8px 4px;
  cursor: pointer;
}
.tool-btn.on { background: var(--accent); color: var(--accent-ink); }
.tool-btn.zoom-btn { font-size: 8px; }

.history { flex-direction: row; }
.history .btn { flex: 1; }

.palette-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 6px; }

.pal-btn {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 4px;
  font: 7px/1.2 var(--pixel);
  color: var(--muted);
  background: #120f26;
  border: 2px solid var(--edge);
  padding: 6px 4px;
  cursor: pointer;
  min-height: 62px;
}

.pal-btn canvas { width: 32px; height: 32px; image-rendering: pixelated; }
.pal-btn span { text-align: center; }
.pal-btn:hover { border-color: var(--hi); color: var(--ink); }
.pal-btn.on { border-color: var(--accent); color: var(--accent); background: #2a2150; }

.ed-help { color: var(--muted); font-size: 12px; line-height: 1.8; margin: 0; }

.ed-canvas-wrap {
  flex: 1;
  min-width: 0;
  overflow: auto;
  background: #0b0917;
  position: relative;
}

#ed-canvas {
  display: block;
  image-rendering: pixelated;
  margin: 12px;
  box-shadow: 0 0 0 3px var(--edge);
  cursor: crosshair;
  touch-action: none;
}

.ed-status {
  display: flex;
  justify-content: space-between;
  gap: 12px;
  padding: 6px 12px;
  background: #1b1738;
  border-top: 3px solid var(--edge);
  font-size: 12px;
  color: var(--muted);
  flex-wrap: wrap;
}

#ed-msg.ok { color: var(--good); }
#ed-msg.warn { color: var(--warn); }

/* ---- Dialogs ----------------------------------------------------------- */

#dialog .panel {
  align-items: stretch;
  text-align: left;
  max-width: 560px;
  max-height: 86vh;
  overflow: auto;
}

#dialog h2 { text-align: center; }

.dialog-foot { display: flex; justify-content: flex-end; gap: 10px; flex-wrap: wrap; }

.level-list { display: flex; flex-direction: column; gap: 10px; max-height: 56vh; overflow-y: auto; }
.level-row {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 12px;
  background: #1b1738;
  border: 2px solid var(--edge);
  padding: 10px 12px;
}
.level-info { display: flex; flex-direction: column; gap: 4px; min-width: 0; }
.level-info strong { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.row-actions { display: flex; gap: 6px; flex: none; }

.form { display: flex; flex-direction: column; gap: 12px; }
.field-row { display: grid; grid-template-columns: 130px 1fr; gap: 10px; align-items: center; }
.field-row .field { width: 100%; }

/* ---- Small screens ----------------------------------------------------- */

@media (max-width: 760px) {
  .logo { font-size: 22px; }
  .btn.big { min-width: 0; width: 80vw; font-size: 13px; }
  .ed-body { flex-direction: column; }
  .ed-side { width: 100%; max-height: 40vh; flex-direction: row; flex-wrap: wrap; border-right: 0; border-bottom: 3px solid var(--edge); }
  .ed-side .side-block { flex: 1 1 200px; }
  .ed-help { display: none; }
  .name-field { width: 100%; }
  .ed-top { gap: 6px; }
}
