:root {
  --bg: #12161a;
  --panel: #1c2228;
  --text: #dfe6d8;
  --muted: #8a968c;
  --accent: #9bbc0f;
  --error: #ff8a7a;
  --lcd: #e0f8d0;
  color-scheme: dark;
}

* { box-sizing: border-box; }

body {
  margin: 0;
  min-height: 100vh;
  background: var(--bg);
  color: var(--text);
  font: 15px/1.5 ui-sans-serif, system-ui, sans-serif;
  display: grid;
  place-items: center;
}

main {
  width: min(100%, 704px);
  padding: 24px 16px;
  display: grid;
  gap: 16px;
}

header { display: flex; flex-wrap: wrap; align-items: baseline; gap: 4px 12px; }
h1 { margin: 0; font-size: 20px; letter-spacing: 0.04em; color: var(--accent); }
#fps { margin-left: auto; font-variant-numeric: tabular-nums; }
.muted { color: var(--muted); }

.screen-wrap {
  position: relative;
  background: var(--panel);
  padding: 32px;
  border-radius: 12px 12px 48px 12px;
  box-shadow: inset 0 0 0 1px #2c343c;
}

canvas {
  display: block;
  width: 100%;
  aspect-ratio: 160 / 144;
  image-rendering: pixelated;
  background: var(--lcd);
  border-radius: 2px;
}

.screen-wrap.dragging canvas { outline: 3px dashed var(--accent); outline-offset: 4px; }

.hint {
  position: absolute;
  inset: 32px;
  display: grid;
  place-content: center;
  text-align: center;
  color: #346856;
  font-weight: 600;
}
.hint[hidden] { display: none; }
.link { text-decoration: underline; cursor: pointer; }
.link input { display: none; }

.error {
  position: absolute;
  left: 32px; right: 32px; bottom: 32px;
  margin: 0;
  padding: 10px 12px;
  background: rgba(18, 22, 26, 0.92);
  color: var(--error);
  border-radius: 6px;
  white-space: pre-wrap;
  font: 13px/1.4 ui-monospace, monospace;
}

.paused {
  position: absolute;
  inset: 32px;
  display: grid;
  place-items: center;
  background: rgba(18, 22, 26, 0.55);
  color: var(--lcd);
  font-weight: 700;
  letter-spacing: 0.2em;
  text-transform: uppercase;
  border-radius: 2px;
}
.paused[hidden] { display: none; }

.toolbar { display: flex; flex-wrap: wrap; gap: 8px; }
.toolbar button {
  font: inherit;
  font-size: 13px;
  padding: 6px 14px;
  color: var(--text);
  background: var(--panel);
  border: 1px solid #3a444e;
  border-radius: 6px;
  cursor: pointer;
}
.toolbar button:hover:not(:disabled) { border-color: var(--accent); }
.toolbar button:disabled { opacity: 0.4; cursor: default; }
.toolbar button[aria-pressed="true"], .toolbar button.fast {
  background: var(--accent);
  border-color: var(--accent);
  color: var(--bg);
}
#speed { min-width: 4em; font-variant-numeric: tabular-nums; }
/* Held down to rewind: no text selection, scrolling or long-press menu. */
#rewind {
  min-width: 5.5em;
  font-variant-numeric: tabular-nums;
  touch-action: none;
  user-select: none;
  -webkit-user-select: none;
  -webkit-touch-callout: none;
}

.controls { display: flex; flex-wrap: wrap; gap: 8px 20px; font-size: 13px; }
kbd {
  font: 12px ui-monospace, monospace;
  padding: 1px 6px;
  border: 1px solid #3a444e;
  border-bottom-width: 2px;
  border-radius: 4px;
  color: var(--text);
}

/* On-screen controls. Hidden unless the main pointer is a finger, or the
   page was opened with ?touch (main.js adds .force-touch). */
.touch {
  display: none;
  grid-template-columns: 1fr 1fr;
  grid-template-areas: "dpad ab" "pills pills";
  align-items: center;
  gap: 24px 8px;
  padding: 4px 8px 0;
  /* No scrolling, pinch/double-tap zoom, text selection or long-press menus. */
  touch-action: none;
  user-select: none;
  -webkit-user-select: none;
  -webkit-touch-callout: none;
}
@media (pointer: coarse) {
  .touch { display: grid; }
  .controls { display: none; }
}
.force-touch .touch { display: grid; }

