* { margin: 0; padding: 0; box-sizing: border-box; }
body {
  background: #0a0a12;
  color: #c8b89a;
  font-family: 'Courier New', monospace;
  height: 100vh;
  height: 100dvh; /* dynamic viewport height — accounts for Firefox/mobile browser chrome */
  overflow: hidden;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
}

#title-screen {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 2rem;
  width: 100%;
  height: 100%;
  background: radial-gradient(ellipse at center, #12122a 0%, #0a0a12 100%);
  position: relative;
  z-index: 10;
  pointer-events: auto;
}

#title-screen h1 {
  font-size: 3rem;
  letter-spacing: 0.5em;
  color: #d4a438;
  text-shadow: 0 0 40px rgba(212,164,56,0.6), 0 0 80px rgba(212,164,56,0.2);
  text-transform: uppercase;
  animation: pulse 3s ease-in-out infinite;
}

@keyframes pulse {
  0%,100% { text-shadow: 0 0 40px rgba(212,164,56,0.6); }
  50% { text-shadow: 0 0 60px rgba(212,164,56,0.9), 0 0 100px rgba(212,164,56,0.4); }
}

#title-screen .subtitle {
  color: #6a5a4a;
  letter-spacing: 0.3em;
  font-size: 0.8rem;
}

.class-grid {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 1rem;
  max-width: 600px;
}

.class-card {
  background: #12121e;
  border: 1px solid #2a2a4a;
  border-radius: 4px;
  padding: 1.2rem;
  cursor: pointer;
  transition: all 0.2s;
  text-align: center;
}

.class-card:hover {
  border-color: #d4a438;
  background: #1a1a2e;
  transform: translateY(-2px);
  box-shadow: 0 4px 20px rgba(212,164,56,0.2);
}

.class-card .icon { font-size: 2rem; margin-bottom: 0.5rem; }
.class-card .name { color: #d4a438; font-weight: bold; letter-spacing: 0.1em; }
.class-card .desc { font-size: 0.7rem; color: #6a5a4a; margin-top: 0.3rem; }

/* ── Game Layout ── */
#game-screen {
  display: none;
  width: 100%;
  height: 100%;
  flex-direction: column;
  /* Sits above the Three.js canvas */
  position: relative;
  z-index: 1;
  /* Transparent background — Three.js shows behind */
  background: transparent;
  pointer-events: none; /* pass clicks through to 3D canvas by default */
}

/* Re-enable pointer events on interactive elements */
#hud,
#log-bar,
#combat-overlay,
#dialog-overlay,
#equip-screen,
#stats-popup,
#mobile-hud,
#mobile-actions,
#dpad,
#minimap,
#key-hints,
#game-screen button,
#game-screen input,
.combat-btn,
.dialog-choice,
#dialog-chat-input,
#dialog-chat-send,
#dialog-close,
.hud-btn,
.mob-btn,
.inv-equip-btn,
.inv-unequip-btn {
  pointer-events: auto;
}

#game-top {
  display: flex;
  gap: 0;
  flex: 1;
  overflow: hidden;
  background: transparent;
}

#canvas-wrap {
  position: relative;
  flex: 1;
  background: transparent;
}

/* The 2D game canvas is kept for backward compat but hidden */
#gameCanvas {
  display: block;
  image-rendering: pixelated;
  width: 1px;
  height: 1px;
  opacity: 0;
  pointer-events: none;
  position: absolute;
}

/* Three.js canvas is inserted by rendering3d.js as a fixed overlay */
#threeCanvas {
  position: fixed;
  top: 0; left: 0;
  width: 100%; height: 100%;
  z-index: 0;
}

