/* ============================================================
   THE BASILISK IS COMING — Style Sheet
   Mobile-first responsive + desktop support
   Church / Wizard theme, pixel-art inspired, forest greens
   ============================================================ */

@import url('https://fonts.googleapis.com/css2?family=MedievalSharp&family=Press+Start+2P&display=swap');

* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
  -webkit-tap-highlight-color: transparent;
}

html, body {
  width: 100%;
  height: 100%;
  overflow: hidden;
  background: #0a0a1a;
  color: #c4a882;
  font-family: 'MedievalSharp', 'Georgia', serif;
  touch-action: none;
  -webkit-user-select: none;
  user-select: none;
}

body {
  min-height: 100dvh;
}

/* ============================================================
   GAME CONTAINER — scales to fit any screen
   ============================================================ */
#game-container {
  position: relative;
  width: 100vmin;
  height: 75vmin;  /* 4:3 aspect ratio */
  max-width: 100vw;
  max-height: 100vh;
  margin: 0 auto;
}

@media (min-aspect-ratio: 4/3) {
  #game-container {
    width: 133.33vmin;
    height: 100vmin;
    max-width: 100vw;
    max-height: 100vh;
  }
}

@media (orientation: portrait) {
  #game-container {
    width: 100vw;
    height: 100dvh;
    max-height: 100vh;
  }
}

#game-canvas {
  display: block;
  position: absolute;
  top: 0; left: 0;
  width: 100%;
  height: 100%;
  background: #0a0a1a;
  cursor: default;
  border: 2px solid #2d4a27;
  box-shadow: 0 0 40px rgba(107, 47, 160, 0.3);
  image-rendering: pixelated;
  image-rendering: crisp-edges;
  touch-action: none;
}

#fullscreen-btn {
  position: absolute;
  top: 8px;
  right: 8px;
  background: rgba(10, 10, 26, 0.7);
  color: #c4a882;
  border: 1px solid #2d4a27;
  font-size: 20px;
  padding: 4px 10px;
  cursor: pointer;
  z-index: 100;
  font-family: 'Press Start 2P', monospace;
  transition: background 0.2s;
}

#fullscreen-btn:hover { background: rgba(107, 47, 160, 0.4); }

/* Hide fullscreen btn on mobile (use system fullscreen) */
@media (pointer: coarse) {
  #fullscreen-btn { display: none; }
}

/* ============================================================
   MOBILE CONTROLS OVERLAY
   ============================================================ */
#mobile-controls {
  display: none; /* Shown on touch devices via JS or media query */
  position: absolute;
  top: 0; left: 0;
  width: 100%; height: 100%;
  pointer-events: none;
  z-index: 50;
}

@media (pointer: coarse) {
  #mobile-controls { display: none; }
  #game-container[data-game-state="GAMEPLAY"] #mobile-controls,
  #game-container[data-game-state="CAMP_REST"] #mobile-controls { display: block; }
}

/* ---------- D-Pad ---------- */
#dpad {
  position: absolute;
  bottom: 20px;
  left: 15px;
  pointer-events: auto;
}

.dpad-ring {
  position: relative;
  width: 140px;
  height: 140px;
}

.dpad-btn {
  position: absolute;
  width: 44px;
  height: 44px;
  background: rgba(45, 74, 39, 0.6);
  border: 2px solid #4a6b42;
  border-radius: 6px;
  color: #c4a882;
  font-size: 18px;
  font-weight: bold;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  -webkit-tap-highlight-color: rgba(107, 47, 160, 0.3);
  transition: background 0.1s;
  touch-action: none;
}

.dpad-btn:active {
  background: rgba(107, 47, 160, 0.5);
  border-color: #6b2fa0;
}

.dpad-up    { top: 0; left: 48px; }
.dpad-down  { bottom: 0; left: 48px; }
.dpad-left  { top: 48px; left: 0; }
.dpad-right { top: 48px; right: 0; }

.dpad-center {
  position: absolute;
  top: 48px; left: 48px;
  width: 44px; height: 44px;
  background: rgba(26, 26, 46, 0.8);
  border-radius: 6px;
  border: 1px solid #2d4a27;
}

/* Larger D-pad on very small screens */
@media (max-height: 500px) {
  .dpad-ring { width: 110px; height: 110px; }
  .dpad-btn { width: 36px; height: 36px; font-size: 14px; }
  .dpad-up    { top: 0; left: 37px; }
  .dpad-down  { bottom: 0; left: 37px; }
  .dpad-left  { top: 37px; left: 0; }
  .dpad-right { top: 37px; right: 0; }
  .dpad-center { top: 37px; left: 37px; width: 36px; height: 36px; }
}

