* { box-sizing: border-box; margin: 0; padding: 0; }

html, body {
  width: 100%;
  min-height: 100%;
  overflow-x: hidden;
  font-family: -apple-system, BlinkMacSystemFont, "Helvetica Neue", "Hiragino Sans", "Yu Gothic", sans-serif;
  color: #e8eef5;
  background: #050d1a;
  user-select: none;
  -webkit-tap-highlight-color: transparent;
}

/* ─── 海の背景 ─── */
.sea-bg {
  position: fixed;
  inset: 0;
  z-index: 0;
  background:
    radial-gradient(ellipse at 50% 0%, #1a3a5c 0%, transparent 60%),
    linear-gradient(to bottom, #0a1a2e 0%, #04101c 50%, #020912 100%);
  animation: sway 6s ease-in-out infinite;
}
@keyframes sway {
  0%, 100% { transform: translate(0, 0) rotate(0deg); }
  25%      { transform: translate(-4px, 2px) rotate(-0.2deg); }
  75%      { transform: translate(4px, -2px) rotate(0.2deg); }
}

/* 嵐モード（プレイ中） */
body.storm .sea-bg {
  animation: storm 0.45s ease-in-out infinite;
  background:
    radial-gradient(ellipse at 50% 0%, #4a1a1a 0%, transparent 60%),
    linear-gradient(to bottom, #1a0a14 0%, #0a0410 50%, #020208 100%);
}
@keyframes storm {
  0%, 100% { transform: translate(-6px, 0) rotate(-0.6deg); }
  50%      { transform: translate(6px, 0) rotate(0.6deg); }
}

/* ─── 星 ─── */
.stars {
  position: fixed; inset: 0;
  z-index: 1;
  pointer-events: none;
}
.star {
  position: absolute;
  width: 2px; height: 2px;
  background: #fff;
  border-radius: 50%;
  box-shadow: 0 0 4px #fff;
  animation: twinkle 3s ease-in-out infinite;
}
@keyframes twinkle {
  0%, 100% { opacity: 0.3; }
  50%      { opacity: 1; }
}
body.storm .stars { opacity: 0.15; }

/* ─── 月 ─── */
.moon {
  position: fixed;
  top: 8%;
  right: 12%;
  width: 60px; height: 60px;
  border-radius: 50%;
  background: radial-gradient(circle at 35% 35%, #fef9e3, #d8c878);
  box-shadow: 0 0 40px rgba(254, 249, 227, 0.4), 0 0 80px rgba(254, 249, 227, 0.2);
  z-index: 1;
  pointer-events: none;
  transition: opacity 1s;
}
body.storm .moon { opacity: 0; }

/* ─── 波 ─── */
.waves {
  position: fixed;
  bottom: 0;
  left: 0;
  width: 100%;
  height: 130px;
  z-index: 2;
  pointer-events: none;
}
.wave { transform-origin: center bottom; }
.wave-back {
  fill: rgba(40, 80, 130, 0.5);
  animation: wave-back 7s ease-in-out infinite;
}
.wave-front {
  fill: rgba(20, 50, 90, 0.7);
  animation: wave-front 5s ease-in-out infinite;
}
@keyframes wave-back {
  0%, 100% { transform: translateX(0); }
  50%      { transform: translateX(-30px); }
}
@keyframes wave-front {
  0%, 100% { transform: translateX(0); }
  50%      { transform: translateX(25px); }
}
body.storm .wave-back { animation: wave-storm-back 1.2s ease-in-out infinite; fill: rgba(70, 30, 40, 0.5); }
body.storm .wave-front { animation: wave-storm-front 0.9s ease-in-out infinite; fill: rgba(40, 10, 20, 0.7); }
@keyframes wave-storm-back {
  0%, 100% { transform: translate(0, 0) scaleY(1); }
  50%      { transform: translate(-50px, -15px) scaleY(1.2); }
}
@keyframes wave-storm-front {
  0%, 100% { transform: translate(0, 0) scaleY(1); }
  50%      { transform: translate(45px, -10px) scaleY(1.25); }
}

/* ─── 船 ─── */
.ship {
  position: fixed;
  bottom: 70px;
  left: 50%;
  width: 140px;
  height: 90px;
  z-index: 3;
  transform-origin: center bottom;
  animation: ship-rock 4s ease-in-out infinite;
  transition: opacity 1s, transform 1.5s;
  filter: drop-shadow(0 4px 8px rgba(0,0,0,0.6));
}
@keyframes ship-rock {
  0%, 100% { transform: translateX(-50%) rotate(-3deg); }
  50%      { transform: translateX(-50%) rotate(3deg); }
}
body.storm .ship {
  animation: ship-storm 0.7s ease-in-out infinite;
}
@keyframes ship-storm {
  0%, 100% { transform: translateX(-50%) translateY(0) rotate(-8deg); }
  25%      { transform: translateX(-55%) translateY(-6px) rotate(6deg); }
  50%      { transform: translateX(-50%) translateY(0) rotate(-6deg); }
  75%      { transform: translateX(-45%) translateY(-4px) rotate(8deg); }
}
body.sinking .ship {
  animation: none;
  transform: translateX(-50%) translateY(120px) rotate(45deg);
  opacity: 0;
}
body.success-mode .ship {
  animation: ship-rock 4s ease-in-out infinite;
}

/* プレイ画面では船を隠す（ALUに集中） */
body.playing .ship { opacity: 0.15; }
body.playing .waves { opacity: 0.4; }

/* ─── フラッシュ ─── */
.flash {
  position: fixed; inset: 0; z-index: 50;
  background: rgba(255, 70, 70, 0);
  pointer-events: none;
}
.flash.lightning {
  animation: lightning 0.8s ease-out;
}
.flash.bug-flash {
  animation: bug-flash 0.4s ease-out;
}
@keyframes lightning {
  0%   { background: rgba(255,255,255,0.95); }
  15%  { background: rgba(255,255,255,0); }
  25%  { background: rgba(255,255,255,0.85); }
  40%  { background: rgba(255,255,255,0); }
  60%  { background: rgba(200,210,255,0.5); }
  100% { background: rgba(255,255,255,0); }
}
@keyframes bug-flash {
  0%   { background: rgba(255,40,40,0.55); }
  100% { background: rgba(255,40,40,0); }
}

/* ─── 暗転 ─── */
.blackout {
  position: fixed; inset: 0; z-index: 60;
  background: #000;
  opacity: 0;
  pointer-events: none;
  transition: opacity 1.2s ease-in;
}
.blackout.on { opacity: 1; }

/* ─── 朝焼け（成功演出） ─── */
.sunrise {
  position: fixed; inset: 0; z-index: 55;
  background: radial-gradient(ellipse at 50% 100%, rgba(255, 200, 130, 0.7), rgba(255, 100, 60, 0.4) 40%, transparent 75%);
  opacity: 0;
  pointer-events: none;
  transition: opacity 1.5s ease-out;
}
.sunrise.on { opacity: 1; }

/* ─── App container ─── */
#app {
  position: relative;
  z-index: 10;
  width: 100%;
  min-height: 100vh;
  min-height: 100dvh;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px;
  padding-top: max(20px, env(safe-area-inset-top));
  padding-bottom: max(20px, env(safe-area-inset-bottom));
}
.screen {
  display: none;
  width: 100%;
  max-width: 560px;
  text-align: center;
  animation: fade-in 0.5s ease-out;
}
.screen.active { display: block; }
@keyframes fade-in {
  from { opacity: 0; transform: translateY(8px); }
  to   { opacity: 1; transform: translateY(0); }
}

.screen-h {
  font-size: 1.4em;
  letter-spacing: 0.25em;
  margin-bottom: 8px;
  text-shadow: 0 0 12px rgba(120, 180, 255, 0.3);
}

/* ─── タイトル ─── */
.title-frame {
  padding: 40px 30px;
  background: rgba(5, 12, 25, 0.5);
  border: 1px solid rgba(120, 180, 255, 0.2);
  backdrop-filter: blur(4px);
  border-radius: 6px;
}
.title-main {
  font-size: 3.4em;
  letter-spacing: 0.35em;
  margin-bottom: 14px;
  text-shadow: 0 0 24px rgba(120, 180, 255, 0.5);
  background: linear-gradient(180deg, #ffffff, #a8c4e8);
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  background-clip: text;
}
.title-line {
  width: 60px;
  height: 1px;
  background: rgba(120, 180, 255, 0.5);
  margin: 0 auto 14px;
}
.title-sub {
  font-size: 0.75em;
  letter-spacing: 0.45em;
  opacity: 0.65;
  margin-bottom: 36px;
}
.title-desc {
  line-height: 1.9;
  margin-bottom: 36px;
  opacity: 0.88;
  font-size: 0.95em;
}

/* ─── ボタン ─── */
.btn-primary {
  background: linear-gradient(180deg, #5BC0EB, #2e6090);
  color: white;
  border: none;
  padding: 14px 36px;
  font-size: 1em;
  letter-spacing: 0.18em;
  border-radius: 4px;
  cursor: pointer;
  box-shadow: 0 0 20px rgba(91, 192, 235, 0.35);
  transition: transform 0.1s, box-shadow 0.2s;
}
.btn-primary:hover { box-shadow: 0 0 32px rgba(91, 192, 235, 0.65); }
.btn-primary:active { transform: scale(0.97); }
.btn-primary:disabled { opacity: 0.3; cursor: not-allowed; }

.btn-ghost {
  background: transparent;
  color: #a8c4e8;
  border: 1px solid rgba(120, 180, 255, 0.4);
  padding: 12px 24px;
  font-size: 0.9em;
  letter-spacing: 0.15em;
  border-radius: 4px;
  cursor: pointer;
  transition: background 0.2s;
}
.btn-ghost:hover:not(:disabled) { background: rgba(120, 180, 255, 0.1); }
.btn-ghost:disabled { opacity: 0.25; cursor: not-allowed; }

.btn-exec {
  display: block;
  margin: 24px auto 0;
  background: linear-gradient(180deg, #d44, #962020);
  color: white;
  border: none;
  padding: 18px 60px;
  font-size: 1.25em;
  letter-spacing: 0.3em;
  border-radius: 4px;
  cursor: pointer;
  box-shadow: 0 0 25px rgba(220, 80, 80, 0.45);
  min-height: 56px;
  position: sticky;
  bottom: max(12px, env(safe-area-inset-bottom));
  z-index: 5;
}
.btn-exec:active { transform: scale(0.96); }
.btn-exec:disabled { opacity: 0.55; cursor: not-allowed; }

/* ─── レベル選択 ─── */
.level-grid {
  display: flex;
  flex-direction: column;
  gap: 14px;
  margin-top: 30px;
}
.level-card {
  background: rgba(10, 25, 45, 0.7);
  border: 1px solid rgba(120, 180, 255, 0.3);
  color: #e8eef5;
  padding: 18px 22px;
  border-radius: 4px;
  cursor: pointer;
  text-align: left;
  transition: background 0.2s, border-color 0.2s, transform 0.1s;
  backdrop-filter: blur(2px);
}
.level-card:hover {
  background: rgba(30, 60, 100, 0.75);
  border-color: rgba(120, 180, 255, 0.7);
  transform: translateX(4px);
}
.level-num {
  font-size: 0.75em;
  opacity: 0.7;
  letter-spacing: 0.25em;
  color: #5BC0EB;
}
.level-name {
  font-size: 1.35em;
  margin: 4px 0;
  letter-spacing: 0.12em;
}
.level-meta {
  font-size: 0.78em;
  opacity: 0.55;
}

/* ─── イントロ（物語導入） ─── */
.intro-box {
  background: rgba(5, 14, 28, 0.75);
  border: 1px solid rgba(120, 180, 255, 0.3);
  padding: 32px 28px;
  border-radius: 6px;
  text-align: left;
  backdrop-filter: blur(4px);
  display: flex;
  flex-direction: column;
}
.intro-chapter {
  font-size: 0.75em;
  letter-spacing: 0.35em;
  color: #E89F4A;
  margin-bottom: 12px;
  text-align: center;
}
.intro-title {
  font-size: 1.4em;
  text-align: center;
  letter-spacing: 0.08em;
  margin-bottom: 24px;
  line-height: 1.5;
}
.intro-figure {
  margin: 0 auto 18px;
  max-width: 100%;
  display: flex;
  justify-content: center;
}
.intro-figure:empty { display: none; }
.intro-figure svg {
  width: 100%;
  max-width: 320px;
  height: auto;
  background: rgba(5, 10, 20, 0.4);
  border: 1px solid rgba(120, 180, 255, 0.15);
  border-radius: 4px;
  padding: 6px;
}
.intro-body {
  line-height: 2;
  opacity: 0.92;
  white-space: pre-line;
  flex-grow: 1;
  font-size: 0.95em;
}
.intro-progress {
  display: flex;
  justify-content: center;
  gap: 10px;
  margin: 24px 0 16px;
}
.intro-progress .dot {
  width: 8px; height: 8px;
  border-radius: 50%;
  background: rgba(120, 180, 255, 0.25);
  transition: background 0.3s;
}
.intro-progress .dot.active {
  background: #5BC0EB;
  box-shadow: 0 0 8px rgba(91, 192, 235, 0.6);
}
.intro-nav {
  display: flex;
  justify-content: space-between;
  gap: 12px;
}

/* ─── HUD ─── */
.hud {
  display: flex;
  justify-content: space-between;
  background: rgba(0, 0, 0, 0.55);
  padding: 10px 16px;
  border-radius: 4px;
  font-size: 0.85em;
  margin-bottom: 12px;
  border: 1px solid rgba(120, 180, 255, 0.15);
}
.hud-label {
  opacity: 0.55;
  margin-right: 6px;
  letter-spacing: 0.1em;
  font-size: 0.85em;
}
.hud-item span:last-child {
  font-weight: bold;
  color: #5BC0EB;
  font-family: "Courier New", monospace;
}

.timer-bar {
  height: 6px;
  background: rgba(0,0,0,0.5);
  border-radius: 3px;
  overflow: hidden;
  margin-bottom: 20px;
  border: 1px solid rgba(120, 180, 255, 0.1);
}
.timer-fill {
  height: 100%;
  width: 100%;
  background: linear-gradient(to right, #5BC0EB, #E89F4A);
  transition: width 0.1s linear;
}
.timer-fill.danger {
  background: #d44;
  animation: pulse 0.3s ease-in-out infinite;
}
@keyframes pulse {
  0%, 100% { opacity: 1; }
  50%      { opacity: 0.5; }
}

/* ─── ALU ─── */
.alu {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 12px;
}
.register {
  background: rgba(5, 14, 28, 0.85);
  border: 1px solid rgba(120, 180, 255, 0.3);
  padding: 14px;
  border-radius: 4px;
  text-align: center;
  position: relative;
}
.register::before {
  content: "";
  position: absolute;
  top: 4px; left: 4px;
  width: 6px; height: 6px;
  background: #5BC0EB;
  border-radius: 50%;
  box-shadow: 0 0 6px #5BC0EB;
  animation: blink 1.2s ease-in-out infinite;
}
.reg-c::before { background: #E89F4A; box-shadow: 0 0 6px #E89F4A; }
@keyframes blink {
  0%, 100% { opacity: 1; }
  50%      { opacity: 0.3; }
}
.reg-label {
  font-size: 0.7em;
  opacity: 0.6;
  letter-spacing: 0.2em;
  margin-bottom: 6px;
}
.reg-value {
  font-size: 2em;
  font-weight: bold;
  font-family: "Courier New", monospace;
  color: #5BC0EB;
  min-height: 1.2em;
  text-shadow: 0 0 10px rgba(91, 192, 235, 0.4);
}
.reg-input {
  width: 100%;
  background: rgba(255, 255, 255, 0.05);
  border: 2px solid rgba(228, 159, 74, 0.6);
  color: #E89F4A;
  font-size: 2em;
  font-weight: bold;
  font-family: "Courier New", monospace;
  text-align: center;
  padding: 4px;
  border-radius: 4px;
  outline: none;
}
.reg-input:focus {
  border-color: #E89F4A;
  box-shadow: 0 0 16px rgba(228, 159, 74, 0.55);
}

.op-box {
  grid-column: 1 / -1;
  background: rgba(228, 159, 74, 0.08);
  border: 1px solid rgba(228, 159, 74, 0.4);
  padding: 10px;
  border-radius: 4px;
  text-align: center;
}
.op-label {
  font-size: 0.7em;
  opacity: 0.6;
  letter-spacing: 0.2em;
  margin-bottom: 4px;
}
.op-symbol {
  font-size: 2.4em;
  color: #E89F4A;
  font-family: "Courier New", monospace;
  text-shadow: 0 0 12px rgba(228, 159, 74, 0.5);
}

/* ─── BUG overlay ─── */
.bug-overlay {
  position: fixed;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%) scale(0);
  font-size: 5em;
  font-weight: bold;
  color: #ff3030;
  text-shadow: 0 0 30px rgba(255, 50, 50, 0.8);
  letter-spacing: 0.2em;
  z-index: 70;
  pointer-events: none;
  opacity: 0;
}
.bug-overlay.show {
  animation: bug-pop 0.5s ease-out;
}
@keyframes bug-pop {
  0%   { transform: translate(-50%, -50%) scale(0.3); opacity: 1; }
  50%  { transform: translate(-50%, -50%) scale(1.2); opacity: 1; }
  100% { transform: translate(-50%, -50%) scale(1); opacity: 0; }
}

/* ─── 結果画面 ─── */
.result-box {
  padding: 40px 28px;
  border-radius: 6px;
  backdrop-filter: blur(4px);
}
.result-box.success {
  background: rgba(255, 220, 170, 0.08);
  border: 1px solid rgba(255, 200, 130, 0.5);
  box-shadow: 0 0 40px rgba(255, 180, 100, 0.2);
}
.result-box.failure {
  background: rgba(40, 10, 10, 0.7);
  border: 1px solid rgba(200, 50, 50, 0.5);
}
.result-icon {
  font-size: 4.5em;
  margin-bottom: 16px;
}
.success .result-icon {
  filter: drop-shadow(0 0 20px rgba(255, 200, 100, 0.7));
}
.result-box h2 {
  font-size: 2em;
  letter-spacing: 0.15em;
  margin-bottom: 16px;
}
.result-sub {
  line-height: 1.8;
  margin-bottom: 24px;
  opacity: 0.92;
}
.result-stats {
  font-size: 0.85em;
  opacity: 0.65;
  margin-bottom: 24px;
  letter-spacing: 0.12em;
  font-family: "Courier New", monospace;
}
.result-quote {
  font-style: italic;
  opacity: 0.75;
  line-height: 1.9;
  margin-bottom: 24px;
  font-size: 0.9em;
}

/* ─── モバイル調整 ─── */
@media (max-width: 420px) {
  .title-main { font-size: 2.4em; }
  .reg-value, .reg-input { font-size: 1.5em; }
  .op-symbol { font-size: 1.8em; }
  .register { padding: 10px; }
  .reg-label { font-size: 0.65em; }
  .intro-box { padding: 22px 18px; }
  .intro-title { font-size: 1.15em; margin-bottom: 18px; }
  .intro-body { font-size: 0.9em; line-height: 1.85; }
  .intro-figure svg { max-width: 260px; }
  .ship { width: 100px; height: 70px; }
  .title-frame { padding: 32px 22px; }
  .btn-exec { padding: 14px 40px; font-size: 1.1em; }
}
@media (max-height: 700px) {
  .intro-figure svg { max-width: 220px; }
  .title-desc { margin-bottom: 24px; }
}
@media (max-height: 640px) {
  .hud { padding: 6px 12px; font-size: 0.78em; margin-bottom: 8px; }
  .timer-bar { margin-bottom: 12px; }
  .alu { gap: 8px; }
  .register { padding: 9px; }
  .reg-value, .reg-input { font-size: 1.4em; }
  .op-box { padding: 6px; }
  .op-symbol { font-size: 1.6em; }
  .btn-exec { margin-top: 14px; padding: 14px 40px; font-size: 1.1em; }
}
