:root {
  color-scheme: dark;
  --night: #08182f;
  --deep: #031022;
  --paper: #f4edcf;
  --ink: #10223b;
  --grid: #2f5474;
  --cyan: #72e1df;
  --gold: #ffd35a;
  --coral: #ff826c;
  --mint: #8de8b5;
  --white: #fffdf2;
}

* { box-sizing: border-box; }

html, body { margin: 0; min-height: 100%; background: var(--deep); }

body {
  min-width: 280px;
  min-height: 100svh;
  min-height: 100dvh;
  overflow: hidden;
  color: var(--white);
  font-family: "Yu Gothic", "Hiragino Kaku Gothic ProN", Meiryo, sans-serif;
  background:
    linear-gradient(#1d3b5b55 1px, transparent 1px),
    linear-gradient(90deg, #1d3b5b55 1px, transparent 1px),
    radial-gradient(circle at 50% 0%, #17345d, var(--deep) 62%);
  background-size: 24px 24px, 24px 24px, auto;
}

button { font: inherit; color: inherit; cursor: pointer; }
button:focus-visible { outline: 3px solid var(--gold); outline-offset: 2px; }
button:disabled { opacity: .5; cursor: default; }
[hidden] { display: none !important; }

.game-shell {
  width: min(100%, 1180px);
  height: 100svh;
  height: 100dvh;
  min-height: 0;
  margin: 0 auto;
  padding: max(4px, env(safe-area-inset-top)) max(6px, env(safe-area-inset-right)) max(4px, env(safe-area-inset-bottom)) max(6px, env(safe-area-inset-left));
  display: grid;
  grid-template-rows: 48px minmax(0, 1fr);
  gap: 4px;
}

.masthead {
  min-height: 0;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  border-bottom: 2px solid #6b91aa;
}

.brand-block { min-width: 0; }
.eyebrow { display: block; color: var(--cyan); font-size: 14px; line-height: 1; letter-spacing: .12em; }
.masthead h1 { margin: 1px 0 0; overflow-wrap: anywhere; font-size: clamp(16px, 3vw, 24px); line-height: 1.05; }
.header-actions { display: flex; gap: 4px; flex: 0 0 auto; }
.header-actions button, .modal-close, #rankingButton {
  min-height: 44px;
  padding: 4px 9px;
  border: 2px solid #8db2c7;
  border-radius: 2px;
  background: #102a45;
  font-size: 14px;
  font-weight: 700;
}

.screen { min-width: 0; min-height: 0; overflow: hidden; }

.start-screen {
  position: relative;
  display: grid;
  place-items: center;
  padding: clamp(10px, 4vw, 42px);
  border: 2px solid #587a91;
  background: #06152bdd;
  isolation: isolate;
}

.start-world { position: absolute; inset: 0; z-index: -1; overflow: hidden; }
.moon-grid {
  position: absolute;
  width: min(62vw, 580px);
  aspect-ratio: 1;
  right: -5%;
  top: 5%;
  border-radius: 50%;
  border: 8px solid #9de7dc;
  opacity: .38;
  background:
    linear-gradient(#507787 2px, transparent 2px),
    linear-gradient(90deg, #507787 2px, transparent 2px), #dff8df;
  background-size: 44px 44px;
  box-shadow: 0 0 60px #72e1df66;
}
.shadow-sample {
  position: absolute;
  right: 12%;
  top: 25%;
  width: min(28vw, 270px);
  height: min(17vw, 150px);
  display: grid;
  place-content: center;
  text-align: center;
  color: #f8e9bd;
  border: 4px dashed #f5b85b;
  background: #07101be6;
  transform: rotate(5deg);
}
.shadow-sample b { font-size: clamp(26px, 5vw, 58px); }
.shadow-sample span { font-size: 14px; }
.net-sample {
  position: absolute;
  right: 4%;
  top: 19%;
  width: min(36vw, 350px);
  aspect-ratio: 4 / 3;
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  grid-template-rows: repeat(3, 1fr);
  transform: rotate(-5deg);
}
.net-sample i { border: 2px solid #72e1dfaa; background: #72e1df12; }
.paper-cannon {
  position: absolute;
  left: 8%;
  bottom: 6%;
  width: 110px;
  height: 65px;
  border: 4px solid #e7c45e;
  background: #173b54;
  clip-path: polygon(0 100%, 12% 45%, 58% 45%, 74% 0, 88% 7%, 78% 48%, 100% 66%, 92% 100%);
}

.start-copy { width: min(660px, 72%); margin: 0; justify-self: start; }
.mission-label { margin: 0 0 6px; color: var(--gold); font: 700 14px/1.2 Consolas, monospace; letter-spacing: .08em; }
.start-copy h2 { margin: 0 0 10px; max-width: 20em; font-size: clamp(24px, 4.5vw, 48px); line-height: 1.2; text-shadow: 3px 3px 0 #07101b; }
.start-copy > p:not(.mission-label) { max-width: 44em; margin: 0 0 10px; font-size: clamp(14px, 1.7vw, 18px); line-height: 1.55; }
.start-facts { display: flex; flex-wrap: wrap; gap: 6px; margin: 0 0 12px; }
.start-facts span { padding: 3px 8px; border-left: 4px solid var(--cyan); background: #061021c9; font-size: 14px; font-weight: 700; }
.primary-button {
  min-width: 170px;
  min-height: 48px;
  border: 3px solid var(--ink);
  border-radius: 2px;
  padding: 8px 18px;
  background: var(--gold);
  color: var(--ink);
  box-shadow: 5px 5px 0 #0009;
  font-weight: 900;
  font-size: 16px;
}
.primary-button:active { transform: translate(2px, 2px); box-shadow: 2px 2px 0 #0009; }

.play-screen {
  display: grid;
  grid-template-rows: 46px minmax(160px, 1fr) 58px 54px auto;
  grid-template-areas: "hud" "world" "bench" "actions" "status";
  gap: 3px;
}

.hud {
  grid-area: hud;
  display: grid;
  grid-template-columns: .85fr 1fr 1fr 1.4fr;
  gap: 3px;
  min-height: 0;
}
.hud > div { min-width: 0; display: grid; place-content: center; text-align: center; border: 2px solid #5e849c; background: #0c233c; }
.hud small { color: #add2dd; font: 700 14px/1 Consolas, monospace; }
.hud b { display: block; overflow-wrap: anywhere; font-size: clamp(14px, 2.6vw, 19px); line-height: 1.15; }
.energy-block { grid-template-columns: auto 38px; grid-template-rows: 17px 11px; column-gap: 4px; }
.energy-block small { grid-column: 1; }
.energy-block b { grid-column: 2; grid-row: 1; }
.energy-block > span { grid-column: 1 / -1; width: 90%; height: 7px; margin: 0 auto; border: 1px solid #9bc4d5; background: #071321; }
.energy-block i { display: block; width: 60%; height: 100%; background: var(--cyan); transition: width .2s ease; }

.world-frame { grid-area: world; position: relative; min-height: 0; overflow: hidden; border: 3px solid #7098ad; background: #07162a; }
#gameField { display: block; width: 100%; height: 100%; min-height: 160px; touch-action: pan-y; cursor: crosshair; }
.world-message {
  position: absolute;
  top: 6px;
  left: 50%;
  max-width: calc(100% - 20px);
  transform: translateX(-50%);
  padding: 4px 9px;
  border: 2px solid #082136;
  background: #f4edcfe6;
  color: var(--ink);
  font-size: 14px;
  font-weight: 900;
  text-align: center;
  pointer-events: none;
}
.pause-veil { position: absolute; inset: 0; display: grid; place-items: center; background: #031022cc; font-size: 26px; font-weight: 900; }

.area-workbench {
  grid-area: bench;
  min-height: 0;
  display: grid;
  grid-template-columns: minmax(120px, 1fr) 70px minmax(120px, 1fr);
  gap: 2px;
  border: 2px solid #8aa9b4;
  background: #dcd1a9;
  color: var(--ink);
}
.dimension-control { position: relative; min-width: 0; display: grid; grid-template-columns: minmax(44px, 1fr) 32px minmax(44px, 1fr); align-items: end; gap: 1px; padding-top: 11px; }
.dimension-control > span { position: absolute; inset: 1px 0 auto; text-align: center; font-size: 14px; line-height: 1; font-weight: 900; }
.dimension-control button { min-width: 44px; min-height: 44px; padding: 0; border: 2px solid var(--ink); background: #f9f2d7; color: var(--ink); font-size: 24px; font-weight: 900; }
.dimension-control output { align-self: center; text-align: center; font: 900 20px/1 Consolas, monospace; }
.formula-panel { display: grid; place-content: center; text-align: center; background: var(--ink); color: var(--white); }
.formula-panel span { font: 700 14px/1.2 Consolas, monospace; }
.formula-panel b { color: var(--gold); font-size: 14px; white-space: nowrap; }

.action-controls { grid-area: actions; min-height: 0; display: grid; grid-template-columns: 1fr minmax(92px, 1.3fr) 1fr; gap: 4px; }
.action-controls button { min-height: 50px; border: 2px solid #b4d3dc; border-radius: 2px; background: #163650; font-weight: 900; font-size: 18px; }
.move-button span { margin: 0 6px; font-size: 14px; }
.fire-button { background: var(--coral) !important; color: #25130f; border-color: #ffccb9 !important; }
.fire-button span, .fire-button b { display: block; line-height: 1; }
.fire-button span { font-size: 14px; }
.fire-button b { font-size: 18px; }
.fire-button.pulse { animation: button-pop .22s ease; }
.status-line {
  grid-area: status;
  min-width: 0;
  min-height: 44px;
  margin: 0;
  padding: 4px 7px;
  display: flex;
  align-items: center;
  border-left: 5px solid var(--gold);
  background: #0b2139;
  font-size: 14px;
  line-height: 18px;
  overflow-wrap: anywhere;
}

.quiz-screen, .result-screen {
  border: 3px solid #7698a5;
  background:
    linear-gradient(#5f7d8b33 1px, transparent 1px),
    linear-gradient(90deg, #5f7d8b33 1px, transparent 1px), var(--paper);
  background-size: 26px 26px;
  color: var(--ink);
}
.quiz-screen { display: grid; grid-template-rows: auto auto minmax(0, 1fr) auto 50px; gap: 5px; padding: clamp(8px, 2vw, 18px); text-align: center; }
.quiz-heading { display: grid; grid-template-columns: 1fr auto; align-items: center; text-align: left; }
.quiz-heading span { grid-column: 1 / -1; font-size: 14px; font-weight: 700; color: #415a67; }
.quiz-heading h2 { margin: 0; font-size: clamp(22px, 4vw, 34px); }
.quiz-heading b { font: 900 18px/1 Consolas, monospace; }
.quiz-question { margin: 0 auto; max-width: 45em; font-size: clamp(16px, 2.6vw, 22px); line-height: 1.4; font-weight: 900; }
.quiz-choices { align-self: center; width: min(100%, 760px); margin: auto; display: grid; grid-template-columns: 1fr 1fr; gap: 6px; }
.quiz-choices button { min-height: 48px; border: 2px solid var(--ink); padding: 6px; background: #fff9df; color: var(--ink); font-size: 14px; font-weight: 800; }
.quiz-choices button.correct { background: #c7f1d4; border-width: 4px; }
.quiz-choices button.incorrect { background: #ffd3c5; text-decoration: line-through; }
.quiz-feedback { min-height: 42px; margin: 0 auto; max-width: 48em; font-size: 14px; line-height: 1.45; font-weight: 700; }
.quiz-screen > .primary-button { justify-self: center; min-height: 46px; }

.result-screen { position: relative; display: grid; place-content: center; justify-items: center; gap: 6px; padding: 10px; text-align: center; }
.result-mark { position: absolute; top: 3%; right: 5%; color: #4d9f78; font: 900 clamp(70px, 16vw, 160px)/1 Georgia, serif; opacity: .2; transform: rotate(-9deg); }
.result-screen h2 { margin: 0; font-size: clamp(24px, 5vw, 42px); }
.result-screen > p { margin: 0; max-width: 48em; font-size: 14px; line-height: 1.4; }
.score-sheet { min-width: min(300px, 90vw); padding: 6px 16px; border: 3px double var(--ink); background: #fff9df; }
.score-sheet span, .score-sheet small { display: block; font-size: 14px; }
.score-sheet b { display: block; color: #b04431; font: 900 clamp(34px, 7vw, 60px)/1 Consolas, monospace; }
.result-stats { display: flex; flex-wrap: wrap; justify-content: center; gap: 5px; }
.result-stats span { padding: 4px 8px; border: 2px solid var(--ink); background: #e4eed7; font-size: 14px; }
.result-actions { display: flex; flex-wrap: wrap; justify-content: center; gap: 8px; }
.result-actions button { min-height: 46px; }

.modal { position: fixed; inset: 0; z-index: 40; display: grid; place-items: center; padding: 10px; background: #020816db; }
.modal-card { position: relative; width: min(720px, 100%); max-height: calc(100svh - 20px); max-height: calc(100dvh - 20px); overflow-y: auto; overscroll-behavior: contain; padding: 18px; border: 4px solid var(--ink); background: var(--paper); color: var(--ink); box-shadow: 8px 8px 0 #000; }
.modal-card h2 { margin: 0 80px 10px 0; font-size: 25px; }
.modal-card li, .modal-card p { font-size: 15px; line-height: 1.55; }
.modal-close { position: absolute; top: 8px; right: 8px; color: var(--white); }
.ranking-card { width: min(780px, 100%); }
#leaderboardPanel { color: var(--ink); }

@keyframes button-pop { 50% { transform: scale(.94); } }

@media (max-width: 680px) {
  .game-shell { grid-template-rows: 46px minmax(0, 1fr); gap: 3px; }
  .eyebrow { display: none; }
  .masthead h1 { font-size: 16px; }
  .header-actions button { min-width: 46px; min-height: 44px; padding: 3px 5px; font-size: 14px; }
  .header-actions #helpButton { display: none; }
  .start-screen { place-items: end start; padding: 10px; }
  .start-copy { width: 100%; margin: 0; padding: 10px; background: #06152bea; }
  .start-copy h2 { font-size: clamp(23px, 7vw, 31px); }
  .start-copy > p:not(.mission-label) { line-height: 1.42; }
  .moon-grid { width: 105vw; right: -38%; top: -14%; }
  .shadow-sample { width: 40vw; height: 25vw; right: 8%; top: 16%; }
  .net-sample { width: 52vw; right: 0; top: 10%; }
  .paper-cannon { left: 5%; bottom: 42%; opacity: .55; }
  .play-screen { grid-template-rows: 44px minmax(160px, 1fr) 58px 54px auto; }
  .hud small, .hud b { font-size: 14px; }
  .hud > div { border-width: 1px; }
  .world-message { top: 3px; padding: 2px 6px; }
  .result-learning { display: none; }
}

@media (max-width: 340px) {
  .game-shell { padding-left: 3px; padding-right: 3px; }
  .masthead h1 { max-width: 122px; }
  .area-workbench { grid-template-columns: minmax(117px, 1fr) 66px minmax(117px, 1fr); }
  .dimension-control { grid-template-columns: minmax(44px, 1fr) 28px minmax(44px, 1fr); }
  .formula-panel span, .formula-panel b { font-size: 14px; }
  .action-controls { grid-template-columns: 1fr 88px 1fr; }
  .move-button span { display: none; }
  .quiz-choices { grid-template-columns: 1fr; }
  .quiz-choices button { min-height: 44px; padding: 3px 5px; }
}

@media (max-height: 600px) {
  .game-shell { grid-template-rows: 46px minmax(0, 1fr); padding-top: 2px; padding-bottom: 2px; }
  .play-screen { grid-template-rows: 42px minmax(160px, 1fr) 56px 50px auto; gap: 2px; }
  .start-copy { padding: 7px; }
  .start-copy h2 { margin-bottom: 5px; font-size: clamp(22px, 5vw, 32px); }
  .start-copy > p:not(.mission-label) { margin-bottom: 5px; }
  .start-facts { margin-bottom: 6px; }
  .quiz-screen { padding: 5px; gap: 3px; }
  .result-screen { gap: 3px; padding: 5px; }
  .score-sheet { padding: 3px 12px; }
}

@media (max-width: 319px), (max-height: 520px) {
  body { overflow-y: auto; }
  .game-shell { height: auto; min-height: 100svh; overflow: visible; }
  .screen { min-height: 470px; overflow: visible; }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { scroll-behavior: auto !important; animation-duration: .001ms !important; animation-iteration-count: 1 !important; transition-duration: .001ms !important; }
  .shadow-sample, .net-sample, .result-mark { transform: none; }
}
