:root {
  color-scheme: dark;
  font-family: 'Fira Sans', 'Inter', 'Segoe UI', system-ui, -apple-system, sans-serif;
  --bg: #0f1114;
  --panel: #161a1f;
  --cell: #1d2126;
  --stroke: #2a2f36;
  --text: #e8eaed;
  --muted: #a0a7b4;
  --accent: #d6b46a;
  --ok: #7dd3fc;
  --warn: #fca5a5;
  --shadow-soft: 0 20px 60px rgba(9, 11, 15, 0.6);
  --transition-fast: 160ms ease;
  --stage-top: rgba(26, 31, 37, 0.9);
  --stage-bottom: rgba(12, 14, 17, 0.96);
  --stage-glow: rgba(125, 211, 252, 0.18);
  --stage-border: rgba(42, 47, 54, 0.8);
  --stage-inner: rgba(214, 180, 106, 0.12);
  --letter-bg: rgba(24, 28, 34, 0.78);
  --letter-stroke: rgba(214, 180, 106, 0.34);
  --letter-fg: var(--text);
  --hit-glow: rgba(214, 180, 106, 0.4);
}

* {
  box-sizing: border-box;
}

body {
  -webkit-tap-highlight-color: transparent;

  margin: 0;
  min-height: 100vh;
  background: radial-gradient(circle at top, #15181f 0%, var(--bg) 55%);
  color: var(--text);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: flex-start;
  padding: clamp(0.28rem, 1.8vw, 0.9rem);
}

#app {
  display: flex;
  width: min(96vw, 46rem);
  margin-top: clamp(0.12rem, 1vw, 0.35rem);
}

.app-shell {

  position: relative;
  display: flex;
  flex-direction: column;
  gap: clamp(0.65rem, 1.6vw, 1rem);
  width: 100%;
  padding: clamp(0.78rem, 1.8vw, 1.45rem);
  min-height: calc(100vh - clamp(0.56rem, 3.6vw, 2.3rem));
  border-radius: 1.25rem;
  background: linear-gradient(160deg, rgba(30, 34, 40, 0.55), rgba(13, 15, 18, 0.95));
  border: 1px solid rgba(60, 65, 76, 0.25);
  box-shadow: var(--shadow-soft);
  backdrop-filter: blur(24px);
  font-size: calc(1rem * var(--font-scale, 1));
}

.app-shell::before {
  content: '';
  position: absolute;
  inset: 0;
  border-radius: inherit;
  pointer-events: none;
  border: 1px solid rgba(214, 180, 106, 0.08);
}

.streak-bar {
  display: flex;
  justify-content: space-between;
  align-items: center;
  font-size: clamp(0.62rem, 1.5vw, 0.8rem);
  letter-spacing: 0.1em;
  text-transform: uppercase;
  color: rgba(232, 234, 237, 0.45);
  margin: 0;
  padding: 0 0.05rem;
}

.streak-value {
  font-weight: 500;
  font-size: clamp(0.88rem, 3vw, 1.12rem);
  letter-spacing: 0.05em;
  color: rgba(232, 234, 237, 0.7);
  transition: color var(--transition-fast);
}

.streak-value--best {
  color: var(--accent);
}

.streak-indicator--flash {
  color: var(--accent) !important;
  animation: streak-flash 520ms ease;
}

.streak-indicator--warn {
  color: var(--warn) !important;
  animation: streak-warn 520ms ease;
}

@keyframes streak-flash {
  0% {
    text-shadow: 0 0 0 var(--hit-glow);
    transform: translateY(-1px);
  }
  60% {
    text-shadow: 0 0 14px var(--hit-glow);
  }
  100% {
    text-shadow: none;
    transform: translateY(0);
  }
}

@keyframes streak-warn {
  0% {
    transform: translateY(-2px);
  }
  100% {
    transform: translateY(0);
  }
}

.stage {
  position: relative;
  display: flex;
  justify-content: center;
  width: 100%;
  height: var(--stage-height, clamp(30rem, 80vh, 46rem));
  flex: 1 1 auto;
}

