:root {
  --ink: #030405;
  --bg-deep: #07090b;
  --panel-bg: #10151a;
  --panel-bg-2: #0b0f13;
  --cyan: #4fd8ff;
  --cyan-dim: #2a7f96;
  --orange: #ff9f43;
  --hazard: #ffcc33;
  --red: #ff5c5c;
  --green: #5cff9d;
  --text-light: #d7e6f0;
  --text-dim: #7d92a3;
}

* { box-sizing: border-box; }

body {
  margin: 0;
  background: var(--bg-deep);
  background-image:
    linear-gradient(rgba(79,216,255,0.05) 1px, transparent 1px),
    linear-gradient(90deg, rgba(79,216,255,0.05) 1px, transparent 1px);
  background-size: 24px 24px;
  color: var(--text-light);
  font-family: 'Share Tech Mono', 'Courier New', monospace;
  font-size: 17px;
  line-height: 1.35;
  display: flex;
  justify-content: center;
  position: relative;
}

/* Resistance-HQ hologram feel: faint CRT scanlines over the whole app,
   never intercepting clicks. */
body::before {
  content: '';
  position: fixed;
  inset: 0;
  pointer-events: none;
  z-index: 999;
  background-image: repeating-linear-gradient(
    to bottom,
    rgba(79,216,255,0.035) 0px,
    rgba(79,216,255,0.035) 1px,
    transparent 1px,
    transparent 3px
  );
  mix-blend-mode: overlay;
}

canvas { image-rendering: pixelated; image-rendering: crisp-edges; }

button, h2, h3, .hud-title, #map-label, .action-btn strong, .recipe-head strong {
  font-family: 'Orbitron', sans-serif;
  letter-spacing: 0.5px;
}

/* Angular tactical corner brackets, reused on panels/HUD */
.bracketed {
  position: relative;
}
.bracketed::before, .bracketed::after {
  content: '';
  position: absolute;
  width: 14px; height: 14px;
  pointer-events: none;
}
.bracketed::before {
  top: -2px; left: -2px;
  border-top: 2px solid var(--cyan);
  border-left: 2px solid var(--cyan);
}
.bracketed::after {
  bottom: -2px; right: -2px;
  border-bottom: 2px solid var(--cyan);
  border-right: 2px solid var(--cyan);
}

#app {
  width: 740px;
  max-width: 100%;
  padding: 12px;
}

/* ---------- HUD ---------- */
#hud {
  display: flex;
  align-items: center;
  gap: 10px;
  background: var(--panel-bg-2);
  border: 1px solid var(--cyan-dim);
  padding: 8px 14px;
  margin-bottom: 6px;
  flex-wrap: wrap;
}
.hud-title { font-size: 0.95em; font-weight: 700; color: var(--cyan); text-transform: uppercase; margin-right: 4px; }
.hud-stats { display: flex; align-items: center; gap: 8px; flex: 1; flex-wrap: wrap; }
#hud-level { font-family: 'Orbitron', sans-serif; font-size: 0.85em; color: var(--text-light); }
.hud-cycle-tag {
  font-family: 'Orbitron', sans-serif; font-size: 0.7em; color: var(--hazard);
  border: 1px solid var(--hazard); padding: 1px 5px;
}
#map-label {
  text-align: center; font-size: 0.8em; color: var(--cyan);
  background: var(--panel-bg-2); border: 1px solid var(--cyan-dim);
  padding: 5px; margin-bottom: 2px; letter-spacing: 2px; text-transform: uppercase;
}
#map-exits {
  text-align: center; font-family: 'Share Tech Mono', monospace; font-size: 0.95em;
  color: var(--text-dim); min-height: 1.3em; margin-bottom: 6px;
}

/* ---------- Stage / canvas ---------- */
#stage { position: relative; }
canvas#overworld-canvas {
  display: block; margin: 0 auto;
  border: 1px solid var(--cyan-dim);
  box-shadow: 0 0 16px rgba(79,216,255,0.08);
  background: #0a0e14;
  max-width: 100%;
  height: auto;
}
#stage::before {
  content: '';
  position: absolute;
  inset: 0;
  pointer-events: none;
  box-shadow: inset 0 0 60px 20px rgba(0,0,0,0.65);
}

/* Reduced Motion (Settings > Display): drops the CRT scanline/vignette
   overlay and every hit/crit/miss/pulse animation to a static equivalent,
   for motion sensitivity. Toggled via a class on <body> rather than the
   prefers-reduced-motion media query, since it's a game setting the player
   can change from inside the app rather than relying on OS-level config. */
