:root {
  color-scheme: dark;
  --sky: #07131f;
  --ink: #0a1a25;
  --cream: #fff4cc;
  --cyan: #53e2df;
  --blue: #559bff;
  --red: #ff665d;
  --gold: #ffd05c;
  --green: #89e18c;
}

* { box-sizing: border-box; }
html, body { margin: 0; min-height: 100%; background: var(--sky); }
body {
  min-width: 280px;
  min-height: 100svh;
  min-height: 100dvh;
  overflow: hidden;
  color: var(--cream);
  font-family: "Yu Gothic", "Hiragino Kaku Gothic ProN", Meiryo, sans-serif;
  background: #07131f;
}
button { font: inherit; color: inherit; cursor: pointer; }
button:focus-visible { outline: 3px solid var(--gold); outline-offset: 2px; }
button:disabled { opacity: .46; cursor: default; }
[hidden] { display: none !important; }

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

.masthead {
  display: flex;
  align-items: center;
  justify-content: space-between;
  min-width: 0;
  min-height: 0;
  border-bottom: 3px solid var(--cyan);
  background: #07131f;
}
.brand-block { min-width: 0; }
.brand-block span { display: block; color: var(--cyan); font: 800 14px/1 monospace; letter-spacing: .08em; }
.brand-block h1 { margin: 1px 0 0; overflow-wrap: anywhere; font-size: clamp(16px, 2.8vw, 23px); line-height: 1; }
.header-actions { display: flex; gap: 3px; flex: 0 0 auto; }
.header-actions button, .modal-close, #rankingButton {
  min-width: 44px;
  min-height: 44px;
  padding: 3px 8px;
  border: 2px solid #88b5c0;
  border-radius: 0;
  background: #112b3c;
  font-size: 14px;
  font-weight: 800;
}

