:root {
  --font: 'VT323', 'Courier New', monospace;
  --ink: #e8e8e8;
  --dim: #8a8a8a;
  --red: #c4161c;
  --green: #45ff6a;
  --safe-t: env(safe-area-inset-top, 0px);
  --safe-r: env(safe-area-inset-right, 0px);
  --safe-b: env(safe-area-inset-bottom, 0px);
  --safe-l: env(safe-area-inset-left, 0px);
}
* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body {
  margin: 0; height: 100%; background: #000; color: var(--ink);
  font-family: var(--font); overflow: hidden;
  touch-action: manipulation; user-select: none; -webkit-user-select: none;
  overscroll-behavior: none;
}
img { -webkit-user-drag: none; user-select: none; pointer-events: none; }
button { font-family: var(--font); cursor: pointer; touch-action: manipulation; }
#app { position: fixed; inset: 0; overflow: hidden; background: #000; }

.screen { position: absolute; inset: 0; display: none; overflow: hidden; }
.screen.active { display: block; }

/* ---------- shared overlays ---------- */
canvas.noise {
  position: absolute; inset: 0; width: 100%; height: 100%;
  pointer-events: none; mix-blend-mode: screen; opacity: .16; image-rendering: pixelated;
}
canvas.noise.strong { opacity: .55; mix-blend-mode: normal; }
.scanlines {
  position: absolute; inset: 0; pointer-events: none;
  background: repeating-linear-gradient(to bottom, rgba(0,0,0,0) 0 2px, rgba(0,0,0,.28) 2px 4px);
  animation: roll 7s linear infinite;
}
@keyframes roll { from { background-position: 0 0; } to { background-position: 0 200px; } }
.vignette {
  position: absolute; inset: 0; pointer-events: none;
  background: radial-gradient(ellipse at center, rgba(0,0,0,0) 45%, rgba(0,0,0,.85) 100%);
}
.vignette.heavy { background: radial-gradient(ellipse at 52% 50%, rgba(0,0,0,0) 30%, rgba(0,0,0,.92) 95%); }

