:root {
  --bg: #0f1020; --panel: #181a2f; --line: #2b2e4a; --text: #e8e8f0; --muted: #8b8fa8; --accent: #f9c74f; --danger: #e63946;
  --font: "Press Start 2P", "Courier New", monospace;
}
* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; background: var(--bg); color: var(--text); font-family: system-ui, -apple-system, Segoe UI, Roboto, sans-serif; }
body { display: flex; flex-direction: column; min-height: 100vh; }
header { display: flex; align-items: center; justify-content: space-between; padding: 10px 16px; border-bottom: 1px solid var(--line); background: var(--panel); }
.logo { font-family: var(--font); font-size: 12px; color: var(--accent); letter-spacing: 1px; text-decoration: none; }
.logo:hover { color: #fff; }
nav { display: flex; gap: 8px; }
button { background: #24284a; color: var(--text); border: 1px solid var(--line); padding: 8px 12px; border-radius: 6px; cursor: pointer; font-size: 13px; }
button:hover { background: #2f3460; border-color: #3d4278; }
button.primary { background: var(--accent); color: #111; border-color: var(--accent); font-weight: 700; }
main { flex: 1; display: grid; grid-template-columns: 1fr 300px; gap: 0; min-height: 0; }
#stage { display: flex; align-items: center; justify-content: center; padding: 16px; background: radial-gradient(ellipse at center, #16183a 0%, var(--bg) 70%); min-width: 0; }
#canvas-wrap { position: relative; box-shadow: 0 0 0 4px #000, 0 0 0 6px #2b2e4a, 0 20px 60px rgba(0,0,0,0.6); border-radius: 4px; background: #000; line-height: 0; }
canvas { image-rendering: pixelated; image-rendering: crisp-edges; display: block; }
#side { border-left: 1px solid var(--line); background: var(--panel); padding: 16px; overflow: auto; }
#side h1 { font-family: var(--font); font-size: 14px; margin: 0 0 8px; color: var(--accent); line-height: 1.5; }
#side h3 { font-size: 12px; text-transform: uppercase; letter-spacing: 1px; color: var(--muted); margin: 18px 0 6px; }
#side p { margin: 6px 0; font-size: 14px; line-height: 1.45; }
.small { font-size: 12px !important; color: #c9cbe0; }
.muted { color: var(--muted); }
.row { display: flex; gap: 8px; flex-wrap: wrap; margin: 10px 0; }
.warn { font-size: 12px; color: #ffcf6e; white-space: pre-wrap; }
.warn:empty { display: none; }
.overlay { position: fixed; inset: 0; background: rgba(5,6,16,0.82); display: flex; align-items: center; justify-content: center; z-index: 50; padding: 20px; }
.overlay.hidden { display: none; }
.hidden { display: none !important; }
.panel { background: var(--panel); border: 1px solid var(--line); border-radius: 10px; padding: 20px; width: min(780px, 100%); max-height: 90vh; overflow: auto; box-shadow: 0 30px 80px rgba(0,0,0,0.6); }
.panel.wide { width: min(1000px, 100%); }
.panel h2 { font-family: var(--font); font-size: 13px; margin: 0 0 14px; color: var(--accent); }
.cards { display: grid; grid-template-columns: repeat(auto-fill, minmax(210px, 1fr)); gap: 12px; margin-bottom: 14px; }
.card { background: #20233f; border: 1px solid var(--line); border-radius: 8px; padding: 14px; cursor: pointer; transition: transform .08s, border-color .08s; }
.card:hover { transform: translateY(-2px); border-color: var(--accent); }
.card .t { font-family: var(--font); font-size: 10px; color: #fff; margin-bottom: 8px; line-height: 1.5; }
.card .m { display: inline-block; font-size: 10px; text-transform: uppercase; letter-spacing: 1px; padding: 2px 6px; border-radius: 4px; background: #2f3460; color: #cbd0ff; margin-bottom: 8px; }
.card .b { font-size: 12px; color: #c9cbe0; line-height: 1.4; }
textarea#json { width: 100%; height: 52vh; background: #0b0c18; color: #d6e2ff; border: 1px solid var(--line); border-radius: 6px; padding: 10px; font: 12px/1.45 ui-monospace, Menlo, Consolas, monospace; resize: vertical; }
.errors { margin: 10px 0 0; padding: 10px; background: #2a1420; color: #ff9aa5; border: 1px solid #5a2030; border-radius: 6px; font-size: 12px; white-space: pre-wrap; }
.errors:empty { display: none; }
input[type=file] { color: var(--muted); font-size: 12px; }
code { background: #0b0c18; padding: 1px 5px; border-radius: 4px; }
/* ---- virtual gamepad (touch devices) */
#stage { flex-direction: column; gap: 12px; }
#pad { display: flex; align-items: center; justify-content: space-between; width: min(100%, 560px); padding: 4px 6px; touch-action: none; user-select: none; -webkit-user-select: none; -webkit-touch-callout: none; }
#pad .dpad { display: grid; grid-template-columns: repeat(3, 58px); grid-template-rows: repeat(3, 58px); gap: 4px; }
#pad .pk { -webkit-tap-highlight-color: transparent; border-radius: 12px; border: 1px solid #3d4278; background: linear-gradient(#2c3160, #1d2040); color: #fff; font: 700 18px/1 var(--font); padding: 0; display: flex; align-items: center; justify-content: center; box-shadow: 0 4px 0 #0a0b18; touch-action: none; }
#pad .pk.on, #pad .pk:active { transform: translateY(3px); box-shadow: 0 1px 0 #0a0b18; background: linear-gradient(#3f4a9a, #2a2f66); border-color: var(--accent); }
#pad .dpad .up { grid-column: 2; grid-row: 1; }
#pad .dpad .left { grid-column: 1; grid-row: 2; }
#pad .dpad .right { grid-column: 3; grid-row: 2; }
#pad .dpad .down { grid-column: 2; grid-row: 3; }
#pad.lr .dpad { grid-template-rows: 58px; }
#pad.lr .dpad .left, #pad.lr .dpad .right { grid-row: 1; }
#pad.lr .dpad .left { grid-column: 1; } #pad.lr .dpad .right { grid-column: 3; }
#pad.no-dpad .dpad { display: none; }
#pad .mid { display: flex; flex-direction: column; gap: 10px; }
#pad .mid .sys { width: 44px; height: 34px; font-size: 12px; border-radius: 8px; color: var(--muted); }
#pad .abtns { display: flex; align-items: flex-end; gap: 14px; }
#pad .abtns .pk { width: 76px; height: 76px; border-radius: 50%; font-size: 9px; letter-spacing: 1px; }
#pad .abtns .a { background: linear-gradient(#ffd66b, #e0a52a); color: #2a1c00; border-color: #ffe08a; box-shadow: 0 5px 0 #8a5f00; margin-bottom: 14px; }
#pad .abtns .a.on { box-shadow: 0 1px 0 #8a5f00; background: linear-gradient(#ffe38f, #f3b73d); }
#pad .abtns .b { background: linear-gradient(#ff7b8c, #d63d55); color: #2a0008; border-color: #ff9aa8; box-shadow: 0 5px 0 #7a1020; }
#pad .abtns .b.on { box-shadow: 0 1px 0 #7a1020; background: linear-gradient(#ff9aa8, #e2536a); }
#pad .pk span { pointer-events: none; }
#touch-hint { margin: 0; text-align: center; }
body:not(.touch) #pad, body:not(.touch) #touch-hint { display: none !important; }

#pad .pk.land-only { display: none; }

@media (max-width: 860px) {
  main { grid-template-columns: 1fr; grid-template-rows: auto auto; }
  /* portrait phone: game + pad fill the first screen, description scrolls in below */
  #stage { padding: 8px; align-items: center; justify-content: flex-start; min-height: calc(100dvh - var(--hh, 48px)); }
  #pad { margin-top: auto; margin-bottom: 10px; }
  #side::before { content: "▲ swipe up for controls & info"; display: block; text-align: center; font-size: 11px; color: var(--muted); margin-bottom: 10px; letter-spacing: 1px; text-transform: uppercase; }
  #side { border-left: 0; border-top: 1px solid var(--line); }
  #side h3:nth-of-type(2), #side h3:nth-of-type(2) + p, #side h3:nth-of-type(3), #side h3:nth-of-type(3) + p { display: none; }
  header { padding: 8px 12px; }
  header nav button { padding: 7px 10px; font-size: 12px; }
  #btn-guide { display: none; }
  .logo { font-size: 10px; }
  #pad .dpad { grid-template-columns: repeat(3, 52px); grid-template-rows: repeat(3, 52px); }
  #pad.lr .dpad { grid-template-rows: 52px; }
  #pad .abtns .pk { width: 68px; height: 68px; }
  .cards { grid-template-columns: 1fr 1fr; gap: 8px; }
  .card { padding: 10px; }
  textarea#json { height: 40vh; }
}
/* landscape phone: header hidden, canvas fills the height, translucent pad floats over the sides */
body.landscape header { display: none; }
body.landscape main { grid-template-columns: 1fr; grid-template-rows: auto auto; }
body.landscape #stage { position: relative; height: 100dvh; min-height: 100dvh; padding: 0; justify-content: center; }
body.landscape #canvas-wrap { box-shadow: none; border-radius: 0; }
body.landscape #pad { position: absolute; inset: 0; width: auto; padding: 0; pointer-events: none; display: block; }
body.landscape #pad .dpad { position: absolute; left: 12px; bottom: 12px; pointer-events: auto; }
body.landscape #pad .abtns { position: absolute; right: 12px; bottom: 16px; pointer-events: auto; }
body.landscape #pad .mid { position: absolute; right: 12px; top: 8px; flex-direction: row; gap: 8px; pointer-events: auto; }
body.landscape #pad .pk.land-only { display: flex; }
body.landscape #pad .pk { opacity: 0.55; }
body.landscape #pad .pk.on { opacity: 0.95; }
body.landscape #pad .mid .sys { width: 40px; height: 32px; }
body.landscape #pad.no-dpad .dpad { display: none; }
body.landscape #touch-hint { display: none !important; }
body.landscape #side::before { content: "▲ rotate back or swipe up for info"; }

@media (max-width: 400px) {
  #pad .dpad { grid-template-columns: repeat(3, 46px); grid-template-rows: repeat(3, 46px); }
  #pad.lr .dpad { grid-template-rows: 46px; }
  #pad .abtns .pk { width: 60px; height: 60px; }
  #pad .mid .sys { width: 38px; }
}