.screen { min-width: 0; min-height: 0; overflow: hidden; }
.start-screen {
  position: relative;
  display: grid;
  place-items: center start;
  padding: clamp(10px, 4vw, 38px);
  border: 3px solid #21546a;
  background: #061725;
  isolation: isolate;
}
.aurora {
  position: absolute;
  inset: 0;
  z-index: -3;
  background:
    linear-gradient(115deg, transparent 22%, #38d9b736 37%, transparent 50%),
    linear-gradient(74deg, transparent 34%, #627cff2e 51%, transparent 66%),
    #071826;
}
.start-art { position: absolute; inset: 0; z-index: -2; overflow: hidden; }
.magnetic-arc { position: absolute; border: 4px solid var(--cyan); border-left-color: transparent; border-right-color: transparent; border-radius: 50%; opacity: .46; transform: rotate(-22deg); }
.arc-one { width: min(70vw, 680px); height: min(45vw, 390px); right: -8%; top: 8%; }
.arc-two { width: min(55vw, 530px); height: min(32vw, 290px); right: 1%; top: 18%; border-color: #668cff transparent; }
.meteor { position: absolute; display: grid; place-content: center; width: 88px; aspect-ratio: 1; border: 6px double var(--cream); border-radius: 50% 44% 53% 40%; background: #172a37; font: 900 36px/1 monospace; box-shadow: -16px 10px 0 #0b1015; }
.meteor-n { right: 17%; top: 17%; color: #71b5ff; transform: rotate(15deg); }
.meteor-s { right: 6%; top: 43%; color: #ff7d73; transform: rotate(-12deg) scale(.7); }
.coil-tower { position: absolute; right: 22%; bottom: 5%; width: 150px; height: 140px; display: flex; align-items: end; justify-content: center; gap: 3px; border-bottom: 12px solid #c9a546; }
.coil-tower i { width: 17px; height: 100px; border: 3px solid var(--gold); border-radius: 50%; transform: skewY(-16deg); }
.coil-tower b { position: absolute; bottom: 7px; color: var(--ink); font: 900 16px/1 monospace; }
.start-copy { width: min(610px, 68%); padding: 14px; border-left: 8px solid var(--gold); background: #071622e8; box-shadow: 8px 8px 0 #0008; }
.episode { margin: 0 0 5px; color: var(--cyan); font: 800 14px/1.2 monospace; letter-spacing: .06em; }
.start-copy h2 { margin: 0 0 8px; font-size: clamp(26px, 4.7vw, 52px); line-height: 1.08; }
.start-copy > p:not(.episode) { margin: 0 0 9px; font-size: clamp(14px, 1.7vw, 18px); line-height: 1.5; }
.start-facts { display: flex; flex-wrap: wrap; gap: 5px; margin: 0 0 11px; }
.start-facts span { padding: 3px 7px; border: 2px solid #3f8492; background: #0a2532; font-size: 14px; font-weight: 800; }
.primary-button {
  min-width: 170px;
  min-height: 48px;
  padding: 7px 16px;
  border: 3px solid var(--ink);
  border-radius: 0;
  background: var(--gold);
  color: var(--ink);
  box-shadow: 5px 5px 0 #0008;
  font-size: 16px;
  font-weight: 900;
}
.primary-button:active { transform: translate(2px, 2px); box-shadow: 2px 2px 0 #0008; }

.play-screen {
  display: grid;
  grid-template-rows: 44px minmax(160px, 1fr) 54px 48px auto;
  grid-template-areas: "hud" "world" "console" "rail" "status";
  gap: 3px;
}
.hud { grid-area: hud; display: grid; grid-template-columns: .8fr 1.15fr 1fr 1.35fr; gap: 3px; min-height: 0; }
.hud > div { min-width: 0; display: grid; place-content: center; text-align: center; border: 2px solid #316071; background: #0b2533; }
.hud small { color: #9bd5d4; font: 800 14px/1 monospace; }
.hud b { display: block; overflow-wrap: anywhere; font: 900 clamp(14px, 2.6vw, 18px)/1.1 monospace; }
.energy-cell { grid-template-columns: auto 34px; grid-template-rows: 17px 10px; column-gap: 3px; }
.energy-cell small { grid-column: 1; }
.energy-cell b { grid-column: 2; grid-row: 1; }
.energy-cell > span { grid-column: 1 / -1; width: 92%; height: 7px; margin: auto; border: 1px solid #9bd5d4; background: #041017; }
.energy-cell i { display: block; width: 100%; height: 100%; background: var(--cyan); transition: width .18s ease; }

.world-frame { grid-area: world; position: relative; min-height: 0; overflow: hidden; border: 3px solid #3d7689; background: #04111b; }
#gameField { display: block; width: 100%; height: 100%; min-height: 160px; touch-action: pan-y; cursor: crosshair; }
.world-message { position: absolute; top: 4px; left: 50%; max-width: calc(100% - 12px); transform: translateX(-50%); padding: 3px 7px; border: 2px solid var(--ink); background: #fff4ccd9; color: var(--ink); text-align: center; font-size: 14px; font-weight: 900; pointer-events: none; }
.pause-veil { position: absolute; inset: 0; display: grid; place-items: center; background: #04111bd9; color: var(--gold); font: 900 30px/1 monospace; }

.coil-console { grid-area: console; display: grid; grid-template-columns: 1fr 1fr 1.25fr; gap: 3px; min-height: 0; }
.coil-console button, .rail-controls button { min-width: 44px; min-height: 44px; padding: 2px 4px; border: 2px solid #8ebac3; border-radius: 0; background: #102d3d; font-weight: 900; }
.coil-console button span, .coil-console button b { display: block; font-size: 14px; line-height: 1.05; }
.coil-console button b { color: var(--gold); font-size: 16px; }
.fire-button { position: relative; overflow: hidden; background: #7b2e2b !important; border-color: #ff9d86 !important; }
.fire-button::before { content: ""; position: absolute; inset: auto 0 0; height: calc(var(--charge, 0) * 100%); background: #ffb65066; pointer-events: none; }
.fire-button span, .fire-button b { position: relative; }

.rail-controls { grid-area: rail; display: grid; grid-template-columns: 1fr minmax(110px, 1.3fr) 1fr; gap: 3px; min-height: 0; }
.rail-controls button { font-size: 17px; }
.rail-controls button span { font-size: 14px; }
.rail-readout { min-width: 0; display: grid; place-content: center; text-align: center; border-top: 3px solid var(--gold); background: #061a25; }
.rail-readout span, .rail-readout b { overflow-wrap: anywhere; font-size: 14px; line-height: 1.1; }
.rail-readout span { color: var(--cyan); }
.status-line { grid-area: status; min-width: 0; min-height: 42px; margin: 0; padding: 3px 7px; display: flex; align-items: center; border-left: 6px solid var(--gold); background: #102632; font-size: 14px; line-height: 18px; overflow-wrap: anywhere; }

.quiz-screen, .result-screen { border: 4px solid #2d6474; background: var(--cream); color: var(--ink); }
.quiz-screen { display: grid; grid-template-rows: auto auto minmax(0, 1fr) auto 48px; gap: 5px; padding: clamp(7px, 2vw, 16px); 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; color: #41636b; font-size: 14px; font-weight: 800; }
.quiz-heading h2 { margin: 0; font-size: clamp(21px, 4vw, 32px); }
.quiz-heading b { font: 900 18px/1 monospace; }
.quiz-question { margin: 0 auto; max-width: 46em; font-size: clamp(16px, 2.5vw, 22px); line-height: 1.35; font-weight: 900; }
.quiz-choices { align-self: center; width: min(100%, 760px); margin: auto; display: grid; grid-template-columns: 1fr 1fr; gap: 5px; }
.quiz-choices button { min-height: 48px; padding: 4px; border: 2px solid var(--ink); border-radius: 0; background: #fffdfa; color: var(--ink); font-size: 14px; font-weight: 800; }
.quiz-choices button.correct { border-width: 4px; background: #c8efcf; }
.quiz-choices button.incorrect { background: #ffd0c9; text-decoration: line-through; }
.quiz-feedback { min-height: 40px; margin: 0 auto; max-width: 50em; font-size: 14px; line-height: 1.4; 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: 5px; padding: 8px; text-align: center; isolation: isolate; }
.result-lines { position: absolute; inset: 7% 5%; z-index: -1; border: 6px double #5eb9ad55; border-radius: 50%; transform: rotate(-7deg); }
.result-kicker { margin: 0; color: #2b6870; font: 900 14px/1 monospace; }
.result-screen h2 { margin: 0; font-size: clamp(24px, 5vw, 42px); }
.result-screen > p { margin: 0; max-width: 50em; font-size: 14px; line-height: 1.4; }
.score-panel { min-width: min(300px, 88vw); padding: 5px 14px; border: 4px solid var(--ink); background: #fffdfa; }
.score-panel span, .score-panel small { display: block; font-size: 14px; }
.score-panel b { display: block; color: #b33a34; font: 900 clamp(34px, 7vw, 56px)/1 monospace; }
.result-stats { display: flex; flex-wrap: wrap; justify-content: center; gap: 4px; }
.result-stats span { padding: 4px 7px; border: 2px solid var(--ink); background: #dff0df; font-size: 14px; }
.result-actions { display: flex; flex-wrap: wrap; justify-content: center; gap: 7px; }
.result-actions button { min-height: 46px; }

.modal { position: fixed; inset: 0; z-index: 50; display: grid; place-items: center; padding: 9px; background: #02080dde; }
.modal-card { position: relative; width: min(720px, 100%); max-height: calc(100svh - 18px); max-height: calc(100dvh - 18px); overflow-y: auto; overscroll-behavior: contain; padding: 17px; border: 4px solid var(--ink); background: var(--cream); color: var(--ink); box-shadow: 8px 8px 0 #000; }
.modal-card h2 { margin: 0 76px 8px 0; font-size: 24px; }
.modal-card li, .modal-card p { font-size: 15px; line-height: 1.55; }
.modal-close { position: absolute; top: 7px; right: 7px; color: var(--cream); }
#leaderboardPanel { color: var(--ink); }

@media (max-width: 680px) {
  .brand-block span { display: none; }
  .brand-block h1 { max-width: 132px; font-size: 16px; }
  .header-actions #helpButton { display: none; }
  .header-actions button { padding: 2px 5px; }
  .start-screen { place-items: end start; padding: 8px; }
  .start-copy { width: 100%; padding: 9px; border-left-width: 6px; }
  .start-copy h2 { font-size: clamp(24px, 8vw, 32px); }
  .start-copy > p:not(.episode) { line-height: 1.36; }
  .meteor-n { right: 10%; top: 10%; }
  .meteor-s { right: 2%; top: 32%; }
  .coil-tower { right: 40%; bottom: 43%; opacity: .7; transform: scale(.75); }
  .arc-one { width: 110vw; right: -35%; }
  .rail-controls button span { display: none; }
  .result-learning { display: none; }
}

@media (max-width: 340px) {
  .game-shell { padding-left: 3px; padding-right: 3px; }
  .play-screen { grid-template-rows: 43px minmax(160px, 1fr) 54px 48px auto; gap: 2px; }
  .hud small, .hud b { font-size: 14px; }
  .hud > div { border-width: 1px; }
  .coil-console { grid-template-columns: .95fr .85fr 1.2fr; }
  .coil-console button b { font-size: 14px; }
  .quiz-choices { grid-template-columns: 1fr; }
  .quiz-choices button { min-height: 44px; padding: 2px 4px; }
}

@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) 52px 46px auto; gap: 2px; }
  .start-copy { padding: 7px; }
  .start-copy h2 { margin-bottom: 4px; font-size: clamp(23px, 5vw, 31px); }
  .start-copy > p:not(.episode), .start-facts { margin-bottom: 5px; }
  .quiz-screen { padding: 4px; gap: 3px; }
  .result-screen { gap: 3px; padding: 4px; }
  .score-panel { padding: 2px 10px; }
}

@media (max-width: 319px), (max-height: 520px) {
  body { overflow-y: auto; }
  .game-shell { height: auto; min-height: 100svh; overflow: visible; }
  .screen { min-height: 468px; 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; }
  .meteor, .result-lines { transform: none; }
}
