:root {
  color-scheme: dark;
  --bg: #06040f;
  --panel: #120a26;
  --panel-2: #1a1038;
  --text: #f2ecff;
  --muted: #b9a9d8;
  --violet: #b48bff;
  --violet-bright: #d3b8ff;
  --violet-ink: #180733;
  --cyan: #35d7ff;
  --rose: #ff7bd5;
  --amber: #ffc857;
  --mint: #8affc1;
  --danger: #ff5d6c;
  --border: rgba(170, 140, 235, 0.34);
  --focus: #ffd166;
}

* {
  box-sizing: border-box;
}

html,
body {
  height: 100%;
}

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  font-size: 18px;
  line-height: 1.5;
  overflow: hidden;
  overscroll-behavior: none;
  -webkit-font-smoothing: antialiased;
  -webkit-tap-highlight-color: transparent;
  user-select: none;
}

[hidden] {
  display: none !important;
}

#app {
  position: fixed;
  inset: 0;
  overflow: hidden;
}

#game-canvas {
  position: absolute;
  inset: 0;
  display: block;
  width: 100%;
  height: 100%;
  background: #06040f;
  touch-action: none;
  cursor: crosshair;
}

#game-canvas.dragging {
  cursor: grabbing;
}

.hud {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  z-index: 3;
  display: flex;
  justify-content: center;
  padding: max(10px, env(safe-area-inset-top)) max(14px, env(safe-area-inset-right)) 0 max(14px, env(safe-area-inset-left));
  pointer-events: none;
}

.hud-row {
  display: flex;
  align-items: stretch;
  gap: clamp(8px, 3vw, 26px);
  padding: 8px clamp(12px, 3.4vw, 22px);
  background: rgba(12, 6, 28, 0.82);
  border: 1px solid var(--border);
  border-radius: 16px;
}

.stat {
  display: flex;
  flex-direction: column;
  align-items: center;
  min-width: 52px;
}

.stat-label {
  font-size: 12px;
  font-weight: 700;
  letter-spacing: 0.12em;
  text-transform: uppercase;
  color: var(--muted);
}

.stat-value {
  font-size: clamp(18px, 4.4vw, 24px);
  font-weight: 800;
  line-height: 1.2;
  font-variant-numeric: tabular-nums;
}

.pips {
  display: inline-flex;
  align-items: center;
  gap: 7px;
  min-height: 30px;
}

.shield-pip {
  width: 13px;
  height: 13px;
  border-radius: 3px;
  transform: rotate(45deg);
  background: var(--violet);
  box-shadow: 0 0 10px rgba(180, 139, 255, 0.85);
}

.hud.low-shields .shield-pip {
  animation: shieldpulse 1s ease-in-out infinite;
}

@keyframes shieldpulse {
  0%,
  100% {
    opacity: 1;
  }
  50% {
    opacity: 0.35;
  }
}

.overlay {
  position: absolute;
  inset: 0;
  z-index: 5;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: max(16px, env(safe-area-inset-top)) max(16px, env(safe-area-inset-right)) max(16px, env(safe-area-inset-bottom)) max(16px, env(safe-area-inset-left));
  background: radial-gradient(circle at 50% 38%, rgba(24, 10, 50, 0.74), rgba(4, 2, 10, 0.92));
}

#loading {
  z-index: 9;
  background: #06040f;
}

#syspause {
  z-index: 8;
}

.card {
  width: min(92vw, 560px);
  max-height: calc(100vh - 32px);
  max-height: calc(100dvh - 32px);
  overflow: auto;
  -webkit-overflow-scrolling: touch;
  padding: clamp(22px, 5vw, 40px);
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 24px;
  box-shadow: 0 24px 70px rgba(0, 0, 0, 0.6);
  text-align: center;
  overscroll-behavior: contain;
}

.menu-card {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 12px;
}

.eyebrow {
  margin: 0;
  font-size: 0.8rem;
  font-weight: 800;
  letter-spacing: 0.28em;
  text-transform: uppercase;
  color: var(--violet);
}

h1 {
  margin: 0;
  font-size: clamp(2.1rem, 8vw, 3.5rem);
  line-height: 0.98;
  font-weight: 900;
  letter-spacing: 0.02em;
  color: #ffffff;
  text-shadow: 0 0 34px rgba(180, 139, 255, 0.6);
}

.subtitle {
  margin: 0;
  max-width: 34ch;
  color: var(--muted);
  font-size: 1.06rem;
}

.tail-preview {
  display: block;
  width: 100%;
  height: auto;
  border-radius: 16px;
  border: 1px solid var(--border);
  background: rgba(10, 5, 26, 0.65);
}

.menu-preview {
  max-width: 460px;
  margin: 2px 0 0;
}

