:root {
  color-scheme: dark;
  --page: #18291f;
  --dark: #213328;
  --dock: #2b4133;
  --lift: #365044;
  --line: #537060;
  --ink: #fff8e7;
  --muted: #c3d3bc;
  --mint: #a9ead2;
  --gold: #f4c667;
  --copper: #f5a486;
  --serif: 'Songti SC', 'Noto Serif CJK SC', SimSun, Georgia, serif;
  --sans: 'PingFang SC', 'Noto Sans CJK SC', 'Microsoft YaHei', sans-serif;
  --mono: 'SFMono-Regular', Consolas, 'Liberation Mono', monospace;
}
* { box-sizing: border-box; }
[hidden] { display: none !important; }
body { margin: 0; background: var(--page); color: var(--ink); font: 15px/1.5 var(--sans); }
body.wide-active { overflow: hidden; }
button, input { font: inherit; }
button { cursor: pointer; }
button:disabled { cursor: not-allowed; opacity: .55; }
:focus-visible { outline: 2px solid var(--mint); outline-offset: 2px; }
p, h1, h2 { margin: 0; text-wrap: pretty; }
.symbols { position: absolute; width: 0; height: 0; overflow: hidden; }
.icon { width: 20px; height: 20px; flex: none; fill: none; stroke: currentColor; stroke-width: 1.7; stroke-linecap: round; stroke-linejoin: round; vertical-align: middle; }
.coin { color: var(--gold); }
kbd { font: 11px var(--mono); padding: 3px 4px; border: 1px solid currentColor; border-radius: 3px; }

.field-guide { width: min(1280px, 100%, max(640px, calc(220px + (100dvh - 235px) * 1.7647))); margin-inline: auto; padding: 16px; }
.masthead, .identity, .tools { display: flex; align-items: center; }
.masthead { justify-content: space-between; gap: 8px; margin-bottom: 8px; }
.identity { gap: 8px; min-width: 0; }
.identity .icon { width: 24px; height: 24px; color: var(--gold); }
h1 { font: 800 clamp(22px, 3.5vw, 32px)/1.1 var(--serif); white-space: nowrap; }
.identity span, .kicker { color: var(--mint); font: 700 11px/1.4 var(--mono); letter-spacing: .13em; }
.tools { gap: 4px; flex: none; }
.tool, .quiet { min-width: 44px; min-height: 44px; border: 1px solid var(--line); border-radius: 4px; background: var(--dark); color: var(--muted); }
.tool:hover:not(:disabled), .quiet:hover:not(:disabled) { background: var(--lift); color: var(--ink); }
.tool[aria-pressed="false"] .icon { opacity: .5; }
.tool[aria-pressed="false"] { position: relative; }
.tool[aria-pressed="false"]::after { content: ''; width: 20px; height: 1px; background: var(--muted); transform: rotate(-45deg); position: absolute; top: 50%; left: calc(50% - 10px); }

