:root {
  --bg: #070a0f;
  --surface: #111827;
  --surface-2: #1a2230;
  --border: rgba(255, 255, 255, 0.08);
  --text: #f0f4fc;
  --muted: rgba(240, 244, 252, 0.62);
  --accent: #5b8cff;
  --accent-glow: rgba(91, 140, 255, 0.45);
  --success: #34d399;
  --radius: 12px;
  --font: "Google Sans", "Product Sans", system-ui, -apple-system, BlinkMacSystemFont,
    "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
}

*,
*::before,
*::after {
  box-sizing: border-box;
}

html {
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
}

body {
  margin: 0;
  min-height: 100dvh;
  background: var(--bg);
  color: var(--text);
  font-family: var(--font);
  overflow-x: hidden;
}

/* Ambient background */
.ambient {
  position: fixed;
  inset: 0;
  pointer-events: none;
  z-index: 0;
  background:
    radial-gradient(ellipse 80% 50% at 50% -20%, rgba(91, 140, 255, 0.12), transparent),
    radial-gradient(ellipse 60% 40% at 100% 100%, rgba(120, 80, 255, 0.06), transparent),
    var(--bg);
}

.app {
  position: relative;
  z-index: 2;
  max-width: 920px;
  margin: 0 auto;
  padding: clamp(20px, 5vw, 40px) clamp(16px, 4vw, 24px) 48px;
  text-align: center;
}

/* Header */
.brand {
  margin-bottom: 8px;
}

.brand-logo {
  margin: 0;
  line-height: 0;
}

.brand-logo img {
  display: block;
  width: min(100%, 280px);
  height: auto;
  margin: 0 auto;
}

.brand p {
  margin: 8px 0 0;
  font-size: 0.95rem;
  color: var(--muted);
  font-weight: 400;
  line-height: 1.45;
}

/* World stats strip */
.world-stats {
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  gap: 10px 20px;
  margin: 20px 0 8px;
  font-size: 2rem;
  color: var(--muted);
  letter-spacing: 0.02em;
}

.world-stats span {
  display: inline-flex;
  align-items: center;
  gap: 6px;
}

.world-stats strong {
  color: var(--text);
  font-weight: 500;
}

/* Status */
.status {
  min-height: 24px;
  margin: 16px 0;
  font-size: clamp(1.2rem, 4.5vw, 2rem);
  color: var(--muted);
  transition: color 0.2s ease;
}

.status.success {
  color: var(--success);
}

.status.error {
  color: #f87171;
}

.status.playing {
  color: var(--accent);
}

.status .country-flag,
.status-meta .country-flag {
  display: inline-block;
  width: 1.35em;
  height: 1em;
  margin: 0 0.2em;
  vertical-align: -0.15em;
  object-fit: cover;
  border-radius: 2px;
  box-shadow: 0 0 0 1px rgba(255, 255, 255, 0.12);
}

.status-meta .country-flag {
  width: 1.15em;
  height: 0.85em;
  margin: 0 0.15em;
  vertical-align: -0.1em;
}

.status-meta {
  margin: -8px auto 8px;
  max-width: min(92%, 900px);
  font-size: 0.8rem;
  color: var(--muted);
  line-height: 1.45;
  text-align: center;
  overflow-wrap: anywhere;
}

.override-area {
  margin: -4px 0 10px;
}

.override-link {
  background: none;
  border: 0;
  padding: 0;
  color: var(--muted);
  text-decoration: underline;
  text-underline-offset: 2px;
  font-size: 0.86rem;
}

.override-link:hover:not(:disabled) {
  color: var(--text);
  background: none;
  border: 0;
}

/* Keyboard */
.keyboard {
  display: grid;
  grid-template-columns: repeat(13, 1fr);
  gap: clamp(4px, 1.2vw, 8px);
  margin: 28px auto;
  max-width: 100%;
  overflow: visible;
}

@media (max-width: 640px) {
  .keyboard {
    grid-template-columns: repeat(7, 1fr);
  }
}

