:root {
  color-scheme: light;
  --paper: #eee4cd;
  --paper-light: #f8f0df;
  --paper-dark: #d8cbaa;
  --ink: #252225;
  --muted: #736b61;
  --line: #b5a98d;
  --yellow: #efc84d;
  --orange: #e9924f;
  --coral: #dc6851;
  --red: #b83f3b;
  --purple: #643c65;
  --mint: #6caa8e;
  --danger: #c73d35;
  --board-size: min(76vh, 68vw, 720px);
}

* { box-sizing: border-box; }

html, body { min-width: 100%; min-height: 100%; }

body {
  margin: 0;
  color: var(--ink);
  background:
    linear-gradient(90deg, rgba(52, 45, 37, .035) 1px, transparent 1px) 0 0 / 28px 28px,
    linear-gradient(rgba(52, 45, 37, .035) 1px, transparent 1px) 0 0 / 28px 28px,
    var(--paper);
  font-family: "IBM Plex Mono", ui-monospace, SFMono-Regular, Consolas, monospace;
  overscroll-behavior: none;
  -webkit-tap-highlight-color: transparent;
}

button { color: inherit; font: inherit; }
button:focus-visible, canvas:focus-visible, a:focus-visible { outline: 3px solid var(--mint); outline-offset: 3px; }

.shell {
  width: 100%;
  min-height: 100vh;
  min-height: 100svh;
  display: flex;
  flex-direction: column;
}

.topbar {
  min-height: 68px;
  display: grid;
  grid-template-columns: 1fr auto 1fr;
  align-items: center;
  gap: 18px;
  padding: 9px clamp(12px, 3vw, 38px);
  border-bottom: 2px solid var(--ink);
  background: rgba(248, 240, 223, .93);
  backdrop-filter: blur(8px);
  position: relative;
  z-index: 5;
}

.brand {
  display: inline-flex;
  align-items: center;
  gap: 10px;
  width: max-content;
  color: var(--ink);
  text-decoration: none;
  font-family: "Archivo Black", Impact, sans-serif;
  font-size: clamp(.82rem, 2vw, 1.05rem);
  letter-spacing: -.035em;
}
.brand img { width: 38px; height: 38px; }
.brand b { color: var(--red); font-weight: inherit; }

.top-stats { display: flex; align-items: stretch; border: 2px solid var(--ink); background: var(--paper-light); }
.top-stats > div { min-width: clamp(76px, 9vw, 120px); display: grid; place-items: center; gap: 1px; padding: 7px 12px; }
.top-stats > div + div { border-left: 2px solid var(--ink); }
.top-stats small, .section-heading small, .wave-card small, .legend-card > small, .modal-card > small {
  color: var(--muted);
  font-size: .58rem;
  font-weight: 700;
  letter-spacing: .16em;
}
.top-stats strong { font-family: "Archivo Black", Impact, sans-serif; font-size: 1.05rem; line-height: 1; font-variant-numeric: tabular-nums; }
.top-stats .core-hearts::before { content: "♥ "; color: var(--red); }
.top-stats .attention { animation: nudge .26s ease 2; color: var(--danger); }

.top-actions { justify-self: end; display: flex; gap: 6px; }
.icon-button {
  width: 36px; height: 36px; padding: 0; border: 2px solid var(--ink); border-radius: 0;
  background: var(--paper-light); font-weight: 800; cursor: pointer; box-shadow: 3px 3px 0 var(--ink);
}
.icon-button:hover { background: var(--yellow); }
.icon-button:active { transform: translate(2px, 2px); box-shadow: 1px 1px 0 var(--ink); }

.game-layout {
  flex: 1;
  width: min(1180px, 100%);
  margin: 0 auto;
  padding: clamp(16px, 3vw, 32px);
  display: grid;
  grid-template-columns: minmax(0, 1fr) 282px;
  align-items: start;
  gap: clamp(20px, 3vw, 38px);
}

