/* ─────────────────────────────────────────────
   뱀꼬리 늘리기 — 전체 스타일시트
───────────────────────────────────────────── */

/* ── Reset & Base ── */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

:root {
  --green-dark:   #1a5c22;
  --green-main:   #2d8c37;
  --green-light:  #4CAF50;
  --green-bright: #7DCE5A;
  --gold:         #F5A623;
  --gold-dark:    #c4821a;
  --white:        #ffffff;
  --cream:        #f9f5e7;
  --text-dark:    #1a2e1c;
  --text-mid:     #3d5c41;
  --shadow:       rgba(0,0,0,0.25);
  --radius-lg:    20px;
  --radius-md:    14px;
  --radius-sm:    8px;
}

html, body {
  height: 100%;
  overflow: hidden;
  font-family: 'Noto Sans KR', sans-serif;
  background: #3a7a2a;
  color: var(--text-dark);
  touch-action: manipulation;
  -webkit-tap-highlight-color: transparent;
  user-select: none;
}

/* ── 화면 공통 ── */
.screen {
  position: fixed;
  inset: 0;
  display: none;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  overflow: hidden;
}
.screen.active { display: flex; }

/* ─────────────────────────────────────────────
   시작 화면
───────────────────────────────────────────── */
#screen-start {
  background: radial-gradient(circle at center, #2e7d32, #1b5e20);
}
#screen-start::before {
  content: '';
  position: absolute;
  inset: 0;
  background: rgba(30, 80, 20, 0.35);
}

.start-content {
  position: relative;
  z-index: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 12px;
  width: 100%;
  padding: 24px 32px;
}

.game-icon-wrap {
  background: rgba(255,255,255,0.92);
  border-radius: 24px;
  width: 90px; height: 90px;
  display: flex; align-items: center; justify-content: center;
  box-shadow: 0 6px 24px rgba(0,0,0,0.3);
  margin-bottom: 4px;
}
.game-icon { font-size: 54px; line-height: 1; }

.game-title {
  font-size: clamp(32px, 8vw, 48px);
  font-weight: 900;
  color: var(--white);
  text-shadow: 0 2px 0 var(--green-dark), 0 4px 12px rgba(0,0,0,0.4);
  letter-spacing: -1px;
  text-align: center;
}

.game-subtitle {
  font-size: clamp(13px, 3.5vw, 16px);
  color: rgba(255,255,255,0.92);
  text-align: center;
  line-height: 1.6;
  text-shadow: 0 1px 4px rgba(0,0,0,0.5);
}

.start-buttons {
  display: flex;
  flex-direction: column;
  gap: 12px;
  width: 100%;
  max-width: 320px;
  margin-top: 16px;
}

/* ─────────────────────────────────────────────
   버튼 공통
───────────────────────────────────────────── */
.btn {
  width: 100%;
  padding: 14px 20px;
  border: none;
  border-radius: 50px;
  font-family: 'Noto Sans KR', sans-serif;
  font-size: clamp(15px, 4vw, 18px);
  font-weight: 700;
  cursor: pointer;
  transition: transform 0.12s, box-shadow 0.12s, filter 0.12s;
  box-shadow: 0 4px 0 rgba(0,0,0,0.3), 0 6px 16px rgba(0,0,0,0.2);
  letter-spacing: 0.5px;
}
.btn:active { transform: translateY(3px); box-shadow: 0 1px 0 rgba(0,0,0,0.3); }
.btn:hover { filter: brightness(1.06); }

