:root {
  --bg-frost: #070d18;
  --ice-blue: #38bdf8;
  --ice-cyan: #06b6d4;
  --ice-glow: rgba(56, 189, 248, 0.45);
  --purple-magic: #a855f7;
  --purple-glow: rgba(168, 85, 247, 0.5);
  --orange-fox: #fb923c;
  --orange-glow: rgba(251, 146, 60, 0.5);
  --gold-accent: #facc15;
  --emerald-green: #10b981;
  --crimson-red: #f43f5e;
  --panel-dark: rgba(10, 18, 36, 0.92);
  --panel-border: rgba(56, 189, 248, 0.35);
  --text-primary: #f8fafc;
  --text-muted: #94a3b8;
}

* {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
  user-select: none;
  -webkit-user-select: none;
  -webkit-touch-callout: none;
  -webkit-tap-highlight-color: transparent;
}

*:focus {
  outline: none;
}

html, body {
  width: 100%;
  height: 100%;
  overflow: hidden;
  background-color: var(--bg-frost);
  font-family: 'Plus Jakarta Sans', system-ui, -apple-system, sans-serif;
  color: var(--text-primary);
}

#game-wrapper {
  position: relative;
  width: 100vw;
  height: 100vh;
  display: flex;
  justify-content: center;
  align-items: center;
  background: radial-gradient(ellipse at center, #111d38 0%, #060b14 100%);
  overflow: hidden;
}

canvas#gameCanvas {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  background: #060c18;
  box-shadow: 0 0 50px rgba(0, 0, 0, 0.9), 0 0 20px rgba(56, 189, 248, 0.2);
  display: block;
}

/* UI OVERLAY */
#ui-container {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  pointer-events: none;
  z-index: 10;
}

/* TOP HUD */
#hud-top {
  position: absolute;
  top: max(8px, env(safe-area-inset-top, 8px));
  left: max(12px, env(safe-area-inset-left, 12px));
  right: max(12px, env(safe-area-inset-right, 12px));
  display: flex;
  justify-content: space-between;
  align-items: center;
  pointer-events: auto;
  z-index: 25;
}

.hud-group {
  display: flex;
  align-items: center;
  gap: 12px;
  background: var(--panel-dark);
  border: 1px solid var(--panel-border);
  padding: 8px 16px;
  border-radius: 999px;
  backdrop-filter: blur(8px);
  -webkit-backdrop-filter: blur(8px);
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.4);
}

.hud-item {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 14px;
  font-weight: 700;
  font-family: 'Orbitron', monospace;
}

.hud-item .icon {
  font-size: 18px;
  display: flex;
  align-items: center;
  justify-content: center;
}

.hearts-container {
  display: flex;
  gap: 4px;
}

.heart {
  color: #f43f5e;
  font-size: 18px;
  transition: transform 0.2s cubic-bezier(0.175, 0.885, 0.32, 1.275);
}

.heart.lost {
  color: #475569;
  filter: grayscale(1);
  transform: scale(0.8);
}

.mana-bar-wrap {
  width: 80px;
  height: 8px;
  background: #1e293b;
  border-radius: 4px;
  overflow: hidden;
  border: 1px solid rgba(251, 146, 60, 0.4);
}

.mana-bar-fill {
  width: 100%;
  height: 100%;
  background: linear-gradient(90deg, #ea580c, #fb923c);
  transition: width 0.15s ease-out;
}

.hud-score {
  color: var(--gold-accent);
}

.hud-crystals {
  color: #c084fc;
}

.hud-controls-btn {
  background: var(--panel-dark);
  border: 1px solid var(--panel-border);
  color: var(--text-primary);
  width: 38px;
  height: 38px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  font-size: 16px;
  box-shadow: 0 4px 12px rgba(0,0,0,0.3);
  transition: all 0.2s;
  pointer-events: auto;
}

.hud-controls-btn:hover {
  border-color: var(--ice-blue);
  box-shadow: 0 0 12px var(--ice-glow);
  transform: scale(1.05);
}

/* BOSS HEALTH BAR */
#boss-hud {
  position: absolute;
  top: 70px;
  left: 50%;
  transform: translateX(-50%);
  display: none;
  flex-direction: column;
  align-items: center;
  gap: 6px;
  width: min(450px, 80vw);
  background: rgba(15, 23, 42, 0.85);
  padding: 10px 16px;
  border-radius: 12px;
  border: 1px solid rgba(56, 189, 248, 0.4);
  box-shadow: 0 8px 30px rgba(0, 0, 0, 0.6), 0 0 20px rgba(6, 182, 212, 0.2);
  z-index: 12;
  pointer-events: none;
}

