/* ============================================================
   Dungeon Escape — Stylesheet
   Flat, clean, minimal. No neon, no glassmorphism, no gradients
   beyond the very subtle shadows used for depth. Themeable via
   CSS variables driven by [data-theme] on <html>.
   ============================================================ */

:root {
  /* Fallback (dark, the app default) so nothing flashes unstyled. */
  --color-bg: #14161B;
  --color-panel: #1D2027;
  --color-surface-2: #262A33;
  --color-surface-2-hover: #2E333D;
  --color-input-bg: #21242B;
  --color-border: #2E323C;
  --color-text: #F1F2F4;
  --color-text-muted: #9AA0AC;
  --color-overlay-bg: rgba(15, 16, 20, 0.92);
  --shadow-soft: 0 2px 10px rgba(0, 0, 0, 0.45);
  --shadow-panel: 0 8px 30px rgba(0, 0, 0, 0.6);

  /* Brand / gameplay accent colors — intentionally constant across themes. */
  --color-player: #3B82F6;
  --color-player-dark: #2563EB;
  --color-enemy: #EF4444;
  --color-key: #F59E0B;
  --color-exit: #10B981;
  --color-coin: #FBBF24;
  --color-danger: #EF4444;

  /* Fallback joystick contrast (dark-theme values). */
  --joystick-ring-fill: rgba(255, 255, 255, 0.14);
  --joystick-ring-border: rgba(255, 255, 255, 0.38);
  --joystick-knob-fill: rgba(96, 165, 250, 0.92);
  --joystick-knob-border: rgba(255, 255, 255, 0.85);

  --radius-md: 12px;
  --radius-sm: 8px;
  --font-main: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;

  /* Controlled live from Settings > Controls > Joystick Opacity (0-1). */
  --joystick-opacity: 0.6;
}

html[data-theme="light"] {
  --color-bg: #F5F5F5;
  --color-panel: #FFFFFF;
  --color-surface-2: #F3F4F6;
  --color-surface-2-hover: #E5E7EB;
  --color-input-bg: #FAFAFA;
  --color-border: #E5E7EB;
  --color-text: #1F1F1F;
  --color-text-muted: #6B7280;
  --color-overlay-bg: rgba(245, 245, 245, 0.92);
  --shadow-soft: 0 2px 10px rgba(0, 0, 0, 0.08);
  --shadow-panel: 0 8px 30px rgba(0, 0, 0, 0.12);

  /* Darker joystick visuals against a light backdrop. */
  --joystick-ring-fill: rgba(31, 31, 41, 0.16);
  --joystick-ring-border: rgba(31, 31, 41, 0.32);
  --joystick-knob-fill: rgba(37, 99, 235, 0.88);
  --joystick-knob-border: rgba(255, 255, 255, 0.9);
}

html[data-theme="dark"] {
  --color-bg: #14161B;
  --color-panel: #1D2027;
  --color-surface-2: #262A33;
  --color-surface-2-hover: #2E333D;
  --color-input-bg: #21242B;
  --color-border: #2E323C;
  --color-text: #F1F2F4;
  --color-text-muted: #9AA0AC;
  --color-overlay-bg: rgba(15, 16, 20, 0.92);
  --shadow-soft: 0 2px 10px rgba(0, 0, 0, 0.45);
  --shadow-panel: 0 8px 30px rgba(0, 0, 0, 0.6);

  /* Brighter joystick visuals so it still reads clearly against a dark backdrop. */
  --joystick-ring-fill: rgba(255, 255, 255, 0.14);
  --joystick-ring-border: rgba(255, 255, 255, 0.38);
  --joystick-knob-fill: rgba(96, 165, 250, 0.92);
  --joystick-knob-border: rgba(255, 255, 255, 0.85);
}

* {
  box-sizing: border-box;
}

html, body {
  margin: 0;
  padding: 0;
  height: 100%;
  overflow: hidden;
  background: var(--color-bg);
  color: var(--color-text);
  font-family: var(--font-main);
  -webkit-tap-highlight-color: transparent;
}

body {
  display: flex;
  flex-direction: column;
  height: 100dvh;
  transition: background-color 0.2s ease, color 0.2s ease;
}

