/* EKA2L1 web page. One layout for desktop, phones and tablets: the screen in the
   middle, touch controls beside it in landscape and below it in portrait. */
:root {
  color-scheme: dark;
  --bg: #111316; --panel: #1b1e23; --line: #2c3138; --text: #e6e8eb; --muted: #9aa1ab; --accent: #6cb4ff;
  --key: #262b33; --key-held: #3d6ea5;
  --unit: clamp(44px, 9.5vmin, 76px); /* touch target size */
}
* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body {
  background: var(--bg); color: var(--text); font: 15px/1.45 system-ui, sans-serif;
  display: flex; flex-direction: column; align-items: center; gap: 10px;
  padding: max(8px, env(safe-area-inset-top)) max(8px, env(safe-area-inset-right)) max(8px, env(safe-area-inset-bottom)) max(8px, env(safe-area-inset-left));
  overflow: hidden; overscroll-behavior: none;
  -webkit-user-select: none; user-select: none; -webkit-touch-callout: none;
}

#bar { width: 100%; max-width: 1100px; display: flex; align-items: center; gap: 10px; min-height: 36px; }
#status { flex: 1; min-width: 0; color: var(--muted); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
#status.error { color: #ff9c8a; white-space: normal; }
#actions { display: flex; gap: 6px; flex: none; }
#actions button { font: inherit; font-size: 13px; padding: 6px 10px; min-height: 32px; border-radius: 6px; border: 1px solid var(--line); background: var(--panel); color: var(--text); cursor: pointer; }
#actions button:disabled { opacity: .4; cursor: default; }
#actions button[aria-pressed="true"] { background: var(--key-held); border-color: var(--key-held); }
/* The multiplayer room this player is in: its code and how many are in it. */
#actions #net-chip { border-color: var(--accent); color: var(--accent); font-variant-numeric: tabular-nums; white-space: nowrap; }
#actions #net-chip.warn { border-color: #f2c66d; color: #f2c66d; }
#net-chip .narrow { display: none; }

#console { flex: 1; min-height: 0; width: 100%; display: flex; align-items: center; justify-content: center; gap: 12px; }
#stage { position: relative; flex: 1 1 auto; align-self: stretch; min-width: 0; min-height: 0; max-width: 900px; }
/* The canvas fills the stage and letterboxes itself, so the picture keeps its
   shape whatever space the layout leaves. */
canvas { display: block; width: 100%; height: 100%; object-fit: contain; image-rendering: pixelated; image-rendering: crisp-edges; outline: none; touch-action: none; }
/* Display → Fill the screen (beta). The stage takes the whole width, and the
   picture is either stretched over it or backed by #backdrop, a small blurred
   copy of the current frame (backdrop.js) that is enlarged to cover the stage.
   Everything stays inside the stage, so the touch keys beside it are never
   covered. */
#backdrop { display: none; }
#stage[data-fill] { max-width: none; }
#stage[data-fill="stretch"], #stage[data-fill="ambient"] { overflow: hidden; border-radius: 6px; }
/* Half a game pixel larger than the stage on every side: the emulator's canvas
   can carry a stray row or column at its very edge (seen white at some sizes),
   which stretching would enlarge into a visible line. */
#stage[data-fill="stretch"] #canvas { object-fit: fill; position: relative; left: -.3%; top: -.25%; width: 100.6%; height: 100.5%; }
#stage[data-fill="ambient"] #canvas { position: relative; }
#stage[data-fill="ambient"] #backdrop { display: block; position: absolute; inset: 0; object-fit: cover; image-rendering: auto; pointer-events: none; }
/* Where the canvas cannot blur its own picture (backdrop.js) it is blurred
   here, and made larger than the stage by the reach of the blur so that the
   blur's faded rim falls outside it. */
#stage[data-fill="ambient"] #backdrop.unblurred { --blur: clamp(14px, 3.5vmin, 36px); left: calc(var(--blur) * -3); top: calc(var(--blur) * -3);
  width: calc(100% + var(--blur) * 6); height: calc(100% + var(--blur) * 6); filter: blur(var(--blur)) saturate(1.25) brightness(.6); }
