:root {
  color-scheme: dark;
  font-family: "Yu Gothic UI", "Hiragino Kaku Gothic ProN", sans-serif;
  background: #11161b;
  color: #fffdf4;
}

* {
  box-sizing: border-box;
}

html,
body,
#game {
  width: 100%;
  height: 100%;
  margin: 0;
  overflow: hidden;
}

body {
  user-select: none;
  touch-action: none;
}

#game::before,
#game::after {
  content: "";
  position: fixed;
  inset: 0;
  pointer-events: none;
}

#game::after {
  z-index: 2;
  background: transparent;
}

#game::before {
  z-index: 3;
  opacity: 0;
  background:
    repeating-linear-gradient(0deg, rgb(0 255 210 / 22%) 0 2px, transparent 2px 7px),
    linear-gradient(90deg, rgb(255 0 65 / 18%), transparent 28%, transparent 72%, rgb(0 255 214 / 16%));
  mix-blend-mode: screen;
  transition: opacity 180ms ease;
}

body.chase #game::before {
  opacity: 0.1;
}

body.tv-glitch #game::before {
  opacity: 0.22;
  background:
    repeating-linear-gradient(0deg, rgb(103 255 225 / 24%) 0 1px, transparent 1px 5px),
    linear-gradient(90deg, rgb(255 27 66 / 30%), transparent 30%, transparent 67%, rgb(18 255 220 / 27%));
  animation: tv-tear 0.9s steps(1, end) infinite;
}

body.tv-glitch #scene {
  animation: scene-bug 1.15s steps(1, end) infinite;
}

#signal-tear {
  position: fixed;
  z-index: 3;
  inset: -16vh 0 auto;
  height: 15vh;
  opacity: 0;
  pointer-events: none;
  background:
    linear-gradient(90deg, transparent, rgb(20 255 219 / 34%) 18%, rgb(235 255 250 / 44%) 49%, rgb(255 26 72 / 31%) 72%, transparent);
  box-shadow:
    0 9px 18px rgb(0 255 205 / 12%),
    0 -7px 16px rgb(255 18 55 / 12%);
  mix-blend-mode: screen;
}

body.tv-glitch #signal-tear {
  animation: tear-roll 1.06s linear infinite;
}

body.wrong #game::before {
  opacity: 0.28;
  animation: signal-break 180ms steps(2, end) 3;
}

body.caught #game::before {
  opacity: 0.78;
  background:
    repeating-linear-gradient(0deg, rgb(255 30 62 / 36%) 0 3px, transparent 3px 10px),
    linear-gradient(90deg, rgb(255 0 30 / 35%), rgb(0 0 0 / 84%), rgb(0 255 214 / 24%));
  animation: signal-break 130ms steps(2, end) 5;
}

body.washed #game::before {
  opacity: 0.82;
  background:
    radial-gradient(circle at 50% 72%, rgb(255 170 160 / 58%), transparent 44%),
    linear-gradient(180deg, rgb(150 35 42 / 72%), rgb(28 3 6 / 92%));
  animation: wash-hit 1.1s ease-out;
}

body.advance #game::after {
  animation: round-change 1000ms ease-in-out;
}

@keyframes signal-break {
  0% { transform: translate(0, 0); filter: none; }
  33% { transform: translate(-7px, 2px); filter: hue-rotate(60deg) contrast(1.5); }
  66% { transform: translate(6px, -1px); filter: hue-rotate(-40deg); }
  100% { transform: translate(0, 0); filter: none; }
}

@keyframes tv-tear {
  0%, 58%, 100% { transform: translate(0, 0); filter: none; }
  59% { transform: translate(-11px, 1px); filter: hue-rotate(75deg) contrast(1.8); }
  64% { transform: translate(9px, -2px); filter: hue-rotate(-48deg) contrast(1.4); }
  70% { transform: translate(-3px, 0); filter: brightness(1.8); }
}

@keyframes scene-bug {
  0%, 64%, 100% { transform: translate(0, 0) scale(1); filter: none; }
  65% { transform: translateX(7px) scale(1.004); filter: contrast(1.2) saturate(1.4); }
  68% { transform: translateX(-5px) scale(1.006); filter: contrast(1.35) hue-rotate(8deg); }
  71% { transform: translateX(2px) scale(1.002); filter: brightness(1.18); }
  74% { transform: translate(0, 0) scale(1); filter: none; }
}

@keyframes tear-roll {
  0% { opacity: 0; transform: translateY(0); }
  12% { opacity: 0.65; }
  62% { opacity: 0.28; }
  88% { opacity: 0.58; }
  100% { opacity: 0; transform: translateY(116vh); }
}

/* The corridor is rebuilt while the screen is black, so the walk into the
   next stretch does not visibly jump back to the start. */
@keyframes round-change {
  0% { background-color: transparent; }
  32% { background-color: rgb(0 0 0 / 97%); }
  70% { background-color: rgb(0 0 0 / 97%); }
  100% { background-color: transparent; }
}

@keyframes wash-hit {
  0% { transform: translateY(-22%); opacity: 0; }
  18% { transform: translateY(0); opacity: 0.86; }
  100% { transform: translateY(14%); opacity: 0.38; }
}