.stage .playfield {
  -webkit-tap-highlight-color: transparent;

  width: min(100%, 40rem);
  height: 100%;
  border-radius: 0;
  border: none;
  background: radial-gradient(circle at 50% -18%, var(--stage-glow), transparent 62%),
    linear-gradient(180deg, var(--stage-top), var(--stage-bottom));
  box-shadow: inset 0 0 0 1px var(--stage-inner);
  position: relative;
  overflow: hidden;
  isolation: isolate;
  padding-bottom: 0;
  margin: 0 auto;
  touch-action: manipulation;
  transition: border-color var(--transition-fast), box-shadow var(--transition-fast);
}



.controls {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(14rem, 1fr));
  gap: clamp(0.65rem, 1.8vw, 1rem);
  align-items: start;
  padding: clamp(0.75rem, 1.8vw, 1.2rem);
  border-radius: 0.9rem;
  background: rgba(22, 26, 31, 0.9);
  border: 1px solid rgba(42, 47, 54, 0.6);
  margin-top: clamp(0.6rem, 2vw, 1.1rem);
}

.controls-group {
  display: grid;
  gap: 0.38rem;
  align-items: start;
}

.controls-group--difficulty {
  grid-column: 1 / -1;
}

.controls-group > label {
  display: block;
  font-size: 0.8rem;
  letter-spacing: 0.12em;
  text-transform: uppercase;
  color: var(--muted);
}

.controls select,
.controls input[type='range'] {
  width: 100%;
}

#difficulty {
  width: 100%;
}

.controls input[type='checkbox'] {
  accent-color: var(--accent);
}

.controls-group--toggle label {
  display: inline-flex;
  align-items: center;
  gap: 0.55rem;
  font-size: 0.9rem;
  letter-spacing: 0.02em;
  text-transform: none;
  color: var(--text);
}

.controls-note--tap {
  margin: 0;
  font-size: 0.75rem;
  letter-spacing: 0.04em;
  color: rgba(232, 234, 237, 0.55);
}

.app-shell[data-tap='off'] .controls-note--tap {
  color: rgba(232, 234, 237, 0.38);
}

.controls-group[data-install-state='hidden'] {
  display: none;
}

.controls-group--install .control-button {
  min-width: 10rem;
}

.controls-group[data-install-state='installed'] .control-button {
  color: var(--ok);
  border-color: rgba(125, 211, 252, 0.45);
  background: rgba(125, 211, 252, 0.16);
  cursor: default;
}

.controls-group[data-install-state='unsupported'] .control-button,
.controls-group[data-install-state='hidden'] .control-button {
  border-color: rgba(58, 63, 72, 0.6);
  background: rgba(58, 63, 72, 0.2);
  color: var(--muted);
  cursor: not-allowed;
}

.controls-note {
  margin: 0;
  font-size: 0.8rem;
  color: rgba(232, 234, 237, 0.6);
  letter-spacing: 0.04em;
}

button,
.controls button,
.controls select,
.controls input[type='range'] {
  font: inherit;
  color: inherit;
  position: relative;
}

button,
.controls .control-button {
  padding: 0.75rem 1.4rem;
  border-radius: 0.75rem;
  border: 1px solid rgba(214, 180, 106, 0.4);
  background: rgba(214, 180, 106, 0.12);
  color: var(--accent);
  letter-spacing: 0.08em;
  text-transform: uppercase;
  min-height: 3rem;
  transition: background var(--transition-fast), transform var(--transition-fast);
}

button:hover,
.controls .control-button:hover {
  background: rgba(214, 180, 106, 0.2);
  transform: translateY(-1px);
}

button:focus-visible,
.controls .control-button:focus-visible {
  outline: 2px solid var(--ok);
  outline-offset: 3px;
}

input[type='range'] {
  appearance: none;
  height: 0.4rem;
  background: rgba(58, 63, 72, 0.6);
  border-radius: 999px;
  cursor: pointer;
}