.best-chip {
  margin: 2px 0 0;
  padding: 8px 18px;
  border-radius: 999px;
  background: rgba(255, 200, 87, 0.12);
  border: 1px solid rgba(255, 200, 87, 0.45);
  color: var(--amber);
  font-weight: 800;
  font-size: 1rem;
  letter-spacing: 0.02em;
}

.actions {
  display: flex;
  flex-direction: column;
  gap: 12px;
  width: min(100%, 320px);
  margin: 6px auto 0;
}

.btn {
  appearance: none;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  min-width: 200px;
  min-height: 54px;
  padding: 14px 24px;
  border: 2px solid transparent;
  border-radius: 14px;
  font: inherit;
  font-size: 1.04rem;
  font-weight: 800;
  letter-spacing: 0.01em;
  cursor: pointer;
  transition: background-color 0.15s ease, border-color 0.15s ease, transform 0.08s ease, box-shadow 0.15s ease;
}

.btn-primary {
  background: var(--violet);
  color: var(--violet-ink);
  box-shadow: 0 10px 26px rgba(180, 139, 255, 0.32);
}

.btn-primary:hover {
  background: var(--violet-bright);
}

.btn-secondary {
  background: rgba(180, 139, 255, 0.07);
  color: var(--text);
  border-color: var(--border);
}

.btn-secondary:hover {
  background: rgba(180, 139, 255, 0.16);
  border-color: rgba(196, 168, 245, 0.7);
}

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

.btn:focus-visible {
  outline: 3px solid var(--focus);
  outline-offset: 3px;
}

.hint {
  margin: 4px 0 0;
  color: var(--muted);
  font-size: 0.94rem;
}

.dialog-title {
  margin: 0 0 10px;
  font-size: clamp(1.6rem, 6vw, 2.2rem);
  line-height: 1.1;
  font-weight: 900;
  color: #ffffff;
}

.dialog-text {
  margin: 0 auto;
  max-width: 40ch;
  color: var(--muted);
  font-size: 1.03rem;
}

.rules {
  list-style: none;
  margin: 18px 0 6px;
  padding: 0;
  display: grid;
  gap: 12px;
  text-align: left;
}

.rules li {
  display: grid;
  grid-template-columns: 22px 1fr;
  gap: 12px;
  align-items: start;
  font-size: 1rem;
  line-height: 1.45;
  color: var(--text);
}

.rule-dot {
  width: 16px;
  height: 16px;
  margin-top: 4px;
  border-radius: 50%;
}

.rule-violet {
  background: var(--violet);
  box-shadow: 0 0 12px rgba(180, 139, 255, 0.7);
}

.rule-cyan {
  background: var(--cyan);
  box-shadow: 0 0 12px rgba(53, 215, 255, 0.7);
}

.rule-amber {
  background: var(--amber);
  box-shadow: 0 0 12px rgba(255, 200, 87, 0.7);
}

.rule-danger {
  background: var(--danger);
  box-shadow: 0 0 12px rgba(255, 93, 108, 0.7);
}

.rule-white {
  background: #ffffff;
  box-shadow: 0 0 12px rgba(255, 255, 255, 0.7);
}

.rule-text {
  border-radius: 4px;
  background: var(--muted);
}

.bead-line {
  margin: 10px 0 0;
  color: var(--muted);
  font-weight: 700;
  font-size: 0.95rem;
}

.score-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 12px;
  margin: 18px 0 8px;
}

.score-box {
  display: flex;
  flex-direction: column;
  gap: 2px;
  padding: 14px;
  border-radius: 16px;
  background: var(--panel-2);
  border: 1px solid var(--border);
}

.score-label {
  font-size: 0.78rem;
  font-weight: 800;
  letter-spacing: 0.14em;
  text-transform: uppercase;
  color: var(--muted);
}

.score-value {
  font-size: clamp(1.8rem, 7vw, 2.6rem);
  line-height: 1.1;
  font-weight: 900;
  color: #ffffff;
  font-variant-numeric: tabular-nums;
}

.loading-card {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 6px;
}

.spinner {
  width: 46px;
  height: 46px;
  margin-bottom: 10px;
  border: 4px solid rgba(180, 139, 255, 0.18);
  border-top-color: var(--violet);
  border-radius: 50%;
  animation: spin 0.9s linear infinite;
}

@keyframes spin {
  to {
    transform: rotate(360deg);
  }
}

.sr-only {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  white-space: nowrap;
  border: 0;
}