.boss-title-row {
  display: flex;
  justify-content: space-between;
  width: 100%;
  font-family: 'Orbitron', monospace;
  font-size: 12px;
  font-weight: 800;
  letter-spacing: 1px;
}

.boss-name {
  color: #38bdf8;
  text-shadow: 0 0 8px rgba(56, 189, 248, 0.6);
}

.boss-percent {
  color: #e2e8f0;
}

.boss-bar-outer {
  width: 100%;
  height: 12px;
  background: #0f172a;
  border-radius: 6px;
  overflow: hidden;
  border: 1px solid rgba(255, 255, 255, 0.15);
  box-shadow: inset 0 2px 4px rgba(0,0,0,0.6);
}

.boss-bar-fill {
  width: 100%;
  height: 100%;
  background: linear-gradient(90deg, #0284c7, #38bdf8, #a855f7);
  box-shadow: 0 0 10px rgba(56, 189, 248, 0.8);
  transition: width 0.2s cubic-bezier(0.1, 0.7, 0.1, 1);
}

/* OVERLAY MODALS (Menu, Quiz, Victory, GameOver, HowToPlay) */
.modal-overlay {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background: rgba(3, 7, 18, 0.82);
  backdrop-filter: blur(10px);
  -webkit-backdrop-filter: blur(10px);
  display: flex;
  justify-content: center;
  align-items: center;
  z-index: 50;
  pointer-events: none; /* Inactive modals must not block clicks/touches */
  opacity: 0;
  visibility: hidden;
  transition: opacity 0.25s ease, visibility 0.25s ease;
  padding: 16px;
}

.modal-overlay.active {
  opacity: 1;
  visibility: visible;
  pointer-events: auto; /* Only active modal receives clicks */
}

.modal-card {
  background: linear-gradient(145deg, rgba(15, 23, 42, 0.96), rgba(8, 14, 28, 0.98));
  border: 2px solid var(--ice-cyan);
  border-radius: 24px;
  width: min(520px, 94vw);
  max-height: 90vh;
  overflow-y: auto;
  padding: 28px;
  box-shadow: 0 20px 60px rgba(0, 0, 0, 0.8), 0 0 30px rgba(6, 182, 212, 0.3);
  text-align: center;
  position: relative;
  animation: popIn 0.3s cubic-bezier(0.34, 1.56, 0.64, 1);
}

@keyframes popIn {
  from { transform: scale(0.85); opacity: 0; }
  to { transform: scale(1); opacity: 1; }
}

.modal-card h2 {
  font-family: 'Orbitron', monospace;
  font-size: 1.6rem;
  letter-spacing: 1px;
  margin-bottom: 8px;
  color: var(--ice-blue);
  text-shadow: 0 0 12px var(--ice-glow);
}

.modal-card p.subtitle {
  color: var(--text-muted);
  font-size: 0.9rem;
  margin-bottom: 20px;
  line-height: 1.5;
}

/* BUTTONS */
.btn-primary {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 10px;
  background: linear-gradient(135deg, #0284c7, #06b6d4);
  color: #ffffff;
  border: none;
  padding: 14px 28px;
  border-radius: 14px;
  font-family: 'Orbitron', monospace;
  font-size: 0.95rem;
  font-weight: 800;
  letter-spacing: 0.5px;
  cursor: pointer;
  box-shadow: 0 6px 20px rgba(6, 182, 212, 0.4);
  transition: all 0.2s cubic-bezier(0.2, 0.8, 0.2, 1);
  width: 100%;
  margin-top: 10px;
}

.btn-primary:hover {
  transform: translateY(-2px);
  filter: brightness(1.15);
  box-shadow: 0 10px 25px rgba(6, 182, 212, 0.6);
}

.btn-secondary {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 10px;
  background: rgba(30, 41, 59, 0.8);
  border: 1px solid rgba(148, 163, 184, 0.3);
  color: var(--text-primary);
  padding: 12px 24px;
  border-radius: 12px;
  font-family: 'Orbitron', monospace;
  font-size: 0.85rem;
  font-weight: 700;
  cursor: pointer;
  transition: all 0.2s;
  width: 100%;
  margin-top: 8px;
}

.btn-secondary:hover {
  border-color: var(--ice-blue);
  background: rgba(30, 41, 59, 1);
}

/* CHECKPOINT QUIZ MODAL */
.quiz-badge {
  display: inline-block;
  background: rgba(168, 85, 247, 0.2);
  border: 1px solid var(--purple-magic);
  color: #c084fc;
  font-family: 'Orbitron', monospace;
  font-size: 0.75rem;
  font-weight: 800;
  padding: 4px 14px;
  border-radius: 20px;
  margin-bottom: 12px;
  letter-spacing: 1px;
}

.quiz-question {
  background: rgba(15, 23, 42, 0.8);
  border: 1px solid rgba(56, 189, 248, 0.2);
  border-radius: 16px;
  padding: 16px;
  font-size: 1rem;
  line-height: 1.6;
  color: #f1f5f9;
  font-weight: 600;
  margin-bottom: 18px;
  text-align: left;
}

.quiz-options {
  display: flex;
  flex-direction: column;
  gap: 12px;
  margin-bottom: 16px;
}

.quiz-opt-btn {
  background: rgba(30, 41, 59, 0.85);
  border: 2px solid rgba(148, 163, 184, 0.35);
  border-radius: 16px;
  padding: 16px 20px;
  text-align: left;
  color: #f8fafc;
  font-size: 1.05rem;
  font-weight: 700;
  cursor: pointer;
  transition: all 0.2s cubic-bezier(0.175, 0.885, 0.32, 1.275);
  display: flex;
  align-items: center;
  gap: 16px;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.3);
}

.quiz-opt-btn .opt-badge {
  background: rgba(15, 23, 42, 0.9);
  border: 2px solid rgba(56, 189, 248, 0.5);
  width: 38px;
  height: 38px;
  border-radius: 10px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-family: 'Orbitron', monospace;
  font-size: 1rem;
  font-weight: 900;
  color: var(--ice-blue);
  flex-shrink: 0;
}

.quiz-opt-btn:hover {
  background: rgba(56, 189, 248, 0.2);
  border-color: var(--ice-blue);
  transform: scale(1.02) translateX(4px);
  box-shadow: 0 8px 20px rgba(56, 189, 248, 0.3);
}

.quiz-opt-btn.correct {
  background: rgba(16, 185, 129, 0.35) !important;
  border-color: #10b981 !important;
  color: #a7f3d0 !important;
  transform: scale(1.03);
  box-shadow: 0 8px 24px rgba(16, 185, 129, 0.5) !important;
}

.quiz-opt-btn.wrong {
  background: rgba(244, 63, 94, 0.3) !important;
  border-color: #f43f5e !important;
  color: #fecdd3 !important;
}

.quiz-feedback {
  display: none;
  padding: 14px;
  border-radius: 12px;
  font-size: 0.88rem;
  line-height: 1.5;
  margin-bottom: 16px;
  text-align: left;
}

.quiz-feedback.show-correct {
  display: block;
  background: rgba(16, 185, 129, 0.15);
  border: 1px solid rgba(16, 185, 129, 0.4);
  color: #6ee7b7;
}

.quiz-feedback.show-wrong {
  display: block;
  background: rgba(244, 63, 94, 0.15);
  border: 1px solid rgba(244, 63, 94, 0.4);
  color: #fca5a5;
}

/* STATS ROW ON VICTORY / GAMEOVER */
.stats-grid {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 12px;
  margin: 18px 0;
}

.stat-box {
  background: rgba(15, 23, 42, 0.6);
  border: 1px solid rgba(56, 189, 248, 0.2);
  border-radius: 12px;
  padding: 12px;
}

.stat-label {
  font-size: 0.75rem;
  color: var(--text-muted);
  text-transform: uppercase;
  margin-bottom: 4px;
}

.stat-val {
  font-family: 'Orbitron', monospace;
  font-size: 1.25rem;
  font-weight: 800;
  color: var(--gold-accent);
}

/* ==========================================================================
   MOBILE TOUCH CONTROLS & HIGH-VISIBILITY BUTTONS
   ========================================================================== */
#touch-controls {
  position: absolute;
  bottom: max(12px, env(safe-area-inset-bottom, 12px));
  left: max(14px, env(safe-area-inset-left, 14px));
  right: max(14px, env(safe-area-inset-right, 14px));
  display: none;
  justify-content: space-between;
  align-items: flex-end;
  pointer-events: none;
  z-index: 30;
}

