:root {
  color-scheme: dark;
  --bg: #090b0d;
  --ink: #f4e5cd;
  --quiet: #a0a6a8;
  --red: #ff514a;
  --line: #344149;
  font-family: system-ui, "PingFang SC", sans-serif;
  background: var(--bg);
  color: var(--ink);
}

* { box-sizing: border-box; }

body {
  min-height: 100svh;
  margin: 0;
  background:
    radial-gradient(ellipse at 75% 20%, #40141570, transparent 44%),
    repeating-linear-gradient(90deg, #cfddf206 0 1px, transparent 1px 55px),
    repeating-linear-gradient(0deg, #cfddf206 0 1px, transparent 1px 55px),
    radial-gradient(ellipse at 35% 44%, #131b20, #08090b 72%);
}

.topbar {
  display: grid;
  grid-template-columns: 1fr auto 1fr;
  align-items: center;
  gap: 8px;
  margin-bottom: 10px;
}

#level-label {
  justify-self: start;
  max-width: 100%;
  min-height: 44px;
  display: grid;
  align-items: center;
  color: var(--ink);
  font: inherit;
  font-size: .81rem;
  padding-inline: 4px;
  white-space: nowrap;
}

#sound {
  justify-self: end;
  min-height: 44px;
  padding-inline: 7px;
  white-space: nowrap;
  font-size: .81rem;
}

button {
  font: inherit;
  color: inherit;
  cursor: pointer;
  min-height: 42px;
  border: 1px solid #f4e5cd48;
  border-radius: 4px;
  background: #101315c9;
  padding: 0 16px;
  transition: background-color .15s, transform .15s;
}

button:hover { background: #252529; }
button:active { transform: scale(.96); }
button:focus-visible { outline: 2px solid #ffd593; outline-offset: 4px; }

main {
  --top: max(12px, env(safe-area-inset-top));
  --bottom: max(12px, env(safe-area-inset-bottom));
  --usable-height: calc(100svh - var(--top) - var(--bottom));
  --gutter: max(12px, env(safe-area-inset-left), env(safe-area-inset-right));
  --board-width: 100%;
  width: min(calc(100% - 2 * var(--gutter)), 1080px, calc((var(--usable-height) - 120px) * 2.625));
  margin: 0 auto;
  padding: var(--top) 0 var(--bottom);
  text-align: center;
}

h1, h2 {
  font-family: "Songti SC", "Noto Serif CJK SC", SimSun, serif;
}

h1 {
  margin: 0;
  font-size: clamp(1.15rem, 5.5vw, 1.90rem);
  font-weight: 800;
  letter-spacing: .05em;
  white-space: nowrap;
  line-height: 1.18;
  text-shadow: 0 4px 30px #000a;
}

#touch-field {
  position: relative;
  touch-action: none;
  user-select: none;
}

#board {
  position: relative;
  isolation: isolate;
  width: var(--board-width);
  margin-inline: auto;
  aspect-ratio: var(--columns, 21) / var(--rows, 8);
  border-radius: 10px;
  overflow: hidden;
  outline: 1px solid #526466;
  background:
    radial-gradient(ellipse at var(--exit-x) var(--exit-y), #3b1216a0 0, transparent 33%),
    linear-gradient(#080b0f57, #080b0f7d),
    #10171b url("assets/office-floor.webp?v=90") center / cover;
  box-shadow: 0 18px 64px #0009, inset 0 0 45px #0009;
  user-select: none;
  touch-action: none;
}

#tiles {
  position: absolute;
  inset: 0;
  display: grid;
  grid-template: repeat(var(--rows), 1fr) / repeat(var(--columns), 1fr);
}

#corridor, #threats {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  pointer-events: none;
}

.tile {
  grid-area: var(--row) / var(--col);
  border: 1px solid #5a75834e;
  background:
    repeating-linear-gradient(128deg, #ffffff06 0 1px, transparent 1px 4px),
    radial-gradient(ellipse at 30% 26%, #2b3c44, #18252d);
  box-shadow: inset 0 0 8px #0d131766;
}

.exit {
  display: grid;
  place-items: center;
  background: #36181b;
  border-color: #aa474580;
}

.exit::before {
  content: "";
  width: 46%;
  height: 72%;
  border: 2px solid var(--red);
  background: radial-gradient(circle at 75% 55%, #f88a78 0 5%, transparent 9%), #311215;
  box-shadow: 0 0 16px #ff352b82, inset 0 0 9px #ff352b41;
}

#player, #start, #gate, #guide {
  position: absolute;
  transform: translate(-50%, -50%);
}

#player {
  width: calc(var(--dot) * 100% / var(--columns));
  aspect-ratio: 1;
  border-radius: 50%;
  background: #fff0d5;
  box-shadow: 0 0 8px 1px #ffd396bd, 0 0 23px 4px #fac88e4d;
  pointer-events: none;
}

#gate {
  width: max(8px, calc(var(--gate) * 100% / var(--columns)));
  height: max(10px, calc(var(--gate) * 123% / var(--rows)));
  border: 1px solid var(--red);
  background: radial-gradient(circle at 72% 55%, #ffa68a 0 7%, transparent 12%), #351618;
  box-shadow: 0 0 13px #ff352b9c, inset 0 0 8px #ff352b55;
  pointer-events: none;
}

#guide {
  color: #ffe1a3;
  font: 700 .88rem/1 system-ui;
  text-shadow: 0 0 6px #1c0e02, 0 0 8px #ba641f;
  pointer-events: none;
}

#start {
  min-height: 0;
  width: calc(100% / var(--columns));
  height: calc(100% / var(--rows));
  padding: 0;
  background: transparent;
  border: 1px solid #ffe1af4f;
  border-radius: 0;
  animation: pulse 2s ease-in-out infinite alternate;
}

#start:hover { background: #ffe3a516; }
#start:active { background: #ffe3a536; }
#touch-intro {
  position: absolute;
  left: 50%;
  bottom: 10%;
  translate: -50% 0;
  min-height: 44px;
  white-space: nowrap;
  background: #182121;
  border-color: #9bac9f;
}
#board[data-mouse] { cursor: none; }

