:root {
  color-scheme: dark;
  font-family: "DejaVu Sans Mono", "Liberation Mono", monospace;
  background: #02040c;
  color: #e8f7ff;
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }

html, body {
  width: 100%;
  min-height: 100%;
  margin: 0;
  overflow: hidden;
  background:
    radial-gradient(circle at 50% 20%, #111c45 0, #050817 42%, #02040c 76%),
    #02040c;
}

body {
  display: grid;
  place-items: center;
  min-height: 100vh;
  min-height: 100dvh;
  padding:
    max(12px, env(safe-area-inset-top, 0px))
    max(12px, env(safe-area-inset-right, 0px))
    max(12px, env(safe-area-inset-bottom, 0px))
    max(12px, env(safe-area-inset-left, 0px));
  overscroll-behavior: none;
}

#game-shell {
  position: relative;
  width: min(100%, 1040px, calc((100dvh - 164px) * 4 / 3));
  display: grid;
  gap: 8px;
  user-select: none;
}

#game-shell:fullscreen {
  width: 100%;
  height: 100%;
  padding:
    max(8px, env(safe-area-inset-top, 0px))
    max(8px, env(safe-area-inset-right, 0px))
    max(8px, env(safe-area-inset-bottom, 0px))
    max(8px, env(safe-area-inset-left, 0px));
  grid-template-rows: auto auto auto;
  align-content: center;
  justify-items: center;
  background: radial-gradient(circle at 50% 20%, #111c45 0, #050817 48%, #02040c 86%);
}

#game-shell:fullscreen > .topbar,
#game-shell:fullscreen > .controls { width: min(100%, 1040px); }

.topbar {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 8px;
  padding: 8px 14px;
  border: 1px solid #2b4d7c;
  background: linear-gradient(180deg, #101c38, #070d1d);
  box-shadow: 0 0 22px #0b64b744, inset 0 1px #73baff44;
  letter-spacing: .12em;
}

.topbar div { display: flex; justify-content: center; gap: .8em; }
.topbar span { color: #6ea5c6; }
.topbar strong { color: #fff; text-shadow: 0 0 9px #34d8ff; }

#stage-wrap {
  position: relative;
  overflow: hidden;
  border: 2px solid #477eb1;
  background: #02040b;
  box-shadow: 0 0 32px #126ad75c, inset 0 0 24px #000;
  aspect-ratio: 4 / 3;
}

#game {
  display: block;
  width: 100%;
  height: 100%;
  object-fit: contain;
  cursor: crosshair;
  touch-action: none;
}

#crt {
  pointer-events: none;
  position: absolute;
  inset: 0;
  background: repeating-linear-gradient(
    180deg,
    transparent 0,
    transparent 3px,
    rgba(9, 16, 35, .13) 4px
  );
  mix-blend-mode: screen;
  opacity: .38;
  transition: opacity .15s ease;
}

#crt.crt-off { opacity: 0; }

#title-menu {
  position: absolute;
  z-index: 5;
  left: 50%;
  bottom: 6%;
  display: flex;
  justify-content: center;
  gap: 10px;
  width: min(94%, 560px);
  transform: translateX(-50%);
}

#title-menu button,
#title-menu a {
  flex: 1 1 0;
  min-height: 42px;
  border: 1px solid #5e9aca;
  border-radius: 2px;
  padding: 8px 10px;
  color: #dff9ff;
  background: linear-gradient(180deg, rgba(18, 68, 108, .96), rgba(6, 25, 48, .96));
  box-shadow: 0 0 16px #139bd347;
  font: bold 11px/1.15 monospace;
  letter-spacing: .07em;
  text-align: center;
  text-decoration: none;
  cursor: pointer;
  touch-action: manipulation;
}

#title-menu button:hover,
#title-menu a:hover { background: linear-gradient(180deg, #17618e, #0a3458); }

.controls {
  position: relative;
  z-index: 6;
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  align-items: center;
  gap: 8px 22px;
  padding: 6px 12px;
  color: #567a98;
  font-size: 12px;
  letter-spacing: .06em;
}

