:root {
    color-scheme: dark;
    --bg: #101418;
    --card: #1b2229;
    --line: #2c3742;
    --text: #eef2f5;
    --muted: #9aa8b5;
    --accent: #e8452c;
    --accent-hover: #f25a42;
    --bar-height: 44px;
}

* { box-sizing: border-box; }

html, body {
    height: 100%;
    margin: 0;
    background: var(--bg);
    color: var(--text);
    font: 16px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
    overflow: hidden;
    overscroll-behavior: none;
    -webkit-text-size-adjust: 100%;
}

body {
    display: flex;
    flex-direction: column;
}

/* The game area: the canvas is letterboxed inside it at 4:3. */
#stage {
    position: relative;
    flex: 1 1 auto;
    min-height: 0;
    display: flex;
    align-items: center;
    justify-content: center;
    background: #000;
    container-type: size;
}

/* Both canvases are stacked in this frame, which carries the letterboxed size. */
#frame {
    position: relative;
    width: min(100cqw, 100cqh * 4 / 3);
    height: min(100cqh, 100cqw * 3 / 4);
    background: #000;
}

/* !important: the SDL runtime writes inline sizes on its canvas; ours must win. */
#frame canvas {
    position: absolute;
    inset: 0;
    display: block;
    width: 100% !important;
    height: 100% !important;
    padding: 0 !important;
    margin: 0 !important;
    outline: none;
    touch-action: none;
    -webkit-user-select: none;
    user-select: none;
    -webkit-tap-highlight-color: transparent;
}
#canvas { background: #000; }
/* Presentation layer: drawn on top, but input goes to #canvas underneath. */
#frame canvas.screen {
    z-index: 1;
    pointer-events: none;
}

#stage:fullscreen { width: 100vw; height: 100vh; }

#overlay {
    position: absolute;
    inset: 0;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 16px;
    background: var(--bg);
    overflow: auto;
}
#overlay[hidden] { display: none; }

.card {
    width: min(100%, 460px);
    margin: auto;
    padding: 28px 28px 24px;
    background: var(--card);
    border: 1px solid var(--line);
    border-radius: 14px;
}

.eyebrow {
    margin: 0 0 6px;
    color: var(--muted);
    font-size: 12px;
    letter-spacing: 0.08em;
    text-transform: uppercase;
}

h1 {
    margin: 0 0 8px;
    font-size: 26px;
    line-height: 1.15;
    text-wrap: balance;
}

.lede {
    margin: 0 0 18px;
    color: var(--muted);
    font-size: 14px;
}

.keys {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 8px 16px;
    margin: 0 0 18px;
    padding: 14px 0;
    border-block: 1px solid var(--line);
    font-size: 14px;
}
.keys div { display: flex; align-items: center; gap: 10px; min-width: 0; }
.keys dt { flex: 0 0 auto; min-width: 58px; }
.keys dd { margin: 0; color: var(--muted); }

kbd {
    display: inline-block;
    min-width: 24px;
    padding: 1px 6px;
    border: 1px solid var(--line);
    border-bottom-width: 2px;
    border-radius: 5px;
    background: #141a20;
    color: var(--text);
    font: 12px/1.6 ui-monospace, SFMono-Regular, Menlo, monospace;
    text-align: center;
}

button {
    font: inherit;
    color: inherit;
    cursor: pointer;
}

#play {
    width: 100%;
    padding: 13px 16px;
    border: 0;
    border-radius: 10px;
    background: var(--accent);
    color: #fff;
    font-size: 17px;
    font-weight: 600;
}
#play:hover { background: var(--accent-hover); }
#play:focus-visible, #bar button:focus-visible { outline: 2px solid #fff; outline-offset: 2px; }
#play[hidden] { display: none; }

.hint {
    margin: 10px 0 0;
    color: var(--muted);
    font-size: 13px;
    text-align: center;
}