.btn-primary {
  background: linear-gradient(160deg, #5abe42, #2d8c37);
  color: white;
  border-bottom: 4px solid #1a5c22;
}
.btn-secondary {
  background: linear-gradient(160deg, #f5f0e0, #e0d5b0);
  color: var(--text-dark);
  border-bottom: 4px solid #b8a870;
}
.btn-gold {
  background: linear-gradient(160deg, #ffd45a, #F5A623);
  color: var(--text-dark);
  border-bottom: 4px solid #c4821a;
}

/* ─────────────────────────────────────────────
   게임 방법 화면
───────────────────────────────────────────── */
#screen-howto {
  background: url('../assets/background.jpg') center/cover no-repeat;
}
#screen-howto::before {
  content: '';
  position: absolute;
  inset: 0;
  background: rgba(20, 60, 15, 0.6);
}

.howto-content {
  position: relative; z-index: 1;
  width: 100%; max-width: 480px;
  padding: 24px 20px;
  display: flex; flex-direction: column; align-items: center; gap: 16px;
}

.screen-title {
  font-size: clamp(22px, 6vw, 30px);
  font-weight: 900;
  color: white;
  text-shadow: 0 2px 8px rgba(0,0,0,0.4);
}

.howto-cards {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 10px;
  width: 100%;
}

.howto-card {
  background: rgba(255,255,255,0.92);
  border-radius: var(--radius-md);
  padding: 14px 12px;
  display: flex; flex-direction: column; align-items: center; gap: 6px;
  text-align: center;
  box-shadow: 0 3px 12px rgba(0,0,0,0.2);
}
.howto-card .howto-icon { font-size: 28px; }
.howto-card .howto-text { font-size: 13px; line-height: 1.5; color: var(--text-dark); }
.howto-card strong { color: var(--green-dark); }

.card-correct { border-top: 4px solid #cc3300; }
.card-wrong   { border-top: 4px solid #7B1FA2; }
.card-danger  { border-top: 4px solid #d32f2f; background: rgba(255,220,220,0.95); }

/* ─────────────────────────────────────────────
   게임 화면
───────────────────────────────────────────── */
#screen-game {
  flex-direction: column;
  background: #2a5a1a;
  justify-content: flex-start;
}

/* HUD */
#hud {
  width: 100%;
  height: 52px;
  background: linear-gradient(180deg, #1a4c14 0%, #2d6e22 100%);
  border-bottom: 2px solid #1a3c10;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 12px;
  gap: 8px;
  flex-shrink: 0;
  box-shadow: 0 2px 8px rgba(0,0,0,0.3);
}

.hud-item {
  display: flex; flex-direction: column; align-items: center;
  min-width: 60px;
}
.hud-score-item { min-width: 80px; }

.hud-label {
  font-size: 9px;
  font-weight: 700;
  color: rgba(255,255,255,0.6);
  letter-spacing: 1px;
  text-transform: uppercase;
}
.hud-value {
  font-size: clamp(16px, 4vw, 22px);
  font-weight: 900;
  color: #b8ff88;
  text-shadow: 0 0 8px rgba(100,255,50,0.5);
  line-height: 1;
}

.pause-btn {
  background: rgba(255,255,255,0.15);
  border: 1.5px solid rgba(255,255,255,0.3);
  border-radius: 20px;
  color: white;
  font-size: 13px;
  font-weight: 700;
  padding: 5px 12px;
  cursor: pointer;
  transition: background 0.15s;
  white-space: nowrap;
}
.pause-btn:hover { background: rgba(255,255,255,0.25); }
.pause-btn:disabled { opacity: 0.4; cursor: not-allowed; }

/* 수식 패널 */
#formula-panel {
  width: 100%;
  min-height: 64px;
  background: linear-gradient(180deg, rgba(255,255,255,0.15) 0%, rgba(255,255,255,0.08) 100%);
  border-bottom: 1.5px solid rgba(255,255,255,0.1);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 6px 16px;
  flex-shrink: 0;
  backdrop-filter: blur(4px);
}

#formula-display .katex-display {
  margin: 0 !important;
  color: white;
  text-shadow: 0 0 20px rgba(180,255,130,0.6);
}
#formula-display .katex {
  font-size: clamp(20px, 5vw, 28px) !important;
  color: white;
}

/* 게임 컨테이너 */
#game-container {
  flex: 1;
  width: 100%;
  position: relative;
  overflow: hidden;
  display: flex;
  align-items: center;
  justify-content: center;
}

#game-canvas {
  display: block;
  image-rendering: pixelated;
}

/* 사과 레이블 오버레이 */
#apple-labels {
  position: absolute;
  inset: 0;
  pointer-events: none;
  overflow: visible;
}

.apple-label {
  position: absolute;
  transform: translateX(-50%) translateY(-100%);
  background: rgba(255,255,255,0.93);
  border-radius: 8px;
  padding: 2px 6px;
  font-size: 11px;
  box-shadow: 0 2px 6px rgba(0,0,0,0.25);
  white-space: nowrap;
  line-height: 1.3;
  pointer-events: none;
}
.apple-label .katex { font-size: 12px !important; }

.label-correct, .label-wrong {
  border-bottom: 2px solid #cc3300;
  color: #8b0000;
}

/* D-패드 */
#dpad {
  flex-shrink: 0;
  width: 100%;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 8px 0 12px;
  gap: 4px;
  background: linear-gradient(180deg, #1a4c14 0%, #142e0e 100%);
  border-top: 2px solid #1a3c10;
}

