:root {
  --bg: #111318;
  --panel: #1a1d25;
  --panel-2: #232733;
  --line: #2c313d;
  --text: #e6e8ee;
  --muted: #9aa0ad;
  --accent: #5b8cff;
  --danger: #ff6b6b;
  --pad: rgba(255, 255, 255, 0.10);
  --pad-active: var(--accent);
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  height: 100%;
  overflow: hidden;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Apple SD Gothic Neo",
    "Noto Sans KR", "Segoe UI", Roboto, sans-serif;
  -webkit-user-select: none;
  user-select: none;
  -webkit-tap-highlight-color: transparent;
  overscroll-behavior: none;
}

/* ── 인게임 스테이지 (그리드 레이아웃) ───────────── */
#stage {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100vh;
  height: 100dvh;               /* 모바일 툴바 대응 (세로모드 잘림 방지) */
  display: none;
  z-index: 1;
  touch-action: none;           /* 터치 스크롤/줌 방지 */
}
body.playing #stage { display: grid; }

/* 세로 모드: 화면 위, 컨트롤 아래(좌 방향키 · 중앙 SELECT/START · 우 A/B) */
body[data-orient="portrait"] #stage {
  grid-template-columns: 1fr 1fr;   /* 좌/우 반반 — 방향키·A/B 가 각 칸에 여유있게 */
  grid-template-rows: auto minmax(0, 1fr) auto auto auto;
  grid-template-areas:
    "hud    hud"
    "screen screen"
    "left   right"
    "sys    sys"
    "legend legend";
}
/* 가로 모드: 방향키 왼쪽 · 화면 가운데 · A/B 오른쪽 (화면을 감싼다) */
body[data-orient="landscape"] #stage {
  grid-template-columns: auto minmax(0, 1fr) auto;
  grid-template-rows: auto minmax(0, 1fr) auto auto;
  grid-template-areas:
    "hud    hud    hud"
    "left   screen right"
    "left   sys    right"
    "legend legend legend";
}

/* 화면 버튼 숨김 토글 (키보드만 쓸 때) */
body.no-pad .side,
body.no-pad .sysbtns { display: none; }

/* ── HUD ─────────────────────────────────────── */
#hud {
  grid-area: hud;
  display: flex;
  align-items: center;
  gap: 8px;
  padding: max(8px, env(safe-area-inset-top)) 12px 8px;
  background: rgba(10, 12, 16, 0.6);
}
#hud .spacer { flex: 1; }
#hud button {
  background: rgba(30, 34, 44, 0.85);
  color: var(--text);
  border: 1px solid var(--line);
  border-radius: 9px;
  padding: 8px 12px;
  font-size: 14px;
  font-weight: 600;
  cursor: pointer;
}
#hud button:active { background: var(--panel-2); }
#hud button.on { background: var(--accent); border-color: var(--accent); color: #fff; }

/* ── 게임 화면 ───────────────────────────────── */
#screen {
  grid-area: screen;
  display: flex;
  align-items: center;
  justify-content: center;
  min-width: 0;
  min-height: 0;
  overflow: hidden;
  background: #000;
}
#canvas {
  width: 100%;
  height: 100%;
  object-fit: contain;          /* 종횡비 유지 레터박스 */
  image-rendering: pixelated;   /* 픽셀 선명하게 확대 */
  image-rendering: crisp-edges;
}

/* ── 가상 게임패드 ───────────────────────────── */
.side {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: clamp(10px, 3vmin, 20px);
  padding: 10px;
}
.side-left  { grid-area: left; }
.side-right { grid-area: right; }