.playfield { position: relative; display: flex; flex-direction: column; min-width: 0; background: var(--dark); border: 1px solid var(--line); border-radius: 6px; }
.battle-area { --hud-min: clamp(164px, 18vw, 232px); display: flex; min-width: 0; min-height: 0; }
.hud { flex: 1; display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); align-content: start; min-width: 0; min-height: 0; overflow: auto; border-right: 1px solid var(--line); }
.hud :is(.stock, .speed-control) { grid-column: 1 / -1; }
:is(.duration, .phase) strong { font-variant-numeric: tabular-nums; white-space: nowrap; }
.metric { display: flex; flex-direction: column; justify-content: center; min-width: 0; gap: 4px; padding: 8px 10px; min-height: 62px; border-bottom: 1px solid var(--line); }
.metric:is(:nth-child(2), .record, .phase) { border-left: 1px solid var(--line); }
.speed-control { border: 0; border-bottom: 1px solid var(--line); background: var(--page); color: var(--mint); text-align: left; }
.speed-control:hover:not(:disabled), .speed-control[data-fast="true"] { background: var(--lift); }
.speed-control:focus-visible { outline-offset: -3px; }
.metric > span { display: flex; align-items: center; gap: 5px; color: var(--muted); font-size: 12px; font-weight: 700; }
.metric .icon { width: 15px; height: 15px; }
.metric strong { font: 700 clamp(16px, 2vw, 23px)/1 var(--mono); letter-spacing: -.05em; overflow-wrap: anywhere; }
.metric:is(.score, .record) strong { font-size: clamp(15px, 1.4vw, 19px); }
.stock-label { margin-left: 4px; font-weight: 400; }
.stock-icons { display: flex; align-items: center; gap: clamp(3px, 1vw, 9px); min-height: 21px; padding-inline: 2px; }
.stock-icons i { position: relative; width: 11px; height: 15px; border: 1px solid var(--gold); background: var(--gold); border-radius: 75% 25% 65% 35%; transform: rotate(24deg); }
.stock-icons i.transit { background: transparent; border-style: dashed; }
.stock-icons i.lost { background: transparent; border-color: var(--muted); }
.stock-icons i.lost::after { content: ''; position: absolute; width: 1px; height: 100%; left: 50%; background: var(--muted); }

.board-wrap { flex: 0 0 var(--board-width, calc(100% - var(--hud-min))); display: grid; place-items: center; min-width: 0; min-height: 0; background: var(--page); }
#board { display: block; width: 100%; height: auto; aspect-ratio: 30 / 17; color: var(--ink); background: var(--dark); cursor: crosshair; touch-action: manipulation; }
#board:focus-visible { outline-offset: -3px; }
.canvas-fallback { display: inline-block; padding: 8px; }

