:root {
  --night: #121126;
  --night-2: #1d1a35;
  --panel: #27213f;
  --panel-dark: #17142b;
  --cream: #fff5df;
  --muted: #b8abc5;
  --pink: #f4789a;
  --pink-2: #ff9bac;
  --peach: #ffc69f;
  --yellow: #ffe08a;
  --green: #82d6a4;
  --blue: #74b9d1;
  --purple: #8c75c7;
  --outline: #080711;
  font-family: "Microsoft YaHei", "PingFang SC", "Noto Sans SC", monospace;
  color: var(--cream);
  background: var(--night);
}

* { box-sizing: border-box; }

html, body { width: 100%; min-height: 100%; margin: 0; }

body {
  min-height: 100dvh;
  overflow-x: hidden;
  background:
    linear-gradient(rgba(18,17,38,.78), rgba(18,17,38,.94)),
    repeating-linear-gradient(0deg, transparent 0 31px, rgba(255,255,255,.025) 31px 32px),
    repeating-linear-gradient(90deg, transparent 0 31px, rgba(255,255,255,.025) 31px 32px),
    #121126;
}

button { font: inherit; }
.hidden { display: none !important; }

.game-shell {
  width: min(1120px, 100%);
  min-height: 100dvh;
  margin: auto;
  padding: max(14px, env(safe-area-inset-top)) 18px max(14px, env(safe-area-inset-bottom));
  display: flex;
  flex-direction: column;
  justify-content: center;
}

.topbar, .statusbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  width: 100%;
  color: var(--muted);
  font-size: 12px;
  letter-spacing: .06em;
}

.topbar { margin-bottom: 10px; }
.statusbar { margin-top: 10px; }
.brand, .top-actions { display: flex; align-items: center; gap: 10px; }
.brand i { width: 9px; height: 9px; background: var(--pink); box-shadow: 0 0 0 2px var(--outline), 0 0 12px var(--pink); }
.love-meter { padding: 5px 9px; color: var(--pink-2); border: 2px solid #3d3457; background: #1a1730; box-shadow: 2px 2px 0 var(--outline); }
.love-meter b { color: var(--cream); }

.quick-gift {
  padding: 5px 9px;
  border: 1px solid #544767;
  color: var(--peach);
  background: transparent;
  font-size: 10px;
  cursor: pointer;
}
.quick-gift:hover, .quick-gift:focus-visible { color: var(--yellow); border-color: var(--yellow); }

.icon-btn {
  width: 31px; height: 31px; padding: 0;
  color: var(--cream); border: 2px solid #4c4167; background: #211d38;
  box-shadow: 3px 3px 0 var(--outline); cursor: pointer;
}
.icon-btn.playing { color: var(--yellow); animation: pixelPulse 1s steps(2) infinite; }
.icon-btn.disabled { opacity: .45; }

.game-frame {
  position: relative;
  width: 100%;
  aspect-ratio: 16 / 9;
  max-height: calc(100dvh - 100px);
  overflow: hidden;
  border: 4px solid var(--outline);
  outline: 3px solid #3d3457;
  background: #17152c;
  box-shadow: 10px 12px 0 rgba(0,0,0,.35), 0 24px 70px rgba(0,0,0,.35);
  image-rendering: pixelated;
}

#gameCanvas {
  display: block;
  width: 100%;
  height: 100%;
  image-rendering: auto;
}

.scanlines {
  position: absolute;
  inset: 0;
  z-index: 2;
  pointer-events: none;
  display: none;
  opacity: 0;
  background: repeating-linear-gradient(to bottom, transparent 0 3px, rgba(0,0,0,.18) 3px 4px);
  mix-blend-mode: multiply;
}

.title-screen {
  position: absolute;
  z-index: 4;
  inset: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  text-align: center;
  background: linear-gradient(180deg, rgba(19,17,41,.22), rgba(13,12,29,.68));
}

.title-screen::after {
  width: 18px;
  height: 25px;
  border-radius: 50% 50% 45% 45%;
  background: var(--pink);
  box-shadow: 65px -22px var(--yellow), -78px 18px var(--blue), 137px 34px var(--peach), -147px -28px var(--purple);
}

