:root {
  color-scheme: dark;
  font-family: "Microsoft YaHei UI", "Courier New", monospace;
  --bg: #0d140f;
  --panel: #152018;
  --panel-deep: #101811;
  --line: #42563b;
  --line-soft: rgba(164, 189, 125, .22);
  --ink: #f4f1d2;
  --muted: #aebd91;
  --gold: #f1cf68;
  --green: #9ed579;
  --red: #e16b63;
  --cyan: #78c8d7;
  --purple: #b18de8;
  --safe-top: max(10px, env(safe-area-inset-top));
  --safe-right: max(10px, env(safe-area-inset-right));
  --safe-bottom: max(10px, env(safe-area-inset-bottom));
  --safe-left: max(10px, env(safe-area-inset-left));
}

* { box-sizing: border-box; }
html, body { width: 100%; min-height: 100%; margin: 0; background: var(--bg); }
body { overflow: hidden; color: var(--ink); user-select: none; -webkit-tap-highlight-color: transparent; }
button, select, output { font: inherit; color: inherit; }
button, select { cursor: pointer; }
button:focus-visible, select:focus-visible, a:focus-visible { outline: 2px solid var(--gold); outline-offset: 3px; }
[hidden] { display: none !important; }

.td-shell { position: relative; width: 100%; height: 100dvh; min-height: 540px; overflow: hidden; background: radial-gradient(circle at 50% -20%, #293a25, var(--bg) 62%); }
.td-topbar { position: absolute; z-index: 40; top: var(--safe-top); right: var(--safe-right); left: var(--safe-left); display: flex; align-items: center; gap: 12px; min-height: 42px; pointer-events: none; }
.td-topbar > * { pointer-events: auto; }
.td-brand { display: flex; align-items: center; gap: 8px; color: var(--ink); font-size: 13px; font-weight: 900; letter-spacing: .08em; text-transform: uppercase; }
.is-battle .td-brand, .is-battle .td-topbar .top-actions { display: none; }
.brand-mark { display: grid; width: 34px; height: 34px; place-items: center; border: 2px solid var(--gold); background: #243721; color: var(--gold); font: 900 13px "Courier New", monospace; box-shadow: 3px 3px 0 #070b08; }
.top-actions { display: flex; align-items: center; gap: 6px; margin-left: auto; }
.top-button, .mini-button { min-height: 32px; padding: 0 8px; border: 1px solid var(--line-soft); border-radius: 3px; background: rgba(14, 22, 15, .82); color: var(--muted); font-size: 10px; font-weight: 800; }
.top-button:hover, .mini-button:hover { border-color: var(--gold); color: var(--gold); }
.top-button.subtle { color: #87a5a2; }
.arcade-home-button { position: static !important; width: 36px !important; height: 36px !important; min-width: 36px; }

.td-screen { position: absolute; inset: 0; overflow: auto; padding: calc(var(--safe-top) + 72px) max(20px, var(--safe-right)) calc(var(--safe-bottom) + 24px) max(20px, var(--safe-left)); }
.title-screen { display: flex; flex-direction: column; gap: 24px; max-width: 1180px; margin: auto; }
.title-copy { max-width: 850px; }
.eyebrow { margin: 0; color: var(--gold); font: 900 10px/1.3 "Courier New", monospace; letter-spacing: .15em; text-transform: uppercase; }
.title-copy h1 { margin: 9px 0 10px; font-size: clamp(30px, 5vw, 62px); line-height: 1.04; letter-spacing: -.04em; }
.title-copy h1 strong { color: var(--green); }
.title-copy > p:not(.eyebrow) { max-width: 700px; margin: 0; color: var(--muted); font-size: 13px; line-height: 1.75; }
.title-hints { display: flex; flex-wrap: wrap; gap: 7px; margin-top: 16px; }
.title-hints span { padding: 5px 8px; border: 1px solid var(--line-soft); background: rgba(24, 37, 23, .58); color: var(--muted); font-size: 10px; }
.setup-grid { display: grid; grid-template-columns: 1.1fr .9fr; gap: 14px; }
.setup-card { min-width: 0; padding: 15px; border: 1px solid var(--line); background: linear-gradient(150deg, rgba(36, 55, 32, .84), rgba(15, 24, 16, .94)); box-shadow: 5px 5px 0 rgba(0, 0, 0, .3); }
.section-heading { display: flex; align-items: end; gap: 9px; margin-bottom: 12px; }
.section-heading h2 { margin: 0; font-size: 18px; }
.star-total { margin-left: auto; color: var(--gold); font-size: 12px; font-weight: 900; }
.choice-list { display: grid; gap: 8px; }
.level-list { grid-template-columns: repeat(3, minmax(0, 1fr)); }
.choice-card { position: relative; min-width: 0; padding: 10px; border: 1px solid var(--line-soft); background: rgba(10, 18, 11, .7); color: var(--ink); text-align: left; transition: transform .15s ease, border-color .15s ease, background .15s ease; }
.choice-card:hover:not(:disabled) { border-color: var(--gold); transform: translateY(-2px); }
.choice-card.is-selected { border-color: var(--gold); background: #2c3d25; box-shadow: inset 0 0 0 1px rgba(241, 207, 104, .28); }
.choice-card:disabled { cursor: not-allowed; opacity: .42; filter: grayscale(.7); }
.choice-card .choice-index { display: block; color: var(--muted); font: 700 9px "Courier New", monospace; }
.choice-card strong { display: block; margin-top: 4px; overflow: hidden; font-size: 14px; text-overflow: ellipsis; white-space: nowrap; }
.choice-card small { display: block; margin-top: 4px; color: var(--muted); font-size: 10px; line-height: 1.45; }
.choice-card .choice-stars { display: block; margin-top: 8px; color: var(--gold); font-size: 12px; letter-spacing: 2px; }
.hero-list { grid-template-columns: repeat(3, minmax(0, 1fr)); }
.hero-choice-icon { display: grid; width: 34px; height: 34px; place-items: center; border: 1px solid currentColor; border-radius: 50%; font-size: 18px; }
.hero-choice-icon.warden { color: var(--gold); }
.hero-choice-icon.ranger { color: #e28d68; }
.hero-choice-icon.sage { color: var(--purple); }
.title-actions { display: flex; align-items: center; gap: 12px; }
.difficulty-label { display: flex; align-items: center; gap: 8px; color: var(--muted); font-size: 11px; }
select { min-height: 42px; padding: 0 10px; border: 1px solid var(--line); border-radius: 3px; background: var(--panel-deep); color: var(--ink); }
.primary-button, .secondary-button { min-height: 42px; padding: 0 15px; border: 2px solid var(--gold); border-radius: 3px; background: var(--gold); color: #182013; font-weight: 900; box-shadow: 3px 3px 0 #070b08; transition: transform .12s ease, filter .12s ease; }
.primary-button:hover { filter: brightness(1.1); transform: translateY(-1px); }
.primary-button:active, .secondary-button:active { transform: translate(2px, 2px); box-shadow: 1px 1px 0 #070b08; }
.secondary-button { border-color: var(--line); background: #1a2a1b; color: var(--ink); box-shadow: 3px 3px 0 #070b08; }
.secondary-button:hover { border-color: var(--gold); color: var(--gold); }
.title-actions .primary-button { min-width: 190px; margin-left: auto; }
.title-status { min-height: 16px; margin: 0; color: var(--muted); font-size: 10px; }

.battle-screen { position: absolute; inset: 0; overflow: hidden; background: #263a26; }
#game-canvas { position: absolute; inset: 0 300px 0 0; display: block; width: calc(100% - 300px); height: 100%; image-rendering: pixelated; touch-action: none; }
.battle-vignette { position: absolute; inset: 0; pointer-events: none; background: radial-gradient(ellipse at 43% 48%, transparent 48%, rgba(5, 10, 6, .36) 100%); }
.battle-hud { position: absolute; z-index: 5; top: calc(var(--safe-top) + 4px); left: calc(var(--safe-left) + 52px); width: min(530px, calc(100% - 390px)); min-width: 300px; pointer-events: none; text-shadow: 1px 2px #071008; }
.hud-row { display: flex; align-items: center; gap: 8px; }
.hud-primary { justify-content: flex-start; }
.hud-pill { padding: 5px 8px; border: 1px solid rgba(244, 241, 210, .25); background: rgba(9, 16, 10, .72); font: 900 13px "Courier New", monospace; }
.gold-pill { color: var(--gold); }
.life-pill { color: #f18b7b; }
.hud-wave { margin-left: auto; color: var(--ink); font: 900 12px "Courier New", monospace; }
.hud-secondary { justify-content: space-between; margin-top: 6px; color: var(--muted); font-size: 10px; }
.wave-progress { height: 5px; margin-top: 6px; overflow: hidden; border: 1px solid rgba(244, 241, 210, .25); background: rgba(0, 0, 0, .48); }
.wave-progress i { display: block; width: 0; height: 100%; background: linear-gradient(90deg, var(--green), var(--gold)); transition: width .15s linear; }
.build-dock { position: absolute; z-index: 8; top: calc(var(--safe-top) + 3px); right: var(--safe-right); bottom: var(--safe-bottom); display: flex; width: 290px; flex-direction: column; gap: 10px; padding: 13px; overflow: auto; border: 1px solid var(--line); background: rgba(16, 26, 17, .95); box-shadow: -6px 0 22px rgba(0, 0, 0, .28); }
.dock-heading, .hero-dock-head, .hero-dock-actions, .dock-actions { display: flex; align-items: center; gap: 8px; }
.dock-heading strong { font-size: 16px; }
.dock-heading .mini-button { margin-left: auto; width: 30px; padding: 0; }
.tower-palette { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 7px; }
.tower-button { display: grid; min-height: 82px; grid-template-columns: 32px 1fr; grid-template-rows: auto auto; align-items: center; gap: 2px 6px; padding: 7px; border: 1px solid var(--line-soft); background: #19291a; color: var(--ink); text-align: left; }
.tower-button:hover:not(:disabled), .tower-button.is-selected { border-color: var(--tower-color, var(--gold)); background: #294025; }
.tower-button:disabled { cursor: not-allowed; opacity: .42; }
.tower-symbol { display: grid; width: 28px; height: 28px; grid-row: 1 / span 2; place-items: center; border: 2px solid var(--tower-color, var(--gold)); border-radius: 50%; color: var(--tower-color, var(--gold)); font-size: 15px; }
.tower-button strong { overflow: hidden; font-size: 11px; text-overflow: ellipsis; white-space: nowrap; }
.tower-button small { overflow: hidden; color: var(--muted); font: 700 9px "Courier New", monospace; text-overflow: ellipsis; white-space: nowrap; }
.tower-button .tower-cost { grid-column: 2; color: var(--gold); font: 900 10px "Courier New", monospace; }
.build-hint { min-height: 28px; margin: 0; color: var(--muted); font-size: 10px; line-height: 1.4; }
.hero-dock { margin-top: auto; padding: 9px; border: 1px solid rgba(213, 179, 92, .35); background: rgba(45, 37, 19, .45); }
.hero-dock-head strong { margin-left: auto; color: var(--gold); font-size: 11px; }
.hero-meter { height: 6px; margin-top: 7px; overflow: hidden; background: #0b120c; }
.hero-meter i { display: block; width: 100%; height: 100%; background: var(--gold); transition: width .2s ease; }
.hero-dock-actions { justify-content: space-between; margin-top: 7px; color: var(--muted); font: 700 10px "Courier New", monospace; }
.ability-button { min-height: 30px; padding: 0 7px; border: 1px solid var(--gold); background: #3d341c; color: var(--gold); font-size: 10px; font-weight: 900; }
.ability-button:disabled { border-color: var(--line); background: #1a2119; color: var(--muted); cursor: wait; }
.dock-actions { margin-top: 2px; }
.dock-actions .secondary-button { flex: 1; min-height: 36px; padding: 0 8px; font-size: 10px; }
.dock-actions .mini-button { width: 36px; height: 36px; padding: 0; }

.tower-inspector { position: absolute; z-index: 10; right: 312px; bottom: calc(var(--safe-bottom) + 14px); width: min(280px, calc(100% - 330px)); padding: 13px; border: 1px solid var(--gold); background: rgba(17, 27, 17, .96); box-shadow: 4px 4px 0 rgba(0, 0, 0, .32); }
.inspector-close { position: absolute; top: 5px; right: 6px; width: 26px; height: 26px; border: 0; background: transparent; color: var(--muted); font-size: 20px; }
.inspector-heading { display: grid; grid-template-columns: 38px 1fr auto; align-items: center; gap: 8px; }
.inspector-heading > span:first-child { display: grid; width: 34px; height: 34px; place-items: center; border: 2px solid var(--gold); border-radius: 50%; color: var(--gold); }
.inspector-heading h2 { margin: 2px 0 0; font-size: 16px; }
.inspector-heading > strong { color: var(--gold); font: 900 11px "Courier New", monospace; }
.inspector-detail { margin: 9px 0; color: var(--muted); font-size: 10px; line-height: 1.45; }
.inspector-stats { display: grid; gap: 3px; }
.inspector-stats div { display: flex; justify-content: space-between; gap: 8px; padding: 4px 6px; background: rgba(255, 255, 255, .035); font-size: 10px; }
.inspector-stats span { color: var(--muted); }
.inspector-stats b { color: var(--ink); font-family: "Courier New", monospace; }
.inspector-actions { display: grid; grid-template-columns: 1fr 1fr; gap: 7px; margin-top: 10px; }
.inspector-actions button { min-height: 34px; padding: 0 6px; font-size: 10px; }

.modal { position: absolute; z-index: 30; inset: 0; display: grid; place-items: center; padding: 22px; background: rgba(5, 10, 6, .72); backdrop-filter: blur(3px); }
.modal-card { width: min(520px, 100%); max-height: min(90vh, 650px); overflow: auto; padding: 22px; border: 2px solid var(--gold); background: linear-gradient(145deg, #233321, #101811); box-shadow: 7px 7px 0 rgba(0, 0, 0, .48); animation: modal-in .22s ease-out; }
.modal-card.compact { width: min(390px, 100%); }
.modal-card h2 { margin: 7px 0 9px; font-size: 25px; }
.modal-card p { color: var(--muted); font-size: 12px; line-height: 1.6; }
.branch-options { display: grid; grid-template-columns: 1fr 1fr; gap: 9px; margin: 15px 0 12px; }
.branch-option { min-height: 125px; padding: 11px; border: 1px solid var(--line); background: #172719; color: var(--ink); text-align: left; }
.branch-option:hover { border-color: var(--gold); background: #2a3b24; }
.branch-option strong { display: block; font-size: 14px; }
.branch-option small { display: block; margin-top: 7px; color: var(--muted); font-size: 10px; line-height: 1.45; }
.branch-option em { display: block; margin-top: 10px; color: var(--gold); font: 900 10px "Courier New", monospace; font-style: normal; }
.modal-actions { display: flex; gap: 9px; margin-top: 17px; }
.modal-actions button { flex: 1; }
.result-card { text-align: center; }
.result-stars { margin: 16px 0 10px; color: var(--gold); font-size: 33px; letter-spacing: 5px; text-shadow: 0 0 16px rgba(241, 207, 104, .55); }
.result-stats { display: grid; grid-template-columns: repeat(3, 1fr); gap: 7px; margin: 16px 0; }
.result-stats div { padding: 8px 4px; background: rgba(0, 0, 0, .2); }
.result-stats dt { color: var(--muted); font-size: 9px; }
.result-stats dd { margin: 3px 0 0; color: var(--ink); font: 900 19px "Courier New", monospace; }
.help-card ul { margin: 12px 0 17px; padding-left: 19px; color: var(--muted); font-size: 11px; line-height: 1.75; }
.battle-announcer { position: absolute; z-index: 15; top: 21%; left: 50%; min-width: 220px; padding: 8px 14px; border: 1px solid var(--gold); background: rgba(15, 25, 15, .85); color: var(--gold); font: 900 14px "Courier New", monospace; text-align: center; opacity: 0; pointer-events: none; transform: translate(-50%, -12px); transition: opacity .2s ease, transform .2s ease; }
.battle-announcer.is-visible { opacity: 1; transform: translate(-50%, 0); }

@keyframes modal-in { from { opacity: 0; transform: translateY(12px) scale(.96); } to { opacity: 1; transform: none; } }
@media (max-width: 820px) {
  body { overflow: auto; }
  .td-shell { min-height: 100dvh; overflow: auto; }
  .title-screen { padding-top: calc(var(--safe-top) + 65px); }
  .setup-grid { grid-template-columns: 1fr; }
  .level-list { grid-template-columns: repeat(3, minmax(0, 1fr)); }
  .battle-screen { min-height: 100dvh; }
  #game-canvas { inset: 0 0 190px; width: 100%; height: calc(100% - 190px); }
  .battle-hud { left: 56px; width: calc(100% - 70px); min-width: 0; }
  .build-dock { top: auto; right: 0; bottom: 0; left: 0; width: 100%; height: 190px; max-height: none; padding: 7px max(9px, var(--safe-left)); border-width: 1px 0 0; }
  .tower-palette { grid-template-columns: repeat(4, minmax(0, 1fr)); gap: 5px; }
  .tower-button { min-height: 58px; grid-template-columns: 24px 1fr; padding: 5px; }
  .tower-symbol { width: 22px; height: 22px; font-size: 12px; }
  .tower-button strong { font-size: 9px; }
  .tower-button small { display: none; }
  .tower-button .tower-cost { font-size: 9px; }
  .build-hint { display: none; }
  .hero-dock { position: static; width: 100%; margin: 0; padding: 5px 7px; }
  .hero-dock-head { justify-content: space-between; }
  .hero-meter { margin-top: 4px; }
  .hero-dock-actions { margin-top: 4px; }
  .dock-heading { padding-right: 0; }
  .dock-actions { display: none; }
  .tower-inspector { right: 9px; bottom: 205px; width: min(300px, calc(100% - 18px)); }
}
@media (max-width: 520px) {
  .top-actions .top-button.subtle { display: none; }
  .title-copy h1 { font-size: 34px; }
  .title-actions { align-items: stretch; flex-direction: column; }
  .title-actions .primary-button { margin-left: 0; }
  .level-list, .hero-list { grid-template-columns: 1fr; }
  .choice-card { min-height: 74px; }
  .hero-list .choice-card { display: grid; grid-template-columns: 38px 1fr; gap: 0 8px; align-items: center; }
  .hero-list .choice-card .choice-index, .hero-list .choice-card small { grid-column: 2; }
  .hero-list .hero-choice-icon { grid-row: 1 / span 3; }
  .battle-hud { top: calc(var(--safe-top) + 3px); left: 49px; }
  .hud-pill { font-size: 11px; padding: 4px 6px; }
  .hud-wave { font-size: 10px; }
  .hud-secondary { font-size: 9px; }
  .hero-dock { width: 100%; }
  .branch-options { grid-template-columns: 1fr; }
  .modal { padding: 12px; }
  .modal-card { padding: 17px; }
}
@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { scroll-behavior: auto !important; animation-duration: .001ms !important; transition-duration: .001ms !important; }
}
