/* Layout: an arcade cabinet — the game screen is the hero, Build mode adds a tool rail beside it. */
:root {
  --ink: #120d1d;
  --well: #0a0712;
  --panel: #1c1530;
  --panel-2: #271e42;
  --line: #3d3060;
  --text: #f1e7d6;
  --muted: #a596c2;
  --gold: #ffc84a;
  --red: #e8434b;
  --teal: #42c7b4;
  --display: 'Silkscreen', 'Courier New', ui-monospace, monospace;
  --body: 'VT323', 'Courier New', ui-monospace, monospace;
  color-scheme: dark;
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }
html, body { min-height: 100%; }
body {
  margin: 0;
  background: var(--ink);
  color: var(--text);
  font-family: var(--body);
  font-size: 20px;
  line-height: 1.25;
}

.app {
  max-width: 1280px;
  margin: 0 auto;
  padding-block: 12px 20px;
  padding-left: max(16px, env(safe-area-inset-left, 0px));
  padding-right: max(16px, env(safe-area-inset-right, 0px));
  display: flex;
  flex-direction: column;
  gap: 12px;
}

/* ---------- Top bar ---------- */
.topbar {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: 10px 16px;
}
.brand {
  display: flex;
  align-items: center;
  gap: 10px;
  font-family: var(--display);
  font-weight: 700;
  font-size: 22px;
  letter-spacing: 0.04em;
  color: var(--gold);
  text-shadow: 2px 2px 0 var(--red);
}
.brand canvas { width: 32px; height: 32px; image-rendering: pixelated; }
.modes, .toggles { display: flex; flex-wrap: wrap; gap: 8px; }