.dpad-row {
  display: flex;
  justify-content: center;
  gap: 6px;
}
.dpad-mid {
  align-items: center;
}
.dpad-center {
  width: 44px; height: 44px;
  background: rgba(255,255,255,0.05);
  border-radius: 8px;
}

.dpad-btn {
  width: 60px; height: 52px;
  background: linear-gradient(180deg, #4a9e3a, #2a6e1e);
  border: none;
  border-radius: 10px;
  color: white;
  font-size: 20px;
  cursor: pointer;
  box-shadow: 0 4px 0 #1a4c10, 0 5px 10px rgba(0,0,0,0.3);
  transition: transform 0.08s, box-shadow 0.08s;
  display: flex; align-items: center; justify-content: center;
  -webkit-tap-highlight-color: transparent;
}
.dpad-btn:active, .dpad-btn.pressed {
  transform: translateY(3px);
  box-shadow: 0 1px 0 #1a4c10;
}

@media (min-width: 600px) {
  .dpad-btn { width: 72px; height: 60px; font-size: 22px; }
  .dpad-center { width: 54px; height: 54px; }
}

@media (max-height: 850px) {
  #hud { height: 42px; }
  .hud-value { font-size: 16px; }
  #formula-panel { min-height: 48px; padding: 4px; }
  .dpad-btn { width: 52px; height: 44px; font-size: 18px; }
  .dpad-center { width: 40px; height: 40px; }
  #dpad { padding: 4px 0 8px; }
}

/* ─────────────────────────────────────────────
   오버레이 공통
───────────────────────────────────────────── */
.overlay {
  position: absolute;
  inset: 0;
  background: rgba(0,0,0,0.6);
  display: flex; align-items: center; justify-content: center;
  z-index: 100;
  backdrop-filter: blur(3px);
  animation: fadeIn 0.25s ease;
}
.overlay.hidden { display: none; }

@keyframes fadeIn {
  from { opacity: 0; }
  to   { opacity: 1; }
}

.overlay-card {
  background: linear-gradient(160deg, #f9f5e7, #e8ead0);
  border-radius: var(--radius-lg);
  padding: 28px 32px;
  width: min(320px, 88vw);
  display: flex; flex-direction: column; align-items: center; gap: 16px;
  box-shadow: 0 12px 40px rgba(0,0,0,0.4);
  border: 2px solid rgba(255,255,255,0.5);
}

.overlay-title {
  font-size: 20px;
  font-weight: 900;
  color: var(--green-dark);
}

.pause-info { width: 100%; text-align: center; }
.pause-info-label { font-size: 11px; color: var(--text-mid); font-weight: 700; margin-bottom: 6px; }
.pause-formula-display .katex { font-size: 22px !important; color: var(--green-dark); }
.pause-tail-info { margin-top: 10px; font-size: 14px; color: var(--text-mid); }
.pause-tail-info strong { color: var(--green-dark); font-size: 18px; }

/* 스테이지 전환 카드 */
.stage-card {
  background: linear-gradient(135deg, #1a5c22, #2d8c37);
  border-radius: var(--radius-lg);
  padding: 32px 28px;
  display: flex; flex-direction: column; align-items: center; gap: 12px;
  box-shadow: 0 12px 40px rgba(0,0,0,0.4);
  min-width: 260px;
  animation: stagePop 0.4s cubic-bezier(0.34, 1.56, 0.64, 1);
}

@keyframes stagePop {
  from { transform: scale(0.6); opacity: 0; }
  to   { transform: scale(1);   opacity: 1; }
}

.stage-clear-badge {
  background: var(--gold);
  color: var(--text-dark);
  font-size: 18px;
  font-weight: 900;
  padding: 6px 18px;
  border-radius: 50px;
  letter-spacing: 1px;
  box-shadow: 0 3px 12px rgba(0,0,0,0.3);
}

#stage-next-formula .katex-display { margin: 0 !important; }
#stage-next-formula .katex { font-size: 26px !important; color: white; }

.stage-next-label {
  font-size: 14px;
  color: rgba(255,255,255,0.8);
  font-weight: 700;
}

/* 콤보 표시 */
.combo-display {
  position: absolute;
  top: 10px;
  left: 50%;
  transform: translateX(-50%);
  background: linear-gradient(135deg, #FFD700, #FF8C00);
  color: white;
  font-weight: 900;
  font-size: clamp(14px, 4vw, 18px);
  padding: 6px 16px;
  border-radius: 50px;
  box-shadow: 0 4px 16px rgba(255, 140, 0, 0.5);
  text-shadow: 0 1px 3px rgba(0,0,0,0.3);
  pointer-events: none;
  z-index: 50;
  white-space: nowrap;
}
.combo-display.hidden { display: none; }
.combo-fade {
  animation: comboFadeOut 1.2s ease forwards;
}
@keyframes comboFadeOut {
  0%   { opacity: 1; transform: translateX(-50%) translateY(0) scale(1); }
  30%  { opacity: 1; transform: translateX(-50%) translateY(-8px) scale(1.08); }
  100% { opacity: 0; transform: translateX(-50%) translateY(-20px) scale(0.9); }
}

/* ─────────────────────────────────────────────
   게임 오버 화면
───────────────────────────────────────────── */
#screen-gameover {
  background: url('../assets/background.jpg') center/cover no-repeat;
}
#screen-gameover::before {
  content: '';
  position: absolute;
  inset: 0;
  background: rgba(15, 40, 10, 0.7);
}

.gameover-content {
  position: relative; z-index: 1;
  display: flex; flex-direction: column; align-items: center;
  gap: 16px; padding: 24px 24px;
  width: 100%; max-width: 400px;
}

.gameover-snake { font-size: 56px; animation: deathBounce 0.6s ease; }
@keyframes deathBounce {
  0%   { transform: scale(0); opacity: 0; }
  60%  { transform: scale(1.2); }
  100% { transform: scale(1); opacity: 1; }
}

.result-grid {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 10px;
  width: 100%;
}

.result-card {
  background: rgba(255,255,255,0.92);
  border-radius: var(--radius-md);
  padding: 12px 8px;
  text-align: center;
  box-shadow: 0 3px 12px rgba(0,0,0,0.2);
}
.result-label { font-size: 11px; color: var(--text-mid); font-weight: 700; margin-bottom: 4px; }
.result-value { font-size: clamp(22px, 6vw, 30px); font-weight: 900; color: var(--green-dark); }

.name-input-wrap {
  width: 100%;
  display: flex; flex-direction: column; gap: 8px; align-items: center;
}
.name-label { font-size: 13px; color: rgba(255,255,255,0.8); font-weight: 700; }
.name-input {
  width: 100%;
  max-width: 220px;
  padding: 12px 16px;
  border-radius: 50px;
  border: 2px solid rgba(255,255,255,0.4);
  background: rgba(255,255,255,0.92);
  font-family: 'Noto Sans KR', sans-serif;
  font-size: 18px;
  font-weight: 700;
  text-align: center;
  color: var(--text-dark);
  outline: none;
}
.name-input:focus { border-color: var(--green-light); }

.gameover-buttons {
  display: flex; flex-direction: column; gap: 10px; width: 100%;
}

/* ─────────────────────────────────────────────
   랭킹 화면
───────────────────────────────────────────── */
#screen-ranking {
  background: url('../assets/background.jpg') center/cover no-repeat;
}
#screen-ranking::before {
  content: '';
  position: absolute;
  inset: 0;
  background: rgba(15, 40, 10, 0.65);
}