.build-dock { flex: none; display: flex; align-items: center; align-content: start; gap: 6px; flex-wrap: wrap; height: clamp(158px, 21dvh, 195px); padding: 8px; border-top: 1px solid var(--line); background: var(--dock); overflow: auto; scrollbar-gutter: stable; overflow-anchor: none; }
.build-button, .tower-detail { display: flex; align-items: center; gap: 8px; min-height: 44px; padding: 4px 9px; color: var(--ink); text-align: left; font-size: 13px; }
.build-button { border: 1px solid var(--line); border-radius: 5px; background: var(--dark); }
.build-button:hover:not(:disabled) { border-color: var(--mint); background: var(--lift); }
.build-button[aria-pressed="true"] { border-color: var(--mint); box-shadow: inset 0 -3px var(--mint); }
.cost { display: inline-flex; align-items: center; gap: 4px; white-space: nowrap; color: var(--gold); font: 700 13px var(--mono); }
.cost::before { content: ''; width: 8px; height: 8px; border: 1px solid currentColor; border-radius: 2px; transform: rotate(45deg); }
.refund .cost { color: var(--mint); margin-left: 5px; }
.tower-detail { flex: 1 1 215px; min-width: 0; padding: 0; }
.tower-detail strong { display: block; font-size: 15px; overflow-wrap: anywhere; }
.tower-tier { color: var(--tower-accent, var(--mint)); font: 700 11px/1.35 var(--mono); }
.tower-icon { display: block; width: 40px; height: 40px; flex: none; border: 1px solid var(--line); border-radius: 5px; background: radial-gradient(circle, var(--lift), var(--page) 85%); }
.tower-detail .tower-icon { width: 48px; height: 48px; }
.tower-detail small, .build-button small, .card-copy small { display: block; color: var(--muted); font-size: 11px; line-height: 1.3; }
.tower-tools { display: flex; flex-wrap: wrap; gap: 4px; }
.branch-key { flex: 1 0 100%; min-width: 0; padding-top: 8px; border-top: 1px solid var(--line); }
.branch-key ul, .upgrade-options { display: flex; flex-wrap: wrap; gap: 6px; }
.branch-key ul { margin: 5px 0 0; padding: 0; list-style: none; }
.branch-key li, .upgrade-card { flex: 0 0 224px; max-width: 100%; min-width: 0; }
.branch-key li { display: flex; align-items: center; gap: 6px; min-height: 34px; color: var(--muted); font-size: 12px; }
.branch-key .tower-icon { width: 30px; height: 30px; }
.upgrade-bay { flex: 1 0 100%; min-width: 0; padding-top: 4px; }
.upgrade-bay header { display: flex; flex-wrap: wrap; gap: 4px 12px; margin: 0 1px 5px; }
.upgrade-bay h2 { font-size: 12px; color: var(--ink); }
#upgrade-path { color: var(--muted); font-size: 11px; }
.upgrade-card { display: grid; grid-template-columns: auto minmax(0, 1fr) auto; align-items: center; gap: 7px; min-height: 55px; padding: 5px 8px; border: 1px solid var(--line); border-radius: 5px; background: var(--dark); color: var(--ink); text-align: left; }
.upgrade-card:not(:disabled):hover { border-color: var(--tower-accent); background: var(--lift); }
.upgrade-card .tower-icon { background: radial-gradient(circle, color-mix(in srgb, var(--tower-accent) 25%, var(--dark)), var(--page) 85%); }
.card-copy strong { display: block; color: var(--tower-accent, var(--ink)); font-size: 13px; line-height: 1.25; overflow-wrap: anywhere; }
.card-meta { display: grid; justify-items: end; gap: 3px; }
.card-meta kbd { padding-block: 1px; color: var(--muted); }
#inspect-upgrades, #downgrade, #cancel, #unmark { padding-inline: 8px; font-size: 12px; }
.combat-stats { display: flex; flex-wrap: wrap; gap: 4px 14px; margin: 0; padding: 6px 8px; }
#tower-stats { flex: 1 0 100%; border-top: 1px solid var(--line); }
.combat-stats div { display: flex; align-items: baseline; flex-wrap: wrap; gap: 0 5px; }
.combat-stats dt { color: var(--muted); font-size: 11px; }
.combat-stats dd { margin: 0; color: var(--ink); font: 700 12px/1.45 var(--mono); overflow-wrap: anywhere; }
.ant-detail { display: flex; flex: 1 0 100%; align-items: center; flex-wrap: wrap; gap: 4px; padding: 4px 4px 0; border-top: 1px solid var(--line); }
.ant-title { display: grid; gap: 2px; padding-inline: 4px; font-size: 13px; }
.ant-title span { color: var(--mint); font-size: 11px; }
.arsenal-list { display: grid; gap: 8px; margin-top: 12px; }
.arsenal-list article { padding: 8px; border: 1px solid var(--line); border-radius: 4px; }
.arsenal-list h3 { margin: 0; font-size: 15px; }
.arsenal-list p { color: var(--muted); font-size: 12px; }
.arsenal-list .combat-stats { padding: 6px 0 0; }
.notice { flex: none; height: 28px; padding: 3px 8px; overflow: auto; color: var(--muted); font-size: 12px; }
.notice[data-alert="true"] { color: var(--gold); }