input[type='range']:focus-visible {
  outline: 3px solid var(--ok);
  outline-offset: 4px;
}

input[type='range']:focus-visible {
  outline: 3px solid var(--ok);
  outline-offset: 4px;
}

input[type='range']::-webkit-slider-thumb {
  appearance: none;
  width: 1rem;
  height: 1rem;
  background: var(--accent);
  border-radius: 50%;
  box-shadow: 0 0 0 4px rgba(214, 180, 106, 0.24);
  transition: transform var(--transition-fast);
}

input[type='range']::-webkit-slider-thumb:active {
  transform: scale(1.1);
}

input[type='range']::-moz-range-thumb {
  width: 1rem;
  height: 1rem;
  background: var(--accent);
  border: none;
  border-radius: 50%;
  box-shadow: 0 0 0 4px rgba(214, 180, 106, 0.24);
}

.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-width: 720px) {
  body {
    padding: 1.25rem;
  }

  .app-shell {
    padding: clamp(1rem, 5vw, 2rem);
  }

  .controls {
    flex-direction: column;
    align-items: stretch;
  }

  .controls-group {
    justify-content: initial;
    justify-items: start;
  }
}

[data-tooltip] {
  position: relative;
}

[data-tooltip]::after {
  content: attr(data-tooltip);
  position: absolute;
  left: 50%;
  top: 0;
  transform: translate(-50%, calc(-100% - 0.6rem));
  background: rgba(16, 20, 26, 0.95);
  color: var(--text);
  font-size: 0.75rem;
  letter-spacing: 0.06em;
  padding: 0.35rem 0.55rem;
  border-radius: 0.5rem;
  border: 1px solid rgba(125, 211, 252, 0.2);
  opacity: 0;
  pointer-events: none;
  white-space: nowrap;
  transition: opacity 140ms ease, transform 140ms ease;
  z-index: 4;
}

[data-tooltip]:hover::after,
[data-tooltip]:focus-visible::after {
  opacity: 1;
  transform: translate(-50%, calc(-100% - 0.35rem));
}


.app-shell[data-theme='midnight'] {
  --accent: #4ea6ff;
  --muted: #8eaedb;
  --stage-top: rgba(20, 30, 46, 0.92);
  --stage-bottom: rgba(10, 14, 24, 0.96);
  --stage-glow: rgba(78, 166, 255, 0.24);
  --letter-bg: rgba(18, 26, 40, 0.82);
  --letter-stroke: rgba(78, 166, 255, 0.38);
  --hit-glow: rgba(78, 166, 255, 0.55);
}

.app-shell[data-theme='aurora'] {
  --accent: #5eead4;
  --muted: #8adfd0;
  --stage-top: rgba(18, 36, 34, 0.92);
  --stage-bottom: rgba(10, 20, 18, 0.96);
  --stage-glow: rgba(94, 234, 212, 0.26);
  --letter-bg: rgba(18, 32, 30, 0.8);
  --letter-stroke: rgba(94, 234, 212, 0.4);
  --hit-glow: rgba(94, 234, 212, 0.5);
}

.app-shell[data-theme='ember'] {
  --accent: #fb923c;
  --muted: #f4c19b;
  --stage-top: rgba(36, 24, 18, 0.9);
  --stage-bottom: rgba(16, 9, 6, 0.96);
  --stage-glow: rgba(251, 146, 60, 0.24);
  --letter-bg: rgba(34, 22, 18, 0.78);
  --letter-stroke: rgba(251, 146, 60, 0.38);
  --hit-glow: rgba(251, 146, 60, 0.5);
}

.rain-stage {
  display: block;
  width: 100%;
  height: 100%;
}

.rain-stage::after {
  content: '';
  position: absolute;
  inset: 50% auto auto 50%;
  transform: translate(-50%, -70%);
  font-size: clamp(1.1rem, 3vw, 1.6rem);
  color: rgba(232, 234, 237, 0.35);
  letter-spacing: 0.12em;
  text-transform: uppercase;
  transition: opacity 200ms ease, transform 200ms ease;
  pointer-events: none;
  opacity: 0;
}

