:root {
  --bg: #15171c;
  --panel: #1f2229;
  --panel-2: #272b34;
  --line: #333845;
  --text: #eceef3;
  --muted: #9aa1b2;
  --accent: #6aa6ff;
  --danger: #ff6a6a;
  --ok: #58d39a;
  --plastic: #e9e9e4;
  --plastic-shade: #c9c9c2;
  --plastic-edge: #b3b3ab;
  --lcd-bg: #a8b39a;
  --lcd-ink: #1d2418;
  --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, "Hiragino Sans", "Noto Sans JP", 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; }

.web-only { display: none; }
html.standalone .web-only { display: inline; }
html.standalone .pc-only { display: none; }
#sheet-body 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(21,23,28,.92); backdrop-filter: blur(10px);
  border-bottom: 1px solid var(--line); }
.brand { display: flex; gap: 12px; align-items: center; }
.brand h1 { font-size: 18px; margin: 0; letter-spacing: .2px; }
.brand p { margin: 0; font-size: 13px; }
.brand-mark { width: 30px; height: 38px; border-radius: 7px; background: var(--plastic); position: relative; flex: none;
  box-shadow: inset 0 -3px 0 var(--plastic-shade); }
.brand-mark::after { content: ""; position: absolute; left: 6px; top: 6px; width: 18px; height: 16px; border-radius: 2px; background: var(--lcd-bg); }
.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: 860px; margin: 0 auto; }
.section-title { font-size: 13px; text-transform: uppercase; letter-spacing: .08em; color: var(--muted); margin: 22px 2px 10px; font-weight: 600; }
.empty { color: var(--muted); background: var(--panel); border: 1px dashed var(--line); border-radius: var(--radius); padding: 16px; margin: 0; }

.banner { margin: 12px 16px 0; padding: 14px; border-radius: var(--radius); background: #2a2f22; border: 1px solid #4a5634;
  display: flex; flex-direction: column; gap: 10px; align-items: flex-start; }

/* ---------- 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: #08142a; border-color: transparent; }
.btn.danger { color: var(--danger); }
.btn.small { padding: 8px 12px; font-size: 14px; }
.link { background: none; border: none; color: var(--accent); font-weight: 600; padding: 0; cursor: pointer; text-decoration: underline; }
.import-row { margin-top: 20px; display: flex; gap: 10px; flex-wrap: wrap; }

/* ---------- game grid ---------- */
.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: 12px;
  display: flex; flex-direction: column; gap: 10px; cursor: pointer; min-height: 150px; position: relative; }
