:root {
  color-scheme: dark;
  --bg: #09111a;
  --bg-soft: rgba(10, 22, 34, 0.82);
  --panel: rgba(19, 36, 52, 0.84);
  --line: rgba(145, 187, 205, 0.18);
  --text: #ebf4f1;
  --muted: #93a8ae;
  --accent: #ffb84d;
  --accent-2: #62d0a5;
  --danger: #f45d6a;
  --shadow: 0 20px 60px rgba(0, 0, 0, 0.35);
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-right: env(safe-area-inset-right, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  --safe-left: env(safe-area-inset-left, 0px);
  --app-height: 100vh;
  --bottom-ui-lift: max(92px, calc(var(--safe-bottom) + 56px));
  --hud-top-gap: max(12px, calc(var(--safe-top) + 12px));
  --screen-edge-gap: 20px;
}

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

html,
body {
  margin: 0;
  min-height: 100%;
  background:
    radial-gradient(circle at top, rgba(79, 164, 140, 0.2), transparent 30%),
    linear-gradient(180deg, #0a1622 0%, #09111a 55%, #060b12 100%);
  color: var(--text);
  font-family: "Trebuchet MS", "Helvetica Neue", sans-serif;
  overflow: hidden;
}

body {
  touch-action: none;
}

.app-shell {
  position: relative;
  width: 100vw;
  height: var(--app-height);
  min-height: 100vh;
  min-height: 100dvh;
  overflow: hidden;
}

.screen {
  position: absolute;
  inset: 0;
  display: none;
}

.screen.active {
  display: block;
}

#start-screen {
  display: none;
  align-items: center;
  justify-content: center;
  padding: calc(24px + var(--safe-top)) calc(18px + var(--safe-right)) calc(24px + var(--safe-bottom)) calc(18px + var(--safe-left));
}

#start-screen.active {
  display: flex;
}

.hero-card,
.dialog {
  width: min(100%, 480px);
  background: linear-gradient(180deg, rgba(17, 31, 46, 0.98), rgba(9, 17, 26, 0.94));
  border: 1px solid var(--line);
  box-shadow: var(--shadow);
  border-radius: 28px;
  padding: 24px;
  backdrop-filter: blur(10px);
}

.eyebrow {
  margin: 0 0 10px;
  color: var(--accent);
  font-size: 12px;
  letter-spacing: 0.24em;
}

h1,
h2 {
  margin: 0;
  font-weight: 800;
  letter-spacing: 0.03em;
}

h1 {
  font-size: clamp(40px, 12vw, 64px);
}

h2 {
  font-size: 32px;
}

.subtitle,
.tips,
.save-summary {
  color: var(--muted);
  line-height: 1.5;
}

.panel {
  margin: 22px 0 18px;
  display: grid;
  gap: 14px;
}

.audio-settings {
  display: grid;
  gap: 12px;
  padding: 14px;
  border-radius: 20px;
  background: rgba(8, 16, 24, 0.52);
  border: 1px solid rgba(255, 255, 255, 0.06);
}

.audio-heading {
  display: flex;
  justify-content: space-between;
  gap: 12px;
  align-items: baseline;
  color: #dce7ea;
}

.audio-heading small {
  color: var(--muted);
  text-align: right;
}

.field {
  display: grid;
  gap: 6px;
}

.field span {
  font-size: 14px;
  color: #c1d0d6;
}

select,
button,
input {
  font: inherit;
}

select,
button {
  border: 0;
  border-radius: 16px;
}

select {
  width: 100%;
  padding: 14px 16px;
  color: var(--text);
  background: var(--panel);
  border: 1px solid var(--line);
}

.toggle {
  grid-template-columns: 1fr auto;
  align-items: center;
}

.toggle input {
  width: 24px;
  height: 24px;
}

.range-field {
  gap: 8px;
}

.range-field span {
  display: flex;
  justify-content: space-between;
  gap: 8px;
  align-items: center;
}

input[type="range"] {
  width: 100%;
  accent-color: var(--accent);
}

.actions {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 12px;
  margin-top: 18px;
}

button {
  min-height: 52px;
  padding: 0 18px;
  color: var(--text);
  background: rgba(94, 117, 132, 0.2);
  border: 1px solid rgba(255, 255, 255, 0.08);
}

button.primary {
  color: #111;
  background: linear-gradient(135deg, #ffcd73, var(--accent));
}

#game-screen {
  background:
    radial-gradient(circle at top center, rgba(74, 177, 143, 0.09), transparent 28%),
    linear-gradient(180deg, rgba(255, 255, 255, 0.02), transparent 40%);
}

#game-screen {
  padding:
    calc(10px + var(--safe-top))
    calc(10px + var(--safe-right))
    calc(10px + var(--safe-bottom))
    calc(10px + var(--safe-left));
}

