/* ==========================================================================
   Arcane Glyphs - Design Tokens & Responsive Stylesheet (Phase 6)
   ========================================================================== */

:root {
  /* Color System */
  --bg-dark: hsl(260, 30%, 6%);
  --parchment: hsl(40, 36%, 86%);
  --parchment-dark: hsl(40, 25%, 75%);
  --leather-brown: hsl(20, 35%, 16%);
  --gold-glow: hsl(45, 90%, 55%);
  --gold-metallic: hsl(45, 40%, 45%);
  
  /* Magical Energies */
  --fire-red: hsl(8, 90%, 50%);
  --ice-cyan: hsl(190, 95%, 45%);
  --lightning-purple: hsl(280, 85%, 65%);
  --time-blue: hsl(215, 95%, 55%);
  --health-ruby: hsl(355, 80%, 45%);
  --mana-sapphire: hsl(210, 85%, 45%);
  
  /* Shadows & Glows */
  --shadow-deep: 0 15px 35px rgba(0, 0, 0, 0.7);
  --rune-glow: 0 0 15px var(--lightning-purple);
  --vial-glow: inset 0 0 15px rgba(255, 255, 255, 0.2), 0 0 20px rgba(0, 0, 0, 0.5);

  /* Fonts */
  --font-magic: 'Cinzel Decorative', Georgia, serif;
  --font-book: 'Cormorant Garamond', serif;
  --font-serif: 'Cinzel', Georgia, serif;
  --font-sans: 'Inter', system-ui, -apple-system, sans-serif;
}

/* Reset & Base Setup */
* {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
  user-select: none;
}

html, body {
  width: 100vw;
  height: 100vh;
  overflow: hidden;
  background-color: var(--bg-dark);
  font-family: var(--font-sans);
  color: #fff;
}

/* Main canvases */
#game-container {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  z-index: 1;
}

#webgl-canvas-container {
  width: 100%;
  height: 100%;
  background: radial-gradient(circle at center, #251249 0%, #0d061f 65%, #05020c 100%);
}

#drawing-canvas {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  z-index: 5;
  pointer-events: none;
}

/* ==========================================================================
   HEADS UP DISPLAY (HUD)
   ========================================================================== */

#hud-overlay {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  z-index: 10;
  pointer-events: none;
  display: flex;
  flex-direction: column;
  justify-content: space-between;
  padding: 24px;
  transition: opacity 1s ease-in-out;
}

#hud-overlay.hidden-ui {
  opacity: 0;
  pointer-events: none !important;
}

.hud-group {
  pointer-events: auto;
}

/* Top-Left Diagnostics Panel */
.stats-panel {
  position: absolute;
  top: 24px;
  left: 24px;
  background: rgba(10, 8, 15, 0.75);
  border: 1px solid rgba(255, 255, 255, 0.1);
  border-radius: 8px;
  padding: 10px 14px;
  font-family: var(--font-serif);
  font-size: 0.8rem;
  display: flex;
  flex-direction: column;
  gap: 4px;
  color: var(--parchment-dark);
  backdrop-filter: blur(4px);
  z-index: 12;
}

#dust-counter-display {
  color: var(--gold-glow);
  font-weight: bold;
}

/* Audio Controls (Top Right-Center) */
.audio-controls {
  position: absolute;
  top: 24px;
  right: 220px;
  z-index: 12;
}

#mute-audio-btn {
  background: rgba(10, 8, 15, 0.75);
  border: 1px solid rgba(255, 255, 255, 0.15);
  color: var(--parchment);
  padding: 6px 14px;
  border-radius: 16px;
  font-family: var(--font-serif);
  font-size: 0.8rem;
  cursor: pointer;
  transition: all 0.2s ease;
}

#mute-audio-btn:hover {
  background: var(--gold-metallic);
  color: var(--bg-dark);
}

/* Liquid Vials (Bottom Left) */
.player-stats {
  position: absolute;
  bottom: 24px;
  left: 24px;
  display: flex;
  gap: 20px;
}

.vial-container {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 6px;
}

