:root {
  --bg: #0b0a12;
  --panel: #16131f;
  --ink: #f4f1ff;
  --dim: #9a93b3;
  --accent: #46e07c;
  --scale: 1;
  --safe-t: env(safe-area-inset-top, 0px);
  --safe-r: env(safe-area-inset-right, 0px);
  --safe-b: env(safe-area-inset-bottom, 0px);
  --safe-l: env(safe-area-inset-left, 0px);
  --vh: 100vh;
  color-scheme: dark;
}

@supports (height: 100dvh) {
  :root {
    --vh: 100dvh;
  }
}

* {
  box-sizing: border-box;
}

html,
body {
  margin: 0;
  height: 100%;
  background: var(--bg);
  color: var(--ink);
  font: 600 15px/1.35 system-ui, -apple-system, 'Segoe UI', Roboto, sans-serif;
  overflow: hidden;
  overscroll-behavior: none;
  touch-action: none;
  -webkit-user-select: none;
  user-select: none;
  -webkit-touch-callout: none;
  -webkit-tap-highlight-color: transparent;
}

button,
select,
input {
  font: inherit;
  color: inherit;
}

/* ---------- layout ---------- */

#app {
  position: fixed;
  inset: 0;
  overflow: hidden;
}

#stage {
  position: absolute;
  background: #000;
}

#canvas {
  display: block;
  width: 100%;
  height: 100%;
  outline: none;
  image-rendering: pixelated;
}

.side {
  position: absolute;
  display: flex;
  flex-direction: column;
  z-index: 2;
}

.side-bar {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  padding: 8px;
  flex: none;
}

#side-right .side-bar {
  justify-content: flex-end;
}

.zone {
  position: relative;
  flex: 1;
  min-height: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  touch-action: none;
}

/* Landscape: the picture fills the height, controls on the sides (over the picture's edges on narrow phones) */
@media (orientation: landscape) {
  #app {
    --stage-w: min(100vw, calc(var(--vh) * 4 / 3));
    --side-w: max(calc((100vw - var(--stage-w)) / 2), 158px);
    --btn: clamp(40px, min(calc(64px * var(--scale)), calc((var(--side-w) - 40px) / 3)), 96px);
    --stick: min(calc(var(--side-w) - 24px), calc(var(--btn) * 2.7), 220px);
  }

  #stage {
    top: 0;
    bottom: 0;
    left: 50%;
    width: var(--stage-w);
    transform: translateX(-50%);
  }

  .side {
    top: 0;
    bottom: 0;
    width: var(--side-w);
  }

  #side-left {
    left: 0;
    padding-left: var(--safe-l);
  }

  #side-right {
    right: 0;
    padding-right: var(--safe-r);
  }

  .zone {
    padding-bottom: calc(var(--safe-b) + 10px);
    align-items: flex-end;
  }

  .zone > * {
    margin-bottom: 6%;
  }
}

/* Portrait: picture on top, controls below */
@media (orientation: portrait) {
  #app {
    --stage-h: min(75vw, calc(var(--vh) * 0.48));
    --left-w: 44vw;
    --right-w: 56vw;
    --btn: clamp(44px, min(calc(64px * var(--scale)), calc((var(--right-w) - 24px) / 3.32)), 100px);
    --stick: min(calc(var(--btn) * 2.75), calc(var(--left-w) - 20px));
  }

  #stage {
    top: var(--safe-t);
    left: 50%;
    height: var(--stage-h);
    width: calc(var(--stage-h) * 4 / 3);
    transform: translateX(-50%);
  }

  .side {
    top: calc(var(--safe-t) + var(--stage-h));
    bottom: 0;
    padding-bottom: calc(var(--safe-b) + 8px);
  }

  #side-left {
    left: 0;
    width: var(--left-w);
    padding-left: var(--safe-l);
  }

  #side-right {
    right: 0;
    width: var(--right-w);
    padding-right: var(--safe-r);
  }

  .zone {
    align-items: flex-end;
    padding-bottom: 9vh;
  }
}

/* No touch controls (desktop, or turned off): the picture is as big as it gets */
#app.no-controls .side {
  display: none;
}

