:root {
  --bg: #111418;
  --fg: #e6e8eb;
  --muted: #8b949e;
  --panel: #1a1f25;
  --line: #2c333b;
  --accent: #3fae5a;
  --accent-fg: #06120a;
  --warn: #e0a030;
  --err: #e05050;
  color-scheme: dark;
}
@media (prefers-color-scheme: light) {
  :root {
    --bg: #f4f5f7;
    --fg: #1b1f24;
    --muted: #5d6670;
    --panel: #ffffff;
    --line: #d5d9de;
    --accent: #2c8a44;
    --accent-fg: #ffffff;
    color-scheme: light;
  }
}

* { box-sizing: border-box; }
html, body { margin: 0; }
body {
  background: var(--bg);
  color: var(--fg);
  font: 14px/1.4 system-ui, -apple-system, "Segoe UI", sans-serif;
  min-height: 100vh;
  display: flex;
  flex-direction: column;
}
code { font-family: ui-monospace, Menlo, monospace; font-size: 0.92em; }
.muted { color: var(--muted); }

.bar {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 8px 16px;
  border-bottom: 1px solid var(--line);
}
.bar h1 { font-size: 16px; margin: 0; letter-spacing: 0.02em; }
.status { color: var(--muted); font-variant-numeric: tabular-nums; }
.status.err { color: var(--err); }
.spacer { flex: 1; }

button, .btn, select, input[type="text"] {
  font: inherit;
  color: var(--fg);
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 6px;
  padding: 5px 10px;
}
button, .btn { cursor: pointer; }
button:hover:not(:disabled), .btn:hover { border-color: var(--accent); }
button:disabled { opacity: 0.45; cursor: default; }
button[type="submit"], .file-btn:first-of-type { background: var(--accent); color: var(--accent-fg); border-color: var(--accent); }
#lbl-import { background: var(--panel); color: var(--fg); border-color: var(--line); }
#lbl-import.disabled { opacity: 0.45; pointer-events: none; }
input[type="color"] { width: 44px; height: 28px; padding: 0; border: 1px solid var(--line); border-radius: 6px; background: none; vertical-align: middle; }

.layout {
  flex: 1;
  display: grid;
  grid-template-columns: minmax(0, 1fr) 300px;
  gap: 16px;
  padding: 16px;
  align-items: start;
}
@media (max-width: 900px) {
  .layout { grid-template-columns: minmax(0, 1fr); padding: 8px 0; }
  .panel { margin: 0 16px; }
  .controls, .help { padding: 0 16px; }
}

.stage-wrap { min-width: 0; }
.stage {
  position: relative;
  background: #000;
  border-radius: 6px;
  overflow: hidden;
  display: flex;
  align-items: center;
  justify-content: center;
  aspect-ratio: 256 / 224;
  max-height: calc(100vh - 180px);
  margin: 0 auto;
}
.stage.fixed { aspect-ratio: auto; max-height: none; }
#screen {
  display: block;
  width: 100%;
  height: 100%;
  object-fit: contain;
  image-rendering: pixelated;
  image-rendering: crisp-edges;
}
.stage.fixed #screen { object-fit: fill; }
.stage:fullscreen { max-height: none; width: 100vw; height: 100vh; aspect-ratio: auto; border-radius: 0; }
.stage.dragging { outline: 3px dashed var(--accent); outline-offset: -8px; }

