:root {
  --bg: #0a0a0e;
  --bg-panel: #121218;
  --border: #2a2a35;
  --text: #c9c9d4;
  --text-dim: #6a6a78;
  --accent: #f0c14b;
  --accent-2: #4bc9f0;
  --danger: #e05252;
  --ok: #52c97a;
  --wall: #3a3a45;
  --floor: #55555f;
  --stairs: #f0c14b;
  font-family: 'Share Tech Mono', 'Courier New', monospace;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
  background: var(--bg);
  color: var(--text);
  font-family: 'Share Tech Mono', 'Courier New', monospace;
  height: 100%;
}

.hidden { display: none !important; }

button {
  font-family: inherit;
  background: var(--bg-panel);
  color: var(--text);
  border: 1px solid var(--border);
  padding: 8px 14px;
  cursor: pointer;
  border-radius: 3px;
  font-size: 0.95rem;
}
button:hover { border-color: var(--accent); color: var(--accent); }
button:active { transform: translateY(1px); }

textarea, input[type="text"] {
  font-family: inherit;
  background: #050507;
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 3px;
  padding: 8px;
  width: 100%;
  resize: none;
}

/* ---------- Title screen ---------- */
#title-screen {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px;
}
.title-box {
  max-width: 480px;
  text-align: center;
  border: 1px solid var(--border);
  background: var(--bg-panel);
  padding: 36px 28px;
  border-radius: 6px;
}
#title-banner {
  display: inline-block;
  margin: 0;
  text-align: left;
  font-family: 'Share Tech Mono', 'Courier New', monospace;
  font-size: 11px;
  line-height: 1.15;
  color: var(--accent);
  text-shadow: 0 0 10px rgba(240, 193, 75, 0.6);
}
.subtitle {
  color: var(--text-dim);
  margin-top: 10px;
  letter-spacing: 0.1em;
}
.lore-intro {
  color: var(--text-dim);
  font-size: 0.85rem;
  line-height: 1.5;
  margin: 14px 0 0;
  text-align: left;
  max-width: 46ch;
}
.title-actions {
  display: flex;
  gap: 10px;
  justify-content: center;
  margin-top: 20px;
  flex-wrap: wrap;
}
#load-form { margin-top: 18px; }
.help { color: var(--text-dim); font-size: 0.8rem; margin-top: 24px; }
.error { color: var(--danger); min-height: 1.2em; }
.muted { color: var(--text-dim); font-size: 0.85rem; }
#run-history-box { margin-top: 16px; text-align: left; }
.hist-heading { margin: 0 0 4px; }
.hist-row { color: var(--text-dim); font-size: 0.8rem; padding: 2px 0; }
.ad-slot {
  width: 100%; min-height: 90px; margin-top: 20px;
  border: 1px dashed var(--border); border-radius: 6px;
  background: var(--bg-panel);
  display: flex; align-items: center; justify-content: center;
}
.footer-links {
  margin-top: 14px; display: flex; gap: 16px; justify-content: center;
  flex-wrap: wrap;
}
.footer-links a { color: var(--text-dim); font-size: 0.75rem; text-decoration: none; }
.footer-links a:hover { color: var(--text); }

/* ---------- Game screen ---------- */
#game-screen {
  display: flex;
  flex-direction: column;
  min-height: 100vh;
  padding: 10px;
  gap: 10px;
  max-width: 1000px;
  margin: 0 auto;
}

#hud {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  gap: 12px;
  flex-wrap: wrap;
  border: 1px solid var(--border);
  background: var(--bg-panel);
  padding: 10px 14px;
  border-radius: 4px;
}
#stats { min-width: 220px; }
.stat-row {
  display: flex;
  justify-content: space-between;
  gap: 10px;
  font-size: 0.95rem;
  padding: 1px 0;
}
.hpbar {
  height: 8px;
  background: #200;
  border: 1px solid var(--border);
  border-radius: 2px;
  margin: 4px 0 6px;
  overflow: hidden;
}
#hpbar-fill {
  height: 100%;
  background: linear-gradient(90deg, var(--danger), var(--ok));
  width: 100%;
  transition: width 0.2s ease;
}
.equip-box { margin-top: 6px; font-size: 0.85rem; color: var(--text-dim); }
.status-poison { color: #b060e0; font-size: 0.85rem; font-weight: bold; }
#actions { display: flex; flex-direction: column; gap: 8px; }
#actions button { width: 100%; }

#minimap-box {
  padding: 0 16px;
  border-left: 1px solid var(--border);
  border-right: 1px solid var(--border);
}
#minimap {
  display: block;
  border: 1px solid var(--border);
  background: #060608;
  image-rendering: pixelated;
}
#legend {
  padding: 0 16px;
  border-left: 1px solid var(--border);
  border-right: 1px solid var(--border);
  font-size: 0.78rem;
  color: var(--text-dim);
}
.legend-title { color: var(--text); font-weight: bold; margin-bottom: 4px; }
.legend-row { display: flex; align-items: center; gap: 6px; padding: 1px 0; }
.legend-glyph { display: inline-block; min-width: 1.6em; text-align: center; font-weight: bold; }