.title-screen::before, .title-screen::after {
  content: "";
  position: absolute;
  width: 8px; height: 8px;
  background: var(--yellow);
  box-shadow: 83px 27px var(--cream), -104px 48px var(--pink), 152px 75px var(--blue), -171px -43px var(--cream), 204px -27px var(--yellow);
  animation: blink 1.8s steps(2) infinite;
}
.title-screen::before { top: 18%; left: 28%; }
.title-screen::after { bottom: 19%; right: 29%; transform: scale(.55); animation-delay: .8s; }
.pixel-stars { color: var(--yellow); font-size: clamp(11px, 1.3vw, 16px); margin-bottom: 5px; }
.birthday-seal {
  margin: 0 0 8px;
  padding: 4px 9px;
  border: 1px solid rgba(255,224,138,.5);
  color: var(--yellow);
  background: rgba(39,33,63,.72);
  font-size: clamp(8px,.9vw,11px);
  letter-spacing: .12em;
}
.title-kicker, .ending-kicker { margin: 0 0 8px; color: var(--peach); font-size: clamp(8px, 1vw, 12px); font-weight: 700; letter-spacing: .25em; }
.title-screen h1 {
  margin: 0;
  color: var(--cream);
  font-size: clamp(31px, 5vw, 72px);
  line-height: 1.02;
  letter-spacing: -.08em;
  text-shadow: 4px 4px 0 #5d365e, 8px 8px 0 var(--outline);
}
.title-screen h1 em { color: var(--pink-2); font-style: normal; }
.title-sub { margin: 13px 0 18px; color: #d8cbd8; font-size: clamp(10px, 1.3vw, 15px); }
.control-tip { margin: 10px 0 0; color: #776c8b; font-size: clamp(8px, .9vw, 11px); }
.title-actions { display: flex; flex-direction: column; align-items: center; gap: 10px; }
.soft-link {
  padding: 2px 4px;
  border: 0;
  color: #a99caf;
  background: transparent;
  font-size: 10px;
  cursor: pointer;
}
.soft-link:hover, .soft-link:focus-visible { color: var(--yellow); }

.pixel-btn {
  position: relative;
  min-height: 37px;
  padding: 8px 18px;
  border: 3px solid var(--outline);
  color: var(--cream);
  font-weight: 700;
  font-size: 12px;
  cursor: pointer;
  box-shadow: 5px 5px 0 var(--outline);
  transition: transform .08s, box-shadow .08s;
}
.pixel-btn::before { content: ""; position: absolute; inset: 2px; border: 1px solid rgba(255,255,255,.2); pointer-events: none; }
.pixel-btn.primary { background: var(--pink); }
.pixel-btn.ghost { background: #332c4b; }
.pixel-btn:hover, .pixel-btn:focus-visible { filter: brightness(1.1); transform: translate(-1px,-1px); box-shadow: 6px 6px 0 var(--outline); }
.pixel-btn:active { transform: translate(4px,4px); box-shadow: 1px 1px 0 var(--outline); }

.chapter-card {
  position: absolute;
  z-index: 8;
  left: 50%; top: 50%;
  width: min(78%, 460px);
  padding: 22px 25px;
  transform: translate(-50%,-50%);
  text-align: center;
  border: 4px double var(--cream);
  color: var(--cream);
  background: rgba(17,15,35,.94);
  box-shadow: 8px 8px 0 var(--outline);
  animation: cardIn .35s steps(5);
}
.chapter-card small { display: block; margin-bottom: 10px; color: var(--pink-2); letter-spacing: .22em; font-size: 10px; }
.chapter-card strong { font-size: clamp(18px, 3vw, 35px); letter-spacing: .08em; }

.objective {
  position: absolute;
  z-index: 5;
  left: 12px; top: 12px;
  display: flex; align-items: center; gap: 7px;
  max-width: 60%; padding: 6px 10px 6px 6px;
  border: 2px solid var(--outline);
  background: rgba(25,21,44,.9);
  box-shadow: 3px 3px 0 rgba(0,0,0,.5);
  font-size: clamp(8px, 1.1vw, 12px);
}
.objective { display: none !important; }
.objective span { display: grid; place-items: center; width: 17px; height: 17px; flex: 0 0 auto; color: var(--outline); background: var(--yellow); font-weight: 900; }
.objective p { margin: 0; }

.interaction-hint {
  position: absolute;
  z-index: 5;
  left: 50%; bottom: 11px;
  transform: translateX(-50%);
  padding: 6px 10px;
  border: 2px solid var(--outline);
  color: var(--yellow);
  background: rgba(19,17,38,.9);
  font-size: clamp(8px, 1vw, 11px);
  animation: hintBob .8s steps(2) infinite;
}
.interaction-hint kbd { padding: 1px 4px; color: var(--cream); border: 1px solid #706287; background: #332c4b; font: inherit; }

.dialogue {
  position: absolute;
  z-index: 10;
  left: 4%; right: 4%; bottom: 5%;
  min-height: 30%;
  display: flex;
  gap: 14px;
  padding: clamp(10px, 1.7vw, 20px);
  border: 4px solid var(--outline);
  outline: 2px solid var(--cream);
  background: rgba(29,25,49,.96);
  box-shadow: 7px 7px 0 rgba(0,0,0,.5);
}
.portrait {
  width: clamp(42px, 7.4vw, 82px);
  height: clamp(42px, 7.4vw, 82px);
  flex: 0 0 auto;
  border: 3px solid var(--outline);
  background-color: #44385c;
  image-rendering: pixelated;
}
.portrait.boy { background: linear-gradient(#2a2748 0 20%, #3d3153 20% 34%, #e0a386 34% 70%, #60568d 70%); }
.portrait.girl { background: linear-gradient(#3a2440 0 18%, #6d3d55 18% 35%, #edaf95 35% 70%, #b75574 70%); }
.portrait.narrator { background: radial-gradient(circle, var(--yellow) 0 12%, transparent 13%), #342d4d; }
.dialogue-body { min-width: 0; flex: 1; }
.dialogue-body > strong { display: block; margin-bottom: 5px; color: var(--pink-2); font-size: clamp(10px, 1.3vw, 15px); }
.dialogue-body > p { margin: 0; color: var(--cream); font-size: clamp(10px, 1.45vw, 17px); line-height: 1.75; }
.dialogue-next { position: absolute; right: 12px; bottom: 7px; border: 0; color: var(--yellow); background: none; cursor: pointer; animation: bob .7s steps(2) infinite; }
.auto-badge {
  position: absolute;
  right: 10px;
  top: 8px;
  color: #8d809b;
  font-size: 8px;
  letter-spacing: .08em;
}

.wish-btn {
  position: absolute;
  z-index: 12;
  left: 50%;
  bottom: 11%;
  transform: translateX(-50%);
  min-width: min(290px,80%);
  padding: 12px 18px;
  border: 3px solid var(--outline);
  outline: 2px solid var(--yellow);
  color: var(--outline);
  background: var(--yellow);
  box-shadow: 6px 6px 0 rgba(0,0,0,.55);
  font-weight: 800;
  cursor: pointer;
  animation: wishGlow 1.4s steps(2) infinite;
}
.wish-btn span { color: #9f486a; margin-right: 6px; }
.wish-btn:hover, .wish-btn:focus-visible { background: var(--cream); }

.choices { display: grid; grid-template-columns: repeat(3,1fr); gap: 7px; margin-top: 9px; }
.choice-btn {
  min-height: 31px;
  padding: 5px 7px;
  border: 2px solid #0a0913;
  color: var(--cream);
  background: #3a3152;
  box-shadow: 2px 2px 0 var(--outline);
  font-size: clamp(8px, 1.05vw, 12px);
  cursor: pointer;
}
.choice-btn:hover, .choice-btn:focus-visible { color: var(--yellow); background: #554268; transform: translateY(-1px); }

.runner-hud {
  position: absolute; z-index: 5; top: 11px; right: 11px;
  display: flex; gap: 6px;
}
.runner-hud span { padding: 5px 7px; border: 2px solid var(--outline); color: var(--cream); background: rgba(26,22,46,.9); font-size: clamp(8px,1vw,11px); }
.runner-hud b { color: var(--yellow); }

.mobile-controls { position: absolute; z-index: 7; inset: auto 12px 10px; align-items: end; justify-content: space-between; pointer-events: none; }
.dpad { display: flex; gap: 7px; }
.mobile-controls button {
  width: 45px; height: 39px; padding: 0;
  border: 3px solid var(--outline); color: var(--cream); background: rgba(49,42,72,.82); box-shadow: 3px 3px 0 rgba(0,0,0,.6);
  pointer-events: auto; touch-action: none; user-select: none;
}
.mobile-controls button.pressed, .mobile-controls button:active { transform: translate(2px,2px); box-shadow: 1px 1px 0 var(--outline); background: var(--pink); }
.mobile-controls .action-btn { width: 43px; height: 43px; border-radius: 50%; color: var(--outline); background: var(--yellow); font-weight: 900; }
.mobile-controls.runner-mode .dpad { visibility: hidden; }

.progress-pips { display: flex; gap: 5px; }
.progress-pips i { width: 11px; height: 5px; background: #3b334e; }
.progress-pips i.active { background: var(--pink); }

.ending {
  position: fixed;
  z-index: 100;
  inset: 0;
  overflow: auto;
  padding: max(28px,env(safe-area-inset-top)) 18px max(32px,env(safe-area-inset-bottom));
  background: rgba(12,10,26,.97);
}
.ending-card { position: relative; z-index: 2; width: min(960px,100%); margin: auto; padding: 24px; text-align: center; }
.ending-card::before {
  content: "FOR YOU\A生日快乐";
  white-space: pre;
  position: absolute;
  right: 2px;
  top: 4px;
  display: grid;
  place-items: center;
  width: 66px;
  height: 66px;
  border: 2px dashed var(--pink-2);
  border-radius: 50%;
  color: var(--pink-2);
  font-size: 8px;
  line-height: 1.5;
  transform: rotate(9deg);
}
.ending-kicker { color: var(--yellow); }
.ending h2 { margin: 10px 0; font-size: clamp(31px,6vw,60px); line-height: 1.2; text-shadow: 4px 4px 0 #4b315a; }
.ending h2 span { color: var(--pink-2); }
.ending-wish { max-width: 640px; margin: 17px auto 24px; color: #c9bdce; font-size: 15px; line-height: 2; }
.ending-stars { position: fixed; inset: 0; opacity: .5; background-image: radial-gradient(circle, var(--cream) 1px, transparent 1px), radial-gradient(circle, var(--pink) 1px, transparent 1px); background-size: 53px 53px, 79px 79px; background-position: 0 0, 20px 25px; animation: starsMove 15s linear infinite; }

.memory-strip { display: grid; grid-template-columns: repeat(3,1fr); gap: 13px; width: min(620px,100%); margin: 0 auto 24px; }
.ending-memory { position: relative; aspect-ratio: 4/3; padding: 7px 7px 23px; transform: rotate(var(--rotate)); border: 3px solid var(--outline); background: var(--cream); box-shadow: 5px 5px 0 rgba(0,0,0,.45); }
.ending-memory img, .ending-memory .photo-placeholder { width: 100%; height: 100%; object-fit: cover; image-rendering: pixelated; }
.photo-placeholder { display: grid; place-items: center; color: #7c6683; background: repeating-linear-gradient(45deg,#e7d9cf 0 8px,#dac9cd 8px 16px); font-size: 22px; }
.ending-memory small { position: absolute; left: 4px; right: 4px; bottom: 4px; overflow: hidden; color: #4d4052; font-size: 9px; white-space: nowrap; text-overflow: ellipsis; }

.ending-image-frame {
  position: relative; width: 100%; margin: 24px auto;
  overflow: hidden; border: 4px solid var(--outline); outline: 2px solid #514569; background: #090814; box-shadow: 8px 8px 0 rgba(0,0,0,.5);
}
.ending-image-frame a { display: block; }
.ending-image-frame a:focus-visible { outline: 3px solid var(--yellow); outline-offset: -3px; }
.ending-image-frame img { display: block; width: 100%; height: auto; image-rendering: auto; }
.ending-image-placeholder { min-height: 280px; display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 16px; padding: 24px; text-align: center; background: radial-gradient(circle,#302444,#121026 68%); }
.ending-image-placeholder span { color: var(--pink); font-size: 48px; }
.ending-image-placeholder strong { color: var(--cream); }
.signature { color: var(--peach); font-size: 18px; }
.ending-actions { display: flex; justify-content: center; gap: 14px; margin: 24px 0; }

.toast {
  position: fixed; z-index: 200; left: 50%; bottom: 22px; transform: translate(-50%,15px);
  max-width: calc(100% - 30px); padding: 8px 12px; border: 3px solid var(--outline); color: var(--cream); background: #332c4b; box-shadow: 4px 4px 0 rgba(0,0,0,.5);
  opacity: 0; pointer-events: none; transition: .2s; font-size: 12px; text-align: center;
}
.toast.show { opacity: 1; transform: translate(-50%,0); }

.rotate-tip {
  position: fixed; z-index: 300; left: 50%; top: 50%; display: none; flex-direction: column; align-items: center; gap: 8px;
  transform: translate(-50%,-50%); padding: 18px; border: 3px solid var(--cream); color: var(--cream); background: rgba(16,14,33,.96); box-shadow: 7px 7px 0 var(--outline); font-size: 27px;
}
.rotate-tip span { font-size: 12px; }

@keyframes blink { 50% { opacity: .25; } }
@keyframes bob { 50% { transform: translateY(2px); } }
@keyframes hintBob { 0%,100% { transform: translateX(-50%) translateY(0); } 50% { transform: translateX(-50%) translateY(2px); } }
@keyframes cardIn { from { opacity: 0; transform: translate(-50%,-45%) scale(.9); } }
@keyframes pixelPulse { 50% { color: var(--pink); } }
@keyframes starsMove { to { background-position: 53px 53px, -59px 104px; } }
@keyframes wishGlow { 50% { transform: translateX(-50%) translateY(-2px); box-shadow: 6px 8px 0 rgba(0,0,0,.5), 0 0 18px rgba(255,224,138,.3); } }

@media (pointer: coarse), (max-width: 800px) {
  .mobile-controls.visible { display: flex !important; }
  .game-shell { padding: max(8px,env(safe-area-inset-top)) 9px max(8px,env(safe-area-inset-bottom)); }
  .topbar, .statusbar { font-size: 9px; }
  .quick-gift { font-size: 8px; padding: 4px 6px; }
  .game-frame { max-height: calc(100dvh - 66px); }
  .dialogue { left: 2.5%; right: 2.5%; bottom: 3%; min-height: 36%; }
  .dialogue-body > p { line-height: 1.5; }
  .dialogue-next { right: 5px; bottom: 2px; }
  .choices { margin-top: 5px; }
  .interaction-hint { bottom: 58px; }
  .objective { top: 7px; left: 7px; }
}

@media (max-width: 640px) and (orientation: portrait) {
  .game-shell { justify-content: flex-start; padding-top: max(24px, env(safe-area-inset-top)); }
  /* A separate row keeps dialogue entirely below the 16:9 scene. */
  .game-frame {
    display: grid;
    grid-template-columns: minmax(0, 1fr);
    grid-template-rows: auto minmax(158px, auto);
    aspect-ratio: auto;
    max-height: none;
  }
  #gameCanvas {
    grid-column: 1;
    grid-row: 1;
    width: 100%;
    height: auto;
    aspect-ratio: 16 / 9;
  }
  .game-frame::after {
    content: "";
    grid-column: 1;
    grid-row: 2;
    background: var(--night-2);
    border-top: 2px solid #514569;
  }
  .game-frame:has(.title-screen:not(.hidden)) { grid-template-rows: auto; }
  .game-frame:has(.title-screen:not(.hidden))::after { display: none; }
  .title-screen { bottom: auto; width: 100%; aspect-ratio: 16 / 9; }
  .chapter-card { top: calc((100vw - 26px) * .28125); }
  .wish-btn { top: calc((100vw - 26px) * .5625 + 46px); bottom: auto; }
  .top-actions .love-meter { display: none; }
  .title-screen h1 { font-size: 9vw; }
  .title-sub { margin: 8px 0 12px; }
  .control-tip { display: none; }
  .birthday-seal { margin-bottom: 5px; }
  .dialogue {
    position: relative;
    grid-column: 1;
    grid-row: 2;
    inset: auto;
    align-self: stretch;
    margin: 12px;
    min-height: 132px;
    gap: 10px;
    padding: 12px 12px 32px;
    background: var(--night-2);
    box-shadow: 3px 3px 0 rgba(0,0,0,.5);
  }
  .dialogue-body > strong { font-size: 12px; padding-right: 62px; }
  .dialogue-body > p { font-size: 14px; line-height: 1.7; overflow-wrap: anywhere; }
  .auto-badge { top: 12px; right: 10px; font-size: 9px; }
  .dialogue-next { right: 8px; bottom: 4px; min-width: 36px; min-height: 30px; }
  .portrait { width: 36px; height: 36px; }
  .choices { grid-template-columns: 1fr; gap: 3px; }
  .choice-btn { min-height: 23px; padding: 2px 4px; }
  .mobile-controls button { width: 39px; height: 34px; }
  .mobile-controls .action-btn { width: 38px; height: 38px; }
  .statusbar { margin-top: 8px; }
  .ending-card { padding: 18px 4px; }
  .ending-card::before { width: 54px; height: 54px; right: 0; font-size: 7px; }
}

@media (max-height: 480px) {
  .game-shell { padding-top: 6px; padding-bottom: 6px; }
  .topbar { margin-bottom: 5px; }
  .statusbar { margin-top: 5px; }
  .game-frame { max-height: calc(100dvh - 57px); width: auto; max-width: 100%; margin: auto; }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation-duration: .01ms !important; animation-iteration-count: 1 !important; }
}
