:root {
  color-scheme: dark;
  --ink: #071014;
  --deep: #0a1c20;
  --panel: rgba(7, 16, 20, .94);
  --steel: #708991;
  --paper: #e5f5ee;
  --cyan: #3ff6d1;
  --cyan-deep: #0d8f85;
  --acid: #e9ff52;
  --orange: #ff7348;
  --violet: #c681ff;
  --line: rgba(120, 226, 209, .28);
}

* { box-sizing: border-box; }
html, body { min-height: 100%; }
body {
  margin: 0;
  display: grid;
  place-items: center;
  padding: 14px;
  overflow-x: hidden;
  background:
    radial-gradient(circle at 50% 10%, rgba(24, 121, 118, .2), transparent 34rem),
    linear-gradient(#071014, #03090b);
  color: var(--paper);
  font: 12px/1.4 "Segoe UI", "Noto Sans KR", system-ui, sans-serif;
}
button, input { font: inherit; }
button { color: inherit; }

#wrap {
  width: min(1120px, 100%);
  border: 1px solid #1e4d50;
  background: #071014;
  box-shadow: 0 24px 90px #000, 0 0 42px rgba(63, 246, 209, .08);
}

.topbar, .bottombar {
  min-height: 48px;
  padding: 8px 12px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 14px;
  background: linear-gradient(180deg, #102a2e, #09171a);
  border-bottom: 1px solid #1e4d50;
}
.bottombar { border: 0; border-top: 1px solid #1e4d50; flex-wrap: wrap; }
.identity { display: grid; }
.identity strong {
  color: var(--cyan);
  font: 900 18px/1 ui-monospace, "Cascadia Mono", monospace;
  letter-spacing: .16em;
}
.identity span, .build-note { color: #7fa2a5; font-size: 10px; letter-spacing: .07em; }
.hud, .health, .toggles { display: flex; align-items: center; gap: 14px; flex-wrap: wrap; }
.hud span { color: #7fa2a5; font: 700 10px/1.2 ui-monospace, monospace; }
.hud b { color: var(--paper); margin-left: 4px; font-size: 12px; }

#stage-box {
  position: relative;
  width: 100%;
  aspect-ratio: 16 / 9;
  overflow: hidden;
  background: #0b252c;
  isolation: isolate;
}
#game {
  display: block;
  width: 100%;
  height: 100%;
  image-rendering: pixelated;
  image-rendering: crisp-edges;
  background: #0b252c;
}
#scanlines {
  position: absolute;
  inset: 0;
  z-index: 5;
  pointer-events: none;
  opacity: .22;
  mix-blend-mode: multiply;
  background: repeating-linear-gradient(0deg, rgba(0,0,0,.25) 0 1px, transparent 1px 3px);
}

#sync-hud {
  position: absolute;
  z-index: 8;
  left: 12px;
  top: 10px;
  width: min(230px, 50%);
  filter: drop-shadow(0 2px 0 #031011);
}
.sync-label { display: flex; justify-content: space-between; color: #b2d4d1; font: 800 9px/1.2 ui-monospace, monospace; }
.sync-label b { color: var(--acid); }
.sync-track { height: 8px; margin-top: 3px; padding: 1px; border: 1px solid #5ca69f; background: #071316; transform: skewX(-10deg); }
.sync-track i { display: block; width: 0; height: 100%; background: linear-gradient(90deg, var(--cyan-deep), var(--cyan), var(--acid)); transition: width .12s linear; }
#module-slots { display: flex; gap: 4px; margin-top: 5px; }
.module-chip { padding: 2px 5px; border: 1px solid rgba(63,246,209,.5); background: rgba(4, 18, 20, .75); color: #b7fff2; font: 700 8px/1.2 ui-monospace, monospace; }

.panel {
  position: absolute;
  z-index: 20;
  inset: 0;
  display: grid;
  align-content: center;
  justify-items: center;
  padding: clamp(18px, 5vw, 54px);
  text-align: center;
  background:
    linear-gradient(90deg, transparent, rgba(7,16,20,.84) 15% 85%, transparent),
    linear-gradient(rgba(7,16,20,.5), rgba(7,16,20,.88));
  backdrop-filter: blur(2px);
}
.panel[hidden] { display: none; }
.eyebrow { margin: 0 0 5px; color: var(--cyan); font: 800 10px/1 ui-monospace, monospace; letter-spacing: .22em; }
.panel h1 {
  margin: 0;
  color: #effff9;
  font: 950 clamp(38px, 10vw, 80px)/.95 ui-monospace, "Cascadia Mono", monospace;
  letter-spacing: .09em;
  text-shadow: 4px 4px 0 #0b7f77, 8px 8px 0 #061517;
}
.panel h2 { margin: 2px 0 4px; font-size: clamp(22px, 4vw, 38px); letter-spacing: -.04em; }
.lead { max-width: 650px; margin: 10px auto 16px; color: #bed3d1; font-size: clamp(12px, 1.7vw, 16px); }
.keys { margin: 9px 0 0; color: #79999b; font-size: clamp(9px, 1.2vw, 12px); }
.audio-readout {
  width: min(690px, 100%);
  display: grid;
  grid-template-columns: auto auto 1fr auto;
  align-items: center;
  gap: 10px;
  margin: 0 0 10px;
  padding: 8px 11px;
  border: 1px solid #376a68;
  background: linear-gradient(90deg, rgba(14,61,58,.92), rgba(8,29,33,.92));
  text-align: left;
}
.audio-readout b { color: var(--acid); font: 900 11px/1 ui-monospace, monospace; letter-spacing: .05em; }
.audio-readout small { color: #84aaa8; font: 700 9px/1.3 ui-monospace, monospace; }
.audio-readout em { padding:4px 6px;border:1px solid #9a684c;color:#ffad7e;background:#2b1a15;font:800 7px/1 ui-monospace,monospace;letter-spacing:.06em;white-space:nowrap }
.audio-bars { display:flex; align-items:flex-end; gap:2px; width:25px; height:14px; }
.audio-bars i { width:3px; height:35%; background:var(--cyan); animation:audio-meter .72s ease-in-out infinite alternate; }
.audio-bars i:nth-child(2){height:78%;animation-delay:-.18s}.audio-bars i:nth-child(3){height:52%;animation-delay:-.4s}.audio-bars i:nth-child(4){height:100%;animation-delay:-.27s}.audio-bars i:nth-child(5){height:64%;animation-delay:-.52s}
@keyframes audio-meter { to { height:100%; filter:brightness(1.35); } }
#result-card{width:min(690px,100%);display:flex;justify-content:space-between;gap:10px;margin:0 0 12px;padding:9px 11px;border:1px solid var(--cyan);background:#082725;text-align:left;font:800 9px/1.4 ui-monospace,monospace}
#result-card b{color:var(--acid)}#result-card span{color:#9ebdbb}
.mode-grid, .route-grid, .card-grid { width: min(690px, 100%); display: grid; grid-template-columns: repeat(2, 1fr); gap: 10px; }
.mobile-solo .mode-grid { grid-template-columns: minmax(0, 1fr); }
.mobile-solo #coop-start { display: none !important; }
.challenge-box {
  width: min(690px, 100%);
  display: grid;
  grid-template-columns: auto 1fr auto auto;
  align-items: center;
  gap: 6px;
  margin-top: 10px;
  padding: 7px;
  border: 1px solid rgba(63,246,209,.22);
  background: rgba(3,16,19,.72);
}
.challenge-box label { padding: 0 5px; color: var(--cyan); font: 800 9px/1 ui-monospace,monospace; letter-spacing: .08em; }
.challenge-box input { min-width: 0; height: 30px; border: 1px solid #397675; background:#071416; color:var(--acid); padding:0 9px; font:800 11px ui-monospace,monospace; }
.challenge-box button { min-height:30px; padding:0 9px; box-shadow:none; font-size:9px; }
.panel button {
  min-height: 64px;
  border: 1px solid #397675;
  background: linear-gradient(180deg, rgba(18, 55, 59, .94), rgba(7, 24, 27, .96));
  box-shadow: 0 5px 0 #031012;
  cursor: pointer;
  transition: transform .08s, filter .08s, border-color .08s;
}
.panel button:hover, .panel button:focus-visible { filter: brightness(1.25); border-color: var(--cyan); outline: 2px solid rgba(63, 246, 209, .2); outline-offset: 2px; }
.panel button.pad-selected { border-color:var(--acid); outline:2px solid rgba(233,255,82,.35); outline-offset:2px; filter:brightness(1.18) }
.panel button:active { transform: translateY(3px); box-shadow: 0 2px 0 #031012; }
.mode-grid button { display: grid; place-content: center; gap: 3px; }
.mode-grid b { font: 900 17px/1 ui-monospace, monospace; color: #d9efec; }
.mode-grid span, .route-grid span { color: #86a6a7; font-size: 10px; }
.mode-grid .primary { border-color: var(--cyan); background: linear-gradient(180deg, #17665e, #0b332f); }
.mode-grid .primary b { color: var(--acid); }
.card-grid { grid-template-columns: repeat(3, 1fr); }
.module-card { min-height: 112px !important; display: grid; align-content: center; gap: 6px; padding: 12px; }
.module-card small, .route-grid small { color: var(--cyan); font: 800 9px/1 ui-monospace, monospace; letter-spacing: .1em; }
.module-card b, .route-grid b { display: block; color: var(--paper); font-size: 16px; }
.module-card span { color: #8da8a7; font-size: 10px; }
.module-card i { color: var(--acid); font: 700 9px/1 ui-monospace, monospace; font-style: normal; }
.route-grid button { min-height: 96px; display: grid; align-content: center; gap: 6px; padding: 12px; }
.route-grid .danger { border-color: #a44839; background: linear-gradient(180deg, #5c2822, #251311); }
.route-grid .danger small { color: var(--orange); }

#toast {
  position: absolute;
  z-index: 30;
  left: 50%;
  top: 18%;
  transform: translate(-50%, -10px);
  opacity: 0;
  pointer-events: none;
  padding: 6px 10px;
  border: 1px solid var(--cyan);
  background: rgba(2, 13, 15, .9);
  color: #e5fff9;
  font: 900 11px/1 ui-monospace, monospace;
  letter-spacing: .08em;
  transition: .16s ease;
}
#toast.show { opacity: 1; transform: translate(-50%, 0); }
#moment-mark {
  position:absolute;
  z-index:31;
  right:12px;
  top:40px;
  min-width:112px;
  padding:7px 9px;
  border-left:3px solid var(--acid);
  background:rgba(3,16,19,.9);
  box-shadow:0 3px 0 #020a0b;
}
#moment-mark[hidden]{display:none}
#moment-mark small{display:block;color:#7b9d9d;font:700 7px/1 ui-monospace,monospace;letter-spacing:.12em}
#moment-mark b{color:var(--acid);font:900 9px/1.4 ui-monospace,monospace}

#touch-controls {
  position: absolute;
  z-index: 15;
  top: auto;
  right: 10px;
  bottom: 10px;
  left: 10px;
  inset: auto 10px 10px;
  display: none;
  justify-content: space-between;
  pointer-events: none;
}
.touch-move, .touch-action { display: flex; gap: 6px; }
#touch-controls button {
  width: 54px;
  min-height: 42px;
  pointer-events: auto;
  border: 1px solid rgba(63,246,209,.5);
  border-radius: 6px;
  background: rgba(3,16,19,.62);
  color: #d7fff8;
  font: 800 9px/1 ui-monospace, monospace;
  touch-action: none;
}
#touch-controls .sync { color: var(--acid); border-color: rgba(233,255,82,.6); }

.health i {
  display: inline-block;
  width: 66px;
  height: 7px;
  vertical-align: middle;
  margin-left: 5px;
  border: 1px solid #527073;
  background: linear-gradient(90deg, var(--cyan) var(--hp, 100%), #17292b var(--hp, 100%));
}
.health .p1 { color: var(--cyan); font-weight: 800; }
.health .p2 { color: var(--violet); font-weight: 800; }
.toggles { color: #88a4a4; font-size: 10px; }
.toggles label { white-space: nowrap; }
.toggles input[type="range"] { width: 56px; vertical-align: middle; accent-color: var(--cyan); }
.toggles input { accent-color: var(--cyan); }
.toggles button { border:1px solid #397675;background:#10282b;color:#9ed1ce;padding:4px 7px;font-size:9px;cursor:pointer }
.audio-toggle { border:1px solid #397675;background:#10282b;color:var(--cyan);padding:4px 7px;font:800 9px/1 ui-monospace,monospace;cursor:pointer;white-space:nowrap }
.audio-toggle[aria-pressed="true"] { color:#ff9b7c;border-color:#8d4b3b;background:#2a1715 }
#fps { min-width: 45px; color: #5f7d7f; font: 10px/1 ui-monospace, monospace; }
.build-note { padding: 7px 12px; background: #050d10; border-top: 1px solid #102d30; }
.build-note b { color: var(--acid); }
.build-note a { float: right; color: var(--cyan); }

body.sync-active #stage-box { box-shadow: inset 0 0 35px rgba(63,246,209,.28); }
body.sync-active #scanlines { opacity: .08; }
body.hit-flash #stage-box::after {
  content: "";
  position: absolute;
  z-index: 12;
  inset: 0;
  pointer-events: none;
  background: rgba(255,255,255,.35);
}

/* ===== Mobile SCRAPMAN: auto-landscape, game-only (no rotate popup, no app chrome) ===== */
@media (pointer: coarse), (max-width: 1200px), (hover: none) {
  html, body {
    width: 100%;
    height: 100%;
    min-height: 100%;
    min-height: 100dvh;
    min-height: 100svh;
    overflow: hidden;
    overscroll-behavior: none;
    touch-action: none;
    background: #03090b;
  }
  body {
    padding: 0;
    margin: 0;
    display: block;
    place-items: unset;
  }
  #wrap {
    width: 100%;
    max-width: none;
    min-height: 100dvh;
    min-height: 100svh;
    height: 100dvh;
    height: 100svh;
    border: 0;
    display: flex;
    flex-direction: column;
    box-shadow: none;
    position: relative;
    overflow: hidden;
  }
  /* App chrome off — only game */
  .identity span,
  .hud span:nth-child(3),
  .toggles,
  .build-note,
  #touch-controls,
  .bottombar,
  .keys,
  #cabin-bar { display: none !important; }
  .panel { padding: 12px; }
  .audio-readout { display: none; }
  body.scrap-game-only .topbar {
    position: fixed !important;
    top: 0;
    left: 0;
    right: 0;
    z-index: 45;
    min-height: 0 !important;
    padding: 2px 8px !important;
    background: linear-gradient(180deg, rgba(3,9,11,.5), transparent);
    border: 0 !important;
    pointer-events: none;
  }
  body.scrap-game-only .topbar .identity strong { font-size: 11px; }
  body.scrap-game-only .topbar .hud { gap: 6px; }
  body.scrap-game-only .topbar .hud span { font-size: 9px; }
  body.scrap-game-only #wrap {
    justify-content: center;
    align-items: center;
    padding: 0;
  }
  body.scrap-game-only #stage-box {
    flex: 0 0 auto;
    width: 100% !important;
    max-width: 100% !important;
    height: 100% !important;
    max-height: 100% !important;
    aspect-ratio: auto;
    margin: 0 !important;
    border: 0;
  }
  body.scrap-game-only #game {
    width: 100%;
    height: 100%;
  }
  body.scrap-game-only .cab-touch {
    position: absolute !important;
    inset: auto 0 0 0 !important;
    z-index: 30;
    background: linear-gradient(180deg, transparent, rgba(0,0,0,.32));
  }
  /* Never show legacy rotate dialog */
  #cab-orientation-gate { display: none !important; }
}

/*
 * Physical portrait → logical landscape by CSS rotate.
 * Game always plays as horizontal; no "please rotate" modal.
 */
html.cab-portrait-rotated,
body.cab-portrait-rotated {
  width: 100%;
  height: 100%;
  overflow: hidden;
  background: #03090b;
}
body.cab-portrait-rotated #wrap {
  position: fixed !important;
  /* After 90° CW: width becomes visual height, height becomes visual width */
  width: 100dvh !important;
  height: 100dvw !important;
  max-width: none !important;
  top: 0 !important;
  left: 100dvw !important;
  margin: 0 !important;
  transform: rotate(90deg);
  transform-origin: top left;
  z-index: 1;
}
body.cab-portrait-rotated.scrap-game-only #stage-box {
  width: 100% !important;
  height: 100% !important;
  max-width: none !important;
  max-height: none !important;
}
body.cab-portrait-rotated .topbar {
  /* stays over game after rotation */
  width: 100%;
}

/* Fullscreen: game only, no browser chrome */
:fullscreen body,
body:fullscreen,
#wrap:fullscreen {
  background: #03090b;
}
body.scrap-game-only:fullscreen #wrap,
body.scrap-game-only #wrap:fullscreen,
#wrap:fullscreen {
  width: 100vw !important;
  height: 100vh !important;
  max-width: none !important;
}
body.scrap-game-only:fullscreen #stage-box,
#wrap:fullscreen #stage-box {
  width: 100% !important;
  height: 100% !important;
  max-width: none !important;
  max-height: none !important;
}

/* Compact title on short heights */
@media (max-height: 480px), (max-width: 480px) {
  .panel h1 { font-size: clamp(28px, 8vmin, 48px); }
  .panel .lead { font-size: 12px; margin: 6px auto 10px; }
  .mode-grid button { min-height: 48px; }
  .challenge-box { margin-top: 6px; }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { scroll-behavior: auto !important; transition-duration: .01ms !important; animation-duration: .01ms !important; }
}
