:root {
  --bg: #0f131b;
  --panel: #171d29;
  --panel2: #202838;
  --line: #2c3548;
  --text: #e6ebf5;
  --dim: #8a95ab;
  --accent: #4aa3ff;
  --good: #46c36b;
  --bad: #e5534b;
  --warn: #e9b43a;
}
* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; scrollbar-width: thin; scrollbar-color: #3a4558 transparent; }
html, body { margin: 0; height: 100%; background: #05070a; color: var(--text); font: 14px/1.35 "Microsoft JhengHei", "PingFang TC", "Noto Sans TC", system-ui, sans-serif; overflow: hidden; user-select: none; -webkit-user-select: none; overscroll-behavior: none; }
body { display: flex; justify-content: center; }
button { font: inherit; color: inherit; cursor: pointer; }
.dim { color: var(--dim); }
[hidden] { display: none !important; }

/* 手機直式比例：寬螢幕/桌面瀏覽器時限制寬度，兩側留白；真实手機(窄螢幕)則填滿 */
#app { position: relative; flex: 0 0 auto; width: min(100vw, 430px, 50vh); height: 100%; display: flex; flex-direction: column; background: var(--bg); overflow: hidden; }
@media (min-width: 600px) { #app { box-shadow: 0 0 0 1px #2c3548, 0 0 40px rgba(0, 0, 0, .6); } }

/* ---------- HUD ---------- */
#hud { flex: 0 0 auto; padding: 6px 8px 6px; background: var(--panel); border-bottom: 1px solid var(--line); padding-top: max(6px, env(safe-area-inset-top)); }
.hud-row { display: flex; align-items: center; gap: 6px; margin-bottom: 5px; }
.hud-info { flex: 1; display: flex; justify-content: space-around; font-size: 15px; }
.hud-info b { font-size: 17px; }
.ibtn { min-width: 34px; height: 32px; padding: 0 8px; border: 1px solid var(--line); border-radius: 8px; background: var(--panel2); font-weight: 700; }
.ibtn.wide { min-width: 44px; }
.ibtn:active { background: #2b3550; }
.bar { position: relative; height: 18px; border-radius: 9px; background: #0b0e14; border: 1px solid var(--line); overflow: hidden; margin-top: 3px; }
.bar .fill { position: absolute; inset: 0 auto 0 0; width: 100%; background: linear-gradient(90deg, #2e9d55, #46c36b); transition: width .15s; }
.bar.stab.low .fill { background: linear-gradient(90deg, #b8382f, #e5534b); }
.bar.stab.mid .fill { background: linear-gradient(90deg, #b8861f, #e9b43a); }
.bar.chaos .fill { background: linear-gradient(90deg, #6d3fc0, #a56bff); }
.bar .txt { position: absolute; inset: 0; text-align: center; font-size: 12px; line-height: 17px; text-shadow: 0 1px 2px #000; }

/* ---------- 地圖 ---------- */
#mapWrap { position: relative; flex: 1 1 0; min-height: 120px; background: #0b0e14; overflow: hidden; }
#map { position: absolute; inset: 0; width: 100%; height: 100%; touch-action: none; display: block; }
#mapTop { position: absolute; left: 8px; top: 6px; pointer-events: none; font-size: 12px; color: #ffd36b; text-shadow: 0 1px 3px #000; }
#zoomBtns { position: absolute; right: 8px; top: 8px; display: flex; flex-direction: column; gap: 6px; }
#zoomBtns .ibtn { background: rgba(23, 29, 41, .85); }
#startRow { flex: 0 0 auto; padding: 5px 8px; border-bottom: 1px solid var(--line); }
#startBtn { width: 100%; height: 38px; border-radius: 10px; border: 1px solid #7bc0ff; background: #2563b0; font-weight: 700; font-size: 15px; }
#startBtn:active { filter: brightness(1.2); }
#startBtn:disabled { opacity: .55; filter: grayscale(.4); }
#startBtn[data-mode=draw] { background: #7a4fc4; border-color: #b794f4; }
#startBtn[data-mode=start] { background: #2a9d57; border-color: #6fe3a0; }
#toast { position: absolute; left: 50%; top: 12%; transform: translateX(-50%); max-width: 86%; padding: 6px 14px; border-radius: 16px; background: rgba(0, 0, 0, .8); border: 1px solid var(--line); font-size: 13px; opacity: 0; transition: opacity .2s; pointer-events: none; text-align: center; }
#toast.show { opacity: 1; }
#toast.bad { border-color: var(--bad); color: #ffb3ae; }

/* ---------- 下方資訊面板 ---------- */
#panel { flex: 0 0 auto; height: clamp(190px, 38%, 340px); display: flex; flex-direction: column; background: var(--panel); border-top: 1px solid var(--line); padding-bottom: env(safe-area-inset-bottom); }
#app.collapsed #panel { height: auto; }
#app.collapsed #pages { display: none; }
#app.collapsed #btnCollapse { transform: rotate(180deg); }
#panelBar { flex: 0 0 auto; display: flex; gap: 6px; padding: 5px 8px; align-items: center; border-bottom: 1px solid var(--line); }
.tab { flex: 1; height: 30px; border: 1px solid var(--line); border-radius: 8px; background: var(--panel2); color: var(--dim); }
.tab.active { color: #fff; border-color: var(--accent); background: #1d3350; }
#pages { flex: 1 1 0; min-height: 0; display: flex; overflow-x: auto; overflow-y: hidden; scroll-snap-type: x mandatory; scrollbar-width: none; }
#pages::-webkit-scrollbar { display: none; }
.page { flex: 0 0 100%; scroll-snap-align: start; overflow-y: auto; padding: 8px; min-width: 0; }

.hint { color: var(--dim); font-size: 12.5px; margin-bottom: 6px; }
.row { display: flex; gap: 6px; align-items: center; flex-wrap: wrap; }
.btn { height: 32px; padding: 0 12px; border: 1px solid var(--line); border-radius: 8px; background: var(--panel2); }
.btn:disabled { opacity: .4; cursor: not-allowed; }
.btn.primary, .primary { background: #2563b0; border-color: #4aa3ff; font-weight: 700; }
.btn.danger { border-color: #8a3a35; color: #ffb3ae; }
.btn.sel { border-color: var(--warn); background: #4a3a14; }
.primary.big { width: 100%; height: 44px; font-size: 16px; border-radius: 10px; margin-top: 12px; border: 1px solid #4aa3ff; }

/* 抽塔卡片 / 放置物件 */
.cards { display: flex; gap: 6px; overflow-x: auto; padding-bottom: 4px; touch-action: pan-x; }
.card { flex: 1 1 0; min-width: 70px; border: 1px solid var(--line); border-radius: 10px; background: var(--panel2); padding: 5px 3px; text-align: center; font-size: 12px; }
.card:active { border-color: var(--accent); }
.card canvas { width: 40px; height: 40px; }
.card .nm { font-weight: 700; font-size: 13px; }
.card .st { text-align: left; margin-top: 3px; line-height: 1.5; }
.card .adv { color: var(--warn); }
.chip { flex: 0 0 62px; height: 70px; border: 2px solid var(--line); border-radius: 10px; background: var(--panel2); display: flex; flex-direction: column; align-items: center; justify-content: center; font-size: 11px; touch-action: none; }
.chip canvas { width: 36px; height: 36px; pointer-events: none; }
.chip.sel { border-color: var(--warn); background: #3a3012; }

/* 塔資訊 */
.tinfo { display: flex; gap: 8px; align-items: center; margin-bottom: 6px; }
.tinfo canvas { width: 44px; height: 44px; flex: 0 0 44px; }
.tinfo .nm { font-weight: 700; font-size: 15px; }
.lvbar { height: 8px; border-radius: 4px; background: #0b0e14; border: 1px solid var(--line); overflow: hidden; margin-top: 3px; width: 120px; }
.lvbar > i { display: block; height: 100%; background: var(--accent); }
.stats { display: flex; gap: 12px; flex-wrap: wrap; font-size: 13px; margin-bottom: 6px; }
.stats b { color: #fff; }
.stats .adv { color: var(--warn); }
.ups { display: grid; grid-template-columns: repeat(auto-fit, minmax(112px, 1fr)); gap: 6px; }
.up { border: 1px solid var(--line); border-radius: 10px; padding: 6px; background: var(--panel2); font-size: 12px; }
.up h4 { margin: 0 0 3px; font-size: 13px; display: flex; justify-content: space-between; }
.up input[type=range] { width: 100%; margin: 2px 0; accent-color: var(--accent); }
.up .est { color: #9fd0ff; min-height: 2.7em; }
.up .btn { width: 100%; margin-top: 3px; height: 28px; }

/* 怪物資訊 */
.mrow { display: flex; gap: 8px; align-items: center; padding: 6px; border: 1px solid var(--line); border-radius: 10px; background: var(--panel2); margin-bottom: 6px; }
.mrow canvas { width: 40px; height: 40px; flex: 0 0 40px; }
.mrow .mi { flex: 1; min-width: 0; font-size: 12px; }
.mrow .nm { font-weight: 700; font-size: 13px; }
.tag { display: inline-block; padding: 0 5px; margin: 1px 2px 1px 0; border-radius: 6px; border: 1px solid var(--line); font-size: 11px; }
.tag.air { border-color: #4a9ac0; color: #8fd4ff; }
.tag.buff { border-color: #6a8a3a; color: #bde07a; }
.tag.debuff { border-color: #8a4a4a; color: #e0a0a0; }
.mrow .cnt { flex: 0 0 auto; text-align: center; font-size: 11px; color: var(--dim); }
.mrow .cnt b { display: block; font-size: 18px; color: #fff; }

/* ---------- 全螢幕 / 對話框 ---------- */
.screen { position: absolute; inset: 0; z-index: 50; display: flex; align-items: center; justify-content: center; background: rgba(8, 10, 15, .92); padding: 14px; overflow-y: auto; }
#home { background: radial-gradient(circle at 50% 20%, #1c2a44, #0b0e14 70%); z-index: 60; }
.home-box, #overlayBox { width: 100%; max-width: 380px; margin: auto; }
.home-box h1 { margin: 0 0 4px; text-align: center; font-size: 28px; letter-spacing: 2px; }
.home-box > p { text-align: center; margin: 0 0 10px; }
#overlayBox { background: var(--panel); border: 1px solid var(--line); border-radius: 14px; padding: 16px; }
#overlayBox h2 { margin: 0 0 10px; text-align: center; }
#overlayBox .btn { width: 100%; margin-top: 8px; height: 38px; }
#modeList { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; margin-bottom: 12px; }
.mode { padding: 12px 8px; border: 2px solid var(--line); border-radius: 12px; background: var(--panel); text-align: center; }
.mode b { display: block; font-size: 16px; }
.mode small { color: var(--dim); font-size: 11px; }
.mode.sel { border-color: var(--accent); background: #1d3350; }
.mode.locked { opacity: .45; }
.opt { display: flex; align-items: center; justify-content: space-between; margin: 8px 0; }
.seg { display: flex; border: 1px solid var(--line); border-radius: 8px; overflow: hidden; }
.seg button { border: 0; background: var(--panel); padding: 6px 12px; color: var(--dim); }
.seg button.on { background: #2563b0; color: #fff; }
table.t { width: 100%; border-collapse: collapse; font-size: 12.5px; margin: 6px 0; }
table.t th, table.t td { padding: 3px 4px; border-bottom: 1px solid var(--line); text-align: right; }
table.t th:first-child, table.t td:first-child { text-align: left; }
.big-num { text-align: center; font-size: 30px; font-weight: 800; color: #ffd36b; margin: 4px 0; }