progress {
    display: block;
    width: 100%;
    height: 10px;
    appearance: none;
    border: 0;
    border-radius: 5px;
    background: #141a20;
    overflow: hidden;
}
progress::-webkit-progress-bar { background: #141a20; }
progress::-webkit-progress-value { background: var(--accent); }
progress::-moz-progress-bar { background: var(--accent); }

.error {
    margin: 12px 0 0;
    padding: 10px 12px;
    border-radius: 8px;
    background: #3a1712;
    color: #ffb4a6;
    font-size: 14px;
    overflow-wrap: anywhere;
}

#bar {
    flex: 0 0 var(--bar-height);
    display: flex;
    align-items: center;
    justify-content: flex-end;
    gap: 8px;
    padding: 0 12px;
    border-top: 1px solid var(--line);
    background: var(--bg);
}
#bar[hidden] { display: none; }
#fps {
    margin-right: auto;
    color: var(--muted);
    font: 12px/1 ui-monospace, SFMono-Regular, Menlo, monospace;
    font-variant-numeric: tabular-nums;
}
#fps[hidden] { display: none; }
#bar button {
    padding: 6px 12px;
    border: 1px solid var(--line);
    border-radius: 8px;
    background: var(--card);
    font-size: 13px;
}
#bar button:hover { border-color: var(--muted); }

/* --- touch controls ----------------------------------------------------------- */
#touch {
    position: absolute;
    inset: 0;
    z-index: 5;
    pointer-events: none;          /* taps between the buttons still reach the game */
    -webkit-user-select: none;
    user-select: none;
    -webkit-touch-callout: none;
}
#touch[hidden] { display: none; }

.pad {
    position: absolute;
    display: grid;
    gap: 10px;
    pointer-events: auto;
    touch-action: none;
}
/* Up on top, left and right side by side, down underneath: narrow enough to sit in the
   letterbox beside the picture on a landscape phone. */
.pad-move {
    left: max(12px, env(safe-area-inset-left));
    bottom: max(14px, env(safe-area-inset-bottom));
    grid-template-columns: repeat(2, auto);
    justify-items: center;
}
.pad-move [data-key="ArrowUp"] { grid-row: 1; grid-column: 1 / span 2; }
.pad-move [data-key="ArrowLeft"] { grid-row: 2; grid-column: 1; }
.pad-move [data-key="ArrowRight"] { grid-row: 2; grid-column: 2; }
.pad-move [data-key="ArrowDown"] { grid-row: 3; grid-column: 1 / span 2; }
/* Jump is the big one under the thumb, throw beside it, switch tucked above. */
.pad-action {
    right: max(12px, env(safe-area-inset-right));
    bottom: max(14px, env(safe-area-inset-bottom));
    grid-template-columns: repeat(2, auto);
    align-items: end;
    justify-items: end;
}
.pad-action [data-key="d"] { grid-row: 1; grid-column: 2; justify-self: center; }
.pad-action [data-key="s"] { grid-row: 2; grid-column: 1; }
.pad-action [data-key="a"] { grid-row: 2; grid-column: 2; }
.pad-system {
    top: max(8px, env(safe-area-inset-top));
    right: max(10px, env(safe-area-inset-right));
    grid-auto-flow: column;
    gap: 8px;
}

.pad button {
    display: grid;
    place-items: center;
    width: 62px;
    height: 62px;
    padding: 0;
    border: 1.5px solid rgb(255 255 255 / 0.55);
    border-radius: 50%;
    background: rgb(16 20 24 / 0.45);
    color: #fff;
    font-size: 12px;
    font-weight: 600;
    letter-spacing: 0.02em;
    touch-action: none;
    -webkit-tap-highlight-color: transparent;
    backdrop-filter: blur(2px);
}
.pad button svg { width: 30px; height: 30px; fill: currentColor; pointer-events: none; }
.pad button span { pointer-events: none; }
.pad button.major { width: 78px; height: 78px; font-size: 14px; }
.pad button.minor { width: 54px; height: 54px; font-size: 11px; }
.pad button.chip {
    width: auto;
    height: 34px;
    padding: 0 12px;
    border-radius: 17px;
    font-size: 12px;
}
.pad button.chip svg { width: 18px; height: 18px; }
.pad button.held {
    background: rgb(232 69 44 / 0.85);
    border-color: #fff;
}

/* Portrait phones: picture at the top, controls in the free space underneath. */
@media (orientation: portrait) {
    body.touch #stage { align-items: flex-start; }
    body.touch .pad-move, body.touch .pad-action { bottom: max(48px, env(safe-area-inset-bottom)); }
    body.touch .pad-system { top: calc(min(100cqh, 100cqw * 3 / 4) + 12px); }
}
/* On touch devices the page behaves like an app, not a document: nothing can be
   selected, dragged or long-pressed into a menu or magnifier, and it does not zoom.
   (iOS ignores user-scalable=no; pinching is also cancelled in kfa.js.) */
