/* ============================================================
   FABLEDEVIL — minimal yellow/orange redesign.
   ============================================================ */

:root,
[data-theme="dark"] {
  --bg: #261809;
  --bg2: #261809;
  --stage: #261809;
  --fg: #fff1c3;
  --muted: #d8b775;
  --line: rgba(255, 216, 74, 0.24);
  --panel: rgba(57, 36, 12, 0.92);
  --glass: rgba(57, 36, 12, 0.88);
  --accent: #f97316;
  --accent-press: #c2410c;
  --danger: #d72638;
  --electric: #fff1c3;
  --hot: #fb923c;
  --ground: #f97316;
  --shadow: 0 16px 34px rgba(0, 0, 0, 0.3);
}

[data-theme="light"] {
  --bg: #ffd84a;
  --bg2: #ffd84a;
  --stage: #ffd84a;
  --fg: #241507;
  --muted: #705229;
  --line: rgba(36, 21, 7, 0.16);
  --panel: rgba(255, 246, 214, 0.88);
  --glass: rgba(255, 246, 214, 0.86);
  --accent: #f97316;
  --accent-press: #c2410c;
  --danger: #d72638;
  --electric: #241507;
  --hot: #fb923c;
  --ground: #f97316;
  --shadow: 0 16px 34px rgba(107, 64, 12, 0.18);
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }

html, body {
  margin: 0;
  padding: 0;
  height: 100%;
  overflow: hidden;
  background: #000;
  font-family: "Vazirmatn", "Outfit", system-ui, -apple-system, "Segoe UI", sans-serif;
  color: var(--fg);
  transition: background 0.35s ease, color 0.35s ease;
}

body {
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: 100dvh;
  background: #000;
}
body::before {
  content: none;
}
@keyframes drift-grid {
  from { transform: translate3d(0, 0, 0) rotate(0.001deg); }
  to { transform: translate3d(42px, 24px, 0) rotate(0.001deg); }
}

#stage {
  position: relative;
  display: flex;
  align-items: center;
  justify-content: center;
  width: 100%;
  height: 100dvh;
}

canvas#game {
  display: block;
  background: var(--stage);
  border: 2px solid rgba(36, 21, 7, 0.12);
  border-radius: 10px;
  box-shadow: var(--shadow);
  image-rendering: -webkit-optimize-contrast;
  touch-action: none;
}
#stage.menu-active canvas#game,
#stage.menu-active #player-lottie,
#stage.menu-active #door-lottie { visibility: hidden; }
#player-lottie {
  position: fixed;
  z-index: 2;
  display: none;
  pointer-events: none;
  --player-facing: 1;
  transform: scaleX(var(--player-facing));
  transform-origin: center bottom;
  will-change: transform, left, top, width, height;
}
#player-lottie svg { overflow: visible !important; }
#door-lottie {
  position: fixed;
  z-index: 1;
  display: none;
  pointer-events: none;
  transform-origin: center bottom;
  will-change: left, top, width, height;
}
#door-lottie svg { overflow: visible !important; }
@media (pointer: coarse) {
  canvas#game { border-radius: 0; box-shadow: none; }
}

.hidden { display: none !important; }

/* ---------------------------------------------------------------- topbar */
#topbar {
  position: fixed;
  top: max(12px, env(safe-area-inset-top));
  right: max(12px, env(safe-area-inset-right));
  z-index: 40;
  display: flex;
  gap: 10px;
}
.ic-btn {
  width: 44px;
  height: 44px;
  display: grid;
  place-items: center;
  border: 1px solid var(--line);
  border-radius: 10px;
  background: var(--glass);
  backdrop-filter: blur(10px);
  -webkit-backdrop-filter: blur(10px);
  color: var(--fg);
  cursor: pointer;
  box-shadow: 0 8px 18px rgba(107,64,12,0.12);
  transition: transform 0.12s ease, background 0.2s ease, border-color 0.2s ease, color 0.2s ease;
}
.ic-btn:hover { border-color: var(--accent); color: var(--accent); transform: translateY(-1px); }
.ic-btn:active { transform: scale(0.9); }

