/* ============================================================
 * Dungeon Crawler - v1.0.20 - styles
 * ============================================================ */

:root {
  --bg: #14100b;
  --panel: #1e1812;
  --panel-2: #262019;
  --edge: #3a3025;
  --ink: #e8dcc2;
  --ink-dim: #a4936f;
  --gold: #d9a53a;
  --hp: #c0392b;
  --hp-low: #ff5a3c;
  --hp-bg: #3a1410;
  --xp: #3d8bd4;
  --good: #7dbf5a;
  --bad: #e05c4a;
  --tile: 34px;
  --wall: #2c2116;
  --floor: #574230;
  --shadow: rgba(0, 0, 0, 0.55);
}

* { box-sizing: border-box; margin: 0; padding: 0; }

html, body {
  height: 100%;
  background: var(--bg);
  color: var(--ink);
  font-family: "Trebuchet MS", "Segoe UI", Verdana, sans-serif;
  overflow: hidden;
}

body {
  background:
    radial-gradient(1200px 600px at 50% -10%, #2a2015 0%, transparent 60%),
    radial-gradient(900px 500px at 80% 110%, #201a12 0%, transparent 55%),
    var(--bg);
}

#app {
  display: grid;
  grid-template-rows: auto 1fr auto;
  height: 100dvh;
  max-width: 1400px;
  margin: 0 auto;
  padding: 8px 10px;
  gap: 8px;
}

/* ---------------- Top bar ---------------- */
#topbar {
  display: flex;
  align-items: center;
  gap: 14px;
  padding: 8px 12px;
  background: linear-gradient(180deg, var(--panel-2), var(--panel));
  border: 1px solid var(--edge);
  border-radius: 10px;
  box-shadow: 0 3px 10px var(--shadow);
}

.title-block { display: flex; align-items: center; gap: 8px; white-space: nowrap; }
.brand-logo {
  width: 34px;
  height: 34px;
  flex: 0 0 34px;
  border-radius: 9px;
  box-shadow: 0 1px 5px rgba(0, 0, 0, 0.65);
}
.title { font-family: Georgia, "Times New Roman", serif; font-size: 1.15rem; font-weight: bold; color: var(--gold); letter-spacing: 0.5px; }
.ver { font-size: 0.7rem; color: var(--ink-dim); border: 1px solid var(--edge); border-radius: 8px; padding: 1px 6px; }

.area-block { flex: 1; text-align: center; min-width: 0; }
#area-title { font-family: Georgia, serif; font-size: 1.05rem; color: var(--ink); text-shadow: 0 1px 3px #000; }
#area-subtitle { font-size: 0.72rem; color: var(--ink-dim); }

.top-actions { display: flex; gap: 6px; }

/* ---------------- Main area ---------------- */
#main {
  display: grid;
  grid-template-columns: minmax(0, 1fr) minmax(260px, 340px);
  gap: 8px;
  min-height: 0;
}

#map-wrap {
  display: flex;
  align-items: center;
  justify-content: center;
  min-width: 0;
  min-height: 0;
  padding: 8px;
  background:
    repeating-linear-gradient(45deg, #171208 0 12px, #1a140d 12px 24px);
  border: 1px solid var(--edge);
  border-radius: 10px;
  box-shadow: inset 0 0 60px rgba(0, 0, 0, 0.6);
  overflow: auto;
}

#map {
  display: grid;
  /* --tile / columns / rows set by JS */
  border: 2px solid #4a3a26;
  border-radius: 4px;
  box-shadow: 0 0 0 3px #0c0906, 0 10px 30px rgba(0, 0, 0, 0.6);
  background: var(--floor);
  image-rendering: pixelated;
}

.tile {
  position: relative;
  cursor: pointer;
  width: var(--tile);
  height: var(--tile);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: calc(var(--tile) * 0.62);
  user-select: none;
}

