/* ==========================================================
   養屍機　外殼（機殼、螢幕框、三顆鍵）
   這個檔只管外殼的形狀與質感。顏色全部來自 skins.css 的變數。
   版型有三種，由舞台自己的寬高決定，不靠 JS：
     desk     桌面或平板橫放：機殼浮在畫面中央
     portrait 手機直拿：機殼滿版，螢幕在上，三鍵在下
     wide     手機橫拿：機殼滿版，螢幕在左，三鍵在右
   邊界規則寫在 docs/外殼規格.md，改這個檔之前先讀。
   ========================================================== */

.ysj-stage{
  /* 螢幕內的圖層順序。遊戲畫面一律掛在這六層，不准自己另開更高的層。 */
  --z-scene:0; --z-hud:10; --z-menu:20; --z-panel:30; --z-cutscene:40; --z-toast:50; --z-lcdfx:90;

  /* 六種屬性的顏色 */
  --el-metal:#ecd277; --el-wood:#7fd36a; --el-water:#5fb4ff;
  --el-fire:#ff7a4a; --el-earth:#c9945a; --el-spell:#b78cff;

  /* 螢幕內介面的基本色 */
  --ui-ink:#17120e; --ui-ink-2:#241c15;
  --ui-paper:#f4e7c3; --ui-paper-2:#e2cf9f;
  --ui-cinnabar:#c8402f; --ui-gold:#dba743; --ui-jade:#58bfa5;
  --ui-line:rgba(255,255,255,.16);

  position:relative; width:100%; height:100%; min-height:0; overflow:hidden;
  container:ysj-stage / size;
  display:grid; place-items:center;
  background:
    radial-gradient(60% 46% at 50% 100%, rgba(0,0,0,.55), transparent 70%),
    radial-gradient(110% 82% at 50% 10%, var(--stage-a) 0%, var(--stage-b) 58%, #060505 100%);
  font-family:"Noto Sans TC","PingFang TC","Microsoft JhengHei",system-ui,sans-serif;
  color:#fff;
  -webkit-user-select:none; user-select:none; -webkit-touch-callout:none;
  touch-action:manipulation;
}
.ysj-stage *, .ysj-stage *::before, .ysj-stage *::after{ box-sizing:border-box; }

/* ---------- 機殼本體 ---------- */
.ysj-device{
  position:relative; isolation:isolate;
  container:ysj-device / size;
  width:min(94cqw, calc(86cqh * 1.6));
  aspect-ratio:1.6 / 1;
}

.ysj-body{
  --fw:clamp(9px, 1.9cqmin, 18px);          /* 螢幕框的厚度 */
  --key-size:clamp(52px, 11cqh, 104px);          /* B、C 鍵的直徑，最小 52px */
  --key-size-a:clamp(62px, 14.5cqh, 136px);       /* A 鍵的直徑，最小 62px */
  --grid:clamp(20px, 6.6cqmin, 50px);       /* 墨斗線的格距 */

  position:absolute; inset:0;
  display:grid; grid-template-columns:minmax(0,1fr) auto; gap:2.4cqw;
  padding:5cqh 2.6cqw 5cqh 3.2cqw;
  border-radius:8cqh;
  background:
    radial-gradient(48% 36% at 13% 0%, rgba(255,255,255,.2), transparent 72%),
    linear-gradient(158deg, var(--body-hi) 0%, var(--body) 36%, var(--body-lo) 100%);
  box-shadow:
    0 1.3cqh 0 var(--body-edge),
    0 3.6cqh 6.5cqh rgba(0,0,0,.6),
    inset 0 .55cqh .5cqh rgba(255,255,255,.32),
    inset 0 -1.1cqh 1.8cqh rgba(0,0,0,.32),
    inset .5cqh 0 .9cqh rgba(255,255,255,.08),
    inset -.5cqh 0 .9cqh rgba(0,0,0,.18);
  isolation:isolate;
}
.ysj-body > *{ position:relative; z-index:1; }

/* 墨斗線：封棺時彈在棺木上的墨線，做成殼上的壓紋 */
.ysj-body::before{
  content:""; position:absolute; inset:0; border-radius:inherit; pointer-events:none; z-index:0;
  background:
    repeating-linear-gradient(33deg,
      transparent 0 calc(var(--grid) - 2.6px),
      var(--line-dark) calc(var(--grid) - 2.6px) calc(var(--grid) - 1.3px),
      var(--line-light) calc(var(--grid) - 1.3px) var(--grid)),
    repeating-linear-gradient(-33deg,
      transparent 0 calc(var(--grid) - 2.6px),
      var(--line-dark) calc(var(--grid) - 2.6px) calc(var(--grid) - 1.3px),
      var(--line-light) calc(var(--grid) - 1.3px) var(--grid));
  opacity:var(--tex, .55);
}
/* 塑膠的細顆粒 */
.ysj-body::after{
  content:""; position:absolute; inset:0; border-radius:inherit; pointer-events:none; z-index:0;
  background-image:url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='180' height='180'%3E%3Cfilter id='n'%3E%3CfeTurbulence type='fractalNoise' baseFrequency='.85' numOctaves='2' stitchTiles='stitch'/%3E%3CfeColorMatrix values='0 0 0 0 .5 0 0 0 0 .5 0 0 0 0 .5 0 0 0 .6 0'/%3E%3C/filter%3E%3Crect width='100%25' height='100%25' filter='url(%23n)'/%3E%3C/svg%3E");
  mix-blend-mode:overlay; opacity:.3;
}
/* 透明殼才看得到的內部電路 */
.ysj-internals{
  position:absolute !important; inset:0; border-radius:inherit; pointer-events:none; z-index:-1 !important;
  opacity:var(--internals, 0);
  background-image:url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='240' height='160' fill='none' stroke='%23fff' stroke-width='2' stroke-linecap='round'%3E%3Crect x='18' y='20' width='64' height='44' rx='5'/%3E%3Ccircle cx='150' cy='46' r='20'/%3E%3Ccircle cx='150' cy='46' r='7'/%3E%3Cpath d='M82 34h30l12 12h6M82 52h22M50 64v30h70l14 14h60M196 30h26v70M20 110h40l10 10h30M170 92v40M30 140h80'/%3E%3Crect x='190' y='112' width='30' height='22' rx='3'/%3E%3Ccircle cx='24' cy='92' r='4'/%3E%3Ccircle cx='128' cy='140' r='4'/%3E%3C/svg%3E");
  background-size:clamp(150px, 34cqmin, 300px) auto;
  mix-blend-mode:soft-light;
}

/* 殼的合模線和四顆螺絲，只是裝飾，點不到 */
.ysj-seam{
  position:absolute !important; inset:1.7cqh; border-radius:6.4cqh; pointer-events:none; z-index:0 !important;
  box-shadow:inset 0 0 0 1.5px rgba(0,0,0,.2), inset 0 0 0 3px rgba(255,255,255,.1);
}
.ysj-screw{
  position:absolute !important; z-index:0 !important; pointer-events:none;
  width:clamp(7px, 1.7cqh, 14px); height:clamp(7px, 1.7cqh, 14px); border-radius:50%;
  background:
    linear-gradient(45deg, transparent 44%, rgba(0,0,0,.6) 44% 56%, transparent 56%),
    linear-gradient(-45deg, transparent 44%, rgba(0,0,0,.6) 44% 56%, transparent 56%),
    radial-gradient(circle at 36% 30%, #f4f4f4, #9a9a9a 55%, #4e4e4e 100%);
  box-shadow:0 1px 0 rgba(255,255,255,.25), inset 0 0 0 1px rgba(0,0,0,.4);
}
.ysj-screw[data-at="tl"]{ left:2.9cqh; top:2.9cqh; } .ysj-screw[data-at="bl"]{ left:2.9cqh; bottom:2.9cqh; }
.ysj-screw[data-at="tr"]{ right:2.9cqh; top:2.9cqh; } .ysj-screw[data-at="br"]{ right:2.9cqh; bottom:2.9cqh; }

/* ---------- 螢幕框 ---------- */
.ysj-frame{
  min-width:0; min-height:0;
  padding:var(--fw);
  border-radius:calc(var(--fw) * 2.1);
  background:linear-gradient(146deg, var(--frame-hi) 0%, var(--frame) 46%, var(--frame-lo) 100%);
  box-shadow:
    0 .5cqh 1.3cqh rgba(0,0,0,.5),
    0 0 0 1px rgba(0,0,0,.28),
    inset 0 1.5px 0 rgba(255,255,255,.6),
    inset 0 -2px 3px rgba(0,0,0,.4);
}
/* 四角的棺釘 */
.ysj-nail{
  position:absolute; width:calc(var(--fw) * .56); height:calc(var(--fw) * .56); border-radius:50%;
  background:radial-gradient(circle at 35% 30%, #fff, var(--nail) 45%, rgba(0,0,0,.75) 100%);
  box-shadow:0 1px 1px rgba(0,0,0,.55);
  pointer-events:none;
}
.ysj-nail[data-at="tl"]{ left:calc(var(--fw) * .22); top:calc(var(--fw) * .22); }
.ysj-nail[data-at="tr"]{ right:calc(var(--fw) * .22); top:calc(var(--fw) * .22); }
.ysj-nail[data-at="bl"]{ left:calc(var(--fw) * .22); bottom:calc(var(--fw) * .22); }
.ysj-nail[data-at="br"]{ right:calc(var(--fw) * .22); bottom:calc(var(--fw) * .22); }

/* 貼在框上的符 */
.ysj-charm{
  position:absolute; z-index:2; pointer-events:none;
  right:calc(var(--fw) * -1.55); top:50%;
  width:calc(var(--fw) * 2.5); height:27cqh;
  transform:translateY(-50%) rotate(1.6deg);
  border-radius:calc(var(--fw) * .22);
  background:linear-gradient(180deg, var(--charm-hi), var(--charm) 30%, var(--charm-lo));
  box-shadow:0 .5cqh 1cqh rgba(0,0,0,.45), inset 0 0 0 1px rgba(120,70,0,.25);
  display:grid; place-items:center;
}
.ysj-charm svg{ width:62%; height:86%; overflow:visible; }

/* ---------- 螢幕（LCD） ----------
   所有遊戲畫面都只能放在這裡面。
   contain 會把裡面的東西（包含 position:fixed）關在螢幕裡，畫不到框外。 */
.ysj-screen{
  position:relative; width:100%; height:100%;
  overflow:hidden; contain:layout paint style; isolation:isolate;
  container:ysj-screen / size;
  border-radius:calc(var(--fw) * 1.2);
  background:#0c1413;
  box-shadow:0 0 0 1.5px rgba(0,0,0,.75);
}
.ysj-layer{
  position:absolute; inset:0; pointer-events:none;
  font-size:clamp(13px, 3.5cqmin, 23px); line-height:1.35;
}
.ysj-layer > *{ pointer-events:auto; }
.ysj-layer[data-layer="scene"]{ z-index:var(--z-scene); }
.ysj-layer[data-layer="hud"]{ z-index:var(--z-hud); }
.ysj-layer[data-layer="menu"]{ z-index:var(--z-menu); }
.ysj-layer[data-layer="panel"]{ z-index:var(--z-panel); }
.ysj-layer[data-layer="cutscene"]{ z-index:var(--z-cutscene); }
.ysj-layer[data-layer="toast"]{ z-index:var(--z-toast); }

/* 舊液晶的質感：掃描線、暗角、玻璃反光。永遠在最上面，而且點不到。 */
.ysj-lcdfx{
  position:absolute; inset:0; z-index:var(--z-lcdfx); pointer-events:none; border-radius:inherit;
  background:
    linear-gradient(128deg, rgba(255,255,255,.13) 0%, rgba(255,255,255,.03) 22%, transparent 34%),
    radial-gradient(ellipse at 50% 46%, transparent 58%, rgba(0,0,0,.36) 100%),
    repeating-linear-gradient(to bottom, rgba(0,0,0,.13) 0 1px, transparent 1px 3px);
  box-shadow:inset 0 .7cqh 1.8cqh rgba(0,0,0,.6), inset 0 0 0 1px rgba(0,0,0,.55);
}
.ysj-stage[data-lcd="off"] .ysj-lcdfx{
  background:linear-gradient(128deg, rgba(255,255,255,.1) 0%, transparent 30%);
}

/* ---------- 按鍵區 ---------- */
.ysj-deck{
  display:flex; flex-direction:column; align-items:center; justify-content:center;
  gap:3.2cqh; padding-inline:.4cqw;
  min-width:calc(var(--key-size-a) + 2.4cqw);
}
.ysj-deck__top{ display:flex; flex-direction:column; align-items:center; gap:1.1cqh; }
.ysj-brand{
  font-family:"Noto Serif TC","Songti TC","PMingLiU",serif; font-weight:900;
  font-size:clamp(11px, 2.7cqh, 22px); letter-spacing:.12em; margin-right:-.12em; white-space:nowrap;
  color:var(--print);
  text-shadow:0 1px 0 rgba(255,255,255,.22), 0 -1px 0 rgba(0,0,0,.3);
}
.ysj-led{
  width:clamp(5px, 1cqh, 9px); height:clamp(5px, 1cqh, 9px); border-radius:50%;
  background:radial-gradient(circle at 40% 35%, #fff, var(--led) 55%);
  box-shadow:0 0 6px 1px var(--led), inset 0 0 1px rgba(0,0,0,.6);
}
.ysj-speaker{ display:flex; gap:clamp(4px, .9cqh, 8px); transform:rotate(-24deg); margin-top:.6cqh; }
.ysj-speaker i{
  width:clamp(3px, .62cqh, 6px); height:clamp(14px, 3.2cqh, 28px); border-radius:99px;
  background:rgba(0,0,0,.58); box-shadow:inset 0 1px 2px rgba(0,0,0,.8), 0 1px 0 rgba(255,255,255,.2);
}

/* 三顆鍵。外觀是 .ysj-key__cap，真正吃觸控的是 button 本身加外擴 8px。 */
.ysj-key{
  --k:var(--key-size);
  position:relative; flex:none; width:var(--k); height:var(--k);
  border:0; padding:0; margin:0; border-radius:50%; background:none;
  font:inherit; color:var(--key-ink); cursor:pointer;
  -webkit-tap-highlight-color:transparent; touch-action:manipulation; outline:none;
}
.ysj-key[data-key="A"]{ --k:var(--key-size-a); }
.ysj-key::before{            /* 殼上凹下去的鍵座 */
  content:""; position:absolute; inset:-8%; border-radius:50%;
  background:radial-gradient(circle at 50% 42%, rgba(0,0,0,.62), rgba(0,0,0,.28) 72%);
  box-shadow:inset 0 2px 4px rgba(0,0,0,.65), 0 1.5px 0 rgba(255,255,255,.2);
}
.ysj-key::after{ content:""; position:absolute; inset:-8px; border-radius:50%; }   /* 外擴的觸控範圍 */
.ysj-key__cap{
  position:absolute; inset:0; border-radius:50%;
  display:grid; place-items:center;
  background:radial-gradient(circle at 34% 26%, var(--key-hi) 0%, var(--key) 52%, var(--key-lo) 100%);
  box-shadow:
    0 calc(var(--k) * .075) 0 var(--key-edge),
    0 calc(var(--k) * .13) calc(var(--k) * .16) rgba(0,0,0,.5),
    inset 0 calc(var(--k) * -.05) calc(var(--k) * .09) rgba(0,0,0,.2),
    inset 0 calc(var(--k) * .035) calc(var(--k) * .05) rgba(255,255,255,.55);
  transform:translateY(calc(var(--k) * -.045));
  transition:transform .07s ease-out, box-shadow .07s ease-out, filter .15s;
}
.ysj-key__cap svg{ width:46%; height:46%; opacity:.86; filter:drop-shadow(0 1px 0 rgba(255,255,255,.3)); }
.ysj-key.is-down .ysj-key__cap{
  transform:translateY(calc(var(--k) * .03));
  box-shadow:
    0 calc(var(--k) * .012) 0 var(--key-edge),
    0 calc(var(--k) * .03) calc(var(--k) * .05) rgba(0,0,0,.45),
    inset 0 calc(var(--k) * .04) calc(var(--k) * .09) rgba(0,0,0,.28),
    inset 0 calc(var(--k) * .015) calc(var(--k) * .03) rgba(255,255,255,.3);
}
.ysj-key:focus-visible .ysj-key__cap{ outline:3px solid #fff; outline-offset:3px; }
.ysj-key__tag{               /* 印在殼上的小字母 */
  position:absolute; right:-4%; bottom:-10%;
  font-weight:800; font-size:calc(var(--k) * .2); line-height:1; color:var(--print);
  text-shadow:0 1px 0 rgba(255,255,255,.2); pointer-events:none;
}
/* 停用：變暗、按不下去 */
.ysj-key:disabled{ cursor:default; }
.ysj-key:disabled .ysj-key__cap{ filter:saturate(.25) brightness(.62); transform:translateY(calc(var(--k) * .012)); }
.ysj-key:disabled .ysj-key__cap svg{ opacity:.45; }
/* 敲擊模式：A 鍵外圈發光，提示可以狂按 */
.ysj-key[data-mode="tap"]::before{ animation:ysj-tap-ring 1.05s ease-in-out infinite; }
@keyframes ysj-tap-ring{
  0%,100%{ box-shadow:inset 0 2px 4px rgba(0,0,0,.65), 0 0 0 0 rgba(255,214,96,0); }
  50%{ box-shadow:inset 0 2px 4px rgba(0,0,0,.65), 0 0 0 calc(var(--k) * .11) rgba(255,214,96,.5); }
}

/* ---------- 吊繩（只有桌面版型看得到） ---------- */
.ysj-strap{
  position:absolute; left:-1.8cqw; top:-9.6cqh; width:13.5cqh; height:21.4cqh;
  pointer-events:none; z-index:-1; filter:drop-shadow(0 .8cqh 1cqh rgba(0,0,0,.5));
}
.ysj-strap svg{ width:100%; height:100%; overflow:visible; }

/* ==========================================================
   版型二：手機直拿
   ========================================================== */
@container ysj-stage (orientation: portrait){
  .ysj-device{ width:100cqw; height:100cqh; aspect-ratio:auto; }
  .ysj-body{
    --key-size:clamp(56px, 16.5cqw, 80px);
    --key-size-a:clamp(78px, 23.5cqw, 108px);
    --grid:clamp(22px, 9cqw, 46px);
    grid-template-columns:none; grid-template-rows:minmax(0,1fr) auto; gap:0;
    padding:max(8px, env(safe-area-inset-top)) max(8px, env(safe-area-inset-right))
            max(10px, env(safe-area-inset-bottom)) max(8px, env(safe-area-inset-left));
    border-radius:0;
    box-shadow:inset 0 3px 3px rgba(255,255,255,.26), inset 0 -8px 14px rgba(0,0,0,.3);
  }
  .ysj-deck{
    flex-direction:row; justify-content:flex-start; gap:4.2cqw;
    height:clamp(122px, 18.5cqh, 178px); min-width:0; padding:0 3.4cqw 0 4.4cqw;
  }
  .ysj-key[data-key="C"]{ order:1; }
  .ysj-key[data-key="B"]{ order:2; }
  .ysj-deck__top{ order:3; flex:1; min-width:0; align-self:flex-end; padding-bottom:1.6cqh; gap:6px; }
  .ysj-key[data-key="A"]{ order:4; }
  .ysj-speaker{ display:none; }
  .ysj-brand{ font-size:clamp(11px, 3cqw, 15px); letter-spacing:.06em; margin-right:-.06em; }
  .ysj-charm{
    right:auto; left:50%; top:auto; bottom:calc(var(--fw) * -5.2);
    width:calc(var(--fw) * 2.7); height:calc(var(--fw) * 6.6);
    transform:translateX(-50%) rotate(-2deg);
  }
  .ysj-strap, .ysj-seam, .ysj-screw{ display:none; }
}

/* ==========================================================
   版型三：手機橫拿（高度很矮的橫向畫面）
   ========================================================== */
@container ysj-stage (orientation: landscape) and (max-height: 540px){
  .ysj-device{ width:100cqw; height:100cqh; aspect-ratio:auto; }
  .ysj-body{
    --key-size:clamp(48px, 15.5cqh, 68px);
    --key-size-a:clamp(60px, 21cqh, 90px);
    gap:1.8cqw;
    padding:max(7px, env(safe-area-inset-top)) max(10px, env(safe-area-inset-right))
            max(7px, env(safe-area-inset-bottom)) max(10px, env(safe-area-inset-left));
    border-radius:0;
    box-shadow:inset 0 3px 3px rgba(255,255,255,.26), inset 0 -8px 14px rgba(0,0,0,.3);
  }
  .ysj-deck{ gap:3.4cqh; }
  .ysj-speaker{ display:none; }
  .ysj-brand{ font-size:clamp(10px, 3.4cqh, 14px); }
  .ysj-charm{ height:34cqh; }
  .ysj-strap, .ysj-seam, .ysj-screw{ display:none; }
}

/* ---------- 邊界檢查用的描線（網址加 ?bounds=1） ---------- */
.ysj-stage[data-bounds="on"] .ysj-screen{ outline:2px dashed #3dff9a; outline-offset:-2px; }
.ysj-stage[data-bounds="on"] .ysj-key{ outline:2px dashed #ff4dd8; outline-offset:8px; }
.ysj-stage[data-bounds="on"] .ysj-deck{ outline:1px dashed #ffd84d; }
.ysj-guard-badge{
  position:absolute; left:8px; bottom:8px; z-index:5; max-width:min(92%, 520px);
  padding:6px 10px; border-radius:8px; font:600 12px/1.45 system-ui, sans-serif;
  background:rgba(0,0,0,.82); color:#9dffc8; pointer-events:none; white-space:pre-wrap;
}
.ysj-guard-badge[data-ok="false"]{ color:#ff9a9a; }

@media (prefers-reduced-motion: reduce){
  .ysj-key__cap{ transition:none; }
  .ysj-key[data-mode="tap"]::before{ animation:none; }
}