/* ---------- Buttons ---------- */
.btn {
  font-family: var(--display);
  font-size: 13px;
  letter-spacing: 0.05em;
  text-transform: uppercase;
  color: var(--text);
  background: var(--panel-2);
  border: 2px solid var(--line);
  border-bottom-width: 4px;
  padding: 7px 12px 5px;
  cursor: pointer;
  border-radius: 0;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  min-height: 36px;
  touch-action: manipulation;
}
.btn:hover { background: var(--line); }
.btn:active { transform: translateY(2px); border-bottom-width: 2px; margin-bottom: 2px; }
.btn:focus-visible, input:focus-visible, textarea:focus-visible, canvas:focus-visible {
  outline: 2px solid var(--gold);
  outline-offset: 2px;
}
.btn.primary { background: var(--gold); color: var(--ink); border-color: #b9821c; }
.btn.primary:hover { background: #ffd877; }
.btn.danger { color: #ffb3b6; }
.btn.armed { background: var(--red); border-color: #8c1f2b; color: var(--text); }
.btn.nav[aria-current="true"] { background: var(--text); color: var(--ink); border-color: var(--muted); }
.btn.toggle[aria-pressed="false"] { color: var(--muted); text-decoration: line-through; text-decoration-thickness: 2px; }
.btn.icon { padding-inline: 10px; }
.btn:disabled { opacity: 0.45; cursor: default; transform: none; }
kbd {
  font-family: var(--display);
  font-size: 10px;
  padding: 1px 4px;
  border: 1px solid currentColor;
  opacity: 0.75;
}

/* ---------- Stage ---------- */
.layout {
  display: grid;
  grid-template-columns: minmax(0, 1fr);
  gap: 16px;
  align-items: start;
}
.stage-col { min-width: 0; display: flex; flex-direction: column; gap: 10px; align-items: center; }
.bezel {
  max-width: 100%;
  padding: 8px;
  background: var(--well);
  border: 3px solid var(--line);
  box-shadow: 0 0 0 3px var(--well), 0 18px 40px rgba(0, 0, 0, 0.45);
  display: flex;
  justify-content: center;
}
#screen {
  display: block;
  width: 400px;
  max-width: 100%;
  height: auto;
  image-rendering: pixelated;
  image-rendering: crisp-edges;
  background: #000;
  touch-action: none;
}
.app[data-mode="edit"] #screen { cursor: crosshair; }
.hint {
  margin: 0;
  color: var(--muted);
  text-align: center;
  text-wrap: balance;
  max-width: 60ch;
}

/* ---------- Touch controls ---------- */
.touch { display: none; width: 100%; max-width: 720px; justify-content: space-between; gap: 12px; user-select: none; -webkit-user-select: none; }
.pad { display: flex; gap: 10px; align-items: center; }
.tbtn {
  font-family: var(--display);
  font-size: 18px;
  width: 64px;
  height: 64px;
  color: var(--text);
  background: var(--panel-2);
  border: 2px solid var(--line);
  border-bottom-width: 5px;
  touch-action: none;
}
.tbtn.jump { width: 96px; background: var(--gold); color: var(--ink); border-color: #b9821c; }
.tbtn.small { width: 48px; height: 48px; font-size: 14px; }
.tbtn.on { transform: translateY(3px); border-bottom-width: 2px; }
@media (pointer: coarse) {
  .app[data-mode="play"] .touch, .app[data-mode="test"] .touch { display: flex; }
  .keys { display: none !important; }
}
.app.touch-seen[data-mode="play"] .touch, .app.touch-seen[data-mode="test"] .touch { display: flex; }

/* ---------- Editor panel ---------- */
.panel {
  display: none;
  min-width: 0;
  background: var(--panel);
  border: 2px solid var(--line);
  padding: 14px;
  flex-direction: column;
  gap: 16px;
}
.app[data-mode="edit"] .panel { display: flex; }
@media (min-width: 1020px) {
  .app[data-mode="edit"] .layout { grid-template-columns: minmax(0, 1fr) 320px; }
}
.panel-row { display: flex; flex-wrap: wrap; gap: 8px; align-items: center; }
.primary-row { display: grid; grid-template-columns: minmax(0, 1fr) auto auto; gap: 6px; }
.primary-row .primary { font-size: 15px; min-height: 44px; }
.primary-row .icon { min-height: 44px; }
.group { display: flex; flex-direction: column; gap: 8px; }
.label {
  margin: 0;
  font-family: var(--display);
  font-size: 11px;
  font-weight: 400;
  letter-spacing: 0.12em;
  text-transform: uppercase;
  color: var(--muted);
}
.tools { display: grid; grid-template-columns: repeat(4, minmax(0, 1fr)); gap: 6px; }
.tool { padding-inline: 4px; font-size: 11px; flex-direction: column; gap: 2px; min-height: 44px; }
.tool[aria-pressed="true"], .pal[aria-pressed="true"] { background: var(--well); border-color: var(--gold); color: var(--gold); }
.palette { display: grid; grid-template-columns: repeat(auto-fill, minmax(62px, 1fr)); gap: 6px; }
.pal {
  flex-direction: column;
  gap: 3px;
  padding: 6px 2px 4px;
  font-size: 9px;
  letter-spacing: 0.02em;
  min-height: 0;
  position: relative;
}
.pal canvas { width: 32px; height: 32px; image-rendering: pixelated; flex: none; }
.pal span { max-width: 100%; white-space: nowrap; }
.pal kbd { position: absolute; top: 2px; right: 3px; font-size: 8px; padding: 0 2px; border: 0; }
.tip, .stats, .status { margin: 0; color: var(--muted); font-size: 18px; min-height: 1.25em; }
.stats .warn { color: var(--gold); }
.status.ok { color: var(--teal); }
.status.err { color: #ff8f94; }

.field { display: flex; flex-direction: column; gap: 4px; min-width: 0; }
.field > span { font-family: var(--display); font-size: 10px; letter-spacing: 0.1em; text-transform: uppercase; color: var(--muted); }
input, textarea {
  font-family: var(--body);
  font-size: 20px;
  color: var(--text);
  background: var(--well);
  border: 2px solid var(--line);
  border-radius: 0;
  padding: 4px 8px;
  width: 100%;
  min-width: 0;
}
textarea { font-size: 16px; font-family: ui-monospace, 'Courier New', monospace; resize: vertical; word-break: break-all; }
.size-row { display: grid; grid-template-columns: 1fr 1fr auto; gap: 8px; align-items: end; }
.file-row { display: grid; grid-template-columns: 1fr 1fr; gap: 6px; }

/* ---------- Footer key help ---------- */
.keys {
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  gap: 6px 18px;
  color: var(--muted);
  font-size: 18px;
}
.keys kbd { margin-right: 2px; }
.app[data-mode="edit"] .keys { display: none; }

/* ---------- Modals ---------- */
.modal {
  position: fixed;
  inset: 0;
  z-index: 20;
  background: rgba(10, 7, 18, 0.82);
  display: flex;
  align-items: flex-start;
  justify-content: center;
  overflow-y: auto;
  padding-block: max(24px, env(safe-area-inset-top, 0px)) 24px;
  padding-left: max(16px, env(safe-area-inset-left, 0px));
  padding-right: max(16px, env(safe-area-inset-right, 0px));
}
.sheet {
  width: 100%;
  max-width: 620px;
  background: var(--panel);
  border: 3px solid var(--line);
  box-shadow: 0 0 0 3px var(--well);
  padding: 18px;
  display: flex;
  flex-direction: column;
  gap: 12px;
}
.sheet-head { display: flex; justify-content: space-between; align-items: center; gap: 12px; }
.sheet h2 { margin: 0; font-family: var(--display); font-size: 20px; color: var(--gold); text-wrap: balance; }
.sheet-note { margin: 0; color: var(--muted); max-width: 60ch; }
.level-list { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 6px; }
.level-list li {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 8px 12px;
  padding: 8px 10px;
  background: var(--panel-2);
  border: 2px solid var(--line);
}
.level-list .meta { flex: 1 1 180px; min-width: 0; display: flex; flex-direction: column; }
.level-list .name { font-family: var(--display); font-size: 14px; overflow-wrap: anywhere; }
.level-list .sub { color: var(--muted); font-size: 17px; font-variant-numeric: tabular-nums; }
.level-list .acts { display: flex; gap: 6px; flex-wrap: wrap; }
.level-list .empty { color: var(--muted); background: none; border-style: dashed; }
.sr-only { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; }

@media (max-width: 520px) {
  .brand { font-size: 18px; }
  .pad { gap: 6px; }
  .tbtn { width: 54px; height: 58px; }
  .tbtn.jump { width: 78px; font-size: 15px; }
  .tbtn.small { width: 42px; height: 42px; }
  .btn { font-size: 12px; padding-inline: 9px; }
  .bezel { padding: 4px; border-width: 2px; }
}
@media (prefers-reduced-motion: reduce) {
  .btn:active { transform: none; }
}
