/* ===== STEAM キューブ ===== */
:root {
  --bg1: #1a1a2e;
  --bg2: #16213e;
  --card: rgba(255, 255, 255, 0.08);
  --text: #f5f5f7;
  --science: #4fc3f7;
  --technology: #9575cd;
  --engineering: #ff8a65;
  --arts: #f06292;
  --math: #66bb6a;
  --radius: 18px;
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
}

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

html, body {
  height: 100%;
  overscroll-behavior: none;
}

body {
  font-family: "Hiragino Maru Gothic ProN", "Hiragino Kaku Gothic ProN",
    "BIZ UDGothic", "Yu Gothic", system-ui, sans-serif;
  background: linear-gradient(160deg, var(--bg1) 0%, var(--bg2) 60%, #0f3460 100%);
  background-attachment: fixed;
  color: var(--text);
  -webkit-user-select: none;
  user-select: none;
  -webkit-tap-highlight-color: transparent;
  -webkit-text-size-adjust: 100%;
}

button {
  font-family: inherit;
  border: none;
  cursor: pointer;
  color: inherit;
  background: none;
  touch-action: manipulation; /* iOS のダブルタップズームを防ぐ */
}

/* ===== 画面切り替え ===== */
.screen {
  display: none;
  min-height: 100dvh;
  padding: calc(var(--safe-top) + 12px) 16px calc(var(--safe-bottom) + 20px);
  max-width: 760px;
  margin: 0 auto;
  flex-direction: column;
}
.screen.active { display: flex; }

/* ===== ホーム ===== */
.home-header { text-align: center; margin-bottom: 8px; }
.home-header h1 {
  font-size: clamp(28px, 7vw, 44px);
  background: linear-gradient(90deg, var(--science), var(--technology), var(--engineering), var(--arts), var(--math));
  -webkit-background-clip: text;
  background-clip: text;
  -webkit-text-fill-color: transparent;
  letter-spacing: 0.04em;
}
.subtitle { opacity: 0.8; font-size: 15px; margin-top: 4px; }

.cube-stage {
  height: 180px;
  display: flex;
  align-items: center;
  justify-content: center;
  perspective: 700px;
  margin: 12px 0 20px;
  touch-action: none;
}
.cube {
  position: relative;
  width: 100px;
  height: 100px;
  transform-style: preserve-3d;
  animation: cube-spin 14s linear infinite;
}
.cube.dragging { animation: none; }
@keyframes cube-spin {
  from { transform: rotateX(-18deg) rotateY(0deg); }
  to   { transform: rotateX(-18deg) rotateY(360deg); }
}
.cube-face {
  position: absolute;
  width: 100px;
  height: 100px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 48px;
  border-radius: 14px;
  border: 2px solid rgba(255, 255, 255, 0.35);
  backdrop-filter: blur(2px);
}
.face-front  { background: rgba(79,195,247,.45);  transform: translateZ(50px); }
.face-back   { background: rgba(149,117,205,.45); transform: rotateY(180deg) translateZ(50px); }
.face-right  { background: rgba(255,138,101,.45); transform: rotateY(90deg) translateZ(50px); }
.face-left   { background: rgba(240,98,146,.45);  transform: rotateY(-90deg) translateZ(50px); }
.face-top    { background: rgba(102,187,106,.45); transform: rotateX(90deg) translateZ(50px); }
.face-bottom { background: rgba(255,255,255,.25); transform: rotateX(-90deg) translateZ(50px); }

.subject-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(140px, 1fr));
  gap: 12px;
}
.subject-btn {
  position: relative;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 2px;
  padding: 16px 8px 12px;
  border-radius: var(--radius);
  background: var(--card);
  border: 2px solid transparent;
  transition: transform 0.12s ease;
}
.subject-btn:active { transform: scale(0.95); }
.subject-btn.science     { border-color: var(--science); }
.subject-btn.technology  { border-color: var(--technology); }
.subject-btn.engineering { border-color: var(--engineering); }
.subject-btn.arts        { border-color: var(--arts); }
.subject-btn.math        { border-color: var(--math); }
.subject-icon { font-size: 36px; }
.subject-name { font-size: 17px; font-weight: 700; }
.subject-en { font-size: 11px; opacity: 0.6; letter-spacing: 0.08em; }
.subject-stars { font-size: 12px; min-height: 16px; color: #ffd54f; }

/* ===== モジュール共通 ===== */
.module-header {
  display: flex;
  align-items: center;
  gap: 10px;
  margin-bottom: 8px;
}
.module-header h2 { font-size: clamp(19px, 5vw, 26px); }
.back-btn {
  width: 44px;
  height: 44px;
  border-radius: 50%;
  background: var(--card);
  font-size: 22px;
  flex-shrink: 0;
}
.back-btn:active { transform: scale(0.92); }
.module-hint {
  font-size: 14px;
  opacity: 0.85;
  margin-bottom: 10px;
  line-height: 1.5;
}
.module-hint small { opacity: 0.75; }

.small-btn {
  padding: 8px 14px;
  border-radius: 12px;
  background: rgba(255, 255, 255, 0.15);
  font-size: 14px;
  font-weight: 700;
}
.small-btn:active { transform: scale(0.94); }

/* ===== かがく ===== */
.science-controls {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 10px;
  margin-bottom: 10px;
}
.planet-picker { display: flex; gap: 6px; }
.planet-btn {
  display: flex;
  flex-direction: column;
  align-items: center;
  font-size: 26px;
  padding: 6px 10px;
  border-radius: 12px;
  background: var(--card);
  border: 2px solid transparent;
}
.planet-btn small { font-size: 10px; }
.planet-btn.selected { border-color: var(--science); background: rgba(79, 195, 247, 0.25); }
.slider-row {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 13px;
}
.slider-row input[type="range"] { width: 110px; }
#science-canvas {
  width: 100%;
  flex: 1;
  min-height: 320px;
  border-radius: var(--radius);
  background: rgba(0, 0, 0, 0.3);
  border: 2px solid rgba(79, 195, 247, 0.4);
  touch-action: none;
}
.science-fact {
  text-align: center;
  font-size: 13px;
  opacity: 0.8;
  margin-top: 8px;
}

