* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
      html, body { margin: 0; padding: 0; }
      body {
        background: #17101f;
        background-image: radial-gradient(ellipse 90% 55% at 50% -10%, #2c1e42 0%, #17101f 60%);
        color: #f3e9d2;
        font-family: "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", system-ui, sans-serif;
        min-height: 100dvh;
        display: flex; flex-direction: column; align-items: center;
        padding: 10px 8px calc(10px + env(safe-area-inset-bottom));
        overflow-x: hidden;
      }
      button { font-family: inherit; cursor: pointer; min-height: 2.25rem; }
      #stage { width: 100%; max-width: 560px; }
      /* ---------- game frame ---------- */
      #frame {
        position: relative; width: 100%; aspect-ratio: 16 / 9;
        border: 3px solid #c9a227; border-radius: 12px;
        box-shadow: 0 0 0 3px #241708, 0 0 22px rgba(201,162,39,.35), 0 16px 38px rgba(0,0,0,.6);
        background: #0d0a14; overflow: hidden;
        touch-action: none; user-select: none; -webkit-user-select: none;
      }
      canvas { width: 100%; height: 100%; display: block; image-rendering: pixelated; image-rendering: crisp-edges; }
      #frame::after {
        content: ''; position: absolute; inset: 0; z-index: 2; pointer-events: none;
        box-shadow: inset 0 0 46px rgba(5,2,10,.5), inset 0 0 6px rgba(5,2,10,.6);
        border-radius: inherit;
      }
      /* ---------- HUD ---------- */
      #hud {
        position: absolute; top: 0; left: 0; right: 0;
        display: flex; align-items: center; gap: 8px;
        padding: 6px 52px 6px 8px; pointer-events: none;
        background: linear-gradient(180deg, rgba(10,6,18,.72), rgba(10,6,18,0));
        font-size: 13px; z-index: 5;
      }
      #hearts { color: #ff5d73; letter-spacing: 1px; font-size: 15px; text-shadow: 0 1px 2px #000; white-space: nowrap;
        background: rgba(12,7,20,.55); border: 1px solid rgba(255,93,115,.45); border-radius: 999px; padding: 2px 9px; }
      #objective {
        flex: 1; min-width: 0; color: #ffe9a8; font-size: 12.5px; line-height: 1.35;
        text-shadow: 0 1px 2px #000;
        background: rgba(12,7,20,.45); border: 1px solid rgba(255,217,77,.3); border-radius: 8px; padding: 3px 8px;
      }
      #gold { color: #ffd94d; font-weight: 700; white-space: nowrap; text-shadow: 0 1px 2px #000;
        background: rgba(12,7,20,.55); border: 1px solid rgba(255,217,77,.45); border-radius: 999px; padding: 2px 9px; }
      #timerChip {
        display: none; background: #7c1d2e; color: #fff; border-radius: 999px;
        padding: 2px 9px; font-weight: 700; font-variant-numeric: tabular-nums; white-space: nowrap;
      }
      /* ---------- toast ---------- */
      #toast {
        position: absolute; top: 44px; left: 50%; transform: translateX(-50%) translateY(-6px);
        background: rgba(24,16,8,.92); border: 2px solid #c9a227; color: #ffe9a8;
        border-radius: 8px; padding: 7px 14px; font-size: 13.5px; font-weight: 600;
        opacity: 0; pointer-events: none; transition: opacity .25s, transform .25s; z-index: 8;
        max-width: 92%; text-align: center;
      }
      #toast.show { opacity: 1; transform: translateX(-50%) translateY(0); }
      /* ---------- dialog ---------- */
      #dialog {
        position: absolute; left: 8px; right: 8px; bottom: 8px; z-index: 9;
        background: linear-gradient(180deg, rgba(43,29,13,.97), rgba(24,16,8,.97)); border: 2px solid #c9a227; border-radius: 12px;
        padding: 10px 12px 12px; display: none; cursor: pointer;
        box-shadow: 0 8px 20px rgba(0,0,0,.5);
      }
      #dialog.show { display: block; }
      #dlgName { color: #ffd94d; font-weight: 700; font-size: 13px; margin-bottom: 3px; }
      #dlgText { font-size: 15px; line-height: 1.55; min-height: 46px; }
      #dlgNext { text-align: right; color: #c9a227; font-size: 12px; animation: bob 0.8s infinite alternate; }
      @keyframes bob { from { transform: translateY(0); } to { transform: translateY(3px); } }
      /* ---------- quiz ---------- */
      #quiz {
        position: absolute; inset: 0; z-index: 10; display: none;
        background: rgba(8,5,16,.78); align-items: center; justify-content: center; padding: 12px;
      }
      #quiz.show { display: flex; }
      #quizCard {
        background: #fdf6e0; color: #2b2118; border: 3px solid #8a6d2f; border-radius: 12px;
        width: 100%; max-width: 430px; padding: 16px 16px 12px;
        box-shadow: 0 16px 40px rgba(0,0,0,.6);
      }
      #quizTag { display: inline-block; background: #7c1d2e; color: #fff; font-size: 12px; font-weight: 700; border-radius: 999px; padding: 2px 10px; margin-bottom: 8px; }
      #quizQ { font-size: 16.5px; font-weight: 700; line-height: 1.5; margin-bottom: 12px; }
      .quizOpt {
        display: block; width: 100%; text-align: left; margin: 0 0 8px;
        background: #fff; border: 2px solid #cbbd93; border-radius: 9px;
        padding: 10px 12px; font-size: 14.5px; color: #2b2118; min-height: 44px;
      }
      .quizOpt:active { background: #ffeeb8; }
      .quizOpt.right { background: #d9f2c2; border-color: #5d9c3f; }
      .quizOpt.wrong { background: #f8d2d2; border-color: #b04040; }
      #quizNote { font-size: 13px; color: #7a6748; min-height: 18px; }
      /* ---------- wardrobe ---------- */
      #wardrobe {
        position: absolute; inset: 0; z-index: 10; display: none;
        background: rgba(8,5,16,.8); align-items: center; justify-content: center; padding: 12px;
      }
      #wardrobe.show { display: flex; }
      #wardCard {
        background: #241708; border: 3px solid #c9a227; border-radius: 12px;
        width: 100%; max-width: 430px; padding: 14px; max-height: 92%; overflow-y: auto;
      }
      #wardCard h3 { margin: 0 0 10px; color: #ffd94d; font-size: 16px; }
      .skinRow {
        display: flex; align-items: center; gap: 10px; width: 100%;
        background: #33240f; border: 2px solid #6b4f2a; border-radius: 9px;
        color: #f3e9d2; padding: 8px 10px; margin-bottom: 8px; font-size: 14.5px; text-align: left;
      }
      .skinRow.on { border-color: #ffd94d; background: #453311; }
      .skinRow.locked { opacity: .45; }
      .skinRow canvas { width: 28px; height: 44px; image-rendering: pixelated; flex: none; }
      .skinRow .tag { margin-left: auto; font-size: 12px; color: #ffd94d; white-space: nowrap; }
      #wardClose { width: 100%; margin-top: 2px; background: #c9a227; border: none; border-radius: 9px; font-weight: 700; font-size: 15px; color: #241708; padding: 10px; }
      /* ---------- save / load ---------- */
      #saveBox {
        position: absolute; inset: 0; z-index: 14; display: none;
        background: rgba(8,5,16,.8); align-items: center; justify-content: center; padding: 12px;
      }
      #saveBox.show { display: flex; }
      #saveCard {
        background: #241708; border: 3px solid #c9a227; border-radius: 12px;
        width: 100%; max-width: 430px; padding: 14px; max-height: 92%; overflow-y: auto;
      }
      #saveCard h3 { margin: 0 0 6px; color: #ffd94d; font-size: 16px; }
      #saveCard .saveNote { font-size: 12px; color: #bfae8e; line-height: 1.55; margin-bottom: 10px; }
      .saveLabel { font-size: 13px; font-weight: 700; color: #ffe9a8; margin: 10px 0 5px; }
      #saveCard textarea {
        width: 100%; resize: none; border-radius: 8px; border: 2px solid #6b4f2a;
        background: #17100a; color: #ffe9a8; font-size: 12px; line-height: 1.5;
        padding: 8px; word-break: break-all; font-family: ui-monospace, Menlo, Consolas, monospace;
      }
      .saveBtn {
        width: 100%; margin-top: 6px; background: #33240f; border: 2px solid #6b4f2a; border-radius: 9px;
        color: #ffe9a8; font-weight: 700; font-size: 14px; padding: 9px;
      }
      .saveBtn:active { background: #54350f; }
      #saveClose { width: 100%; margin-top: 12px; background: #c9a227; border: none; border-radius: 9px; font-weight: 700; font-size: 15px; color: #241708; padding: 10px; }
      .miniBtn {
        background: transparent; color: #ffd94d; border: 2px solid #c9a227;
        border-radius: 999px; font-size: 13.5px; font-weight: 700; letter-spacing: 1px;
        padding: 7px 22px; margin-top: 8px; min-height: 36px;
      }
      .miniBtn:active { background: rgba(201,162,39,.25); }
      /* ---------- title / ending overlays ---------- */
      .bigOverlay {
        position: absolute; inset: 0; z-index: 11; display: none;
        flex-direction: column; align-items: center; justify-content: safe center;
        text-align: center; padding: 10px; overflow-y: auto;
      }
      .bigOverlay.show { display: flex; }
      #titleScreen { background: linear-gradient(180deg, rgba(23,13,38,.97), rgba(23,13,38,.99)); }
      .titleRow { display: flex; align-items: center; gap: 12px; max-width: 100%; }
      .titleCol { text-align: left; min-width: 0; }
      .titleCol .bigBtn { margin-top: 6px; }
      #titleScreen h1 {
        margin: 0 0 2px; font-size: clamp(22px, 6vw, 34px); letter-spacing: 2px; white-space: nowrap;
        color: #ffd94d; text-shadow: 0 3px 0 #8a5a12, 0 6px 18px rgba(0,0,0,.6);
      }
      #titleScreen .sub { color: #d9c8f5; font-size: 12.5px; margin-bottom: 2px; }
      #titleArt {
        width: 104px; height: 88px; image-rendering: pixelated; flex: none;
        background: radial-gradient(ellipse 80% 60% at 50% 38%, #3d2a63 0%, #241743 70%);
        border: 3px solid #ffd94d; border-radius: 10px;
        box-shadow: 0 6px 18px rgba(0,0,0,.55), inset 0 0 0 2px #241708;
      }
      .bigBtn {
        background: linear-gradient(180deg, #ffd94d, #e0a81c); color: #3a2703; border: none;
        border-radius: 999px; font-size: 17px; font-weight: 800; letter-spacing: 2px;
        padding: 11px 38px; margin-top: 8px; box-shadow: 0 6px 0 #8a5a12, 0 10px 22px rgba(0,0,0,.5);
      }
      .bigBtn:active { transform: translateY(3px); box-shadow: 0 3px 0 #8a5a12; }
      .howto { margin-top: 9px; font-size: 12px; color: #bfaed6; line-height: 1.6; max-width: 400px; }
      #endScreen { background: linear-gradient(180deg, rgba(43,16,32,.97), rgba(23,10,26,.99)); justify-content: flex-start; }
      #endScreen h2 { margin: 4px 0; font-size: clamp(24px, 6.6vw, 36px); color: #ffb3c2; text-shadow: 0 3px 0 #7c2440, 0 6px 18px rgba(0,0,0,.6); letter-spacing: 2px; }
      #endArt {
        width: min(52vw, 190px); height: auto; image-rendering: pixelated;
        background: radial-gradient(ellipse 85% 65% at 50% 36%, #5a2340 0%, #33102a 72%);
        border: 3px solid #ffd94d; border-radius: 12px;
        box-shadow: 0 12px 30px rgba(0,0,0,.6), inset 0 0 0 2px #241708;
      }
      #endText { font-size: 14.5px; line-height: 1.75; color: #ffe9f0; max-width: 430px; margin-top: 10px; }
      #endStats { margin-top: 8px; font-size: 13px; color: #ffd94d; }
      /* ---------- touch controls ---------- */
      #controls {
        position: absolute; left: 0; right: 0; bottom: 0; z-index: 6;
        display: flex; justify-content: space-between; align-items: flex-end;
        padding: 0 10px 10px; pointer-events: none;
      }
      #stickBase {
        width: 104px; height: 104px; border-radius: 50%; pointer-events: auto;
        background: radial-gradient(circle at 50% 42%, rgba(255,255,255,.07), rgba(20,12,34,.16) 72%);
        border: 1.5px solid rgba(255,217,77,.3);
        box-shadow: inset 0 0 14px rgba(0,0,0,.18);
        backdrop-filter: blur(2px); -webkit-backdrop-filter: blur(2px);
        position: relative; flex: none; touch-action: none;
      }
      #stickKnob {
        width: 46px; height: 46px; border-radius: 50%;
        background: radial-gradient(circle at 35% 30%, rgba(255,237,176,.72), rgba(232,182,42,.5) 68%, rgba(201,143,20,.45));
        border: 1.5px solid rgba(138,90,18,.55);
        box-shadow: 0 2px 6px rgba(0,0,0,.25), inset 0 1px 2px rgba(255,255,255,.5);
        position: absolute; left: 29px; top: 29px;
      }
      #btnCol { display: flex; gap: 10px; align-items: flex-end; pointer-events: auto; }
      .actBtn {
        width: 62px; height: 62px; border-radius: 50%; border: 1.5px solid rgba(255,255,255,.38);
        color: #fff; touch-action: none; padding: 0;
        display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 1px;
        background: radial-gradient(circle at 35% 28%, rgba(154,127,224,.32), rgba(43,27,80,.28) 72%);
        box-shadow: 0 3px 10px rgba(0,0,0,.22), inset 0 1px 0 rgba(255,255,255,.3);
        backdrop-filter: blur(3px); -webkit-backdrop-filter: blur(3px);
      }
      .actBtn svg { width: 25px; height: 25px; display: block; filter: drop-shadow(0 1px 1px rgba(0,0,0,.55)); }
      .actBtn .lbl { font-size: 10px; font-weight: 700; line-height: 1.1; letter-spacing: 1px; text-shadow: 0 1px 2px rgba(0,0,0,.6); }
      .actBtn.small { width: 54px; height: 54px; }
      .actBtn.small svg { width: 22px; height: 22px; }
      #btnJump { background: radial-gradient(circle at 35% 28%, rgba(99,207,130,.32), rgba(18,84,40,.28) 72%); }
      #btnAtk { background: radial-gradient(circle at 35% 28%, rgba(239,122,106,.32), rgba(112,30,24,.28) 72%); }
      .actBtn:active, .actBtn.held { filter: brightness(1.45); transform: translateY(1px); background-color: rgba(255,255,255,.14); box-shadow: 0 2px 6px rgba(0,0,0,.2), inset 0 1px 0 rgba(255,255,255,.35); }
      /* ---------- collapsible corner menu ---------- */
      /* Wrap spans the full frame height so the panel can never be taller
         than the frame: it shrinks and scrolls internally instead of being
         clipped by the frame's overflow:hidden. */
      #menuWrap {
        position: absolute; top: 6px; right: 6px; bottom: 6px; z-index: 20;
        display: flex; flex-direction: column; align-items: flex-end;
        pointer-events: none;
      }
      #menuBtn {
        width: 38px; height: 38px; min-height: 38px; border-radius: 50%; padding: 0; flex: none;
        pointer-events: auto;
        background: radial-gradient(circle at 35% 28%, #4a3a1c, #241708 75%);
        border: 2px solid #c9a227; box-shadow: 0 3px 8px rgba(0,0,0,.5), inset 0 1px 0 rgba(255,255,255,.25);
        display: flex; align-items: center; justify-content: center;
      }
      #menuBtn:active { filter: brightness(1.35); }
      #menuPanel {
        display: none; margin-top: 6px; width: 168px; flex: 1 1 auto; min-height: 0;
        overflow-y: auto; overflow-x: hidden;
        touch-action: pan-y; -webkit-overflow-scrolling: touch; overscroll-behavior: contain;
        pointer-events: auto;
        background: linear-gradient(180deg, rgba(43,29,13,.98), rgba(24,16,8,.98));
        border: 2px solid #c9a227; border-radius: 12px; padding: 5px;
        box-shadow: 0 12px 26px rgba(0,0,0,.6);
        scrollbar-width: thin; scrollbar-color: #c9a227 #241708;
      }
      #menuPanel::-webkit-scrollbar { width: 5px; }
      #menuPanel::-webkit-scrollbar-thumb { background: #c9a227; border-radius: 3px; }
      #menuPanel::-webkit-scrollbar-track { background: #241708; }
      #menuPanel.show { display: block; }
      #menuPanel button {
        display: block; width: 100%; text-align: left; margin: 0 0 3px;
        background: #33240f; border: 1px solid #6b4f2a; border-radius: 8px;
        color: #ffe9a8; font-size: 13.5px; font-weight: 600; padding: 6px 11px; min-height: 32px;
      }
      #menuPanel button:last-child { margin-bottom: 0; }
      #menuPanel button:active { background: #54350f; }
      #hint { margin-top: 8px; font-size: 12px; color: #9b8bb5; line-height: 1.6; text-align: center; }
      @media (prefers-reduced-motion: reduce) { #dlgNext { animation: none; } }
      /* title / ending overlays cover the frame: controls and HUD are not rendered beneath them */
      #frame.covered #controls, #frame.covered #hud, #frame.covered #menuWrap { display: none; }
      .actBtn:disabled { opacity: .5; }
      /* ---------- landscape: size the frame by height so it fills the screen ---------- */
      @media (orientation: landscape) and (max-height: 620px) {
        body { padding: 6px; justify-content: center; }
        #stage { max-width: none; width: 100%; display: flex; flex-direction: column; align-items: center; }
        #frame { width: auto; height: calc(100dvh - 14px); max-width: 100%; }
        #hint { display: none; }
      }
      /* ---------- fullscreen stage ---------- */
      #stage:fullscreen, #stage:-webkit-full-screen {
        max-width: none; width: 100%; height: 100%;
        display: flex; flex-direction: column; align-items: center; justify-content: center;
        background: #17101f; padding: 0;
      }
      #stage:fullscreen #hint, #stage:-webkit-full-screen #hint { display: none; }
      #stage:fullscreen #frame, #stage:-webkit-full-screen #frame {
        width: min(100%, calc(100dvh * 16 / 9)); height: auto; max-width: none;
        border-radius: 0;
      }
      /* fake fullscreen fallback (browsers without element fullscreen, e.g. iPhone Safari) */
      #stage.fake-full {
        position: fixed; inset: 0; z-index: 999; max-width: none; width: 100%; height: 100dvh;
        display: flex; flex-direction: column; align-items: center; justify-content: center;
        background: #17101f; padding: 0;
      }
      #stage.fake-full #hint { display: none; }
      #stage.fake-full #frame { width: min(100%, calc(100dvh * 16 / 9)); height: auto; max-width: none; border-radius: 0; }
      body.fake-full-lock { overflow: hidden; }
      /* portrait nudge: only while not fullscreen and not on the title screen flow */
      #rotateTip {
        display: none; position: absolute; left: 50%; bottom: 6px; transform: translateX(-50%);
        z-index: 4; pointer-events: none; white-space: nowrap;
        background: rgba(12,7,20,.78); border: 1px solid rgba(255,217,77,.5); border-radius: 999px;
        color: #ffe9a8; font-size: 11.5px; padding: 3px 10px;
      }
      @media (orientation: portrait) {
        #frame:not(.covered) #rotateTip { display: block; }
      }