@media (max-height: 560px) {
  body {
    font-size: 16px;
  }

  .card {
    padding: 18px 20px;
  }

  h1 {
    font-size: clamp(1.9rem, 8vh, 2.8rem);
  }

  .menu-preview {
    height: 84px;
    width: auto;
    max-width: 100%;
    margin: 0 auto;
  }

  .actions {
    gap: 9px;
  }

  .btn {
    min-height: 48px;
    padding: 11px 18px;
  }

  .score-grid {
    margin: 12px 0 4px;
  }
}

@media (max-height: 480px) and (orientation: landscape) {
  .card {
    padding: 12px 20px;
    border-radius: 18px;
  }

  .menu-card {
    gap: 8px;
  }

  h1 {
    font-size: clamp(1.7rem, 9vh, 2.4rem);
  }

  .subtitle {
    font-size: 0.95rem;
  }

  .best-chip {
    padding: 4px 14px;
    font-size: 0.9rem;
  }

  .tail-preview {
    height: 88px;
    width: auto;
    max-width: 100%;
    margin: 4px auto;
  }

  .menu-preview {
    height: 64px;
    width: auto;
    max-width: 100%;
    margin: 0 auto;
  }

  .hint {
    display: none;
  }

  .actions {
    gap: 8px;
  }

  .btn {
    min-height: 48px;
    padding: 10px 18px;
  }

  .dialog-title {
    margin-bottom: 6px;
  }

  .rules {
    margin: 8px 0 4px;
    gap: 6px;
  }

  .rules li {
    font-size: 0.9rem;
    line-height: 1.32;
  }

  .score-grid {
    margin: 8px 0 4px;
  }

  .bead-line {
    margin: 4px 0 0;
  }

  #gameover .dialog-title {
    margin-bottom: 4px;
    font-size: clamp(1.4rem, 6vh, 1.9rem);
  }

  #gameover .dialog-text {
    font-size: 0.92rem;
  }

  #gameover .tail-preview {
    height: 64px;
    margin: 4px auto;
  }

  #gameover .bead-line {
    margin: 2px 0 0;
    font-size: 0.85rem;
  }

  #gameover .score-grid {
    display: flex;
    gap: 8px;
    margin: 6px 0 4px;
  }

  #gameover .score-box {
    flex: 1;
    flex-direction: row;
    align-items: baseline;
    justify-content: center;
    gap: 8px;
    padding: 6px 10px;
  }

  #gameover .score-value {
    font-size: 1.5rem;
  }

  #gameover .score-label {
    font-size: 0.68rem;
  }
}

@media (max-height: 380px) and (orientation: landscape) {
  .menu-card {
    gap: 4px;
  }

  .menu-preview {
    height: 48px;
  }

  #howto .card {
    display: flex;
    flex-direction: column;
    overflow: hidden;
    padding: 12px 20px;
  }

  #howto .dialog-title {
    flex: 0 0 auto;
    margin-bottom: 4px;
  }

  #howto .rules {
    flex: 1 1 auto;
    min-height: 0;
    overflow-y: auto;
    margin: 4px 0;
    padding-right: 6px;
    overscroll-behavior: contain;
  }

  #howto .actions {
    flex: 0 0 auto;
    margin-top: 4px;
  }
}

@media (max-height: 340px) and (orientation: landscape) {
  .menu-card .eyebrow,
  .menu-preview {
    display: none;
  }

  #gameover .card {
    padding-top: 10px;
    padding-bottom: 10px;
  }

  #gameover .dialog-text {
    display: none;
  }

  #gameover .tail-preview {
    height: 44px;
    margin: 2px auto;
  }

  #gameover .bead-line {
    font-size: 0.8rem;
  }

  #gameover .score-grid {
    margin: 4px 0 2px;
  }

  #gameover .actions {
    margin-top: 2px;
    gap: 6px;
  }
}

.back-link {
  position: fixed;
  top: max(10px, env(safe-area-inset-top));
  left: max(12px, env(safe-area-inset-left));
  z-index: 12;
  display: inline-flex;
  align-items: center;
  gap: 0.3rem;
  padding: 0.42rem 0.8rem;
  border: 1px solid rgba(170, 140, 235, 0.34);
  border-radius: 999px;
  background: rgba(18, 10, 38, 0.72);
  color: #f2ecff;
  font-size: 0.82rem;
  font-weight: 600;
  text-decoration: none;
  backdrop-filter: blur(8px);
  -webkit-backdrop-filter: blur(8px);
  transition: background 0.2s ease, border-color 0.2s ease, color 0.2s ease;
}

.back-link:hover,
.back-link:focus-visible {
  background: rgba(180, 139, 255, 0.28);
  border-color: rgba(211, 184, 255, 0.7);
  color: #ffffff;
}

@media (prefers-reduced-motion: reduce) {
  *,
  *::before,
  *::after {
    animation-duration: 0.001ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: 0.001ms !important;
  }
}
