:root {
  color-scheme: light;
  --bg: #f3e4cc;
  --panel: #f9f0dd;
  --border: #b59463;
  --text: #3b2a16;
  --muted: #7b6244;
  --accent: #bf8b35;
  --danger: #8a2f2f;
  --rage-1: #ff2e88;
  --rage-2: #ff8a00;
  --rage-3: #fff24f;
}

* {
  box-sizing: border-box;
}

[hidden] {
  display: none !important;
}

body {
  margin: 0;
  font-family: "SF Pro Text", "Helvetica Neue", Arial, sans-serif;
  background:
    radial-gradient(circle at 20% 20%, rgba(255, 227, 171, 0.5) 0, rgba(255, 227, 171, 0) 32%),
    radial-gradient(circle at 80% 75%, rgba(204, 167, 104, 0.35) 0, rgba(204, 167, 104, 0) 28%),
    linear-gradient(170deg, #ecd7b5 0%, #cfad7b 60%, #b88f58 100%);
  color: var(--text);
  min-height: 100vh;
  min-height: 100dvh;
  display: flex;
  justify-content: center;
  align-items: center;
  padding: 32px 16px;
  padding-top: max(16px, env(safe-area-inset-top));
  padding-right: max(12px, env(safe-area-inset-right));
  padding-bottom: max(16px, env(safe-area-inset-bottom));
  padding-left: max(12px, env(safe-area-inset-left));
  position: relative;
  overflow: hidden;
  overscroll-behavior: none;
}

body::before {
  content: "";
  position: fixed;
  inset: 0;
  pointer-events: none;
  background:
    radial-gradient(circle at 16px 12px, rgba(88, 54, 19, 0.14) 0 7px, transparent 8px),
    radial-gradient(circle at 28px 7px, rgba(88, 54, 19, 0.12) 0 5px, transparent 6px),
    radial-gradient(circle at 8px 7px, rgba(88, 54, 19, 0.12) 0 5px, transparent 6px),
    radial-gradient(circle at 22px 24px, rgba(88, 54, 19, 0.16) 0 9px, transparent 10px);
  background-size: 90px 90px;
  opacity: 0.55;
  z-index: 0;
}

body::after {
  content: "";
  position: fixed;
  inset: 0;
  pointer-events: none;
  z-index: 2;
  opacity: 0;
  background:
    radial-gradient(circle at 18% 20%, rgba(255, 88, 151, 0.35) 0, rgba(255, 88, 151, 0) 42%),
    radial-gradient(circle at 80% 78%, rgba(255, 161, 45, 0.3) 0, rgba(255, 161, 45, 0) 46%),
    repeating-linear-gradient(
      -20deg,
      rgba(255, 255, 255, 0.08) 0 6px,
      rgba(255, 255, 255, 0) 6px 20px
    );
  mix-blend-mode: screen;
}

body.rage-mode {
  animation: rage-screen 0.55s linear infinite;
}

body.rage-mode::after {
  opacity: 1;
  animation: rage-overlay 0.42s steps(2) infinite;
}

@keyframes rage-screen {
  0% {
    filter: hue-rotate(0deg) saturate(1.15) contrast(1.05);
  }
  25% {
    filter: hue-rotate(20deg) saturate(1.5) contrast(1.18);
  }
  50% {
    filter: hue-rotate(55deg) saturate(1.75) contrast(1.26);
  }
  75% {
    filter: hue-rotate(32deg) saturate(1.55) contrast(1.19);
  }
  100% {
    filter: hue-rotate(0deg) saturate(1.15) contrast(1.05);
  }
}

@keyframes rage-overlay {
  0% {
    transform: translate3d(0, 0, 0);
    opacity: 0.45;
  }
  100% {
    transform: translate3d(-4px, 3px, 0);
    opacity: 0.82;
  }
}

.shell {
  width: min(1080px, 100%);
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 16px;
  padding: 20px;
  display: grid;
  gap: 20px;
  box-shadow: 0 24px 40px rgba(70, 41, 10, 0.22);
  position: relative;
  z-index: 1;
  height: calc(100vh - 24px);
  height: calc(100dvh - 24px);
  max-height: calc(100vh - 24px);
  max-height: calc(100dvh - 24px);
  overflow: hidden;
  grid-template-rows: auto 1fr auto;
}

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

.rage-indicator {
  display: grid;
  justify-items: center;
  gap: 2px;
  background: linear-gradient(135deg, #ff7ac1 0%, #ffd95a 30%, #77ff9e 55%, #7eaaff 80%, #c17bff 100%);
  border: 1px solid #80521d;
  border-radius: 10px;
  padding: 6px 10px;
  min-width: 130px;
  box-shadow: 0 6px 14px rgba(70, 41, 10, 0.22);
}

.rage-indicator strong {
  font-size: 12px;
  line-height: 1.1;
  color: #2a1708;
}

.rage-indicator span {
  font-size: 16px;
  font-weight: 800;
  color: #2a1708;
}

.audio-toggle {
  min-width: 90px;
  padding: 8px 10px;
  font-size: 12px;
  font-weight: 700;
  border-radius: 8px;
}

.game-title {
  margin: 0 0 6px 0;
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 6px 12px 8px 10px;
  border-radius: 999px;
  border: 1px solid #7f541f;
  background: linear-gradient(125deg, #ffe6b8 0%, #ebb15e 42%, #cd8f3e 100%);
  box-shadow: 0 7px 16px rgba(92, 53, 14, 0.34), inset 0 1px 0 rgba(255, 248, 225, 0.75);
  letter-spacing: 0.03em;
  line-height: 1;
}

.game-title span {
  display: inline-block;
  text-transform: uppercase;
  font-weight: 900;
  font-size: 24px;
  color: #fff7e7;
  text-shadow:
    0 1px 0 rgba(76, 43, 12, 0.9),
    0 2px 0 rgba(76, 43, 12, 0.82),
    0 3px 0 rgba(76, 43, 12, 0.72),
    0 4px 8px rgba(44, 22, 5, 0.4);
}

.game-title span:first-child {
  color: #6d3d08;
  background: linear-gradient(180deg, #fff6de 0%, #ffd786 80%);
  border-radius: 999px;
  padding: 2px 8px 3px;
  text-shadow: 0 1px 0 rgba(255, 255, 255, 0.65), 0 2px 3px rgba(87, 55, 16, 0.3);
}

.game-title span:last-child {
  padding-right: 3px;
}

.title p {
  margin: 0;
  color: var(--muted);
  font-size: 14px;
}

.scoreboard {
  text-align: right;
  display: grid;
  gap: 4px;
}

.scoreboard span {
  color: var(--muted);
  font-size: 12px;
  text-transform: uppercase;
  letter-spacing: 0.08em;
}

.scoreboard strong {
  font-size: 28px;
}

.stage {
  position: relative;
  display: flex;
  justify-content: center;
  align-items: center;
  padding: 12px;
  background: #e7d0a8;
  border-radius: 12px;
  border: 1px solid var(--border);
  overflow: hidden;
  min-height: 0;
}

.stage::before {
  content: "";
  position: absolute;
  inset: 0;
  pointer-events: none;
  background:
    radial-gradient(circle at 14px 10px, rgba(74, 52, 22, 0.12) 0 6px, transparent 7px),
    radial-gradient(circle at 24px 6px, rgba(74, 52, 22, 0.1) 0 4px, transparent 5px),
    radial-gradient(circle at 7px 6px, rgba(74, 52, 22, 0.1) 0 4px, transparent 5px),
    radial-gradient(circle at 19px 19px, rgba(74, 52, 22, 0.13) 0 7px, transparent 8px);
  background-size: 88px 88px;
  opacity: 0.18;
}

.stage::after {
  content: "";
  position: absolute;
  inset: 0;
  pointer-events: none;
  z-index: 2;
  opacity: 0;
  background:
    radial-gradient(circle at 16% 18%, rgba(255, 74, 152, 0.35) 0, rgba(255, 74, 152, 0) 46%),
    radial-gradient(circle at 78% 82%, rgba(255, 150, 43, 0.35) 0, rgba(255, 150, 43, 0) 44%),
    repeating-linear-gradient(
      180deg,
      rgba(255, 255, 255, 0.1) 0 2px,
      rgba(255, 255, 255, 0) 2px 8px
    );
}

canvas {
  width: 100%;
  height: 100%;
  max-width: none;
  aspect-ratio: 1 / 1;
  display: block;
  background: #789d63;
  border-radius: 8px;
  border: 1px solid #587447;
  position: relative;
  z-index: 1;
  touch-action: none;
}

body.rage-mode .shell {
  border-color: #a1451c;
  box-shadow:
    0 0 0 2px rgba(255, 170, 66, 0.65),
    0 22px 42px rgba(115, 30, 10, 0.4),
    0 0 48px rgba(255, 66, 157, 0.26);
  animation: rage-shell 0.24s steps(2) infinite;
}

body.rage-mode .stage {
  border-color: #9c3219;
  box-shadow:
    inset 0 0 0 2px rgba(255, 246, 191, 0.45),
    0 0 0 2px rgba(255, 102, 163, 0.26);
}

body.rage-mode .stage::after {
  opacity: 0.72;
  animation: rage-scan 0.32s linear infinite;
}

body.rage-mode canvas {
  filter: saturate(1.42) contrast(1.18) brightness(1.06);
}

body.rage-mode .rage-indicator {
  animation: rage-indicator-pulse 0.35s ease-in-out infinite alternate;
  box-shadow:
    0 0 0 2px rgba(255, 246, 191, 0.64),
    0 0 26px rgba(255, 51, 166, 0.58);
}

body.rage-mode .game-title {
  border-color: #8b2f14;
  background: linear-gradient(120deg, var(--rage-1) 0%, var(--rage-2) 48%, var(--rage-3) 100%);
  animation: rage-title 0.26s steps(2) infinite;
}

body.rage-mode .game-title span {
  color: #fff;
}

@keyframes rage-shell {
  0% {
    transform: translate3d(0, 0, 0);
  }
  50% {
    transform: translate3d(-1px, 1px, 0);
  }
  100% {
    transform: translate3d(1px, -1px, 0);
  }
}

@keyframes rage-scan {
  0% {
    background-position: 0 0, 0 0, 0 0;
  }
  100% {
    background-position: 8px -8px, -8px 8px, 0 14px;
  }
}

@keyframes rage-indicator-pulse {
  from {
    transform: scale(1);
  }
  to {
    transform: scale(1.06);
  }
}

@keyframes rage-title {
  0% {
    transform: translate3d(0, 0, 0);
  }
  100% {
    transform: translate3d(1px, -1px, 0);
  }
}

.overlay {
  position: absolute;
  inset: 12px;
  background: rgba(31, 21, 10, 0.48);
  border-radius: 12px;
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 3;
}

.rage-popup {
  position: absolute;
  inset: 12px;
  z-index: 4;
  display: grid;
  place-items: center;
  background: rgba(20, 10, 4, 0.55);
}

.chaser-alert {
  position: absolute;
  top: 14px;
  left: 50%;
  transform: translateX(-50%);
  z-index: 5;
  pointer-events: none;
  width: min(94%, 560px);
  padding: 10px 14px;
  border-radius: 11px;
  border: 2px solid #5e3114;
  background: linear-gradient(135deg, #ffe4ae 0%, #f6b95d 52%, #cf853a 100%);
  color: #301a0a;
  text-align: center;
  box-shadow: 0 8px 24px rgba(42, 21, 7, 0.35);
}

.chaser-alert strong {
  display: block;
  font-size: 13px;
  letter-spacing: 0.04em;
}

.chaser-alert span {
  display: block;
  margin-top: 3px;
  font-size: 24px;
  font-weight: 900;
  letter-spacing: 0.08em;
}

.chaser-alert.pop {
  animation: chaser-alert-pop 0.4s ease-out;
}

@keyframes chaser-alert-pop {
  0% {
    transform: translateX(-50%) translateY(-10px) scale(0.95);
    opacity: 0;
  }
  100% {
    transform: translateX(-50%) translateY(0) scale(1);
    opacity: 1;
  }
}

.rage-card {
  width: min(420px, 88%);
  border-radius: 14px;
  padding: 16px;
  text-align: center;
  background: linear-gradient(130deg, #ff79bd 0%, #ffd95a 30%, #6aff8f 55%, #6ea7ff 78%, #b06eff 100%);
  border: 3px solid #fff8d8;
  box-shadow: 0 16px 36px rgba(0, 0, 0, 0.35);
  animation: rage-card 0.7s ease-in-out infinite alternate;
}

.rage-card h2 {
  margin: 0 0 10px 0;
  font-size: 42px;
  line-height: 1;
  color: #2f1708;
  text-shadow: 0 2px 0 #fff8d8;
}

.rage-card img {
  width: 190px;
  height: 190px;
  object-fit: cover;
  border-radius: 50%;
  border: 4px solid #fff8d8;
  animation: rage-face 0.4s ease-in-out infinite alternate;
}

@keyframes rage-card {
  from {
    transform: scale(1) rotate(-1.5deg);
  }
  to {
    transform: scale(1.06) rotate(1.5deg);
  }
}

@keyframes rage-face {
  from {
    transform: rotate(-14deg);
    filter: saturate(1.1);
  }
  to {
    transform: rotate(14deg);
    filter: saturate(1.5);
  }
}

.menu-card {
  width: min(360px, calc(100% - 24px));
  padding: 20px;
  border-radius: 12px;
  background: #f9f0dd;
  border: 1px solid #b59463;
  box-shadow: 0 10px 30px rgba(45, 28, 10, 0.2);
  display: grid;
  gap: 12px;
  text-align: center;
}

.overlay h2 {
  margin: 0;
  font-size: 24px;
  color: #5d3a15;
}

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

.menu-scores {
  border: 1px solid var(--border);
  border-radius: 10px;
  background: rgba(255, 248, 231, 0.8);
  padding: 10px;
  text-align: left;
}

.menu-scores h3 {
  margin: 0 0 6px 0;
  font-size: 14px;
  color: #5d3a15;
}

.menu-highscore-list {
  margin: 0;
  padding-left: 18px;
  min-height: 88px;
  display: grid;
  gap: 4px;
  color: #5d3a15;
}

.menu-highscore-list li.top5-new {
  background: linear-gradient(90deg, rgba(255, 218, 122, 0.55), rgba(255, 244, 201, 0.3));
  border-radius: 8px;
  padding: 4px 6px;
  animation: top5-pop 0.9s ease;
}

.score-name-input {
  width: 128px;
  border: 1px solid #9f712a;
  border-radius: 6px;
  padding: 3px 6px;
  font-size: 13px;
  background: #fff9ec;
  color: #3b2a16;
}

@keyframes top5-pop {
  0% {
    transform: scale(0.96);
    box-shadow: 0 0 0 rgba(255, 221, 128, 0);
  }
  55% {
    transform: scale(1.02);
    box-shadow: 0 0 0 8px rgba(255, 221, 128, 0.24);
  }
  100% {
    transform: scale(1);
    box-shadow: 0 0 0 rgba(255, 221, 128, 0);
  }
}

.name-entry {
  display: grid;
  gap: 6px;
  text-align: left;
}

.name-entry label {
  font-size: 13px;
  color: #5d3a15;
}

.name-entry input {
  width: 100%;
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 8px 10px;
  background: #fffdf7;
  color: #3b2a16;
}

.controls {
  display: flex;
  justify-content: center;
  gap: 16px;
  align-items: center;
}

button {
  background: #c2954f;
  border: 1px solid var(--border);
  color: #fff8eb;
  border-radius: 10px;
  padding: 10px 14px;
  font-size: 14px;
  cursor: pointer;
  min-width: 64px;
  touch-action: manipulation;
  -webkit-tap-highlight-color: transparent;
  user-select: none;
}

button:active {
  transform: translateY(1px);
}

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

@media (max-width: 640px) {
  body {
    align-items: stretch;
    justify-content: flex-start;
    padding: 8px;
    padding-top: max(8px, env(safe-area-inset-top));
    padding-bottom: max(8px, env(safe-area-inset-bottom));
  }

  .shell {
    height: calc(100vh - 8px);
    height: calc(100dvh - 8px);
    max-height: calc(100vh - 8px);
    max-height: calc(100dvh - 8px);
    padding: 12px;
    gap: 12px;
    border-radius: 12px;
  }

  .topbar {
    display: grid;
    grid-template-columns: 1fr auto;
    grid-template-areas:
      "title score"
      "rage audio";
    gap: 8px 10px;
    align-items: center;
  }

  .title {
    grid-area: title;
  }

  .title h1 {
    margin-bottom: 2px;
  }

  .game-title {
    padding: 5px 10px 7px 9px;
    gap: 4px;
  }

  .game-title span {
    font-size: 18px;
  }

  .title p {
    font-size: 12px;
  }

  .scoreboard {
    grid-area: score;
    text-align: right;
  }

  .scoreboard strong {
    font-size: 24px;
  }

  .rage-indicator {
    grid-area: rage;
    min-width: 118px;
    justify-self: start;
  }

  .audio-toggle {
    grid-area: audio;
    justify-self: end;
  }

  .controls {
    justify-content: center;
  }

  .actions {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 10px;
    width: 100%;
  }

  .actions button {
    min-width: 0;
    width: 100%;
    min-height: 46px;
    font-size: 16px;
  }

  .stage {
    padding: 6px;
  }

  .overlay,
  .rage-popup {
    inset: 6px;
  }

  .chaser-alert {
    top: 8px;
    padding: 8px 10px;
  }

  .chaser-alert strong {
    font-size: 11px;
  }

  .chaser-alert span {
    font-size: 18px;
  }
}

@media (max-width: 420px) {
  .title p {
    display: none;
  }

  .actions {
    grid-template-columns: 1fr 1fr;
    width: 100%;
  }

  .actions button {
    width: 100%;
    min-width: 0;
  }

}