#scene {
  width: 100%;
  height: 100%;
  display: block;
  cursor: grab;
}

#scene:active {
  cursor: grabbing;
}

#hud {
  position: fixed;
  z-index: 4;
  inset: 18px 18px auto;
  display: flex;
  justify-content: space-between;
  gap: 18px;
  align-items: center;
  pointer-events: none;
  text-shadow: 0 2px 8px #000;
}

#round-label,
#hint {
  padding: 10px 16px;
  border: 1px solid rgb(255 255 255 / 26%);
  border-radius: 999px;
  background: rgb(14 19 23 / 76%);
  font-size: clamp(15px, 2vw, 22px);
  font-weight: 700;
}

#hint {
  text-align: right;
}

#crosshair {
  position: fixed;
  z-index: 4;
  left: 50%;
  top: 50%;
  width: 12px;
  height: 12px;
  border: 2px solid rgb(255 252 229 / 82%);
  border-radius: 50%;
  transform: translate(-50%, -50%);
  pointer-events: none;
  box-shadow: 0 0 10px #000;
}

#move-buttons {
  position: fixed;
  z-index: 4;
  inset: auto 0 28px;
  display: flex;
  justify-content: center;
  flex-wrap: wrap;
  gap: 12px;
  pointer-events: none;
}

#walk-guide {
  position: fixed;
  z-index: 4;
  left: 50%;
  bottom: 108px;
  display: grid;
  justify-items: center;
  gap: 1px;
  padding: 7px 15px 5px;
  border: 2px solid rgb(255 226 113 / 78%);
  border-radius: 16px;
  background: rgb(14 19 23 / 82%);
  color: #fff3b5;
  font-size: 14px;
  font-weight: 800;
  line-height: 1.25;
  pointer-events: none;
  transform: translateX(-50%);
  transition: opacity 500ms ease, visibility 500ms ease;
}

#walk-guide.dismissed {
  opacity: 0;
  visibility: hidden;
}

#walk-guide .down-arrow {
  color: #ffe273;
  font-size: 25px;
  line-height: 1;
}

body:has(#overlay.visible) #walk-guide,
body:has(#overlay.visible) #move-buttons {
  opacity: 0.18;
}

#move-buttons button,
.main-button {
  border: 0;
  border-radius: 18px;
  font: inherit;
  font-size: 22px;
  font-weight: 800;
  color: #171b1e;
  background: #fff6cf;
  box-shadow: 0 8px 24px rgb(0 0 0 / 34%);
}

#move-buttons button {
  width: min(28vw, 160px);
  min-height: 64px;
  pointer-events: auto;
  touch-action: none;
}

#move-buttons .turn-button {
  background: #d7e6df;
}

body.corner-ready #move-buttons .turn-button {
  background: #ffe48d;
  box-shadow: 0 0 0 4px rgb(255 226 115 / 28%), 0 8px 24px rgb(0 0 0 / 34%);
  animation: turn-glow 700ms ease-in-out infinite alternate;
}

@keyframes turn-glow {
  from { box-shadow: 0 0 0 3px rgb(255 226 115 / 24%), 0 8px 24px rgb(0 0 0 / 34%); }
  to { box-shadow: 0 0 0 8px rgb(255 226 115 / 12%), 0 8px 24px rgb(0 0 0 / 34%); }
}

#move-buttons button:active,
.main-button:active {
  transform: translateY(2px);
  background: #ffe48d;
}

#toast {
  position: fixed;
  z-index: 5;
  left: 50%;
  top: 18%;
  min-width: min(80vw, 440px);
  padding: 18px 24px;
  border-radius: 20px;
  background: rgb(13 19 23 / 92%);
  font-size: clamp(20px, 3vw, 32px);
  font-weight: 800;
  text-align: center;
  opacity: 0;
  transform: translate(-50%, -12px);
  transition: opacity 180ms ease, transform 180ms ease;
  pointer-events: none;
}

#toast.show {
  opacity: 1;
  transform: translate(-50%, 0);
}

#toast.good {
  color: #d8ffb0;
}

#toast.bad {
  color: #ffd0b8;
}

#overlay {
  position: fixed;
  z-index: 10;
  inset: 0;
  display: grid;
  place-items: center;
  padding: 24px;
  background: rgb(8 12 15 / 64%);
  backdrop-filter: blur(5px);
  opacity: 0;
  visibility: hidden;
  transition: opacity 220ms ease, visibility 220ms ease;
}

#overlay.visible {
  opacity: 1;
  visibility: visible;
}

#overlay.goal .label,
#overlay.goal .keys,
#overlay.goal .pace-choice,
#overlay.goal .start-cue {
  display: none;
}

#overlay.goal > .quality-link {
  display: none;
}

#overlay.goal .paper {
  background: linear-gradient(145deg, #33422f, #171f19);
  border-color: rgb(255 226 115 / 48%);
}

#overlay.goal h1 {
  color: #ffe273;
  text-shadow: 0 8px 30px rgb(255 200 80 / 28%);
}