.pad {
  pointer-events: auto;
  background: var(--pad);
  color: rgba(255, 255, 255, 0.88);
  border: 1px solid rgba(255, 255, 255, 0.14);
  backdrop-filter: blur(2px);
  font-weight: 700;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  user-select: none;
}
.pad.active { background: var(--pad-active); color: #fff; border-color: var(--pad-active); }

/* 방향키 십자패드 */
.dpad {
  --dsz: clamp(38px, 11vmin, 60px);
  display: grid;
  grid-template-columns: repeat(3, var(--dsz));
  grid-template-rows: repeat(3, var(--dsz));
}
.dpad .pad { width: var(--dsz); height: var(--dsz); border-radius: 10px; font-size: calc(var(--dsz) * 0.4); }
.dpad .up    { grid-area: 1 / 2; }
.dpad .left  { grid-area: 2 / 1; }
.dpad .right { grid-area: 2 / 3; }
.dpad .down  { grid-area: 3 / 2; }

/* A / B */
.face {
  --fsz: clamp(46px, 13vmin, 74px);
  display: grid;
  grid-template-columns: repeat(2, var(--fsz));
  grid-template-rows: repeat(2, var(--fsz));
  gap: 6px;
}
.face .pad { width: var(--fsz); height: var(--fsz); border-radius: 50%; font-size: calc(var(--fsz) * 0.34); }
.face .a { grid-area: 1 / 2; background: rgba(91, 140, 255, 0.30); }
.face .b { grid-area: 2 / 1; background: rgba(255, 107, 107, 0.26); }

/* L / R 범퍼 */
.bumper {
  width: clamp(72px, 20vmin, 116px);
  height: clamp(34px, 9vmin, 46px);
  border-radius: 9px;
  font-size: 15px;
}

/* SELECT / START */
.sysbtns {
  grid-area: sys;
  display: flex;
  gap: 16px;
  align-items: center;
  justify-content: center;
  padding: 6px;
}
.sysbtns .pad { padding: 8px 18px; border-radius: 16px; font-size: 12px; letter-spacing: 0.5px; }

/* ── 키보드 매핑 범례 (하단) ──────────────────── */
#keylegend {
  grid-area: legend;
  display: flex;
  flex-wrap: wrap;
  gap: 6px 14px;
  align-items: center;
  justify-content: center;
  padding: 8px 12px calc(8px + env(safe-area-inset-bottom));
  background: rgba(10, 12, 16, 0.55);
  color: var(--muted);
  font-size: 12px;
}
#keylegend span { white-space: nowrap; }
#keylegend b {
  color: var(--text);
  background: var(--panel-2);
  border: 1px solid var(--line);
  border-radius: 5px;
  padding: 1px 6px;
  margin-right: 4px;
  font-weight: 600;
  font-size: 11px;
}

/* ── 라이브러리 ───────────────────────────────── */
#library {
  position: fixed;
  inset: 0;
  overflow-y: auto;
  background: var(--bg);
  padding: max(24px, env(safe-area-inset-top)) 20px calc(40px + env(safe-area-inset-bottom));
  z-index: 40;
  -webkit-overflow-scrolling: touch;
}
body.playing #library { display: none; }

#library header { max-width: 640px; margin: 0 auto 20px; }
#library h1 { margin: 4px 0; font-size: 26px; }
#library .sub { margin: 0; color: var(--muted); font-size: 14px; }

.add { max-width: 640px; margin: 0 auto 20px; }
.upload {
  display: block;
  text-align: center;
  padding: 20px;
  border: 2px dashed var(--line);
  border-radius: 14px;
  color: var(--accent);
  font-weight: 700;
  font-size: 16px;
  cursor: pointer;
  background: var(--panel);
}
.upload:active { background: var(--panel-2); }
.add .hint { text-align: center; color: var(--muted); font-size: 12px; margin: 8px 0 0; }
.add .secondary {
  display: block;
  width: 100%;
  margin-top: 12px;
  padding: 12px;
  background: var(--panel-2);
  color: var(--text);
  border: 1px solid var(--line);
  border-radius: 12px;
  font-size: 14px;
  font-weight: 600;
  cursor: pointer;
}
.add .secondary:active { background: var(--line); }

