* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { margin: 0; padding: 0; width: 100%; height: 100%; overflow: hidden; background: #f8f4ef; font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "PingFang SC", "Hiragino Sans GB", sans-serif; }
#app { position: relative; width: 100%; height: 100%; display: flex; align-items: center; justify-content: center; }
#bg-canvas { position: absolute; top: 0; left: 0; width: 100%; height: 100%; z-index: 0; pointer-events: none; background: #fff3e0; }
#game { position: relative; z-index: 1; display: block; max-width: 100%; max-height: 100%; background: #fffbe8; box-shadow: 0 0 20px rgba(0,0,0,0.1); opacity: 0; transition: opacity 0.3s ease; }
.screen { z-index: 2; }

.screen { position: absolute; inset: 0; display: none; flex-direction: column; align-items: center; justify-content: center; padding: 16px; overflow-y: auto; }
.screen.active { display: flex; }
.screen.hud { display: none; pointer-events: none; background: transparent; justify-content: space-between; padding: 6px; z-index: 6; }
.screen.hud.active { display: flex; }
#screen-game { pointer-events: none; background: transparent; }
#game.fullscreen { position: absolute; top: 0; left: 0; width: 100vw; height: 100vh; max-width: none; max-height: none; opacity: 1 !important; z-index: 5; box-shadow: none; border-radius: 0; }
#bg-canvas.hidden { opacity: 0; pointer-events: none; }
/* 结算/弹窗必须盖在战斗全屏画布(z-index:5)之上，否则胜利/失败界面看不见 */
#screen-result { z-index: 20; }
#screen-result.fail { z-index: 20; }

.panel { background: rgba(255,255,255,0.98); border-radius: 18px; padding: 24px 20px; width: 92%; max-width: 340px; box-shadow: 0 12px 32px rgba(0,0,0,0.18); text-align: center; border: 2px solid rgba(255,138,101,0.25); }
.panel h2 { margin: 0 0 16px; color: #5a4a3a; }
input { width: 100%; padding: 12px; margin-bottom: 10px; border: 1px solid #ddd; border-radius: 10px; font-size: 15px; }
.btn { display: inline-flex; align-items: center; justify-content: center; min-width: 120px; min-height: 44px; padding: 10px 18px; margin: 6px 4px; border: none; border-radius: 14px; background: #ffe082; color: #5a4a3a; font-size: 15px; font-weight: 600; cursor: pointer; box-shadow: 0 4px 0 #d8b65c, 0 6px 12px rgba(0,0,0,0.12); transition: transform .08s, box-shadow .08s; pointer-events: auto; }
.btn:active { transform: translateY(3px); box-shadow: 0 1px 0 #d8b65c, 0 2px 4px rgba(0,0,0,0.1); }
.btn.primary { background: linear-gradient(135deg, #ffab91 0%, #ff8a65 100%); color: #fff; box-shadow: 0 4px 0 #d86c4e, 0 6px 12px rgba(255,112,67,0.25); }
.btn.primary:active { box-shadow: 0 1px 0 #d86c4e, 0 2px 4px rgba(255,112,67,0.15); }
.btn.big { font-size: 18px; min-width: 160px; min-height: 52px; border-radius: 16px; }
.btn.small { font-size: 13px; min-width: 80px; min-height: 34px; border-radius: 12px; box-shadow: 0 3px 0 #d8b65c; }

.gradient-1 { background: linear-gradient(135deg, #ffd54f 0%, #ffb300 100%); color: #5a4a3a; box-shadow: 0 4px 0 #d8a600, 0 6px 12px rgba(255,179,0,0.2); }
.gradient-2 { background: linear-gradient(135deg, #81d4fa 0%, #29b6f6 100%); color: #fff; box-shadow: 0 4px 0 #0288d1, 0 6px 12px rgba(41,182,246,0.2); }
.gradient-3 { background: linear-gradient(135deg, #a5d6a7 0%, #66bb6a 100%); color: #fff; box-shadow: 0 4px 0 #388e3c, 0 6px 12px rgba(102,187,106,0.2); }
.gradient-4 { background: linear-gradient(135deg, #ce93d8 0%, #ab47bc 100%); color: #fff; box-shadow: 0 4px 0 #8e24aa, 0 6px 12px rgba(171,71,188,0.2); }
.gradient-5 { background: linear-gradient(135deg, #ffcc80 0%, #ffa726 100%); color: #fff; box-shadow: 0 4px 0 #ef6c00, 0 6px 12px rgba(255,167,38,0.2); }
.gradient-1:active, .gradient-2:active, .gradient-3:active, .gradient-4:active, .gradient-5:active { box-shadow: 0 1px 0 rgba(0,0,0,0.2), 0 2px 4px rgba(0,0,0,0.1); }

.code-row { display: flex; gap: 8px; margin-bottom: 10px; align-items: center; }
.code-row input { flex: 1; margin-bottom: 0; min-width: 0; }
.code-btn { min-width: 100px; min-height: 44px; font-size: 13px; padding: 0 8px; background: linear-gradient(135deg, #b39ddb 0%, #7e57c2 100%); color: #fff; box-shadow: 0 4px 0 #5e35b1; border-radius: 12px; }
.code-btn:disabled { filter: grayscale(0.6); cursor: wait; }
#auth-code { letter-spacing: 4px; text-align: center; font-weight: 700; }
.auth-links { display: flex; justify-content: space-between; font-size: 13px; margin-top: 10px; }
.auth-links a { color: #5d4037; text-decoration: underline; }

.loading-box { text-align: center; color: #5a4a3a; }
.loading-box h1 { font-size: 32px; margin: 0 0 12px; color: #ff8a65; }
.progress { width: 220px; height: 12px; background: #e0e0e0; border-radius: 6px; overflow: hidden; margin: 12px auto; }
#load-bar { width: 0%; height: 100%; background: #ffab91; transition: width .2s; }

.menu-top { position: absolute; top: 8px; left: 8px; right: 8px; display: flex; justify-content: space-around; z-index: 3; }
.res { background: rgba(255,255,255,0.92); padding: 6px 10px; border-radius: 12px; font-size: 13px; color: #5a4a3a; font-weight: 700; box-shadow: 0 2px 6px rgba(0,0,0,0.08); }
.menu-content { position: relative; z-index: 3; background: rgba(255,255,255,0.85); border-radius: 24px; padding: 28px 22px; width: 92%; max-width: 340px; box-shadow: 0 12px 32px rgba(0,0,0,0.12); text-align: center; backdrop-filter: blur(4px); }
.title { font-size: 38px; color: #ff8a65; margin: 0 0 22px; text-shadow: 2px 2px 0 #fff, 0 4px 12px rgba(255,138,101,0.2); }
.menu-btns { display: flex; flex-direction: column; align-items: center; gap: 10px; }

.auth-panel .toggle { margin-top: 10px; font-size: 13px; }
.msg { color: #e53935; font-size: 13px; min-height: 18px; margin: 6px 0 0; }

.calendar { display: grid; grid-template-columns: repeat(7,1fr); gap: 6px; margin: 12px 0; }
.cal-day { padding: 8px 0; border-radius: 8px; background: #f0f0f0; font-size: 12px; }
.cal-day.signed { background: #a5d6a7; color: #1b5e20; }

.gacha-panel { max-width: 360px; }
.gacha-btns { display: flex; justify-content: center; gap: 8px; margin: 12px 0; }
.gacha-result { display: flex; flex-wrap: wrap; justify-content: center; gap: 8px; margin: 10px 0; }
.gacha-item { width: 56px; text-align: center; font-size: 12px; background: #fff3e0; border-radius: 8px; padding: 6px 2px; }
.inventory { display: flex; flex-wrap: wrap; justify-content: center; gap: 6px; margin: 8px 0; }
.inv-item { background: #e3f2fd; border-radius: 8px; padding: 5px 8px; font-size: 12px; }

.rank-panel { max-width: 360px; }
#rank-list { text-align: left; max-height: 360px; overflow-y: auto; padding-left: 0; list-style: none; }
#rank-list li { margin: 6px 0; padding: 6px 10px; background: #fff8f0; border-radius: 10px; display: flex; justify-content: space-between; }
#rank-list .rank-num { display: inline-block; width: 24px; height: 24px; line-height: 24px; text-align: center; border-radius: 50%; background: #ffe082; color: #5a4a3a; font-weight: 800; margin-right: 8px; font-size: 12px; }
#rank-list li:nth-child(1) .rank-num { background: #ffd54f; }
#rank-list li:nth-child(2) .rank-num { background: #e0e0e0; }
#rank-list li:nth-child(3) .rank-num { background: #ffcc80; }

.back-btn { position: absolute; top: 10px; left: 10px; z-index: 10; min-width: 64px; min-height: 34px; font-size: 13px; background: linear-gradient(135deg, #ef9a9a 0%, #e57373 100%); color: #fff; box-shadow: 0 3px 0 #c62828; border-radius: 12px; }
.level-overlay { position: absolute; inset: 0; background: rgba(0,0,0,0.35); z-index: 1; }
.level-panel { position: relative; z-index: 2; width: 92%; max-width: 360px; max-height: 88%; background: rgba(255,255,255,0.98); border-radius: 22px; padding: 22px 16px; box-shadow: 0 16px 40px rgba(0,0,0,0.25); display: flex; flex-direction: column; align-items: center; border: 3px solid rgba(255,138,101,0.25); overflow: hidden; }
.level-header { width: 100%; text-align: center; margin-bottom: 10px; }
.level-header h2 { margin: 38px 0 10px; font-size: 20px; color: #5a4a3a; }
.chapter-tabs { display: flex; justify-content: center; gap: 10px; }
.tab { padding: 8px 18px; border-radius: 18px; border: none; background: #f0f0f0; color: #5a4a3a; font-weight: 600; box-shadow: 0 3px 0 rgba(0,0,0,0.12); transition: transform .08s, box-shadow .08s; cursor: pointer; }
.tab.active { background: linear-gradient(135deg, #ffab91 0%, #ff8a65 100%); color: #fff; box-shadow: 0 3px 0 #d86c4e, 0 4px 10px rgba(255,112,67,0.25); transform: translateY(-2px); }
.tab:active { transform: translateY(1px); box-shadow: 0 1px 0 rgba(0,0,0,0.12); }
.level-grid { display: grid; grid-template-columns: repeat(4,1fr); gap: 10px; width: 100%; padding: 10px 4px; overflow-y: auto; }
.level-bottom { width: 100%; background: rgba(255,255,255,0.95); padding: 10px; border-radius: 14px; margin-top: 10px; }
.level-cell { aspect-ratio: 1; border-radius: 12px; background: #fff8f0; display: flex; flex-direction: column; align-items: center; justify-content: center; box-shadow: 0 2px 6px rgba(0,0,0,0.08); position: relative; border: 1px solid rgba(255,138,101,0.15); }
.level-cell.locked { opacity: .5; background: #f5f5f5; }
.level-cell .num { font-size: 16px; font-weight: 700; color: #5a4a3a; }
.level-cell .stars { font-size: 11px; color: #ffb300; }
.level-cell .boss { position: absolute; top: 2px; right: 4px; font-size: 10px; color: #e53935; }
.level-bottom p { margin: 0 0 6px; font-size: 13px; }
.rel-select { display: flex; justify-content: center; flex-wrap: wrap; gap: 6px; }
.rel-chip { padding: 6px 10px; border-radius: 14px; background: #f0f0f0; font-size: 12px; cursor: pointer; position: relative; }
.rel-chip.active { background: #a5d6a7; }
.rel-chip.locked { opacity: .5; }
.rel-help-btn { margin-left: 6px; width: 20px; height: 20px; border-radius: 50%; border: none; background: #ffcc80; color: #5a4a3a; font-weight: 800; font-size: 12px; cursor: pointer; }

.modal-overlay { position: fixed; inset: 0; background: rgba(0,0,0,0.45); z-index: 50; display: none; align-items: center; justify-content: center; }
.modal-overlay.show { display: flex; }
.modal-panel { background: #fff; border-radius: 20px; padding: 20px; width: 92%; max-width: 340px; max-height: 80vh; overflow-y: auto; box-shadow: 0 16px 40px rgba(0,0,0,0.25); text-align: center; }
.modal-panel h3 { margin: 0 0 12px; color: #5a4a3a; }
.modal-item { display: flex; align-items: center; gap: 10px; padding: 10px; margin: 6px 0; background: #fff8f0; border-radius: 12px; text-align: left; }
.modal-item .icon { width: 36px; height: 36px; border-radius: 50%; background: #ffcc80; display: flex; align-items: center; justify-content: center; font-size: 16px; flex-shrink: 0; }
.modal-item .desc { font-size: 12px; color: #6d5e4f; }
.modal-item .name { font-weight: 700; color: #5a4a3a; font-size: 14px; }

.unlock-modal .modal-panel { animation: popIn 0.35s ease; }
@keyframes popIn { from { transform: scale(0.7); opacity: 0; } to { transform: scale(1); opacity: 1; } }
.unlock-icon { width: 72px; height: 72px; margin: 0 auto 12px; border-radius: 50%; background: linear-gradient(135deg, #ffd54f 0%, #ff8a65 100%); display: flex; align-items: center; justify-content: center; font-size: 36px; box-shadow: 0 6px 16px rgba(255,138,101,0.35); }

.hud-row { position: absolute; left: 6px; right: 6px; display: flex; justify-content: space-between; align-items: center; font-size: 13px; font-weight: 700; color: #5a4a3a; pointer-events: none; }
.hud-row.top { top: 6px; }
.hud-row.bottom { bottom: 6px; }
.hud-row div { background: rgba(255,255,255,0.85); padding: 4px 8px; border-radius: 10px; }
.tower-bar { display: flex; gap: 6px; pointer-events: auto; }
.tower-icon { width: 44px; height: 44px; border-radius: 10px; background: #fff; border: 2px solid #ffcc80; display: flex; align-items: center; justify-content: center; font-size: 11px; cursor: pointer; }
.tower-icon.selected { border-color: #ff7043; background: #fff3e0; }
.tower-icon.locked { opacity: .55; border-color: #e0e0e0; background: #f5f5f5; cursor: not-allowed; }
.tower-icon.mom-skill { background: linear-gradient(135deg, #ffcc80 0%, #ffab91 100%); color: #5a4a3a; position: relative; overflow: hidden; }
.tower-icon.mom-skill.cooling { filter: grayscale(0.6); }
.tower-icon .cd-overlay { position: absolute; left: 0; right: 0; bottom: 0; background: rgba(0,0,0,0.45); pointer-events: none; }

.tower-popup { display: none; position: absolute; background: rgba(255,255,255,0.98); border-radius: 14px; padding: 8px; box-shadow: 0 8px 24px rgba(0,0,0,0.22); z-index: 15; min-width: 110px; border: 2px solid rgba(255,138,101,0.25); }
.tower-popup.show { display: block; }
.tower-popup .tower-name { font-size: 12px; font-weight: 700; color: #5a4a3a; text-align: center; margin-bottom: 6px; border-bottom: 1px solid #f0f0f0; padding-bottom: 4px; }
.tmbtns { display: grid; grid-template-columns: 1fr 1fr; gap: 6px; }
.tmbtns button { display: block; width: 100%; padding: 8px 4px; border: none; border-radius: 8px; background: #ffe082; font-size: 12px; color: #5a4a3a; font-weight: 600; }
.tmbtns button[data-act="sell"] { background: #ef9a9a; color: #fff; }
.tmbtns button:disabled { opacity: .5; }

.result-panel { max-width: 300px; }
.result-panel h2 { color: #43a047; }
.result-panel.fail h2 { color: #e53935; }
#result-stars { font-size: 28px; color: #ffb300; margin: 8px 0; }

#toast { position: absolute; bottom: 80px; left: 50%; transform: translateX(-50%); background: rgba(0,0,0,0.75); color: #fff; padding: 8px 14px; border-radius: 14px; font-size: 13px; opacity: 0; transition: opacity .3s; pointer-events: none; z-index: 30; }
.tower-hint { position: absolute; left: 6px; right: 6px; bottom: 60px; text-align: center; font-size: 12px; color: #5a4a3a; background: rgba(255,255,255,0.9); padding: 5px 8px; border-radius: 10px; pointer-events: none; transition: opacity .2s; z-index: 7; }
#toast.show { opacity: 1; }

@media (max-width: 380px) {
  .title { font-size: 28px; }
  .level-grid { grid-template-columns: repeat(3,1fr); }
  .level-panel { max-width: 340px; }
}
