:root {
  --bg: #0d0f14; --panel: #151922; --key: #212837; --key-hi: #3b465e; --line: #2a3142;
  --text: #e9edf5; --muted: #8b93a7; --accent: #ffb000; --accent-ink: #1b1300;
  --sat: env(safe-area-inset-top, 0px); --sab: env(safe-area-inset-bottom, 0px);
  --sal: env(safe-area-inset-left, 0px); --sar: env(safe-area-inset-right, 0px);
}
* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; background: var(--bg); color: var(--text); overscroll-behavior: none; }
body { font: 16px/1.4 -apple-system, BlinkMacSystemFont, "SF Pro Text", "Segoe UI", Roboto, sans-serif;
  -webkit-text-size-adjust: 100%; -webkit-tap-highlight-color: transparent; touch-action: manipulation; }
a { color: inherit; text-decoration: none; }
button { font: inherit; color: inherit; }

/* ------------------------------------------------------------- library */
body.library { padding: calc(var(--sat) + 20px) calc(var(--sar) + 18px) calc(var(--sab) + 28px) calc(var(--sal) + 18px); overflow-y: auto; }
.library main { max-width: 720px; margin: 0 auto; }
.library h1 { font-size: 28px; letter-spacing: -.02em; margin: 6px 0 2px; }
.library h1 span { color: var(--accent); }
.library .sub { color: var(--muted); margin: 0 0 22px; }
.cards { display: grid; gap: 12px; grid-template-columns: repeat(auto-fill, minmax(260px, 1fr)); }
.card { display: flex; gap: 14px; align-items: center; padding: 14px; border-radius: 16px; background: var(--panel); border: 1px solid var(--line);
  transition: transform .12s, border-color .12s; }
.card:active { transform: scale(.98); border-color: var(--accent); }
.card img { width: 56px; height: 56px; image-rendering: pixelated; border-radius: 12px; background: #000; padding: 6px; border: 1px solid var(--line); flex: none; }
.card .ph { width: 56px; height: 56px; border-radius: 12px; background: var(--key); flex: none; }
.card h2 { font-size: 17px; margin: 0 0 2px; }
.card p { margin: 0; color: var(--muted); font-size: 13px; }
.card .go { margin-left: auto; background: var(--accent); color: var(--accent-ink); font-weight: 700; padding: 8px 14px; border-radius: 999px; font-size: 14px; }
.tip { margin-top: 26px; padding: 14px 16px; border-radius: 14px; border: 1px dashed var(--line); color: var(--muted); font-size: 14px; }
.tip b { color: var(--text); }
.empty { color: var(--muted); }

/* -------------------------------------------------------------- player */
body.player { overflow: hidden; position: fixed; inset: 0; width: 100%; }
#app { height: 100dvh; display: grid; grid-template-rows: auto minmax(0, 1fr) auto;
  padding: var(--sat) var(--sar) var(--sab) var(--sal); }
#bar { display: flex; align-items: center; gap: 10px; padding: 6px 10px; min-height: 42px; }
#bar h1 { flex: 1; font-size: 15px; font-weight: 600; margin: 0; text-align: center; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
#back { font-size: 30px; line-height: 1; width: 40px; text-align: left; padding-bottom: 4px; color: var(--muted); }
#logBtn { background: none; border: 1px solid var(--line); color: var(--muted); border-radius: 8px; font-size: 12px; padding: 4px 8px; width: 40px; }

#stage { display: flex; align-items: center; justify-content: center; min-height: 0; padding: 4px; }
#bezel { position: relative; padding: 7px; border-radius: 16px; background: #04060a;
  box-shadow: 0 0 0 2px #232a39, 0 12px 40px rgba(0,0,0,.65), inset 0 0 12px rgba(255,255,255,.04); }
#screen { display: block; image-rendering: pixelated; image-rendering: crisp-edges; background: #000; border-radius: 4px; touch-action: none; }

#deck { padding: 6px 12px 10px; display: grid; gap: 8px; justify-items: center; }
#softrow { display: grid; grid-template-columns: 1fr auto 1fr; align-items: center; gap: 10px; width: min(100%, 380px); }
.key { appearance: none; -webkit-appearance: none; border: 0; background: var(--key); border-radius: 14px; touch-action: none;
  box-shadow: 0 2px 0 #0a0d13, inset 0 1px 0 rgba(255,255,255,.07); user-select: none; -webkit-user-select: none; -webkit-touch-callout: none;
  display: flex; flex-direction: column; align-items: center; justify-content: center; cursor: pointer; }