.game-layout {
  width: 100%;
  height: 100%;
  display: grid;
  grid-template-rows: auto minmax(0, 1fr) auto;
  gap: 10px;
}

.status-panel,
.controls-panel {
  min-width: 0;
}

#game-screen .top-status {
  display: grid;
  gap: 10px;
}

#game-screen .hud {
  position: static;
  inset: auto;
  transform: none;
  width: auto;
  max-width: none;
}

#game-screen .status-line {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 10px;
}

#game-screen .primary-status {
  justify-content: space-between;
}

#game-screen .secondary-status {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto;
  gap: 10px;
  align-items: center;
}

#game-screen .secondary-status .bar-group {
  min-width: 0;
}

#game-screen .secondary-status .boss {
  grid-column: 1 / -1;
}

.game-stage {
  min-height: 0;
  display: grid;
  place-items: center;
}

.canvas-shell {
  position: relative;
  width: min(100%, calc((var(--app-height) - 260px) * 9 / 14));
  max-width: 100%;
  aspect-ratio: 9 / 14;
  max-height: 100%;
  border-radius: 28px;
  overflow: hidden;
  border: 1px solid rgba(255, 255, 255, 0.08);
  background:
    linear-gradient(180deg, rgba(255, 255, 255, 0.04), rgba(255, 255, 255, 0.01)),
    rgba(5, 10, 16, 0.82);
  box-shadow: 0 22px 48px rgba(0, 0, 0, 0.28);
}

.side-panel {
  display: none;
}

#game-screen .bottom-controls {
  min-height: 0;
}

#game-screen .controls,
#game-screen .controls-bottom {
  position: static;
  inset: auto;
  bottom: auto;
  padding: 0;
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 16px;
}

#game-screen .control-pad,
#game-screen .right-controls {
  flex-shrink: 0;
}

#game-canvas {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
}

.hud {
  position: absolute;
  z-index: 10;
  display: flex;
  gap: 8px;
}

.top-left {
  top: var(--hud-top-gap);
  left: calc(12px + var(--safe-left));
  flex-wrap: wrap;
  max-width: 54vw;
}

.top-right {
  top: var(--hud-top-gap);
  right: calc(12px + var(--safe-right));
}

.center-top {
  top: calc(72px + var(--safe-top));
  left: 50%;
  transform: translateX(-50%);
  width: min(86vw, 520px);
  flex-direction: column;
}

.hud-chip,
.hud-btn {
  min-height: 36px;
  padding: 0 12px;
  display: inline-flex;
  align-items: center;
  border-radius: 999px;
  background: rgba(7, 13, 20, 0.62);
  border: 1px solid rgba(255, 255, 255, 0.08);
  backdrop-filter: blur(8px);
}

.bar-group {
  display: grid;
  grid-template-columns: 48px 1fr;
  align-items: center;
  gap: 10px;
  padding: 8px 12px;
  border-radius: 999px;
  background: rgba(7, 13, 20, 0.55);
  border: 1px solid rgba(255, 255, 255, 0.08);
}

.bar-group.boss.hidden {
  display: none;
}

.bar {
  height: 12px;
  background: rgba(255, 255, 255, 0.08);
  border-radius: 999px;
  overflow: hidden;
}

.bar-fill {
  height: 100%;
  width: 50%;
  transition: width 0.18s ease;
}