.tile.wall {
  background:
    linear-gradient(135deg, #3a2c1d 0%, #2c2116 55%, #241a10 100%);
  box-shadow: inset 0 0 6px rgba(0, 0, 0, 0.8), inset 2px 2px 2px rgba(255, 235, 200, 0.06);
}
.tile.wall.t-dark { background: #120d08; box-shadow: none; }

.tile.floor {
  background:
    radial-gradient(circle at 30% 25%, rgba(255, 240, 210, 0.10), transparent 55%),
    repeating-linear-gradient(0deg, #574230 0 2px, #54402f 2px 4px),
    #574230;
  box-shadow: inset 0 0 0 1px rgba(0, 0, 0, 0.25);
}
.tile.floor.t-dim { filter: brightness(0.62) saturate(0.8); }
.tile.floor.t-dark { filter: brightness(0.22); }

/* The Forgotten Crypt has colder, older stone than Level 1. */
#map[data-area="level2"] {
  background: #354047;
  border-color: #566872;
}
#map[data-area="level2"] .tile.wall {
  background: linear-gradient(135deg, #354047 0%, #293238 55%, #20282d 100%);
  box-shadow: inset 0 0 7px rgba(0, 0, 0, 0.85), inset 2px 2px 2px rgba(205, 231, 235, 0.07);
}
#map[data-area="level2"] .tile.wall.t-dark { background: #10171a; box-shadow: none; }
#map[data-area="level2"] .tile.floor {
  background:
    radial-gradient(circle at 30% 25%, rgba(205, 231, 235, 0.10), transparent 55%),
    repeating-linear-gradient(0deg, #46545c 0 2px, #424f56 2px 4px),
    #46545c;
}

/* The Ashen Halls glow faintly with heat beneath soot-dark stone. */
#map[data-area="level3"] {
  background: #443236;
  border-color: #76505a;
  box-shadow: 0 0 0 3px #0c0906, 0 10px 30px rgba(72, 18, 18, 0.65);
}
#map[data-area="level3"] .tile.wall {
  background: linear-gradient(135deg, #49363a 0%, #35272b 55%, #261b1e 100%);
  box-shadow: inset 0 0 7px rgba(0, 0, 0, 0.88), inset 2px 2px 2px rgba(255, 188, 148, 0.07);
}
#map[data-area="level3"] .tile.wall.t-dark { background: #160e11; box-shadow: none; }
#map[data-area="level3"] .tile.floor {
  background:
    radial-gradient(circle at 35% 30%, rgba(231, 126, 80, 0.12), transparent 55%),
    repeating-linear-gradient(0deg, #584044 0 2px, #533b40 2px 4px),
    #584044;
}

/* The Obsidian Forge is black volcanic rock lit by molten seams. */
#map[data-area="level4"] {
  background: #382a22;
  border-color: #a35b2a;
  box-shadow: 0 0 0 3px #0c0906, 0 10px 34px rgba(151, 55, 13, 0.55);
}
#map[data-area="level4"] .tile.wall {
  background: linear-gradient(135deg, #342b29 0%, #211b1a 55%, #171212 100%);
  box-shadow: inset 0 0 7px rgba(0, 0, 0, 0.9), inset 2px 2px 2px rgba(255, 116, 43, 0.10);
}
#map[data-area="level4"] .tile.wall.t-dark { background: #0d0908; box-shadow: none; }
#map[data-area="level4"] .tile.floor {
  background:
    radial-gradient(circle at 30% 25%, rgba(255, 112, 42, 0.18), transparent 52%),
    repeating-linear-gradient(0deg, #594033 0 2px, #50382e 2px 4px),
    #594033;
}

/* The Dragon Vault mixes royal purple stone with a deep crimson glow. */
#map[data-area="level5"] {
  background: #34253f;
  border-color: #a7793d;
  box-shadow: 0 0 0 3px #09060d, 0 10px 36px rgba(69, 26, 95, 0.7);
}
#map[data-area="level5"] .tile.wall {
  background: linear-gradient(135deg, #44344f 0%, #2d2237 55%, #1c1524 100%);
  box-shadow: inset 0 0 8px rgba(0, 0, 0, 0.9), inset 2px 2px 2px rgba(226, 178, 94, 0.10);
}
#map[data-area="level5"] .tile.wall.t-dark { background: #0d0912; box-shadow: none; }
#map[data-area="level5"] .tile.floor {
  background:
    radial-gradient(circle at 35% 30%, rgba(175, 66, 74, 0.20), transparent 52%),
    repeating-linear-gradient(0deg, #503c5c 0 2px, #493653 2px 4px),
    #503c5c;
}