.rain-stage--intro::after {
  content: 'Letter rain incoming…';
  opacity: 1;
  transform: translate(-50%, -50%);
}

.rain-stage:focus-visible {
  outline: none;
}

.rain-layer {
  position: absolute;
  inset: 0;
  pointer-events: none;
  z-index: 1;
}

.rain-layer--letters {
  pointer-events: auto;
}

.rain-layer--splash,
.rain-layer--hit {
  mix-blend-mode: screen;
  z-index: 2;
  pointer-events: none;
}

.rain-splash {
  position: absolute;
  left: var(--x, 50%);
  bottom: 0;
  width: clamp(2.8rem, 12vw, 4.6rem);
  height: clamp(0.9rem, 4vw, 1.6rem);
  transform: translate(-50%, 0) scale(0.6);
  border-radius: 999px;
  background: radial-gradient(circle, rgba(214, 180, 106, 0.55), rgba(214, 180, 106, 0));
  opacity: 0;
  animation: rain-splash 520ms ease-out forwards;
}

@keyframes rain-splash {
  0% {
    opacity: 0.52;
    transform: translate(-50%, 0) scale(0.6);
  }
  70% {
    opacity: 0.3;
    transform: translate(-50%, -6%) scale(1.05);
  }
  100% {
    opacity: 0;
    transform: translate(-50%, 6%) scale(1.2);
  }
}

.rain-letter {
  -webkit-tap-highlight-color: transparent;
  position: absolute;
  left: var(--x, 50%);
  top: var(--y, 0%);
  transform: translate(-50%, -50%);
  font-size: clamp(1.3rem, 4vw, 2.2rem);
  font-weight: 600;
  letter-spacing: 0.12em;
  color: var(--letter-fg);
  background: var(--letter-bg);
  border-radius: 0.55rem;
  border: 1px solid var(--letter-stroke);
  padding: clamp(0.32rem, 1.6vw, 0.52rem) clamp(0.42rem, 1.8vw, 0.68rem);
  box-shadow: 0 12px 28px rgba(7, 9, 12, 0.45);
  transition: transform 50ms linear, opacity 120ms ease;
  will-change: transform, opacity;
  z-index: 1;
  user-select: none;
}

.app-shell[data-tap='on'] .playfield {
  cursor: pointer;
}

.app-shell[data-tap='off'] .playfield {
  cursor: default;
}

.rain-letter[data-alive="false"] {
  opacity: 0;
  transform: translate(-50%, -35%) scale(0.92);
}