button {
  font-family: inherit;
}

.hidden {
  display: none !important;
}

/* ---------- Generic screen / panel ---------- */

.screen {
  position: fixed;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px;
  z-index: 30;
}

.overlay {
  background: var(--color-overlay-bg);
  backdrop-filter: blur(2px);
  animation: fadeIn 0.18s ease-out;
}

.panel {
  background: var(--color-panel);
  border: 1px solid var(--color-border);
  border-radius: var(--radius-md);
  box-shadow: var(--shadow-panel);
  padding: 32px 28px;
  width: 100%;
  max-width: 380px;
  text-align: center;
  animation: panelIn 0.22s cubic-bezier(0.16, 1, 0.3, 1);
  transition: background-color 0.2s ease, border-color 0.2s ease;
}

.start-panel {
  max-width: 400px;
}

.game-title {
  margin: 0 0 6px;
  font-size: 30px;
  font-weight: 700;
  letter-spacing: -0.02em;
}

.game-subtitle {
  margin: 0 0 24px;
  color: var(--color-text-muted);
  font-size: 14px;
}

.panel-title {
  margin: 0 0 20px;
  font-size: 22px;
  font-weight: 700;
}

.field-label {
  display: block;
  text-align: left;
  font-size: 12px;
  font-weight: 600;
  color: var(--color-text-muted);
  margin-bottom: 6px;
  text-transform: uppercase;
  letter-spacing: 0.04em;
}

.text-input {
  width: 100%;
  padding: 12px 14px;
  border-radius: var(--radius-sm);
  border: 1px solid var(--color-border);
  background: var(--color-input-bg);
  font-size: 15px;
  color: var(--color-text);
  margin-bottom: 18px;
  outline: none;
  transition: border-color 0.15s ease, background-color 0.2s ease;
}

.text-input:focus {
  border-color: var(--color-player);
  background: var(--color-panel);
}

.btn {
  display: block;
  width: 100%;
  padding: 13px 16px;
  border-radius: var(--radius-sm);
  border: none;
  font-size: 15px;
  font-weight: 600;
  cursor: pointer;
  margin-bottom: 10px;
  transition: transform 0.08s ease, opacity 0.15s ease, background-color 0.15s ease, color 0.15s ease;
}

.btn:last-child {
  margin-bottom: 0;
}

.btn:active {
  transform: scale(0.98);
}

.btn:focus-visible {
  outline: 2px solid var(--color-player);
  outline-offset: 2px;
}

.btn-primary {
  background: var(--color-player);
  color: #FFFFFF;
}

.btn-primary:hover {
  background: var(--color-player-dark);
}

.btn-secondary {
  background: var(--color-surface-2);
  color: var(--color-text);
  border: 1px solid var(--color-border);
}

.btn-secondary:hover {
  background: var(--color-surface-2-hover);
}

.btn-ghost {
  background: transparent;
  color: var(--color-text-muted);
}

.btn-ghost:hover {
  color: var(--color-text);
}

.instructions {
  margin-top: 22px;
  padding-top: 18px;
  border-top: 1px solid var(--color-border);
}

.instructions-row {
  display: flex;
  align-items: center;
  gap: 8px;
  justify-content: center;
  margin-bottom: 8px;
}

.instructions-row:last-child {
  margin-bottom: 0;
}

.key-badge {
  font-size: 11px;
  font-weight: 700;
  background: var(--color-surface-2);
  border: 1px solid var(--color-border);
  border-radius: 6px;
  padding: 3px 8px;
  color: var(--color-text-muted);
}

.instructions-text {
  font-size: 13px;
  color: var(--color-text-muted);
}

.stats-list {
  margin-bottom: 22px;
}

.stats-row {
  display: flex;
  justify-content: space-between;
  padding: 10px 0;
  border-bottom: 1px solid var(--color-border);
}

.stats-row:last-child {
  border-bottom: none;
}

.stats-label {
  font-size: 13px;
  color: var(--color-text-muted);
}

.stats-value {
  font-size: 14px;
  font-weight: 700;
}

/* ---------- HUD ---------- */