/* The Abyssal Rift floats over a cold, lightless blue void. */
#map[data-area="level6"] {
  background: #111d2b;
  border-color: #427c91;
  box-shadow: 0 0 0 3px #03070b, 0 10px 38px rgba(18, 100, 127, 0.62);
}
#map[data-area="level6"] .tile.wall {
  background: linear-gradient(135deg, #263c4b 0%, #172936 56%, #0b151d 100%);
  box-shadow: inset 0 0 8px rgba(0, 0, 0, 0.95), inset 2px 2px 2px rgba(92, 220, 235, 0.10);
}
#map[data-area="level6"] .tile.wall.t-dark { background: #03080d; box-shadow: none; }
#map[data-area="level6"] .tile.floor {
  background:
    radial-gradient(circle at 32% 28%, rgba(69, 206, 222, 0.18), transparent 50%),
    repeating-linear-gradient(135deg, #314c59 0 2px, #29434f 2px 5px),
    #314c59;
}

/* The Celestial Ruin is pale starstone suspended in violet night. */
#map[data-area="level7"] {
  background: #27203e;
  border-color: #d2bd72;
  box-shadow: 0 0 0 3px #080610, 0 10px 40px rgba(111, 77, 175, 0.72);
}
#map[data-area="level7"] .tile.wall {
  background: linear-gradient(135deg, #6a6081 0%, #443b5d 55%, #252039 100%);
  box-shadow: inset 0 0 8px rgba(0, 0, 0, 0.9), inset 2px 2px 2px rgba(255, 232, 150, 0.16);
}
#map[data-area="level7"] .tile.wall.t-dark { background: #0b0815; box-shadow: none; }
#map[data-area="level7"] .tile.floor {
  background:
    radial-gradient(circle at 30% 25%, rgba(244, 223, 148, 0.21), transparent 48%),
    repeating-linear-gradient(0deg, #65597d 0 2px, #5b4f72 2px 4px),
    #65597d;
}

.sprite { line-height: 1; filter: drop-shadow(0 2px 2px rgba(0, 0, 0, 0.7)); }
.special-pulse { animation: pulse 2.2s ease-in-out infinite; }