#map-wrap {
  position: relative;
  border: 1px solid var(--border);
  background: #060608;
  border-radius: 4px;
  padding: 10px;
  overflow: auto;
}
#map {
  margin: 0;
  font-family: 'Share Tech Mono', 'Courier New', monospace;
  font-size: 15px;
  line-height: 1.1;
  white-space: pre;
}
#map .cell { display: inline-block; width: 1ch; }
#map .unseen { color: transparent; }

/* Overlay for floating combat numbers, aligned to the same character grid as
   #map — must mirror its font-size/line-height (including the mobile
   override below) so `ch`/line-height-based positions land on the right cell. */
#fx-layer {
  position: absolute;
  top: 10px;
  left: 10px;
  pointer-events: none;
  overflow: visible;
  font-family: 'Share Tech Mono', 'Courier New', monospace;
  font-size: 15px;
  line-height: 1.1;
}
#map .tile-0 { color: var(--wall); }
#map .tile-1 { color: var(--floor); }
#map .tile-2 { color: var(--stairs); font-weight: bold; }
#map .player { color: #fff; font-weight: bold; text-shadow: 0 0 6px rgba(255,255,255,0.7); animation: pop-step 0.12s ease-out; }
#map .dim { opacity: 0.42; }
#map .boss { text-shadow: 0 0 8px currentColor; font-weight: bold; }
#map .hit { animation: flash-hit 0.3s ease-out; }

