/* platform 模組的樣式：版面（含安全區）、載入畫面、標題畫面、側欄、PWA 提示、效能面板 */
:root {
  --sat: env(safe-area-inset-top, 0px); --sar: env(safe-area-inset-right, 0px);
  --sab: env(safe-area-inset-bottom, 0px); --sal: env(safe-area-inset-left, 0px);
  --side: clamp(240px, 21vw, 320px); --gap: clamp(18px, 3.2vw, 56px);
}

/* ================= 右側資訊欄 ================= */
.side { gap: 18px; max-width: var(--side); align-self: center; }
.side-brand { display: flex; gap: 14px; align-items: center; }
.side-brand img { width: 64px; height: 64px; border-radius: 18px; box-shadow: 0 10px 24px -12px rgba(90, 55, 25, .55); }
.side h1 { font-size: 30px; line-height: 1.15; letter-spacing: .02em; }
.side .eyebrow { font-size: 12px; letter-spacing: .18em; color: var(--sub); font-weight: 600; }
.side-lede { font-size: 15px; color: var(--ink); line-height: 1.55; }
.side-lede b { color: #e8566f; }
.side-stat { font-size: 13px; color: var(--sub); background: var(--card); border: 1px solid var(--line); border-radius: 14px; padding: 9px 12px; line-height: 1.6; }
.side-stat:empty { display: none; }
.side-stat b { color: var(--ink); }
.side h2 { font-size: 12px; letter-spacing: .2em; color: var(--sub); font-weight: 700; margin-bottom: 8px; }
.side ol { padding-left: 20px; display: grid; gap: 5px; }
.side li { font-size: 14px; color: var(--sub); }
.side li b { color: var(--ink); }
.side-keys dl { display: grid; grid-template-columns: auto 1fr auto 1fr; gap: 8px 10px; align-items: center; font-size: 13px; color: var(--sub); }
.side-keys dd { margin: 0; }
kbd { display: inline-block; min-width: 30px; text-align: center; font: 700 12px/1 ui-monospace, Menlo, monospace; color: var(--ink); background: var(--card); border: 1px solid var(--line); border-bottom-width: 3px; border-radius: 8px; padding: 5px 7px; }
.side-rotate { display: none; font-size: 13px; color: var(--sub); }
.side-install {
  appearance: none; border: 3px solid var(--ink); background: linear-gradient(#ffe680, #ffb020); border-radius: 16px; padding: 10px 14px; font-weight: 800; font-size: 14px;
  box-shadow: 0 5px 0 var(--ink); transition: transform .18s var(--spring), box-shadow .18s var(--ease);
}
.side-install:active { transform: translateY(4px); box-shadow: 0 1px 0 var(--ink); }
.side-dev { display: none; flex-direction: column; gap: 10px; border: 2px dashed #e0cdb8; border-radius: 16px; padding: 12px; }
body.dev .side-dev { display: flex; }
.side-foot { font-size: 12px; color: var(--sub); line-height: 1.7; }
@media (hover: hover) { .side-install:hover, .side .link:hover { filter: brightness(1.04); } }

/* ================= 版面 ================= */
/* 桌機／平板橫式：遊戲畫面＋右側資訊欄，兩者一起置中；畫面高度同時受視窗高、寬限制 */
body {
  grid-template-columns: auto var(--side); gap: var(--gap);
  padding: calc(var(--pad) + var(--sat)) calc(var(--pad) + var(--sar)) calc(var(--pad) + var(--sab)) calc(var(--pad) + var(--sal));
}
.stage {
  height: min(1180px, calc(100dvh - 2 * var(--pad) - var(--sat) - var(--sab)), calc((100vw - var(--side) - var(--gap) - 2 * var(--pad) - var(--sal) - var(--sar)) * 16 / 9));
}
/* 直式（手機、平板直拿）：只放遊戲畫面，盡量放大，避開瀏海與底部橫條 */
@media (orientation: portrait), (max-width: 760px) {
  body { display: grid; grid-template-columns: 1fr; place-items: center; place-content: center; gap: 0; padding: var(--sat) var(--sar) var(--sab) var(--sal); }
  .stage { width: min(calc(100vw - var(--sal) - var(--sar)), calc((100dvh - var(--sat) - var(--sab)) * 9 / 16)); height: auto; max-height: none; margin: 0 auto; border-radius: 0; }
  .side { display: none; }
}
@media (orientation: portrait) and (min-width: 600px) {   /* 平板直拿：兩側有空間，留圓角 */
  body { padding: calc(16px + var(--sat)) calc(16px + var(--sar)) calc(16px + var(--sab)) calc(16px + var(--sal)); }
  .stage { width: min(calc(100vw - 32px - var(--sal) - var(--sar)), calc((100dvh - 32px - var(--sat) - var(--sab)) * 9 / 16)); border-radius: 26px; }
}
/* 手機橫拿：畫面滿高，旁邊放精簡版資訊欄（提示轉直） */
@media (orientation: landscape) and (max-height: 560px) {
  body { display: flex; justify-content: center; align-items: center; gap: clamp(14px, 3vw, 32px); padding: var(--sat) calc(12px + var(--sar)) var(--sab) calc(12px + var(--sal)); }
  .stage { height: calc(100dvh - var(--sat) - var(--sab)); width: auto; max-height: none; margin: 0; border-radius: 0; flex: none; }
  .side { display: flex; width: min(300px, 38vw); max-height: calc(100dvh - var(--sat) - var(--sab)); overflow: auto; gap: 10px; padding: 10px 0; }
  .side-how, .side-keys { display: none; }
  .side-brand img { width: 44px; height: 44px; border-radius: 13px; }
  .side h1 { font-size: 22px; }
  .side-lede { font-size: 13px; }
}
@media (orientation: landscape) and (max-height: 560px) and (pointer: coarse) { .side-rotate { display: block; } }
body.bot .side, body.bot #boot { display: none; }

/* ================= 載入畫面 ================= */
#boot {
  position: fixed; inset: 0; z-index: 50; display: grid; place-content: center; justify-items: center; gap: 12px;
  background: radial-gradient(120% 90% at 50% 18%, #fff8f1 0%, var(--bg) 66%); transition: opacity .5s var(--ease), visibility 0s .5s;
}
#boot.out { opacity: 0; visibility: hidden; }
.boot-mochi { position: relative; width: 140px; height: 150px; display: grid; justify-items: center; align-items: end; }
.boot-mochi svg { position: relative; z-index: 1; overflow: visible; transform-origin: 50% 100%; animation: bootHop .86s infinite; }
.boot-shadow { position: absolute; bottom: 2px; width: 92px; height: 14px; border-radius: 50%; background: rgba(90, 60, 30, .18); animation: bootShadow .86s infinite; }
@keyframes bootHop {
  0% { transform: translateY(0) scale(1.16, .84); animation-timing-function: cubic-bezier(.2, .7, .4, 1); }
  14% { transform: translateY(-8px) scale(.9, 1.12); animation-timing-function: cubic-bezier(.25, .8, .35, 1); }
  50% { transform: translateY(-52px) scale(1, 1); animation-timing-function: cubic-bezier(.6, 0, .8, .3); }
  86% { transform: translateY(-4px) scale(.94, 1.08); animation-timing-function: ease-out; }
  100% { transform: translateY(0) scale(1.16, .84); }
}
@keyframes bootShadow { 0%, 100% { transform: scale(1.1); opacity: 1; } 50% { transform: scale(.6); opacity: .5; } }
.boot-name { font: 900 30px/1.1 "PingFang TC", "Noto Sans TC", system-ui, sans-serif; color: #ff7d93; -webkit-text-stroke: 6px var(--ink); paint-order: stroke fill; letter-spacing: .06em; margin-top: 4px; }
.boot-bar { width: 188px; height: 14px; border: 3px solid var(--ink); border-radius: 10px; background: #fffaf2; overflow: hidden; }
.boot-bar i { display: block; height: 100%; background: linear-gradient(#ffe680, #ffb020); transform: scaleX(0); transform-origin: 0 50%; transition: transform .35s var(--ease); }
.boot-tip { font-size: 13px; color: var(--sub); min-height: 1.6em; }

/* ================= 標題畫面（在 #ui 1080×1920 設計尺寸裡） ================= */
#modal:has(.title-screen) { background: none; backdrop-filter: none; -webkit-backdrop-filter: none; }
.title-screen {
  position: absolute; inset: 0; display: flex; flex-direction: column; align-items: center; justify-content: center; padding: 120px 0 170px; overflow: hidden;
  background:
    linear-gradient(rgba(255, 125, 147, .075) 50%, transparent 0) 0 0 / 144px 144px,
    linear-gradient(90deg, rgba(255, 125, 147, .075) 50%, transparent 0) 0 0 / 144px 144px,
    radial-gradient(90% 55% at 50% 24%, #fffaf3 0%, #ffefe2 55%, #f8dccb 100%);
}
.ts-sky { position: absolute; inset: 0; width: 100%; height: 100%; pointer-events: none; }
.ts-corner { position: absolute; right: 30px; top: 36px; display: flex; gap: 20px; z-index: 2; }
.ts-logo { position: relative; flex: none; display: grid; justify-items: center; pointer-events: none; }
.ts-hero { width: 260px; height: 260px; transform-origin: 50% 88%; filter: drop-shadow(0 16px 0 rgba(74, 44, 31, .1)); }
body.ready .ts-hero { animation: heroIn .9s var(--spring) both, heroHop 2.6s var(--ease) 1.2s infinite; }
@keyframes heroIn { from { transform: translateY(-120px) scale(.4); opacity: 0; } to { transform: none; opacity: 1; } }
@keyframes heroHop {
  0%, 62%, 100% { transform: none; }
  70% { transform: scale(1.12, .88); }
  80% { transform: translateY(-46px) scale(.94, 1.08); }
  90% { transform: scale(1.06, .94); }
}
.ts-logo h2 { display: flex; gap: 2px; margin-top: -18px; font: 900 158px/1.05 "PingFang TC", "Noto Sans TC", "Arial Rounded MT Bold", sans-serif; }
.ts-logo h2 span {
  display: inline-block; opacity: 0; color: var(--c, #ff7d93); -webkit-text-stroke: 16px var(--ink); paint-order: stroke fill;
  text-shadow: 0 14px 0 var(--ink), 0 26px 30px rgba(90, 55, 25, .25);
}
.ts-logo h2 span:nth-child(1) { --c: #ff7d93; } .ts-logo h2 span:nth-child(2) { --c: #ffb627; } .ts-logo h2 span:nth-child(3) { --c: #9fd36a; }
.ts-logo h2 span:nth-child(4) { --c: #ff8fb5; } .ts-logo h2 span:nth-child(5) { --c: #b89cff; }
body.ready .ts-logo h2 span {
  animation: logoDrop .8s var(--spring) both, logoBob 3.2s ease-in-out infinite;
  animation-delay: calc(var(--i) * 85ms + 180ms), calc(var(--i) * 160ms + 1.3s);
}
@keyframes logoDrop { 0% { opacity: 0; transform: translateY(-260px) rotate(-14deg) scale(.6); } 60% { opacity: 1; } 100% { opacity: 1; transform: none; } }
@keyframes logoBob { 0%, 100% { transform: none; } 50% { transform: translateY(-14px) rotate(calc((var(--i) - 2) * 1.5deg)); } }
.ts-tag {
  margin-top: 34px; background: var(--ink); color: #ffe7a8; font-weight: 800; font-size: 42px; letter-spacing: .06em; padding: 8px 40px; border-radius: 999px;
  transform: rotate(-2deg); box-shadow: 0 10px 24px -10px rgba(74, 44, 31, .6); opacity: 0;
}
body.ready .ts-tag { animation: tagIn .6s var(--spring) .75s both; }
@keyframes tagIn { from { opacity: 0; transform: rotate(-2deg) scale(.5); } to { opacity: 1; transform: rotate(-2deg); } }
.ts-card { position: relative; flex: 0 1 auto; min-height: 0; width: 920px; margin-top: 52px; padding: 40px 44px 44px; max-height: 1040px; box-shadow: 0 18px 0 rgba(74, 44, 31, .16); }
body:not(.ready) .ts-card { opacity: 0; }
body.ready .ts-card { animation: cardUp .7s var(--ease) .55s both; }
@keyframes cardUp { from { opacity: 0; transform: translateY(80px); } }
.ts-stats { display: grid; grid-template-columns: repeat(3, 1fr); gap: 18px; margin: 0 0 32px; text-align: center; }
.ts-stats > div { background: #fff1dc; border-radius: 30px; padding: 16px 8px 14px; }
.ts-stats dt { font-size: 30px; color: var(--sub); }
.ts-stats dd { font-size: 42px; font-weight: 800; text-align: center; line-height: 1.25; white-space: nowrap; }
.ts-slot:empty, .ts-more:empty, .ts-corner:empty { display: none; }
.ts-slot { margin: 0 0 28px; }
.ts-main .b { height: 144px; font-size: 56px; }
.ts-main #mStart { flex: 1.7; position: relative; overflow: hidden; }
.ts-main #mStart::after { content: ""; position: absolute; inset: 0; background: linear-gradient(100deg, transparent 35%, rgba(255, 255, 255, .7) 48%, transparent 60%); transform: translateX(-130%); animation: shine 3.4s var(--ease) 1.8s infinite; pointer-events: none; }
@keyframes shine { 0% { transform: translateX(-130%); } 40%, 100% { transform: translateX(130%); } }
.ts-more { display: flex; flex-wrap: wrap; gap: 18px; justify-content: center; margin-top: 26px; }
.ts-more .b { height: 104px; font-size: 38px; }
.ts-foot { position: absolute; bottom: 40px; font-size: 28px; color: var(--sub); letter-spacing: .12em; opacity: .8; }
@media (prefers-reduced-motion: reduce) {
  .ts-logo h2 span, .ts-tag, .ts-card { opacity: 1 !important; }
  .boot-mochi svg { transform: none; }
}

/* ================= PWA：新版本提示 ================= */
.pwa-toast {
  position: fixed; left: 50%; bottom: calc(16px + var(--sab)); z-index: 60; transform: translateX(-50%);
  display: flex; gap: 8px; align-items: center; background: var(--ink); color: #fff3dc; padding: 7px 7px 7px 16px; border-radius: 999px;
  font-weight: 700; font-size: 14px; box-shadow: 0 16px 34px -14px rgba(60, 30, 15, .7); animation: toastIn .5s var(--spring); white-space: nowrap;
}
@keyframes toastIn { from { transform: translate(-50%, 30px); opacity: 0; } }
.pwa-toast button { appearance: none; border: 0; font: inherit; cursor: pointer; }
.pwa-toast .go { background: linear-gradient(#ffe680, #ffb020); color: var(--ink); border-radius: 999px; padding: 6px 14px; font-weight: 800; }
.pwa-toast .go[disabled] { opacity: .6; }
.pwa-toast .no { background: none; color: #fff3dc; font-size: 18px; padding: 0 8px; opacity: .8; }

/* ================= 效能面板（?perf=1） ================= */
#perfBox { position: fixed; left: 8px; top: calc(8px + var(--sat)); z-index: 99; background: rgba(40, 24, 16, .86); color: #fff3dc; font: 600 12px/1.45 ui-monospace, Menlo, monospace; padding: 8px 10px; border-radius: 10px; pointer-events: none; min-width: 190px; }
#perfBox b { color: #ffd84d; }
#perfBox small { display: block; margin-top: 4px; opacity: .75; font-size: 11px; }

/* [總管] 標題內容多（工廠、地圖、續玩、轉生…）時，logo 自動縮小，主要按鈕一定看得到 */
.title-screen.dense { padding: 70px 0 120px; }
.dense .ts-hero { width: 170px; height: 170px; }
.dense .ts-logo h2 { font-size: 120px; margin-top: -10px; }
.dense .ts-tag { transform: scale(.85); }
.dense .ts-card { margin-top: 30px; max-height: 1400px; padding-top: 32px; }
.dense .ts-stats { margin-bottom: 22px; }
.dense .ts-stats dd { font-size: 38px; }
.dense .mp .th { aspect-ratio: 260 / 220; }
.title-screen.denser { padding: 50px 0 96px; }
.denser .ts-hero { display: none; }
.denser .ts-logo h2 { font-size: 104px; margin-top: 0; }
.denser .ts-card { max-height: 1560px; }
.denser .mp .th { aspect-ratio: 260 / 170; }
.denser .ts-slot { margin-bottom: 20px; }