.board-column { min-width: 0; display: grid; justify-items: center; gap: 14px; }
.board-frame {
  width: min(100%, var(--board-size));
  aspect-ratio: 1;
  position: relative;
  border: 3px solid var(--ink);
  background: #e6dcc5;
  box-shadow: 10px 10px 0 rgba(37, 34, 37, .2);
  touch-action: none;
  user-select: none;
  -webkit-user-select: none;
}
#game { display: block; width: 100%; height: 100%; touch-action: none; cursor: crosshair; }
#game.demolish { cursor: not-allowed; }
#game.shock { cursor: cell; }

.edge-label {
  position: absolute;
  top: 50%;
  display: flex;
  align-items: center;
  gap: 5px;
  pointer-events: none;
  color: var(--ink);
  font-size: clamp(.5rem, 1vw, .63rem);
  font-weight: 800;
  letter-spacing: .08em;
}
.edge-label i { font-size: 1.1rem; font-style: normal; }
.edge-entry { right: calc(100% + 7px); transform: translateY(-50%); flex-direction: column; }
.edge-core { left: calc(100% + 7px); transform: translateY(-50%); flex-direction: column; }
.edge-entry span, .edge-core span { writing-mode: vertical-rl; }
.edge-entry span { transform: rotate(180deg); }

.announcement {
  position: absolute;
  z-index: 7;
  left: 50%; top: 16%;
  min-width: 180px;
  padding: 10px 17px;
  border: 2px solid var(--ink);
  background: var(--paper-light);
  box-shadow: 5px 5px 0 var(--ink);
  text-align: center;
  font-family: "Archivo Black", Impact, sans-serif;
  font-size: clamp(.8rem, 2.5vw, 1.15rem);
  transform: translate(-50%, -12px) scale(.94);
  opacity: 0;
  pointer-events: none;
  transition: opacity .16s, transform .16s;
}
.announcement.show { opacity: 1; transform: translate(-50%, 0) scale(1); }
.announcement span { display: block; margin-top: 3px; color: var(--red); font-family: "IBM Plex Mono", monospace; font-size: .65em; letter-spacing: .08em; }

.resume-card {
  position: absolute; z-index: 10; inset: 0; display: grid; place-content: center; justify-items: center; gap: 8px;
  background: rgba(238, 228, 205, .82); backdrop-filter: blur(3px);
}
.resume-card strong { font-family: "Archivo Black", Impact, sans-serif; font-size: clamp(2rem, 8vw, 4.5rem); }
.resume-card span { color: var(--muted); font-size: .75rem; }
.resume-card button, .secondary-button { border: 2px solid var(--ink); background: var(--paper-light); padding: 10px 22px; font-weight: 800; box-shadow: 4px 4px 0 var(--ink); cursor: pointer; }
.hidden { display: none !important; }

.hint {
  width: min(100%, var(--board-size));
  min-height: 46px;
  display: grid;
  grid-template-columns: auto 1fr auto;
  align-items: center;
  gap: 10px;
  border-top: 2px solid var(--ink);
  border-bottom: 2px solid var(--ink);
  padding: 7px 4px;
}
.hint-number { display: grid; place-items: center; width: 30px; height: 30px; background: var(--ink); color: var(--paper-light); font-weight: 800; font-size: .7rem; }
.hint p { margin: 0; font-size: clamp(.66rem, 1.7vw, .78rem); font-weight: 700; }
.hint button { border: 0; background: transparent; font-size: 1.4rem; cursor: pointer; }

.control-panel { display: grid; gap: 11px; }
.control-panel section { border: 2px solid var(--ink); background: rgba(248, 240, 223, .72); box-shadow: 5px 5px 0 rgba(37, 34, 37, .14); }
.wave-card { padding: 14px; }
.wave-card > div:first-child { display: flex; align-items: end; justify-content: space-between; }
.wave-card > div:first-child strong { font-family: "Archivo Black", Impact, sans-serif; font-size: .9rem; }