.dpad {
  grid-area: dpad;
  position: relative;
  width: 144px;
  aspect-ratio: 1;
}
.dpad .arm {
  position: absolute;
  background: #2c343c;
  box-shadow: inset 0 0 0 1px #3a444e;
}
.dpad .up, .dpad .down { left: 34%; width: 32%; height: 36%; }
.dpad .left, .dpad .right { top: 34%; height: 32%; width: 36%; }
.dpad .up { top: 0; border-radius: 6px 6px 0 0; }
.dpad .down { bottom: 0; border-radius: 0 0 6px 6px; }
.dpad .left { left: 0; border-radius: 6px 0 0 6px; }
.dpad .right { right: 0; border-radius: 0 6px 6px 0; }
.dpad::after { /* the middle of the cross */
  content: "";
  position: absolute;
  inset: 34%;
  background: #2c343c;
}

/* Like the real thing: B lower-left, A upper-right. */
.ab { grid-area: ab; justify-self: end; display: flex; align-items: flex-start; gap: 16px; }
.ab [data-button="5"] { margin-top: 40px; }
.round, .pill {
  display: grid;
  place-items: center;
  background: #2c343c;
  box-shadow: inset 0 0 0 1px #3a444e;
  color: var(--muted);
  font-weight: 700;
}
.round { width: 64px; aspect-ratio: 1; border-radius: 50%; font-size: 18px; }
.pills { grid-area: pills; justify-self: center; display: flex; gap: 24px; }
.pill {
  padding: 6px 16px;
  border-radius: 999px;
  font-size: 11px;
  letter-spacing: 0.08em;
  text-transform: uppercase;
}
.touch .pressed { background: var(--accent); color: var(--bg); }

@media (max-width: 480px) {
  main { padding: 12px 10px; }
  .screen-wrap { padding: 14px; border-radius: 10px 10px 28px 10px; }
  .hint, .paused { inset: 14px; }
  .error { left: 14px; right: 14px; bottom: 14px; }
}

details { background: var(--panel); border-radius: 8px; padding: 8px 12px; }
summary { cursor: pointer; color: var(--muted); }
/* Wrap long serial output: one unbroken line must not widen the whole page. */
#serial {
  margin: 8px 0 0;
  max-height: 200px;
  overflow: auto;
  white-space: pre-wrap;
  overflow-wrap: anywhere;
  font: 13px/1.4 ui-monospace, monospace;
}
main > * { min-width: 0; }