body.touch, body.touch * {
    -webkit-user-select: none;
    user-select: none;
    -webkit-touch-callout: none;
    -webkit-tap-highlight-color: transparent;
    -webkit-user-drag: none;
}
body.touch { touch-action: manipulation; }           /* no double-tap zoom */
body.touch #overlay { touch-action: pan-y; }         /* the start card may still scroll */
/* An error message is worth being able to copy. */
body.touch .error, body.touch #output {
    -webkit-user-select: text;
    user-select: text;
}

/* The start card explains whichever input the device has. */
.touch-note {
    display: none;
    margin: 0 0 18px;
    padding: 14px 0;
    border-block: 1px solid var(--line);
    color: var(--muted);
    font-size: 14px;
}
body.touch .keys { display: none; }
body.touch .touch-note { display: block; }

/* The bottom bar only offers fullscreen, which moves into the touch layer. */
body.touch #bar { display: none; }

/* --- emulator panel, paused badge, typing bar ---------------------------------------- */
#panel {
    position: absolute;
    z-index: 8;
    right: max(10px, env(safe-area-inset-right));
    bottom: 10px;
    display: grid;
    gap: 10px;
    padding: 12px;
    border: 1px solid var(--line);
    border-radius: 12px;
    background: rgb(27 34 41 / 0.96);
    font-size: 14px;
}
#panel[hidden], #panel [hidden] { display: none; }
body.touch #panel { top: calc(max(8px, env(safe-area-inset-top)) + 44px); bottom: auto; }
#panel .emulator-only { display: grid; gap: 10px; }
#panel .row { display: flex; align-items: center; gap: 8px; }
#panel button, #typing button {
    min-height: 36px;
    padding: 0 12px;
    border: 1px solid var(--line);
    border-radius: 8px;
    background: #141a20;
    font-size: 14px;
    touch-action: manipulation;
}

#panel .states { display: flex; gap: 8px; }
#panel .state { display: grid; gap: 4px; }
#panel .state img {
    display: block;
    width: 96px;
    height: 72px;
    border: 1px solid var(--line);
    border-radius: 6px;
    background: #0d1115;
    object-fit: cover;
}
#panel .state span { display: flex; gap: 4px; }
#panel .state button { flex: 1 1 0; min-height: 30px; padding: 0 4px; font-size: 12px; }
#panel button:disabled { opacity: 0.4; }
#panel .note { margin: 0; color: var(--muted); font-size: 12px; }
#panel .cheat { display: flex; align-items: center; gap: 8px; }
#panel .cheat input { width: 16px; height: 16px; accent-color: var(--accent); }

#paused-badge {
    position: absolute;
    z-index: 6;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    margin: 0;
    padding: 10px 18px;
    border-radius: 10px;
    background: rgb(16 20 24 / 0.85);
    font-size: 18px;
    font-weight: 600;
    pointer-events: none;
}
#paused-badge[hidden] { display: none; }

#typing {
    position: absolute;
    z-index: 9;
    top: max(6px, env(safe-area-inset-top));
    left: 50%;
    transform: translateX(-50%);
    display: flex;
    gap: 6px;
    width: min(92%, 460px);
    padding: 6px;
    border: 1px solid var(--line);
    border-radius: 10px;
    background: rgb(27 34 41 / 0.96);
}
#typing[hidden] { display: none; }
#typing input {
    flex: 1 1 auto;
    min-width: 0;
    min-height: 36px;
    padding: 0 10px;
    border: 1px solid var(--line);
    border-radius: 8px;
    background: #0d1115;
    color: var(--text);
    font: 16px/1 system-ui, sans-serif;          /* 16px: iOS does not zoom into the field */
}
/* The text field stays an ordinary text field even on touch devices. */
body.touch #typing input {
    -webkit-user-select: text;
    user-select: text;
    -webkit-touch-callout: default;
}

/* Debug console (?debug) */
#output {
    flex: 0 0 180px;
    width: 100%;
    margin: 0;
    padding: 8px;
    border: 0;
    border-top: 1px solid var(--line);
    background: #000;
    color: #9fe6a0;
    font: 11px/1.4 ui-monospace, SFMono-Regular, Menlo, monospace;
    resize: none;
}
#output[hidden] { display: none; }

@media (max-width: 480px) {
    .card { padding: 20px 18px 18px; }
    h1 { font-size: 22px; }
    .keys { grid-template-columns: 1fr; }
}
