:root {
  color-scheme: dark;
  --ink: #080c17;
  --panel: #10172a;
  --panel-deep: #0c1221;
  --line: #39466b;
  --text: #edf1ff;
  --muted: #9ea9c8;
  --cyan: #50e6ff;
  --red: #ff4566;
  --gold: #ffd35a;
  --green: #75e6a0;
}

* { box-sizing: border-box; }

html, body {
  width: 100%;
  height: 100%;
  margin: 0;
  overflow: hidden;
  background: var(--ink);
  color: var(--text);
  font-family: "Courier New", Courier, monospace;
}

body { min-height: 100dvh; }
button { font: inherit; }

.world-shell {
  width: 100%;
  height: 100dvh;
  min-height: 100svh;
  display: grid;
  grid-template-rows: auto minmax(0, 1fr) auto;
  padding: max(10px, env(safe-area-inset-top)) max(14px, env(safe-area-inset-right)) max(10px, env(safe-area-inset-bottom)) max(14px, env(safe-area-inset-left));
  background: #090d1a;
}

.game-hud,
.bottom-bar {
  width: min(1240px, 100%);
  margin: 0 auto;
}

.game-hud {
  min-height: 70px;
  display: grid;
  grid-template-columns: minmax(170px, 1fr) minmax(260px, 1.5fr) minmax(230px, 1fr);
  gap: 14px;
  align-items: center;
  border-bottom: 1px solid var(--line);
}

.hud-block,
.hud-stats {
  min-width: 0;
  height: 48px;
}

.hud-block { padding: 5px 12px 5px 0; border-right: 1px solid var(--line); }
.title-block strong { display: block; margin-top: 3px; color: var(--text); font-size: 15px; letter-spacing: 1px; }
.hud-label,
.level-line { color: var(--muted); font-size: 10px; letter-spacing: 1px; }
.level-line { display: flex; justify-content: space-between; gap: 12px; }
.level-block strong { display: block; margin: 4px 0 6px; font-size: 14px; color: var(--cyan); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }

.hud-stats { display: grid; grid-template-columns: 0.8fr 1fr 1.2fr; gap: 12px; align-items: center; }
.hud-stat { min-width: 0; }
.hud-stat strong { display: block; margin-top: 4px; font-size: 13px; color: var(--text); }
.cast-stat { min-width: 80px; }

.meter { width: 100%; height: 7px; overflow: hidden; border: 1px solid #4e5b80; background: #151d35; }
.meter span { display: block; height: 100%; width: 0; background: var(--cyan); transition: width 100ms linear; }
.meter-wave span { background: var(--red); }
.meter-cast span { background: var(--gold); }

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

.game-stage {
  position: relative;
  width: min(1240px, 100%);
  max-height: 100%;
  aspect-ratio: 16 / 9;
  overflow: hidden;
  border: 2px solid #46557e;
  background: #0b1020;
  box-shadow: 0 14px 40px rgba(0, 0, 0, 0.35);
}

#gameCanvas {
  display: block;
  width: 100%;
  height: 100%;
  image-rendering: pixelated;
  image-rendering: crisp-edges;
  touch-action: none;
  user-select: none;
}