/* Debugger panel */
#debug-panel .toolbar { margin-top: 8px; align-items: center; }
#debug-note { font-size: 13px; font-variant-numeric: tabular-nums; }
.debug {
  display: grid;
  grid-template-columns: auto 1fr;
  gap: 12px 24px;
  margin-top: 12px;
  font: 13px/1.5 ui-monospace, monospace;
}
@media (max-width: 480px) { .debug { grid-template-columns: 1fr; } }
.regs {
  display: grid;
  grid-template-columns: auto auto;
  justify-content: start;
  gap: 0 12px;
  margin: 0;
}
.regs dt { color: var(--muted); }
.regs dd { margin: 0; }
.flags { display: flex; flex-wrap: wrap; gap: 4px; margin-top: 8px; }
.flags span {
  padding: 0 6px;
  border: 1px solid #3a444e;
  border-radius: 4px;
  color: var(--muted);
}
.flags span.on { background: var(--accent); border-color: var(--accent); color: var(--bg); }
.disasm { margin: 0; overflow-x: auto; white-space: pre; }
/* A gutter for ▶ (the next instruction) and ● (a breakpoint). */
.disasm div { cursor: pointer; border-radius: 3px; }
.disasm div:hover { background: #2c343c; }
.disasm div::before { content: ""; display: inline-block; width: 3ch; }
.disasm .current { color: var(--accent); }
.disasm .current::before { content: "▶"; }
.disasm .bp::before { content: "●"; color: var(--error); }
.disasm .bp.current::before { content: "●▶"; }

/* Breakpoint list, under the flags */
.bps { margin-top: 12px; display: grid; gap: 6px; }
.bps input {
  font: 13px ui-monospace, monospace;
  padding: 4px 6px;
  width: 100%;
  min-width: 0;
  color: var(--text);
  background: var(--bg);
  border: 1px solid #3a444e;
  border-radius: 6px;
}
.bps input.invalid { border-color: var(--error); }
.bp-list { list-style: none; margin: 0; padding: 0; }
.bp-list li { display: flex; align-items: center; gap: 8px; }
.bp-list li:not(.muted)::before { content: "●"; color: var(--error); }
.bp-list button {
  font: inherit;
  padding: 0 4px;
  color: var(--muted);
  background: none;
  border: none;
  cursor: pointer;
}
.bp-list button:hover { color: var(--error); }

/* Memory hex view */
.memory > :not(summary) { margin-top: 8px; }
.memory .toolbar { align-items: center; font-size: 13px; }
.memory input, .memory select {
  font: 13px ui-monospace, monospace;
  padding: 5px 8px;
  color: var(--text);
  background: var(--bg);
  border: 1px solid #3a444e;
  border-radius: 6px;
}
.memory input.invalid { border-color: var(--error); }
.mem-jumps button { padding: 3px 10px; font-size: 12px; }
.memview {
  overflow-x: auto;
  font: 13px/1.5 ui-monospace, monospace;
  white-space: pre;
  user-select: none;
}
.memview .addr { color: var(--muted); margin-right: 12px; }
.memview .byte { padding: 0 3px; border-radius: 3px; cursor: pointer; }
.memview .byte:hover { background: #2c343c; }
.memview .ascii { margin-left: 12px; color: var(--muted); }
/* Markers: a coloured outline for PC/SP/HL, a fill for recent changes. */
.memview .changed, .mem-legend i.changed { background: #4a5a1c; color: var(--text); }
.memview .pc, .mem-legend i.pc { box-shadow: inset 0 0 0 1px var(--accent); }
.memview .sp, .mem-legend i.sp { box-shadow: inset 0 0 0 1px #7ab8ff; }
.memview .hl, .mem-legend i.hl { box-shadow: inset 0 0 0 1px #ffb86b; }
.memview .selected { background: var(--accent); color: var(--bg); }
.mem-legend {
  display: flex;
  flex-wrap: wrap;
  justify-content: space-between;
  gap: 4px 16px;
  font-size: 13px;
}
#mem-info { font-variant-numeric: tabular-nums; }
.mem-legend i {
  display: inline-block;
  width: 10px;
  height: 10px;
  margin: 0 4px 0 8px;
  border-radius: 2px;
  vertical-align: -1px;
}

/* Collapsible sections inside the debugger */
.debug-section { margin-top: 16px; padding: 0; background: none; }
.debug-section > summary { margin-bottom: 8px; }

/* VRAM viewer: tiles (2:3) beside the map (1:1), equal heights */
.vram { display: grid; grid-template-columns: 2fr 3fr; gap: 16px; align-items: end; }
@media (max-width: 480px) { .vram { grid-template-columns: 1fr; } }
.vram figure { margin: 0; display: grid; gap: 6px; min-width: 0; }
.vram figcaption { font-size: 13px; display: flex; flex-wrap: wrap; align-items: center; gap: 4px 12px; }
.vram canvas { width: 100%; height: auto; background: var(--lcd); cursor: crosshair; }
#vram-tiles { aspect-ratio: 128 / 192; }
#vram-map-canvas { aspect-ratio: 1; }
.vram select {
  font: inherit;
  padding: 3px 6px;
  color: var(--text);
  background: var(--bg);
  border: 1px solid #3a444e;
  border-radius: 6px;
}
.vram-key i {
  display: inline-block;
  width: 10px;
  height: 10px;
  margin-right: 4px;
  border: 2px solid;
  vertical-align: -1px;
}
.vram-key i.screen { border-color: rgb(255, 64, 96); }
.vram-key i.window { border-color: rgb(64, 128, 255); margin-left: 8px; }
.vram-info { margin-top: 8px; font-size: 13px; font-variant-numeric: tabular-nums; min-height: 1.5em; }

/* Save state slots */
.states-panel summary .muted { margin-left: 8px; font-size: 13px; }
.error-text { color: var(--error) !important; }
.slots {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 12px;
  margin-top: 10px;
}
@media (max-width: 480px) { .slots { grid-template-columns: repeat(2, 1fr); } }
.slot {
  margin: 0;
  padding: 8px;
  display: grid;
  gap: 6px;
  min-width: 0;
  border: 1px solid #2c343c;
  border-radius: 8px;
  cursor: pointer;
}
.slot.selected { border-color: var(--accent); box-shadow: 0 0 0 1px var(--accent); }
.slot img {
  display: block;
  width: 100%;
  height: auto;
  aspect-ratio: 160 / 144;
  image-rendering: pixelated;
  background: var(--bg);
  border-radius: 2px;
}
.slot:not(.empty) img { background: var(--lcd); }
.slot figcaption { font-size: 12px; color: var(--muted); font-variant-numeric: tabular-nums; }
.slot div { display: flex; gap: 6px; }
.slot button {
  flex: 1;
  font: inherit;
  font-size: 12px;
  padding: 4px 0;
  color: var(--text);
  background: var(--panel);
  border: 1px solid #3a444e;
  border-radius: 6px;
  cursor: pointer;
}
.slot button:hover:not(:disabled) { border-color: var(--accent); }
.slot button:disabled { opacity: 0.4; cursor: default; }

/* Console and palettes */
.console-panel summary .muted { margin-left: 8px; font-size: 13px; }
.console-panel .toolbar { margin-top: 10px; }
.panel-help { margin: 8px 0 0; font-size: 13px; }
.panel-heading { margin: 16px 0 0; font-size: 14px; font-weight: 600; color: var(--text); }
.palettes {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(96px, 1fr));
  gap: 8px;
  margin-top: 10px;
}
.palettes button {
  display: grid;
  gap: 6px;
  padding: 6px;
  font: inherit;
  font-size: 12px;
  color: var(--text);
  background: var(--panel);
  border: 1px solid #3a444e;
  border-radius: 6px;
  cursor: pointer;
}
.palettes button:hover { border-color: var(--accent); }
.palettes button[aria-pressed="true"] { border-color: var(--accent); box-shadow: 0 0 0 1px var(--accent); }
/* The background's 4 colors as a row of tall swatches, the sprites' 8 below. */
.swatches { display: grid; grid-template-columns: repeat(8, 1fr); border-radius: 3px; overflow: hidden; }
.swatches i { height: 18px; grid-column: span 2; }
.swatches i.obj { height: 8px; grid-column: span 1; }

/* Link cable */
.link-panel summary .muted { margin-left: 8px; font-size: 13px; }
.link-panel .toolbar { margin-top: 10px; }
.link-help { margin: 8px 0 0; font-size: 13px; }
.link-heading { margin: 16px 0 0; font-size: 14px; font-weight: 600; color: var(--text); }
.link-steps { display: grid; gap: 10px; margin-top: 10px; }
.link-steps label { display: block; font-size: 13px; margin-bottom: 4px; }
.link-code { display: flex; gap: 8px; align-items: stretch; }
.link-code textarea {
  flex: 1;
  min-width: 0;
  font: 12px/1.4 ui-monospace, monospace;
  padding: 6px 8px;
  color: var(--text);
  background: var(--bg);
  border: 1px solid #3a444e;
  border-radius: 6px;
  resize: vertical;
  word-break: break-all;
}
.link-code button {
  font: inherit;
  font-size: 13px;
  padding: 6px 12px;
  color: var(--text);
  background: var(--panel);
  border: 1px solid #3a444e;
  border-radius: 6px;
  cursor: pointer;
}
.link-code button:hover:not(:disabled) { border-color: var(--accent); }
.link-code button:disabled { opacity: 0.4; cursor: default; }

/* "try Link Pong" in the drop hint: a button that looks like the link beside it. */
.link-button {
  font: inherit;
  color: inherit;
  background: none;
  border: none;
  padding: 0;
  cursor: pointer;
}
