:root {
  --bg: #1a1428;
  --panel: rgba(28, 20, 44, 0.94);
  --text: #fff8ef;
  --muted: #c9b8e0;
  --kiko: #c49a6c;
  --edward: #f0a05a;
  --accent: #ffd56a;
  --line: rgba(255, 248, 239, 0.12);
  --shadow: 0 18px 50px rgba(0, 0, 0, 0.45);
  --radius: 18px;
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  --safe-left: env(safe-area-inset-left, 0px);
  --safe-right: env(safe-area-inset-right, 0px);
  font-family: "Avenir Next", "Segoe UI", system-ui, -apple-system, sans-serif;
}

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

html,
body {
  margin: 0;
  min-height: 100%;
  min-height: 100dvh;
  background:
    radial-gradient(1200px 600px at 20% -10%, #4a2f78 0%, transparent 55%),
    radial-gradient(900px 500px at 90% 10%, #8a4b2a 0%, transparent 45%),
    var(--bg);
  color: var(--text);
  overscroll-behavior: none;
  touch-action: manipulation;
}

body {
  min-height: 100dvh;
}

#app {
  width: min(100%, 1200px);
  margin: 0 auto;
  padding:
    calc(12px + var(--safe-top))
    calc(14px + var(--safe-right))
    calc(14px + var(--safe-bottom))
    calc(14px + var(--safe-left));
  display: flex;
  flex-direction: column;
  gap: 12px;
  min-height: 100dvh;
}

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  flex-wrap: wrap;
  flex-shrink: 0;
}

.brand {
  display: flex;
  align-items: center;
  gap: 10px;
}

.brand-mark {
  display: inline-grid;
  place-items: center;
  width: 46px;
  height: 46px;
  border-radius: 14px;
  background: linear-gradient(145deg, #ffd56a, #c49a6c);
  font-size: 1.35rem;
  box-shadow: 0 8px 20px rgba(196, 154, 108, 0.35);
}

.brand-text {
  display: grid;
  gap: 1px;
}

.brand-title {
  font-weight: 800;
  letter-spacing: 0.01em;
  font-size: 1.05rem;
}

.brand-sub {
  font-size: 0.78rem;
  color: var(--muted);
}

.hud {
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
}

.hud-chip {
  background: rgba(255, 255, 255, 0.08);
  border: 1px solid var(--line);
  border-radius: 999px;
  padding: 8px 14px;
  font-size: 0.95rem;
  font-weight: 600;
}

.hud-chip.active {
  background: linear-gradient(135deg, rgba(196, 154, 108, 0.3), rgba(240, 160, 90, 0.2));
  border-color: rgba(255, 213, 106, 0.4);
}

main {
  flex: 1;
  display: grid;
  min-height: 0;
}

.screen {
  display: grid;
  gap: 14px;
  min-height: 0;
}

.hidden {
  display: none !important;
}

.banner {
  width: 100%;
  height: auto;
  max-height: min(42vh, 420px);
  object-fit: cover;
  border-radius: calc(var(--radius) + 4px);
  box-shadow: var(--shadow);
  border: 1px solid rgba(255, 255, 255, 0.08);
  display: block;
}

.panel {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 20px 22px 22px;
  box-shadow: var(--shadow);
  backdrop-filter: blur(10px);
}

.panel h1 {
  margin: 0 0 8px;
  font-size: clamp(1.7rem, 3.5vw, 2.3rem);
  letter-spacing: -0.02em;
}

.tagline {
  margin: 0 0 14px;
  color: var(--muted);
  line-height: 1.5;
  max-width: 52ch;
  font-size: 1.02rem;
}

.tagline strong {
  color: var(--text);
}

.how {
  list-style: none;
  margin: 0 0 14px;
  padding: 0;
  display: grid;
  gap: 8px;
  color: var(--muted);
  font-size: 0.98rem;
}

.how li {
  display: flex;
  align-items: center;
  gap: 10px;
  flex-wrap: wrap;
}

kbd {
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
  font-size: 0.82rem;
  background: rgba(255, 255, 255, 0.08);
  border: 1px solid rgba(255, 255, 255, 0.14);
  border-bottom-width: 2px;
  border-radius: 8px;
  padding: 3px 8px;
  color: var(--text);
}

.abilities {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(220px, 1fr));
  gap: 10px;
  margin-bottom: 16px;
}

.ability {
  border-radius: 14px;
  padding: 12px 14px;
  border: 1px solid var(--line);
  display: grid;
  gap: 4px;
  background: rgba(255, 255, 255, 0.04);
}

.ability strong {
  font-size: 1rem;
}

.ability span {
  color: var(--muted);
  font-size: 0.95rem;
  line-height: 1.35;
}

.ability.kiko {
  border-color: rgba(196, 154, 108, 0.55);
}

.ability.edward {
  border-color: rgba(240, 160, 90, 0.45);
  opacity: 0.95;
}

.btn {
  appearance: none;
  border: none;
  cursor: pointer;
  font: inherit;
  font-weight: 700;
  border-radius: 999px;
  padding: 14px 26px;
  transition: transform 0.12s ease, box-shadow 0.12s ease;
  min-height: 48px;
}

.btn:hover {
  transform: translateY(-1px);
}

.btn:active {
  transform: translateY(1px);
}

.btn.primary {
  background: linear-gradient(135deg, #ffd56a, #c49a6c);
  color: #2a1a08;
  box-shadow: 0 10px 24px rgba(196, 154, 108, 0.35);
  font-size: 1.05rem;
}

.btn.ghost {
  background: transparent;
  color: var(--text);
  border: 1px solid var(--line);
}

.btn-row {
  display: flex;
  gap: 10px;
  flex-wrap: wrap;
  margin-top: 8px;
  justify-content: center;
}

.install-hint {
  margin: 14px 0 0;
  color: var(--muted);
  font-size: 0.9rem;
  line-height: 1.4;
}

#game-screen {
  grid-template-rows: 1fr auto;
  height: 100%;
  align-content: stretch;
}

