* {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

body {
  background-color: #0f172a;
  color: #fff;
  font-family: 'Arial Black', sans-serif;
  overflow: hidden;
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100dvh;
}

/* ゲームモニター */
#game-container {
  position: relative;
  width: 800px;
  height: 500px;
  background: #020617;
  border: 4px solid #38bdf8;
  box-shadow: 0 0 35px rgba(56, 189, 248, 0.4);
  overflow: hidden;
}

/* 激しいクラッシュ揺れ（時間切れ時） */
#game-container.crash-shake {
  animation: bigShake 0.4s ease-in-out;
}

@keyframes bigShake {
  0%, 100% { transform: translate(0, 0); }
  20% { transform: translate(-12px, 12px); }
  40% { transform: translate(12px, -12px); }
  60% { transform: translate(-8px, -8px); }
  80% { transform: translate(8px, 8px); }
}

/* トンネル（背景）と走行揺れ */
.tunnel {
  position: absolute;
  width: 100%;
  height: 100%;
  background: radial-gradient(circle, #1e293b 0%, #020617 80%);
  perspective: 400px;
  transition: background 1s ease-out;
}

.tunnel.outside-world {
  background: radial-gradient(circle, #fef08a 0%, #38bdf8 60%, #0284c7 100%) !important;
}

.shaking {
  animation: shake 0.08s infinite alternate;
}

@keyframes shake {
  0% { transform: translateY(-3px) translateX(-1px); }
  100% { transform: translateY(3px) translateX(1px); }
}

/* レール */
.rail {
  position: absolute;
  bottom: -50px;
  left: 50%;
  width: 240px;
  height: 400px;
  background: repeating-linear-gradient(0deg, #38bdf8, #38bdf8 20px, #0f172a 20px, #0f172a 75px);
  background-size: 100% 75px;
  transform: translateX(-50%) rotateX(70deg);
  transform-origin: bottom center;
  transition: background-image 1s, opacity 0.5s ease;
  opacity: 0;
}

.rail.running {
  opacity: 1;
  animation: moveRail 0.35s linear infinite;
}

@keyframes moveRail {
  0% { background-position: 0 0; }
  100% { background-position: 0 75px; }
}

.outside-world .rail {
  background: repeating-linear-gradient(0deg, #fff, #fff 20px, #f59e0b 20px, #f59e0b 75px) !important;
  background-size: 100% 75px !important;
}

/* 迫ってくる問題（壁） */
#wall {
  position: absolute;
  top: 35%;
  left: 50%;
  transform: translate(-50%, -50%) scale(0.1);
  width: 360px;
  height: 220px;
  background: linear-gradient(135deg, #1e293b, #0f172a);
  border: 6px solid #38bdf8;
  box-shadow: 0 0 25px #0284c7;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  border-radius: 16px;
  opacity: 0;
  pointer-events: none;
}

#wall.approaching {
  animation: approach 10.0s linear forwards;
}

@keyframes approach {
  0% { transform: translate(-50%, -50%) scale(0.1); opacity: 0.2; }
  20% { opacity: 1; }
  100% { transform: translate(-50%, -50%) scale(1.5); opacity: 1; }
}

/* --- 上部情報エリア（種目＆何問目か） --- */
#header-info {
  position: absolute;
  top: 15px;
  left: 50%;
  transform: translateX(-50%);
  z-index: 10;
  display: flex;
  align-items: center;
  gap: 12px;
  background: rgba(15, 23, 42, 0.85);
  padding: 6px 20px;
  border-radius: 20px;
  border: 2px solid #0284c7;
  box-shadow: 0 0 10px rgba(56, 189, 248, 0.3);
}

#genre-tag {
  font-size: 18px;
  font-weight: bold;
  color: #38bdf8;
  letter-spacing: 1px;
}

#question-counter {
  font-size: 18px;
  font-weight: bold;
  color: #fde047;
  letter-spacing: 1px;
  border-left: 2px solid #0284c7;
  padding-left: 12px;
}

#question {
  font-size: 48px;
  font-weight: bold;
  color: #fff;
  text-shadow: 0 0 10px rgba(56, 189, 248, 0.8);
}

.mitori-box {
  display: flex;
  flex-direction: column;
  align-items: flex-end;
  font-family: monospace;
  font-size: 36px;
  border-bottom: 3px solid #38bdf8;
  padding-bottom: 4px;
}

/* 入力・操作 UI */
#ui-panel {
  position: absolute;
  bottom: 20px;
  left: 50%;
  transform: translateX(-50%);
  display: flex;
  gap: 10px;
  z-index: 10;
}