/* ---------------------------------------------------------------- HUD */
#hud {
  position: absolute;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  padding: clamp(6px, 1.6vw, 10px);
  min-height: 40px;
  overflow: hidden;
  isolation: isolate;
  border: 1px solid rgba(249, 115, 22, 0.56);
  border-radius: 10px;
  background: var(--glass);
  box-shadow: 0 4px 0 rgba(194, 65, 12, 0.22), 0 14px 24px rgba(107,64,12,0.18);
  backdrop-filter: blur(10px);
  -webkit-backdrop-filter: blur(10px);
  pointer-events: none;
  font-weight: 700;
  z-index: 5;
  direction: rtl;
}
#hud::before {
  content: "";
  position: absolute;
  inset-block: 0;
  inset-inline-start: 0;
  width: 5px;
  background: var(--accent);
  z-index: 0;
}
.hud-left, .hud-right {
  position: relative;
  z-index: 1;
  flex: 0 0 auto;
  min-width: 0;
  max-width: calc(50% - 4px);
  min-height: 0;
  padding: 0;
  white-space: nowrap;
  overflow: hidden;
}
.hud-left { display: flex; align-items: center; gap: 8px; }
.stage-label {
  font-size: clamp(11px, 2vw, 13px);
  font-weight: 900;
  color: var(--fg);
}
.pill {
  display: inline-grid;
  place-items: center;
  min-width: 28px;
  height: 28px;
  padding: 0 8px;
  border-radius: 8px;
  background: var(--accent);
  color: #fff7d1;
  font-weight: 800;
  font-size: 14px;
  border: 1px solid rgba(255, 247, 209, 0.52);
  box-shadow: 0 3px 0 rgba(154, 52, 18, 0.26);
}
#hud-levelname {
  display: none;
}
.hud-right {
  display: flex;
  align-items: center;
  gap: 7px;
  padding-inline-start: 10px;
  border-inline-start: 1px solid rgba(249, 115, 22, 0.34);
  font-size: 15px;
}
.lives-label {
  font-size: clamp(11px, 2vw, 13px);
  font-weight: 900;
  color: var(--fg);
}
.hearts {
  direction: ltr;
  flex: 0 0 auto;
  min-width: 0;
  color: var(--danger);
  font-size: clamp(12px, 2.8vw, 17px);
  font-weight: 900;
  letter-spacing: 1px;
  line-height: 1;
}
.hint {
  display: none;
  margin-left: 10px;
  font-size: 11px;
  font-weight: 600;
  letter-spacing: 0.06em;
  color: var(--muted);
  text-transform: uppercase;
}

/* ---------------------------------------------------------------- overlays (menu / end) */
#menu, #end-screen {
  position: absolute;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: 10px;
  overflow: hidden;
  z-index: 10;
  background: var(--panel);
  animation: fade 0.35s ease;
}
#menu {
  width: min(92vw, 560px);
  height: auto;
  max-height: calc(100dvh - 24px);
  border: 1px solid rgba(249, 115, 22, 0.62);
  box-shadow: 0 7px 0 rgba(194, 65, 12, 0.28), 0 24px 48px rgba(75, 40, 5, 0.24);
}
@media (pointer: coarse) { #menu, #end-screen { border-radius: 0; } }
@keyframes fade { from { opacity: 0; } to { opacity: 1; } }

.menu-inner {
  position: relative;
  width: 100%;
  height: 100%;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: clamp(10px, 2.2vh, 18px);
  padding: clamp(22px, 4vh, 38px) clamp(18px, 5vw, 52px);
  text-align: center;
  overflow-y: auto;
  overflow-x: hidden;
  overscroll-behavior: contain;
}
#menu .menu-inner {
  height: auto;
  max-height: calc(100dvh - 40px);
}
#menu .menu-inner::before { display: none; }
.menu-inner > * {
  position: relative;
  z-index: 1;
  max-width: 100%;
}
.menu-inner::before {
  content: "";
  position: absolute;
  inset: 8px;
  border: 1px solid var(--line);
  border-radius: 10px;
  pointer-events: none;
  box-shadow: none;
}
.menu-kicker {
  margin: 0 0 -4px;
  color: var(--accent);
  font-size: clamp(11px, 2.5vw, 13px);
  font-weight: 900;
  letter-spacing: 0;
}
.menu-hero {
  display: block;
  width: clamp(220px, 42vh, 340px);
  max-height: 44dvh;
  object-fit: contain;
  margin: 0;
  filter: drop-shadow(0 12px 16px rgba(107, 64, 12, 0.2));
}