/* ---------- Action Buttons ---------- */
#action-buttons {
  position: absolute;
  bottom: 25px;
  right: 15px;
  display: flex;
  flex-direction: column;
  gap: 10px;
  pointer-events: auto;
}

@media (pointer: fine) {
  #action-buttons { display: none; }
}

/* In combat, the canvas has its own enlarged 2×2 action grid. Hide the
   exploration overlay so the D-pad/action stack cannot cover Attack/Defend. */
#game-container[data-game-state="COMBAT"] #mobile-controls {
  display: none;
}

#game-container[data-modal-open="true"] #mobile-controls {
  display: none !important;
}

.action-btn {
  width: 52px;
  height: 52px;
  background: rgba(45, 74, 39, 0.6);
  border: 2px solid #4a6b42;
  border-radius: 50%;
  font-size: 22px;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  -webkit-tap-highlight-color: rgba(107, 47, 160, 0.3);
  transition: background 0.1s;
  touch-action: none;
}

.action-btn:active {
  background: rgba(107, 47, 160, 0.5);
  border-color: #6b2fa0;
}

.action-btn.active {
  background: rgba(107, 47, 160, 0.4);
  border-color: #8b6914;
}

/* ============================================================
   MOBILE MENU MODAL
   ============================================================ */
.mobile-modal {
  position: absolute;
  top: 0; left: 0;
  width: 100%; height: 100%;
  background: rgba(0, 0, 0, 0.7);
  z-index: 200;
  display: flex;
  align-items: center;
  justify-content: center;
  pointer-events: auto;
}

.mobile-modal.hidden { display: none; }

.menu-panel {
  background: rgba(10, 10, 26, 0.95);
  border: 2px solid #4a6b42;
  border-radius: 12px;
  padding: 24px;
  min-width: 240px;
  max-width: 85vw;
  text-align: center;
}

.menu-panel h3 {
  font-family: 'Press Start 2P', monospace;
  font-size: 12px;
  color: #8b6914;
  margin-bottom: 16px;
  text-transform: uppercase;
  letter-spacing: 2px;
}

.menu-option {
  display: block;
  width: 100%;
  padding: 12px 16px;
  margin: 6px 0;
  background: rgba(45, 74, 39, 0.3);
  border: 1px solid #2d4a27;
  border-radius: 8px;
  color: #c4a882;
  font-family: 'MedievalSharp', serif;
  font-size: 16px;
  cursor: pointer;
  transition: background 0.15s;
}

.menu-option:active {
  background: rgba(107, 47, 160, 0.4);
}

.menu-option:disabled {
  opacity: 0.5;
  cursor: default;
}

.menu-option.menu-close {
  margin-top: 12px;
  background: rgba(139, 58, 58, 0.3);
  border-color: #8b3a3a;
}

/* ============================================================
   TITLE SCREEN (Canvas-rendered — CSS fallbacks)
   ============================================================ */