#answer-input {
  font-size: 28px;
  padding: 8px 15px;
  width: 140px;
  text-align: center;
  border: 3px solid #0891b2;
  border-radius: 8px;
  background: #000;
  color: #fff;
  outline: none;
  transition: border-color 0.2s, transform 0.1s;
}

#answer-input.error {
  border-color: #ef4444;
  animation: inputError 0.2s ease-in-out 2;
}

@keyframes inputError {
  0%, 100% { transform: translateX(0); }
  25% { transform: translateX(-6px); }
  75% { transform: translateX(6px); }
}

#submit-btn {
  font-size: 22px;
  padding: 8px 25px;
  background: #0ea5e9;
  color: #fff;
  font-weight: bold;
  border: none;
  border-radius: 8px;
  cursor: pointer;
  box-shadow: 0 0 10px #0284c7;
}

#submit-btn:hover { background: #38bdf8; }

/* 画面上のテンキー（PCでは初期非表示） */
#numpad {
  display: none;
}

/* オーバーレイ */
#overlay {
  position: absolute;
  inset: 0;
  background: #020617;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  z-index: 20;
  transition: background 0.8s ease;
  padding: 20px;
}

#overlay.clear-bg { background: rgba(255, 255, 255, 0.3); }
#overlay.break-bg { background: #020617; }

#main-msg {
  font-size: clamp(22px, 5vw, 36px);
  color: #38bdf8;
  letter-spacing: 2px;
  margin-bottom: 4px;
  text-shadow: 0 0 15px #0284c7;
  text-align: center;
  line-height: 1.3;
  word-break: keep-all;
}

#sub-msg {
  font-size: 18px;
  color: #cbd5e1;
  margin-bottom: 8px;
  text-align: center;
}

#time-msg {
  font-size: 20px;
  color: #34d399;
  font-weight: bold;
  margin-bottom: 2px;
  text-shadow: 0 0 10px rgba(52, 211, 153, 0.5);
}

#score-msg {
  font-size: 22px;
  color: #fef08a;
  font-weight: bold;
  margin-bottom: 12px;
  text-shadow: 0 0 10px rgba(254, 240, 138, 0.5);
}

/* 復習エリア */
#review-box {
  width: 90%;
  max-width: 580px;
  background: rgba(15, 23, 42, 0.95);
  border: 2px solid #38bdf8;
  border-radius: 12px;
  padding: 10px 15px;
  margin-bottom: 15px;
  color: #fff;
  box-shadow: 0 4px 15px rgba(0, 0, 0, 0.5);
}

.review-title {
  font-size: 14px;
  color: #fde047;
  margin-bottom: 8px;
  text-align: center;
}

.review-list {
  display: flex;
  flex-wrap: wrap;
  gap: 10px 12px;
  justify-content: center;
  align-items: flex-start;
  max-height: 140px;
  overflow-y: auto;
  padding: 2px;
}

.review-item {
  font-size: 15px;
  background: #1e293b;
  padding: 6px 12px;
  border-radius: 6px;
  border: 1px solid #475569;
  display: flex;
  align-items: center;
}

.review-item .q-text { color: #93c5fd; }
.review-item .a-text { color: #f43f5e; font-weight: bold; margin-left: 4px; }

/* みとり算専用の復習カードスタイル（コンパクト幅＆直前で強制改行） */
.review-item.mitori-card {
  flex-basis: 100%;
  width: max-content;
  min-width: 80px;
  max-width: 110px;
  flex-direction: column;
  align-items: flex-end;
  padding: 5px 10px;
  margin-top: 4px;
}

.review-mitori-box {
  display: flex;
  flex-direction: column;
  align-items: flex-end;
  font-family: monospace;
  font-size: 15px;
  color: #93c5fd;
  border-bottom: 2px solid #38bdf8;
  padding-bottom: 2px;
  width: 100%;
}

.mitori-a-text {
  color: #f43f5e;
  font-weight: bold;
  font-size: 15px;
  margin-top: 2px;
  text-align: right;
  width: 100%;
}

#overlay.clear-bg #main-msg {
  color: #0f172a;
  text-shadow: 
    0 0 10px #fff,
    -2px -2px 0 #fff, 2px -2px 0 #fff, -2px 2px 0 #fff, 2px 2px 0 #fff;
}

#overlay.clear-bg #sub-msg {
  color: #0f172a;
  font-weight: bold;
  text-shadow: 
    -1px -1px 0 #fff, 1px -1px 0 #fff, -1px 1px 0 #fff, 1px 1px 0 #fff;
}