body.reduced-motion::before,
body.reduced-motion #stage::before {
  display: none;
}
body.reduced-motion .portrait-hit,
body.reduced-motion .portrait-crit,
body.reduced-motion .portrait-miss,
body.reduced-motion .dmg-popup,
body.reduced-motion .bar-fill.hp.critical {
  animation: none !important;
}

.hint { text-align: center; font-size: 0.85em; color: var(--text-dim); margin: 8px 0 0; }

/* ---------- Bars (tactical readout meters) ---------- */
.bar-wrap {
  width: 90px; height: 10px; background: #0a0e14;
  border: 1px solid var(--cyan-dim); position: relative;
}
.bar-wrap.small { width: 74px; }
.bar-fill {
  height: 100%;
  background-image: repeating-linear-gradient(90deg, transparent 0 5px, rgba(0,0,0,0.5) 5px 6px);
}
.bar-fill.hp { background-color: var(--red); box-shadow: 0 0 6px rgba(255,92,92,0.5); }
.bar-fill.xp { background-color: var(--cyan); box-shadow: 0 0 6px rgba(79,216,255,0.5); }
.bar-fill.charge { background-color: var(--orange); box-shadow: 0 0 6px rgba(255,204,51,0.5); }
.bar-fill.rep { background-color: var(--green); box-shadow: 0 0 6px rgba(92,255,157,0.5); }
.bar-fill.hp.critical { animation: hp-critical-pulse 0.8s ease-in-out infinite; }
@keyframes hp-critical-pulse {
  0%, 100% { box-shadow: 0 0 6px rgba(255,92,92,0.5); filter: brightness(1); }
  50% { box-shadow: 0 0 14px rgba(255,92,92,0.95); filter: brightness(1.4); }
}
.bar-text { font-family: 'Share Tech Mono', monospace; font-size: 0.85em; color: var(--text-light); }

