:root {
  --bg: #120e22;
  --panel: #1b1635;
  --panel2: #241d46;
  --line: #3b3270;
  --text: #ece8ff;
  --muted: #9a93c4;
  --accent: #ffcf4a;
  --danger: #ff5d6c;
  --good: #63c74d;
  --mono: ui-monospace, "SF Mono", "Cascadia Mono", Menlo, Consolas, "Courier New", monospace;
}
* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body {
  background: var(--bg); color: var(--text); font-family: var(--mono); font-size: 13px; font-weight: 700;
  letter-spacing: .03em; overflow: hidden; user-select: none; -webkit-user-select: none;
}
button, select, input { font: inherit; letter-spacing: inherit; color: inherit; }
#app { display: flex; flex-direction: column; height: 100%; }

/* ------------------------------- top bar ------------------------------- */
#topbar {
  display: flex; align-items: center; gap: 8px; padding: 8px 12px; background: var(--panel);
  border-bottom: 3px solid var(--line); flex: none;
}
.brand { font-size: 20px; color: #fff; text-shadow: 2px 2px 0 #a22633; margin-right: 10px; letter-spacing: .12em; }
.brand span { color: var(--accent); text-shadow: 2px 2px 0 #8a5a00; }
.spacer { flex: 1; }
#modebar { display: flex; gap: 6px; }

button {
  background: var(--panel2); border: 2px solid var(--line); border-radius: 3px; padding: 6px 10px; cursor: pointer;
  text-transform: uppercase; font-size: 12px; box-shadow: 0 3px 0 #0b0818; transition: background .08s;
}
button:hover { background: #31285e; border-color: #5a4ca8; }
button:active { transform: translateY(2px); box-shadow: 0 1px 0 #0b0818; }
button:focus-visible, select:focus-visible, input:focus-visible { outline: 2px solid var(--accent); outline-offset: 1px; }
button.primary { background: var(--accent); color: #2a1b00; border-color: #fff0a8; }
button.primary:hover { background: #ffdc7a; }
button.danger:hover { background: #5a1f35; border-color: var(--danger); }
button.toggle.off { opacity: .55; text-decoration: line-through; }
button:disabled { opacity: .4; cursor: default; }

/* ------------------------------- layout ------------------------------- */
#main { flex: 1; display: flex; min-height: 0; }
#stageCol { flex: 1; display: flex; flex-direction: column; min-width: 0; padding: 10px 12px 6px; gap: 6px; }
#stageWrap { flex: 1; min-height: 0; display: flex; align-items: center; justify-content: center; }
#stage { position: relative; width: 960px; height: 576px; background: #000; box-shadow: 0 0 0 4px var(--line), 0 10px 30px rgba(0,0,0,.6); }
#screen { position: absolute; inset: 0; width: 100%; height: 100%; image-rendering: pixelated; cursor: default; display: block; }
#miniWrap { flex: none; display: none; }
#minimap { width: 100%; height: 44px; display: block; image-rendering: pixelated; background: #000; border: 2px solid var(--line); cursor: ew-resize; }
#status { flex: none; min-height: 18px; color: var(--muted); font-size: 11px; text-align: center; text-transform: uppercase; }
#status b { color: var(--accent); }
#status .warn { color: var(--danger); }

#panel {
  width: 292px; flex: none; background: var(--panel); border-left: 3px solid var(--line); padding: 10px 12px 20px;
  overflow-y: auto; display: none;
}
#panel section { margin-bottom: 14px; }
#panel h3 { margin: 0 0 8px; font-size: 11px; color: var(--accent); letter-spacing: .18em; text-transform: uppercase; border-bottom: 2px solid var(--line); padding-bottom: 4px; }
.row { display: flex; gap: 6px; margin-top: 6px; align-items: center; }
.row button { flex: 1; padding: 6px 4px; }
.width-row span { color: var(--muted); font-size: 11px; text-transform: uppercase; }
.width-row b { min-width: 30px; text-align: center; color: var(--accent); }
.width-row button { flex: none; padding: 4px 6px; }
.field { display: flex; align-items: center; gap: 8px; margin-bottom: 6px; color: var(--muted); font-size: 11px; text-transform: uppercase; }
.field input, .field select { flex: 1; min-width: 0; background: #0f0b20; border: 2px solid var(--line); border-radius: 3px; padding: 5px 6px; color: var(--text); text-transform: none; }
.check { display: flex; gap: 6px; align-items: center; margin-top: 8px; color: var(--muted); font-size: 11px; cursor: pointer; }
kbd { background: #0f0b20; border: 1px solid var(--line); border-radius: 3px; padding: 0 4px; font-size: 10px; color: var(--text); }
.hint { margin: 8px 0 0; color: var(--muted); font-size: 11px; line-height: 1.4; min-height: 30px; font-weight: 400; }

.pal { display: grid; grid-template-columns: repeat(4, 1fr); gap: 6px; }
.pal button { position: relative; padding: 4px; height: 52px; display: flex; align-items: center; justify-content: center; flex-direction: column; gap: 2px; }
.pal button canvas { width: 32px; height: 32px; image-rendering: pixelated; }
.pal button .k { position: absolute; right: 3px; bottom: 1px; font-size: 9px; color: var(--muted); }
.pal button .t { font-size: 10px; }
.pal button.sel { background: #3a2f78; border-color: var(--accent); box-shadow: 0 3px 0 #0b0818, 0 0 0 2px rgba(255,207,74,.35); }
#tools button { height: 40px; font-size: 10px; }

/* ----------------------------- overlay menus ----------------------------- */
#overlay { position: absolute; inset: 0; display: none; align-items: center; justify-content: center; flex-direction: column; font-size: calc(var(--s, 2) * 6px); }
#overlay.show { display: flex; }
.menu { display: flex; flex-direction: column; gap: calc(var(--s, 2) * 4px); align-items: stretch; min-width: calc(var(--s, 2) * 110px); }
.menu button {
  font-size: calc(var(--s, 2) * 6.5px); padding: calc(var(--s, 2) * 3px) calc(var(--s, 2) * 8px); border-width: calc(var(--s, 2) * 1px);
  box-shadow: 0 calc(var(--s, 2) * 2px) 0 #0b0818; letter-spacing: .1em;
}
.menu button.primary { box-shadow: 0 calc(var(--s, 2) * 2px) 0 #8a5a00; }
.panelbox {
  background: rgba(18,14,34,.93); border: calc(var(--s, 2) * 1.5px) solid var(--line); box-shadow: 0 0 0 calc(var(--s, 2) * 1px) #000;
  padding: calc(var(--s, 2) * 8px) calc(var(--s, 2) * 12px); text-align: center; display: flex; flex-direction: column; gap: calc(var(--s, 2) * 5px);
}
.panelbox h2 { margin: 0; font-size: calc(var(--s, 2) * 11px); color: var(--accent); letter-spacing: .15em; text-shadow: calc(var(--s, 2) * 1px) calc(var(--s, 2) * 1px) 0 #8a5a00; }
.panelbox p { margin: 0; font-size: calc(var(--s, 2) * 6px); color: var(--text); line-height: 1.5; font-weight: 400; }
.stats { display: grid; grid-template-columns: auto auto; gap: calc(var(--s, 2) * 1px) calc(var(--s, 2) * 14px); text-align: left; font-size: calc(var(--s, 2) * 6px); justify-content: center; }
.stats b { color: var(--accent); text-align: right; }
.stats .new { color: var(--good); }
#overlay.title { justify-content: flex-end; padding-bottom: calc(var(--s, 2) * 12px); }
.tinyhelp { margin-top: calc(var(--s, 2) * 3px); font-size: calc(var(--s, 2) * 4.2px); color: #fff; text-shadow: 1px 1px 0 #000; font-weight: 400; text-align: center; line-height: 1.6; }
.tinyhelp kbd { font-size: inherit; }

#toast {
  position: absolute; left: 50%; bottom: 8%; transform: translateX(-50%); background: rgba(10,6,22,.92); border: 2px solid var(--accent);
  padding: 6px 12px; color: #fff; font-size: 12px; text-transform: uppercase; opacity: 0; transition: opacity .18s; pointer-events: none; white-space: nowrap; z-index: 5;
}
#toast.show { opacity: 1; }
#toast.err { border-color: var(--danger); }

/* ------------------------------- modals ------------------------------- */
#modal { position: fixed; inset: 0; background: rgba(5,3,14,.72); display: flex; align-items: center; justify-content: center; z-index: 50; }
#modal[hidden] { display: none; }
.dialog { background: var(--panel); border: 3px solid var(--line); box-shadow: 0 0 0 3px #000, 0 20px 50px rgba(0,0,0,.7); padding: 16px 18px; width: min(560px, 94vw); max-height: 90vh; display: flex; flex-direction: column; gap: 10px; }
.dialog h2 { margin: 0; font-size: 16px; color: var(--accent); letter-spacing: .15em; text-shadow: 2px 2px 0 #8a5a00; }
.dialog h4 { margin: 8px 0 2px; font-size: 11px; color: var(--muted); letter-spacing: .18em; text-transform: uppercase; }
.dialog p { margin: 0; font-weight: 400; line-height: 1.5; color: var(--text); }
.dialog input[type=text], .dialog textarea { width: 100%; background: #0f0b20; border: 2px solid var(--line); border-radius: 3px; padding: 8px; color: var(--text); user-select: text; -webkit-user-select: text; }
.dialog textarea { height: 180px; font-size: 11px; resize: vertical; }
.dialog .btns { display: flex; gap: 8px; justify-content: flex-end; flex-wrap: wrap; }
.list { overflow-y: auto; display: flex; flex-direction: column; gap: 6px; padding-right: 4px; max-height: 52vh; }
.item { display: flex; align-items: center; gap: 8px; background: var(--panel2); border: 2px solid var(--line); padding: 6px 8px; }
.item .nm { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.item .nm small { display: block; color: var(--muted); font-weight: 400; font-size: 10px; text-transform: uppercase; }
.item button { padding: 4px 8px; font-size: 11px; }
.empty { color: var(--muted); font-weight: 400; padding: 8px 2px; }

@media (max-width: 980px) {
  #main { flex-direction: column; overflow-y: auto; }
  #panel { width: auto; border-left: 0; border-top: 3px solid var(--line); }
  body { overflow: auto; }
  #app { height: auto; min-height: 100%; }
  #stageWrap { min-height: 200px; }
}

/* ------------------------------- modes ------------------------------- */
.mode-editor #panel { display: block; }
.mode-editor #miniWrap { display: block; }
.mode-editor #status { min-height: 32px; }
.mode-editor #screen { cursor: crosshair; }