.ranking-content {
  position: relative; z-index: 1;
  width: 100%; max-width: 480px;
  padding: 20px 16px;
  display: flex; flex-direction: column; align-items: center; gap: 14px;
  max-height: 100vh; overflow-y: auto;
}

.ranking-list {
  width: 100%;
  display: flex; flex-direction: column; gap: 8px;
}

.ranking-row {
  background: rgba(255,255,255,0.9);
  border-radius: var(--radius-md);
  padding: 10px 14px;
  display: grid;
  grid-template-columns: 40px 1fr auto 60px;
  align-items: center;
  gap: 8px;
  box-shadow: 0 2px 8px rgba(0,0,0,0.15);
}

.ranking-top { background: rgba(255,245,210,0.95); border: 1.5px solid var(--gold); }

.rank-badge { font-size: 20px; text-align: center; }
.rank-name { font-weight: 700; color: var(--text-dark); font-size: 15px; }
.rank-detail { display: flex; flex-direction: column; align-items: flex-end; gap: 2px; }
.rank-score { font-weight: 900; color: var(--green-dark); font-size: 15px; }
.rank-meta { font-size: 11px; color: var(--text-mid); }
.rank-date { font-size: 10px; color: #999; text-align: right; }

.ranking-empty {
  background: rgba(255,255,255,0.9);
  border-radius: var(--radius-md);
  padding: 32px;
  text-align: center;
  font-size: 15px;
  line-height: 1.8;
  color: var(--text-mid);
  width: 100%;
}

/* ─────────────────────────────────────────────
   KaTeX 전역 조정
───────────────────────────────────────────── */
.katex { font-size: 1em; }
.katex-display { margin: 0; }

/* ─────────────────────────────────────────────
   반응형
───────────────────────────────────────────── */
@media (max-width: 400px) {
  .howto-cards { grid-template-columns: 1fr; }
  .result-grid { grid-template-columns: repeat(3, 1fr); }
  .dpad-btn { width: 52px; height: 46px; font-size: 18px; }
  .apple-label .katex { font-size: 10px !important; }
}

@media (min-height: 900px) {
  .start-buttons { gap: 16px; }
  .start-content { gap: 16px; }
}

/* ═════════ 10. 실험실로 돌아가기 버튼 ═════════ */
.back-to-lab-btn {
  display: flex;
  align-items: center;
  gap: 0.75rem;
  background: rgba(255, 255, 255, 0.8);
  backdrop-filter: blur(12px);
  -webkit-backdrop-filter: blur(12px);
  padding: 0.625rem 1rem;
  border-radius: 1rem;
  border: 1px solid rgba(255, 255, 255, 0.5);
  box-shadow: 0 10px 15px -3px rgba(0, 0, 0, 0.1), 0 4px 6px -4px rgba(0, 0, 0, 0.1);
  text-decoration: none;
  transition: all 0.2s cubic-bezier(0.4, 0, 0.2, 1);
  cursor: pointer;
  position: absolute;
  top: 1rem;
  left: 1rem;
  z-index: 9999;
}
.back-to-lab-btn:hover {
  box-shadow: 0 20px 25px -5px rgba(0, 0, 0, 0.1), 0 8px 10px -6px rgba(0, 0, 0, 0.1);
  transform: scale(1.05);
}
.back-to-lab-btn .icon-container {
  width: 2rem;
  height: 2rem;
  background-color: #6366f1;
  border-radius: 0.75rem;
  display: flex;
  align-items: center;
  justify-content: center;
  box-shadow: 0 10px 15px -3px rgba(99, 102, 241, 0.3), 0 4px 6px -4px rgba(99, 102, 241, 0.3);
  transition: transform 0.2s cubic-bezier(0.4, 0, 0.2, 1);
}
.back-to-lab-btn:hover .icon-container {
  transform: rotate(12deg);
}
.back-to-lab-btn .icon-container img {
  width: 1.25rem;
  height: 1.25rem;
  filter: brightness(0) invert(1);
}
.back-to-lab-btn .text-container {
  display: flex;
  flex-direction: column;
  text-align: left;
}
.back-to-lab-btn .subtitle {
  font-size: 0.625rem;
  font-weight: 700;
  color: rgba(99, 102, 241, 0.6);
  text-transform: uppercase;
  letter-spacing: 0.05em;
  line-height: 1;
  font-family: 'Inter', 'Noto Sans KR', sans-serif;
}
.back-to-lab-btn .title {
  font-size: 0.875rem;
  font-weight: 900;
  color: #1f2937;
  letter-spacing: -0.025em;
  font-family: 'Inter', 'Noto Sans KR', sans-serif;
}

/* ═════════ 11. 가로 모드 회전 안내 ═════════ */
#rotate-device-overlay {
  display: none;
  position: fixed;
  inset: 0;
  background: #2a5a1a;
  z-index: 10000;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  color: white;
  text-align: center;
  gap: 20px;
}

#rotate-device-overlay .rotate-icon {
  font-size: 60px;
  animation: rotateAnim 2s infinite ease-in-out;
}

@keyframes rotateAnim {
  0% { transform: rotate(0deg); }
  50% { transform: rotate(-90deg); }
  100% { transform: rotate(0deg); }
}

#rotate-device-overlay .rotate-text {
  font-size: 20px;
  font-weight: 700;
  line-height: 1.5;
  text-shadow: 0 2px 4px rgba(0,0,0,0.5);
}

/* 터치 기기(모바일, 태블릿)이면서 가로 모드일 때만 표시 */
@media (pointer: coarse) and (orientation: landscape) {
  #rotate-device-overlay {
    display: flex;
  }
}