.player-tile { background: radial-gradient(circle, rgba(217, 165, 58, 0.35), transparent 70%); }
.player-tile::after {
  content: "";
  position: absolute;
  inset: 8%;
  border: 1px dashed rgba(217, 165, 58, 0.85);
  border-radius: 50%;
  animation: pulse 1.8s ease-in-out infinite;
}
.player-sprite { line-height: 1; filter: drop-shadow(0 2px 3px #000); }

.monster { animation: bob 1.6s ease-in-out infinite; }
.tile.sealed .sprite { filter: grayscale(0.8) brightness(0.7); }

@keyframes pulse {
  0%, 100% { opacity: 1; transform: scale(1); }
  50% { opacity: 0.65; transform: scale(1.06); }
}
@keyframes bob {
  0%, 100% { transform: translateY(0); }
  50% { transform: translateY(-8%); }
}
/* ---------------- Side panel ---------------- */
#side {
  display: grid;
  grid-template-rows: 1fr auto;
  gap: 8px;
  min-height: 0;
}

#log-panel {
  background: var(--panel);
  border: 1px solid var(--edge);
  border-radius: 10px;
  padding: 10px 12px;
  display: flex;
  flex-direction: column;
  min-height: 0;
  overflow: hidden;
}
#log {
  flex: 1;
  overflow-y: auto;
  font-size: 0.82rem;
  line-height: 1.45;
  display: flex;
  flex-direction: column;
  gap: 4px;
  scrollbar-width: thin;
  scrollbar-color: var(--edge) transparent;
}
.log-line { color: var(--ink); animation: fadein 0.25s ease; }
.log-line.muted { color: var(--ink-dim); font-style: italic; }
.log-line.hint { color: var(--gold); font-style: italic; }
.log-line.loot { color: var(--good); }
.log-line.heal { color: #7fd48a; }
.log-line.bad { color: var(--bad); }
.log-line.travel { color: #9db8e8; }
.log-line.levelup { color: var(--gold); font-weight: bold; }
@keyframes fadein { from { opacity: 0; transform: translateY(-3px); } to { opacity: 1; transform: none; } }

#controls {
  background: var(--panel);
  border: 1px solid var(--edge);
  border-radius: 10px;
  padding: 10px;
  display: grid;
  grid-template-columns: auto 1fr;
  gap: 8px;
  align-items: center;
}
.dpad {
  display: grid;
  grid-template-columns: repeat(3, 34px);
  grid-template-rows: repeat(2, 32px);
  gap: 2px;
}
.dpad-btn {
  display: flex;
  align-items: center;
  justify-content: center;
  background: var(--panel-2);
  border: 1px solid var(--edge);
  color: var(--ink);
  border-radius: 6px;
  font-size: 0.78rem;
  cursor: pointer;
  transition: background 0.12s, transform 0.05s;
}
/* The arrow is an inline SVG drawn in the button's text colour, so the four
   directions share one shape and weight whatever fonts the device has. */
.dpad-icon {
  display: block;
  width: 1.1em;
  height: 1.1em;
  fill: currentColor;
  pointer-events: none;
}
.dpad-btn:nth-child(1) { grid-column: 2; grid-row: 1; }
.dpad-btn:nth-child(2) { grid-column: 1; grid-row: 2; }
.dpad-btn:nth-child(3) { grid-column: 2; grid-row: 2; }
.dpad-btn:nth-child(4) { grid-column: 3; grid-row: 2; }
.dpad-btn:nth-child(2) .dpad-icon { transform: rotate(-90deg); }
.dpad-btn:nth-child(3) .dpad-icon { transform: rotate(180deg); }
.dpad-btn:nth-child(4) .dpad-icon { transform: rotate(90deg); }
.dpad-btn:hover { background: #35291c; }
.dpad-btn:active { transform: scale(0.94); }

#controls .btn { width: 100%; }
#btn-town {
  grid-column: auto;
  padding: 6px 8px;
  font-size: 0.76rem;
  white-space: nowrap;
}
#btn-town[hidden] { display: none; }

/* ---------------- Buttons ---------------- */
.btn {
  background: linear-gradient(180deg, #3a2f22, #2b2118);
  border: 1px solid var(--edge);
  color: var(--ink);
  border-radius: 8px;
  padding: 7px 12px;
  font-size: 0.85rem;
  cursor: pointer;
  font-family: inherit;
  transition: filter 0.12s, transform 0.06s;
}
.btn:hover { filter: brightness(1.18); }
.btn:active { transform: translateY(1px); }
.btn.small { padding: 5px 9px; font-size: 0.75rem; }
.btn.big { padding: 12px; font-size: 0.95rem; font-weight: bold; }
.btn.primary { background: linear-gradient(180deg, #7a5a1e, #5c4214); border-color: #a07a2a; color: #ffe9b0; }
.btn.danger { background: linear-gradient(180deg, #7a2a1e, #54150e); border-color: #a03a28; color: #ffc9bc; }
.btn.buy { background: linear-gradient(180deg, #3f5a28, #2c3f1c); border-color: #6a8f42; color: #d9f0bd; }
.btn.sell { background: linear-gradient(180deg, #405d72, #293f50); border-color: #6388a3; color: #d8eefc; }
.btn.ghost { background: transparent; border-color: var(--edge); }
.btn.disabled, .btn.disabled:hover { filter: grayscale(0.9) brightness(0.7); cursor: not-allowed; }
.count {
  display: inline-block;
  min-width: 18px;
  text-align: center;
  background: var(--gold);
  color: #241a0c;
  border-radius: 9px;
  font-weight: bold;
  font-size: 0.72rem;
  padding: 0 5px;
}

/* ---------------- Bottom HUD: the health bar ---------------- */
#hud {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto minmax(0, 1fr);
  align-items: center;
  gap: 16px;
  padding: 10px 14px;
  background: linear-gradient(180deg, var(--panel-2), var(--panel));
  border: 1px solid var(--edge);
  border-radius: 10px;
  box-shadow: 0 -3px 12px var(--shadow);
}

.hud-hp, .hud-xp { display: flex; align-items: center; gap: 8px; min-width: 0; }
.hud-xp { justify-content: flex-end; }
.hud-icon { font-size: 1.2rem; color: #e05545; filter: drop-shadow(0 1px 2px #000); }

.bar {
  flex: 1;
  height: 22px;
  background: var(--hp-bg);
  border: 1px solid #000;
  border-radius: 11px;
  overflow: hidden;
  box-shadow: inset 0 2px 5px rgba(0, 0, 0, 0.8), 0 0 0 1px rgba(255, 255, 255, 0.04);
  min-width: 90px;
}
.bar.xp { height: 12px; background: #14202e; border-radius: 6px; }
.fill {
  height: 100%;
  width: 100%;
  border-radius: inherit;
  transition: width 0.35s ease;
}
.hp-fill {
  background: linear-gradient(180deg, #ff6a52 0%, var(--hp) 45%, #7e1d12 100%);
  box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.25);
}
#hp-bar.low .hp-fill { background: linear-gradient(180deg, #ffb84a 0%, #ff5a3c 50%, #a01a08 100%); animation: hpflash 0.9s infinite; }
@keyframes hpflash { 0%, 100% { filter: brightness(1); } 50% { filter: brightness(1.45); } }
.xp-fill { background: linear-gradient(180deg, #6aa9e8, var(--xp)); box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.3); }

.bar-text {
  font-size: 0.85rem;
  font-weight: bold;
  color: var(--ink);
  text-shadow: 0 1px 2px #000;
  white-space: nowrap;
  font-variant-numeric: tabular-nums;
}
.xp-text { font-size: 0.7rem; color: var(--ink-dim); }

.hud-stats { display: flex; gap: 12px; }
.hud-stat {
  font-size: 0.85rem;
  color: var(--ink-dim);
  background: rgba(0, 0, 0, 0.25);
  border: 1px solid var(--edge);
  border-radius: 8px;
  padding: 3px 8px;
  white-space: nowrap;
}
.hud-stat b { color: var(--ink); font-variant-numeric: tabular-nums; }

/* ---------------- Modal ---------------- */
#overlay {
  position: fixed;
  inset: 0;
  background: rgba(8, 5, 2, 0.78);
  backdrop-filter: blur(3px);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 50;
  padding: 14px;
}
#overlay.hidden { display: none; }

#modal { width: min(560px, 96vw); max-height: 92dvh; overflow-y: auto; }

.modal-card {
  background: linear-gradient(180deg, #2b2216, #1d1610);
  border: 1px solid #5a4526;
  border-radius: 14px;
  box-shadow: 0 18px 50px rgba(0, 0, 0, 0.85), inset 0 1px 0 rgba(255, 235, 190, 0.08);
  padding: 18px;
  animation: pop 0.2s ease;
}
@keyframes pop { from { opacity: 0; transform: scale(0.94) translateY(6px); } to { opacity: 1; transform: none; } }

.modal-title {
  font-family: Georgia, serif;
  font-size: 1.2rem;
  color: var(--gold);
  text-align: center;
  margin-bottom: 10px;
  letter-spacing: 0.5px;
}
.modal-body { font-size: 0.92rem; line-height: 1.5; }
.modal-footer {
  display: flex;
  gap: 8px;
  justify-content: center;
  margin-top: 14px;
  flex-wrap: wrap;
}
.modal-footer .btn { min-width: 120px; }

.big-emoji { font-size: 2.6rem; text-align: center; margin: 4px 0; }
.area-name { text-align: center; color: var(--gold); font-size: 1.25rem; margin-bottom: 4px; }
.hint { color: var(--ink-dim); font-size: 0.82rem; }
.npc-line { color: #d9c9a5; font-style: italic; }
.controls-list { margin: 8px 0 8px 18px; }
.controls-list li { margin: 2px 0; }
.controls-list b { color: var(--gold); }
.intro-body p { margin: 6px 0; }

.shop-list { margin-top: 10px; display: flex; flex-direction: column; gap: 6px; }
.shop-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  background: rgba(0, 0, 0, 0.22);
  border: 1px solid var(--edge);
  border-radius: 8px;
  padding: 8px 10px;
}
.shop-info { min-width: 0; }
.shop-name { font-weight: bold; color: var(--ink); }
.shop-desc { font-size: 0.78rem; color: var(--ink-dim); margin-top: 2px; }
.gear-line { margin-top: 12px; font-size: 0.85rem; color: var(--gold); text-align: center; }

/* ---------------- Inventory equipment slots ---------------- */
.inventory-heading {
  margin-top: 14px;
  margin-bottom: 7px;
  color: var(--gold);
  font-family: Georgia, serif;
  font-size: 0.92rem;
  letter-spacing: 0.3px;
}
.inventory-heading:first-child { margin-top: 0; }
.store-sell-heading {
  border-top: 1px solid var(--edge);
  padding-top: 13px;
}
/* Worn gear is refused at the counter until it is taken off. */
.sell-hint {
  margin: 4px 0 8px;
  color: var(--gold);
  font-style: italic;
}
.equipment-slots {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 8px;
}
.equipment-slot {
  min-height: 126px;
  padding: 10px;
  border: 1px solid #6b512a;
  border-radius: 10px;
  background: linear-gradient(145deg, rgba(83, 61, 31, 0.38), rgba(0, 0, 0, 0.28));
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 5px;
  text-align: center;
}
.slot-label {
  color: var(--gold);
  font-size: 0.75rem;
  font-weight: bold;
  text-transform: uppercase;
  letter-spacing: 0.8px;
}
.slot-item { color: var(--ink); font-weight: bold; }
.slot-bonus { min-height: 1.2em; color: var(--good); font-size: 0.76rem; }
.slot-empty { color: var(--ink-dim); font-style: italic; }
.equipment-slot .btn { margin-top: 3px; }
.equipped-label {
  color: #b9db8e;
  border: 1px solid #58733b;
  border-radius: 7px;
  padding: 4px 8px;
  font-size: 0.72rem;
  white-space: nowrap;
}
.inventory-empty { margin: 3px 0 7px; }
.gear-list .shop-row { border-color: #51462f; }

/* ---------------- Battle modal ---------------- */
.battle-body { text-align: center; }
.foe {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 4px;
  padding: 8px 0 4px;
}
.foe-emoji { font-size: 3rem; filter: drop-shadow(0 4px 6px #000); animation: bob 1.4s ease-in-out infinite; }
.battle-modal-card .modal-title {
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  font-size: 1rem;
  margin-bottom: 6px;
}
.foe-hpbar {
  width: min(320px, 80%);
  height: 14px;
  background: #2a0f0c;
  border: 1px solid #000;
  border-radius: 7px;
  overflow: hidden;
  box-shadow: inset 0 2px 4px rgba(0, 0, 0, 0.8);
}
.foe-hpfill {
  height: 100%;
  width: 100%;
  background: linear-gradient(180deg, #ff7a60, #b02a17);
  border-radius: inherit;
  transition: width 0.25s ease;
}
.foe-flavor { font-size: 0.78rem; color: var(--ink-dim); font-style: italic; max-width: 400px; }

.battle-log {
  margin: 10px auto 0;
  min-height: 92px;
  max-height: 160px;
  overflow-y: auto;
  background: rgba(0, 0, 0, 0.3);
  border: 1px solid var(--edge);
  border-radius: 8px;
  padding: 8px 10px;
  text-align: left;
  font-size: 0.85rem;
  display: flex;
  flex-direction: column;
  gap: 3px;
  scrollbar-width: thin;
}
.battle-line { animation: fadein 0.18s ease; }

.battle-actions {
  display: flex;
  gap: 8px;
  justify-content: center;
  margin-top: 10px;
  flex-wrap: wrap;
}
/* The three action buttons always share their row equally, whatever the screen
   width. flex-basis has to stay at 0 with no min-width, or a wide hint line
   would be sized first and squeeze the buttons down to a sliver. */
.battle-btn {
  flex: 1 1 0;
  min-width: 0;
}
.battle-hints {
  width: 100%;
  text-align: center;
  font-size: 0.72rem;
  color: var(--ink-dim);
  margin-top: 2px;
}

/* ---------------- Responsive ---------------- */
@media (max-width: 860px) {
  #main { grid-template-columns: 1fr; grid-template-rows: 1fr auto; }
  #side { grid-template-rows: auto; }
  #log-panel { display: none; }
  /* The HUD keeps its single HP | stats | Lv + XP row: the HP bar gives up
     width so the level and XP stay on screen (e.g. a phone in landscape). */
  #hud { gap: 12px; }
  .bar { min-width: 60px; }
  #topbar .title { font-size: 1rem; }
  #area-subtitle { display: none; }
}

/* Portrait phones are too narrow for both bars beside the stat pills, so HP
   and Lv + XP share the top row and the stats drop to a row beneath them. */
@media (max-width: 640px) {
  #hud {
    grid-template-columns: minmax(0, 1fr) minmax(0, 1fr);
    grid-template-areas:
      "hp xp"
      "stats stats";
    gap: 6px 10px;
  }
  .hud-hp { grid-area: hp; }
  .hud-xp { grid-area: xp; }
  .hud-stats { grid-area: stats; justify-content: center; }
}

@media (max-width: 680px) {
  .equipment-slots { grid-template-columns: repeat(2, minmax(0, 1fr)); }
}

@media (max-width: 520px) {
  .equipment-slots { grid-template-columns: 1fr; }
  .equipment-slot { min-height: 104px; }
}

/* ---------------- Touch and Android polish ---------------- */
html, body {
  overscroll-behavior: none;
  -webkit-tap-highlight-color: transparent;
}

button {
  touch-action: manipulation;
  -webkit-user-select: none;
  user-select: none;
}

@media (max-width: 520px) {
  #app {
    padding: max(5px, env(safe-area-inset-top)) max(6px, env(safe-area-inset-right)) max(5px, env(safe-area-inset-bottom)) max(6px, env(safe-area-inset-left));
    gap: 6px;
  }

  #topbar {
    gap: 6px;
    padding: 6px 8px;
  }

  .title-block {
    min-width: 0;
    gap: 5px;
  }

  .brand-logo {
    width: 30px;
    height: 30px;
    flex-basis: 30px;
  }

  #topbar .title {
    min-width: 0;
    overflow: hidden;
    text-overflow: ellipsis;
    font-size: 0.92rem;
  }

  .ver,
  .area-block {
    display: none;
  }

  .top-actions {
    flex: 0 0 auto;
    margin-left: auto;
  }

  #btn-new,
  #btn-mute {
    width: 36px;
    height: 34px;
    padding: 4px;
  }

  #btn-new {
    font-size: 0;
  }

  #btn-new::before {
    content: "✚";
    font-size: 1rem;
  }

  #btn-mute {
    font-size: 1rem;
  }

  #map-wrap {
    padding: 4px;
  }

  #controls {
    padding: 6px;
    gap: 6px;
  }

  .dpad {
    grid-template-columns: repeat(3, 36px);
    grid-template-rows: repeat(2, 34px);
    gap: 2px;
  }

  .dpad-btn {
    min-height: 34px;
    font-size: 0.85rem;
  }

  #controls .btn {
    min-height: 39px;
  }

  #hud {
    gap: 5px 8px;
    padding: 7px 8px;
  }

  .hud-hp,
  .hud-xp {
    gap: 4px;
  }

  .hud-icon {
    font-size: 1rem;
  }

  .bar {
    min-width: 0;
    height: 18px;
  }

  .bar-text {
    font-size: 0.74rem;
  }

  .xp-text {
    font-size: 0.7rem;
  }

  .hud-stats {
    gap: 6px;
  }

  .hud-stat {
    padding: 2px 5px;
    font-size: 0.72rem;
  }

  #modal {
    width: min(560px, calc(100vw - 18px));
    max-height: calc(100dvh - 18px);
  }

  .modal-card {
    padding: 14px;
  }
}

/* Portrait phones keep the chronicle visible above the action controls. */
@media (max-width: 520px) and (orientation: portrait) {
  #side {
    grid-template-rows: 94px auto;
  }

  #log-panel {
    display: flex;
    min-height: 0;
    padding: 7px 9px;
  }

  #log {
    font-size: 0.72rem;
    line-height: 1.3;
    gap: 2px;
  }
}

@media (max-width: 360px) {
  #topbar .title {
    font-size: 0.84rem;
  }

  .dpad {
    grid-template-columns: repeat(3, 32px);
    grid-template-rows: repeat(2, 32px);
  }

  .dpad-btn {
    min-height: 32px;
  }

  .hud-stat {
    padding-inline: 4px;
  }
}