#rom-list, .shelf-list {
  list-style: none;
  max-width: 640px;
  margin: 0 auto;
  padding: 0;
  display: flex;
  flex-direction: column;
  gap: 8px;
}
#rom-list li {
  display: flex;
  gap: 8px;
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 12px;
  overflow: hidden;
}
#rom-list .rom-play {
  flex: 1;
  text-align: left;
  padding: 16px;
  font-size: 16px;
  font-weight: 600;
  background: none;
  border: none;
  color: var(--text);
  cursor: pointer;
}
#rom-list .rom-play:active { background: var(--panel-2); }
#rom-list .rom-play .cloud {
  color: var(--accent);
  margin-right: 8px;
  font-size: 14px;
}
#rom-list .rom-del {
  width: 52px;
  background: none;
  border: none;
  border-left: 1px solid var(--line);
  color: var(--muted);
  font-size: 16px;
  cursor: pointer;
}
#rom-list .rom-del:active { color: var(--danger); background: var(--panel-2); }
#rom-list .empty {
  display: block;
  text-align: center;
  color: var(--muted);
  padding: 30px 16px;
  border: 1px dashed var(--line);
  border-radius: 12px;
  font-size: 14px;
}

#shelf { max-width: 640px; margin: 24px auto 0; }
#shelf h2 { font-size: 15px; color: var(--muted); margin: 0 0 10px; font-weight: 600; }
.shelf-list li {
  display: flex;
  align-items: center;
  gap: 10px;
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 12px;
  padding: 12px 14px;
}
.shelf-list li span { flex: 1; font-weight: 600; }
.shelf-list button {
  background: var(--accent);
  color: #fff;
  border: none;
  border-radius: 9px;
  padding: 8px 14px;
  font-weight: 700;
  cursor: pointer;
}
.shelf-list button:disabled { opacity: 0.6; }

#library footer {
  max-width: 640px;
  margin: 32px auto 0;
  color: var(--muted);
  font-size: 12px;
  line-height: 1.6;
  text-align: center;
}

/* ── 로딩 / 토스트 ────────────────────────────── */
#loading {
  position: fixed;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  background: var(--bg);
  color: var(--muted);
  font-size: 15px;
  z-index: 100;
  text-align: center;
  padding: 24px;
}
#loading .err { color: var(--text); line-height: 1.7; }
#loading .err small { color: var(--muted); font-size: 13px; }

/* ── 드래그앤드롭 오버레이 ────────────────────── */
#drop {
  position: fixed;
  inset: 0;
  z-index: 90;
  display: none;
  align-items: center;
  justify-content: center;
  padding: 24px;
  background: rgba(17, 19, 24, 0.82);
  backdrop-filter: blur(3px);
  pointer-events: none;      /* 드래그 이벤트는 window 에서 처리 */
}
#drop.show { display: flex; }
#drop .drop-box {
  border: 3px dashed var(--accent);
  border-radius: 20px;
  padding: 44px 56px;
  font-size: 22px;
  font-weight: 700;
  color: var(--text);
  text-align: center;
  line-height: 1.6;
  background: rgba(91, 140, 255, 0.10);
}
#drop .drop-box small { color: var(--muted); font-size: 14px; font-weight: 500; }

/* ── 모달 (설정 / 세이브 공용) ─────────────────── */
.modal {
  position: fixed;
  inset: 0;
  z-index: 80;
  display: none;
  align-items: center;
  justify-content: center;
  padding: 24px;
  background: rgba(0, 0, 0, 0.5);
  backdrop-filter: blur(2px);
}
.modal.show { display: flex; }
.modal-panel {
  width: 100%;
  max-width: 400px;
  max-height: 82vh;
  overflow-y: auto;
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 16px;
  padding: 0 20px 18px;
}
.modal-head {
  position: sticky;
  top: 0;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 14px 0 10px;
  background: var(--panel);
}
.modal-head h2 { margin: 0; font-size: 18px; }
.modal-close {
  background: none;
  border: none;
  color: var(--muted);
  font-size: 18px;
  cursor: pointer;
  padding: 4px 8px;
}
.modal-note { color: var(--muted); font-size: 12px; line-height: 1.6; margin: 2px 0 8px; }
.modal-note b { color: var(--text); }