.drop {
  position: absolute;
  inset: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  text-align: center;
  padding: 24px;
  color: #d8dde2;
  background: radial-gradient(circle at 50% 40%, #1c2a20 0%, #07090b 70%);
}
.drop p { max-width: 42ch; margin: 6px 0; }
.drop .muted { color: #8b949e; }
.drop-title { font-size: 20px; font-weight: 600; }
.drop[hidden] { display: none; }

.paused-badge {
  position: absolute;
  top: 10px;
  right: 10px;
  background: rgba(0, 0, 0, 0.7);
  color: #fff;
  font-weight: 700;
  letter-spacing: 0.1em;
  padding: 3px 8px;
  border-radius: 4px;
}
.paused-badge[hidden] { display: none; }

.controls {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 8px;
  margin-top: 10px;
}
.controls .sep { width: 1px; height: 22px; background: var(--line); }
.help { margin-top: 10px; }
.help summary { cursor: pointer; color: var(--muted); }
.help label { display: inline-block; margin: 8px 12px 0 0; }
.sprite-row { display: flex; align-items: center; flex-wrap: wrap; gap: 10px; margin-top: 8px; }
.sprite-row label { margin: 0; }
.sprite-row #lbl-sprite { background: var(--panel); color: var(--fg); border-color: var(--line); }
.sprite-preview {
  width: 48px;
  height: 72px;
  image-rendering: pixelated;
  image-rendering: crisp-edges;
  background: rgba(127, 127, 127, 0.15);
  border: 1px solid var(--line);
  border-radius: 4px;
}
.sprite-preview[hidden] { display: none; }
.keys { border-collapse: collapse; margin-top: 8px; }
.keys th, .keys td { text-align: left; padding: 3px 14px 3px 0; border-bottom: 1px solid var(--line); }
.keys th { color: var(--muted); font-weight: 500; }

.panel {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 14px;
}
.panel h2 { font-size: 15px; margin: 0 0 10px; }
.panel h3 { font-size: 13px; margin: 14px 0 6px; color: var(--muted); font-weight: 500; }
.panel form > label, .panel details label { display: block; margin-bottom: 8px; color: var(--muted); }
.panel input[type="text"] { display: block; width: 100%; margin-top: 3px; background: var(--bg); }
.panel .row { display: flex; align-items: center; gap: 12px; flex-wrap: wrap; margin-bottom: 8px; }
.panel .row label { color: var(--fg); }
.panel details { margin-bottom: 8px; }
.panel details summary { cursor: pointer; color: var(--muted); margin-bottom: 6px; }
.panel details p { font-size: 12px; margin: 0 0 6px; }
.mp-stage { margin: 8px 0 0; color: var(--muted); }
#mp-stage { color: var(--fg); }
#mp-stage.ok { color: var(--accent); }
#mp-stage.err { color: var(--err); }
#mp-stage.wait { color: var(--warn); }
.players { list-style: none; padding: 0; margin: 0; }
.players li { display: flex; align-items: center; gap: 8px; padding: 4px 0; border-bottom: 1px solid var(--line); }
.players .swatch { width: 12px; height: 12px; border-radius: 3px; border: 1px solid rgba(255, 255, 255, 0.3); }
.players .here { margin-left: auto; font-size: 12px; color: var(--muted); }

.chat-log {
  list-style: none;
  padding: 6px 8px;
  margin: 0;
  max-height: 180px;
  overflow-y: auto;
  background: var(--bg);
  border: 1px solid var(--line);
  border-radius: 6px;
  font-size: 13px;
  overflow-wrap: anywhere;
}
.chat-log li { padding: 2px 0; }
.chat-log .who { font-weight: 600; }
.chat-log .sys { color: var(--muted); font-style: italic; }
.chat-form { display: flex; gap: 6px; margin-top: 6px; }
.chat-form input[type="text"] { flex: 1; min-width: 0; margin: 0; }

.toasts {
  position: fixed;
  left: 50%;
  bottom: 20px;
  transform: translateX(-50%);
  display: flex;
  flex-direction: column;
  gap: 6px;
  z-index: 10;
  pointer-events: none;
  max-width: calc(100vw - 32px);
}
.toast {
  background: rgba(20, 24, 28, 0.94);
  color: #fff;
  border: 1px solid var(--line);
  border-left: 3px solid var(--accent);
  padding: 8px 14px;
  border-radius: 6px;
  box-shadow: 0 4px 16px rgba(0, 0, 0, 0.35);
  animation: toast-in 0.18s ease-out;
}
.toast.err { border-left-color: var(--err); }
@keyframes toast-in { from { opacity: 0; transform: translateY(6px); } }

.foot { padding: 8px 16px 14px; font-size: 12px; }

/* Touch controls (coarse pointers only). */
.touch {
  position: fixed;
  left: 0;
  right: 0;
  bottom: 0;
  display: flex;
  justify-content: space-between;
  align-items: flex-end;
  padding: 12px 16px calc(12px + env(safe-area-inset-bottom));
  pointer-events: none;
  z-index: 5;
  user-select: none;
  -webkit-user-select: none;
  touch-action: none;
}
.touch[hidden] { display: none; }
.touch button {
  pointer-events: auto;
  background: rgba(40, 46, 54, 0.55);
  color: #fff;
  border: 1px solid rgba(255, 255, 255, 0.25);
  touch-action: none;
}
.touch button.on { background: rgba(63, 174, 90, 0.7); }
.dpad { position: relative; width: 138px; height: 138px; }
.dpad button { position: absolute; width: 46px; height: 46px; padding: 0; border-radius: 8px; }
.t-up { left: 46px; top: 0; }
.t-down { left: 46px; bottom: 0; }
.t-left { left: 0; top: 46px; }
.t-right { right: 0; top: 46px; }
.mid { display: flex; flex-wrap: wrap; gap: 6px; justify-content: center; max-width: 150px; }
.mid button { font-size: 12px; padding: 6px 10px; border-radius: 14px; }
.face { position: relative; width: 150px; height: 150px; }
.face button { position: absolute; width: 50px; height: 50px; border-radius: 50%; padding: 0; font-weight: 700; }
.t-x { left: 50px; top: 0; }
.t-b { left: 50px; bottom: 0; }
.t-y { left: 0; top: 50px; }
.t-a { right: 0; top: 50px; }
body.has-touch .foot { padding-bottom: 180px; }

/* Dedicated-server mode hides the peer-room-only options. */
#mp-form.server-mode .peer-only { display: none; }
.scripts-head { margin-top: 22px !important; }
.light-row { display: flex; flex-wrap: wrap; gap: 10px; margin: 8px 0; align-items: center; }
#sc-list li.err .here { color: var(--err); }
