:root {
  color-scheme: dark;
  font-family: "Trebuchet MS", "Segoe UI", system-ui, sans-serif;
  background: #0d0819;
  color: #f2ecff;
  --gold: #ffd166;
  --cyan: #5ee7ff;
  --purple: #8b5cf6;
  --panel: #1c1533;
  --edge: #3d2f66;
}

* { box-sizing: border-box; }

html, body { margin: 0; }

body {
  min-height: 100dvh;
  display: flex;
  flex-direction: column;
  align-items: center;
  padding: 10px max(10px, env(safe-area-inset-right)) 28px max(10px, env(safe-area-inset-left));
  background:
    radial-gradient(900px 500px at 50% -10%, #34215f 0%, transparent 70%),
    radial-gradient(700px 400px at 100% 100%, #1b1240 0%, transparent 60%),
    #0d0819;
  overscroll-behavior: none;
}

header {
  width: min(100%, 900px);
  display: flex;
  align-items: center;
  justify-content: space-between;
  flex-wrap: wrap;
  gap: 8px;
  padding: 2px 2px 10px;
}

h1 {
  margin: 0;
  font-size: clamp(1.2rem, 3.4vw, 1.9rem);
  letter-spacing: .5px;
  text-shadow: 0 0 16px rgba(139, 92, 246, .85);
}

.hud {
  display: flex;
  align-items: center;
  gap: 8px;
  font-weight: bold;
  font-size: clamp(.9rem, 2.2vw, 1.1rem);
}

.pill {
  background: #241c3f;
  border: 1px solid var(--edge);
  border-radius: 999px;
  padding: 4px 12px;
  white-space: nowrap;
}

#hearts { color: #ff6b8a; letter-spacing: 2px; }
#score { color: var(--gold); }

main { width: min(100%, 900px); }

.stage {
  position: relative;
  width: 100%;
  border-radius: 18px;
  overflow: hidden;
  border: 2px solid var(--edge);
  box-shadow: 0 20px 60px rgba(0, 0, 0, .65);
}

canvas {
  display: block;
  width: 100%;
  height: auto;
  aspect-ratio: 3 / 2;
  background: #120c22;
  touch-action: none;
}

.overlay {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 14px;
  background: rgba(8, 5, 18, .76);
  backdrop-filter: blur(3px);
}

.overlay[hidden] { display: none; }

.card {
  width: min(100%, 470px);
  max-height: 100%;
  overflow: auto;
  padding: 18px 20px 20px;
  text-align: center;
  border: 2px solid #6d4fd0;
  border-radius: 16px;
  background: linear-gradient(180deg, #2a1f4c, #171129);
  box-shadow: 0 0 40px rgba(139, 92, 246, .35);
}

.card[hidden] { display: none; }

.card h2 {
  margin: 0 0 8px;
  font-size: clamp(1.15rem, 3.5vw, 1.6rem);
  color: var(--gold);
}

.card p { margin: 8px 0; line-height: 1.4; font-size: clamp(.85rem, 2.4vw, 1rem); }

.controls {
  list-style: none;
  margin: 10px 0 14px;
  padding: 0;
  font-size: clamp(.8rem, 2.2vw, .95rem);
  line-height: 1.7;
}

.controls b { color: var(--cyan); }

button {
  font: inherit;
  font-weight: bold;
  cursor: pointer;
  border: 0;
  border-radius: 12px;
  padding: 11px 20px;
  color: #1a1030;
  background: linear-gradient(180deg, #ffe08a, #ffc33d);
  box-shadow: 0 4px 0 #a97a12;
  touch-action: manipulation;
}

button:active { transform: translateY(2px); box-shadow: 0 2px 0 #a97a12; }
button:disabled { opacity: .55; cursor: default; }

#play-again { margin-top: 6px; }

.hint {
  margin: 12px 2px 0;
  text-align: center;
  font-size: clamp(.72rem, 1.9vw, .85rem);
  color: #b9a9e0;
}

.touch-controls {
  display: flex;
  margin-top: 12px;
  align-items: center;
  justify-content: center;
  gap: 12px;
  user-select: none;
  -webkit-user-select: none;
}

@media (hover: none) and (pointer: coarse) {
  .touch-controls { justify-content: space-between; }
  .joystick { display: block; }
  .hint { display: none; }
}

@media (max-width: 700px), (max-height: 560px) {
  .overlay {
    position: fixed;
    z-index: 50;
    padding: max(12px, env(safe-area-inset-top)) max(12px, env(safe-area-inset-right)) max(12px, env(safe-area-inset-bottom)) max(12px, env(safe-area-inset-left));
  }
  .card { max-height: calc(100dvh - 24px); }
}

@media (max-width: 600px) {
  .card { padding: 14px 16px 16px; }
  .card h2 { font-size: 1.05rem; margin-bottom: 4px; }
  .card p { font-size: .78rem; margin: 5px 0; line-height: 1.35; }
  .controls { font-size: .74rem; line-height: 1.45; margin: 8px 0 12px; }
  .controls li:last-child { display: none; }
  .card button { padding: 9px 16px; font-size: .88rem; }
}

.joystick {
  display: none;
  position: relative;
  width: 124px;
  height: 124px;
  flex: 0 0 auto;
  border-radius: 50%;
  border: 2px solid #59478f;
  background: radial-gradient(circle at 50% 45%, #2c2250, #171129);
  box-shadow: inset 0 0 18px rgba(139, 92, 246, .35);
  touch-action: none;
}

.stick {
  position: absolute;
  left: 50%;
  top: 50%;
  width: 54px;
  height: 54px;
  border-radius: 50%;
  background: radial-gradient(circle at 40% 35%, #a98bff, #5b3fb8);
  border: 2px solid #c9b6ff;
  transform: translate(-50%, -50%);
  pointer-events: none;
}

.spell-buttons { display: flex; gap: 10px; }

.spell-buttons button {
  width: 80px;
  height: 80px;
  padding: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 2px;
  border-radius: 50%;
  font-size: 1.55rem;
  line-height: 1;
  color: #fff;
  background: radial-gradient(circle at 40% 30%, #6f4fd6, #3a2477);
  box-shadow: 0 4px 0 #241453, 0 0 18px rgba(139, 92, 246, .45);
  border: 2px solid #8f74e8;
}

.spell-buttons button span { font-size: .56rem; letter-spacing: .3px; }

#btn-fireball {
  background: radial-gradient(circle at 40% 30%, #ff9a3d, #b83c16);
  border-color: #ffc46b;
  box-shadow: 0 4px 0 #6d210c, 0 0 18px rgba(255, 150, 60, .5);
}

#btn-lightning {
  background: radial-gradient(circle at 40% 30%, #45c8e8, #1d5b96);
  border-color: #7fe6ff;
  box-shadow: 0 4px 0 #123a63, 0 0 18px rgba(94, 231, 255, .45);
}

@media (max-width: 480px) {
  .joystick { width: 104px; height: 104px; }
  .stick { width: 48px; height: 48px; }
  .spell-buttons { gap: 8px; }
  .spell-buttons button { width: 66px; height: 66px; font-size: 1.25rem; }
  .spell-buttons button span { font-size: .48rem; }
}

@media (hover: none) and (pointer: coarse) and (orientation: landscape) {
  body { padding-top: 4px; }
  header { padding-bottom: 4px; }
  header h1 { font-size: 1rem; }
  .hud { font-size: .85rem; }
  .hint { display: none; }
  .stage { width: min(100%, calc((100dvh - 210px) * 1.5)); margin: 0 auto; }
  .touch-controls { justify-content: center; gap: 24px; margin-top: 8px; }
  .joystick { width: 92px; height: 92px; }
  .stick { width: 42px; height: 42px; }
  .spell-buttons button { width: 66px; height: 66px; font-size: 1.3rem; }
  .spell-buttons button span { font-size: .5rem; }
}

@media (max-width: 460px) {
  .card { padding: 14px; }
  .card p { font-size: .82rem; }
  .controls { line-height: 1.5; }
}