#app.no-controls #stage {
  top: 50%;
  left: 50%;
  width: min(100vw, calc(var(--vh) * 4 / 3));
  height: min(var(--vh), 75vw);
  transform: translate(-50%, -50%);
}

/* ---------- stick ---------- */

#stick {
  position: relative;
  width: var(--stick);
  height: var(--stick);
  border-radius: 50%;
  background: radial-gradient(circle at 50% 50%, rgba(255, 255, 255, 0.06) 0 38%, rgba(255, 255, 255, 0.1) 39% 100%);
  border: 2px solid rgba(255, 255, 255, 0.18);
  pointer-events: none;
}

#stick-knob {
  position: absolute;
  left: 50%;
  top: 50%;
  width: 42%;
  height: 42%;
  border-radius: 50%;
  transform: translate(-50%, -50%);
  background: radial-gradient(circle at 40% 35%, #ff6b6b, #c41d2d 70%);
  box-shadow: 0 4px 10px rgba(0, 0, 0, 0.5), inset 0 -3px 0 rgba(0, 0, 0, 0.25);
}

.arrow {
  position: absolute;
  width: 0;
  height: 0;
  border: 8px solid transparent;
  opacity: 0.45;
}

.arrow.up {
  left: 50%;
  top: 6px;
  transform: translateX(-50%);
  border-bottom-color: var(--ink);
  border-top-width: 0;
}

.arrow.down {
  left: 50%;
  bottom: 6px;
  transform: translateX(-50%);
  border-top-color: var(--ink);
  border-bottom-width: 0;
}

.arrow.left {
  top: 50%;
  left: 6px;
  transform: translateY(-50%);
  border-right-color: var(--ink);
  border-left-width: 0;
}

.arrow.right {
  top: 50%;
  right: 6px;
  transform: translateY(-50%);
  border-left-color: var(--ink);
  border-right-width: 0;
}

#stick[data-dir='1'] .arrow.up,
#stick[data-dir='9'] .arrow.up,
#stick[data-dir='5'] .arrow.up,
#stick[data-dir='2'] .arrow.down,
#stick[data-dir='6'] .arrow.down,
#stick[data-dir='10'] .arrow.down,
#stick[data-dir='4'] .arrow.left,
#stick[data-dir='5'] .arrow.left,
#stick[data-dir='6'] .arrow.left,
#stick[data-dir='8'] .arrow.right,
#stick[data-dir='9'] .arrow.right,
#stick[data-dir='10'] .arrow.right {
  opacity: 1;
}

/* ---------- attack buttons ---------- */

.buttons {
  display: grid;
  grid-template-columns: repeat(3, var(--btn));
  gap: calc(var(--btn) * 0.16);
  pointer-events: none;
}

.btn {
  width: var(--btn);
  height: var(--btn);
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: calc(var(--btn) * 0.27);
  font-weight: 800;
  letter-spacing: 0.02em;
  color: rgba(255, 255, 255, 0.92);
  text-shadow: 0 1px 2px rgba(0, 0, 0, 0.6);
  border: 2px solid rgba(255, 255, 255, 0.25);
  box-shadow: 0 4px 0 rgba(0, 0, 0, 0.45);
  transition: transform 40ms linear, filter 40ms linear;
  opacity: 0.9;
}