.dmg-float {
  position: absolute;
  font-weight: bold;
  white-space: nowrap;
  animation: dmg-float-anim 0.7s ease-out forwards;
}
.dmg-float-out { color: #f0e050; text-shadow: 0 0 4px rgba(0,0,0,0.9), 0 0 4px rgba(0,0,0,0.9); }
.dmg-float-in { color: #ff5050; text-shadow: 0 0 4px rgba(0,0,0,0.9), 0 0 4px rgba(0,0,0,0.9); }
.dmg-float-heal { color: #50f080; text-shadow: 0 0 4px rgba(0,0,0,0.9), 0 0 4px rgba(0,0,0,0.9); }
.dmg-float-poison { color: #b060e0; text-shadow: 0 0 4px rgba(0,0,0,0.9), 0 0 4px rgba(0,0,0,0.9); }
@keyframes dmg-float-anim {
  0% { transform: translateY(0); opacity: 1; }
  100% { transform: translateY(-1.4em); opacity: 0; }
}

@keyframes pop-step {
  0% { transform: scale(0.65); }
  100% { transform: scale(1); }
}
@keyframes flash-hit {
  0% { filter: brightness(2.4) saturate(1.6); }
  100% { filter: none; }
}
@keyframes shake {
  10%, 90% { transform: translateX(-1px); }
  20%, 80% { transform: translateX(2px); }
  30%, 50%, 70% { transform: translateX(-5px); }
  40%, 60% { transform: translateX(5px); }
}
@keyframes pulse-dmg {
  0% { box-shadow: inset 0 0 40px rgba(224, 82, 82, 0.55); }
  100% { box-shadow: inset 0 0 0 rgba(224, 82, 82, 0); }
}
#boss-banner {
  position: fixed;
  top: 16px;
  left: 50%;
  transform: translateX(-50%);
  background: rgba(20, 0, 0, 0.92);
  border: 1px solid var(--danger);
  color: var(--danger);
  padding: 10px 22px;
  border-radius: 4px;
  font-weight: bold;
  letter-spacing: 0.03em;
  text-shadow: 0 0 8px rgba(224, 82, 82, 0.8);
  box-shadow: 0 0 20px rgba(224, 82, 82, 0.4);
  z-index: 20;
  pointer-events: none;
  animation: boss-banner-anim 3s ease-in-out forwards;
}
@keyframes boss-banner-anim {
  0% { opacity: 0; transform: translateX(-50%) translateY(-10px); }
  10% { opacity: 1; transform: translateX(-50%) translateY(0); }
  85% { opacity: 1; }
  100% { opacity: 0; }
}
@keyframes level-glow {
  0% { box-shadow: 0 0 0 rgba(240, 193, 75, 0); }
  30% { box-shadow: 0 0 22px rgba(240, 193, 75, 0.9); }
  100% { box-shadow: 0 0 0 rgba(240, 193, 75, 0); }
}
#game-screen.shake { animation: shake 0.35s ease-in-out; }
#hud.dmg-flash { animation: pulse-dmg 0.4s ease-out; }
#hud.level-flash { animation: level-glow 0.6s ease-out; }

#log {
  border: 1px solid var(--border);
  background: var(--bg-panel);
  border-radius: 4px;
  padding: 8px 12px;
  height: 110px;
  overflow-y: auto;
  font-size: 0.85rem;
  color: var(--text-dim);
}
#log div:last-child { color: var(--text); }

#touch-controls {
  display: none;
  justify-content: space-between;
  align-items: center;
  gap: 10px;
}
.dpad {
  display: grid;
  grid-template-columns: repeat(3, 46px);
  grid-template-rows: repeat(2, 46px);
  gap: 4px;
}
.dbtn { font-size: 1.1rem; padding: 0; }
.dup { grid-column: 2; grid-row: 1; }
.dleft { grid-column: 1; grid-row: 2; }
.ddown { grid-column: 2; grid-row: 2; }
.dright { grid-column: 3; grid-row: 2; }
.tbuttons { display: flex; flex-direction: column; gap: 6px; }

@media (max-width: 720px) {
  #touch-controls { display: flex; }
  #map, #fx-layer { font-size: 12px; }
  #hud { flex-direction: column; }
  #legend { border-left: none; border-right: none; border-top: 1px solid var(--border); border-bottom: 1px solid var(--border); padding: 10px 0; }
  #minimap-box { border-left: none; border-right: none; border-top: 1px solid var(--border); padding: 10px 0; }

  /* #touch-controls already covers move/pickup/flee/descend with a thumb-
     sized dpad; the equivalent text buttons in the desktop #actions column
     would just be redundant clutter on a phone (Inventory/Craft/Save stay,
     since touch-controls has no equivalent for those). */
  .mobile-hide { display: none; }

  /* On a narrow/tall viewport, put the map and its touch controls before
     the stats/minimap/legend/log reference panels, so you're not scrolling
     past a big stats block just to reach the game or the movement buttons.
     #game-screen is already a column flexbox, so this is a pure reorder --
     no DOM/JS changes, and it has no effect above the 720px breakpoint. */
  #map-wrap { order: 1; }
  #touch-controls { order: 2; }
  #hud { order: 3; }
  #log { order: 4; }
}

/* ---------- Modals ---------- */
.modal {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.7);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 16px;
  z-index: 10;
}
.modal-box {
  background: var(--bg-panel);
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 22px 24px;
  max-width: 480px;
  width: 100%;
  max-height: 85vh;
  overflow-y: auto;
}
.modal-box-wide { max-width: 640px; }
.modal-box h2 { margin-top: 0; color: var(--accent); }
.merchant-quote { color: var(--text-dim); font-style: italic; font-size: 0.85rem; margin: -6px 0 14px; }
.modal-box h3 { margin: 14px 0 4px; color: var(--accent-2); font-size: 1rem; }
.modal-box h3:first-of-type { margin-top: 4px; }

/* Equipment "paper doll" cards at the top of the inventory panel */
.equip-cards { display: flex; gap: 10px; margin: 4px 0 14px; flex-wrap: wrap; }
.equip-card {
  flex: 1 1 160px;
  border: 1px solid var(--border);
  border-radius: 4px;
  padding: 8px 10px;
  background: rgba(255, 255, 255, 0.02);
}
.equip-card.has-passive { border-color: var(--accent-2); box-shadow: 0 0 10px rgba(75, 201, 240, 0.15); }
.equip-card.empty { opacity: 0.6; }
.equip-card-label { font-size: 0.72rem; text-transform: uppercase; letter-spacing: 0.06em; color: var(--text-dim); }
.equip-card-name { font-weight: bold; margin-top: 2px; }
.equip-card-stat { font-size: 0.8rem; color: var(--text-dim); margin-top: 2px; }
.equip-card-empty { font-size: 0.85rem; color: var(--text-dim); margin-top: 2px; }
.equip-card-passive { font-size: 0.8rem; color: var(--accent-2); margin-top: 2px; }

/* Category tabs (inventory) */
.tab-bar { display: flex; gap: 4px; flex-wrap: wrap; margin-bottom: 10px; border-bottom: 1px solid var(--border); padding-bottom: 8px; }
.tab-btn { padding: 5px 10px; font-size: 0.78rem; }
.tab-btn.active { border-color: var(--accent); color: var(--accent); background: rgba(240, 193, 75, 0.1); }