/* devil face */
.devil-face {
  position: relative;
  flex: 0 0 auto;
  width: clamp(58px, 11vh, 88px);
  height: clamp(50px, 9vh, 72px);
  margin-bottom: 2px;
  animation: bob 2.1s ease-in-out infinite;
  filter: none;
}
@keyframes bob { 0%, 100% { transform: translateY(0) rotate(-2deg) scale(1); } 50% { transform: translateY(-9px) rotate(2deg) scale(1.04); } }
.face {
  position: absolute;
  bottom: 0;
  left: 10%;
  width: 80%;
  height: 84%;
  background: var(--accent);
  border-radius: 50% 50% 46% 46%;
}
.horn {
  position: absolute;
  top: -10%;
  width: 0; height: 0;
  border-left: 11px solid transparent;
  border-right: 11px solid transparent;
  border-bottom: 24px solid var(--accent);
}
.horn.left { left: 7%; transform: rotate(-22deg); }
.horn.right { right: 7%; transform: rotate(22deg); }
.eye {
  position: absolute;
  top: 37%;
  width: 11px; height: 14px;
  background: #14110b;
  border-radius: 50%;
}
.eye.l { left: 26%; }
.eye.r { right: 26%; }
.grin {
  position: absolute;
  bottom: 21%;
  left: 50%;
  transform: translateX(-50%);
  width: 34px; height: 17px;
  border-bottom: 4px solid #14110b;
  border-radius: 0 0 18px 18px;
}

.subtitle {
  margin: 0 0 4px;
  color: var(--fg);
  font-size: clamp(14px, 3.2vw, 17px);
  font-weight: 700;
  line-height: 1.5;
  width: min(100%, 500px);
  text-shadow: none;
  overflow-wrap: anywhere;
}
.wink { color: var(--accent); font-weight: 800; }

.game-guide {
  width: min(100%, 440px);
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 0;
  border-block: 1px solid rgba(249, 115, 22, 0.34);
  padding-block: 10px;
}
.guide-item {
  min-width: 0;
  display: grid;
  justify-items: center;
  gap: 6px;
  padding-inline: 7px;
  color: var(--muted);
  font-size: clamp(10px, 2.5vw, 12px);
  font-weight: 800;
}
.guide-item + .guide-item { border-inline-start: 1px solid rgba(249, 115, 22, 0.24); }
.guide-item strong {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 4px;
  min-height: 26px;
  color: var(--fg);
}
.guide-item b {
  display: inline-grid;
  place-items: center;
  min-width: 25px;
  height: 24px;
  padding-inline: 6px;
  border: 1px solid rgba(249, 115, 22, 0.42);
  border-radius: 6px;
  background: rgba(249, 115, 22, 0.12);
  color: var(--fg);
  font-size: 12px;
  line-height: 1;
}

button#play-btn, button#end-menu-btn {
  font-family: inherit;
  font-size: clamp(17px, 4vw, 20px);
  font-weight: 800;
  letter-spacing: 0;
  color: #16110a;
  background: var(--accent);
  border: none;
  border-radius: 10px;
  min-width: min(240px, 100%);
  max-width: 100%;
  padding: 14px clamp(26px, 7vw, 54px);
  cursor: pointer;
  box-shadow: 0 6px 0 var(--accent-press);
  transition: transform 0.1s ease, box-shadow 0.1s ease, filter 0.2s ease;
}
button#play-btn:hover, button#end-menu-btn:hover { filter: brightness(1.08) saturate(1.15); transform: translateY(-2px); }
button#play-btn:active, button#end-menu-btn:active {
  transform: translateY(6px);
  box-shadow: 0 2px 0 var(--accent-press);
}
.install-app {
  min-width: min(240px, 100%);
  max-width: 100%;
  min-height: 42px;
  padding: 9px 22px;
  border: 1px solid rgba(249, 115, 22, 0.56);
  border-radius: 8px;
  background: var(--glass);
  color: var(--fg);
  font: inherit;
  font-size: 14px;
  font-weight: 900;
  cursor: pointer;
  transition: transform 0.12s ease, background 0.2s ease, border-color 0.2s ease;
}
.install-app:hover { background: rgba(249, 115, 22, 0.16); border-color: var(--accent); transform: translateY(-1px); }
.install-app:active { transform: translateY(1px); }