/* ===== テクノロジー ===== */
.tech-level {
  text-align: center;
  font-weight: 700;
  font-size: 16px;
  margin-bottom: 8px;
  color: #ffd54f;
}
.tech-board-wrap { display: flex; justify-content: center; }
.tech-board {
  position: relative;
  display: grid;
  grid-template-columns: repeat(5, 1fr);
  grid-template-rows: repeat(5, 1fr);
  gap: 4px;
  width: min(86vw, 380px);
  aspect-ratio: 1;
  padding: 6px;
  border-radius: var(--radius);
  background: rgba(0, 0, 0, 0.3);
  border: 2px solid rgba(149, 117, 205, 0.5);
}
.tech-cell {
  border-radius: 8px;
  background: rgba(255, 255, 255, 0.07);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: clamp(20px, 6vw, 32px);
}
.tech-cell.wall { background: rgba(149, 117, 205, 0.55); }
.tech-robot {
  position: absolute;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: clamp(24px, 7vw, 38px);
  transition: left 0.28s ease, top 0.28s ease;
  pointer-events: none;
  z-index: 2;
}
.tech-robot.crash { animation: shake 0.4s; }
@keyframes shake {
  0%, 100% { transform: translateX(0); }
  25% { transform: translateX(-6px); }
  75% { transform: translateX(6px); }
}
.tech-queue {
  display: flex;
  flex-wrap: wrap;
  gap: 4px;
  justify-content: center;
  min-height: 38px;
  margin: 10px auto;
  padding: 6px;
  width: min(86vw, 380px);
  border-radius: 12px;
  background: rgba(255, 255, 255, 0.08);
}
.queue-item {
  font-size: 20px;
  background: rgba(255, 255, 255, 0.12);
  border-radius: 8px;
  padding: 2px 4px;
}
.queue-item.running { background: #ffd54f; }
.tech-controls {
  display: flex;
  justify-content: center;
  flex-wrap: wrap;
  gap: 8px;
}
.cmd-btn {
  font-size: 26px;
  width: 52px;
  height: 52px;
  border-radius: 14px;
  background: var(--card);
}
.cmd-btn:active { transform: scale(0.9); background: rgba(149, 117, 205, 0.4); }
.run-btn {
  padding: 0 20px;
  height: 52px;
  border-radius: 14px;
  background: var(--technology);
  font-size: 17px;
  font-weight: 700;
  color: #fff;
}
.run-btn:disabled { opacity: 0.5; }
.run-btn:active { transform: scale(0.95); }

/* ===== こうがく ===== */
.seesaw-stage {
  position: relative;
  height: 190px;
  display: flex;
  align-items: flex-end;
  justify-content: center;
  margin: 18px 0 4px;
}
.seesaw-beam {
  position: absolute;
  bottom: 64px;
  width: min(92vw, 480px);
  height: 14px;
  background: linear-gradient(#c98850, #9c6437);
  border-radius: 7px;
  transition: transform 0.7s cubic-bezier(0.34, 1.4, 0.64, 1);
  transform-origin: center center;
}
.seesaw-slots {
  position: absolute;
  left: 0;
  right: 0;
  bottom: 14px;
  display: flex;
  justify-content: space-between;
  padding: 0 2%;
}
.seesaw-slot {
  width: 11%;
  aspect-ratio: 1;
  max-width: 52px;
  border-radius: 10px;
  border: 2px dashed rgba(255, 255, 255, 0.3);
  display: flex;
  align-items: center;
  justify-content: center;
  position: relative;
}
.seesaw-slot.fixed { border-style: solid; border-color: rgba(255, 255, 255, 0.15); }
.seesaw-slot.droppable { border-color: var(--engineering); }
.seesaw-slot .dist {
  position: absolute;
  top: -18px;
  font-size: 10px;
  opacity: 0.6;
}
.weight-chip {
  width: 100%;
  height: 100%;
  border-radius: 10px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: 800;
  font-size: clamp(11px, 3vw, 15px);
  color: #1a1a2e;
  background: #ffd54f;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.3);
}
.weight-chip.fixed-w { background: #90a4ae; }
.seesaw-pivot {
  position: absolute;
  bottom: 8px;
  width: 0;
  height: 0;
  border-left: 26px solid transparent;
  border-right: 26px solid transparent;
  border-bottom: 58px solid rgba(255, 138, 101, 0.8);
}
.eng-status {
  text-align: center;
  font-size: 15px;
  font-weight: 700;
  min-height: 24px;
  margin: 6px 0;
}
.eng-inventory {
  display: flex;
  justify-content: center;
  flex-wrap: wrap;
  gap: 10px;
  margin: 10px 0 14px;
}
.inv-weight {
  width: 58px;
  height: 58px;
  border-radius: 12px;
  background: #ffd54f;
  color: #1a1a2e;
  font-size: 17px;
  font-weight: 800;
  border: 3px solid transparent;
  box-shadow: 0 3px 6px rgba(0, 0, 0, 0.3);
}
.inv-weight.selected { border-color: #fff; transform: scale(1.1); }
.inv-weight.used { opacity: 0.25; pointer-events: none; }
#eng-reset { align-self: center; }

/* ===== アート ===== */
.arts-controls {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 10px;
  margin-bottom: 10px;
}
.symmetry-picker { display: flex; gap: 6px; }
.sym-btn {
  width: 40px;
  height: 40px;
  border-radius: 10px;
  background: var(--card);
  font-size: 16px;
  font-weight: 800;
  border: 2px solid transparent;
}
.sym-btn.selected { border-color: var(--arts); background: rgba(240, 98, 146, 0.3); }
.color-palette { display: flex; gap: 6px; flex-wrap: wrap; }
.color-swatch {
  width: 34px;
  height: 34px;
  border-radius: 50%;
  border: 3px solid transparent;
}
.color-swatch.selected { border-color: #fff; transform: scale(1.15); }
#arts-canvas {
  width: 100%;
  flex: 1;
  min-height: 320px;
  border-radius: var(--radius);
  background: #11111d;
  border: 2px solid rgba(240, 98, 146, 0.4);
  touch-action: none;
}

/* ===== さんすう ===== */
.math-info {
  display: flex;
  justify-content: space-between;
  font-size: 15px;
  font-weight: 700;
  margin-bottom: 10px;
  color: #ffd54f;
}
.math-grid {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 10px;
  width: min(90vw, 420px);
  margin: 0 auto;
}
.math-card {
  aspect-ratio: 1;
  border-radius: 16px;
  background: rgba(102, 187, 106, 0.2);
  border: 2px solid rgba(102, 187, 106, 0.6);
  font-size: clamp(22px, 7vw, 34px);
  font-weight: 800;
  color: var(--text);
  transition: transform 0.12s, opacity 0.3s;
}
.math-card:active { transform: scale(0.93); }
.math-card.selected {
  background: rgba(255, 213, 79, 0.4);
  border-color: #ffd54f;
  transform: scale(1.06);
}
.math-card.cleared {
  opacity: 0;
  pointer-events: none;
  transform: scale(0.4);
}
.math-card.wrong { animation: shake 0.4s; }

/* ===== オーバーレイ ===== */
.overlay {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.6);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 100;
}
.overlay.hidden { display: none; }
.overlay-card {
  background: linear-gradient(160deg, #2a2a4a, #1d1d35);
  border: 2px solid rgba(255, 255, 255, 0.25);
  border-radius: 24px;
  padding: 32px 44px;
  text-align: center;
  animation: pop 0.35s cubic-bezier(0.34, 1.56, 0.64, 1);
}
@keyframes pop {
  from { transform: scale(0.5); opacity: 0; }
  to { transform: scale(1); opacity: 1; }
}
.overlay-emoji { font-size: 56px; }
.overlay-title { font-size: 24px; font-weight: 800; margin: 8px 0; }
.overlay-stars { font-size: 28px; margin-bottom: 16px; }
.overlay-btn {
  padding: 12px 34px;
  border-radius: 14px;
  background: linear-gradient(90deg, var(--science), var(--math));
  color: #fff;
  font-size: 17px;
  font-weight: 800;
}
.overlay-btn:active { transform: scale(0.95); }
