:root {
  --bg: #0d0f14;
  --panel: #161a22;
  --panel-border: #262c38;
  --accent: #3fb8ff;
  --accent-dim: #1f5a78;
  --text: #e8e2d0;
  --text-dim: #8a93a3;
  --danger: #c62828;
  --btn-face: #232937;
  --btn-face-active: #323b4f;
  color-scheme: dark;
}

* {
  box-sizing: border-box;
  -webkit-tap-highlight-color: transparent;
  user-select: none;
  -webkit-user-select: none;
}

html, body {
  height: 100%;
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  overscroll-behavior: none;
  touch-action: none;
}

#app {
  display: flex;
  flex-direction: column;
  height: 100dvh;
  width: 100vw;
  padding: env(safe-area-inset-top) env(safe-area-inset-right) env(safe-area-inset-bottom) env(safe-area-inset-left);
  overflow: hidden;
}

/* ---------- Header ---------- */
#header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 8px 14px;
  flex: 0 0 auto;
  border-bottom: 1px solid var(--panel-border);
}

#title {
  font-size: 18px;
  letter-spacing: 0.12em;
  margin: 0;
  font-weight: 800;
  color: var(--accent);
  text-shadow: 0 0 12px rgba(63, 184, 255, 0.35);
}

#header-actions {
  display: flex;
  gap: 8px;
}

.icon-btn {
  background: var(--btn-face);
  border: 1px solid var(--panel-border);
  color: var(--text);
  font-size: 16px;
  width: 36px;
  height: 36px;
  border-radius: 8px;
  display: flex;
  align-items: center;
  justify-content: center;
}

.icon-btn:active,
.icon-btn.active {
  background: var(--btn-face-active);
}

/* ---------- Picker panel ---------- */
#picker-panel.panel {
  flex: 0 0 auto;
  padding: 14px;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 10px;
  border-bottom: 1px solid var(--panel-border);
  transition: max-height 0.25s ease, opacity 0.25s ease, padding 0.25s ease;
}

#picker-panel.collapsed {
  max-height: 0;
  padding-top: 0;
  padding-bottom: 0;
  opacity: 0;
  overflow: hidden;
  border-bottom: none;
  pointer-events: none;
}

.picker-hint {
  margin: 0;
  color: var(--text-dim);
  font-size: 13px;
  text-align: center;
}

.btn {
  display: inline-block;
  padding: 10px 20px;
  border-radius: 10px;
  border: 1px solid var(--panel-border);
  font-size: 15px;
  font-weight: 600;
  cursor: pointer;
  text-align: center;
}

.btn-primary {
  background: var(--accent);
  color: #06202b;
  border-color: var(--accent);
}

.btn-primary:active {
  filter: brightness(0.9);
}

.btn-secondary {
  background: var(--btn-face);
  color: var(--text);
}

.rom-status {
  margin: 0;
  font-size: 12px;
  color: var(--text-dim);
  min-height: 14px;
  text-align: center;
}

.saved-state-row {
  display: flex;
  justify-content: center;
}

.saved-state-row[hidden] {
  display: none;
}

/* ---------- Game canvas area ---------- */
#game-area {
  flex: 1 1 auto;
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: 0;
  padding: 8px;
  position: relative;
}

#canvas-wrap {
  position: relative;
  display: flex;
  align-items: center;
  justify-content: center;
  max-width: 100%;
  max-height: 100%;
}

#screen {
  background: #000;
  image-rendering: pixelated;
  image-rendering: -webkit-optimize-contrast;
  image-rendering: crisp-edges;
  border: 2px solid var(--panel-border);
  border-radius: 4px;
  max-width: 100%;
  max-height: 100%;
  display: block;
}

#no-rom-overlay {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  color: var(--text-dim);
  font-size: 13px;
  pointer-events: none;
}

#no-rom-overlay.hidden {
  display: none;
}

/* ---------- Controls ---------- */
#controls {
  flex: 0 0 auto;
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto minmax(0, 1fr);
  align-items: center;
  gap: 4px;
  padding: 10px 12px calc(10px + env(safe-area-inset-bottom));
}

.control-zone {
  display: flex;
  align-items: center;
  justify-content: center;
}

/* D-pad */
#dpad-zone {
  justify-content: flex-start;
}

#dpad {
  position: relative;
  width: 156px;
  height: 156px;
  touch-action: none;
}

.dpad-btn {
  position: absolute;
  background: var(--btn-face);
  border: 1px solid var(--panel-border);
  color: var(--text-dim);
  touch-action: none;
}

.dpad-btn.pressed {
  background: var(--accent-dim);
  color: var(--text);
}

.dpad-btn[data-dir="up"] {
  top: 0;
  left: 52px;
  width: 52px;
  height: 52px;
  border-radius: 8px 8px 0 0;
}

.dpad-btn[data-dir="down"] {
  bottom: 0;
  left: 52px;
  width: 52px;
  height: 52px;
  border-radius: 0 0 8px 8px;
}

.dpad-btn[data-dir="left"] {
  top: 52px;
  left: 0;
  width: 52px;
  height: 52px;
  border-radius: 8px 0 0 8px;
}

.dpad-btn[data-dir="right"] {
  top: 52px;
  right: 0;
  width: 52px;
  height: 52px;
  border-radius: 0 8px 8px 0;
}

#dpad-center {
  position: absolute;
  top: 52px;
  left: 52px;
  width: 52px;
  height: 52px;
  background: var(--btn-face);
  border: 1px solid var(--panel-border);
  pointer-events: none;
}

/* Center zone: select/start + save/load */
#center-zone {
  flex-direction: column;
  gap: 8px;
}