.grid-label,
#level-grid {
  display: none !important;
}

.grid-label {
  margin: 4px 0 0;
  font-size: 11px;
  letter-spacing: 0;
  text-transform: uppercase;
  color: var(--muted);
}

/* level grid (handles 30+ levels) */
#level-grid {
  display: grid;
  grid-template-columns: repeat(10, minmax(0, 1fr));
  gap: clamp(4px, 1vw, 7px);
  width: min(420px, 100%);
}
#level-grid button {
  aspect-ratio: 1;
  display: grid;
  place-items: center;
  font-family: inherit;
  font-size: 14px;
  font-weight: 700;
  color: var(--fg);
  background: linear-gradient(135deg, var(--panel), rgba(22, 24, 43, 0.88));
  border: 1px solid var(--line);
  border-radius: 12px;
  cursor: pointer;
  transition: transform 0.1s ease, border-color 0.15s ease, background 0.15s ease;
}
#level-grid button:hover:not(:disabled) { border-color: var(--electric); color: var(--electric); transform: translateY(-3px) scale(1.03); }
#level-grid button:disabled { opacity: 0.32; cursor: not-allowed; }
#level-grid button.done {
  background: linear-gradient(135deg, var(--accent), var(--hot));
  border-color: var(--accent);
  color: #16110a;
}

.menu-deaths { margin: 4px 0 0; color: var(--fg); font-size: 13px; font-weight: 800; }
.controls-hint {
  width: min(100%, 500px);
  margin: 0;
  color: var(--muted);
  font-size: clamp(12px, 3vw, 14px);
  line-height: 1.65;
  opacity: 0.88;
  overflow-wrap: anywhere;
}

/* end screen */
.end-title {
  font-size: clamp(40px, 9vw, 80px);
  font-weight: 900;
  line-height: 0.9;
  margin: 0 0 10px;
  color: var(--accent);
  text-shadow: 0 0 28px rgba(255, 45, 102, 0.42);
}
.end-sub { color: var(--muted); font-size: 18px; margin: 0 0 18px; }
.end-stats { color: var(--fg); font-size: 15px; max-width: 420px; margin: 0 0 26px; line-height: 1.5; }

/* ---------------------------------------------------------------- touch controls (float over canvas) */
#touch-controls {
  position: fixed;
  inset: 0;
  z-index: 30;
  pointer-events: none;
  display: flex;
  align-items: flex-end;
  justify-content: space-between;
  padding: 0 max(20px, env(safe-area-inset-left)) max(24px, env(safe-area-inset-bottom));
}
.tc-group { display: flex; align-items: flex-end; gap: 16px; pointer-events: none; }
.tc-actions { align-items: center; }
.tc-btn {
  pointer-events: auto;
  display: grid;
  place-items: center;
  width: 66px; height: 66px;
  border-radius: 18px;
  border: 1.5px solid var(--line);
  background: var(--glass);
  backdrop-filter: blur(8px);
  -webkit-backdrop-filter: blur(8px);
  color: var(--fg);
  touch-action: none;
  user-select: none;
  box-shadow: 0 14px 34px rgba(0,0,0,0.24);
  transition: transform 0.08s ease, background 0.12s ease, color 0.12s ease, border-color 0.12s ease;
}
.tc-btn.held, .tc-btn:active {
  background: linear-gradient(135deg, var(--accent), var(--hot));
  color: #16110a;
  border-color: var(--accent);
  transform: scale(0.93) translateY(3px);
}
.tc-jump { width: 82px; height: 82px; }
.tc-small { width: 50px; height: 50px; opacity: 0.85; }