.rain-hit {
  position: absolute;
  left: var(--x, 50%);
  top: var(--y, 50%);
  transform: translate(-50%, -50%) scale(0.85);
  color: var(--hit-color, #e8eaed);
  font-weight: 700;
  font-size: clamp(1.25rem, 4vw, 2.2rem);
  letter-spacing: 0.16em;
  pointer-events: none;
  filter: drop-shadow(0 0 12px rgba(255, 255, 255, 0.35));
  animation: rain-hit-pop 360ms ease-out forwards;
}

@keyframes rain-hit-pop {
  0% {
    opacity: 0.9;
    transform: translate(-50%, -55%) scale(0.85);
  }
  60% {
    opacity: 0.65;
    transform: translate(-50%, -60%) scale(1.05);
  }
  100% {
    opacity: 0;
    transform: translate(-50%, -70%) scale(1.2);
  }
}


.virtual-keyboard-host {
  position: static;
  min-height: var(--keyboard-height, 168px);
  padding: clamp(0.32rem, 2.4vw, 0.72rem) clamp(0.48rem, 2.6vw, 1.1rem);
  display: flex;
  justify-content: center;
  align-items: flex-end;
  pointer-events: auto;
}

.virtual-keyboard {
  display: none;
  pointer-events: auto;
  flex-direction: column;
  gap: clamp(0.28rem, 2vw, 0.45rem);
  width: min(100%, 36rem);
  margin: 0 auto;
}

.virtual-keyboard--visible {
  display: flex;
}

.virtual-keyboard__row {
  display: flex;
  justify-content: center;
  gap: clamp(0.32rem, 2vw, 0.55rem);
}

.virtual-keyboard__row:nth-child(2) {
  padding-inline: clamp(1rem, 4vw, 2rem);
}

.virtual-keyboard__row:nth-child(3) {
  padding-inline: clamp(2rem, 6vw, 3rem);
}

.virtual-keyboard__key {
  flex: 0 1 clamp(2.1rem, 8.5vw, 2.8rem);
  padding: clamp(0.6rem, 3vw, 0.75rem) clamp(0.35rem, 2vw, 0.55rem);
  border-radius: 0.45rem;
  border: 1px solid rgba(42, 47, 54, 0.8);
  background: rgba(30, 35, 42, 0.95);
  color: rgba(232, 234, 237, 0.92);
  font-size: clamp(0.82rem, 2.6vw, 0.95rem);
  font-weight: 600;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  cursor: pointer;
  transition: background 140ms ease, transform 100ms ease;
}

.virtual-keyboard__key:focus-visible {
  outline: 2px solid var(--ok);
  outline-offset: 2px;
}

.virtual-keyboard__key:active {
  transform: translateY(1px);
  background: rgba(42, 47, 54, 0.95);
}

@media (max-width: 480px) {
  .virtual-keyboard {
    width: min(100%, 28rem);
    gap: 0.4rem;
  }
  .virtual-keyboard__row {
    gap: clamp(0.28rem, 3vw, 0.45rem);
  }
  .virtual-keyboard__row:nth-child(2) {
    padding-inline: clamp(0.8rem, 6vw, 1.8rem);
  }
  .virtual-keyboard__row:nth-child(3) {
    padding-inline: clamp(1.6rem, 8vw, 2.6rem);
  }
  .virtual-keyboard__key {
    flex: 0 1 clamp(1.9rem, 10vw, 2.4rem);
    padding: 0.55rem 0.3rem;
    font-size: 0.92rem;
  }
}

.badge-stack {
  position: absolute;
  top: clamp(0.8rem, 2vw, 1.25rem);
  right: clamp(0.8rem, 2vw, 1.25rem);
  display: grid;
  gap: 0.65rem;
  pointer-events: none;
  z-index: 3;
}

.badge-toast {
  position: relative;
  background: rgba(16, 20, 26, 0.92);
  border: 1px solid rgba(214, 180, 106, 0.5);
  border-radius: 0.85rem;
  padding: 0.85rem 1.05rem;
  min-width: clamp(12rem, 28vw, 16rem);
  box-shadow: 0 16px 40px rgba(8, 10, 13, 0.55);
  animation: badge-toast-enter 260ms ease forwards;
}

.badge-toast::before {
  content: '';
  position: absolute;
  inset: 0;
  border-radius: inherit;
  box-shadow: inset 0 0 0 1px rgba(125, 211, 252, 0.1);
  pointer-events: none;
}

.badge-toast__title {
  margin: 0;
  font-size: 0.95rem;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  color: var(--text);
}

.badge-toast__body {
  margin: 0.35rem 0 0;
  font-size: 0.85rem;
  color: rgba(232, 234, 237, 0.75);
  letter-spacing: 0.04em;
}

.badge-toast--leaving {
  animation: badge-toast-exit 220ms ease forwards;
}

@keyframes badge-toast-enter {
  0% {
    opacity: 0;
    transform: translateY(12px) scale(0.96);
  }
  100% {
    opacity: 1;
    transform: translateY(0) scale(1);
  }
}

@keyframes badge-toast-exit {
  0% {
    opacity: 1;
    transform: translateY(0) scale(1);
  }
  100% {
    opacity: 0;
    transform: translateY(-12px) scale(0.96);
  }
}