.controls b { color: #9dc8e7; font-weight: 600; }

#mute,
#music,
#pause-button,
#settings-button,
#game-shell .menu-link,
#fullscreen-button {
  border: 1px solid #31597c;
  border-radius: 2px;
  padding: 5px 10px;
  color: #75dcff;
  background: #081426;
  font: inherit;
  cursor: pointer;
  touch-action: manipulation;
  min-height: 32px;
  text-decoration: none;
}

#mute:hover,
#music:hover,
#pause-button:hover,
#settings-button:hover,
#game-shell .menu-link:hover,
#fullscreen-button:hover { background: #102743; }

#settings-panel {
  position: absolute;
  z-index: 20;
  inset: 0;
  display: grid;
  place-items: center;
  padding: clamp(8px, 2.5vw, 28px);
  overflow: auto;
  background: rgba(1, 4, 13, .88);
  backdrop-filter: blur(8px);
}

.settings-card {
  width: min(100%, 720px);
  padding: clamp(16px, 3vw, 28px);
  border: 1px solid #4b83b6;
  background: linear-gradient(160deg, rgba(13, 29, 58, .98), rgba(4, 9, 22, .98));
  box-shadow: 0 0 42px #0876d761, inset 0 1px #91d6ff4a;
}

.settings-heading { text-align: center; }
.settings-heading p {
  margin: 0 0 4px;
  color: #ff7897;
  font-size: 12px;
  letter-spacing: .24em;
}
.settings-heading h1 {
  margin: 0;
  color: #eefdff;
  font-size: clamp(23px, 4vw, 34px);
  letter-spacing: .12em;
  text-shadow: 0 0 16px #35cfff;
}
.settings-heading > span { color: #82a8c4; font-size: 12px; }

.settings-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 16px 22px;
  margin-top: 22px;
}