.vial-glass {
  position: relative;
  width: 50px;
  height: 150px;
  background: rgba(255, 255, 255, 0.05);
  border: 2px solid rgba(255, 255, 255, 0.2);
  border-radius: 25px;
  overflow: hidden;
  box-shadow: var(--vial-glow);
  backdrop-filter: blur(4px);
}

.vial-glass::before {
  content: '';
  position: absolute;
  top: 5px;
  left: 10px;
  width: 8px;
  height: 140px;
  background: rgba(255, 255, 255, 0.15);
  border-radius: 4px;
  pointer-events: none;
  z-index: 3;
}

.vial-liquid {
  position: absolute;
  bottom: 0;
  left: 0;
  width: 100%;
  transition: height 0.3s cubic-bezier(0.1, 0.8, 0.3, 1);
  z-index: 2;
  overflow: hidden;
}

.health-vial .vial-liquid {
  background: linear-gradient(0deg, hsl(355, 80%, 25%) 0%, var(--health-ruby) 100%);
  box-shadow: 0 0 15px var(--health-ruby);
}

.mana-vial .vial-liquid {
  background: linear-gradient(0deg, hsl(210, 85%, 25%) 0%, var(--mana-sapphire) 100%);
  box-shadow: 0 0 15px var(--mana-sapphire);
}

.liquid-bubbles {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background-image: radial-gradient(circle, rgba(255,255,255,0.4) 1px, transparent 1px);
  background-size: 12px 16px;
  animation: floatBubbles 2.5s linear infinite;
  opacity: 0.6;
}

@keyframes floatBubbles {
  0% { background-position: 0 0; }
  100% { background-position: 0 -32px; }
}

.vial-label {
  font-family: var(--font-serif);
  font-size: 0.8rem;
  letter-spacing: 1px;
  color: var(--parchment-dark);
  text-shadow: 0 2px 4px rgba(0,0,0,0.8);
}

.vial-value {
  font-family: var(--font-sans);
  font-size: 0.75rem;
  font-weight: 600;
  color: #fff;
  background: rgba(0, 0, 0, 0.5);
  padding: 2px 6px;
  border-radius: 4px;
  border: 1px solid rgba(255,255,255,0.1);
}

/* Webcam Frame (Magic Mirror) */
.magic-mirror-frame {
  position: absolute;
  top: 24px;
  right: 24px;
  width: 180px;
  height: 135px;
  background: #000;
  box-shadow: var(--shadow-deep), 0 0 10px var(--gold-metallic);
  border-radius: 12px;
  overflow: hidden;
  z-index: 100;
}

.mirror-border {
  position: absolute;
  top: -4px;
  left: -4px;
  right: -4px;
  bottom: -4px;
  border: 4px solid var(--gold-metallic);
  border-radius: 16px;
  pointer-events: none;
  box-shadow: inset 0 0 15px rgba(0,0,0,0.8);
  z-index: 3;
}

.mirror-inner {
  width: 100%;
  height: 100%;
  position: relative;
}

#webcam-video {
  width: 100%;
  height: 100%;
  object-fit: cover;
  transform: scaleX(-1);
}

#mirror-canvas {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  transform: scaleX(-1);
  pointer-events: none;
  z-index: 2;
}

.mirror-loading {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background: rgba(10, 8, 15, 0.85);
  display: flex;
  align-items: center;
  justify-content: center;
  font-family: var(--font-serif);
  font-size: 0.75rem;
  color: var(--gold-glow);
  text-align: center;
  padding: 10px;
  text-shadow: 0 0 5px rgba(255,255,255,0.3);
  z-index: 3;
}

/* Cast Feedback Panel (Bottom Center) */
.casting-feedback {
  position: absolute;
  bottom: 24px;
  left: 50%;
  transform: translateX(-50%);
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 8px;
}

.spell-symbol-glow {
  font-family: var(--font-magic);
  font-size: 2.2rem;
  color: var(--gold-glow);
  text-shadow: 0 0 10px var(--gold-glow);
  transition: all 0.3s ease;
  min-height: 40px;
}