.screen-panel {
  position: absolute;
  inset: 0;
  z-index: 5;
  display: grid;
  place-items: center;
  padding: 20px;
  background: rgba(7, 10, 20, 0.72);
}
.screen-panel.is-hidden { display: none; }
.screen-panel:not(.is-hidden) ~ .touch-controls { visibility: hidden; }
.panel-box {
  width: min(590px, 100%);
  padding: 28px 30px 24px;
  border: 2px solid #93a4d5;
  background: var(--panel);
  box-shadow: 8px 8px 0 #070b14;
  border-radius: 2px;
}
.panel-kicker { color: var(--gold); font-size: 11px; letter-spacing: 1px; }
h1 { margin: 10px 0 12px; color: var(--text); font-size: 44px; line-height: 0.95; letter-spacing: 1px; }
.panel-box p { max-width: 54ch; margin: 0; color: #c3cbe2; font-size: 14px; line-height: 1.55; }
.panel-stats { margin-top: 17px; color: var(--cyan); font-size: 10px; line-height: 1.5; letter-spacing: 1px; }
.panel-actions { display: flex; flex-wrap: wrap; gap: 10px; margin-top: 22px; }
.panel-button {
  min-height: 48px;
  padding: 0 18px;
  border: 2px solid #98a8d5;
  border-radius: 2px;
  color: var(--text);
  background: #1b2644;
  cursor: pointer;
  letter-spacing: 1px;
  font-weight: 700;
}
.panel-button:hover,
.panel-button:focus-visible { border-color: var(--cyan); color: var(--cyan); outline: none; }
.panel-button.primary { color: #08101c; border-color: var(--cyan); background: var(--cyan); }
.panel-button.primary:hover,
.panel-button.primary:focus-visible { color: #08101c; background: #9af4ff; }
.panel-button.secondary { color: var(--muted); }
.panel-button.is-hidden { display: none; }
.control-strip { display: flex; flex-wrap: wrap; gap: 12px 18px; margin-top: 20px; color: var(--muted); font-size: 10px; }
.control-strip b { color: var(--text); }

.stage-toast {
  position: absolute;
  top: 16%;
  left: 50%;
  z-index: 4;
  min-width: 260px;
  transform: translateX(-50%);
  padding: 9px 14px;
  border: 1px solid #8494c1;
  background: rgba(10, 15, 30, 0.84);
  color: var(--gold);
  text-align: center;
  font-size: 12px;
  letter-spacing: 1px;
  opacity: 0;
  pointer-events: none;
  transition: opacity 140ms ease;
}
.stage-toast.is-visible { opacity: 1; }

.touch-controls { display: none; }
.portrait-notice { display: none; }

.bottom-bar {
  display: flex;
  justify-content: space-between;
  gap: 16px;
  padding-top: 7px;
  color: var(--muted);
  font-size: 10px;
  letter-spacing: 1px;
}
#objective { color: var(--cyan); }

@media (max-width: 820px) {
  .world-shell { padding-left: 8px; padding-right: 8px; }
  .game-hud { grid-template-columns: 1fr 1.4fr; gap: 8px; }
  .hud-stats { grid-column: 1 / -1; height: 34px; padding-bottom: 4px; }
  .hud-block { border-right: 0; }
  .game-stage-wrap { padding: 8px 0; }
  .bottom-bar { padding-top: 4px; font-size: 9px; }
}

@media (max-height: 640px) and (orientation: landscape) {
  .game-hud { min-height: 54px; }
  .hud-block, .hud-stats { height: 38px; }
  .hud-stats strong { margin-top: 2px; }
  .game-stage-wrap { padding: 6px 0; }
  .panel-box { padding: 20px 22px 18px; }
  h1 { font-size: 34px; }
  .panel-box p { font-size: 12px; }
  .control-strip { margin-top: 12px; }
}

@media (max-width: 900px) and (orientation: landscape) {
  .panel-box { padding: 14px 16px 12px; }
  h1 { margin: 6px 0 8px; font-size: 28px; }
  .panel-box p { font-size: 11px; line-height: 1.3; }
  .panel-stats { margin-top: 10px; font-size: 8px; }
  .panel-actions { margin-top: 12px; }
  .panel-button { min-height: 42px; }
  .control-strip { gap: 6px 10px; margin-top: 10px; font-size: 8px; }
}

@media (pointer: coarse), (max-width: 760px) {
  .touch-controls {
    position: absolute;
    inset: auto 0 0;
    z-index: 3;
    display: flex;
    justify-content: space-between;
    align-items: end;
    padding: 12px max(12px, env(safe-area-inset-left)) max(12px, env(safe-area-inset-bottom)) max(12px, env(safe-area-inset-right));
    pointer-events: none;
  }
  .touch-pad { position: relative; width: 144px; height: 104px; pointer-events: auto; }
  .touch-key,
  .cast-button {
    border: 2px solid #a8b6de;
    border-radius: 2px;
    color: var(--text);
    background: rgba(16, 23, 42, 0.92);
    box-shadow: 4px 4px 0 rgba(7, 11, 20, 0.8);
    touch-action: none;
    user-select: none;
  }
  .touch-key { position: absolute; width: 44px; height: 44px; padding: 0; font-size: 20px; pointer-events: auto; }
  .touch-key:active, .cast-button:active { color: var(--cyan); border-color: var(--cyan); background: rgba(27, 38, 68, 0.96); }
  .touch-up { left: 50px; top: 0; }
  .touch-left { left: 0; top: 50px; }
  .touch-down { left: 50px; top: 50px; }
  .touch-right { left: 100px; top: 50px; }
  .cast-button { width: 96px; height: 74px; padding: 0; color: var(--gold); border-color: var(--gold); font-weight: 700; letter-spacing: 1px; pointer-events: auto; }
  .cast-button span { display: block; margin-top: 5px; color: var(--muted); font-size: 9px; }
}

@media (orientation: portrait) and (max-width: 760px) {
  .portrait-notice {
    position: absolute;
    inset: 0;
    z-index: 20;
    display: grid;
    place-content: center;
    gap: 10px;
    padding: 22px;
    background: #0b1121;
    color: var(--gold);
    text-align: center;
    font-size: 18px;
    letter-spacing: 1px;
  }
  .portrait-notice span { color: var(--cyan); font-size: 11px; }
}

@media (max-width: 560px) {
  .title-block strong { font-size: 12px; }
  .level-block strong { font-size: 12px; }
  .hud-label, .level-line { font-size: 9px; }
  h1 { font-size: 30px; }
  .panel-box { padding: 22px 20px 18px; }
  .panel-box p { font-size: 12px; }
  .panel-stats { font-size: 9px; }
  .control-strip { gap: 8px 12px; font-size: 9px; }
  .desktop-hint { display: none; }
}