/* ── Save banner on title screen ── */
#save-banner {
  background: #12121e;
  border: 1px solid #d4a43844;
  border-radius: 4px;
  padding: 0.8rem 1.2rem;
  text-align: center;
}
.save-btn {
  background: #1a1a2e;
  border: 1px solid #d4a43866;
  border-radius: 3px;
  padding: 0.5rem 1.2rem;
  color: #d4a438;
  font-family: inherit;
  font-size: 0.8rem;
  cursor: pointer;
  transition: all 0.15s;
}
.save-btn:hover { background: #252540; border-color: #d4a438; }
.danger-btn { border-color: #cc333366; color: #ff6666; }
.danger-btn:hover { background: #2a1a1a; border-color: #cc3333; }

/* ── HUD utility button ── */
.hud-btn {
  display: block;
  width: 100%;
  background: #1a1a2e;
  border: 1px solid #2a2a4a;
  border-radius: 3px;
  padding: 0.3rem 0.5rem;
  color: #c8b89a;
  font-family: inherit;
  font-size: 0.68rem;
  cursor: pointer;
  text-align: left;
  transition: all 0.15s;
}
.hud-btn:hover { border-color: #d4a438; color: #d4a438; }

/* ── Status effect bar ── */
.status-bar {
  font-size: 0.7rem;
  min-height: 1.1rem;
  margin-top: 2px;
  color: #ffcc44;
  letter-spacing: 0.05em;
}

/* ── Side HUD ── */
#hud {
  position: fixed;
  right: 0;
  top: 0;
  bottom: 120px; /* leave space for log */
  width: 220px;
  background: rgba(13,13,26,0.92);
  border-left: 1px solid #2a2a4a;
  display: flex;
  flex-direction: column;
  padding: 0.75rem;
  gap: 0.6rem;
  overflow-y: auto;
  font-size: 0.75rem;
  z-index: 50;
  pointer-events: auto;
  backdrop-filter: blur(4px);
}

.hud-section {
  background: #12121e;
  border: 1px solid #2a2a4a;
  border-radius: 3px;
  padding: 0.5rem 0.7rem;
}

.hud-label {
  color: #6a5a4a;
  font-size: 0.6rem;
  letter-spacing: 0.2em;
  text-transform: uppercase;
  margin-bottom: 0.3rem;
}

.hud-name { color: #d4a438; font-size: 0.9rem; font-weight: bold; }
.hud-class { color: #7a5af8; font-size: 0.7rem; }

.stat-bar-wrap { margin: 0.2rem 0; }
.stat-label { display: flex; justify-content: space-between; color: #c8b89a; margin-bottom: 2px; }
.stat-bar {
  height: 6px;
  background: #1a1a2e;
  border-radius: 3px;
  overflow: hidden;
}
.stat-fill {
  height: 100%;
  border-radius: 3px;
  transition: width 0.3s;
}
.hp-fill { background: linear-gradient(90deg, #cc3333, #ff6666); }
.mp-fill { background: linear-gradient(90deg, #3366cc, #66aaff); }
.xp-fill { background: linear-gradient(90deg, #44aa44, #88ff88); }

.stat-row { display: flex; justify-content: space-between; margin: 0.15rem 0; }
.stat-row span:first-child { color: #6a5a4a; }
.stat-row span:last-child { color: #c8b89a; }

#inventory-list { list-style: none; }
#inventory-list li {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 0.15rem 0;
  border-bottom: 1px solid #1a1a2e;
  gap: 4px;
}
#inventory-list li:hover { color: #d4a438; }

.inv-equip-btn, .inv-unequip-btn {
  background: #1a1a2e;
  border: 1px solid #d4a43844;
  border-radius: 2px;
  color: #d4a438;
  font-size: 0.58rem;
  padding: 1px 5px;
  cursor: pointer;
  font-family: inherit;
  white-space: nowrap;
  flex-shrink: 0;
}
.inv-equip-btn:hover { background: #252540; border-color: #d4a438; }
.inv-unequip-btn { color: #888; border-color: #88888844; }
.inv-unequip-btn:hover { color: #ff6666; border-color: #ff6666; }

/* ── Bottom Log ── */
#log-bar {
  position: fixed;
  bottom: 0;
  left: 0;
  right: 220px; /* beside HUD */
  height: 120px;
  background: rgba(13,13,26,0.88);
  border-top: 1px solid #2a2a4a;
  display: flex;
  gap: 0;
  z-index: 50;
  pointer-events: auto;
  backdrop-filter: blur(4px);
}

#narrative-log {
  flex: 1;
  padding: 0.6rem 0.8rem;
  overflow-y: auto;
  display: flex;
  flex-direction: column-reverse;
}

.log-entry {
  padding: 0.2rem 0;
  line-height: 1.4;
  font-size: 0.78rem;
  border-bottom: 1px solid #12121e;
  animation: fadeIn 0.3s ease;
}

@keyframes fadeIn { from { opacity: 0; transform: translateY(4px); } to { opacity: 1; transform: none; } }

.log-combat { color: #ff8888; }
.log-action { color: #88ddff; }
.log-loot { color: #ffcc44; }
.log-story { color: #c8b89a; font-style: italic; }
.log-system { color: #6a5a4a; }
.log-npc { color: #88ff88; }

/* ── Dialog Box ── */
#dialog-overlay {
  display: none;
  position: fixed;
  top: 0; left: 0; right: 0; bottom: 0;
  background: rgba(0,0,0,0.5);
  align-items: flex-end;
  justify-content: center;
  padding: 1rem;
  z-index: 100;
  pointer-events: auto;
}

#dialog-box {
  background: #12121e;
  border: 2px solid #d4a438;
  border-radius: 4px;
  padding: 1rem 1.2rem;
  max-width: 600px;
  width: 100%;
  position: relative;
}

.dialog-speaker {
  color: #d4a438;
  font-size: 0.8rem;
  letter-spacing: 0.15em;
  margin-bottom: 0.3rem;
  text-transform: uppercase;
  transition: color 0.4s ease;
}

/* ── Reputation bar ── */
#rep-bar-wrap {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  margin-bottom: 0.5rem;
}
#rep-bar {
  flex: 1;
  height: 5px;
  background: #2a2a3a;
  border-radius: 3px;
  overflow: hidden;
}
#rep-fill {
  height: 100%;
  width: 50%;
  background: #d4a438;
  border-radius: 3px;
  transition: width 0.5s ease, background 0.5s ease;
}
#rep-label {
  font-size: 0.6rem;
  color: #888;
  white-space: nowrap;
  min-width: 80px;
  text-align: right;
}

.dialog-text {
  color: #e8d8b8;
  font-size: 0.85rem;
  line-height: 1.6;
  min-height: 2rem;
}

.dialog-choices {
  margin-top: 0.8rem;
  display: flex;
  flex-direction: column;
  gap: 0.3rem;
}

.dialog-choice {
  background: #1a1a2e;
  border: 1px solid #2a2a4a;
  border-radius: 3px;
  padding: 0.4rem 0.7rem;
  cursor: pointer;
  font-family: inherit;
  font-size: 0.78rem;
  color: #c8b89a;
  text-align: left;
  transition: all 0.15s;
}
.dialog-choice:hover {
  border-color: #d4a438;
  color: #d4a438;
  background: #1e1e34;
}

#dialog-close {
  position: absolute;
  top: 0.5rem;
  right: 0.7rem;
  background: none;
  border: none;
  color: #6a5a4a;
  font-size: 1rem;
  cursor: pointer;
  font-family: inherit;
}

/* ── Dialog History & Free-form Chat ── */
#dialog-history {
  max-height: 120px;
  overflow-y: auto;
  margin-top: 0.5rem;
  padding: 0.4rem 0;
  border-top: 1px solid #2a2a3e;
  display: flex;
  flex-direction: column;
  gap: 0.35rem;
  scrollbar-width: thin;
  scrollbar-color: #2a2a4a transparent;
}
#dialog-history:empty { display: none; }

.hist-player {
  color: #88bbff;
  font-size: 0.75rem;
}
.hist-player::before { content: 'You: '; color: #5588cc; }

.hist-npc {
  color: #e8d8b8;
  font-size: 0.75rem;
  padding-left: 0.8rem;
  border-left: 2px solid #d4a43844;
}

#dialog-chat-wrap {
  margin-top: 0.8rem;
  padding-top: 0.6rem;
  border-top: 1px solid #2a2a3e;
}

.dialog-chat-label {
  color: #6a5a4a;
  font-size: 0.65rem;
  letter-spacing: 0.1em;
  margin-bottom: 0.3rem;
}

.dialog-chat-row {
  display: flex;
  gap: 0.4rem;
}

#dialog-chat-input {
  flex: 1;
  background: #0e0e1a;
  border: 1px solid #2a2a4a;
  border-radius: 3px;
  padding: 0.4rem 0.6rem;
  color: #e8d8b8;
  font-family: inherit;
  font-size: 0.78rem;
  outline: none;
}
#dialog-chat-input:focus { border-color: #d4a43888; }
#dialog-chat-input::placeholder { color: #3a3a5a; }

#dialog-chat-send {
  background: #1a1a2e;
  border: 1px solid #d4a43866;
  border-radius: 3px;
  padding: 0.4rem 0.8rem;
  color: #d4a438;
  font-family: inherit;
  font-size: 0.75rem;
  cursor: pointer;
  transition: all 0.15s;
  white-space: nowrap;
}
#dialog-chat-send:hover { background: #252540; border-color: #d4a438; }
#dialog-chat-send:disabled { opacity: 0.4; cursor: not-allowed; }

/* ── Combat Overlay ── */
#combat-overlay {
  display: none;
  position: fixed;
  top: 0; left: 0; right: 0; bottom: 0;
  background: rgba(5,5,15,0.92);
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 1rem;
  z-index: 200;
  pointer-events: auto;
}

/* 3D combat mode — overlay becomes transparent so 3D shows through.
   Narration + buttons sit at bottom, 3D renders behind everything. */
#combat-overlay.combat-3d-mode {
  background: transparent;
  justify-content: flex-end;
  padding-bottom: 2.5rem;
}

#combat-overlay.combat-3d-mode #combat-scene {
  display: none;
}

/* Bottom HUD strip for 3D mode narration + buttons */
#combat-overlay.combat-3d-mode #combat-narration {
  background: rgba(10,10,20,0.88);
  border-color: rgba(100,80,200,0.5);
  max-width: 600px;
  width: 90%;
}

#combat-overlay.combat-3d-mode #combat-actions {
  /* stays same, just repositioned by flex-end */
}

#combat-scene {
  display: flex;
  gap: 4rem;
  align-items: center;
  justify-content: center;
  margin-bottom: 1rem;
}

.combatant {
  text-align: center;
  min-width: 120px;
}

#player-sprite-canvas {
  display: block;
  margin: 0 auto 0.5rem;
  filter: drop-shadow(0 0 8px rgba(212,164,56,0.5));
  animation: combatFloat 2s ease-in-out infinite;
}

#enemy-sprite-canvas {
  display: block;
  margin: 0 auto 0.5rem;
  filter: drop-shadow(0 0 8px rgba(255,80,80,0.4));
  animation: enemyFloat 1.5s ease-in-out infinite;
}

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

@keyframes enemyFloat {
  0%,100% { transform: scaleX(-1) translateY(0); }
  50% { transform: scaleX(-1) translateY(-6px); }
}

.combatant-name { color: #d4a438; font-weight: bold; margin-bottom: 0.3rem; }
.combatant-hp { font-size: 0.75rem; color: #c8b89a; }
.combatant-hp-bar { width: 120px; height: 8px; background: #1a1a2e; border-radius: 4px; margin: 0.3rem auto; overflow: hidden; }
.combatant-hp-fill { height: 100%; border-radius: 4px; background: linear-gradient(90deg, #cc3333, #ff6666); transition: width 0.4s; }

#combat-narration {
  max-width: 500px;
  text-align: center;
  color: #e8d8b8;
  font-style: italic;
  font-size: 0.85rem;
  min-height: 2.5rem;
  padding: 0.5rem 1rem;
  background: #12121e;
  border: 1px solid #2a2a4a;
  border-radius: 3px;
}

#combat-actions {
  display: flex;
  gap: 0.5rem;
  flex-wrap: wrap;
  justify-content: center;
}

.combat-btn {
  background: #12121e;
  border: 1px solid #2a2a4a;
  border-radius: 3px;
  padding: 0.5rem 1rem;
  color: #c8b89a;
  font-family: inherit;
  font-size: 0.8rem;
  cursor: pointer;
  transition: all 0.15s;
  min-width: 80px;
}
.combat-btn:hover:not(:disabled) {
  border-color: #d4a438;
  color: #d4a438;
  background: #1a1a2e;
}
.combat-btn:disabled {
  opacity: 0.4;
  cursor: default;
}
.combat-btn.danger { border-color: #cc3333; }
.combat-btn.magic { border-color: #7a5af8; }

.vs-text {
  color: #6a5a4a;
  font-size: 1.5rem;
  font-weight: bold;
}

/* Mini map */
#minimap {
  position: fixed;
  top: 8px;
  right: 230px; /* sits left of HUD */
  width: 80px;
  height: 80px;
  background: rgba(0,0,0,0.7);
  border: 1px solid #2a2a4a;
  border-radius: 3px;
  z-index: 50;
  pointer-events: none;
}

/* Key hints */
#key-hints {
  position: fixed;
  bottom: 125px; /* above log bar */
  left: 6px;
  font-size: 0.6rem;
  color: #3a3a5a;
  z-index: 5;
  pointer-events: none;
}

/* Loading spinner */
.loading-dot::after {
  content: '';
  animation: dots 1s steps(4,end) infinite;
}
@keyframes dots {
  0%,100% { content: ''; }
  25% { content: '.'; }
  50% { content: '..'; }
  75% { content: '...'; }
}

/* ── Virtual D-Pad (mobile) ── */
#dpad {
  display: none;
  position: fixed;
  bottom: 60px;
  left: 16px;
  z-index: 60;
  width: 130px;
  height: 130px;
  user-select: none;
  -webkit-user-select: none;
  pointer-events: auto;
}

.dpad-btn {
  position: absolute;
  width: 40px;
  height: 40px;
  background: rgba(30,30,60,0.75);
  border: 2px solid rgba(212,164,56,0.5);
  border-radius: 8px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 18px;
  color: #d4a438;
  cursor: pointer;
  touch-action: none;
  transition: background 0.1s;
  -webkit-tap-highlight-color: transparent;
}

.dpad-btn:active, .dpad-btn.pressed {
  background: rgba(212,164,56,0.3);
  border-color: #d4a438;
}

#dpad-up    { top: 0;   left: 45px; }
#dpad-down  { bottom: 0; left: 45px; }
#dpad-left  { left: 0;  top: 45px; }
#dpad-right { right: 0; top: 45px; }
#dpad-center {
  position: absolute;
  top: 45px; left: 45px;
  width: 40px; height: 40px;
  background: rgba(20,20,40,0.5);
  border: 1px solid #2a2a4a;
  border-radius: 50%;
}

/* Action buttons (mobile) */
#mobile-actions {
  display: none;
  position: fixed;
  bottom: 60px;
  right: 8px;
  z-index: 60;
  flex-direction: column;
  gap: 8px;
  align-items: flex-end;
  pointer-events: auto;
}

.mob-btn {
  background: rgba(30,30,60,0.8);
  border: 2px solid rgba(212,164,56,0.5);
  border-radius: 10px;
  padding: 8px 14px;
  color: #d4a438;
  font-family: 'Courier New', monospace;
  font-size: 14px;
  font-weight: bold;
  cursor: pointer;
  touch-action: manipulation;
  -webkit-tap-highlight-color: transparent;
}
.mob-btn:active { background: rgba(212,164,56,0.3); }

/* Mobile HUD bar (top strip) */
#mobile-hud {
  display: none;
  position: fixed;
  top: 0; left: 0; right: 0;
  background: rgba(10,10,18,0.88);
  border-bottom: 1px solid #2a2a4a;
  padding: 4px 8px;
  z-index: 60;
  flex-direction: row;
  align-items: center;
  gap: 8px;
  font-size: 11px;
  pointer-events: auto;
  backdrop-filter: blur(4px);
}
.mob-stat { display: flex; flex-direction: column; gap: 2px; min-width: 60px; }
.mob-stat-label { color: #6a5a4a; font-size: 9px; letter-spacing: 0.1em; }
.mob-bar-wrap { height: 5px; background: #1a1a2e; border-radius: 3px; overflow: hidden; }
.mob-bar-fill { height: 100%; border-radius: 3px; transition: width 0.3s; }
.mob-hp { background: linear-gradient(90deg, #cc3333, #ff6666); }
.mob-mp { background: linear-gradient(90deg, #3366cc, #66aaff); }
.mob-zone { color: #d4a438; font-size: 10px; margin-left: auto; }
.mob-level { color: #88ff88; font-size: 10px; }

/* ── Stats Popup ── */
#stats-popup {
  display: none;
  position: fixed;
  top: 0; left: 0; right: 0; bottom: 0;
  background: rgba(0,0,0,0.6);
  z-index: 150;
  align-items: center;
  justify-content: center;
  pointer-events: auto;
}
#stats-popup.visible { display: flex; }

#stats-popup-box {
  background: #12121e;
  border: 2px solid #d4a438;
  border-radius: 6px;
  padding: 1.2rem;
  min-width: 280px;
  max-width: 90vw;
  max-height: 80vh;
  overflow-y: auto;
  position: relative;
  font-size: 0.82rem;
}

.stats-popup-close {
  position: absolute;
  top: 0.5rem; right: 0.7rem;
  background: none; border: none;
  color: #6a5a4a; font-size: 1.1rem;
  cursor: pointer; font-family: inherit;
  padding: 4px 8px;
}

.stats-popup-title {
  color: #d4a438;
  letter-spacing: 0.2em;
  font-size: 0.7rem;
  margin-bottom: 0.8rem;
  text-align: center;
}

.sp-row {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin: 0.25rem 0;
  gap: 0.5rem;
}
.sp-bar-row { gap: 0.5rem; }
.sp-lbl { color: #6a5a4a; min-width: 50px; }
.sp-bar-wrap {
  flex: 1;
  height: 7px;
  background: #1a1a2e;
  border-radius: 4px;
  overflow: hidden;
}
.sp-bar-fill { height: 100%; border-radius: 4px; transition: width 0.3s; }
.sp-hp { background: linear-gradient(90deg, #cc3333, #ff6666); }
.sp-mp { background: linear-gradient(90deg, #3366cc, #66aaff); }
.sp-xp { background: linear-gradient(90deg, #44aa44, #88ff88); }
.sp-divider { height: 1px; background: #2a2a4a; margin: 0.5rem 0; }
.sp-section-label { color: #6a5a4a; font-size: 0.6rem; letter-spacing: 0.2em; text-transform: uppercase; margin-bottom: 0.2rem; }
.sp-text { color: #c8b89a; line-height: 1.6; }

/* ── Mobile responsive layout ── */
/* Breakpoint at 900px to catch foldable devices like Z Fold 5 (unfolded ~820px CSS) */
@media (max-width: 900px) {
  body { overflow: hidden; }

  #title-screen h1 { font-size: 1.8rem; letter-spacing: 0.2em; }
  .class-grid { grid-template-columns: repeat(3, 1fr); gap: 0.5rem; }
  .class-card { padding: 0.7rem 0.5rem; }
  .class-card .icon { font-size: 1.5rem; }
  .class-card .name { font-size: 0.75rem; }
  .class-card .desc { font-size: 0.6rem; }

  #hud { display: none !important; }
  #log-bar { height: 55px; right: 0; } /* full width on mobile */
  #narrative-log { font-size: 0.7rem; }
  #key-hints { display: none; }
  #minimap {
    right: 8px !important;
    top: 30px !important;
    width: 60px !important;
    height: 60px !important;
  }

  #dpad { display: block; }
  #mobile-actions { display: flex; }
  #mobile-hud { display: flex; }

  /* Mobile action buttons — compact 2-column grid to save vertical space */
  #mobile-actions {
    flex-direction: row;
    flex-wrap: wrap;
    gap: 4px;
    align-items: flex-end;
    justify-content: flex-end;
    max-width: 150px;
    bottom: 58px;
  }
  #dpad { bottom: 58px; }
  .mob-btn { font-size: 11px; padding: 6px 8px; min-width: 60px; text-align: center; }

  /* Combat buttons bigger on mobile */
  #combat-actions { gap: 0.4rem; }
  .combat-btn { padding: 0.9rem 0.8rem; font-size: 0.9rem; min-width: 72px; }

  /* ── Equipment screen — single column on mobile ── */
  #equip-screen {
    padding: 10px !important;
  }
  #equip-grid {
    grid-template-columns: 1fr !important;
    gap: 8px !important;
  }

  /* ── Dialog box: constrained height, scrollable ── */
  #dialog-overlay { padding: 0.5rem; align-items: center; }
  #dialog-box {
    margin: 0;
    max-height: 75vh;
    max-height: 75dvh;
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
    padding: 0.8rem 1rem;
  }
  #dialog-history { max-height: 80px; }
  .dialog-text { font-size: 0.8rem; line-height: 1.5; max-height: 4.5rem; overflow-y: auto; }
  .dialog-choices { max-height: 35vh; overflow-y: auto; }
  .dialog-choice { padding: 0.5rem 0.7rem; font-size: 0.8rem; }
  #dialog-close { font-size: 1.3rem; padding: 6px 10px; }

  /* Chat input compact on mobile */
  #dialog-chat-wrap { margin-top: 0.6rem; padding-top: 0.4rem; }
  .dialog-chat-label { font-size: 0.65rem; color: #d4a43888; }
  #dialog-chat-input { font-size: 16px; padding: 0.5rem 0.6rem; }
  #dialog-chat-send { font-size: 0.8rem; padding: 0.5rem 0.8rem; }

  /* Combat scene — scale down for mobile */
  #combat-scene { gap: 1.5rem; }
  .combatant { min-width: 70px; }
  #player-sprite-canvas, [id^="enemy-sprite-canvas"] { width: 50px !important; height: 50px !important; }

  /* Stats popup full-screen-ish on mobile */
  #stats-popup-box { min-width: 90vw; font-size: 0.88rem; }
}

/* Touch device override: if coarse pointer (touchscreen), force mobile layout regardless of width */
@media (pointer: coarse) {
  #hud { display: none !important; }
  #dpad { display: block; }
  #mobile-actions { display: flex; }
  #mobile-hud { display: flex; }
  #key-hints { display: none; }
  #log-bar { height: 55px; right: 0; }

  #equip-grid {
    grid-template-columns: 1fr !important;
  }
  #dialog-box {
    max-height: 75vh;
    max-height: 75dvh;
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
  }
}