.spell-cast-status {
  font-family: var(--font-serif);
  font-size: 0.9rem;
  letter-spacing: 1px;
  color: rgba(255, 255, 255, 0.7);
  background: rgba(0, 0, 0, 0.6);
  padding: 6px 16px;
  border-radius: 20px;
  border: 1px solid rgba(255, 255, 255, 0.1);
  backdrop-filter: blur(8px);
  box-shadow: var(--shadow-deep);
}

/* Target health plate */
.target-info {
  position: absolute;
  top: 24px;
  left: 50%;
  transform: translateX(-50%);
  width: 320px;
  background: rgba(10, 8, 15, 0.75);
  border: 1px solid var(--gold-metallic);
  border-radius: 8px;
  padding: 10px;
  text-align: center;
  backdrop-filter: blur(5px);
  transition: all 0.5s ease;
}

.target-name {
  font-family: var(--font-serif);
  font-size: 1rem;
  margin-bottom: 6px;
  color: var(--parchment);
}

.target-hp-bg {
  width: 100%;
  height: 8px;
  background: rgba(255, 255, 255, 0.1);
  border-radius: 4px;
  overflow: hidden;
}

.target-hp-fill {
  height: 100%;
  background: var(--fire-red);
  box-shadow: 0 0 8px var(--fire-red);
  transition: width 0.2s ease-out;
}

.target-shield {
  font-family: var(--font-serif);
  font-size: 0.8rem;
  margin-top: 4px;
  font-weight: bold;
}
.fire-shield { color: var(--fire-red); }
.ice-shield { color: var(--ice-cyan); }
.void-shield { color: var(--lightning-purple); }

.hidden {
  display: none !important;
}

/* ==========================================================================
   GRIMOIRE OVERLAY & INTERACTIVE BOOK
   ========================================================================== */

#grimoire-overlay {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  z-index: 20;
  background: rgba(5, 4, 8, 0.85);
  backdrop-filter: blur(8px);
  display: flex;
  align-items: center;
  justify-content: center;
  transition: opacity 0.5s ease-in-out;
  padding: 20px;
}

.grimoire-book-wrapper {
  perspective: 1200px;
  width: 100%;
  max-width: 900px;
  display: flex;
  justify-content: center;
}

.grimoire-book {
  display: flex;
  width: 900px;
  height: 600px;
  background: var(--leather-brown);
  border: 12px solid hsl(20, 20%, 8%);
  border-radius: 12px;
  box-shadow: var(--shadow-deep), 0 0 30px rgba(0,0,0,0.9);
  position: relative;
  overflow: hidden;
}

/* Page spine */
.grimoire-book::after {
  content: '';
  position: absolute;
  left: 50%;
  top: 0;
  width: 16px;
  height: 100%;
  transform: translateX(-50%);
  background: linear-gradient(90deg, rgba(0,0,0,0.4), rgba(0,0,0,0.8) 50%, rgba(0,0,0,0.4));
  border-left: 1px solid var(--gold-metallic);
  border-right: 1px solid var(--gold-metallic);
  z-index: 4;
}

.book-page {
  flex: 1;
  height: 100%;
  background: var(--parchment);
  color: #1a1510;
  padding: 40px;
  overflow-y: auto;
  position: relative;
  background-image: radial-gradient(var(--parchment-dark) 1px, transparent 0);
  background-size: 24px 24px;
}

.left-page {
  border-right: 1px solid rgba(0, 0, 0, 0.15);
}

.right-page {
  border-left: 1px solid rgba(0, 0, 0, 0.15);
}

/* Typography on Parchment */
.book-title {
  font-family: var(--font-magic);
  font-size: 2.2rem;
  text-align: center;
  color: #3b2c1b;
  margin-bottom: 4px;
}

.book-subtitle {
  font-family: var(--font-serif);
  font-size: 0.9rem;
  text-align: center;
  color: #705539;
  letter-spacing: 2px;
  text-transform: uppercase;
}

