:root {
  color-scheme: dark;
  --bg: #121620;
  --panel: #202737;
  --line: #394357;
  --text: #edf0f5;
  --muted: #aab2c4;
  --accent: #72b7ff;
  --danger: #f17373;
  --ui-gap: 8px;
  --joy-size: clamp(120px, 21vh, 170px);
  --game-max: min(95vw, 800px, calc(100dvh - 250px));
}

* {
  box-sizing: border-box;
}

html,
body {
  margin: 0;
  min-height: 100%;
  width: 100%;
  height: 100%;
  background: radial-gradient(circle at top, #1e2738 0%, #111621 45%, #0d111a 100%);
  color: var(--text);
  font-family: "Noto Sans", "Segoe UI", "Helvetica Neue", Arial, sans-serif;
  overscroll-behavior: none;
}

body {
  position: fixed;
  inset: 0;
  display: flex;
  justify-content: center;
  padding: 8px;
  overflow: hidden;
  touch-action: none;
}

.app {
  width: min(100%, 980px);
  height: 100%;
  display: grid;
  align-content: start;
  gap: var(--ui-gap);
  animation: appRise 0.8s ease-out;
}

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 2px;
}

.topbar-actions {
  display: flex;
  align-items: center;
  gap: 8px;
}

.topbar h1 {
  margin: 0;
  font-size: clamp(0.95rem, 2.4vw, 1.25rem);
  letter-spacing: 0.02em;
  background: linear-gradient(120deg, #d5e6ff 0%, #9fd0ff 45%, #f0f8ff 100%);
  -webkit-background-clip: text;
  background-clip: text;
  color: transparent;
  filter: drop-shadow(0 2px 8px rgba(103, 154, 255, 0.35));
  animation: titleShimmer 3.6s ease-in-out infinite;
}

.menu {
  border: 1px solid var(--line);
  border-radius: 12px;
  background: rgba(20, 26, 37, 0.82);
  overflow: hidden;
  backdrop-filter: blur(6px);
}

.menu > summary {
  list-style: none;
  cursor: pointer;
  user-select: none;
  padding: 8px 12px;
  font-weight: 700;
}

.menu > summary::-webkit-details-marker {
  display: none;
}

.menu-body {
  border-top: 1px solid var(--line);
  padding: 10px;
  min-width: 220px;
  display: grid;
  gap: 8px;
}

.menu-body p {
  margin: 0;
  color: var(--muted);
  font-size: 0.86rem;
}

button,
.home-link-btn {
  border: 1px solid var(--line);
  background: linear-gradient(180deg, #34425b, #273347);
  color: var(--text);
  padding: 10px 12px;
  border-radius: 8px;
  font: inherit;
  text-decoration: none;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  transition: transform 0.12s ease, border-color 0.12s ease, box-shadow 0.12s ease, background 0.12s ease;
}

.home-link-btn {
  gap: 6px;
  font-size: 0.86rem;
  font-weight: 700;
  white-space: nowrap;
}

button:hover,
.home-link-btn:hover {
  border-color: #506282;
  background: linear-gradient(180deg, #425474, #33435f);
  box-shadow: 0 5px 12px rgba(53, 72, 106, 0.35);
}

button:active,
.home-link-btn:active {
  transform: translateY(1px);
}

.home-link-btn:focus-visible {
  outline: 2px solid #b8ceff;
  outline-offset: 2px;
}

.hud-panel {
  width: var(--game-max);
  max-width: 800px;
  margin: 0 auto;
  display: grid;
  grid-template-columns: 1.15fr 0.86fr 1.2fr;
  gap: 6px;
  padding: 6px;
  border-radius: 12px;
  border: 1px solid rgba(130, 157, 211, 0.28);
  background: linear-gradient(180deg, rgba(20, 28, 45, 0.74), rgba(12, 18, 31, 0.62));
  box-shadow: 0 10px 18px rgba(0, 0, 0, 0.3);
}

.hud-cell {
  min-height: 70px;
  border-radius: 9px;
  border: 1px solid rgba(133, 166, 232, 0.22);
  background: linear-gradient(180deg, rgba(55, 75, 116, 0.42), rgba(24, 34, 56, 0.64));
  padding: 6px 8px;
}

.hud-label {
  margin: 0 0 2px;
  font-size: 0.62rem;
  color: rgba(198, 216, 255, 0.78);
  font-weight: 700;
  letter-spacing: 0.06em;
}

.hud-value {
  margin: 0;
  line-height: 1.1;
  color: #f6fbff;
  font-size: clamp(0.96rem, 2.5vw, 1.45rem);
  font-weight: 800;
}

.hud-meta {
  margin: 4px 0 0;
  color: #dce8ff;
  font-size: 0.68rem;
  font-weight: 700;
  display: flex;
  justify-content: space-between;
  gap: 6px;
}

.hud-bar {
  margin-top: 5px;
  height: 9px;
  border-radius: 999px;
  overflow: hidden;
  background: rgba(31, 42, 64, 0.78);
  border: 1px solid rgba(165, 192, 248, 0.22);
}

.hud-bar > span {
  display: block;
  height: 100%;
  width: 0%;
  border-radius: 999px;
  transition: width 0.12s linear;
}

#hudTimeFill {
  background: linear-gradient(90deg, #6da2e6, #95c9ff);
}

#hudHpFill {
  background: linear-gradient(90deg, #7ee38b, #b9f4c1);
}

.hud-effect {
  margin: 4px 0 0;
  min-height: 0.9rem;
  color: rgba(202, 216, 232, 0.9);
  font-size: 0.74rem;
  font-weight: 700;
  letter-spacing: 0.04em;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.hud-panel.is-low-time #hudTime {
  color: #d8eeff;
  animation: titleShimmer 0.8s ease-in-out infinite;
}

.hud-panel.is-low-time #hudTimeFill {
  background: linear-gradient(90deg, #73b5ff, #9ed7ff);
}

.hud-panel.is-low-hp #hudHpFill {
  background: linear-gradient(90deg, #ff7676, #ffb1b1);
}

.hud-panel.effect-slow .hud-status {
  border-color: rgba(152, 206, 255, 0.52);
  box-shadow: inset 0 0 0 1px rgba(152, 206, 255, 0.2);
}

.hud-panel.effect-slow #hudEffect {
  color: #c7e8ff;
}

.hud-panel.effect-fast .hud-status {
  border-color: rgba(130, 255, 220, 0.58);
  box-shadow: inset 0 0 0 1px rgba(130, 255, 220, 0.2);
}

.hud-panel.effect-fast #hudEffect {
  color: #b8ffe9;
}

.hud-panel.effect-invincible .hud-status {
  border-color: rgba(255, 231, 130, 0.62);
  box-shadow: inset 0 0 0 1px rgba(255, 231, 130, 0.22);
}

.hud-panel.effect-invincible #hudEffect {
  color: #fff3b3;
}