/* ---------------- Compact intro on short screens ----------------
   The controls list makes this card taller than a landscape phone. Shrinking
   the type and splitting the list into two columns keeps it inside the
   viewport, so "Begin Adventure" is reachable without scrolling. */
@media (max-height: 640px) {
  .intro-modal-card {
    padding: 10px 12px;
  }

  .intro-modal-card .modal-title {
    font-size: 1rem;
    margin-bottom: 4px;
  }

  .intro-modal-card .modal-body {
    font-size: 0.82rem;
    line-height: 1.35;
  }

  .intro-modal-card .intro-body p {
    margin: 2px 0;
  }

  .intro-modal-card .controls-list {
    display: grid;
    grid-template-columns: repeat(2, minmax(0, 1fr));
    gap: 1px 16px;
    margin: 5px 0 3px 16px;
  }

  .intro-modal-card .controls-list li {
    margin: 0;
  }

  .intro-modal-card .modal-footer {
    margin-top: 7px;
  }

  .intro-modal-card .modal-footer .btn {
    min-width: 150px;
    padding: 8px 12px;
  }
}

/* Portrait phones are too narrow for two columns; the compact type still fits. */
@media (max-height: 640px) and (max-width: 560px) {
  .intro-modal-card .controls-list {
    grid-template-columns: 1fr;
  }
}