.magic-divider {
  border: none;
  height: 2px;
  background: linear-gradient(90deg, transparent, var(--gold-metallic) 50%, transparent);
  margin: 20px 0;
}

.spellbook-list h3 {
  font-family: var(--font-serif);
  font-size: 1.1rem;
  color: #3b2c1b;
  margin-bottom: 12px;
}

.spell-item {
  list-style: none;
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 10px 14px;
  border-radius: 6px;
  cursor: pointer;
  transition: all 0.2s ease-in-out;
  margin-bottom: 8px;
  border: 1px solid transparent;
}

.spell-item .rune-glyph {
  font-size: 1.5rem;
  width: 32px;
  text-align: center;
  color: #554433;
}

.spell-item .spell-name {
  font-family: var(--font-serif);
  font-weight: 700;
  font-size: 0.95rem;
  color: #3d2f21;
}

.spell-item:hover {
  background: rgba(0, 0, 0, 0.05);
  border-color: rgba(0, 0, 0, 0.15);
}

.spell-item.active {
  background: rgba(255, 255, 255, 0.45);
  border-color: var(--gold-metallic);
  box-shadow: inset 0 0 5px rgba(0,0,0,0.05);
}

.spell-item.active .rune-glyph {
  color: var(--lightning-purple);
  text-shadow: 0 0 3px rgba(160, 50, 240, 0.4);
}

/* Mastery Progression UI */
.mastery-box {
  background: rgba(40, 25, 10, 0.05);
  border: 1px solid var(--gold-metallic);
  border-radius: 8px;
  padding: 14px;
  margin-bottom: 18px;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 8px;
}

.mastery-title {
  font-family: var(--font-serif);
  font-size: 0.85rem;
  letter-spacing: 1px;
  color: #554433;
  text-transform: uppercase;
}

.mastery-level {
  font-family: var(--font-serif);
  font-weight: bold;
  font-size: 1.1rem;
  color: #3b2c1b;
}

#selected-spell-title {
  font-family: var(--font-serif);
  font-size: 1.6rem;
  color: #3b2c1b;
  margin-bottom: 10px;
}

.spell-desc {
  font-family: var(--font-book);
  font-size: 1.1rem;
  line-height: 1.4;
  color: #4a3c2c;
  margin-bottom: 20px;
}

.practice-box {
  background: rgba(0, 0, 0, 0.04);
  border: 2px dashed rgba(0, 0, 0, 0.15);
  border-radius: 8px;
  padding: 16px;
  margin-bottom: 20px;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 10px;
}

.practice-label {
  font-family: var(--font-serif);
  font-size: 0.8rem;
  text-transform: uppercase;
  letter-spacing: 1px;
  color: #705539;
}

.practice-gesture-feedback {
  position: relative;
  width: 180px;
  height: 180px;
  background: #fbf8f0;
  border: 1px solid rgba(139, 90, 43, 0.2);
  box-shadow: inset 0 2px 8px rgba(0,0,0,0.06);
  border-radius: 8px;
  display: flex;
  justify-content: space-around;
  align-items: center;
  overflow: hidden;
}

.target-gesture-display,
.detected-gesture-display {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  font-size: 3rem;
  width: 75px;
  height: 105px;
  border-radius: 6px;
  background: rgba(139, 90, 43, 0.05);
  border: 1px dashed rgba(139, 90, 43, 0.3);
  position: relative;
  padding-top: 15px;
  box-sizing: border-box;
}

.target-gesture-display::before {
  content: "GOAL";
  position: absolute;
  top: 6px;
  font-size: 0.65rem;
  font-family: var(--font-sans);
  color: #8b5a2b;
  font-weight: bold;
  letter-spacing: 0.5px;
}

.detected-gesture-display::before {
  content: "YOU";
  position: absolute;
  top: 6px;
  font-size: 0.65rem;
  font-family: var(--font-sans);
  color: #8b5a2b;
  font-weight: bold;
  letter-spacing: 0.5px;
}