#hudEffect.is-active {
  font-weight: 800;
}

.game-wrap {
  position: relative;
  width: var(--game-max);
  max-width: 800px;
  aspect-ratio: 1 / 1;
  margin: 0 auto;
  border: 1px solid var(--line);
  border-radius: 14px;
  overflow: hidden;
  box-shadow:
    0 24px 48px rgba(0, 0, 0, 0.42),
    0 0 0 1px rgba(125, 147, 193, 0.14);
}

canvas {
  width: 100%;
  height: 100%;
  display: block;
  background: #181c24;
  touch-action: none;
}

.state-overlay {
  position: absolute;
  inset: 0;
  display: grid;
  place-items: center;
  padding: 20px;
  background: radial-gradient(circle at 50% 35%, rgba(54, 78, 130, 0.24), rgba(10, 12, 18, 0.67));
  backdrop-filter: blur(4px);
  opacity: 1;
  transition: opacity 0.24s ease;
}

.state-overlay.hidden {
  opacity: 0;
  pointer-events: none;
}

.state-card {
  width: min(90%, 420px);
  background: linear-gradient(180deg, rgba(39, 49, 69, 0.95), rgba(24, 31, 43, 0.95));
  border: 1px solid var(--line);
  border-radius: 14px;
  padding: 18px;
  display: grid;
  gap: 12px;
  text-align: center;
  box-shadow:
    0 14px 30px rgba(0, 0, 0, 0.36),
    inset 0 1px 0 rgba(255, 255, 255, 0.08);
  animation: panelFloat 2.5s ease-in-out infinite;
}

.state-card h2 {
  margin: 0;
  font-size: clamp(1.2rem, 3.2vw, 1.8rem);
}

.state-card p {
  margin: 0;
  color: var(--muted);
}

.state-actions {
  display: grid;
  gap: 8px;
}

.state-card.is-countdown {
  border-color: #7f9de2;
  box-shadow:
    0 14px 30px rgba(0, 0, 0, 0.36),
    0 0 0 1px rgba(120, 164, 255, 0.3),
    0 0 24px rgba(120, 164, 255, 0.22);
  animation: panelCountdownPulse 0.9s ease-in-out infinite;
}

.state-card.is-countdown h2 {
  font-size: clamp(4rem, 20vw, 8rem);
  line-height: 0.9;
  letter-spacing: -0.03em;
  color: #f2f7ff;
  text-shadow:
    0 0 12px rgba(148, 188, 255, 0.8),
    0 0 26px rgba(148, 188, 255, 0.4);
}

.state-card.is-fail {
  border-color: #8f5e5e;
}