.setting {
  display: grid;
  gap: 7px;
  color: #8bc6e7;
  font-size: 12px;
  letter-spacing: .07em;
}
.setting-wide { grid-column: 1 / -1; }
.setting output { float: right; color: #fff; }
.setting select {
  width: 100%;
  min-height: 42px;
  border: 1px solid #31597c;
  border-radius: 2px;
  padding: 8px 10px;
  color: #eefdff;
  background: #081426;
  font: inherit;
}
.setting input[type="range"] {
  width: 100%;
  accent-color: #58d9ff;
}

.toggles {
  align-content: center;
  gap: 9px;
  border-left: 1px solid #244866;
  padding-left: 20px;
}
.toggles label { cursor: pointer; }
.toggles input { accent-color: #58d9ff; }

.settings-note {
  margin: 20px 0 0;
  color: #6e91aa;
  font-size: 10px;
  line-height: 1.5;
  text-align: center;
}

.settings-actions {
  display: flex;
  justify-content: center;
  gap: 10px;
  margin-top: 17px;
}
.settings-actions button {
  min-width: 140px;
  min-height: 42px;
  border: 1px solid #5e9aca;
  border-radius: 2px;
  padding: 8px 18px;
  color: #dff9ff;
  background: #0b2642;
  font: bold 12px/1 monospace;
  letter-spacing: .08em;
  cursor: pointer;
}
#settings-start {
  border-color: #76d9f4;
  background: linear-gradient(180deg, #17608b, #0a385c);
  box-shadow: 0 0 18px #1ab3f34d;
}

.touch-help { display: none; color: #9dc8e7; }

@media (max-width: 700px) {
  body {
    padding:
      max(4px, env(safe-area-inset-top, 0px))
      max(4px, env(safe-area-inset-right, 0px))
      max(4px, env(safe-area-inset-bottom, 0px))
      max(4px, env(safe-area-inset-left, 0px));
  }
  #game-shell { gap: 4px; }
  .topbar { padding: 5px 4px; font-size: 11px; letter-spacing: .02em; }
  .topbar div { gap: .35em; }
  .controls { font-size: 10px; gap: 4px 6px; padding: 3px 2px; }
  .desktop-help { display: none; }
  .touch-help { display: inline; width: 100%; text-align: center; }
  #mute, #music, #pause-button, #settings-button, #game-shell .menu-link, #fullscreen-button { padding: 6px 8px; font-size: 9px; }
  #title-menu { gap: 5px; width: 97%; }
  #title-menu button, #title-menu a { min-height: 36px; padding: 5px 3px; font-size: 8px; letter-spacing: .02em; }
  #settings-panel { position: fixed; width: 100vw; height: 100dvh; }
  .settings-card { padding: 14px; }
  .settings-grid { gap: 11px 14px; margin-top: 15px; }
  .settings-heading > span { font-size: 10px; }
  .settings-note { margin-top: 13px; }
}

/* Portrait phones: the shell fills the whole screen and the stage takes every
   pixel between the status bar and the controls — the game adopts a matching
   portrait logical canvas so there is no letterboxing. Placed after the generic
   phone block so these rules win the cascade. */
@media (max-width: 700px) and (orientation: portrait) {
  body { padding: 0; place-items: stretch; }
  #game-shell {
    width: 100%;
    height: 100vh;
    height: 100dvh;
    grid-template-rows: auto minmax(0, 1fr) auto;
    padding:
      max(2px, env(safe-area-inset-top, 0px))
      max(2px, env(safe-area-inset-right, 0px))
      max(2px, env(safe-area-inset-bottom, 0px))
      max(2px, env(safe-area-inset-left, 0px));
  }
  #stage-wrap {
    aspect-ratio: auto;
    min-height: 0;
    height: 100%;
    border-left-width: 1px;
    border-right-width: 1px;
  }
}

@media (max-width: 520px) {
  .settings-grid { grid-template-columns: 1fr; }
  .setting-wide { grid-column: auto; }
  .toggles {
    grid-template-columns: 1fr 1fr;
    border-left: 0;
    border-top: 1px solid #244866;
    padding: 12px 0 0;
    font-size: 10px;
  }
}

@media (max-height: 560px) and (orientation: landscape) {
  body {
    padding:
      max(2px, env(safe-area-inset-top, 0px))
      max(2px, env(safe-area-inset-right, 0px))
      max(2px, env(safe-area-inset-bottom, 0px))
      max(2px, env(safe-area-inset-left, 0px));
  }
  #game-shell {
    width: min(
      100%,
      calc((100dvh - 90px - env(safe-area-inset-top, 0px) - env(safe-area-inset-bottom, 0px)) * 4 / 3)
    );
    gap: 2px;
  }
  .topbar { padding: 3px 5px; font-size: 9px; }
  .controls { min-height: 26px; padding: 1px; gap: 3px; }
  .touch-help { display: none; }
  #mute, #music, #pause-button, #settings-button, #game-shell .menu-link, #fullscreen-button { min-height: 28px; padding: 4px 6px; font-size: 8px; }
  #title-menu { bottom: 3%; }
  #title-menu button, #title-menu a { min-height: 30px; font-size: 7px; }
  #settings-panel { position: fixed; width: 100vw; height: 100dvh; align-items: start; padding: 4px; }
  .settings-card { width: min(100%, 710px); padding: 8px 12px; }
  .settings-heading p, .settings-heading > span, .settings-note { display: none; }
  .settings-heading h1 { font-size: 17px; }
  .settings-grid { grid-template-columns: 1.35fr 1fr 1fr; gap: 7px 13px; margin-top: 8px; }
  .setting, .toggles { font-size: 9px; gap: 3px; }
  .setting-wide { grid-column: auto; }
  .setting select { min-height: 32px; padding: 3px 5px; font-size: 9px; }
  .toggles { grid-template-columns: 1fr 1fr; border: 0; padding: 0; }
  .settings-actions { margin-top: 7px; }
  .settings-actions button { min-height: 31px; min-width: 112px; padding: 4px 10px; font-size: 9px; }
}