.touch-cluster-left, .touch-cluster-right {
  display: flex;
  align-items: flex-end;
  gap: 12px;
  pointer-events: auto;
}

.touch-btn {
  width: 66px;
  height: 66px;
  border-radius: 18px;
  background: linear-gradient(145deg, rgba(15, 23, 42, 0.95), rgba(30, 41, 59, 0.95));
  border: 2.5px solid #38bdf8;
  color: #38bdf8;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 2px;
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.8), 0 0 16px rgba(56, 189, 248, 0.45), inset 0 2px 4px rgba(255, 255, 255, 0.25);
  backdrop-filter: blur(8px);
  -webkit-backdrop-filter: blur(8px);
  touch-action: manipulation;
  user-select: none;
  -webkit-user-select: none;
  cursor: pointer;
  transition: transform 0.08s, filter 0.08s, box-shadow 0.08s;
}

.touch-btn .btn-icon {
  font-size: 24px;
  line-height: 1;
}

.touch-btn .btn-lbl {
  font-family: 'Orbitron', monospace;
  font-size: 9px;
  font-weight: 800;
  letter-spacing: 0.5px;
  color: #ffffff;
  text-shadow: 0 1px 3px rgba(0,0,0,0.9);
}

.touch-btn:active, .touch-btn.active {
  transform: scale(0.9) translateY(2px);
  filter: brightness(1.3);
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.9), 0 0 25px rgba(56, 189, 248, 0.8) !important;
}