.stage {
  position: relative;
  display: grid;
  place-items: center;
  min-height: 0;
  width: 100%;
}

#game {
  width: 100%;
  max-width: 1100px;
  height: auto;
  max-height: min(58dvh, 620px);
  background: #0f0b18;
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  border: 1px solid rgba(255, 255, 255, 0.08);
  image-rendering: pixelated;
  touch-action: none;
  display: block;
}

.toast {
  position: absolute;
  left: 50%;
  bottom: 14px;
  transform: translateX(-50%);
  background: rgba(18, 12, 28, 0.92);
  border: 1px solid rgba(255, 213, 106, 0.35);
  color: var(--text);
  padding: 10px 16px;
  border-radius: 999px;
  font-weight: 600;
  box-shadow: var(--shadow);
  pointer-events: none;
  max-width: min(92%, 560px);
  text-align: center;
  font-size: 0.95rem;
}

/* Touch controls — always visible on coarse pointers / tablets */
.touch-controls {
  display: none;
  justify-content: space-between;
  align-items: flex-end;
  gap: 16px;
  padding: 8px 4px 0;
  user-select: none;
  flex-shrink: 0;
}

.dpad {
  display: grid;
  grid-template-columns: repeat(3, 64px);
  grid-template-rows: repeat(3, 64px);
  gap: 6px;
  grid-template-areas:
    ". up ."
    "left center right"
    ". down .";
}

.pad-up { grid-area: up; }
.pad-left { grid-area: left; }
.pad-center { grid-area: center; visibility: hidden; }
.pad-right { grid-area: right; }
.pad-down { grid-area: down; }

.action-pad {
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.pad-btn {
  appearance: none;
  border: 1px solid rgba(255, 255, 255, 0.16);
  background: rgba(255, 255, 255, 0.1);
  color: var(--text);
  border-radius: 18px;
  font: inherit;
  font-weight: 800;
  font-size: 1.15rem;
  cursor: pointer;
  box-shadow: 0 6px 0 rgba(0, 0, 0, 0.25);
  touch-action: manipulation;
}

.pad-btn:active,
.pad-btn.is-pressed {
  transform: translateY(3px);
  box-shadow: 0 2px 0 rgba(0, 0, 0, 0.25);
  background: rgba(255, 213, 106, 0.22);
}

.pad-btn.action {
  min-width: 108px;
  min-height: 72px;
  display: grid;
  place-items: center;
  gap: 2px;
  padding: 10px 14px;
  background: linear-gradient(145deg, rgba(240, 160, 90, 0.35), rgba(196, 154, 108, 0.25));
  border-color: rgba(240, 160, 90, 0.5);
}

.pad-btn.action.ghost {
  background: rgba(255, 255, 255, 0.08);
  border-color: var(--line);
  min-height: 56px;
}

.action-icon {
  font-size: 1.35rem;
  line-height: 1;
}

.action-label {
  font-size: 0.78rem;
  font-weight: 700;
  letter-spacing: 0.02em;
  opacity: 0.9;
}

.panel.win {
  text-align: center;
  max-width: 520px;
  margin: 40px auto;
}

.panel.win .score {
  color: var(--accent);
  font-weight: 700;
  font-size: 1.15rem;
}

footer {
  color: var(--muted);
  font-size: 0.85rem;
  text-align: center;
  opacity: 0.8;
  flex-shrink: 0;
}

/* Tablets & touch devices */
@media (pointer: coarse), (max-width: 1024px) {
  .touch-controls {
    display: flex;
  }

  #game {
    max-height: min(48dvh, 560px);
  }

  .banner {
    max-height: min(36vh, 360px);
  }
}

/* iPad landscape / large tablet */
@media (min-width: 768px) and (pointer: coarse) {
  #app {
    width: min(100%, 1180px);
    padding-left: calc(20px + var(--safe-left));
    padding-right: calc(20px + var(--safe-right));
  }

  .dpad {
    grid-template-columns: repeat(3, 76px);
    grid-template-rows: repeat(3, 76px);
  }

  .pad-btn.action {
    min-width: 124px;
    min-height: 84px;
  }

  #game {
    max-height: min(56dvh, 640px);
  }

  .brand-title {
    font-size: 1.2rem;
  }

  .hud-chip {
    font-size: 1rem;
    padding: 10px 16px;
  }
}

/* iPad portrait */
@media (min-width: 768px) and (max-width: 1100px) and (orientation: portrait) {
  #game {
    max-height: min(42dvh, 520px);
  }

  .banner {
    max-height: min(28vh, 300px);
  }
}

/* Standalone PWA (home screen) */
@media (display-mode: standalone) {
  #app {
    padding-top: calc(8px + var(--safe-top));
  }

  .install-hint {
    display: none;
  }
}

/* Desktop: hide big touch pad unless forced */
@media (pointer: fine) and (min-width: 1025px) {
  .touch-controls {
    display: none;
  }

  #game {
    max-height: min(62dvh, 620px);
  }
}

/* Optional: show touch controls on desktop with mouse for testing via body class */
body.force-touch .touch-controls {
  display: flex;
}

@media (max-width: 640px) {
  #app {
    padding: calc(10px + var(--safe-top)) 10px calc(12px + var(--safe-bottom));
  }

  .panel {
    padding: 16px;
  }

  .dpad {
    grid-template-columns: repeat(3, 58px);
    grid-template-rows: repeat(3, 58px);
  }
}