.sys-btn {
  background: var(--btn-face);
  border: 1px solid var(--panel-border);
  color: var(--text-dim);
  font-size: 11px;
  font-weight: 700;
  letter-spacing: 0.05em;
  border-radius: 20px;
  padding: 8px 14px;
  min-width: 64px;
  touch-action: none;
}

.sys-btn.pressed {
  background: var(--accent-dim);
  color: var(--text);
}

#state-buttons {
  display: flex;
  gap: 6px;
  margin-top: 4px;
}

.sys-btn.small {
  font-size: 10px;
  padding: 6px 10px;
  min-width: 0;
}

/* A/B buttons */
#ab-zone {
  justify-content: flex-end;
  position: relative;
  height: 156px;
}

.ab-btn {
  position: absolute;
  width: 68px;
  height: 68px;
  border-radius: 50%;
  background: var(--btn-face);
  border: 1px solid var(--panel-border);
  color: var(--text);
  font-size: 20px;
  font-weight: 800;
  touch-action: none;
}

.ab-btn.pressed {
  background: var(--accent);
  color: #06202b;
}

#btn-b {
  right: 78px;
  top: 66px;
}

#btn-a {
  right: 0;
  top: 22px;
}

/* ---------- Toast ---------- */
#toast {
  position: fixed;
  bottom: calc(12px + env(safe-area-inset-bottom));
  left: 50%;
  transform: translateX(-50%);
  background: rgba(20, 24, 32, 0.95);
  border: 1px solid var(--panel-border);
  color: var(--text);
  padding: 8px 16px;
  border-radius: 20px;
  font-size: 13px;
  margin: 0;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.2s ease;
  z-index: 50;
}

#toast.visible {
  opacity: 1;
}

/* ---------- Layout: narrow portrait phones ---------- */
/* Shrink the D-pad and A/B cluster so both side columns fit inside their
   1fr grid track without overlapping the center Select/Start/Save/Load
   column. Sized so 2 x 128px (pad/ab) + center column comfortably fits
   360-430px wide phones in portrait. */
@media (orientation: portrait) and (max-width: 480px) {
  #controls {
    padding: 8px 8px calc(8px + env(safe-area-inset-bottom));
    gap: 0;
  }

  #dpad {
    width: 128px;
    height: 128px;
  }

  .dpad-btn[data-dir="up"],
  .dpad-btn[data-dir="down"] {
    left: 42px;
    width: 44px;
    height: 44px;
  }

  .dpad-btn[data-dir="left"],
  .dpad-btn[data-dir="right"] {
    top: 42px;
    width: 44px;
    height: 44px;
  }

  #dpad-center {
    top: 42px;
    left: 42px;
    width: 44px;
    height: 44px;
  }

  #ab-zone {
    height: 128px;
    width: 100%;
    position: relative;
  }

  .ab-btn {
    width: 56px;
    height: 56px;
    font-size: 17px;
  }

  #btn-b {
    right: 62px;
    top: 58px;
  }

  #btn-a {
    right: 4px;
    top: 14px;
  }

  #center-zone {
    gap: 6px;
  }

  .sys-btn {
    padding: 6px 10px;
    font-size: 10px;
    min-width: 52px;
  }

  #state-buttons {
    margin-top: 2px;
  }
}

/* ---------- Layout: landscape (primary play orientation) ---------- */
@media (orientation: landscape) {
  #app {
    display: grid;
    grid-template-columns: 1fr;
    grid-template-rows: auto 1fr auto;
  }

  #header {
    padding: 4px 12px;
  }

  #picker-panel.panel {
    padding: 8px 14px;
  }

  #game-area {
    padding: 4px 8px;
  }

  #controls {
    padding: 6px 16px calc(6px + env(safe-area-inset-bottom));
  }

  #dpad {
    width: 132px;
    height: 132px;
  }

  .dpad-btn[data-dir="up"],
  .dpad-btn[data-dir="down"] {
    left: 44px;
    width: 44px;
    height: 44px;
  }

  .dpad-btn[data-dir="left"],
  .dpad-btn[data-dir="right"] {
    top: 44px;
    width: 44px;
    height: 44px;
  }

  #dpad-center {
    top: 44px;
    left: 44px;
    width: 44px;
    height: 44px;
  }

  #ab-zone {
    height: 132px;
  }

  .ab-btn {
    width: 58px;
    height: 58px;
    font-size: 18px;
  }

  #btn-b {
    right: 66px;
    top: 58px;
  }

  #btn-a {
    right: 0;
    top: 20px;
  }
}

/* Very short landscape screens (small phones): shrink further */
@media (orientation: landscape) and (max-height: 400px) {
  #title {
    font-size: 15px;
  }

  #dpad {
    width: 108px;
    height: 108px;
  }

  .dpad-btn[data-dir="up"],
  .dpad-btn[data-dir="down"] {
    left: 36px;
    width: 36px;
    height: 36px;
  }

  .dpad-btn[data-dir="left"],
  .dpad-btn[data-dir="right"] {
    top: 36px;
    width: 36px;
    height: 36px;
  }

  #dpad-center {
    top: 36px;
    left: 36px;
    width: 36px;
    height: 36px;
  }

  #ab-zone {
    height: 108px;
  }

  .ab-btn {
    width: 48px;
    height: 48px;
    font-size: 16px;
  }

  #btn-b {
    right: 54px;
    top: 46px;
  }

  #btn-a {
    right: 0;
    top: 16px;
  }

  .sys-btn {
    padding: 5px 10px;
    font-size: 10px;
    min-width: 48px;
  }
}