/* Action A: LOMPAT (Electric Blue) */
.touch-btn.btn-action-a {
  background: linear-gradient(145deg, #0284c7, #0369a1);
  border-color: #7dd3fc;
  box-shadow: 0 8px 24px rgba(2, 132, 199, 0.65), 0 0 20px rgba(56, 189, 248, 0.65), inset 0 2px 4px rgba(255, 255, 255, 0.35);
  color: #ffffff;
}

/* Action B: API (Fiery Orange) */
.touch-btn.btn-action-b {
  background: linear-gradient(145deg, #ea580c, #c2410c);
  border-color: #fdba74;
  box-shadow: 0 8px 24px rgba(234, 88, 12, 0.65), 0 0 20px rgba(251, 146, 60, 0.65), inset 0 2px 4px rgba(255, 255, 255, 0.35);
  color: #ffffff;
}

/* Action E: AKSI (Royal Purple) */
.touch-btn.btn-action-e {
  background: linear-gradient(145deg, #9333ea, #7e22ce);
  border-color: #e9d5ff;
  box-shadow: 0 8px 24px rgba(147, 51, 234, 0.65), 0 0 20px rgba(168, 85, 247, 0.65), inset 0 2px 4px rgba(255, 255, 255, 0.35);
  color: #ffffff;
}

/* ==========================================================================
   ORIENTATION PROMPT (PORTRAIT WARNING OVERLAY)
   ========================================================================== */
#orientation-prompt {
  position: fixed;
  top: 0;
  left: 0;
  width: 100vw;
  height: 100vh;
  background: rgba(4, 9, 20, 0.96);
  z-index: 9999;
  display: none;
  justify-content: center;
  align-items: center;
  padding: 24px;
  backdrop-filter: blur(12px);
  -webkit-backdrop-filter: blur(12px);
}

.orientation-box {
  background: linear-gradient(145deg, rgba(15, 23, 42, 0.98), rgba(8, 14, 28, 0.98));
  border: 2px solid var(--ice-blue);
  box-shadow: 0 0 40px rgba(56, 189, 248, 0.35);
  border-radius: 24px;
  padding: 32px 24px;
  text-align: center;
  max-width: 380px;
  animation: pulseCard 2.5s infinite alternate ease-in-out;
}

@keyframes pulseCard {
  from { box-shadow: 0 0 30px rgba(56, 189, 248, 0.25); }
  to { box-shadow: 0 0 60px rgba(56, 189, 248, 0.55); }
}

.orientation-icon {
  font-size: 48px;
  margin-bottom: 16px;
  animation: spinPhone 2s infinite ease-in-out;
}

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

.orientation-box h2 {
  font-family: 'Orbitron', monospace;
  font-size: 1.25rem;
  color: var(--ice-blue);
  margin-bottom: 12px;
}

.orientation-box p {
  font-size: 0.92rem;
  color: #cbd5e1;
  line-height: 1.6;
  margin-bottom: 20px;
}

/* ==========================================================================
   RESPONSIVE MEDIA QUERIES
   ========================================================================== */

/* SHOW ORIENTATION PROMPT ONLY ON PHONES HELD IN PORTRAIT */
@media (orientation: portrait) and (max-width: 900px) {
  #orientation-prompt {
    display: flex;
  }
}

/* SHOW TOUCH BUTTONS ON MOBILE, TABLET, TOUCH, OR COMPACT VIEWPORTS */
@media (max-width: 1024px), (max-height: 560px), (hover: none) and (pointer: coarse) {
  #touch-controls {
    display: flex !important;
  }
}

