/* 朝天门：颠倒世界 — layout, CRT overlay, DOM UI layer.
   The world is a 390x844 pixelated canvas; every glyph of Chinese text lives in
   the DOM layer above it so it stays vector-crisp when the stage is scaled up. */

:root{
  --bg-dark:#050505;  --asphalt:#1a1614; --concrete:#2e2a26;
  --shade:#14181f;    --sun:#3a3026;
  --red:#ff1e1e;      --red-deep:#7a0f0f;
  --cream:#d8cfc4;    --player:#e8dcc8;
  --crowd:#6b5c50;    --vest:#a03030;    --guide:#ffd24a;
  --jade:#4a8f7a;
  --serif:"Songti SC","STSong","SimSun","Noto Serif CJK SC",serif;
}

*{margin:0;padding:0;box-sizing:border-box;-webkit-tap-highlight-color:transparent}

html,body{
  width:100%;height:100%;overflow:hidden;background:var(--bg-dark);
  font-family:var(--serif);color:var(--cream);
  touch-action:none;user-select:none;-webkit-user-select:none;
  overscroll-behavior:none;
}

/* ---------- stage: fixed logical size, scaled + letterboxed ---------- */
#stage{
  position:absolute;left:50%;top:50%;
  width:390px;height:844px;
  transform-origin:center center;
  background:var(--bg-dark);
  overflow:hidden;
}

#game{
  position:absolute;inset:0;
  width:390px;height:844px;
  image-rendering:pixelated;
  image-rendering:crisp-edges;
}

/* ---------- CRT scanlines, always on, above the canvas ---------- */
#scan{
  position:absolute;inset:0;pointer-events:none;z-index:5;
  background:repeating-linear-gradient(
    to bottom,
    rgba(0,0,0,.55) 0px, rgba(0,0,0,.55) 2px,
    rgba(0,0,0,0)   2px, rgba(0,0,0,0)   4px);
  opacity:.35;
  mix-blend-mode:multiply;
}

#ui{position:absolute;inset:0;z-index:10;pointer-events:none}
#ui > *{pointer-events:auto}
.hidden{display:none !important}
/* gameplay UI fades out over 800ms when an ending takes over */
.faded{opacity:0 !important;pointer-events:none !important}
#hud,#sprintbtn{transition:opacity .8s}

/* ---------- HUD ---------- */
#hud{position:absolute;inset:0;pointer-events:none}

#hpwrap{
  position:absolute;left:14px;top:14px;width:210px;height:18px;
  border:2px solid #4a423a;background:#0d0b0a;
  box-shadow:0 0 0 2px #050505;
}
#hpbar{
  height:100%;width:100%;
  background:linear-gradient(90deg,var(--red-deep),var(--red));
  transition:width .12s linear;
}
#hpbar.low{animation:hppulse .7s infinite}
@keyframes hppulse{50%{filter:brightness(1.9) saturate(1.4)}}
#hplabel{
  position:absolute;left:0;top:0;width:100%;height:100%;
  font-size:12px;line-height:16px;text-align:center;letter-spacing:4px;
  color:#0d0b0a;text-shadow:0 0 2px rgba(216,207,196,.5);
}

#items{position:absolute;right:12px;top:12px;display:flex;gap:6px}
.item{
  width:34px;height:34px;border:2px solid #4a423a;background:#0d0b0a;
  display:flex;align-items:center;justify-content:center;
  font-size:11px;color:var(--cream);letter-spacing:0;line-height:1.05;
  text-align:center;pointer-events:auto;
}
.item.usable{border-color:var(--guide);color:var(--guide);animation:itemglow 1.4s infinite}
.item.active{border-color:var(--jade);color:var(--jade);animation:none}
@keyframes itemglow{50%{box-shadow:0 0 8px var(--guide)}}

#corner{
  position:absolute;right:12px;top:56px;text-align:right;
  font-size:14px;color:var(--guide);text-shadow:0 0 6px rgba(255,210,74,.4);
  line-height:1.5;
}

/* ---------- touch controls ---------- */
#stick{
  position:absolute;width:112px;height:112px;border-radius:50%;
  border:2px solid rgba(216,207,196,.22);background:rgba(216,207,196,.05);
  transform:translate(-50%,-50%);
}
#knob{
  position:absolute;left:50%;top:50%;width:46px;height:46px;border-radius:50%;
  background:rgba(216,207,196,.28);transform:translate(-50%,-50%);
}
#sprintbtn{
  position:absolute;right:24px;bottom:44px;width:78px;height:78px;border-radius:50%;
  border:2px solid rgba(255,30,30,.35);background:rgba(122,15,15,.18);
  display:flex;align-items:center;justify-content:center;
  font-size:26px;color:rgba(255,30,30,.75);
}
#sprintbtn.on{background:rgba(255,30,30,.32);color:#fff}

/* ---------- toast (level intro line) ---------- */
#toast{
  position:absolute;left:0;right:0;top:40%;text-align:center;
  font-size:21px;letter-spacing:3px;color:var(--cream);
  text-shadow:0 2px 10px #000;opacity:0;transition:opacity .8s;
  pointer-events:none;padding:0 30px;line-height:1.8;
}
#toast.show{opacity:1}