.hud {
  flex: 0 0 auto;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 10px 16px;
  background: var(--color-panel);
  border-bottom: 1px solid var(--color-border);
  box-shadow: var(--shadow-soft);
  z-index: 20;
  transition: background-color 0.2s ease, border-color 0.2s ease;
}

.hud-left {
  display: flex;
  flex-direction: column;
  min-width: 0;
}

.hud-name {
  font-size: 13px;
  font-weight: 700;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  max-width: 30vw;
}

.hud-floor {
  font-size: 11px;
  color: var(--color-text-muted);
}

.hud-center {
  flex: 1;
  display: flex;
  align-items: center;
  gap: 8px;
  max-width: 220px;
}

.health-track {
  flex: 1;
  height: 10px;
  border-radius: 999px;
  background: var(--color-surface-2);
  overflow: hidden;
}

.health-fill {
  height: 100%;
  width: 100%;
  background: var(--color-danger);
  border-radius: 999px;
  transition: width 0.2s ease;
}

.health-text {
  font-size: 11px;
  font-weight: 600;
  color: var(--color-text-muted);
  min-width: 52px;
  text-align: right;
}

.hud-right {
  display: flex;
  align-items: center;
  gap: 6px;
}

.coin-icon {
  width: 12px;
  height: 12px;
  border-radius: 50%;
  background: var(--color-coin);
  border: 1.5px solid #D97706;
  display: inline-block;
}

.hud-coins {
  font-size: 14px;
  font-weight: 700;
  min-width: 18px;
}

.icon-btn {
  width: 32px;
  height: 32px;
  border-radius: 50%;
  border: 1px solid var(--color-border);
  background: var(--color-input-bg);
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  margin-left: 4px;
  transition: background-color 0.15s ease, transform 0.08s ease;
}

.icon-btn:hover {
  background: var(--color-surface-2-hover);
}

.icon-btn:active {
  transform: scale(0.94);
}

.pause-icon {
  position: relative;
  width: 10px;
  height: 10px;
}

.pause-icon::before,
.pause-icon::after {
  content: '';
  position: absolute;
  top: 0;
  width: 3px;
  height: 10px;
  background: var(--color-text);
  border-radius: 1px;
}

.pause-icon::before { left: 1px; }
.pause-icon::after { right: 1px; }

/* ---------- Game area ---------- */

#game-container {
  flex: 1;
  min-height: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  position: relative;
  background: var(--color-bg);
  padding: 12px;
}

#game-canvas {
  /* Kept intentionally theme-independent: the dungeon's paper-white
     floor is core to its art direction and to entity readability. */
  background: #FFFFFF;
  border: 1px solid var(--color-border);
  border-radius: var(--radius-sm);
  box-shadow: var(--shadow-soft);
  touch-action: none;
  user-select: none;
}

.floor-banner {
  position: absolute;
  top: 24px;
  left: 50%;
  transform: translateX(-50%);
  background: rgba(31, 31, 31, 0.85);
  color: #FFFFFF;
  padding: 8px 22px;
  border-radius: 999px;
  font-size: 14px;
  font-weight: 700;
  letter-spacing: 0.03em;
  pointer-events: none;
  opacity: 1;
  transition: opacity 0.4s ease;
  z-index: 15;
}

.floor-banner.fade-out {
  opacity: 0;
}

/* ---------- Pickup toast (healing feedback etc.) ---------- */

.pickup-toast {
  position: absolute;
  top: 68px;
  left: 50%;
  transform: translateX(-50%) translateY(0);
  background: rgba(244, 63, 94, 0.92);
  color: #FFFFFF;
  padding: 6px 18px;
  border-radius: 999px;
  font-size: 13px;
  font-weight: 700;
  letter-spacing: 0.02em;
  pointer-events: none;
  z-index: 16;
  opacity: 1;
  transition: opacity 0.3s ease, transform 0.3s ease;
}

.pickup-toast.pop {
  animation: toastPop 0.28s cubic-bezier(0.34, 1.56, 0.64, 1);
}

.pickup-toast.fade-out {
  opacity: 0;
  transform: translateX(-50%) translateY(-8px);
}

/* ---------- Virtual joystick (touch devices only) ---------- */