.btn.l {
  background: radial-gradient(circle at 40% 35%, #8fd2ff, #2b7fd0 75%);
}

.btn.m {
  background: radial-gradient(circle at 40% 35%, #ffe680, #d8a400 75%);
}

.btn.h {
  background: radial-gradient(circle at 40% 35%, #ff8a8a, #c62828 75%);
}

.btn.down,
.chip.down {
  transform: translateY(3px) scale(0.94);
  filter: brightness(1.35);
  box-shadow: 0 1px 0 rgba(0, 0, 0, 0.45);
}

/* ---------- small buttons ---------- */

.chip {
  min-width: 44px;
  min-height: 44px;
  padding: 0 12px;
  border-radius: 12px;
  border: 1px solid rgba(255, 255, 255, 0.18);
  background: rgba(30, 26, 44, 0.78);
  font-size: 13px;
  letter-spacing: 0.06em;
  touch-action: none;
}

.chip.start {
  background: rgba(70, 224, 124, 0.22);
  border-color: rgba(70, 224, 124, 0.55);
}

.chip.wide {
  flex: 1;
}

/* ---------- loader ---------- */

#loader {
  position: fixed;
  inset: 0;
  z-index: 10;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 16px;
  background: radial-gradient(ellipse at 50% 30%, #241c3a, var(--bg) 70%);
}

#loader.hidden {
  display: none;
}

.loader-card {
  width: min(420px, 100%);
  text-align: center;
}

.loader-icon {
  border-radius: 22px;
  box-shadow: 0 8px 30px rgba(0, 0, 0, 0.5);
}

h1 {
  margin: 14px 0 6px;
  font-size: 30px;
  font-weight: 900;
  letter-spacing: 0.08em;
  font-style: italic;
  background: linear-gradient(180deg, #fff 30%, #ffb347 60%, #ff4d4d);
  -webkit-background-clip: text;
  background-clip: text;
  color: transparent;
}

#status {
  margin: 8px 0;
  min-height: 1.4em;
}

.bar {
  height: 12px;
  border-radius: 6px;
  background: rgba(255, 255, 255, 0.1);
  overflow: hidden;
}

#bar-fill {
  height: 100%;
  width: 0;
  background: linear-gradient(90deg, #46e07c, #b9ff6b);
  transition: width 120ms linear;
}

.detail {
  color: var(--dim);
  font-size: 13px;
  font-weight: 500;
  min-height: 1.3em;
}

.hint {
  color: var(--dim);
  font-size: 12px;
  font-weight: 500;
}

.big {
  margin-top: 14px;
  min-height: 56px;
  width: 100%;
  border: 0;
  border-radius: 14px;
  background: linear-gradient(180deg, #ff6b4a, #d62e2e);
  font-size: 20px;
  font-weight: 900;
  letter-spacing: 0.1em;
  box-shadow: 0 6px 0 #7d1414;
}

.big:active {
  transform: translateY(4px);
  box-shadow: 0 2px 0 #7d1414;
}

/* ---------- settings ---------- */

#settings {
  position: fixed;
  inset: 0;
  z-index: 20;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 16px;
  background: rgba(0, 0, 0, 0.6);
  touch-action: auto;
}

#settings[hidden] {
  display: none;
}

.sheet {
  width: min(440px, 100%);
  max-height: 100%;
  overflow: auto;
  padding: 16px;
  border-radius: 16px;
  background: var(--panel);
  border: 1px solid rgba(255, 255, 255, 0.1);
}

.sheet h2 {
  margin: 0 0 8px;
  font-size: 18px;
}

.row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  min-height: 48px;
}

.row select,
.row input[type='range'] {
  min-height: 40px;
}

.row input[type='checkbox'] {
  width: 26px;
  height: 26px;
}

.keys {
  margin: 8px 0;
  color: var(--dim);
  font-size: 13px;
  font-weight: 500;
}

/* ---------- intro video ---------- */

#intro {
  position: fixed;
  inset: 0;
  z-index: 30;
  background: #000;
  display: flex;
  align-items: center;
  justify-content: center;
}

#intro[hidden] {
  display: none;
}

#intro-video {
  width: 100%;
  height: 100%;
  object-fit: contain;
  background: #000;
}

#intro-skip {
  position: absolute;
  right: calc(var(--safe-r) + 12px);
  bottom: calc(var(--safe-b) + 12px);
  min-width: 88px;
}

#intro-hint {
  position: absolute;
  left: 50%;
  bottom: calc(var(--safe-b) + 20px);
  transform: translateX(-50%);
  padding: 10px 16px;
  border-radius: 999px;
  background: rgba(0, 0, 0, 0.7);
  border: 1px solid rgba(255, 255, 255, 0.3);
  font-size: 14px;
  pointer-events: none;
  animation: pulse 1.6s ease-in-out infinite;
}

@keyframes pulse {
  50% {
    opacity: 0.55;
  }
}

@media (prefers-reduced-motion: reduce) {
  #intro-hint {
    animation: none;
  }
}

#loader.private #status {
  color: #ffb347;
  font-size: 18px;
}