.title-bg { fill: #0a0a1a; }

.title-main {
  font-family: 'Press Start 2P', monospace;
  font-size: 28px;
  fill: #c4a882;
  text-anchor: middle;
  filter: drop-shadow(2px 2px 0 #6b2fa0);
}

.title-sub {
  font-family: 'MedievalSharp', serif;
  font-size: 16px;
  fill: #8b6914;
  text-anchor: middle;
}

.title-sigil {
  font-size: 48px;
  fill: #6b2fa0;
  text-anchor: middle;
}

.menu-btn {
  font-family: 'Press Start 2P', monospace;
  font-size: 14px;
  fill: #c4a882;
  text-anchor: middle;
  cursor: pointer;
  transition: fill 0.15s;
}

-menu-btn:hover {
  fill: #8b6914;
}

/* ============================================================
   HUD ELEMENTS
   ============================================================ */
.hud-panel {
  position: absolute;
  background: rgba(10, 10, 26, 0.85);
  border: 1px solid #2d4a27;
  border-radius: 6px;
  padding: 8px 12px;
  pointer-events: none;
  font-size: 11px;
}

.hud-top-left { top: 8px; left: 8px; }
.hud-top-right { top: 8px; right: 38px; }
.hud-bottom { bottom: 8px; left: 50%; transform: translateX(-50%); }

@media (pointer: coarse) {
  .hud-top-left { top: 8px; left: 8px; }
  .hud-top-right { top: 8px; right: 8px; }
  /* Leave room for D-pad on left */
  .hud-bottom { bottom: 160px; left: 50%; transform: translateX(-50%); }
}

/* ============================================================
   COMBAT UI
   ============================================================ */
.combat-arena {
  display: flex;
  justify-content: space-around;
  align-items: center;
  padding: 20px;
}

.combat-entity {
  text-align: center;
}

.combat-hp-bar {
  width: 120px;
  height: 12px;
  background: #1a1a2e;
  border: 1px solid #4a6b42;
  border-radius: 3px;
  overflow: hidden;
  margin: 6px auto;
}

.combat-hp-fill {
  height: 100%;
  background: #4a8b4a;
  transition: width 0.3s;
}

.combat-log {
  background: rgba(10, 10, 26, 0.9);
  border: 1px solid #2d4a27;
  border-radius: 6px;
  padding: 10px;
  max-height: 120px;
  overflow-y: auto;
  font-size: 12px;
  line-height: 1.6;
}

.combat-actions {
  display: flex;
  justify-content: center;
  gap: 8px;
  flex-wrap: wrap;
  padding: 10px;
}

.combat-action-btn {
  padding: 10px 18px;
  background: rgba(45, 74, 39, 0.4);
  border: 1px solid #4a6b42;
  border-radius: 6px;
  color: #c4a882;
  font-family: 'MedievalSharp', serif;
  font-size: 14px;
  cursor: pointer;
  pointer-events: auto;
}

.combat-action-btn:active {
  background: rgba(107, 47, 160, 0.5);
}

@media (pointer: coarse) {
  .combat-action-btn {
    padding: 14px 22px;
    font-size: 16px;
    min-width: 80px;
  }
}

/* ============================================================
   INVENTORY PANEL
   ============================================================ */
.inventory-panel {
  position: absolute;
  top: 50%; left: 50%;
  transform: translate(-50%, -50%);
  background: rgba(10, 10, 26, 0.95);
  border: 2px solid #4a6b42;
  border-radius: 10px;
  padding: 16px;
  min-width: 280px;
  max-width: 90vw;
  max-height: 80vh;
  overflow-y: auto;
  pointer-events: auto;
  z-index: 150;
}

.inventory-panel.hidden { display: none; }

.inventory-title {
  font-family: 'Press Start 2P', monospace;
  font-size: 12px;
  color: #8b6914;
  text-align: center;
  margin-bottom: 12px;
}

.inventory-item {
  display: flex;
  align-items: center;
  padding: 8px 10px;
  margin: 4px 0;
  background: rgba(45, 74, 39, 0.2);
  border: 1px solid transparent;
  border-radius: 6px;
  font-size: 13px;
  cursor: pointer;
}

.inventory-item.selected {
  border-color: #8b6914;
  background: rgba(139, 105, 20, 0.2);
}

.inventory-item-name { flex: 1; }
.inventory-item-type { font-size: 10px; color: #6b6b7a; margin-left: 8px; }

.inventory-controls {
  text-align: center;
  margin-top: 10px;
  padding-top: 10px;
  border-top: 1px solid #2d4a27;
  font-size: 11px;
  color: #6b6b7a;
}

@media (pointer: coarse) {
  .inventory-item { padding: 14px 12px; font-size: 16px; }
  .inventory-item:active { background: rgba(107, 47, 160, 0.4); }
}

/* ============================================================
   CLASS SELECT
   ============================================================ */
.class-cards {
  display: flex;
  justify-content: center;
  gap: 12px;
  flex-wrap: wrap;
  padding: 16px;
}

.class-card {
  background: rgba(10, 10, 26, 0.8);
  border: 2px solid #2d4a27;
  border-radius: 10px;
  padding: 16px;
  min-width: 160px;
  max-width: 200px;
  text-align: center;
  cursor: pointer;
  transition: border-color 0.15s, background 0.15s;
}

.class-card:hover, .class-card.selected {
  border-color: #8b6914;
  background: rgba(139, 105, 20, 0.15);
}

.class-card-icon { font-size: 40px; margin-bottom: 8px; }
.class-card-name { font-family: 'Press Start 2P', monospace; font-size: 11px; color: #c4a882; margin-bottom: 6px; }
.class-card-desc { font-size: 12px; color: #8b8b9a; line-height: 1.4; }
.class-card-stats { font-size: 11px; color: #6b6b7a; margin-top: 8px; text-align: left; }

@media (pointer: coarse) {
  .class-cards { flex-direction: column; align-items: center; }
  .class-card { width: 85vw; max-width: 300px; padding: 20px; }
  .class-card-icon { font-size: 48px; }
  .class-card-name { font-size: 13px; }
  .class-card-desc { font-size: 14px; }
}

/* ============================================================
   CAMP REST SCREEN
   ============================================================ */
.camp-container {
  text-align: center;
  padding: 40px 20px;
}

.camp-fire {
  font-size: 64px;
  animation: flicker 2s ease-in-out infinite;
}

@keyframes flicker {
  0%, 100% { opacity: 1; }
  50% { opacity: 0.85; transform: scale(1.05); }
}

.camp-message {
  font-size: 16px;
  color: #8b6914;
  margin: 16px 0;
}

.camp-btn {
  padding: 12px 28px;
  background: rgba(45, 74, 39, 0.4);
  border: 1px solid #4a6b42;
  border-radius: 6px;
  color: #c4a882;
  font-family: 'MedievalSharp', serif;
  font-size: 16px;
  cursor: pointer;
}

.camp-btn:active { background: rgba(107, 47, 160, 0.4); }

/* ============================================================
   GAME OVER / VICTORY
   ============================================================ */
.end-screen {
  text-align: center;
  padding: 40px 20px;
}

.end-title {
  font-family: 'Press Start 2P', monospace;
  font-size: 24px;
  margin-bottom: 20px;
}

.end-stats {
  font-size: 14px;
  color: #a8a8c0;
  line-height: 2;
  margin-bottom: 24px;
}

.end-btn {
  display: block;
  width: 200px;
  margin: 8px auto;
  padding: 12px;
  background: rgba(45, 74, 39, 0.4);
  border: 1px solid #4a6b42;
  border-radius: 6px;
  color: #c4a882;
  font-family: 'MedievalSharp', serif;
  font-size: 14px;
  cursor: pointer;
}

.end-btn:active { background: rgba(107, 47, 160, 0.4); }

/* ============================================================
   TABLET ADJUSTMENTS
   ============================================================ */
@media (min-width: 768px) and (max-width: 1024px) {
  .dpad-ring { width: 130px; height: 130px; }
  .dpad-btn { width: 42px; height: 42px; }
}

/* ============================================================
   LANDSCAPE MOBILE (phone sideways)
   ============================================================ */
@media (pointer: coarse) and (orientation: landscape) {
  #game-container {
    width: 100vw;
    height: 100vh;
  }

  #dpad {
    bottom: 10px;
    left: 10px;
  }

  .dpad-ring { width: 100px; height: 100px; }
  .dpad-btn { width: 32px; height: 32px; font-size: 13px; }
  .dpad-up    { top: 0; left: 34px; }
  .dpad-down  { bottom: 0; left: 34px; }
  .dpad-left  { top: 34px; left: 0; }
  .dpad-right { top: 34px; right: 0; }
  .dpad-center { top: 34px; left: 34px; width: 32px; height: 32px; }

  #action-buttons {
    bottom: 10px;
    right: 10px;
    gap: 6px;
  }

  .action-btn { width: 44px; height: 44px; font-size: 18px; }
}

/* ============================================================
   ACCESSIBILITY / REDUCED MOTION
   ============================================================ */
@media (prefers-reduced-motion: reduce) {
  .camp-fire { animation: none; }
}

/* ============================================================
   MOBILE FALLBACK FIX — do not rely only on pointer: coarse
   Some phones / browser shells report pointer media queries unreliably.
   ============================================================ */
@media (max-width: 900px), (max-height: 700px) {
  #mobile-controls {
    display: none !important;
  }

  #game-container[data-game-state="GAMEPLAY"] #mobile-controls,
  #game-container[data-game-state="CAMP_REST"] #mobile-controls {
    display: block !important;
  }

  #action-buttons {
    display: none !important;
  }

  #game-container[data-game-state="GAMEPLAY"] #action-buttons,
  #game-container[data-game-state="CAMP_REST"] #action-buttons {
    display: flex !important;
  }

  #fullscreen-btn {
    display: none !important;
  }
}

#game-container[data-game-state="COMBAT"] #mobile-controls,
#game-container[data-game-state="COMBAT"] #action-buttons {
  display: none !important;
}

/* Ensure hidden modals stay hidden even when mobile fallback is active */
.hidden {
  display: none !important;
}

/* Final mobile-modal override: keep DOM controls away from canvas modal UIs.
   Placed last on purpose so it beats GAMEPLAY/CAMP display:block rules. */
@media (pointer: coarse) {
  #game-container[data-modal-open="true"] #mobile-controls {
    display: none !important;
    visibility: hidden !important;
    pointer-events: none !important;
  }

  #game-container[data-game-state="GAMEPLAY"][data-inventory-open="true"] #mobile-controls {
    display: block !important;
    visibility: visible !important;
    /* Keep the D-pad visible during inventory, but don't let the full-screen
       overlay swallow taps meant for the canvas-drawn Use/Inspect/etc buttons. */
    pointer-events: none !important;
  }
}