/* Match animation & highlight */
.practice-gesture-feedback.matched {
  background: #e8f5e9;
  border-color: #4caf50;
  box-shadow: 0 0 10px rgba(76, 175, 80, 0.2);
}

.practice-gesture-feedback.matched .detected-gesture-display {
  background: rgba(76, 175, 80, 0.1);
  border: 1px solid #4caf50;
  animation: pulse-green 1s infinite alternate;
}

@keyframes pulse-green {
  0% { transform: scale(1); }
  100% { transform: scale(1.05); }
}

.practice-score {
  font-family: var(--font-serif);
  font-size: 0.85rem;
  color: #3b2c1b;
  font-weight: bold;
}

.controls-guide h4 {
  font-family: var(--font-serif);
  font-size: 0.9rem;
  color: #3b2c1b;
  margin-bottom: 6px;
}

.controls-guide p {
  font-family: var(--font-book);
  font-size: 0.95rem;
  color: #554433;
  margin-bottom: 4px;
  line-height: 1.3;
}

.magical-btn {
  display: inline-block;
  font-family: var(--font-serif);
  font-weight: 700;
  font-size: 0.9rem;
  letter-spacing: 1px;
  text-transform: uppercase;
  color: #fff;
  background: linear-gradient(135deg, hsl(20, 45%, 15%), var(--leather-brown));
  border: 2px solid var(--gold-metallic);
  padding: 10px 24px;
  border-radius: 6px;
  cursor: pointer;
  box-shadow: 0 4px 10px rgba(0,0,0,0.3);
  transition: all 0.3s ease;
  position: relative;
  overflow: hidden;
}

.magical-btn:hover {
  background: var(--gold-metallic);
  color: var(--leather-brown);
  box-shadow: 0 0 15px var(--gold-glow);
}

.magical-btn:active {
  transform: translateY(2px);
}

.grimoire-footer {
  margin-top: 20px;
  text-align: center;
}

/* ==========================================================================
   RESPONSIVE DESIGN & PORTRAIT FALLBACKS
   ========================================================================== */

/* Mobile Layout (Max-Width 768px or Portrait) */
@media (max-width: 768px), (orientation: portrait) {
  #hud-overlay {
    padding: 12px;
  }

  /* Shrink Webcam Mirror */
  .magic-mirror-frame {
    width: 100px;
    height: 75px;
    top: 12px;
    right: 12px;
  }
  
  .audio-controls {
    right: 124px;
    top: 12px;
  }

  .stats-panel {
    top: 12px;
    left: 12px;
    font-size: 0.7rem;
    padding: 6px 10px;
  }

  /* Scale down Stats Vials */
  .player-stats {
    bottom: 12px;
    left: 12px;
    gap: 12px;
  }

  .vial-glass {
    width: 36px;
    height: 100px;
  }

  .vial-glass::before {
    height: 90px;
    left: 6px;
  }

  .vial-value {
    font-size: 0.65rem;
  }

  /* Scale down casting feedback overlay */
  .casting-feedback {
    bottom: 12px;
  }
  
  .spell-symbol-glow {
    font-size: 1.6rem;
  }
  
  .spell-cast-status {
    font-size: 0.75rem;
    padding: 4px 10px;
  }

  /* Target info adjustments */
  .target-info {
    width: 220px;
    padding: 6px;
    top: 12px;
  }

  .target-name {
    font-size: 0.8rem;
  }

  /* Stack Grimoire Overlay to single page */
  .grimoire-book {
    flex-direction: column;
    width: 100%;
    max-width: 440px;
    height: 80vh;
    overflow-y: auto;
    border-width: 6px;
  }

  .grimoire-book::after {
    display: none; /* Hide page spine */
  }

  .book-page {
    flex: none;
    height: auto;
    padding: 24px;
  }

  .left-page {
    border-right: none;
    border-bottom: 2px solid var(--gold-metallic);
  }
  
  .right-page {
    border-left: none;
  }
  
  .book-title {
    font-size: 1.6rem;
  }
}

/* Animations & Transitions */
.book-page::-webkit-scrollbar {
  width: 6px;
}