.paper {
  width: min(92vw, 620px);
  max-height: calc(100vh - 24px);
  overflow-y: auto;
  padding: clamp(26px, 5vw, 52px);
  border: 1px solid rgb(255 255 255 / 18%);
  border-radius: 28px;
  background: linear-gradient(145deg, #26313a, #171e24);
  box-shadow: 0 28px 70px rgb(0 0 0 / 50%);
  text-align: center;
}

.label {
  margin: 0 0 6px;
  color: #ffd977;
  font-size: 15px;
  font-weight: 800;
  letter-spacing: 0.12em;
}

h1 {
  margin: 0;
  font-size: clamp(42px, 9vw, 84px);
  letter-spacing: 0.08em;
}

#overlay-message {
  margin: 22px auto;
  font-size: clamp(18px, 3vw, 25px);
  line-height: 1.9;
}

.keys {
  display: grid;
  gap: 6px;
  margin: 24px 0;
  color: #d9e3e9;
  font-size: clamp(14px, 2.2vw, 18px);
}

.main-button {
  min-width: 190px;
  min-height: 64px;
  padding: 14px 30px;
}

.quality-link {
  display: block;
  width: max-content;
  max-width: 100%;
  margin: 20px auto 0;
  padding: 9px 18px;
  color: #bcded7;
  background: rgb(188 222 215 / 8%);
  border: 1px solid rgb(188 222 215 / 42%);
  border-radius: 999px;
  font-size: clamp(14px, 2.2vw, 18px);
  text-decoration-color: rgb(188 222 215 / 45%);
  text-underline-offset: 4px;
}

.quality-link:hover {
  color: #f3f0df;
}

.quality-link:focus-visible {
  outline: 4px solid #8bd6ff;
  outline-offset: 4px;
}

#overlay > .quality-link {
  position: absolute;
  top: 24px;
  right: 24px;
  z-index: 11;
  margin: 0;
  color: #e7f0ea;
  background: rgb(24 37 34 / 82%);
  border-color: rgb(188 222 215 / 58%);
  box-shadow: 0 6px 20px rgb(0 0 0 / 34%);
  backdrop-filter: blur(5px);
}

.start-cue {
  display: grid;
  justify-items: center;
  gap: 2px;
  margin: 20px 0 8px;
  color: #ffe273;
  font-size: clamp(17px, 2.6vw, 22px);
  font-weight: 800;
}

.start-cue span {
  font-size: 30px;
  line-height: 1;
  animation: cue-bounce 900ms ease-in-out infinite alternate;
}

@keyframes cue-bounce {
  from { transform: translateY(-2px); }
  to { transform: translateY(5px); }
}

.main-button:focus-visible,
#move-buttons button:focus-visible {
  outline: 4px solid #8bd6ff;
  outline-offset: 4px;
}

@media (min-width: 601px) and (hover: hover) and (pointer: fine) {
  #move-buttons {
    bottom: 12px;
    gap: 8px;
  }

  #move-buttons button {
    width: 100px;
    min-height: 46px;
    border-radius: 12px;
    font-size: 16px;
    box-shadow: 0 4px 12px rgb(0 0 0 / 30%);
  }

  #walk-guide {
    bottom: 66px;
    padding: 4px 10px 3px;
    font-size: 12px;
  }

  #walk-guide .down-arrow {
    font-size: 15px;
  }
}

@media (max-width: 600px) {
  #hud {
    align-items: flex-start;
    flex-direction: column;
    gap: 8px;
  }

  #hint {
    text-align: left;
  }

  #walk-guide {
    bottom: 156px;
    font-size: 12px;
  }

  #move-buttons button {
    width: min(42vw, 150px);
  }
}

@media (max-height: 800px) {
  .paper { padding: clamp(18px, 3vw, 34px); }
  h1 { font-size: clamp(38px, 7vw, 62px); }
  #overlay-message {
    margin: 12px auto;
    font-size: clamp(16px, 2.4vw, 21px);
    line-height: 1.55;
  }
  .keys { gap: 3px; margin: 12px 0; }
  .start-cue { margin: 10px 0 6px; font-size: 17px; }
}

@media (max-height: 620px) {
  #overlay { padding: 12px; }
  .paper { max-height: calc(100vh - 24px); overflow-y: auto; padding: 18px; touch-action: pan-y; }
  h1 { font-size: 42px; }
  #overlay-message { margin: 10px auto; line-height: 1.5; }
  .keys { margin: 10px 0; }
}

.pace-choice {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  margin: 10px 0 0;
  font-size: 16px;
  font-weight: 700;
  color: #e7eee9;
  cursor: pointer;
}

.pace-choice input {
  width: 20px;
  height: 20px;
  accent-color: #e4b52d;
  cursor: pointer;
}

#sound-toggle {
  position: fixed;
  top: 58px;
  right: 14px;
  z-index: 6;
  padding: 8px 14px;
  border: 1px solid #ffffff40;
  border-radius: 999px;
  background: #10171cd9;
  color: #eef3f1;
  font: 700 14px "Yu Gothic UI", "Meiryo", sans-serif;
  cursor: pointer;
  pointer-events: auto;
}

#sound-toggle:hover {
  background: #1b262bd9;
}
