:root {
  --bg: #16181d;
  --panel: #20232a;
  --panel-2: #282c35;
  --line: #343946;
  --text: #eceef3;
  --muted: #9aa1b2;
  --accent: #9db8ff;
  --accent-ink: #0b1a40;
  --danger: #ff6a6a;
  --ok: #58d39a;
  --lcd-0: #d9dfd2;
  --lcd-1: #9ea696;
  --lcd-2: #5b6257;
  --lcd-3: #161b15;
  --radius: 14px;
  color-scheme: dark;
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { margin: 0; height: 100%; background: var(--bg); color: var(--text);
  font: 15px/1.4 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif; }
body { overscroll-behavior: none; }
button { font: inherit; color: inherit; }
.hidden { display: none !important; }
.muted { color: var(--muted); }
code { background: var(--panel-2); padding: 1px 5px; border-radius: 5px; font-size: .92em; }
#sheet-body a, .banner a { color: var(--accent); }
#sheet-body .about { border-top: 1px solid var(--line); padding-top: 10px; margin-top: 12px; }

.view { display: none; min-height: 100%; }
.view.active { display: block; }

/* ---------- top bar ---------- */
.topbar { position: sticky; top: 0; z-index: 5; display: flex; align-items: center; justify-content: space-between;
  padding: calc(env(safe-area-inset-top) + 12px) 16px 12px; background: rgba(22,24,29,.92); backdrop-filter: blur(10px);
  border-bottom: 1px solid var(--line); }
.brand { display: flex; gap: 12px; align-items: center; min-width: 0; }
.brand h1 { font-size: 18px; margin: 0; letter-spacing: .2px; }
.brand p { margin: 0; font-size: 13px; }
/* a tiny P/ECE: silver, screen in the middle */
.brand-mark { width: 40px; height: 26px; border-radius: 6px; background: linear-gradient(#e6e8ec, #b9bdc5); position: relative; flex: none; box-shadow: inset 0 -3px 0 #9a9ea7; }
.brand-mark::before { content: ""; position: absolute; left: 12px; top: 5px; width: 16px; height: 12px; border-radius: 2px; background: var(--lcd-0); box-shadow: 0 0 0 1.5px #2b2e34; }
.brand-mark::after { content: ""; position: absolute; left: 3px; top: 9px; width: 6px; height: 6px; border-radius: 2px; background: #3c4047; box-shadow: 28px -2px 0 0 #3c4047; }
.icon-btn { background: var(--panel); border: 1px solid var(--line); border-radius: 10px; width: 40px; height: 40px;
  font-size: 20px; line-height: 1; display: grid; place-items: center; cursor: pointer; flex: none; }
.icon-btn:active { background: var(--panel-2); }

main { padding: 8px 16px calc(env(safe-area-inset-bottom) + 32px); max-width: 980px; margin: 0 auto; }
.section-title { font-size: 13px; text-transform: uppercase; letter-spacing: .08em; color: var(--muted); margin: 22px 2px 10px; font-weight: 600; }
.banner { margin: 12px auto 0; padding: 14px; border-radius: var(--radius); background: #1e2538; border: 1px solid #38476e;
  display: flex; flex-direction: column; gap: 10px; align-items: flex-start; max-width: 948px; width: calc(100% - 32px); }
.banner-row, .import-row { display: flex; gap: 10px; flex-wrap: wrap; }
.import-row { margin-top: 16px; }
.empty { color: var(--muted); margin: 4px 2px; }

/* ---------- buttons ---------- */
.btn { display: inline-flex; align-items: center; justify-content: center; gap: 6px; padding: 11px 16px; border-radius: 11px;
  border: 1px solid var(--line); background: var(--panel); cursor: pointer; font-weight: 600; text-decoration: none; }
.btn:active { background: var(--panel-2); }
.btn.primary { background: var(--accent); color: var(--accent-ink); border-color: transparent; }
.btn.danger { color: var(--danger); }
.btn.small { padding: 8px 12px; font-size: 14px; }
.btn[disabled] { opacity: .5; cursor: default; }

/* ---------- library ---------- */
.game-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(150px, 1fr)); gap: 12px; }
.game { text-align: left; background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); padding: 10px;
  display: flex; flex-direction: column; gap: 8px; cursor: pointer; position: relative; }
.game:active { transform: scale(.98); }
.game .thumb { width: 100%; aspect-ratio: 3 / 2; border-radius: 8px; background: radial-gradient(90% 90% at 50% 20%, #343a48, #23262e); display: grid; place-items: center; }
.game .thumb img, .game .thumb .noicon { width: 64px; height: 64px; image-rendering: pixelated; border-radius: 6px; }
.game .thumb .noicon { background: var(--lcd-0); display: grid; place-items: center; color: var(--lcd-3); font-weight: 800; font-size: 22px; }
.game .name { font-weight: 650; font-size: 14px; line-height: 1.25; overflow-wrap: anywhere; }
.game .meta { font-size: 12px; color: var(--muted); display: flex; gap: 6px; flex-wrap: wrap; align-items: center; min-width: 0; }
.game .meta span { min-width: 0; overflow-wrap: anywhere; }
.tag { font-size: 11px; font-weight: 700; padding: 1px 7px; border-radius: 99px; background: #2b3140; color: #b9c2d6; }
.tag.good { background: #17352a; color: #7fe0b2; }
.game .more { position: absolute; top: 6px; right: 6px; width: 30px; height: 30px; border-radius: 8px; border: none; background: rgba(0,0,0,.45); color: #fff; font-size: 16px; cursor: pointer; }

.card-list { display: flex; flex-direction: column; gap: 8px; }
.card { display: flex; align-items: center; gap: 12px; padding: 10px 12px; background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); cursor: pointer; }
.card .chip { width: 40px; height: 28px; border-radius: 6px; background: linear-gradient(#e6e8ec, #b9bdc5); flex: none; position: relative; box-shadow: inset 0 -3px 0 #9a9ea7; }
.card .chip::before { content: ""; position: absolute; left: 12px; top: 6px; width: 16px; height: 12px; border-radius: 2px; background: var(--lcd-0); box-shadow: 0 0 0 1.5px #2b2e34; }
.card .info { flex: 1; min-width: 0; }
.card .info .name { font-weight: 650; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.card .info .meta { font-size: 12px; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.card .more { border: none; background: none; font-size: 20px; padding: 4px 8px; cursor: pointer; color: var(--muted); }

/* ---------- player ---------- */
#player { background: radial-gradient(120% 80% at 50% 0%, #2a2e38 0%, var(--bg) 70%); height: 100dvh; min-height: 0; display: none; flex-direction: column; }
#player.active { display: flex; }
html.in-player, html.in-player body { overflow: hidden; }
.player-bar { display: flex; align-items: center; gap: 8px; padding: calc(env(safe-area-inset-top) + 10px) 12px 6px; flex: none; }
.player-title { flex: 1; min-width: 0; display: flex; flex-direction: column; }
.player-title span:first-child { font-weight: 650; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.player-title .muted { font-size: 12px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.device-wrap { flex: 1; min-height: 0; display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 10px;
  padding: 6px 12px calc(env(safe-area-inset-bottom) + 14px); user-select: none; -webkit-user-select: none; touch-action: none; container-type: size; }
.note { color: var(--muted); font-size: 13px; margin: 0; text-align: center; }

/* The P/ECE. Everything inside is sized in --u (1/100 of the body's width), so it scales as one piece.
   Upright (a phone held normally): screen on top, controls underneath. Sideways or on a desktop: the real
   layout, direction pad left of the screen and the buttons right of it. */
.piece { --u: min(0.94cqw, 0.72cqh, 5px); width: calc(100 * var(--u)); height: calc(128 * var(--u)); position: relative; flex: none;
  background: linear-gradient(160deg, #e6e8ec 0%, #cfd2d8 50%, #b4b8c0 100%); border-radius: calc(8 * var(--u));
  box-shadow: inset 0 calc(-1.6 * var(--u)) 0 #9a9ea7, inset 0 calc(.8 * var(--u)) 0 #f7f8fa, 0 calc(4 * var(--u)) calc(9 * var(--u)) rgba(0,0,0,.45); }
.piece-screen { position: absolute; left: calc(7 * var(--u)); right: calc(7 * var(--u)); top: calc(7 * var(--u)); padding: calc(4 * var(--u)) calc(4 * var(--u)) calc(6 * var(--u));
  background: #2b2e34; border-radius: calc(4 * var(--u)); box-shadow: inset 0 calc(.8 * var(--u)) calc(1.6 * var(--u)) rgba(0,0,0,.6); }
.piece-glass { background: var(--lcd-0); padding: calc(1 * var(--u)); border-radius: calc(.8 * var(--u)); box-shadow: inset 0 0 calc(2 * var(--u)) rgba(0,0,0,.3); }
#lcd { display: block; width: 100%; aspect-ratio: 128 / 88; image-rendering: pixelated; image-rendering: crisp-edges; }
.piece-logo { position: absolute; right: calc(5 * var(--u)); bottom: calc(1.2 * var(--u)); color: #8d929b; font-weight: 800; font-style: italic; font-size: calc(3 * var(--u)); letter-spacing: .12em; }
.dpad { position: absolute; left: calc(8 * var(--u)); bottom: calc(10 * var(--u)); width: calc(36 * var(--u)); height: calc(36 * var(--u)); touch-action: none; cursor: pointer; }
.dpad span { position: absolute; background: linear-gradient(#4a4e56, #33363c); border-radius: calc(2.4 * var(--u)); box-shadow: 0 calc(.9 * var(--u)) 0 #1f2125; }
.dpad .dpad-v { left: 34%; width: 32%; top: 2%; height: 96%; }
.dpad .dpad-h { top: 34%; height: 32%; left: 2%; width: 96%; }
.dpad::after { content: ""; position: absolute; left: 40%; top: 40%; width: 20%; height: 20%; border-radius: 50%; background: #2b2e33; }
.dpad.up .dpad-v { background: linear-gradient(#2b2e33 0 45%, #4a4e56 60%); }
.dpad.down .dpad-v { background: linear-gradient(#4a4e56 40%, #2b2e33 55%); }
.dpad.left .dpad-h { background: linear-gradient(90deg, #2b2e33 0 45%, #4a4e56 60%); }
.dpad.right .dpad-h { background: linear-gradient(90deg, #4a4e56 40%, #2b2e33 55%); }
.piece-ab { position: absolute; right: calc(7 * var(--u)); bottom: calc(12 * var(--u)); width: calc(40 * var(--u)); height: calc(30 * var(--u)); }
.piece-small { position: absolute; left: 50%; transform: translateX(-50%); bottom: calc(3 * var(--u)); width: calc(40 * var(--u)); height: calc(7 * var(--u)); }
.pbtn { position: absolute; border: none; padding: 0; cursor: pointer; touch-action: none; background: none; }
.pbtn span { display: grid; place-items: center; width: 100%; height: 100%; font-weight: 800; pointer-events: none; }
.pbtn.round { width: calc(16 * var(--u)); height: calc(16 * var(--u)); }
.pbtn.round span { border-radius: 50%; background: radial-gradient(circle at 35% 30%, #5d626b, #33363c); color: #e3e5e9; font-size: calc(6 * var(--u)); box-shadow: 0 calc(1 * var(--u)) 0 #1f2125; }
.pbtn.round[data-btn="16"] { left: 0; bottom: 0; }
.pbtn.round[data-btn="32"] { right: 0; top: 0; }
.pbtn.oval { width: calc(18 * var(--u)); height: calc(7 * var(--u)); top: 0; }
.pbtn.oval span { border-radius: 99px; background: linear-gradient(#6f747d, #555961); color: #eef0f3; font-size: calc(2.4 * var(--u)); letter-spacing: .04em; box-shadow: 0 calc(.7 * var(--u)) 0 #33363c; }
.pbtn.oval[data-btn="64"] { left: 0; }
.pbtn.oval[data-btn="128"] { right: 0; }
.pbtn.pressed span { transform: translateY(calc(.8 * var(--u))); box-shadow: none; filter: brightness(.82); }

@container (min-aspect-ratio: 13 / 10) {
  .piece { --u: min(0.62cqw, 1.3cqh, 7px); width: calc(160 * var(--u)); height: calc(74 * var(--u)); }
  .piece-screen { left: calc(46 * var(--u)); right: calc(46 * var(--u)); top: calc(6 * var(--u)); padding: calc(3 * var(--u)) calc(3 * var(--u)) calc(5 * var(--u)); }
  .dpad { left: calc(6 * var(--u)); bottom: calc(22 * var(--u)); width: calc(34 * var(--u)); height: calc(34 * var(--u)); }
  .piece-ab { right: calc(5 * var(--u)); bottom: calc(24 * var(--u)); width: calc(36 * var(--u)); height: calc(28 * var(--u)); }
  .piece-small { bottom: calc(4 * var(--u)); }
}

/* phone held sideways: the bar floats over the corners so the unit gets the full height */
@media (orientation: landscape) and (max-height: 560px) {
  .player-bar { position: absolute; top: 0; left: 0; right: 0; z-index: 3; padding: calc(env(safe-area-inset-top) + 6px) calc(env(safe-area-inset-right) + 10px) 0 calc(env(safe-area-inset-left) + 10px); pointer-events: none; }
  .player-bar > * { pointer-events: auto; }
  .player-title { visibility: hidden; }
  .device-wrap { padding: 6px 56px calc(env(safe-area-inset-bottom) + 6px); }
}

/* ---------- sheet / toast ---------- */
.sheet { position: fixed; inset: 0; z-index: 20; background: rgba(0,0,0,.55); display: flex; align-items: flex-end; justify-content: center; }
.sheet-panel { width: 100%; max-width: 520px; background: var(--panel); border-radius: 18px 18px 0 0; padding: 18px 16px calc(env(safe-area-inset-bottom) + 16px);
  border: 1px solid var(--line); max-height: 88dvh; overflow: auto; }
.sheet-panel h3 { margin: 0 0 8px; font-size: 17px; overflow-wrap: anywhere; }
#sheet-body { color: var(--muted); font-size: 14px; }
#sheet-body p { margin: 6px 0 10px; }
#sheet-body input[type="text"] { width: 100%; padding: 10px 12px; border-radius: 10px; border: 1px solid var(--line); background: var(--bg); color: var(--text); font: inherit; }
.sheet-actions { display: flex; flex-direction: column; gap: 8px; margin-top: 12px; }
.sheet-actions .btn { width: 100%; }
.field { display: flex; align-items: center; gap: 10px; margin: 10px 0; color: var(--text); }
.field input[type="checkbox"] { width: 20px; height: 20px; accent-color: var(--accent); flex: none; }
.keys, .files { width: 100%; border-collapse: collapse; color: var(--text); }
.keys td, .files td { padding: 5px 4px; border-bottom: 1px solid var(--line); vertical-align: middle; }
.keys td:last-child { text-align: right; color: var(--muted); }
.files td:nth-child(2) { color: var(--muted); font-size: 12px; white-space: nowrap; }
.files td:last-child { text-align: right; white-space: nowrap; }
.files img { width: 32px; height: 32px; image-rendering: pixelated; vertical-align: middle; border-radius: 4px; }
.files .mini { border: 1px solid var(--line); background: var(--bg); border-radius: 8px; padding: 4px 9px; cursor: pointer; font-size: 13px; }
.files .mini.danger { color: var(--danger); }
kbd { background: var(--bg); border: 1px solid var(--line); border-radius: 5px; padding: 0 5px; font: inherit; font-size: 12px; }
.toast { position: fixed; left: 50%; bottom: calc(env(safe-area-inset-bottom) + 24px); transform: translateX(-50%); z-index: 30;
  background: #2d323d; border: 1px solid var(--line); padding: 10px 16px; border-radius: 12px; font-size: 14px; max-width: 90vw; box-shadow: 0 10px 30px rgba(0,0,0,.4); }

@media (min-width: 700px) {
  .sheet { align-items: center; }
  .sheet-panel { border-radius: 18px; }
}