.key.down, .key:active { background: var(--key-hi); transform: translateY(1px); box-shadow: 0 1px 0 #0a0d13; }
.key.soft { height: clamp(40px, 6.4dvh, 54px); font-size: 20px; color: var(--accent); }
#softrow .soft:first-child { justify-self: start; width: min(100%, 92px); }
#softrow .soft:last-child { justify-self: end; width: min(100%, 92px); }
#numpad { display: grid; grid-template-columns: repeat(3, 1fr); gap: 7px; width: min(100%, 330px); }
#numpad .key { height: clamp(36px, 5.6dvh, 50px); }
#numpad .key b { font-size: 20px; line-height: 1; font-weight: 600; }
#numpad .key small { font-size: 8px; letter-spacing: .12em; color: var(--muted); line-height: 1.2; margin-top: 1px; }

#dpad { position: relative; width: clamp(112px, 17.5dvh, 150px); aspect-ratio: 1; border-radius: 50%; touch-action: none; user-select: none; -webkit-user-select: none;
  background: radial-gradient(circle at 50% 40%, #2a3245, #1a2030 70%); box-shadow: 0 3px 0 #0a0d13, inset 0 1px 0 rgba(255,255,255,.08); }
#dpad .arrow { position: absolute; width: 0; height: 0; border: 9px solid transparent; transition: filter .06s; }
#dpad .up { top: 9%; left: calc(50% - 9px); border-bottom: 13px solid #6f7b96; border-top: 0; }
#dpad .down { bottom: 9%; left: calc(50% - 9px); border-top: 13px solid #6f7b96; border-bottom: 0; }
#dpad .left { left: 9%; top: calc(50% - 9px); border-right: 13px solid #6f7b96; border-left: 0; }
#dpad .right { right: 9%; top: calc(50% - 9px); border-left: 13px solid #6f7b96; border-right: 0; }
#dpad .fire { position: absolute; inset: 31%; border-radius: 50%; background: #11151e; box-shadow: inset 0 0 0 2px #2c3447; }
#dpad.a-up .up { border-bottom-color: var(--accent); } #dpad.a-down .down { border-top-color: var(--accent); }
#dpad.a-left .left { border-right-color: var(--accent); } #dpad.a-right .right { border-left-color: var(--accent); }
#dpad.a-fire .fire { background: var(--accent); }
#kbhint { display: none; margin: 0; color: var(--muted); font-size: 12px; }
@media (hover: hover) and (pointer: fine) { #kbhint { display: block; } }

/* landscape: dpad left of the screen, keypad right */
body.landscape #app { grid-template-columns: 1fr auto 1fr; grid-template-rows: auto minmax(0, 1fr); }
body.landscape #bar { grid-column: 1 / 4; }
body.landscape #stage { grid-column: 2; grid-row: 2; }
body.landscape #deck { display: contents; }
body.landscape #softrow { grid-column: 1; grid-row: 2; align-self: center; justify-self: center; width: auto; display: grid;
  grid-template-columns: auto auto; grid-template-rows: auto auto; gap: 10px; }
body.landscape #softrow #dpad { grid-column: 1 / 3; grid-row: 1; justify-self: center; }
body.landscape #softrow .soft:first-child { grid-column: 1; grid-row: 2; justify-self: center; width: 84px; }
body.landscape #softrow .soft:last-child { grid-column: 2; grid-row: 2; justify-self: center; width: 84px; }
body.landscape #numpad { grid-column: 3; grid-row: 2; align-self: center; justify-self: center; width: min(92%, 270px); }
body.landscape #numpad .key { height: clamp(34px, 11dvh, 48px); }
body.landscape #kbhint { display: none; }

/* ------------------------------------------------------------- overlays */
#msg, #textDlg { position: fixed; inset: 0; background: rgba(5,7,10,.78); display: flex; align-items: center; justify-content: center; padding: 20px; z-index: 20; }
#msg[hidden], #textDlg[hidden], #logPanel[hidden] { display: none; }
#msg > div, #textForm { background: var(--panel); border: 1px solid var(--line); border-radius: 18px; padding: 18px; width: min(100%, 420px); }
#msg h2 { margin: 0 0 8px; font-size: 18px; }
#msg pre { white-space: pre-wrap; word-break: break-word; margin: 0 0 14px; color: var(--muted); font: 13px/1.4 ui-monospace, Menlo, monospace; max-height: 40dvh; overflow: auto; }
#msg button, #textForm button { background: var(--accent); color: var(--accent-ink); border: 0; border-radius: 12px; padding: 10px 18px; font-weight: 700; }
#textForm label { display: block; margin-bottom: 10px; color: var(--muted); font-size: 14px; }
#textInput { width: 100%; font-size: 18px; padding: 12px; border-radius: 12px; border: 1px solid var(--line); background: #0b0e14; color: var(--text); }
#textForm .row { display: flex; gap: 10px; justify-content: flex-end; margin-top: 14px; }
#textForm #textCancel { background: var(--key); color: var(--text); }
#logPanel { position: fixed; inset: 0; background: rgba(8,10,14,.97); z-index: 30; display: flex; flex-direction: column; padding: calc(var(--sat) + 10px) 12px calc(var(--sab) + 10px); }
.logHead { display: flex; justify-content: space-between; align-items: center; margin-bottom: 8px; }
.logHead button { background: var(--key); border: 0; border-radius: 10px; padding: 6px 12px; }
#logText { flex: 1; overflow: auto; margin: 0; font: 12px/1.35 ui-monospace, Menlo, monospace; white-space: pre-wrap; word-break: break-word; color: #b9c3da; }