#touch-stick {
  display: none;
  position: absolute;
  left: var(--stick-x, max(32px, calc((100% - var(--board-width)) * .5 - 5px)));
  top: var(--stick-y, calc(100% - 38px));
  translate: -50% -50%;
  width: 61px;
  height: 61px;
  border: 1px solid #9cb0ad7d;
  border-radius: 50%;
  background: #101b1a8c;
  pointer-events: none;
}

#touch-stick span {
  position: absolute;
  inset: 50% auto auto 50%;
  width: 22px;
  height: 22px;
  border-radius: 50%;
  background: #bdccc2ae;
  translate: calc(-50% + var(--thumb-x, 0px)) calc(-50% + var(--thumb-y, 0px));
}

#touch-stick[data-held] {
  display: block;
  border-color: #c6d9ccba;
  opacity: .70;
}

#help {
  margin: 10px 0 7px;
  min-height: 1.55em;
  font-size: .82rem;
  line-height: 1.55;
  letter-spacing: .04em;
  color: var(--quiet);
}

.progress {
  display: flex;
  justify-content: space-between;
  gap: 8px;
  color: #a9b5b0;
  font-size: .75rem;
  letter-spacing: .03em;
}

#control-status { color:#e0befd; font-weight:650; white-space:nowrap; }

dialog { color: var(--ink); text-align: center; }
dialog::backdrop { background: #08090ae0; }

#caught {
  position: fixed;
  inset: 0;
  width: 100%;
  height: 100%;
  max-width: none;
  max-height: none;
  padding: 0;
  margin: 0;
  border: 0;
  overflow: hidden;
  background: #0a130d;
}

.surprise {
  display: block;
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.surprise[hidden] { display: none; }

.caught-caption {
  position: absolute;
  inset: auto 0 0;
  display: grid;
  justify-items: center;
  padding: 40px 16px max(22px, env(safe-area-inset-bottom));
  background: linear-gradient(transparent, #070b08ed);
}

.caught-caption h2 {
  margin: 0 0 11px;
  font-size: clamp(1.75rem, 5vw, 4rem);
  text-shadow: 0 2px 12px #301208;
}

.caught-caption button {
  background: #efe2bd;
  border-color: #efe2bd;
  color: #272a1e;
  font-weight: 700;
}

.caught-caption button:hover { background: #fff0c8; }

#caught[data-face="cute"] { background: #a14f2e; }

#caught[data-face="cute"] .caught-caption {
  background: linear-gradient(transparent, #34150eeb);
}

#caught[data-face="cute"] .caught-caption button {
  background: #ffdfae;
  border-color: #ffdfae;
  color: #432619;
}

#caught[data-face="cute"] .caught-caption button:hover { background: #fff0c8; }

#escaped {
  background: #12181a;
  border: 1px solid var(--line);
  padding: 25px 45px 30px;
}

#escaped h2 { font-size: 2.3rem; margin: 0; }
#escaped p { color: var(--quiet); margin: 12px 0 22px; }

@keyframes pulse { to { border-color: #ffdc9caf; } }

@media (orientation: portrait) {
  main {
    --board-width: min(100%, 344px, calc(min(69svh, var(--usable-height) - 117px) * .470588));
    width: min(calc(100% - 2 * var(--gutter)), 430px);
  }

  .topbar, .progress { padding-inline: 2px; }
  #help { min-height: 2lh; }
}

@media (pointer: coarse) {
  #touch-stick { display: block; }
  #start { width: max(44px, calc(100% / var(--columns))); height: max(44px, calc(100% / var(--rows))); }
}

@media (max-height: 520px) {
  .topbar { column-gap: 4px; }
  h1 { font-size: .92rem; }
  #level-label, #sound { font-size: .72rem; padding-inline: 2px; }
}

@media (prefers-reduced-motion: reduce) {
  #start { animation: none; }
  button { transition: none; }
}