.game:active { transform: scale(.98); }
.game .thumb { width: 64px; height: 64px; border-radius: 10px; background: var(--lcd-bg); display: grid; place-items: center; overflow: hidden; }
.game .thumb canvas { width: 64px; height: 64px; image-rendering: pixelated; }
.game .name { font-weight: 650; font-size: 14px; line-height: 1.25; display: -webkit-box; -webkit-line-clamp: 3; -webkit-box-orient: vertical; overflow: hidden; }
.game .meta { font-size: 12px; color: var(--muted); margin-top: auto; }
.badge { position: absolute; top: 10px; right: 10px; font-size: 11px; font-weight: 700; padding: 2px 7px; border-radius: 99px; background: #3b2d14; color: #ffc864; }

/* ---------- memory cards ---------- */
.card-list { display: flex; flex-direction: column; gap: 10px; }
details.mc { background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); overflow: hidden; }
details.mc summary { list-style: none; padding: 14px; display: flex; gap: 12px; align-items: center; cursor: pointer; }
details.mc summary::-webkit-details-marker { display: none; }
.mc-icon { width: 34px; height: 42px; border-radius: 5px; background: #3a3f4c; flex: none; position: relative; }
.mc-icon::after { content: ""; position: absolute; left: 5px; right: 5px; bottom: 5px; height: 6px; border-radius: 2px; background: #555c6c; }
.mc-name { font-weight: 650; }
.mc-sub { font-size: 12px; color: var(--muted); }
.mc-body { border-top: 1px solid var(--line); padding: 6px 14px 14px; }
.save-row { display: flex; align-items: center; gap: 10px; padding: 8px 0; border-bottom: 1px solid #2a2e37; }
.save-row:last-of-type { border-bottom: none; }
.save-row canvas { width: 32px; height: 32px; image-rendering: pixelated; border-radius: 4px; background: #111; flex: none; }
.save-row .t { flex: 1; min-width: 0; }
.save-row .t div:first-child { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.save-row .t div:last-child { font-size: 12px; color: var(--muted); }
.pill { font-size: 11px; padding: 2px 7px; border-radius: 99px; background: #1d3350; color: #9cc6ff; font-weight: 700; flex: none; }
.mc-actions { display: flex; gap: 8px; flex-wrap: wrap; margin-top: 10px; }

/* ---------- player ---------- */
#player { background: radial-gradient(120% 80% at 50% 0%, #2a2e38 0%, var(--bg) 70%); min-height: 100dvh; display: none; flex-direction: column; }
#player.active { display: flex; }
.player-bar { display: flex; align-items: center; gap: 10px; padding: calc(env(safe-area-inset-top) + 10px) 12px 10px; }
.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; }
.device-wrap { flex: 1; display: flex; flex-direction: column; align-items: center; justify-content: center; padding: 8px 16px calc(env(safe-area-inset-bottom) + 20px); gap: 14px; }

.device { width: min(92vw, 400px, calc((100dvh - 120px) * 0.62)); aspect-ratio: 0.62; background: linear-gradient(180deg, #f2f2ee, var(--plastic) 40%, #deded8);
  border-radius: 9% / 6%; padding: 6% 7% 7%; display: flex; flex-direction: column; gap: 4%;
  box-shadow: 0 2px 0 #fff inset, 0 -6px 0 var(--plastic-shade) inset, 0 24px 60px rgba(0,0,0,.55), 0 0 0 1px var(--plastic-edge);
  user-select: none; -webkit-user-select: none; touch-action: none; }
.device-top { display: flex; align-items: center; justify-content: space-between; height: 5%; }
.logo { font: italic 700 clamp(10px, 3.2vw, 14px)/1 system-ui, sans-serif; color: #8c8c86; letter-spacing: .02em; }
.led { width: 9px; height: 9px; border-radius: 50%; background: #5a2020; box-shadow: inset 0 1px 1px rgba(0,0,0,.4); transition: background .05s; }
.led.on { background: #ff3b3b; box-shadow: 0 0 8px 2px rgba(255,60,60,.7); }
.screen-bezel { background: #5d6168; border-radius: 6%; padding: 7%; box-shadow: inset 0 3px 8px rgba(0,0,0,.5); }
#lcd { width: 100%; aspect-ratio: 1; display: block; border-radius: 3px; background: var(--lcd-bg); image-rendering: pixelated; }
.controls { flex: 1; position: relative; display: flex; align-items: center; justify-content: space-between; }
.dpad { position: relative; width: 56%; aspect-ratio: 1; }
.pbtn { position: absolute; border: none; background: linear-gradient(180deg, #fbfbf8, #d8d8d1); cursor: pointer; touch-action: none;
  box-shadow: 0 3px 0 #a9a9a1, 0 4px 8px rgba(0,0,0,.25); transition: transform .04s, box-shadow .04s; padding: 0; }
.pbtn.pressed { transform: translateY(3px); box-shadow: 0 0 0 #a9a9a1, 0 1px 3px rgba(0,0,0,.25); background: #d0d0c8; }
.dpad .pbtn { width: 34%; height: 34%; border-radius: 22%; }
.dpad .up { left: 33%; top: 0; }
.dpad .down { left: 33%; bottom: 0; }
.dpad .left { left: 0; top: 33%; }
.dpad .right { right: 0; top: 33%; }
.dpad .pbtn::after { content: ""; position: absolute; inset: 0; margin: auto; width: 0; height: 0; border: 6px solid transparent; }
.dpad .up::after { border-bottom-color: #9a9a92; transform: translateY(-3px); }
.dpad .down::after { border-top-color: #9a9a92; transform: translateY(3px); }
.dpad .left::after { border-right-color: #9a9a92; transform: translateX(-3px); }
.dpad .right::after { border-left-color: #9a9a92; transform: translateX(3px); }
.fire.pbtn { position: relative; width: 30%; aspect-ratio: 1; border-radius: 50%; margin-right: 2%;
  background: radial-gradient(circle at 40% 35%, #ffffff, #dcdcd4 70%); }
.unsynced { font-size: 13px; color: #ffc864; margin: 0; text-align: center; }

/* ---------- 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: 85dvh; overflow: auto; }
.sheet-panel h3 { margin: 0 0 8px; font-size: 17px; }
#sheet-body { color: var(--muted); font-size: 14px; }
#sheet-body p { margin: 6px 0 10px; }
.sheet-actions { display: flex; flex-direction: column; gap: 8px; margin-top: 12px; }
.sheet-actions .btn { width: 100%; }
.field { display: flex; flex-direction: column; gap: 6px; margin: 10px 0; color: var(--text); }
.field input { background: var(--bg); border: 1px solid var(--line); border-radius: 10px; padding: 10px; color: var(--text); font: inherit; }
.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; }
}