.inv-row {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 6px 0;
  border-bottom: 1px solid var(--border);
  flex-wrap: wrap;
}
.inv-name { font-weight: bold; }
.inv-stat { font-size: 0.78rem; }
.inv-row button { padding: 4px 8px; font-size: 0.8rem; }
.inv-row .btn-equip { border-color: var(--accent-2); color: var(--accent-2); }
.inv-row .btn-equip:hover { background: rgba(75, 201, 240, 0.15); }
.inv-row .btn-use { border-color: var(--ok); color: var(--ok); }
.inv-row .btn-use:hover { background: rgba(82, 201, 122, 0.15); }
.inv-row .btn-drop { border-color: var(--text-dim); color: var(--text-dim); }
.inv-row .btn-drop:hover { background: rgba(255, 255, 255, 0.06); }
.inv-row.has-passive { background: rgba(75, 201, 240, 0.05); border-radius: 3px; }
#gameover-stats, #victory-stats { line-height: 1.6; }

#shop-modal .stat-row { max-width: 200px; }
.shop-row { display: flex; align-items: center; gap: 8px; padding: 6px 0; border-bottom: 1px solid var(--border); flex-wrap: wrap; }
.shop-row .price { color: var(--accent); margin-left: auto; }
.shop-row button { padding: 4px 8px; font-size: 0.8rem; }

.item-passive { color: var(--accent-2); font-size: 0.85em; }
.craft-row {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px 0;
  border-bottom: 1px solid var(--border);
  flex-wrap: wrap;
}
.craft-row button { padding: 4px 8px; font-size: 0.8rem; margin-left: auto; }
.craft-row button:disabled { opacity: 0.4; cursor: not-allowed; }

/* Selected-item card pinned above the recipe list, once an item is chosen */
.craft-selected-item {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 10px 12px;
  margin-bottom: 12px;
  border: 1px solid var(--accent-2);
  border-radius: 4px;
  background: rgba(75, 201, 240, 0.06);
}
.craft-selected-item strong { flex: 1; }
.craft-selected-item button { padding: 4px 10px; font-size: 0.8rem; }

/* One recipe card per available passive */
.recipe-card {
  border: 1px solid var(--border);
  border-radius: 4px;
  padding: 10px 12px;
  margin-bottom: 10px;
}
.recipe-title { color: var(--accent-2); font-weight: bold; }
.recipe-desc { font-size: 0.82rem; margin: 2px 0 8px; }
.recipe-mats { display: flex; flex-direction: column; gap: 3px; margin-bottom: 8px; }
.recipe-mat { display: flex; align-items: center; gap: 6px; font-size: 0.82rem; }
.recipe-mat.have { color: var(--text); }
.recipe-mat.missing { color: var(--danger); }
.recipe-check { width: 1em; text-align: center; font-weight: bold; }
.recipe-count { margin-left: auto; color: var(--text-dim); }
.recipe-card button { padding: 5px 12px; font-size: 0.82rem; }
.recipe-card button:disabled { opacity: 0.4; cursor: not-allowed; }

.codex-row {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 7px 0;
  border-bottom: 1px solid var(--border);
  flex-wrap: wrap;
  font-size: 0.85rem;
}
.codex-row.unknown { opacity: 0.5; }
.codex-glyph { display: inline-block; min-width: 1.4em; text-align: center; font-weight: bold; }
.codex-row .muted { margin-left: auto; }
.codex-lore-text { color: var(--text-dim); font-style: italic; line-height: 1.4; }

/* ---------- Leaderboard ---------- */
#leaderboard-list {
  display: flex;
  flex-direction: column;
  gap: 4px;
  max-height: 320px;
  overflow-y: auto;
  margin: 12px 0;
}
.leaderboard-row {
  display: flex;
  align-items: center;
  gap: 10px;
  font-size: 0.85rem;
  padding: 6px 10px;
  border: 1px solid var(--border);
  border-radius: 4px;
  background: var(--bg-panel);
}
.leaderboard-row.you { border-color: var(--accent); color: var(--accent); }
.lb-rank { width: 22px; color: var(--text-dim); text-align: right; }
.leaderboard-row.you .lb-rank { color: var(--accent); }
.lb-name { flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.lb-floor { color: var(--text-dim); }
.leaderboard-row.you .lb-floor { color: var(--accent); }
.lb-level { color: var(--text-dim); font-size: 0.78rem; }

#leaderboard-submit { display: flex; gap: 8px; margin: 10px 0; flex-wrap: wrap; }
#leaderboard-submit input { flex: 1; min-width: 140px; }
#leaderboard-submit p { flex-basis: 100%; margin: 0; }