/* Landscape phones have room for the map and controls side by side. */
@media (max-height: 560px) and (orientation: landscape) and (max-width: 1024px) {
  #app {
    padding: 5px 8px;
    gap: 5px;
  }

  #main {
    grid-template-columns: minmax(0, 1fr) 220px;
    grid-template-rows: 1fr;
  }

  #side {
    grid-template-rows: 1fr auto;
  }

  #log-panel {
    display: flex;
  }

  #hud {
    padding-block: 5px;
  }

  /* Keep combat actions above the fold on short landscape screens. */
  .battle-modal-card {
    padding: 10px 12px;
  }

  .battle-modal-card .modal-title {
    font-size: 0.9rem;
    margin-bottom: 3px;
  }

  .battle-modal-card .foe {
    gap: 2px;
    padding: 0 0 2px;
  }

  .battle-modal-card .foe-emoji {
    font-size: 2rem;
  }

  .battle-modal-card .foe-hpbar {
    height: 10px;
  }

  .battle-modal-card .foe-flavor {
    max-width: 100%;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
    font-size: 0.68rem;
  }

  .battle-modal-card .battle-log {
    min-height: 42px;
    max-height: 64px;
    margin-top: 4px;
    padding: 4px 6px;
    font-size: 0.72rem;
  }

  /* Attack / Item / Flee keep an identical, full width on short screens. */
  .battle-modal-card .battle-actions {
    gap: 4px;
    margin-top: 4px;
    flex-wrap: nowrap;
  }

  .battle-modal-card .battle-btn {
    min-width: 0;
    flex: 1 1 0;
    padding: 7px 5px;
    font-size: 0.78rem;
  }

  .battle-modal-card .battle-hints {
    font-size: 0.62rem;
    margin-top: 0;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
  }
}