/* === v2 polish & codex === */
#stage, #frame, #controls, .actBtn, #stickBase {
  touch-action: none;
  -webkit-user-select: none;
  user-select: none;
  -webkit-touch-callout: none;
}
#codex { position:absolute; inset:0; background:rgba(10,10,18,.78); display:none; align-items:center; justify-content:center; z-index:45; padding:10px; }
#codex.show { display:flex; }
#codexCard { width:min(92%, 410px); max-height:88%; overflow-y:auto; background:#1c1d2e; border:2px solid #ffd94d; border-radius:12px; padding:12px 14px; color:#f4f0e8; box-shadow:0 8px 28px rgba(0,0,0,.6); }
#codexCard h3 { margin:0 0 6px; font-size:15px; color:#ffd94d; text-align:center; }
.codexSub { font-size:11px; color:#a9b3cc; text-align:center; margin-bottom:8px; }
.codexItem { display:flex; align-items:flex-start; gap:8px; background:rgba(255,255,255,.05); border:1px solid rgba(255,255,255,.1); border-radius:8px; padding:7px 9px; margin-bottom:6px; }
.codexItem.locked { opacity:.55; }
.codexBadge { font-size:16px; line-height:1.1; flex-shrink:0; }
.codexTitle { font-size:12px; font-weight:700; color:#ffe9a8; }
.codexDesc { font-size:11px; color:#cfd6e6; margin-top:2px; line-height:1.35; }
#codexClose { width:100%; margin-top:6px; padding:8px; border:none; border-radius:8px; background:#ffd94d; color:#1b1526; font-weight:700; font-size:13px; cursor:pointer; }
#btnDash { background:linear-gradient(180deg,#3d7bd9,#244d8f); border-color:#8ec5ff; }
#btnDash.cd { opacity:.45; }