/* ---------- MENU ---------- */
#menu { background: #000; }
.menu-photo { position: absolute; top: 0; right: 0; bottom: 0; width: 64%; overflow: hidden; }
.menu-photo img {
  position: absolute; inset: 0; width: 100%; height: 100%; object-fit: cover; object-position: 50% 30%;
  filter: grayscale(.85) contrast(1.35) brightness(.55) sepia(.15);
  -webkit-mask-image: linear-gradient(to right, transparent 0%, #000 38%);
          mask-image: linear-gradient(to right, transparent 0%, #000 38%);
  animation: twitchIdle 9s infinite;
}
#menuPhotoGhost { opacity: 0; mix-blend-mode: screen; filter: grayscale(1) contrast(2) brightness(.9) blur(1px); }
#menuPhotoGhost.on { opacity: .55; transform: translate(-6px, 2px) scale(1.04); }
.menu-photo.twitch img:first-child { transform: translate(4px,-3px) scale(1.03) skewX(-1deg); filter: grayscale(1) contrast(1.8) brightness(.8) invert(.08); }
@keyframes twitchIdle { 0%,100% { transform: scale(1.02); } 50% { transform: scale(1.06) translateX(-1%); } }

.menu-text {
  position: absolute; left: calc(5vw + var(--safe-l)); top: calc(7vh + var(--safe-t)); z-index: 3;
  display: flex; flex-direction: column; align-items: flex-start;
}
.menu-text h1 {
  margin: 0 0 4vh; font-weight: 400; font-size: clamp(42px, 8.5vh, 92px); line-height: .9;
  letter-spacing: 2px; text-shadow: 0 0 10px rgba(255,255,255,.25);
  animation: titleFlicker 6s infinite;
}
@keyframes titleFlicker { 0%,93%,100% { opacity: 1; } 94% { opacity: .4; } 95% { opacity: 1; } 97% { opacity: .6; } }
.menu-btn {
  background: none; border: none; color: var(--ink); font-size: clamp(30px, 5.5vh, 52px);
  padding: 6px 0; display: flex; align-items: center; gap: 12px; min-height: 48px;
}
.menu-btn.small { font-size: clamp(22px, 3.5vh, 32px); color: var(--dim); margin-top: 1vh; }
.menu-btn .arrow { opacity: 0; transition: opacity .1s; font-size: .8em; }
.menu-btn:hover .arrow, .menu-btn:focus-visible .arrow, .menu-btn.pulse .arrow { opacity: 1; }
.menu-btn:disabled { color: #444; cursor: default; }
.menu-btn:disabled .arrow { opacity: 0; }
#loadWrap { margin-bottom: 2vh; width: min(260px, 40vw); }
.load-label { font-size: 22px; color: var(--dim); }
.load-bar { height: 6px; background: #222; margin-top: 4px; }
#loadBar { height: 100%; width: 0; background: var(--ink); transition: width .2s; }
#stars { font-size: 34px; color: #ffd84a; text-shadow: 0 0 8px rgba(255,210,0,.6); min-height: 4px; }
.menu-foot { position: absolute; left: calc(5vw + var(--safe-l)); bottom: calc(3vh + var(--safe-b)); color: #555; font-size: 18px; z-index: 3; }

/* ---------- INTRO ---------- */
#intro { display: none; align-items: center; justify-content: center; background: #000; }
#intro.active { display: flex; }
.intro-text { text-align: center; font-size: clamp(30px, 6vh, 56px); animation: fadeInOut 3.2s forwards; }
.intro-text .big { font-size: 1.4em; }
@keyframes fadeInOut { 0% { opacity: 0; } 20%,75% { opacity: 1; } 100% { opacity: 0; } }

/* ---------- GAME VIEWS ---------- */
.view { position: absolute; inset: 0; overflow: hidden; }
#cams { display: none; }
#game.cams-up #cams { display: block; }
#game.cams-up #office { visibility: hidden; }

.fill-bg {
  position: absolute; inset: -40px; background-size: cover; background-position: center;
  filter: blur(26px) brightness(.18) saturate(.5);
}
.stage { position: absolute; left: 50%; top: 50%; transform: translate(-50%, -50%); overflow: hidden; }
.pan { position: absolute; inset: 0; transition: transform .25s ease-out; will-change: transform; }
.room { position: absolute; inset: 0; width: 100%; height: 100%; object-fit: cover; }

/* office lighting */
#office .base { filter: brightness(.3) contrast(1.15) saturate(.55); transition: opacity .12s; }
#officeClosedImg, #litClosed { opacity: 0; }
#game.door-closed #officeClosedImg, #game.door-closed #litClosed { opacity: 1; }
#game.door-closed #officeOpenImg, #game.door-closed #litOpen { opacity: 0; }
.lit { position: absolute; inset: 0; opacity: 0; transition: opacity .05s;
  -webkit-mask-image: radial-gradient(ellipse 34% 46% at 55% 52%, #000 25%, transparent 100%);
          mask-image: radial-gradient(ellipse 34% 46% at 55% 52%, #000 25%, transparent 100%); }
.lit .room { filter: brightness(.95) contrast(1.1) saturate(.6) sepia(.25); }
#game.light-on .lit { opacity: 1; }
#game.light-on.flick .lit { opacity: .55; }
.light-glow { position: absolute; inset: 0; pointer-events: none; opacity: 0;
  background: radial-gradient(ellipse 30% 40% at 55% 52%, rgba(255,235,190,.12), transparent 70%); mix-blend-mode: screen; }
#game.light-on .light-glow { opacity: 1; }

.chars { position: absolute; inset: 0; pointer-events: none; }
.char {
  position: absolute; transform: translateX(-50%); width: auto;
  transition: opacity .3s;
}
#officeChars .char { filter: brightness(.06) contrast(1.2); transition: filter .05s; }
#game.light-on #officeChars .char { filter: brightness(.85) contrast(1.15) saturate(.55) sepia(.25); }
#game.light-on #officeChars .char.far { filter: brightness(.55) contrast(1.1) saturate(.4) sepia(.2) blur(.4px); }
#game.door-closed #officeChars { display: none; }
#officeChars .char.poface { animation: poFlick .9s steps(1) infinite; }
@keyframes poFlick {
  0%   { filter: brightness(.05); } 20% { filter: brightness(.7) contrast(1.4) grayscale(.6); }
  35%  { filter: brightness(.05); } 60% { filter: brightness(.55) contrast(1.4) grayscale(.6); }
  70%  { filter: brightness(.04); } 100% { filter: brightness(.04); }
}
#game.blackout #officeChars { display: none; }
.char.breath { animation: breath 3.2s ease-in-out infinite; transform-origin: 50% 100%; }
@keyframes breath { 0%,100% { transform: translateX(-50%) scaleY(1); } 50% { transform: translateX(-50%) scaleY(1.008); } }

.dark-overlay { position: absolute; inset: 0; background: #000; opacity: 0; pointer-events: none; transition: opacity .6s; }
#game.power-out .dark-overlay { opacity: .82; }
#game.power-out.blackout .dark-overlay { opacity: 1; transition: opacity .1s; }
#game.power-out #office .base { filter: brightness(.14) contrast(1.2) saturate(.3); }

/* cameras */
#camStage { filter: grayscale(1) brightness(.6) contrast(1.35); }
.cam-fill { filter: blur(26px) brightness(.15) grayscale(1); }
.cam-pan { animation: camPan 14s ease-in-out infinite alternate; }
@keyframes camPan { from { transform: scale(1.12) translateX(-4.5%); } to { transform: scale(1.12) translateX(4.5%); } }
#camStage.glitch { animation: glitch .12s steps(2) infinite; }
@keyframes glitch { 0% { transform: translate(-50%,-50%) translateX(-6px); } 100% { transform: translate(-50%,-50%) translateX(7px) skewX(2deg); } }
.cam-static { position: absolute; inset: 0; pointer-events: none; opacity: 0; background: #888; mix-blend-mode: normal; }
.cam-border { position: absolute; inset: calc(10px + var(--safe-t)) calc(10px + var(--safe-r)) calc(10px + var(--safe-b)) calc(10px + var(--safe-l));
  border: 2px solid rgba(255,255,255,.35); pointer-events: none; }
.cam-label { position: absolute; left: calc(26px + var(--safe-l)); top: calc(20px + var(--safe-t)); font-size: clamp(22px, 4vh, 36px);
  display: flex; align-items: center; gap: 10px; text-shadow: 0 0 6px #000; }
.rec { width: 16px; height: 16px; border-radius: 50%; background: #e00; animation: blink 1s steps(1) infinite; box-shadow: 0 0 8px #f00; }
@keyframes blink { 50% { opacity: 0; } }
.cam-disrupted { position: absolute; left: 0; right: 0; top: 42%; text-align: center; font-size: clamp(26px, 5vh, 46px);
  display: none; letter-spacing: 3px; text-shadow: 0 0 8px #000; }
#cams.disrupted .cam-disrupted { display: block; }
#cams.disrupted #camStage { visibility: hidden; }
#cams.disrupted .cam-static { opacity: .9; }

.map-wrap {
  position: absolute; right: calc(18px + var(--safe-r)); bottom: calc(76px + var(--safe-b));
  height: min(52vh, 430px); aspect-ratio: 850 / 1180;
}
#map { width: 100%; height: 100%; overflow: visible; }
#map .room-shape { fill: rgba(255,255,255,.05); stroke: rgba(255,255,255,.75); stroke-width: 6; }
#map .room-tag { fill: rgba(255,255,255,.45); font-family: var(--font); font-size: 46px; }
#map .cam-btn rect { fill: #3a3a3a; stroke: #cfcfcf; stroke-width: 5; transition: fill .1s; }
#map .cam-btn text { fill: #fff; font-family: var(--font); font-size: 40px; pointer-events: none; }
#map .cam-btn { cursor: pointer; }
#map .cam-btn:hover rect { fill: #5a5a5a; }
#map .cam-btn.sel rect { fill: #3f9a35; animation: selBlink .8s steps(1) infinite; }
@keyframes selBlink { 50% { fill: #2b6a24; } }
#map .you rect { fill: #fff; } #map .you text { fill: #000; font-family: var(--font); font-size: 40px; }

/* ---------- HUD ---------- */
#hud { position: absolute; inset: 0; pointer-events: none; z-index: 5; }
#hud > * { pointer-events: auto; }
.hud-time { position: absolute; right: calc(24px + var(--safe-r)); top: calc(14px + var(--safe-t)); text-align: right; pointer-events: none !important; }
#clock { font-size: clamp(38px, 7vh, 64px); line-height: 1; text-shadow: 0 0 8px #000; }
.night { font-size: clamp(20px, 3.4vh, 30px); color: #ccc; }
.hud-power { position: absolute; left: calc(20px + var(--safe-l)); bottom: calc(18px + var(--safe-b)); font-size: clamp(22px, 3.6vh, 32px); text-shadow: 0 0 6px #000; pointer-events: none !important; }
.usage { display: flex; align-items: center; gap: 6px; }
#usageBars { display: inline-flex; gap: 4px; }
#usageBars i { display: inline-block; width: 14px; height: 22px; background: #3fdc3f; }
#usageBars i:nth-child(3) { background: #e0d23a; } #usageBars i:nth-child(4) { background: #e06b2a; } #usageBars i:nth-child(5) { background: #e02a2a; }
#game.low-power #powerPct { color: #ff4040; animation: blink 1s steps(1) infinite; }

.side-btns { position: absolute; right: calc(16px + var(--safe-r)); top: 50%; transform: translateY(-50%);
  display: flex; flex-direction: column; gap: 18px; padding: 14px 10px; background: rgba(20,20,20,.7);
  border: 2px solid #333; border-radius: 6px; }
.ctl { width: 92px; height: 84px; border: 3px solid #555; border-radius: 6px; background: #1b1b1b; color: #ddd;
  font-size: 26px; display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 6px;
  box-shadow: inset 0 -4px 0 rgba(0,0,0,.6); }
.ctl .lamp { width: 30px; height: 30px; border-radius: 50%; background: #3a0b0b; border: 2px solid #111; }
#doorBtn .lamp { background: #3a0b0b; }
#game.door-closed #doorBtn .lamp { background: #ff2a2a; box-shadow: 0 0 14px #ff2a2a; }
#lightBtn .lamp { background: #2a2a2a; }
#game.light-on #lightBtn .lamp { background: #fff6d0; box-shadow: 0 0 14px #fff2b0; }
.ctl:active { transform: translateY(2px); box-shadow: none; }
#game.cams-up .side-btns, #game.power-out .side-btns { display: none; }
#game.power-out #camBtn, #game.power-out .hud-power { display: none; }

#camBtn {
  position: absolute; left: 50%; transform: translateX(-50%); bottom: calc(10px + var(--safe-b));
  width: min(520px, 54vw); height: 52px; border: 2px solid rgba(255,255,255,.75); background: rgba(255,255,255,.08);
  color: #fff; font-size: 30px; letter-spacing: 3px; display: flex; align-items: center; justify-content: center; gap: 18px;
}
#camBtn:hover { background: rgba(255,255,255,.18); }
#game.cams-up #camBtn .chev { display: inline-block; transform: rotate(180deg); }
.key-hint { position: absolute; left: 50%; transform: translateX(-50%); bottom: calc(68px + var(--safe-b)); color: rgba(255,255,255,.35);
  font-size: 18px; white-space: nowrap; pointer-events: none !important; }
#game.cams-up .key-hint { display: none; }
@media (hover: none) { .key-hint { display: none; } }

/* camera flip-up animation */
.flip { position: absolute; inset: 0; pointer-events: none; z-index: 4; background: #0a0a0a; transform: translateY(100%); opacity: 0; }
.flip.go { animation: flipUp .22s ease-in forwards; }
@keyframes flipUp { 0% { transform: translateY(100%); opacity: 1; } 100% { transform: translateY(0); opacity: 1; } }

/* ---------- JUMPSCARE ---------- */
#jumpscare { background: #000; z-index: 20; }
#jumpImg { position: absolute; inset: 0; width: 100%; height: 100%; object-fit: cover; object-position: 50% 30%;
  animation: jump .9s cubic-bezier(.2,1.6,.4,1) forwards, shake .06s linear infinite;
  filter: contrast(1.35) saturate(1.2) brightness(1.05); }
@keyframes jump { 0% { transform: scale(.3); opacity: 0; } 25% { transform: scale(1.25); opacity: 1; } 100% { transform: scale(1.12); opacity: 1; } }
@keyframes shake { 0% { translate: 0 0; } 25% { translate: -14px 9px; } 50% { translate: 12px -8px; } 75% { translate: -9px -12px; } 100% { translate: 10px 10px; } }
.jump-flash { position: absolute; inset: 0; background: #fff; animation: flash .25s forwards; pointer-events: none; }
@keyframes flash { from { opacity: .9; } to { opacity: 0; } }

/* ---------- GAME OVER ---------- */
#gameover { background: #111; }
.over-text { position: absolute; inset: 0; display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 4px; text-align: center; padding: 20px; }
.over-text h2 { font-size: clamp(54px, 12vh, 120px); font-weight: 400; margin: 0; text-shadow: 0 0 12px #000, 3px 0 #f00, -3px 0 #0ff; }
.over-text p { font-size: clamp(22px, 4vh, 34px); color: #ccc; margin: 0 0 3vh; text-shadow: 0 0 6px #000; }
.over-text .menu-btn { font-size: clamp(28px, 5vh, 44px); }

/* ---------- WIN ---------- */
#win { background: #000; }
#win.active { display: flex; flex-direction: column; align-items: center; justify-content: center; }
.win-clock { display: flex; align-items: flex-end; font-size: clamp(90px, 22vh, 200px); line-height: 1; }
.roll { height: 1em; overflow: hidden; }
#rollInner { transition: transform 3.5s cubic-bezier(.6,0,.4,1) 1s; }
#rollInner div { height: 1em; }
#win.rolled #rollInner { transform: translateY(-1em); }
.am { margin-left: .2em; }
.win-sub { font-size: clamp(22px, 4vh, 34px); color: #aaa; margin: 3vh 0 2vh; opacity: 0; transition: opacity 1s 5s; text-align: center; padding: 0 20px; }
#win.rolled .win-sub { opacity: 1; }
#win .menu-btn { opacity: 0; transition: opacity 1s 6s; }
#win.rolled .menu-btn { opacity: 1; }

#rotateHint { display: none; }

/* ---------- MOBILE / NARROW ---------- */
@media (max-aspect-ratio: 1/1) {
  .menu-photo { width: 100%; height: 52%; top: 0; bottom: auto; }
  .menu-photo img { -webkit-mask-image: linear-gradient(to bottom, #000 55%, transparent 100%); mask-image: linear-gradient(to bottom, #000 55%, transparent 100%); }
  .menu-text { top: auto; bottom: calc(9vh + var(--safe-b)); }
  .menu-text h1 { font-size: clamp(40px, 6.8vh, 70px); margin-bottom: 2vh; }
  .menu-foot { bottom: calc(2vh + var(--safe-b)); }
  .map-wrap { height: min(28vh, 250px); bottom: calc(74px + var(--safe-b)); right: calc(12px + var(--safe-r)); }
  #camBtn { width: min(440px, 80vw); }
  .hud-power { top: calc(16px + var(--safe-t)); bottom: auto; }
  .side-btns { top: auto; bottom: calc(78px + var(--safe-b)); right: auto; left: 50%; transform: translateX(-50%); flex-direction: row; }
  .cam-label { top: calc(96px + var(--safe-t)); }
}
@media (max-height: 520px) {
  .ctl { width: 76px; height: 64px; font-size: 21px; gap: 3px; }
  .ctl .lamp { width: 22px; height: 22px; }
  .side-btns { gap: 10px; padding: 8px; }
  #camBtn { height: 44px; font-size: 24px; width: min(520px, 44vw); }
  .map-wrap { height: min(62vh, 300px); bottom: calc(58px + var(--safe-b)); }
  .menu-text h1 { margin-bottom: 2vh; }
  .hud-power { font-size: 20px; }
  #usageBars i { width: 10px; height: 16px; }
}
@media (max-width: 480px) {
  .ctl { width: 74px; height: 70px; font-size: 21px; }
  .side-btns { padding: 8px; gap: 12px; }
  .hud-power { font-size: 21px; }
  #usageBars i { width: 10px; height: 16px; }
}