.state-card.is-fail h2 {
  font-size: clamp(2rem, 11vw, 4.2rem);
  line-height: 1;
  color: #ffd3d3;
  text-shadow: 0 0 14px rgba(255, 136, 136, 0.45);
}

.state-card.is-fail p {
  color: #ffdede;
  font-weight: 700;
}

.state-card.is-clear {
  border-color: #5f8f6a;
}

#restartFromMenuBtn {
  border-color: #6d4a4a;
  background: #3e2a2a;
}

#restartFromMenuBtn:hover {
  border-color: #8b5e5e;
  background: #503333;
}

#toggleSoundBtn {
  border-color: #4d6486;
  background: #30415d;
}

#toggleSoundBtn:hover {
  border-color: #6584b0;
  background: #3c5173;
}

#toggleSoundBtn.is-muted {
  border-color: #6d4a4a;
  background: #3e2a2a;
}

.hint {
  margin: 0;
  text-align: center;
  color: var(--muted);
  font-size: 0.78rem;
}

.touch-controls {
  width: var(--game-max);
  max-width: 800px;
  margin: 0 auto;
  display: flex;
  justify-content: center;
  touch-action: none;
  user-select: none;
}

.joystick {
  position: relative;
  width: var(--joy-size);
  height: var(--joy-size);
  touch-action: none;
  filter: drop-shadow(0 10px 20px rgba(0, 0, 0, 0.4));
}

.joystick-ring {
  position: absolute;
  inset: 0;
  border-radius: 50%;
  border: 2px solid #4f6184;
  background:
    radial-gradient(circle at 30% 30%, rgba(105, 133, 190, 0.35), rgba(38, 48, 66, 0.92) 62%),
    radial-gradient(circle at center, rgba(40, 53, 78, 0.85), rgba(25, 33, 47, 0.98));
  box-shadow:
    inset 0 8px 16px rgba(255, 255, 255, 0.06),
    inset 0 -10px 16px rgba(0, 0, 0, 0.35);
}

.joystick-knob {
  position: absolute;
  left: 50%;
  top: 50%;
  width: calc(var(--joy-size) * 0.39);
  height: calc(var(--joy-size) * 0.39);
  margin-left: calc(var(--joy-size) * -0.195);
  margin-top: calc(var(--joy-size) * -0.195);
  border-radius: 50%;
  border: 2px solid #9cb7eb;
  background:
    radial-gradient(circle at 30% 25%, #8fb2f4, #5377ba 48%, #3f5f9c 100%);
  box-shadow:
    0 8px 15px rgba(0, 0, 0, 0.35),
    inset 0 6px 10px rgba(255, 255, 255, 0.2);
  transform: translate(0, 0);
  transition: transform 0.06s linear;
}

.joystick.is-active .joystick-ring {
  border-color: #87a7e3;
  box-shadow:
    inset 0 8px 16px rgba(255, 255, 255, 0.06),
    inset 0 -10px 16px rgba(0, 0, 0, 0.35),
    0 0 24px rgba(109, 156, 255, 0.4);
}

.joystick.is-active .joystick-knob {
  transition: none;
}

@media (max-width: 860px), (max-height: 860px) {
  :root {
    --ui-gap: 6px;
    --joy-size: clamp(114px, 19vh, 148px);
    --game-max: min(95vw, 800px, calc(100dvh - 214px));
  }

  body {
    padding: 6px;
  }

  .menu > summary {
    padding: 6px 10px;
    font-size: 0.84rem;
  }

  .hud-panel {
    padding: 5px;
    gap: 5px;
  }

  .hud-cell {
    min-height: 60px;
    padding: 5px 7px;
  }

  .hud-label {
    font-size: 0.56rem;
  }

  .hud-value {
    font-size: clamp(0.82rem, 2.2vw, 1.18rem);
  }

  .hud-meta,
  .hud-effect {
    font-size: 0.6rem;
  }

  .hint {
    display: none;
  }
}

@keyframes titleShimmer {
  0% {
    filter: drop-shadow(0 2px 8px rgba(103, 154, 255, 0.28));
  }
  50% {
    filter: drop-shadow(0 2px 12px rgba(146, 196, 255, 0.52));
  }
  100% {
    filter: drop-shadow(0 2px 8px rgba(103, 154, 255, 0.28));
  }
}

@keyframes panelFloat {
  0% {
    transform: translateY(0);
  }
  50% {
    transform: translateY(-3px);
  }
  100% {
    transform: translateY(0);
  }
}

@keyframes panelCountdownPulse {
  0% {
    transform: scale(1);
  }
  50% {
    transform: scale(1.015);
  }
  100% {
    transform: scale(1);
  }
}

@keyframes appRise {
  from {
    opacity: 0;
    transform: translateY(10px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}