.key {
  position: relative;
  aspect-ratio: 1;
  max-height: 52px;
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  user-select: none;
  font-size: clamp(0.75rem, 2.5vw, 0.9rem);
  font-weight: 500;
  color: var(--text);
  transition:
    transform 0.15s cubic-bezier(0.34, 1.56, 0.64, 1),
    background 0.15s ease,
    border-color 0.15s ease,
    box-shadow 0.2s ease;
  -webkit-tap-highlight-color: transparent;
  overflow: visible;
}

.key-label {
  position: relative;
  z-index: 1;
}

.key-sparks {
  position: absolute;
  left: 50%;
  top: 50%;
  width: 0;
  height: 0;
  pointer-events: none;
  z-index: 2;
}

.spark {
  position: absolute;
  left: 0;
  top: 0;
  width: 5px;
  height: 5px;
  margin: -2.5px 0 0 -2.5px;
  border-radius: 50%;
  background: radial-gradient(circle, rgba(180, 210, 255, 1) 0%, rgba(91, 140, 255, 0.6) 50%, transparent 100%);
  box-shadow: 0 0 6px rgba(91, 140, 255, 0.8);
  animation: sparkBurst 0.7s cubic-bezier(0.22, 1, 0.36, 1) forwards;
  opacity: 0;
}

@keyframes sparkBurst {
  0% {
    transform: translate(0, 0) scale(1);
    opacity: 1;
  }
  100% {
    transform: translate(var(--tx), var(--ty)) scale(0.15);
    opacity: 0;
  }
}

.key::after {
  content: "";
  position: absolute;
  inset: 0;
  border-radius: inherit;
  opacity: 0;
  background: radial-gradient(circle at var(--rx, 50%) var(--ry, 50%), var(--accent-glow), transparent 70%);
  transition: opacity 0.25s ease;
  pointer-events: none;
}

.key:hover {
  border-color: rgba(91, 140, 255, 0.35);
  transform: translateY(-1px);
}

.key:active {
  transform: scale(0.96);
}

.key.active {
  background: linear-gradient(145deg, #4a7aff 0%, #3d66e8 100%);
  border-color: rgba(255, 255, 255, 0.2);
  box-shadow: 0 0 24px var(--accent-glow);
  transform: scale(1.04);
}

.key.playing {
  border-color: rgba(91, 140, 255, 0.55);
  box-shadow:
    0 0 18px rgba(91, 140, 255, 0.35),
    0 0 0 1px rgba(255, 255, 255, 0.04) inset;
}

.key.playing::after {
  opacity: 0.9;
}

.key.active::after {
  opacity: 1;
}

.key.pulse {
  animation: keyPulse 0.4s ease;
}

@keyframes keyPulse {
  0% {
    box-shadow: 0 0 0 0 var(--accent-glow);
  }
  70% {
    box-shadow: 0 0 0 12px transparent;
  }
  100% {
    box-shadow: 0 0 24px var(--accent-glow);
  }
}

/* Form */
.volume-control {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 12px;
  margin: 16px auto 0;
  max-width: 320px;
  padding: 0 8px;
}

.volume-control label {
  font-size: 0.85rem;
  color: var(--muted);
  flex-shrink: 0;
}

.volume-control input[type="range"] {
  flex: 1;
  min-width: 0;
  height: 44px;
  margin: 0;
  accent-color: var(--accent);
  cursor: pointer;
}

.volume-control input[type="range"]:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 3px;
}

.key:disabled {
  cursor: not-allowed;
}

.form-block {
  margin-top: 24px;
}

input#initials {
  padding: 14px 18px;
  border-radius: var(--radius);
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text);
  text-align: center;
  width: min(200px, 100%);
  font-size: 1rem;
  font-family: inherit;
  letter-spacing: 0.12em;
  text-transform: uppercase;
  outline: none;
  transition: border-color 0.2s, box-shadow 0.2s;
}

input#initials::placeholder {
  letter-spacing: normal;
  text-transform: none;
  color: var(--muted);
}

input#initials:focus {
  border-color: rgba(91, 140, 255, 0.5);
  box-shadow: 0 0 0 3px rgba(91, 140, 255, 0.15);
}

.actions {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 10px;
  margin-top: 16px;
}