/* rotate hint */
#rotate-hint {
  position: fixed;
  bottom: 96px;
  left: 50%;
  transform: translateX(-50%);
  z-index: 35;
  padding: 8px 16px;
  font-size: 12px;
  font-weight: 600;
  letter-spacing: 0;
  color: var(--fg);
  background: var(--glass);
  border: 1px solid var(--line);
  border-radius: 999px;
  backdrop-filter: blur(8px);
  -webkit-backdrop-filter: blur(8px);
  display: none;
  pointer-events: none;
  animation: pulse 2.4s ease-in-out infinite;
}
#rotate-hint.show { display: block; }
@keyframes pulse { 0%, 100% { opacity: 0.5; } 50% { opacity: 1; } }

/* small screens: tighten the level grid */
@media (max-width: 540px) {
  #topbar {
    top: max(8px, env(safe-area-inset-top));
    right: max(8px, env(safe-area-inset-right));
    gap: 8px;
  }
  .ic-btn {
    width: 38px;
    height: 38px;
    border-radius: 9px;
  }
  #hud {
    min-height: 34px;
    padding: 5px 8px;
    border-radius: 9px;
  }
  .hud-left, .hud-right {
    min-height: 0;
    padding: 0;
  }
  .lives-label { display: none; }
  .hearts { font-size: 13px; letter-spacing: 0; }
  #menu {
    width: min(calc(100vw - 24px), 560px) !important;
    height: auto !important;
    max-height: calc(100dvh - 24px) !important;
    left: 50% !important;
    top: 50% !important;
    transform: translate(-50%, -50%) !important;
    border-radius: 14px;
  }
  .menu-inner {
    justify-content: center;
    gap: 14px;
    padding: 24px 20px 28px;
  }
  #menu .menu-inner { height: auto; max-height: calc(100dvh - 40px); }
  .menu-inner::before { inset: 8px; }
  #level-grid { grid-template-columns: repeat(6, minmax(0, 1fr)); width: min(330px, 100%); gap: 7px; }
  .devil-face { transform: none; width: 72px; height: 62px; }
  .subtitle { font-size: 15px; line-height: 1.65; }
  .menu-hero { width: clamp(190px, 62vw, 280px); max-height: 38dvh; }
  .menu-kicker { font-size: 12px; }
  .game-guide { padding-block: 8px; }
  .guide-item { padding-inline: 4px; font-size: 10px; }
  .guide-item b { min-width: 23px; height: 23px; padding-inline: 4px; font-size: 11px; }
  .menu-deaths { font-size: 14px; }
  .controls-hint { font-size: 13px; line-height: 1.7; }
}
@media (max-height: 680px) {
  .menu-inner {
    gap: 10px;
    padding-top: 18px;
    padding-bottom: 18px;
  }
  .subtitle { line-height: 1.5; }
  button#play-btn, button#end-menu-btn { padding-block: 12px; }
  #level-grid { width: min(370px, 100%); }
  #level-grid button { font-size: 12px; border-radius: 9px; }
  .controls-hint { line-height: 1.5; }
}
@media (max-height: 560px) and (pointer: coarse) {
  .grid-label, #level-grid { display: none; }
  #menu {
    height: auto !important;
    max-height: calc(100dvh - 24px) !important;
    top: 50% !important;
  }
  .menu-inner { justify-content: center; padding-top: 18px; padding-bottom: 18px; }
  .subtitle { margin-bottom: 2px; line-height: 1.45; }
  .controls-hint { font-size: 12px; line-height: 1.5; }
}
@media (max-height: 430px) {
  .menu-hero { width: 140px; max-height: 30dvh; }
  .devil-face { width: 44px; height: 38px; }
  .subtitle { font-size: 12px; line-height: 1.35; }
  .controls-hint { font-size: 11px; }
}
@media (max-height: 500px) {
  .grid-label, #level-grid { display: none; }
}