/* ---------- rhythm minigame ---------- */
#rhythm{position:absolute;inset:0;pointer-events:none}
#notefield{position:absolute;left:0;right:0;bottom:130px;height:330px;overflow:hidden}
/* the judgement line: notes are on the beat when they land on it */
#notefield::after{
  content:'';position:absolute;left:10px;right:10px;bottom:0;height:3px;
  background:var(--red);box-shadow:0 0 10px rgba(255,30,30,.7);opacity:.85;
}
.note{
  position:absolute;width:64px;height:22px;margin-left:-32px;
  background:var(--guide);box-shadow:0 0 10px rgba(255,210,74,.6);
}
.note.hit{background:var(--jade);box-shadow:0 0 14px var(--jade)}
.note.miss{background:var(--red-deep);box-shadow:none;opacity:.4}
#lanes{
  position:absolute;left:0;right:0;bottom:34px;height:96px;
  display:flex;gap:6px;padding:0 10px;pointer-events:auto;
}
.lane{
  flex:1;border:2px solid #4a423a;background:rgba(13,11,10,.72);
  display:flex;flex-direction:column;align-items:center;justify-content:center;
  font-size:26px;color:var(--cream);
}
.lane b{font-size:11px;color:#6b5c50;font-weight:normal;margin-top:2px}
.lane.press{border-color:var(--guide);background:rgba(255,210,74,.18)}
#rhythmmsg{
  position:absolute;left:0;right:0;bottom:150px;text-align:center;
  font-size:28px;letter-spacing:6px;color:var(--guide);
  text-shadow:0 0 12px rgba(255,210,74,.5);opacity:0;transition:opacity .2s;
}

/* ---------- overlays ---------- */
#overlay{
  position:absolute;inset:0;display:flex;flex-direction:column;
  align-items:center;justify-content:center;padding:34px 30px;
  text-align:center;
}
#overlay.dark{background:rgba(5,5,5,.9)}
/* ending panels sit over a scene that is still doing work — keep it visible */
#overlay.soft{background:linear-gradient(to bottom,
  rgba(5,5,5,.35) 0%, rgba(5,5,5,.72) 30%, rgba(5,5,5,.72) 72%, rgba(5,5,5,.4) 100%)}
#overlay.clear{background:transparent}

.o-title{font-size:15px;letter-spacing:8px;color:var(--crowd);margin-bottom:18px}
.o-big{font-size:34px;letter-spacing:8px;color:var(--red);
  text-shadow:0 0 18px rgba(255,30,30,.55),0 0 44px rgba(122,15,15,.7);margin-bottom:16px}
.o-body{font-size:17px;line-height:2.1;color:var(--cream);max-width:320px;margin-bottom:26px}
.o-note{font-size:13px;line-height:1.9;color:var(--crowd);max-width:320px;margin-top:14px}

.btn{
  border:2px solid #4a423a;background:rgba(13,11,10,.85);color:var(--cream);
  font-family:var(--serif);font-size:17px;letter-spacing:5px;
  padding:12px 30px;margin:6px;min-width:190px;
}
.btn:active{border-color:var(--red);color:var(--red)}
.btn.primary{border-color:var(--red);color:var(--red);
  text-shadow:0 0 10px rgba(255,30,30,.5)}

/* ending gallery */
#gallery{display:flex;flex-direction:column;gap:8px;margin:16px 0 22px;width:100%;max-width:320px}
.grow{display:flex;justify-content:space-between;align-items:center;
  border:1px solid #2e2a26;padding:9px 14px;font-size:15px;letter-spacing:3px}
.grow.seen{color:var(--cream);border-color:#4a423a}
.grow.unseen{color:#3a3026}
.grow small{font-size:11px;letter-spacing:1px;color:var(--crowd)}
.stats{font-size:12px;color:var(--crowd);letter-spacing:2px;line-height:1.9}

/* ---------- the letter (江风 ending) ---------- */
/* The letter sits over the live river, not on a black card. The scrim that makes
   it readable is drawn on the canvas (see drawPost) so it eases in on the game
   clock rather than on a CSS transition. */
#letter{
  position:absolute;inset:0;padding:38px 32px 26px;
  display:flex;flex-direction:column;justify-content:center;
  overflow:hidden;
  text-align:left;              /* a letter, not a monument */
}
#letter p{
  font-size:15.5px;line-height:1.95;color:var(--cream);
  margin-bottom:10px;opacity:0;transition:opacity 1.6s ease-out;
  text-shadow:0 2px 6px #050505,0 0 16px rgba(5,5,5,.95);
}
#letter p.in{opacity:1}
#letter p.sign{text-align:right;margin-top:12px;
  color:var(--red);text-shadow:0 0 12px rgba(255,30,30,.55),0 2px 10px #000}
#letter .lie{font-style:italic;color:var(--red);
  text-shadow:0 0 14px rgba(255,30,30,.6),0 2px 10px #000}
#letter .restart{
  align-self:center;margin-top:18px;opacity:0;transition:opacity 2s;
  pointer-events:none;
}
#letter .restart.in{opacity:1;pointer-events:auto}

/* The letter still reveals one paragraph at a time (that is scheduled in JS),
   it just stops cross-fading. */
@media (prefers-reduced-motion: reduce){
  #letter p, #letter .restart, #toast, #hpbar{transition:none}
  #hpbar.low, .item.usable{animation:none}
}

/* ---------- mute ---------- */
#mute{
  position:absolute;left:12px;bottom:12px;width:34px;height:34px;
  border:1px solid #2e2a26;color:#6b5c50;font-size:17px;
  display:flex;align-items:center;justify-content:center;z-index:20;
}
#mute.off{color:#2e2a26;text-decoration:line-through}

/* ---------- debug ---------- */
#debug{
  position:absolute;left:0;right:0;bottom:0;background:rgba(5,5,5,.94);
  border-top:1px solid var(--red);padding:6px;z-index:30;
  display:flex;flex-wrap:wrap;gap:4px;font-size:11px;
}
#debug button{
  font-family:var(--serif);font-size:11px;padding:5px 7px;
  background:#14181f;color:var(--cream);border:1px solid #4a423a;
}
#debug button.on{border-color:var(--red);color:var(--red)}
#debug .dl{width:100%;color:var(--crowd);font-size:10px;letter-spacing:1px}