button {
  padding: 12px 22px;
  border-radius: var(--radius);
  border: 1px solid var(--border);
  background: var(--surface-2);
  color: var(--text);
  font-family: inherit;
  font-size: 0.95rem;
  font-weight: 500;
  cursor: pointer;
  transition:
    background 0.15s ease,
    border-color 0.15s ease,
    transform 0.1s ease,
    opacity 0.15s ease;
  -webkit-tap-highlight-color: transparent;
}

button:hover:not(:disabled) {
  background: #24314a;
  border-color: rgba(91, 140, 255, 0.25);
}

button:active:not(:disabled) {
  transform: scale(0.98);
}

button:focus-visible,
.key:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}

button:disabled {
  opacity: 0.45;
  cursor: not-allowed;
}

button.primary {
  background: linear-gradient(145deg, #4a7aff 0%, #3d66e8 100%);
  border-color: transparent;
  color: #fff;
  min-width: 200px;
}

button.primary:hover:not(:disabled) {
  filter: brightness(1.06);
  background: linear-gradient(145deg, #5582ff 0%, #456ee8 100%);
}

.playback-actions {
  margin-top: 8px;
  display: flex;
  justify-content: center;
  gap: 8px;
  flex-wrap: wrap;
}

button.play-btn {
  min-width: 240px;
}

button.share-btn {
  margin-top: 8px;
  min-width: 240px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
}

button.stop-btn {
  min-width: 240px;
  background: transparent;
  border-color: rgba(248, 113, 113, 0.35);
  color: rgba(248, 113, 113, 0.95);
}

button.stop-btn:hover:not(:disabled) {
  background: rgba(248, 113, 113, 0.08);
  border-color: rgba(248, 113, 113, 0.55);
}

.hidden {
  display: none !important;
}

.cookie-gate {
  margin: 0 0 20px;
  padding: 16px 18px;
  border-radius: var(--radius);
  border: 1px solid rgba(248, 113, 113, 0.45);
  background: rgba(248, 113, 113, 0.1);
  color: var(--text);
  font-size: 0.95rem;
  line-height: 1.5;
  text-align: center;
}

.cookie-gate p {
  margin: 0;
}

.app.app--cookies-blocked .brand,
.app.app--cookies-blocked .world-stats,
.app.app--cookies-blocked .status,
.app.app--cookies-blocked .volume-control,
.app.app--cookies-blocked .keyboard,
.app.app--cookies-blocked .form-block {
  pointer-events: none;
  opacity: 0.35;
  user-select: none;
}

.override-modal-overlay {
  position: fixed;
  inset: 0;
  z-index: 1400;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px;
}

.override-modal-backdrop {
  position: absolute;
  inset: 0;
  background: rgba(7, 10, 15, 0.62);
}

.override-modal-card {
  position: relative;
  z-index: 1;
  width: min(420px, calc(100vw - 40px));
  padding: 22px;
  background: #141c2a;
  border: 1px solid rgba(91, 140, 255, 0.35);
  border-radius: 16px;
  box-shadow:
    0 24px 64px rgba(0, 0, 0, 0.6),
    0 0 0 1px rgba(255, 255, 255, 0.06) inset;
  text-align: left;
}

.override-modal-title {
  margin: 0 0 10px;
  font-size: 1.05rem;
  font-weight: 500;
}

.override-modal-body {
  margin: 0 0 14px;
  color: rgba(240, 244, 252, 0.88);
  line-height: 1.5;
}

.override-modal-label {
  display: block;
  margin-bottom: 14px;
  font-size: 0.85rem;
  color: var(--muted);
}

.override-modal-input {
  width: 100%;
  margin-top: 6px;
  padding: 10px 12px;
  border-radius: var(--radius);
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text);
  text-transform: uppercase;
  letter-spacing: 0.08em;
}

.override-modal-input:focus {
  outline: 2px solid var(--accent);
  outline-offset: 1px;
}

.override-modal-actions {
  display: flex;
  gap: 8px;
  justify-content: flex-end;
}

/* Playback mode (future scale) */
.playback-modes {
  margin-top: 12px;
  font-size: 0.8rem;
  color: var(--muted);
}

.playback-modes label {
  margin: 0 10px;
  cursor: pointer;
}

.playback-modes input {
  margin-right: 4px;
  accent-color: var(--accent);
}

/* Loading shimmer on stats */
.world-stats.loading strong {
  opacity: 0.4;
}

/* First-visit tour */
body.tour-open {
  overflow: hidden;
}

.tour-overlay {
  position: fixed;
  inset: 0;
  z-index: 1000;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px;
}

.tour-backdrop {
  position: absolute;
  inset: 0;
  background: rgba(7, 10, 15, 0.55);
}

.tour-card {
  position: relative;
  z-index: 1;
  width: min(400px, calc(100vw - 40px));
  padding: 24px 22px 20px;
  background: #141c2a;
  border: 1px solid rgba(91, 140, 255, 0.35);
  border-radius: 16px;
  box-shadow:
    0 24px 64px rgba(0, 0, 0, 0.6),
    0 0 0 1px rgba(255, 255, 255, 0.06) inset;
  text-align: left;
  color: var(--text);
}

.tour-card--anchored {
  position: fixed;
  z-index: 1001;
}

@media (max-width: 640px) {
  .tour-overlay {
    padding: max(16px, env(safe-area-inset-top, 0px)) 16px
      max(16px, env(safe-area-inset-bottom, 0px));
    align-items: center;
    justify-content: center;
  }

  .tour-card--anchored {
    position: relative;
    top: auto !important;
    left: auto !important;
    transform: none !important;
  }
}

.tour-step-count {
  margin: 0 0 8px;
  font-size: 0.75rem;
  color: var(--muted);
  letter-spacing: 0.06em;
  text-transform: uppercase;
}

.tour-title {
  margin: 0 0 10px;
  font-size: 1.15rem;
  font-weight: 500;
  letter-spacing: -0.02em;
}

.tour-body {
  margin: 0 0 20px;
  font-size: 0.92rem;
  line-height: 1.55;
  color: rgba(240, 244, 252, 0.88);
}

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

.tour-actions .tour-skip {
  margin-right: auto;
  background: transparent;
  border-color: transparent;
  color: var(--muted);
  font-size: 0.85rem;
}

.tour-actions .tour-skip:hover {
  color: var(--text);
  background: transparent;
}

.tour-actions .tour-next {
  min-width: 88px;
}

.tour-highlight {
  position: relative;
  z-index: 1002 !important;
  box-shadow:
    0 0 0 3px var(--accent),
    0 0 32px var(--accent-glow) !important;
  border-radius: var(--radius);
}

/* Subtle glitch system */
.glitch-idle {
  animation: glitchIdle 4.2s steps(1, end) infinite;
}

.glitch-burst {
  animation: glitchBurst 300ms steps(2, end);
}

.tour-card.glitch-idle {
  animation: tourGlitchIdle 3.6s steps(1, end) infinite;
}

.tour-card.glitch-burst {
  animation: tourGlitchBurst 320ms steps(2, end);
}

.tour-card.glitch-burst .tour-title,
.tour-card.glitch-burst .tour-body,
.tour-card.glitch-idle .tour-title,
.tour-card.glitch-idle .tour-body {
  text-shadow:
    1px 0 rgba(255, 70, 180, 0.26),
    -1px 0 rgba(80, 220, 255, 0.26);
}

.tour-card::before,
.tour-card::after {
  content: "";
  position: absolute;
  inset: 0;
  pointer-events: none;
  border-radius: inherit;
  opacity: 0;
}

.tour-card.glitch-burst::before {
  background:
    linear-gradient(
      to bottom,
      transparent 0%,
      transparent 18%,
      rgba(255, 80, 180, 0.12) 18%,
      rgba(255, 80, 180, 0.12) 22%,
      transparent 22%,
      transparent 54%,
      rgba(80, 220, 255, 0.12) 54%,
      rgba(80, 220, 255, 0.12) 57%,
      transparent 57%
    );
  animation: tourScanlineBurst 320ms steps(2, end);
}

.tour-card.glitch-idle::after {
  background: linear-gradient(90deg, rgba(255, 80, 180, 0.08), rgba(80, 220, 255, 0.08));
  mix-blend-mode: screen;
  animation: tourRgbIdle 3.6s steps(1, end) infinite;
}

@keyframes glitchIdle {
  0%, 88%, 100% {
    transform: translate(0, 0);
    opacity: 1;
  }
  90% {
    transform: translate(-1px, 0.5px);
    opacity: 0.97;
  }
  93% {
    transform: translate(1px, -0.5px);
  }
  96% {
    transform: translate(-0.8px, 0.4px);
  }
}

@keyframes glitchBurst {
  0%, 100% {
    transform: translate(0, 0) skew(0);
    opacity: 1;
    text-shadow: none;
  }
  22% {
    transform: translate(-1.6px, 1.2px) skew(-0.8deg);
    opacity: 0.93;
    text-shadow:
      1px 0 rgba(255, 80, 180, 0.25),
      -1px 0 rgba(80, 220, 255, 0.25);
  }
  47% {
    transform: translate(1.6px, -1.2px) skew(0.8deg);
  }
  72% {
    transform: translate(-0.6px, 0.6px);
    opacity: 0.98;
  }
}

@keyframes tourGlitchIdle {
  0%, 84%, 100% {
    opacity: 1;
    filter: none;
  }
  86% {
    opacity: 0.97;
    filter: saturate(1.05);
  }
  90% {
    opacity: 0.94;
  }
  94% {
    opacity: 0.98;
  }
}

@keyframes tourGlitchBurst {
  0%, 100% {
    opacity: 1;
    filter: none;
    box-shadow:
      0 24px 64px rgba(0, 0, 0, 0.6),
      0 0 0 1px rgba(255, 255, 255, 0.06) inset;
  }
  33% {
    opacity: 0.9;
    filter: saturate(1.12);
    box-shadow:
      0 24px 64px rgba(0, 0, 0, 0.6),
      1px 0 rgba(255, 80, 180, 0.2),
      -1px 0 rgba(80, 220, 255, 0.2),
      0 0 0 1px rgba(255, 255, 255, 0.06) inset;
  }
  66% {
    opacity: 0.95;
  }
}

@keyframes tourScanlineBurst {
  0%, 100% { opacity: 0; transform: translateX(0); }
  25% { opacity: 0.9; transform: translateX(-1px); }
  50% { opacity: 0.65; transform: translateX(1px); }
  75% { opacity: 0.45; transform: translateX(-0.5px); }
}

@keyframes tourRgbIdle {
  0%, 88%, 100% { opacity: 0; transform: translateX(0); }
  90% { opacity: 0.35; transform: translateX(-1px); }
  94% { opacity: 0.2; transform: translateX(1px); }
}

/* Reduced motion */
@media (prefers-reduced-motion: reduce) {
  .key,
  .key.active,
  button {
    transition: none;
  }
  .key.pulse {
    animation: none;
  }
  .spark {
    animation: none;
    display: none;
  }
  .glitch-idle,
  .glitch-burst {
    animation: none !important;
    transform: none !important;
    opacity: 1 !important;
    text-shadow: none !important;
    filter: none !important;
  }
}

.site-footer {
  position: relative;
  z-index: 2;
  padding: 0 16px 24px;
  margin-top: 28px;
  text-align: center;
  font-size: 0.8rem;
  color: var(--muted);
}

.site-footer a {
  color: inherit;
  text-decoration: underline;
  text-underline-offset: 2px;
}

.site-footer .privacy-link {
  margin: 0 0 0 0.15em;
  padding: 0;
  border: none;
  background: none;
  font: inherit;
  color: inherit;
  text-decoration: underline;
  text-underline-offset: 2px;
  cursor: pointer;
}

.site-footer .privacy-link::before {
  content: "·";
  display: inline-block;
  margin: 0 0.35em;
  text-decoration: none;
}

.tour-card--privacy {
  width: min(440px, calc(100vw - 40px));
}

.tour-actions--privacy {
  justify-content: flex-end;
}

.tour-actions--privacy .privacy-close {
  min-width: 88px;
}