.wave-preview { min-height: 59px; display: flex; align-items: center; flex-wrap: wrap; gap: 7px 12px; padding: 11px 0; }
.preview-unit { display: flex; align-items: center; gap: 5px; font-size: .73rem; font-weight: 800; }
.preview-icon { width: 16px; height: 16px; display: grid; place-items: center; border: 2px solid var(--ink); border-radius: 50%; background: var(--coral); font-size: .48rem; line-height: 1; }
.preview-unit[data-type="runner"] .preview-icon { background: var(--yellow); }
.preview-unit[data-type="tank"] .preview-icon { width: 19px; height: 19px; border-width: 4px; background: var(--purple); color: white; }
.preview-unit[data-type="healer"] .preview-icon { background: var(--mint); }
.preview-unit[data-type="sapper"] .preview-icon { background: var(--orange); box-shadow: 0 0 0 2px var(--paper-light), 0 0 0 3px var(--ink); }
.preview-unit[data-type="flyer"] .preview-icon { background: var(--paper-light); box-shadow: 3px 3px 0 var(--purple); }

.start-button {
  width: 100%; min-height: 46px; display: flex; align-items: center; justify-content: space-between;
  border: 2px solid var(--ink); border-radius: 0; padding: 9px 12px; background: var(--yellow);
  font-family: "Archivo Black", Impact, sans-serif; cursor: pointer; box-shadow: 4px 4px 0 var(--ink);
}
.start-button:hover { background: #f8d966; }
.start-button:active { transform: translate(2px, 2px); box-shadow: 2px 2px 0 var(--ink); }
.start-button:disabled { cursor: default; color: var(--muted); background: var(--paper-dark); box-shadow: none; transform: none; }
.start-button b { font-size: 1.2rem; }
.wave-progress { height: 5px; margin-top: 12px; background: var(--paper-dark); overflow: hidden; }
.wave-progress i { display: block; width: 0; height: 100%; background: var(--red); transition: width .15s; }

.build-card, .power-card, .legend-card { padding: 12px; }
.section-heading { display: flex; justify-content: space-between; align-items: center; margin-bottom: 10px; }
.section-heading span { font-size: .62rem; font-weight: 800; letter-spacing: .08em; }
.mode-button { width: 100%; display: flex; align-items: center; gap: 11px; padding: 9px; border: 2px solid var(--ink); background: var(--paper-light); text-align: left; cursor: pointer; }
.mode-button:hover, .mode-button[aria-pressed="true"] { background: var(--coral); color: #241e1d; }
.tool-icon { display: grid; place-items: center; width: 30px; height: 30px; border: 2px solid currentColor; font-weight: 900; }
.mode-button b, .mode-button small { display: block; }
.mode-button b { font-size: .7rem; }
.mode-button small { margin-top: 2px; font-size: .51rem; opacity: .7; }
.build-card p, .legend-card p { margin: 9px 0 0; color: var(--muted); font-size: .59rem; line-height: 1.5; }
.legend-card p b { color: var(--ink); }

.power-buttons { display: grid; grid-template-columns: repeat(3, 1fr); gap: 6px; }
.power-buttons button { min-width: 0; display: grid; justify-items: center; gap: 2px; padding: 7px 2px; border: 2px solid var(--ink); background: var(--paper-light); cursor: pointer; }
.power-buttons button:hover, .power-buttons button.active { background: var(--mint); }
.power-buttons button:disabled { opacity: .45; cursor: default; }
.power-icon { font-size: 1rem; font-weight: 900; }
.power-buttons b { font-size: .54rem; }
.power-buttons small { font-size: .55rem; }

.height-legend { display: grid; grid-template-columns: repeat(5, 1fr); gap: 4px; margin-top: 8px; align-items: end; }
.height-legend i { display: grid; place-items: center; height: calc(19px + (var(--level) * 3px)); border: 2px solid var(--ink); box-shadow: calc(var(--level) * 1px) calc(var(--level) * 1px) 0 rgba(37,34,37,.6); font-style: normal; font-size: .58rem; font-weight: 900; }
.height-legend i[data-level="1"] { --level: 1; background: var(--yellow); }
.height-legend i[data-level="2"] { --level: 2; background: var(--orange); }
.height-legend i[data-level="3"] { --level: 3; background: var(--coral); }
.height-legend i[data-level="4"] { --level: 4; background: var(--red); color: white; }
.height-legend i[data-level="5"] { --level: 5; background: var(--purple); color: white; }

.modal { position: fixed; z-index: 40; inset: 0; display: grid; place-items: center; padding: 20px; background: rgba(37, 34, 37, .78); backdrop-filter: blur(4px); }
.modal-card { position: relative; width: min(500px, 100%); border: 3px solid var(--ink); background: var(--paper-light); padding: clamp(22px, 6vw, 42px); box-shadow: 12px 12px 0 var(--yellow); }
.modal-card h1 { margin: 6px 0 22px; font-family: "Archivo Black", Impact, sans-serif; font-size: clamp(2rem, 8vw, 4.25rem); line-height: .94; letter-spacing: -.04em; }
.final-stats { display: grid; grid-template-columns: repeat(2, 1fr); border: 2px solid var(--ink); }
.final-stats > div { display: grid; gap: 3px; padding: 12px; }
.final-stats > div:nth-child(even) { border-left: 2px solid var(--ink); }
.final-stats > div:nth-child(n+3) { border-top: 2px solid var(--ink); }
.final-stats span { color: var(--muted); font-size: .58rem; }
.final-stats strong { font-family: "Archivo Black", Impact, sans-serif; font-size: 1.4rem; }
.modal-actions { display: grid; grid-template-columns: 1fr auto; gap: 10px; margin-top: 18px; }
.modal-close { position: absolute; right: 12px; top: 12px; width: 34px; height: 34px; border: 2px solid var(--ink); background: transparent; font-size: 1.25rem; cursor: pointer; }
.help-card h1 { font-size: clamp(1.7rem, 7vw, 3.6rem); }
.how-grid { display: grid; gap: 12px; margin-bottom: 22px; }
.how-grid p { display: grid; grid-template-columns: 86px 1fr; gap: 10px; margin: 0; font-size: .7rem; line-height: 1.45; }
.how-grid b { color: var(--red); text-transform: uppercase; }
.how-grid span { color: var(--muted); }

@keyframes nudge { 0%,100% { transform: translateX(0); } 35% { transform: translateX(-4px); } 70% { transform: translateX(4px); } }

@media (max-width: 900px) {
  :root { --board-size: min(78vw, 680px); }
  .game-layout { grid-template-columns: 1fr; }
  .control-panel { width: min(100%, var(--board-size)); justify-self: center; grid-template-columns: 1fr 1fr; }
  .wave-card { grid-row: span 2; }
  .legend-card { grid-column: 1 / -1; }
}

@media (max-width: 620px) {
  :root { --board-size: calc(100vw - 30px); }
  .topbar { min-height: 61px; grid-template-columns: auto 1fr auto; padding: 7px 10px; gap: 8px; }
  .brand img { width: 34px; height: 34px; }
  .brand > span { display: none; }
  .top-stats { justify-self: center; }
  .top-stats > div { min-width: 59px; padding: 6px 7px; }
  .top-stats small { font-size: .47rem; }
  .top-stats strong { font-size: .82rem; }
  .top-actions { gap: 4px; }
  .icon-button { width: 31px; height: 31px; box-shadow: 2px 2px 0 var(--ink); }
  #help-button { display: none; }
  .game-layout { padding: 14px 15px 24px; gap: 14px; }
  .board-frame { box-shadow: 6px 6px 0 rgba(37,34,37,.2); }
  .edge-label { display: none; }
  .hint { min-height: 40px; }
  .control-panel { grid-template-columns: 1fr; }
  .wave-card { grid-row: auto; }
  .legend-card { grid-column: auto; }
  .legend-card { display: none; }
  .power-buttons button { min-height: 53px; }
}

@media (max-height: 720px) and (min-width: 901px) {
  :root { --board-size: min(76vh, 660px); }
  .topbar { min-height: 58px; }
  .game-layout { padding-top: 14px; }
  .hint { display: none; }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { scroll-behavior: auto !important; animation-duration: .01ms !important; animation-iteration-count: 1 !important; transition-duration: .01ms !important; }
}