.joystick-base {
  display: none;
  position: absolute;
  left: clamp(14px, 4vw, 30px);
  bottom: clamp(14px, 5vh, 30px);
  /* Scales with the smaller viewport dimension so it sizes sensibly on
     small phones, large phones, and tablets alike — no fixed px steps. */
  width: clamp(72px, 20vmin, 132px);
  height: clamp(72px, 20vmin, 132px);
  border-radius: 50%;
  background: var(--joystick-ring-fill);
  border: 1.5px solid var(--joystick-ring-border);
  box-shadow: inset 0 0 0 1px rgba(255, 255, 255, 0.06), var(--shadow-soft);
  touch-action: none;
  z-index: 18;
  opacity: var(--joystick-opacity);
  transition: opacity 0.15s ease, background-color 0.2s ease, border-color 0.2s ease;
}

.joystick-knob {
  position: absolute;
  top: 50%;
  left: 50%;
  width: 44%;
  height: 44%;
  border-radius: 50%;
  background: var(--joystick-knob-fill);
  border: 2px solid var(--joystick-knob-border);
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.35);
  transform: translate(-50%, -50%);
  transition: transform 0.02s linear, background-color 0.2s ease;
}

body.touch-device .joystick-base {
  display: block;
}

/* ---------- Watermark ----------
   Fixed, corner-anchored, non-interactive so it never blocks panel
   buttons or gameplay input; visible above every screen including
   menus and overlays. */

.watermark {
  position: fixed;
  left: 12px;
  bottom: 8px;
  z-index: 40;
  display: flex;
  align-items: center;
  gap: 6px;
  font-size: 11px;
  color: var(--color-text-muted);
  opacity: 0.6;
  letter-spacing: 0.02em;
  pointer-events: none;
  user-select: none;
}

.watermark img {
  width: 14px;
  height: 14px;
  border-radius: 3px;
  display: block;
  opacity: 0.9;
}

/* ---------- Settings menu ---------- */

.settings-panel {
  max-width: 440px;
  text-align: left;
  display: flex;
  flex-direction: column;
  max-height: 82vh;
}

.settings-panel .panel-title {
  text-align: center;
}

.settings-scroll {
  overflow-y: auto;
  padding-right: 4px;
  margin: 0 -4px 14px;
  flex: 1;
  min-height: 0;
}

.settings-group {
  margin-bottom: 18px;
}

.settings-group:last-child {
  margin-bottom: 0;
}

.settings-group-title {
  margin: 0 0 10px;
  font-size: 11px;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--color-text-muted);
}

.settings-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 14px;
  padding: 9px 0;
}

.settings-row-label {
  font-size: 14px;
  font-weight: 600;
  flex: 0 0 auto;
}

.settings-control {
  display: flex;
  align-items: center;
  gap: 10px;
  flex: 1;
  justify-content: flex-end;
  min-width: 0;
}

.settings-row-value {
  font-size: 12px;
  font-weight: 700;
  color: var(--color-text-muted);
  min-width: 44px;
  text-align: right;
  flex: 0 0 auto;
}

.settings-done {
  margin-top: 4px;
}

/* Slider */

.slider {
  -webkit-appearance: none;
  appearance: none;
  flex: 1;
  min-width: 90px;
  height: 4px;
  border-radius: 999px;
  background: var(--color-surface-2);
  outline: none;
  cursor: pointer;
}

.slider::-webkit-slider-thumb {
  -webkit-appearance: none;
  appearance: none;
  width: 16px;
  height: 16px;
  border-radius: 50%;
  background: var(--color-player);
  border: 2px solid var(--color-panel);
  box-shadow: var(--shadow-soft);
  cursor: pointer;
}

.slider::-moz-range-thumb {
  width: 16px;
  height: 16px;
  border-radius: 50%;
  background: var(--color-player);
  border: 2px solid var(--color-panel);
  box-shadow: var(--shadow-soft);
  cursor: pointer;
}

.slider::-moz-range-track {
  height: 4px;
  border-radius: 999px;
  background: var(--color-surface-2);
}

/* Select */