/* DYNAMIC LANDSCAPE MOBILE OPTIMIZATIONS (HEIGHT <= 520PX) */
@media (max-height: 520px) {
  #hud-top {
    top: max(4px, env(safe-area-inset-top, 4px));
    left: max(8px, env(safe-area-inset-left, 8px));
    right: max(8px, env(safe-area-inset-right, 8px));
  }
  .hud-group {
    padding: 4px 8px;
    gap: 8px;
    border-radius: 10px;
  }
  .hud-item {
    font-size: 11px;
    gap: 4px;
  }
  .heart {
    font-size: 13px;
  }
  .mana-bar-wrap {
    width: 48px;
    height: 9px;
  }
  .hud-controls-btn {
    width: 30px;
    height: 30px;
    font-size: 13px;
    border-radius: 8px;
  }

  /* Compact Touch Buttons for Landscape */
  .touch-btn {
    width: 60px;
    height: 60px;
    border-radius: 16px;
    gap: 1px;
  }
  .touch-btn .btn-icon {
    font-size: 21px;
  }
  .touch-btn .btn-lbl {
    font-size: 8.5px;
  }
  .touch-cluster-left, .touch-cluster-right {
    gap: 8px;
  }

  /* Modal in Landscape Mode: Never clipped! */
  .modal-overlay {
    padding: 6px;
  }
  .modal-card {
    width: min(650px, 96vw);
    max-height: 96vh;
    padding: 12px 18px;
    border-radius: 16px;
  }
  .modal-card h2 {
    font-size: 1.15rem;
    margin-bottom: 4px;
  }
  .modal-card p.subtitle {
    font-size: 0.8rem;
    margin-bottom: 8px;
    line-height: 1.35;
  }
  .quiz-badge {
    font-size: 9px;
    padding: 2px 8px;
    margin-bottom: 4px;
  }
  #btn-speak-question {
    margin-bottom: 8px !important;
    padding: 5px 12px !important;
    font-size: 0.8rem !important;
  }
  .quiz-question {
    font-size: 0.95rem !important;
    padding: 6px 12px !important;
    margin-bottom: 8px !important;
  }
  .quiz-options {
    display: grid !important;
    grid-template-columns: 1fr 1fr;
    gap: 8px !important;
    margin-bottom: 8px !important;
  }
  .quiz-opt-btn {
    padding: 8px 10px !important;
    font-size: 0.85rem !important;
    border-radius: 10px !important;
  }
  .quiz-opt-btn .opt-badge {
    width: 26px !important;
    height: 26px !important;
    font-size: 0.8rem !important;
  }
  .quiz-feedback {
    padding: 6px 10px !important;
    font-size: 0.85rem !important;
    margin-bottom: 6px !important;
  }
  .btn-primary, .btn-secondary {
    padding: 8px 16px !important;
    font-size: 0.85rem !important;
  }
}