/* ---------- Buttons ---------- */
.action-btn {
  background: var(--panel-bg);
  color: var(--text-light);
  border: 1px solid var(--cyan-dim);
  clip-path: polygon(8px 0, 100% 0, 100% calc(100% - 8px), calc(100% - 8px) 100%, 0 100%, 0 8px);
  padding: 10px 12px;
  font-size: 0.82em;
  cursor: pointer;
  text-align: left;
}
.action-btn:hover:not(:disabled) { border-color: var(--cyan); background: #17202c; box-shadow: 0 0 8px rgba(79,216,255,0.25); }
.action-btn:active:not(:disabled) { background: #0d141c; }
.action-btn:disabled { opacity: 0.4; cursor: not-allowed; }
.action-btn.primary { background: #123b2c; border-color: var(--green); color: var(--green); text-align: center; }
.action-btn.ghost { background: transparent; border-color: var(--cyan-dim); color: var(--cyan); }
.action-btn.ability-btn { display: flex; flex-direction: column; gap: 6px; }
.ability-desc { font-family: 'Share Tech Mono', monospace; font-size: 1.05em; color: var(--text-dim); }
.charge-tag { font-size: 0.8em; color: var(--orange); }
.key-hint {
  display: inline-block; min-width: 1.2em; padding: 0 3px; margin-right: 4px;
  background: var(--panel-bg-2); border: 1px solid var(--cyan-dim); color: var(--cyan);
  font-family: 'Share Tech Mono', monospace; font-size: 0.75em; text-align: center;
}
#battle-pack-mates { display: flex; flex-direction: column; gap: 4px; margin-bottom: 8px; }
.pack-mate { font-size: 0.9em; color: var(--text-light); background: var(--panel-bg-2); border: 1px solid var(--cyan-dim); padding: 4px 8px; }
.pack-mate-dead { color: var(--text-dim); text-decoration: line-through; opacity: 0.6; }
.action-btn.flee-btn { grid-column: 1 / -1; text-align: center; background: #3a1414; border-color: var(--red); color: var(--red); }

/* ---------- Panels ---------- */
.panel {
  position: absolute; inset: 0;
  background: rgba(9,13,19,0.97);
  color: var(--text-light);
  border: 1px solid var(--cyan-dim);
  padding: 14px;
  overflow-y: auto;
}
#battle-panel, #craft-panel, #codex-panel, #chronicle-panel, #victory-panel { position: relative; margin-top: 8px; min-height: 300px; }
.hidden { display: none !important; }

/* A floating modal over the battle panel, not another full-bleed panel in
   the normal show/hide rotation — the player needs to see the puzzle
   without losing the battle state underneath it. Shared by every aiming
   puzzle (Snell's law, diffraction fringe, ...). */
.puzzle-overlay {
  position: fixed; inset: auto; top: 50%; left: 50%; transform: translate(-50%, -50%);
  width: min(320px, 90vw); max-height: 85vh; overflow-y: auto;
  z-index: 600; box-shadow: 0 0 30px rgba(0,0,0,0.6);
}
.puzzle-overlay canvas { display: block; margin: 8px auto; background: var(--panel-bg-2); border: 1px solid var(--cyan-dim); }
.puzzle-overlay .puzzle-fire { width: 100%; margin-top: 10px; }

#dialogue-panel {
  position: absolute; left: 0; right: 0; bottom: 0;
  background: rgba(9,13,19,0.97); color: var(--text-light);
  border: 1px solid var(--cyan-dim); border-bottom: none;
  padding: 12px;
}
#dialogue-text { min-height: 3em; font-size: 1.05em; }
.choice-list { display: flex; flex-direction: column; gap: 6px; margin-bottom: 8px; }
.choice-list .action-btn { background: var(--panel-bg-2); }

/* ---------- Battle ---------- */
.battle-row { display: flex; justify-content: space-around; margin-bottom: 10px; }
.combatant { text-align: center; position: relative; }
.portrait-canvas {
  background: #0a0e14;
  border: 1px solid var(--cyan-dim);
  box-shadow: inset 0 0 12px rgba(0,0,0,0.6);
  margin-bottom: 4px;
}

/* Hit feedback on battle portraits */
.portrait-hit { animation: portrait-shake 0.35s ease; }
.portrait-crit { animation: portrait-shake-big 0.4s ease; box-shadow: 0 0 18px 4px rgba(255,204,51,0.8) !important; }
.portrait-miss { animation: portrait-fade 0.35s ease; }
@keyframes portrait-shake {
  0%, 100% { transform: translateX(0); }
  20% { transform: translateX(-6px); }
  40% { transform: translateX(5px); }
  60% { transform: translateX(-4px); }
  80% { transform: translateX(3px); }
}
@keyframes portrait-shake-big {
  0%, 100% { transform: translateX(0) scale(1); }
  15% { transform: translateX(-9px) scale(1.03); }
  35% { transform: translateX(8px) scale(1.03); }
  55% { transform: translateX(-6px) scale(1.02); }
  75% { transform: translateX(5px) scale(1.01); }
}
@keyframes portrait-fade {
  0%, 100% { opacity: 1; }
  50% { opacity: 0.4; }
}

.dmg-popup {
  position: absolute;
  top: 10px; left: 50%;
  transform: translateX(-50%);
  font-family: 'Orbitron', sans-serif;
  font-size: 1.1em;
  color: var(--red);
  text-shadow: 0 0 6px rgba(0,0,0,0.9), 0 0 2px #000;
  pointer-events: none;
  animation: dmg-popup-float 0.9s ease-out forwards;
  z-index: 5;
}
.dmg-popup-crit { color: var(--hazard); font-size: 1.4em; }
.dmg-popup-miss { color: var(--text-dim); font-size: 0.9em; }
@keyframes dmg-popup-float {
  0% { transform: translate(-50%, 0); opacity: 0; }
  15% { opacity: 1; }
  100% { transform: translate(-50%, -34px); opacity: 0; }
}
#battle-log {
  background: #0a0e14; color: var(--text-light);
  border: 1px solid var(--cyan-dim);
  padding: 8px; height: 110px; overflow-y: auto; font-size: 1em; margin-bottom: 10px;
}
.action-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }

/* ---------- Craft / Codex / Chronicle panels ---------- */
.panel-header {
  display: flex; justify-content: space-between; align-items: center;
  position: relative; padding-top: 9px; margin-bottom: 4px;
}
.panel-header::before {
  content: '';
  position: absolute; top: 0; left: 0; right: 0;
  height: 5px;
  background-image: repeating-linear-gradient(45deg, var(--hazard) 0 8px, var(--ink) 8px 16px);
  opacity: 0.85;
}
.panel-header h2 { font-size: 0.95em; color: var(--cyan); }
.mat-grid { display: grid; grid-template-columns: repeat(2, 1fr); gap: 6px; margin-bottom: 10px; }
.mat-chip {
  background: var(--panel-bg-2); border: 1px solid var(--cyan-dim); padding: 6px 8px; font-size: 1em;
}
.equip-grid { display: grid; grid-template-columns: repeat(2, 1fr); gap: 8px; margin-bottom: 10px; }
.equip-slot {
  display: flex; flex-direction: column; gap: 4px;
  background: var(--panel-bg-2); border: 1px solid var(--cyan-dim); padding: 6px 8px;
}
.equip-slot label { font-family: 'Orbitron', sans-serif; font-size: 0.6em; color: var(--text-dim); text-transform: uppercase; }
.equip-slot select {
  background: #0a0e14; color: var(--text-light); border: 1px solid var(--cyan-dim); padding: 4px;
  font-family: 'Share Tech Mono', monospace; font-size: 1em;
}
.equip-effect { font-size: 0.85em; color: var(--green); line-height: 1.3; }
.recipe-list { display: flex; flex-direction: column; gap: 8px; }
.trading-row { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; margin-bottom: 6px; }
.trading-row select {
  background: #0a0e14; color: var(--text-light); border: 1px solid var(--cyan-dim); padding: 4px;
  font-family: 'Share Tech Mono', monospace; font-size: 0.9em; flex: 1; min-width: 120px;
}
.trading-row span { font-size: 0.85em; color: var(--text-dim); }
.recipe-row { background: var(--panel-bg-2); border: 1px solid var(--cyan-dim); padding: 8px; }
.recipe-head { margin-bottom: 4px; font-size: 1.05em; }
.slot-tag { font-family: 'Orbitron', sans-serif; font-size: 0.55em; color: var(--orange); margin-left: 6px; text-transform: uppercase; }
.recipe-req { font-size: 0.95em; color: var(--text-light); }
.recipe-effects { margin: 4px 0; padding-left: 18px; font-size: 0.95em; color: var(--green); }
.recipe-effects li { margin-bottom: 2px; }
.recipe-fact { font-size: 0.92em; color: var(--text-dim); margin: 4px 0 8px; }

.slot-row { background: var(--panel-bg-2); border: 1px solid var(--cyan-dim); padding: 8px; margin-bottom: 8px; }
.slot-row-current { border-color: var(--orange); }
.slot-row-head { font-family: 'Orbitron', sans-serif; font-size: 0.85em; color: var(--cyan); margin-bottom: 2px; }
.slot-row-desc { font-size: 0.9em; color: var(--text-dim); margin-bottom: 6px; }
.slot-row-actions { display: flex; gap: 8px; }
.slot-row-actions .action-btn { flex: 1; }

.whatsnew-list { margin: 4px 0 0; padding-left: 18px; font-size: 0.95em; color: var(--text-light); }
.whatsnew-list li { margin-bottom: 4px; }
.recipe-btn-row { display: flex; gap: 8px; }
.recipe-btn-row .action-btn { flex: 1; }

.codex-progress { color: var(--orange); margin-bottom: 8px; font-family: 'Orbitron', sans-serif; font-size: 0.7em; }
.codex-list { display: flex; flex-direction: column; gap: 10px; }
.codex-entry { background: var(--panel-bg-2); border: 1px solid var(--cyan-dim); padding: 10px; }
.codex-entry h3 { margin: 0 0 6px; color: var(--green); font-size: 0.85em; }
.codex-entry.locked { border-color: #2a3440; }
.codex-entry.locked h3 { color: var(--text-dim); }
.codex-entry.quest-ready { border-color: var(--green); box-shadow: 0 0 8px rgba(125,255,176,0.25); }
.codex-entry p { margin: 0; font-size: 1em; line-height: 1.4; color: var(--text-light); }

#victory-panel { text-align: center; }
#victory-panel h2 { color: var(--orange); font-size: 1.1em; line-height: 1.6; }
#victory-ngplus { margin-top: 8px; }
.ngplus-hint { font-size: 0.85em; color: var(--text-dim); margin-top: 8px; }

/* ---------- Completion / Field Log ---------- */
.completion-row { display: flex; flex-direction: column; gap: 4px; margin-bottom: 12px; }
.completion-row-head { display: flex; justify-content: space-between; font-size: 0.95em; }
.completion-bar { width: 100%; height: 12px; }
.completion-subhead { color: var(--orange); font-family: 'Orbitron', sans-serif; font-size: 0.8em; margin: 16px 0 8px; border-top: 1px solid var(--cyan-dim); padding-top: 12px; }
.completion-stats { display: flex; flex-direction: column; gap: 4px; margin-bottom: 12px; }
.save-transfer-row { display: flex; gap: 8px; }
.save-transfer-row .action-btn { flex: 1; }
.save-transfer-msg { font-size: 0.85em; color: var(--text-dim); min-height: 1.2em; margin-top: 6px; }
.settings-select {
  width: 100%; background: #0a0e14; color: var(--text-light); border: 1px solid var(--cyan-dim);
  padding: 6px 8px; font-family: 'Share Tech Mono', monospace; font-size: 1em; margin-bottom: 8px;
}
#settings-panel .action-btn { width: 100%; margin-bottom: 8px; }
.volume-row { display: flex; align-items: center; justify-content: space-between; gap: 10px; margin-bottom: 8px; }
.volume-row label { font-size: 0.85em; color: var(--text-dim); flex: 0 0 auto; }
.volume-row input[type="range"] { flex: 1; accent-color: var(--cyan-dim); }

/* ---------- Tactical Map ---------- */
.map-grid {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 10px;
}
.map-node {
  background: var(--panel-bg-2); border: 1px solid var(--cyan-dim);
  padding: 12px 8px; text-align: center; min-height: 60px;
  display: flex; flex-direction: column; justify-content: center; gap: 4px;
}
.map-node-name { font-family: 'Orbitron', sans-serif; font-size: 0.68em; color: var(--cyan); }
.map-node-status { font-size: 0.85em; color: var(--text-dim); }
.map-node-wanderer { font-size: 0.72em; color: var(--text-dim); opacity: 0.85; }
.map-node-current { border-color: var(--hazard); box-shadow: 0 0 10px rgba(255,204,51,0.4); }
.map-node-current .map-node-name { color: var(--hazard); }
.map-node-unvisited { opacity: 0.55; }
.map-node-empty { background: transparent; border: none; }

/* ---------- On-screen D-pad (touch/mobile movement) ---------- */
#dpad {
  position: absolute; right: 14px; bottom: 14px;
  display: grid;
  grid-template-columns: repeat(3, 34px);
  grid-template-rows: repeat(3, 34px);
  gap: 2px;
  opacity: 0.7;
}
#dpad:hover, #dpad:active { opacity: 1; }
.dpad-btn {
  background: rgba(18,24,33,0.85); color: var(--cyan);
  border: 1px solid var(--cyan-dim); font-size: 14px;
  cursor: pointer; user-select: none; touch-action: manipulation;
}
.dpad-btn:active { background: var(--cyan-dim); }
.dpad-up { grid-column: 2; grid-row: 1; }
.dpad-left { grid-column: 1; grid-row: 2; }
.dpad-down { grid-column: 2; grid-row: 3; }
.dpad-right { grid-column: 3; grid-row: 2; }

@media (max-width: 480px) {
  body { font-size: 15px; }
  #app { padding: 6px; }
  .action-btn { font-size: 0.75em; padding: 8px; }
  #dpad { grid-template-columns: repeat(3, 42px); grid-template-rows: repeat(3, 42px); opacity: 0.85; }
}

/* ---------- Toast notifications ---------- */
#toast-container {
  position: fixed; top: 12px; right: 12px; z-index: 500;
  display: flex; flex-direction: column; gap: 8px;
  pointer-events: none; max-width: 320px;
}
.toast {
  background: var(--panel-bg-2); border: 1px solid var(--hazard); color: var(--text-light);
  padding: 10px 14px; font-family: 'Share Tech Mono', monospace; font-size: 0.85em; line-height: 1.35;
  box-shadow: 0 0 10px rgba(255,204,51,0.25);
  opacity: 0; transform: translateX(24px);
  transition: opacity 0.25s ease, transform 0.25s ease;
}
.toast.toast-visible { opacity: 1; transform: translateX(0); }

/* ---------- HUD "new content" badges ---------- */
.action-btn.has-badge { position: relative; }
.action-btn.has-badge::after {
  content: ''; position: absolute; top: -4px; right: -4px;
  width: 9px; height: 9px; border-radius: 50%;
  background: var(--hazard); box-shadow: 0 0 5px rgba(255,204,51,0.8);
}
