:root {
  color-scheme: dark;
  --ink: #fff0d0;
  --muted: #c5bca9;
  --gold: #f4c76d;
  --brass: #87643b;
  --panel: linear-gradient(145deg, #242c32, #11191e 75%);
  --button: linear-gradient(#30404d, #172229 78%);
  --primary: linear-gradient(#ffe094, #eab653 52%, #bd7c2e);
  --serif: Georgia, 'Times New Roman', serif;
  --trim: inset 0 0 0 2px #100e0a, inset 0 0 0 3px #b18b51, 0 5px 15px #09100dc0;
}

* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; overflow: hidden; }
body { background: #192719; color: var(--ink); font: 14px/1.4 system-ui, sans-serif; }
[hidden], .screen-hidden { display: none !important; }
button, summary, input { -webkit-tap-highlight-color: transparent; }
button {
  background: var(--button);
  border: 1px solid #997445;
  border-radius: 6px;
  box-shadow: inset 0 1px #ffe3a345, 0 2px 3px #0007;
  color: var(--ink);
  cursor: pointer;
  font: inherit;
  touch-action: manipulation;
}
button:hover:not(:disabled) { filter: brightness(1.17); }
button:disabled { cursor: not-allowed; }
:focus-visible { outline: 2px solid #ffe19b; outline-offset: 3px; }

#titleScreen, #gameScreen { position: fixed; inset: 0; filter: brightness(var(--screen-brightness, 1)); }
#titleScreen {
  display: grid;
  align-items: center;
  padding: clamp(18px, 7vw, 110px);
  overflow: auto;
  background: linear-gradient(90deg, #0c150ee8, #101a1290 40%, #10180f08 75%),
    #253d24 url('assets/title.webp') 66% center / cover no-repeat;
}
.title-card, .masthead, .side, .upgrade-panel, .settings-card, .modal {
  background: var(--panel);
  border: 1px solid #d3a363;
  border-radius: 10px;
  box-shadow: var(--trim);
}
.title-card {
  width: min(100%, 460px);
  padding: clamp(24px, 4vw, 51px);
  background: linear-gradient(145deg, #232c30f7, #101822f5 75%);
  text-align: center;
}
.crest { display: grid; place-items: center; color: var(--gold); font: 700 31px/1 var(--serif); }
.title-card .crest::before, .title-card .crest::after {
  content: ''; width: 58px; height: 1px; background: linear-gradient(90deg, transparent, #c98d45);
  grid-area: 1 / 1; transform: translateX(-53px);
}
.title-card .crest::after { transform: translateX(53px) scaleX(-1); }
.title-card .crest { margin-bottom: 13px; }
.title-card .crest span { grid-area: 1 / 1; }
.eyebrow { color: var(--gold); font-size: 10px; font-weight: 700; letter-spacing: .22em; text-transform: uppercase; }
.title-card h1 {
  margin: 10px 0 16px;
  color: #ffe4ab;
  font: 700 clamp(46px, 5.9vw, 77px)/.92 var(--serif);
  letter-spacing: -.065em;
  text-shadow: 0 3px #57351b, 0 6px 16px #000b;
}
.title-card .intro { margin: 0 auto 24px; max-width: 31ch; color: #d8ceba; }
.title-actions { display: grid; gap: 9px; }
.title-actions button { min-height: 45px; padding: 8px 14px; font: 700 17px/1.15 var(--serif); }
.title-actions button span { margin-left: 8px; font-size: 11px; font-weight: 600; white-space: nowrap; }
.title-actions .launch, .actions .primary, .settings-actions .done, .modal button {
  background: var(--primary);
  border-color: #ffd784;
  box-shadow: inset 0 0 0 2px #9a562f, inset 0 0 0 3px #ffe59d, 0 3px 5px #0008;
  color: #2c1a10;
  text-shadow: 0 1px #ffe4a2;
}

.shell { height: 100%; padding: 12px; display: grid; grid-template-rows: auto minmax(0, 1fr); gap: 10px; }
.masthead {
  z-index: 1;
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 14px;
  min-height: 68px;
  padding: 8px 15px;
}
.masthead header { display: flex; align-items: center; gap: 10px; white-space: nowrap; }
.masthead h1 { margin: 0; color: #ffe1a8; font: 700 clamp(23px, 2.4vw, 34px)/1 var(--serif); letter-spacing: -.055em; }
.masthead .eyebrow { font-size: 8px; }
.masthead .crest { color: #efb858; font-size: 31px; }
.hud, .stats, .stat { display: flex; align-items: center; }
.hud { gap: clamp(8px, 2.3vw, 30px); }
.stats { gap: clamp(7px, 1.5vw, 21px); }
.stat { gap: 6px; color: #e5dbc6; white-space: nowrap; font: 700 15px/1 var(--serif); }
.stat + .stat { border-left: 1px solid #94734783; padding-left: clamp(7px, 1.5vw, 21px); }
.stat b { color: #ffefc9; font-size: 22px; font-variant-numeric: tabular-nums; }
.stat-symbol { color: #f5be56; font-size: 19px; text-shadow: 0 1px #3a1c0c; }
.stat .coin, .cost::before { display: inline-block; width: 13px; height: 9px; border: 1px solid #ffd975; border-radius: 50%; background: linear-gradient(#ffdc73, #e99b1d); box-shadow: 0 2px #c47822, 0 3px #ffce66, 0 4px #96501a; }
.stat.lives .stat-symbol, .stat.lives b { color: #ff8d7a; }
.tag { padding: 5px 8px; border: 1px solid #b0925b55; border-radius: 4px; font-size: 11px; color: #e6d4a6; white-space: nowrap; }
.menu-tab { padding: 7px 12px; white-space: nowrap; font: 700 16px/1.2 var(--serif); }

.layout { display: grid; min-height: 0; grid-template: 'board shop' minmax(0, 1fr) / minmax(0, 1fr) clamp(270px, 24vw, 355px); gap: 10px; }
.board-wrap { grid-area: board; position: relative; min-width: 0; min-height: 0; overflow: hidden; border: 1px solid #9a7948; border-radius: 8px; background: #39532b; }
#game { position: relative; display: block; width: 100%; height: 100%; cursor: crosshair; touch-action: manipulation; }
.battlefield-render { position: absolute; inset: 0; width: 100%; height: 100%; pointer-events: none; }
.three-active #game { opacity: 0; }
.side { grid-area: shop; min-height: 0; display: flex; flex-direction: column; gap: 11px; padding: 14px; overflow: auto; scrollbar-color: #9f784b #181e23; }
.side h2 { display: flex; align-items: center; gap: 9px; margin: 0; color: #ffe4ad; font: 700 23px/1.15 var(--serif); }
.side h2::after { content: ''; flex: 1; height: 1px; background: linear-gradient(90deg, #c69355, transparent); }
.tower-select { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 8px; }
.tower-btn { display: grid; grid-template-rows: auto auto 1fr auto; justify-items: center; align-items: start; min-width: 0; padding: 5px 4px 7px; text-align: center; border-color: #856847; background: radial-gradient(ellipse at 50% 30%, #3c49533b, transparent 70%), linear-gradient(150deg, #2b343b, #172026); touch-action: none; user-select: none; }
.tower-btn:not(:disabled) { cursor: grab; }
#gameScreen.dragging #game, #gameScreen.dragging .tower-btn.selected { cursor: grabbing; }
.drag-ghost { position: fixed; z-index: 4; width: 70px; height: 70px; object-fit: contain; pointer-events: none; transform: translate(-50%, -70%); filter: drop-shadow(0 3px 5px #0009); }
.tower-btn.selected { border-color: #ffdc83; background-color: #3a3730; box-shadow: inset 0 0 0 1px #e8a84b, inset 0 0 13px #bd87374d, 0 0 8px #e8a84b46; }
.tower-btn:disabled img { opacity: .61; filter: saturate(.65); }
.tower-btn:disabled .cost { color: #d7b578; }
.tower-btn.unaffordable { filter: grayscale(1); border-color: #605b52; }
.tower-btn img { width: 100%; height: clamp(38px, 10vh, 105px); object-fit: contain; filter: drop-shadow(0 3px 3px #0009); }
.tower-copy { font: 700 16px/1.05 var(--serif); color: #fff0d0; }
.tower-btn small { margin: 4px 0 3px; color: var(--muted); font-size: 9px; line-height: 1.15; }
.cost { display: inline-flex; align-items: center; gap: 6px; color: #ffd076; font: 700 15px/1 var(--serif); }
.cost::before { content: ''; width: 9px; height: 5px; margin-top: -3px; }
.status { min-height: 2.7em; color: #f5deb1; font-size: 11px; }
.actions { display: grid; grid-template-columns: 1fr 1fr; gap: 7px; }
.actions button { min-height: 36px; padding: 5px; font: 700 15px/1.1 var(--serif); }
.actions .primary { grid-column: 1 / -1; font-size: 18px; }
.actions button:disabled { opacity: .6; }
.help { margin-top: auto; font-size: 11px; color: var(--muted); }
.help summary { width: fit-content; cursor: pointer; color: #f6db9e; }
.help p { margin: 6px 0 0; }
.rules { margin: 6px 0 0; padding-left: 19px; }

.upgrade-panel { position: absolute; left: 9px; bottom: 9px; z-index: 2; width: min(650px, calc(100% - 18px)); max-height: calc(100% - 18px); overflow: auto; padding: 12px 14px; display: none; flex-direction: column; gap: 8px; }
.upgrade-panel.open { display: flex; }
.upgrade-heading, .upgrade-footer, .path-heading { display: flex; justify-content: space-between; align-items: flex-start; gap: 8px; }
.upgrade-heading h2, .settings-head h2, .modal h2 { margin: 0; color: #ffe4ad; font: 700 25px/1.1 var(--serif); }
.upgrade-heading p, .upgrade-foot { margin: 4px 0 0; color: var(--muted); font-size: 11px; }
.upgrade-close, .settings-head button { padding: 1px 8px; font-size: 21px; line-height: 1.18; }
.tower-properties, .upgrade-paths { display: grid; gap: 6px; }
.tower-properties { grid-template-columns: repeat(4, minmax(0, 1fr)); }
.upgrade-paths { grid-template-columns: repeat(3, minmax(0, 1fr)); }
.property-chip, .upgrade-path { padding: 6px; border: 1px solid #8b704872; border-radius: 5px; background: #11191e; }
.property-chip small { display: block; font-size: 8px; letter-spacing: .1em; color: #dabc89; text-transform: uppercase; }
.property-chip b, .path-heading b, .path-next b { font-size: 11px; }
.path-level, .path-stage { color: #ecd194; font-size: 9px; white-space: nowrap; }
.path-stage { font-weight: 700; text-transform: uppercase; }
.path-next { min-height: 42px; margin: 5px 0; font-size: 10px; color: var(--muted); }
.path-next b { display: block; color: var(--ink); }
.upgrade-path button, .upgrade-sell { padding: 6px; font-size: 10px; }
.upgrade-path button { width: 100%; }
.upgrade-path button:disabled { opacity: .6; }
.upgrade-sell { flex: 0 0 auto; background: linear-gradient(#87473d, #582921); }
.upgrade-path.complete { border-color: #7bb4b2; }

.overlay, .settings-backdrop { position: fixed; inset: 0; z-index: 5; padding: 16px; place-items: center; background: #0b130fe0; }
.overlay { display: none; }
.overlay.show, .settings-backdrop { display: grid; }
.settings-backdrop { z-index: 10; }
.settings-card, .modal { width: min(100%, 435px); max-height: 100%; overflow: auto; padding: 24px; }
.settings-head { display: flex; justify-content: space-between; align-items: flex-start; gap: 8px; }
.settings-head p, .modal p { color: var(--muted); }
.settings-row { display: grid; grid-template-columns: 1fr auto; gap: 6px; margin: 14px 0; }
.settings-row output { color: var(--gold); }
.settings-row input { grid-column: 1 / -1; width: 100%; }
.settings-row input, .mute-row input { accent-color: #d9aa57; }
.mute-row { display: flex; align-items: center; gap: 7px; }
.settings-actions { display: flex; justify-content: flex-end; margin-top: 16px; }
.settings-actions button, .modal button { padding: 7px 15px; font-weight: 700; }
.modal { text-align: center; }

@media (max-width: 820px) {
  .tag { display: none; }
}

@media (max-width: 760px) and (orientation: portrait) {
  #titleScreen { align-items: end; justify-items: center; background-image: linear-gradient(0deg, #0c150ee5, #111a1340 58%, #10180f10 75%), url('assets/title.webp'); background-position: 67% center; }
  .masthead { padding: 7px 10px; min-height: 48px; }
  .masthead .crest, .masthead .eyebrow { display: none; }
  .menu-tab { padding: 5px 7px; font-size: 13px; }
  .stat { gap: 4px; font-size: 12px; }
  .stat b { font-size: 17px; }
  .stat-symbol { font-size: 14px; }
  .shell { padding: 6px; gap: 6px; }
  .layout { grid-template: 'board' minmax(0, 1fr) 'shop' auto / minmax(0, 1fr); gap: 6px; }
  .side { gap: 6px; padding: 8px; max-height: 44dvh; }
  .side h2 { font-size: 19px; }
  .tower-select { grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 5px; }
  .tower-btn { padding: 2px 2px 4px; }
  .tower-btn img { height: clamp(24px, 5.5dvh, 54px); }
  .tower-copy { font-size: 13px; }
  .cost { font-size: 12px; }
  .status { min-height: 1.4em; }
  .actions { grid-template-columns: 1.25fr 1fr 1fr; gap: 5px; }
  .actions .primary { grid-column: auto; font-size: 14px; }
  .actions button { min-height: 32px; font-size: 13px; }
  .tower-properties { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .upgrade-paths { grid-template-columns: repeat(3, minmax(118px, 1fr)); overflow: auto; }
}

@media (max-width: 530px) {
  .stat-label { position: absolute; width: 1px; height: 1px; overflow: hidden; clip-path: inset(50%); }
  .title-card .intro { margin-bottom: 16px; }
}

@media (max-height: 540px) and (orientation: landscape) {
  .shell { padding: 5px; gap: 5px; }
  .masthead { min-height: 44px; }
  .layout { grid-template-columns: minmax(0, 1fr) 265px; gap: 5px; }
  .side { padding: 8px; gap: 7px; }
  .side h2 { font-size: 18px; }
  .tower-select { grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 5px; }
  .tower-btn { padding: 3px 2px 5px; }
  .tower-btn img { height: 32px; }
  .tower-copy, .cost { font-size: 12px; }
  .status { min-height: 1.4em; }
  .actions { grid-template-columns: 1.25fr 1fr 1fr; }
  .actions button { min-height: 32px; font-size: 12px; }
  .actions .primary { grid-column: auto; font-size: 13px; }
}