.bar-fill.player {
  background: linear-gradient(90deg, #6ff3b6, var(--accent-2));
}

.bar-fill.boss {
  background: linear-gradient(90deg, #ff8d6e, var(--danger));
}

.controls {
  position: absolute;
  inset-inline: 0;
  bottom: var(--bottom-ui-lift);
  display: flex;
  justify-content: space-between;
  align-items: flex-end;
  padding: 0 calc(var(--screen-edge-gap) + var(--safe-right)) 0 calc(var(--screen-edge-gap) + var(--safe-left));
  z-index: 20;
  pointer-events: none;
}

.control-pad,
.shoot-btn,
.skill-btn {
  pointer-events: auto;
}

.control-pad {
  position: relative;
  width: clamp(124px, 30vw, 168px);
  height: clamp(124px, 30vw, 168px);
  border-radius: 50%;
  background: rgba(10, 18, 26, 0.42);
  border: 1px solid rgba(255, 255, 255, 0.08);
  backdrop-filter: blur(8px);
  box-shadow: 0 16px 36px rgba(0, 0, 0, 0.22);
}

.knob {
  position: absolute;
  left: 50%;
  top: 50%;
  width: 44%;
  height: 44%;
  border-radius: 50%;
  transform: translate(-50%, -50%);
  background: radial-gradient(circle at 30% 30%, #d8f8e7, #4dbd90 68%);
  box-shadow: inset 0 -8px 16px rgba(0, 0, 0, 0.22);
}

.right-controls {
  display: grid;
  justify-items: end;
  gap: 14px;
}

.shoot-btn,
.skill-btn {
  width: clamp(104px, 25vw, 132px);
  min-height: 62px;
  padding: 0 16px;
  border-radius: 22px;
  font-weight: 700;
}

.shoot-btn {
  background: linear-gradient(135deg, #ffdd8b, #f49d4f);
  color: #23180a;
}

.skill-btn {
  background: rgba(91, 183, 207, 0.24);
}

.overlay {
  position: absolute;
  inset: 0;
  z-index: 50;
  display: grid;
  place-items: center;
  padding: 20px;
  background: rgba(4, 7, 12, 0.72);
  backdrop-filter: blur(10px);
}

.overlay.hidden {
  display: none;
}

.upgrade-options {
  display: grid;
  gap: 12px;
  margin-top: 20px;
}

.upgrade-card {
  text-align: left;
  padding: 16px;
  border-radius: 18px;
  background: rgba(255, 255, 255, 0.04);
}

.upgrade-card strong,
.result-grid strong {
  display: block;
  margin-top: 6px;
  font-size: 20px;
}

.upgrade-card small,
.result-grid span {
  color: var(--muted);
}

.result-grid {
  margin: 18px 0 20px;
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 14px;
}

.result-grid > div {
  padding: 14px;
  border-radius: 18px;
  background: rgba(255, 255, 255, 0.04);
}

@media (orientation: landscape) {
  #game-screen {
    padding:
      calc(8px + var(--safe-top))
      calc(8px + var(--safe-right))
      calc(8px + var(--safe-bottom))
      calc(8px + var(--safe-left));
  }

  .game-layout {
    grid-template-columns: minmax(176px, 22vw) minmax(0, 1fr) minmax(176px, 22vw);
    grid-template-rows: auto minmax(0, 1fr);
    gap: 8px;
  }

  #game-screen .top-status,
  #game-screen .bottom-controls,
  #game-screen .controls,
  #game-screen .controls-bottom {
    display: contents;
  }

  .side-panel {
    display: block;
    grid-row: 1 / span 2;
    border-radius: 26px;
    background: linear-gradient(180deg, rgba(12, 23, 35, 0.9), rgba(7, 14, 22, 0.82));
    border: 1px solid rgba(255, 255, 255, 0.08);
    box-shadow: 0 18px 38px rgba(0, 0, 0, 0.24);
    pointer-events: none;
  }

  .left-side-panel {
    grid-column: 1;
  }

  .right-side-panel {
    grid-column: 3;
  }

  #game-screen .primary-status,
  #game-screen .secondary-status,
  #game-screen #joystick-zone,
  #game-screen .right-controls {
    position: relative;
    z-index: 1;
  }

  #game-screen .primary-status {
    grid-column: 1;
    grid-row: 1;
    align-self: start;
    padding: 12px;
    display: grid;
    justify-content: stretch;
  }

  #game-screen .secondary-status {
    grid-column: 3;
    grid-row: 1;
    align-self: start;
    padding: 12px;
    grid-template-columns: 1fr;
  }

  #game-screen .secondary-status .boss {
    grid-column: 1;
  }

  .game-stage {
    grid-column: 2;
    grid-row: 1 / span 2;
  }

  .canvas-shell {
    width: min(100%, calc((var(--app-height) - 16px - var(--safe-top) - var(--safe-bottom)) * 9 / 14));
    height: auto;
  }

  #game-screen #joystick-zone {
    grid-column: 1;
    grid-row: 2;
    justify-self: center;
    align-self: end;
    margin-bottom: 16px;
  }

  #game-screen .right-controls {
    grid-column: 3;
    grid-row: 2;
    justify-self: center;
    align-self: end;
    width: calc(100% - 24px);
    margin-bottom: 16px;
  }

  .control-pad {
    width: clamp(110px, 18vw, 150px);
    height: clamp(110px, 18vw, 150px);
  }

  .right-controls {
    justify-items: stretch;
    gap: 12px;
  }

  .shoot-btn,
  .skill-btn {
    width: 100%;
    min-height: 56px;
  }
}

@media (max-width: 480px) {
  :root {
    --bottom-ui-lift: max(108px, calc(var(--safe-bottom) + 64px));
    --screen-edge-gap: 18px;
  }

  .controls {
    align-items: end;
  }

  .right-controls {
    gap: 12px;
  }

  .canvas-shell {
    width: 100%;
  }
}

@media (max-height: 760px) {
  :root {
    --bottom-ui-lift: max(116px, calc(var(--safe-bottom) + 72px));
  }

  .control-pad {
    width: clamp(116px, 28vw, 152px);
    height: clamp(116px, 28vw, 152px);
  }

  .shoot-btn,
  .skill-btn {
    width: clamp(100px, 24vw, 124px);
    min-height: 58px;
  }

  .canvas-shell {
    width: min(100%, calc((var(--app-height) - 224px) * 9 / 14));
  }
}