#overlay.clear-bg #time-msg {
  color: #065f46;
  text-shadow: 
    -1px -1px 0 #fff, 1px -1px 0 #fff, -1px 1px 0 #fff, 1px 1px 0 #fff;
}

#overlay.clear-bg #score-msg {
  color: #854d0e;
  text-shadow: 
    -1px -1px 0 #fff, 1px -1px 0 #fff, -1px 1px 0 #fff, 1px 1px 0 #fff;
}

.btn-style {
  font-size: 26px;
  padding: 10px 35px;
  background: #0284c7;
  color: #fff;
  font-weight: bold;
  border: 3px solid #38bdf8;
  border-radius: 50px;
  cursor: pointer;
  box-shadow: 0 0 20px rgba(56, 189, 248, 0.6);
}

.btn-style:hover { background: #0ea5e9; }

.btn-style.end-btn {
  background: #e11d48;
  border-color: #fda4af;
  box-shadow: 0 0 20px rgba(244, 63, 94, 0.6);
}

.btn-style.end-btn:hover { background: #f43f5e; }


/* ====================================================
   スマホ・タブレット用のレスポンシブ自動調整（画面幅768px以下）
   ==================================================== */
@media (max-width: 768px) {
  #game-container {
    width: 95vw;
    height: 90dvh;
    max-height: 650px;
    border-width: 2px;
  }

  /* スマホ用にトンネルの奥行きを調整 */
  .tunnel {
    perspective: 300px;
  }

  /* スマホ用線路の位置・長さを調整（テンキーの上にしっかり見えるように調整） */
  .rail {
    bottom: 120px;
    height: 520px;
    width: 200px;
  }

  /* 壁（問題）のサイズと初期位置調整 */
  #wall {
    top: 24%;
    width: 280px;
    height: 170px;
  }

  #question {
    font-size: 30px;
  }

  .mitori-box {
    font-size: 26px;
  }

  /* 入力欄の位置調整 */
  #ui-panel {
    bottom: 215px;
  }

  #answer-input {
    font-size: 22px;
    width: 110px;
    padding: 4px 8px;
  }

  #submit-btn {
    display: none;
  }

  /* スマホ用オンスクリーン・テンキーの確実な表示設定 */
  #numpad {
    display: grid !important;
    grid-template-columns: repeat(3, 1fr);
    gap: 6px;
    position: absolute;
    bottom: 12px;
    left: 50%;
    transform: translateX(-50%);
    width: 90%;
    max-width: 320px;
    height: 190px;
    z-index: 15;
  }

  .num-btn {
    background: #1e293b;
    color: #38bdf8;
    border: 2px solid #0284c7;
    border-radius: 8px;
    font-size: 22px;
    font-weight: bold;
    display: flex;
    justify-content: center;
    align-items: center;
    cursor: pointer;
    user-select: none;
    -webkit-tap-highlight-color: transparent;
  }

  .num-btn:active {
    background: #0284c7;
    color: #fff;
  }

  .clear-btn {
    background: #475569;
    color: #f87171;
    border-color: #94a3b8;
  }

  .submit-num-btn {
    background: #0ea5e9;
    color: #fff;
    border-color: #38bdf8;
  }

  /* スマホ用メッセージ文字サイズ調整 */
  #main-msg { font-size: 28px; }
  #sub-msg { font-size: 15px; }
  #time-msg { font-size: 16px; }
  #score-msg { font-size: 18px; }
  .btn-style { font-size: 20px; padding: 8px 25px; }
}

/* 上の位へ移動ボタンのスタイル */
.digit-btn {
  background-color: #0284c7;
  color: #ffffff;
  border: 1px solid #38bdf8;
  border-radius: 6px;
  padding: 8px 12px;
  font-size: 16px;
  font-weight: bold;
  cursor: pointer;
  white-space: nowrap;
  transition: background-color 0.2s, transform 0.1s;
  margin-right: 6px;
}

.digit-btn:active {
  background-color: #0369a1;
  transform: scale(0.95);
}

/* 入力エリアのレイアウト調整 */
.input-group {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
}

/* 上の位ボタンが活性化している時の見た目 */
.digit-btn.active-shift {
  background-color: #f59e0b !important;
  border-color: #fbbf24 !important;
  box-shadow: 0 0 8px rgba(245, 158, 11, 0.6);
}