/* 세이브 슬롯 */
#slot-list { list-style: none; padding: 0; margin: 4px 0 0; display: flex; flex-direction: column; gap: 8px; }
.slot-row {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 10px 12px;
  background: var(--panel-2);
  border: 1px solid var(--line);
  border-radius: 10px;
}
.slot-label { flex: 1; font-size: 14px; font-weight: 600; display: flex; align-items: center; gap: 8px; }
.slot-dot { width: 8px; height: 8px; border-radius: 50%; background: var(--accent); }
.slot-empty { color: var(--muted); font-weight: 400; font-size: 12px; }
.slot-row button {
  border: 1px solid var(--line);
  background: var(--panel);
  color: var(--text);
  border-radius: 8px;
  padding: 7px 12px;
  font-size: 13px;
  font-weight: 600;
  cursor: pointer;
}
.slot-row .slot-save { background: var(--accent); border-color: var(--accent); color: #fff; }
.slot-row button:disabled { opacity: 0.4; cursor: default; }

/* 텍스트 입력 (설정 > 서버 토큰) */
.text-input {
  flex: none;
  width: 160px;
  background: var(--bg);
  color: var(--text);
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 7px 10px;
  font-size: 13px;
}

/* 저장 파일 목록 (서버 동기화 모달) */
.file-list { list-style: none; padding: 0; margin: 4px 0 8px; display: flex; flex-direction: column; gap: 6px; }
.file-list .empty-sm { color: var(--muted); font-size: 13px; padding: 8px 2px; }
.file-row {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 9px 10px;
  background: var(--panel-2);
  border: 1px solid var(--line);
  border-radius: 9px;
}
.file-row .file-name {
  flex: 1;
  font-size: 13px;
  font-weight: 600;
  word-break: break-all;
}
.file-row button {
  flex: none;
  border: 1px solid var(--line);
  background: var(--panel);
  color: var(--text);
  border-radius: 8px;
  padding: 6px 11px;
  font-size: 12px;
  font-weight: 600;
  cursor: pointer;
}
.file-row button:disabled { opacity: 0.5; cursor: default; }
.file-row .danger-btn { color: var(--danger); padding: 6px 9px; }

.settings-cat {
  font-size: 12px;
  color: var(--muted);
  font-weight: 600;
  margin: 10px 0 2px;
}
.settings-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  padding: 12px 2px;
  cursor: pointer;
  border-top: 1px solid var(--line);
}
.settings-row .label { font-size: 15px; }

/* 토글 스위치 (checkbox) */
.toggle {
  appearance: none;
  -webkit-appearance: none;
  flex: none;
  width: 46px;
  height: 26px;
  border-radius: 13px;
  background: var(--line);
  position: relative;
  cursor: pointer;
  transition: background 0.15s;
}
.toggle::after {
  content: "";
  position: absolute;
  top: 3px;
  left: 3px;
  width: 20px;
  height: 20px;
  border-radius: 50%;
  background: #fff;
  transition: transform 0.15s;
}
.toggle:checked { background: var(--accent); }
.toggle:checked::after { transform: translateX(20px); }

#toast {
  position: fixed;
  left: 50%;
  bottom: calc(90px + env(safe-area-inset-bottom));
  transform: translateX(-50%) translateY(10px);
  background: rgba(0,0,0,0.85);
  color: #fff;
  padding: 10px 18px;
  border-radius: 20px;
  font-size: 14px;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.2s, transform 0.2s;
  z-index: 60;
}
#toast.show { opacity: 1; transform: translateX(-50%) translateY(0); }