.select-input {
  appearance: none;
  -webkit-appearance: none;
  background-color: var(--color-input-bg);
  border: 1px solid var(--color-border);
  border-radius: var(--radius-sm);
  color: var(--color-text);
  font-family: inherit;
  font-size: 13px;
  font-weight: 600;
  padding: 8px 30px 8px 12px;
  cursor: pointer;
  background-image:
    linear-gradient(45deg, transparent 50%, var(--color-text-muted) 50%),
    linear-gradient(135deg, var(--color-text-muted) 50%, transparent 50%);
  background-position: calc(100% - 16px) center, calc(100% - 11px) center;
  background-size: 5px 5px, 5px 5px;
  background-repeat: no-repeat;
}

.select-input:focus {
  outline: none;
  border-color: var(--color-player);
}

/* Toggle switch */

.switch {
  position: relative;
  display: inline-block;
  width: 42px;
  height: 24px;
  flex: 0 0 auto;
  cursor: pointer;
}

.switch input {
  position: absolute;
  opacity: 0;
  width: 100%;
  height: 100%;
  margin: 0;
  cursor: pointer;
  z-index: 1;
}

.switch-track {
  position: absolute;
  inset: 0;
  background: var(--color-surface-2);
  border: 1px solid var(--color-border);
  border-radius: 999px;
  transition: background-color 0.15s ease;
}

.switch-track::before {
  content: '';
  position: absolute;
  top: 2px;
  left: 2px;
  width: 18px;
  height: 18px;
  border-radius: 50%;
  background: var(--color-panel);
  box-shadow: var(--shadow-soft);
  transition: transform 0.18s cubic-bezier(0.34, 1.56, 0.64, 1);
}

.switch input:checked ~ .switch-track {
  background: var(--color-player);
  border-color: var(--color-player);
}

.switch input:checked ~ .switch-track::before {
  transform: translateX(18px);
  background: #FFFFFF;
}

.switch input:focus-visible ~ .switch-track {
  outline: 2px solid var(--color-player);
  outline-offset: 2px;
}

/* ---------- Larger UI (Accessibility) ---------- */

body.large-ui .game-title { font-size: 34px; }
body.large-ui .game-subtitle { font-size: 15px; }
body.large-ui .panel-title { font-size: 25px; }
body.large-ui .btn { font-size: 17px; padding: 15px 16px; }
body.large-ui .field-label { font-size: 13px; }
body.large-ui .text-input { font-size: 16px; padding: 14px 15px; }
body.large-ui .hud { padding: 13px 18px; }
body.large-ui .hud-name { font-size: 15px; }
body.large-ui .hud-floor { font-size: 12px; }
body.large-ui .health-text { font-size: 12px; min-width: 58px; }
body.large-ui .hud-coins { font-size: 16px; }
body.large-ui .icon-btn { width: 38px; height: 38px; }
body.large-ui .stats-label,
body.large-ui .stats-value { font-size: 15px; }
body.large-ui .settings-row-label { font-size: 15px; }
body.large-ui .key-badge { font-size: 12px; padding: 4px 9px; }
body.large-ui .instructions-text { font-size: 14px; }

/* ---------- Animations ---------- */

@keyframes fadeIn {
  from { opacity: 0; }
  to { opacity: 1; }
}

@keyframes panelIn {
  from { opacity: 0; transform: translateY(6px) scale(0.98); }
  to { opacity: 1; transform: translateY(0) scale(1); }
}

@keyframes toastPop {
  0% { transform: translateX(-50%) scale(0.85); }
  60% { transform: translateX(-50%) scale(1.06); }
  100% { transform: translateX(-50%) scale(1); }
}

/* ---------- Responsive ---------- */

@media (max-width: 480px) {
  .panel {
    padding: 26px 20px;
  }

  .game-title {
    font-size: 26px;
  }

  .hud-center {
    max-width: 140px;
  }

  .hud-name {
    max-width: 26vw;
  }

  .settings-panel {
    max-height: 86vh;
  }

  .settings-row {
    flex-wrap: wrap;
  }

  .settings-control {
    width: 100%;
    justify-content: space-between;
  }
}

@media (max-width: 360px) {
  .hud {
    padding: 8px 10px;
  }

  .hud-floor {
    display: none;
  }

  .watermark span {
    display: none;
  }
}

@media (prefers-reduced-motion: reduce) {
  * {
    animation-duration: 0.001ms !important;
    transition-duration: 0.001ms !important;
  }
}