.book-page::-webkit-scrollbar-track {
  background: transparent;
}

.book-page::-webkit-scrollbar-thumb {
  background: var(--parchment-dark);
  border-radius: 3px;
}

/* ==========================================================================
   INTERACTIVE TUTORIAL OVERLAY
   ========================================================================== */

#tutorial-overlay {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  z-index: 100;
  background: rgba(10, 8, 18, 0.85);
  backdrop-filter: blur(8px);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px;
  transition: opacity 0.5s ease-in-out;
}

.tutorial-card {
  background: rgba(30, 24, 48, 0.9);
  border: 2px solid var(--gold-glow);
  box-shadow: 0 0 35px rgba(223, 178, 244, 0.35);
  border-radius: 12px;
  padding: 30px;
  max-width: 520px;
  width: 100%;
  text-align: center;
  color: #eae7f0;
}

.tutorial-title {
  font-family: var(--font-serif);
  color: var(--gold-glow);
  font-size: 1.6rem;
  margin-bottom: 20px;
  text-shadow: 0 0 10px rgba(255, 215, 0, 0.4);
}

.tutorial-step {
  display: none;
  min-height: 250px;
}

.tutorial-step.active {
  display: block;
  animation: fadeInStep 0.4s ease-out;
}

@keyframes fadeInStep {
  from { opacity: 0; transform: translateY(10px); }
  to { opacity: 1; transform: translateY(0); }
}

.tutorial-icon {
  font-size: 3rem;
  margin-bottom: 12px;
  animation: tutorialFloat 2.5s ease-in-out infinite;
}

@keyframes tutorialFloat {
  0%, 100% { transform: translateY(0); }
  50% { transform: translateY(-8px); }
}

.tutorial-step h3 {
  font-family: var(--font-serif);
  font-size: 1.15rem;
  margin-bottom: 10px;
  color: #dfb2f4;
}

.tutorial-step p {
  font-size: 0.95rem;
  line-height: 1.4;
  color: #c9c3d6;
  margin-bottom: 14px;
}

/* Gesture Grid Legend */
.gesture-legend-grid {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 10px;
  margin-top: 15px;
}

.gesture-legend-item {
  background: rgba(255, 255, 255, 0.05);
  border: 1px solid rgba(255, 255, 255, 0.1);
  border-radius: 6px;
  padding: 10px;
  display: flex;
  align-items: center;
  gap: 12px;
  transition: all 0.2s ease;
}

.gesture-legend-item:hover {
  background: rgba(255, 255, 255, 0.1);
  border-color: var(--gold-glow);
}

.gesture-legend-glyph {
  font-size: 1.6rem;
  text-shadow: 0 0 8px rgba(0, 255, 200, 0.5);
  width: 32px;
  text-align: center;
}

.gesture-legend-name {
  font-size: 0.85rem;
  font-weight: 600;
  color: #eae7f0;
  line-height: 1.3;
}

.gesture-legend-name small {
  font-size: 0.7rem;
  color: var(--gold-glow);
  font-weight: normal;
}

/* Dots and Controls */
.tutorial-controls {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-top: 25px;
}

.tutorial-dots {
  display: flex;
  gap: 8px;
}

.tutorial-dots .dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: rgba(255, 255, 255, 0.25);
  transition: background 0.3s ease;
}

.tutorial-dots .dot.active {
  background: var(--gold-glow);
  box-shadow: 0 0 8px var(--gold-glow);
}

.magical-btn.secondary {
  background: transparent;
  border-color: rgba(255, 255, 255, 0.3);
  color: rgba(255, 255, 255, 0.7);
}

.magical-btn.secondary:hover:not(:disabled) {
  background: rgba(255, 255, 255, 0.05);
  color: #fff;
  border-color: #fff;
  box-shadow: none;
}

.magical-btn:disabled {
  opacity: 0.3;
  cursor: not-allowed;
}

@media (max-width: 480px) {
  .gesture-legend-grid {
    grid-template-columns: 1fr;
  }
  .tutorial-card {
    padding: 20px;
  }
}