.overlay { position: absolute; z-index: 1; inset: 0; display: grid; justify-items: center; align-items: safe center; padding: 12px; overflow: auto; background: #142319bb; }
.overlay-panel { width: min(340px, 100%); border: 1px solid var(--line); border-radius: 5px; background: var(--dark); box-shadow: 0 8px 30px #14231999; padding: clamp(12px, 2.4vw, 24px); }
.overlay-panel h2 { font: 900 clamp(26px, 4vw, 42px)/1.2 var(--serif); margin-block: 4px 8px; }
.overlay-panel #end-detail { color: var(--muted); font-size: 14px; margin-bottom: 12px; }
.overlay[data-state="lost"] { background: linear-gradient(150deg, #301f15b8, #142319e6 66%); }
.overlay[data-state="lost"] .overlay-panel { width: min(470px, 100%); border-color: var(--copper); box-shadow: 0 12px 48px #101a13bd; }
.overlay[data-state="lost"] .kicker { color: var(--copper); }
.overlay[data-record="true"] .overlay-panel { border-color: var(--gold); }
.overlay[data-record="true"] .kicker, .overlay[data-record="true"] .result-best { color: var(--gold); }
.result-stats { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 1px; margin: 0; border: 1px solid var(--line); border-radius: 5px; background: var(--line); overflow: hidden; }
.result-stats div { display: grid; align-content: center; gap: 4px; min-width: 0; padding: 8px; background: var(--page); }
.result-stats dt { color: var(--muted); font-size: 11px; }
.result-stats dd { margin: 0; font: 700 clamp(14px, 3.5vw, 21px)/1.1 var(--mono); overflow-wrap: anywhere; }
.result-stats .result-wave { grid-column: 1 / -1; grid-template-columns: auto 1fr; align-items: center; justify-content: start; gap: 12px; background: var(--dock); }
.result-wave dt { font-size: 13px; }
.result-wave dd { color: var(--gold); font-size: clamp(28px, 6vw, 48px); }
.result-best { display: flex; align-items: center; gap: 6px; min-height: 30px; margin-block: 6px 9px; color: var(--muted); font-size: 13px; }
.result-best .icon { width: 16px; height: 16px; }
.overlay[data-state="lost"] .primary { background: var(--gold); border-color: var(--gold); }
.overlay[data-state="lost"] .primary:hover { background: var(--ink); }
@media (prefers-reduced-motion: no-preference) {
  .overlay[data-state="lost"] .overlay-panel { animation: report-enter .27s cubic-bezier(.2,.8,.3,1) both; }
  .overlay[data-state="lost"] .result-wave dd { animation: report-enter .28s .08s cubic-bezier(.2,.8,.3,1) both; }
}
@keyframes report-enter { from { opacity: 0; transform: translateY(9px); } }
.primary { width: 100%; min-height: 44px; padding: 6px 10px; border: 1px solid var(--mint); border-radius: 4px; background: var(--mint); color: var(--dark); font-weight: 800; }
.primary:hover { background: var(--ink); }
#restart { margin-top: 6px; width: 100%; }
#record-form { margin-top: 10px; color: var(--gold); font-size: 13px; }
.name-entry { display: flex; gap: 6px; margin-top: 3px; }
.name-entry input { min-width: 0; width: 100%; padding: 4px 8px; border: 1px solid var(--line); border-radius: 4px; color: var(--ink); background: var(--page); }
.name-entry button { padding-inline: 8px; }

dialog { width: min(540px, calc(100vw - 24px)); max-height: calc(100dvh - 24px); padding: 16px; border: 1px solid var(--line); border-radius: 6px; background: var(--dark); color: var(--ink); }
dialog::backdrop { background: #142319ba; }
.history-head { display: flex; justify-content: space-between; align-items: center; gap: 8px; }
.history-head h2 { font-family: var(--serif); }
#history-best, #history-empty { margin-block: 10px; color: var(--muted); }
.history-scroll { overflow-x: auto; }
table { width: 100%; border-collapse: collapse; text-align: left; font-size: 13px; }
th, td { padding: 7px 6px; border-bottom: 1px solid var(--line); overflow-wrap: anywhere; }
th { color: var(--mint); font-size: 11px; }

.field-guide:is(:fullscreen, .wide) { width: 100%; max-width: none; height: 100dvh; padding: max(4px, env(safe-area-inset-top)) max(8px, env(safe-area-inset-right)) max(4px, env(safe-area-inset-bottom)) max(8px, env(safe-area-inset-left)); display: flex; flex-direction: column; background: var(--page); }
.field-guide.wide { position: fixed; inset: 0; z-index: 5; }
.field-guide:is(:fullscreen, .wide) .masthead { margin-bottom: 4px; }
.field-guide:is(:fullscreen, .wide) .playfield { flex: 1; min-height: 0; }
.field-guide:is(:fullscreen, .wide) .battle-area { flex: 1; container-type: size; --board-width: min(calc(100% - var(--hud-min)), calc(100cqh * 30 / 17)); }

@media (min-width: 601px) and (min-height: 560px) {
  .field-guide { display: flex; flex-direction: column; height: 100dvh; }
  .playfield { flex: 1; min-height: 0; }
  .battle-area { flex: 1; container-type: size; --board-width: min(calc(100% - var(--hud-min)), calc(100cqh * 30 / 17)); }
}
@media (max-width: 600px) {
  .field-guide { width: 100%; padding: 8px; }
  .identity span { display: none; }
  .identity { gap: 4px; }
  .identity .icon { width: 17px; height: 17px; }
  .masthead { gap: 4px; }
  .tools { gap: 1px; }
  .build-dock { height: clamp(185px, 30dvh, 250px); }
  .tower-detail { flex-basis: 100%; }
  .build-button kbd, #cancel kbd { display: none; }
}
@media (max-width: 600px) and (orientation: portrait) {
  .battle-area { display: grid; grid-template-rows: auto minmax(0, 1fr); grid-template-columns: minmax(0, 1fr); }
  .field-guide:is(:fullscreen, .wide) .board-wrap { container-type: size; }
  .field-guide:is(:fullscreen, .wide) #board { width: min(100cqw, calc(100cqh * 30 / 17)); }
  .hud { grid-template-columns: repeat(4, minmax(0, 1fr)); border-right: 0; }
  .hud :is(.stock, .speed-control) { grid-column: auto; }
  .metric { padding: 4px 5px; min-height: 44px; }
  .metric:nth-child(n) { border-left: 1px solid var(--line); }
  .metric:nth-child(4n+1) { border-left: 0; }
  .metric > span { gap: 2px; font-size: 11px; }
  .metric .icon { width: 13px; height: 13px; }
  .stock-label { position: absolute; width: 1px; height: 1px; clip-path: inset(50%); overflow: hidden; white-space: nowrap; }
  .stock-icons { gap: 2px; min-height: 16px; }
  .stock-icons i { width: 6px; height: 10px; }
}
@media (max-width: 355px) {
  h1 { font-size: 19px; }
  .identity .icon { display: none; }
}
@media (max-height: 500px) and (orientation: landscape) {
  .hud :is(.stock, .speed-control) { grid-column: auto; }
  .metric { min-height: 40px; padding: 3px 6px; gap: 2px; }
  .metric:nth-child(odd) { border-left: 0; }
  .metric:nth-child(even) { border-left: 1px solid var(--line); }
  .metric strong { font-size: 16px; }
  .metric .icon { width: 13px; height: 13px; }
  .stock-label { position: absolute; width: 1px; height: 1px; clip-path: inset(50%); overflow: hidden; white-space: nowrap; }
  .stock-icons { gap: 2px; min-height: 16px; }
  .stock-icons i { width: 6px; height: 9px; }
  .field-guide:is(:fullscreen, .wide) .build-dock { height: clamp(108px, 34dvh, 148px); padding-block: 4px; }
  .field-guide:is(:fullscreen, .wide) .notice { height: 20px; padding-block: 0; }
  .field-guide:not(:fullscreen, .wide) .overlay { align-items: start; }
  .overlay[data-state="lost"] .overlay-panel { padding: 10px 12px; }
  .overlay[data-state="lost"] h2 { font-size: 26px; margin-block: 2px 4px; }
  .overlay[data-state="lost"] #end-detail { margin-bottom: 6px; }
  .result-stats { grid-template-columns: repeat(4, minmax(0, 1fr)); }
  .result-stats .result-wave { grid-column: auto; grid-template-columns: 1fr; gap: 4px; }
  .result-wave dt { font-size: 11px; }
  .result-wave dd { font-size: 24px; }
  .result-best { min-height: 24px; margin-block: 4px; }
  #record-form { margin-top: 6px; }
}