/* The loader covers the whole stage, picture and backdrop included, and keeps
   its contents to the usual width. */
#stage[data-fill] #setup { padding-left: max(16px, 50% - 434px); padding-right: max(16px, 50% - 434px); }
#overlay { position: absolute; inset: 0; border-radius: 6px; display: flex; align-items: center; justify-content: center; text-align: center; padding: 16px; background: rgba(0, 0, 0, .72); }
#overlay[hidden] { display: none; }

/* Touch controls */
.pad { display: flex; flex-direction: column; align-items: center; justify-content: center; gap: calc(var(--unit) * .3); flex: none; touch-action: none; }
.key { font: inherit; font-weight: 600; color: var(--text); background: var(--key); border: 1px solid var(--line); border-radius: 10px;
       min-width: var(--unit); min-height: var(--unit); padding: 0; touch-action: none; cursor: pointer; }
.key.held { background: var(--key-held); border-color: var(--key-held); }
.key.soft { min-height: calc(var(--unit) * .92); width: calc(var(--unit) * 1.5); }
.key.round { border-radius: 50%; }
.key.primary { background: #2f4f73; }
.pad-row { display: flex; gap: calc(var(--unit) * .3); }
.key b { display: block; font-size: 1.05em; line-height: 1.1; }
/* What the key does in the running game: two short lines at most, whatever the catalogue wrote. */
.key small { display: -webkit-box; -webkit-box-orient: vertical; -webkit-line-clamp: 2; line-clamp: 2; font-size: 9px; font-weight: 500; line-height: 1.05; color: var(--muted); max-width: var(--unit); overflow: hidden; overflow-wrap: anywhere; }
.key small:empty { display: none; }
.dpad { position: relative; width: calc(var(--unit) * 2.5); height: calc(var(--unit) * 2.5); border-radius: 50%; background: var(--key); border: 1px solid var(--line); touch-action: none; }
.dpad::before, .dpad::after { content: ""; position: absolute; background: var(--line); border-radius: 4px; }
.dpad::before { left: 38%; right: 38%; top: 10%; bottom: 10%; }
.dpad::after { top: 38%; bottom: 38%; left: 10%; right: 10%; }
.dpad span { position: absolute; inset: 38%; background: var(--panel); border-radius: 4px; z-index: 1; }
.dpad.held-up { box-shadow: inset 0 calc(var(--unit) * .5) calc(var(--unit) * .4) calc(var(--unit) * -.35) var(--key-held); }
.dpad.held-down { box-shadow: inset 0 calc(var(--unit) * -.5) calc(var(--unit) * .4) calc(var(--unit) * -.35) var(--key-held); }
.dpad.held-left { box-shadow: inset calc(var(--unit) * .5) 0 calc(var(--unit) * .4) calc(var(--unit) * -.35) var(--key-held); }
.dpad.held-right { box-shadow: inset calc(var(--unit) * -.5) 0 calc(var(--unit) * .4) calc(var(--unit) * -.35) var(--key-held); }

#keypad { display: grid; grid-template-columns: repeat(3, var(--unit)); gap: calc(var(--unit) * .12); }
#keypad .key { min-height: calc(var(--unit) * .92); }

#setup { position: absolute; inset: 0; overflow: auto; background: var(--panel); border-radius: 6px; padding: 14px 16px; display: grid; gap: 10px; align-content: start; -webkit-user-select: auto; user-select: auto; }
#setup[hidden] { display: none; }
#setup h2 { margin: 0; font-size: 17px; }
#setup p { margin: 0; color: var(--muted); }
#setup p:empty { display: none; }
#setup fieldset { border: 1px solid var(--line); border-radius: 8px; padding: 10px 12px; margin: 0; display: grid; gap: 8px; min-width: 0; }
#setup legend { color: var(--muted); font-size: 13px; padding: 0 4px; }
#setup label { display: grid; gap: 4px; color: var(--muted); font-size: 13px; min-width: 0; }
#setup label[hidden] { display: none; }
#setup input[type="url"] { font: inherit; color: var(--text); background: var(--bg); border: 1px solid var(--line); border-radius: 6px; padding: 8px; min-width: 0; width: 100%; }
#setup input[type="file"] { min-width: 0; max-width: 100%; }
#setup button { font: inherit; padding: 10px 14px; border-radius: 6px; border: 1px solid var(--line); background: var(--key); color: var(--text); cursor: pointer; }
#setup button.primary { border: 0; background: var(--accent); color: #06121f; font-weight: 600; }
#setup button[hidden] { display: none; }
kbd { background: var(--panel); border-radius: 4px; padding: 1px 5px; }
#help { color: var(--muted); font-size: 13px; text-align: center; margin: 0; flex: none; max-width: 1100px; }
#help:empty { display: none; }

dialog { background: var(--panel); color: var(--text); border: 1px solid var(--line); border-radius: 10px; padding: 16px 18px; width: min(92vw, 560px); max-height: 86vh; overflow: auto; -webkit-user-select: text; user-select: text; }
dialog::backdrop { background: rgba(0, 0, 0, .6); }
dialog h2 { margin: 0 0 10px; font-size: 17px; }
dialog h3 { margin: 14px 0 6px; font-size: 13px; color: var(--muted); text-transform: uppercase; letter-spacing: .04em; }
dialog ul { list-style: none; margin: 0; padding: 0; display: grid; gap: 4px; }
dialog li { display: grid; grid-template-columns: 1.4em 1fr; gap: 4px; }
dialog li small { grid-column: 2; color: var(--muted); overflow-wrap: anywhere; }
dialog .ok { color: #7ddc9a; } dialog .bad { color: #ff9c8a; } dialog .warn { color: #f2c66d; }
dialog form { margin-top: 14px; display: flex; flex-wrap: wrap; gap: 8px; justify-content: flex-end; }
dialog button { font: inherit; padding: 8px 14px; border-radius: 6px; border: 1px solid var(--line); background: var(--key); color: var(--text); cursor: pointer; }
dialog button[type="submit"] { border: 0; background: var(--accent); color: #06121f; }
dialog button:disabled { opacity: .4; cursor: default; }
dialog p { margin: 8px 0; }
dialog .hint { color: var(--muted); font-size: 13px; }
dialog .hint:empty { display: none; }
dialog code { background: var(--bg); border-radius: 4px; padding: 1px 4px; }
dialog .row { display: flex; align-items: center; justify-content: space-between; gap: 12px; margin: 10px 0 4px; }
dialog .beta { font-size: 11px; font-weight: 600; text-transform: uppercase; letter-spacing: .04em; color: #f2c66d; border: 1px solid currentColor; border-radius: 4px; padding: 0 4px; margin-left: 4px; }
dialog select { font: inherit; color: var(--text); background: var(--bg); border: 1px solid var(--line); border-radius: 6px; padding: 7px 8px; max-width: 62%; }
dialog .table-wrap { overflow: auto; max-height: 46vh; border: 1px solid var(--line); border-radius: 8px; }
dialog table { border-collapse: collapse; width: 100%; font-size: 13px; }
dialog th, dialog td { text-align: left; padding: 6px 8px; border-bottom: 1px solid var(--line); vertical-align: middle; }
dialog th { position: sticky; top: 0; background: var(--panel); color: var(--muted); font-weight: 600; }
dialog tr:last-child td { border-bottom: 0; }
dialog td.num { text-align: right; font-variant-numeric: tabular-nums; white-space: nowrap; }
dialog td.empty { color: var(--muted); text-align: center; }
.chips { display: flex; flex-wrap: wrap; gap: 4px; }
.chip { font: inherit; font-size: 12px; padding: 2px 7px; border-radius: 10px; border: 1px solid var(--line); background: var(--bg); color: var(--text); cursor: pointer; white-space: nowrap; }
.chip.add { color: var(--accent); }
.chip.listening { background: var(--key-held); border-color: var(--key-held); }
.menu-grid { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 8px; }
.menu-grid button, .menu-grid a { padding: 12px 10px; }
/* Links to the rest of the site sit in the grid like the buttons. */
.menu-grid a { font: inherit; text-align: center; text-decoration: none; border-radius: 6px; border: 1px solid var(--line); background: var(--key); color: var(--accent); }
.menu-grid a[hidden] { display: none; }
#controls { width: min(96vw, 760px); }
/* Multiplayer dialog */
dialog input[type="text"] { font: inherit; color: var(--text); background: var(--bg); border: 1px solid var(--line); border-radius: 6px; padding: 7px 8px; min-width: 0; }
dialog .row input[type="text"] { flex: 0 1 62%; }
dialog button.primary { border: 0; background: var(--accent); color: #06121f; font-weight: 600; }
dialog .line { display: flex; gap: 8px; margin: 8px 0; }
dialog .line[hidden] { display: none; }
dialog .line input { flex: 1 1 0; }
dialog .line button { flex: none; }
dialog .check { display: flex; align-items: center; gap: 8px; margin: 10px 0 4px; cursor: pointer; }
dialog .check[hidden] { display: none; }
dialog .check input { width: 18px; height: 18px; margin: 0; accent-color: var(--accent); }
dialog .notice { background: var(--bg); border: 1px solid var(--line); border-radius: 6px; padding: 8px 10px; overflow-wrap: anywhere; }
dialog .notice:empty { display: none; }
dialog .notice.bad { border-color: #ff9c8a; color: #ff9c8a; }
dialog .notice.warn { border-color: #f2c66d; color: #f2c66d; }
dialog .room-head { display: flex; flex-wrap: wrap; align-items: baseline; justify-content: space-between; gap: 4px 12px; color: var(--muted); }
#mp-room-code { color: var(--text); font: 600 26px/1.1 ui-monospace, "SF Mono", Menlo, Consolas, monospace; letter-spacing: .1em; margin-left: 4px; }
#mp-link { font-size: 13px; }
#mp-code { text-transform: uppercase; font-family: ui-monospace, "SF Mono", Menlo, Consolas, monospace; letter-spacing: .08em; }
#mp-code::placeholder { text-transform: none; font-family: system-ui, sans-serif; letter-spacing: 0; }
dialog .rooms li { display: flex; align-items: center; justify-content: space-between; gap: 10px; border: 1px solid var(--line); border-radius: 8px; padding: 6px 6px 6px 10px; }
dialog .rooms li > span { min-width: 0; overflow-wrap: anywhere; }
dialog .rooms li small { color: var(--muted); }
dialog .rooms button { flex: none; padding: 6px 12px; }
dialog .rooms:empty { display: none; }
#mp-refresh { margin-top: 8px; }
#mp-players li > span { overflow-wrap: anywhere; }
#guide { width: min(96vw, 720px); }

/* Touch controls only where there is a touch screen (or ?touch=1). */
.touch-only { display: none; }
body.touch .touch-only { display: flex; }
body.touch .desktop-only { display: none; }

/* Portrait: screen on top, both control clusters in a row below it. */
@media (orientation: portrait) {
  body.touch #console { display: grid; grid-template-columns: 1fr 1fr; grid-template-rows: minmax(0, 1fr) auto; gap: 10px 0; }
  body.touch #stage { grid-area: 1 / 1 / 2 / 3; max-width: none; }
  body.touch .pad-left { grid-area: 2 / 1; }
  body.touch .pad-right { grid-area: 2 / 2; }
}

/* Narrow screens: keep the toolbar on one line. */
@media (max-width: 520px) {
  #bar { gap: 6px; }
  #actions { gap: 4px; }
  #actions button { font-size: 12px; padding: 5px 7px; }
  #net-chip .wide { display: none; }
  #net-chip .narrow { display: inline; }
}

/* Short landscape screens (phones on their side): tighten everything. */
@media (orientation: landscape) and (max-height: 480px) {
  body { gap: 4px; }
  #bar { min-height: 28px; }
  #actions button { padding: 3px 8px; min-height: 28px; }
  :root { --unit: clamp(40px, 13.5vmin, 60px); }
  .pad { gap: calc(var(--unit) * .14); }
  .key.soft { min-height: calc(var(--unit) * .86); }
  .dpad { width: calc(var(--unit) * 2.2); height: calc(var(--unit) * 2.2); }
  .pad-left .pad-row .key { min-height: calc(var(--unit) * .86); }
}
