:root {
  --ink: #090a0a;
  --coal: #111312;
  --panel: #181a18;
  --panel-2: #22231f;
  --panel-3: #2b2a24;
  --line: #4d3b29;
  --line-hot: #a86f2f;
  --amber: #e4a849;
  --amber-bright: #ffd37a;
  --teal: #55c2bd;
  --teal-dark: #174745;
  --cream: #f2e7d2;
  --muted: #a79d8d;
  --dim: #736d63;
  --danger: #e2674f;
}

* { box-sizing: border-box; margin: 0; padding: 0; }

html,
body {
  width: 100%;
  height: 100%;
  overflow: hidden;
  background: #080909;
  color: var(--cream);
  font-family: "Avenir Next", "PingFang SC", "Microsoft YaHei", sans-serif;
  font-feature-settings: "tnum" 1;
}

button { font: inherit; }

#app {
  display: flex;
  align-items: stretch;
  justify-content: center;
  gap: 12px;
  height: 100vh;
  height: 100dvh;
  padding: 10px;
  background-color: #0b0c0c;
}

#stage {
  position: relative;
  display: flex;
  flex: 1 1 auto;
  min-width: 0;
  height: 100%;
  align-items: center;
  justify-content: center;
  overflow: hidden;
  background: #14110e;
  border: 1px solid #5a432c;
  border-radius: 5px;
  box-shadow: 0 16px 40px rgba(0, 0, 0, 0.48), inset 0 0 0 2px #171310;
}

#stage::after {
  position: absolute;
  inset: 5px;
  z-index: 2;
  border: 1px solid rgba(226, 168, 73, 0.22);
  pointer-events: none;
  content: "";
}

#game {
  display: block;
  width: auto;
  height: auto;
  max-width: 100%;
  max-height: 100%;
  cursor: crosshair;
  background: #211b15;
}

/* ---------- Command rail ---------- */

#sidebar {
  display: flex;
  flex: 0 0 clamp(288px, 22vw, 318px);
  flex-direction: column;
  gap: 6px;
  height: 100%;
  overflow-y: auto;
  padding: 11px;
  border: 1px solid #5b452e;
  border-radius: 5px;
  background: var(--panel);
  box-shadow: 0 16px 40px rgba(0, 0, 0, 0.52), inset 0 0 0 2px #101110;
  scrollbar-color: #6d4d2c #111312;
  scrollbar-width: thin;
  user-select: none;
}

.brand {
  display: grid;
  grid-template-columns: 40px minmax(0, 1fr) auto;
  align-items: center;
  gap: 9px;
  min-height: 42px;
  padding-bottom: 8px;
  border-bottom: 1px solid var(--line);
}

.brand-mark {
  position: relative;
  display: grid;
  width: 38px;
  height: 38px;
  place-items: center;
  color: #17120b;
  background: var(--amber);
  border: 1px solid var(--amber-bright);
  clip-path: polygon(20% 0, 80% 0, 100% 20%, 100% 80%, 80% 100%, 20% 100%, 0 80%, 0 20%);
}

.brand-mark::after {
  position: absolute;
  inset: 4px;
  border: 1px solid rgba(43, 29, 13, 0.45);
  content: "";
  clip-path: inherit;
}

.brand-mark span { position: relative; z-index: 1; font-size: 19px; transform: rotate(-8deg); }
.brand-copy { min-width: 0; }

.eyebrow {
  overflow: hidden;
  color: var(--teal);
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
  font-size: 7px;
  font-weight: 800;
  letter-spacing: 1.35px;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.brand h1 {
  margin-top: 2px;
  color: var(--cream);
  font-size: 17px;
  font-weight: 850;
  letter-spacing: 0.6px;
  line-height: 1.05;
}

.brand-meta { display: flex; flex-direction: column; align-items: flex-end; gap: 3px; }
.best { max-width: 70px; color: var(--dim); font-size: 8px; line-height: 1.15; text-align: right; }

.reset-run {
  width: 23px;
  min-height: 20px;
  padding: 0 !important;
  color: var(--dim) !important;
  font-size: 15px !important;
  line-height: 1;
  background: transparent !important;
  border-color: transparent !important;
}

#hud {
  display: grid;
  grid-template-columns: 1.3fr 0.85fr 0.85fr;
  gap: 4px;
}

.stat {
  display: flex;
  min-width: 0;
  min-height: 42px;
  flex-direction: column;
  justify-content: center;
  padding: 5px 7px;
  background: var(--panel-2);
  border: 1px solid #35332c;
  border-top-color: #5a4732;
}

.stat .label {
  overflow: hidden;
  color: var(--dim);
  font-size: 8px;
  font-weight: 700;
  letter-spacing: 0.4px;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.stat > span:last-child {
  margin-top: 1px;
  color: var(--cream);
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
  font-size: 15px;
  font-weight: 850;
  line-height: 1;
}

.stat-gold > span:last-child { color: var(--amber-bright); }
.stat.combo {
  grid-column: 1 / -1;
  min-height: 0;
  height: 4px;
  padding: 0;
  color: var(--amber-bright);
  background: #342516;
  border: 0;
  text-align: center;
}
.stat.combo:has(#combo:not(:empty)) { height: 22px; padding: 3px 8px; }
.stat.combo #combo { margin: 0; font-size: 11px; }

.core-row {
  display: grid;
  grid-template-columns: auto 1fr auto;
  align-items: center;
  gap: 7px;
  min-height: 19px;
}

.core-row .label,
#coreText { color: var(--muted); font-size: 9px; font-weight: 700; }
#coreText { font-family: ui-monospace, SFMono-Regular, Menlo, monospace; }

.core-track {
  height: 7px;
  overflow: hidden;
  background: #090a09;
  border: 1px solid #454038;
}

#coreBar {
  width: 100%;
  height: 100%;
  background: var(--teal);
  box-shadow: 0 0 12px rgba(85, 194, 189, 0.38);
  transition: width 0.2s ease;
}

#controls,
#speeds,
.btn-row,
.resume-actions,
.qp-btns { display: flex; gap: 4px; }
#speeds { flex: 1; }
#speeds button,
.btn-row button { flex: 1; }

button {
  min-height: 28px;
  padding: 4px 8px;
  color: var(--cream);
  font-size: 11px;
  font-weight: 700;
  line-height: 1.1;
  background: #252620;
  border: 1px solid #554430;
  border-radius: 3px;
  cursor: pointer;
  transition: transform 90ms ease, background-color 120ms ease, border-color 120ms ease, color 120ms ease;
}

button:hover:not(:disabled) {
  color: #fff5e2;
  background: #343027;
  border-color: #9e6b33;
}

button:active:not(:disabled) { transform: translateY(1px); }
button:focus-visible { outline: 2px solid var(--teal); outline-offset: 2px; }
button:disabled { color: #746f66; background: #1a1b19; border-color: #302f2a; cursor: not-allowed; }
button.active { color: #cafffb; background: var(--teal-dark); border-color: var(--teal); }

button.primary {
  min-height: 34px;
  color: #21160b;
  background: var(--amber);
  border-color: var(--amber-bright);
  box-shadow: inset 0 -3px 0 rgba(85, 48, 16, 0.34);
  font-size: 12px;
  font-weight: 850;
  letter-spacing: 0.2px;
}

button.primary:hover:not(:disabled) { color: #160f08; background: #f1b95c; border-color: #ffe0a0; }

#startWave { width: 100%; }
#autoBtn.active { color: #caffd8; background: #25432e; border-color: #62b977; }

#wavePreview {
  display: flex;
  max-height: 34px;
  flex-wrap: wrap;
  gap: 2px 8px;
  overflow: hidden;
  padding: 5px 7px;
  color: var(--muted);
  background: #141614;
  border: 1px solid #34322c;
  font-size: 9px;
}

#wavePreview .pv { display: inline-flex; align-items: center; gap: 4px; }
.dot { display: inline-block; width: 7px; height: 7px; border: 1px solid rgba(255,255,255,0.28); border-radius: 50%; }

.section-title {
  display: flex;
  align-items: center;
  justify-content: space-between;
  min-height: 14px;
  color: var(--amber);
  font-size: 9px;
  font-weight: 850;
  letter-spacing: 1.1px;
  text-transform: uppercase;
}

.section-title small { color: var(--dim); font-size: 8px; font-weight: 650; letter-spacing: 0; }
.lv-row { display: flex; align-items: center; gap: 8px; }
.lv-row > .section-title { flex: 0 0 auto; }
.lv-row .lv-sel { flex: 1; }
.lv-sel { display: flex; gap: 3px; }
.lv-sel button { min-height: 25px; flex: 1; padding: 3px 1px; font-size: 9px; }

#palette {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 4px;
}

.tool {
  position: relative;
  display: grid;
  min-width: 0;
  min-height: 50px;
  grid-template-columns: 30px minmax(0, 1fr);
  grid-template-rows: auto auto;
  align-items: center;
  column-gap: 4px;
  padding: 5px 5px;
  overflow: hidden;
  text-align: left;
  background: #20221f;
  border-color: #413b30;
}

.tool::before {
  position: absolute;
  inset: 0 auto 0 0;
  width: 2px;
  background: var(--tool-color, #76532f);
  content: "";
}

.tool.active {
  background: #233533;
  border-color: var(--teal);
  box-shadow: inset 0 0 0 1px rgba(85, 194, 189, 0.16);
}

.tool-icon {
  display: grid;
  grid-row: span 2;
  width: 30px;
  height: 30px;
  place-items: center;
  background: transparent;
  border: 0;
  border-radius: 0;
  font-size: 14px;
}

.tool-icon img {
  display: block;
  width: 34px;
  height: 34px;
  object-fit: contain;
  filter: drop-shadow(0 3px 2px rgba(0, 0, 0, 0.34));
}

.lock-mark {
  position: absolute;
  right: -2px;
  bottom: -2px;
  padding: 2px 3px;
  color: #e8ddc8;
  background: #24221d;
  border: 1px solid #75634d;
  border-radius: 2px;
  font: 700 5px/1 ui-monospace, monospace;
  letter-spacing: .4px;
}

.asset-fallback { line-height: 1; }
.inline-asset { width: 30px; height: 30px; object-fit: contain; filter: drop-shadow(0 3px 2px rgba(0,0,0,.34)); }
.sel-title { display: flex; align-items: center; gap: 6px; }

.tool-name {
  overflow: hidden;
  color: #e8ddc8;
  font-size: 9px;
  font-weight: 800;
  line-height: 1.15;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.tool-cost {
  color: var(--amber-bright);
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
  font-size: 8px;
  line-height: 1;
}

.tool-key { position: absolute; top: 2px; right: 3px; color: #6f695f; font: 7px ui-monospace, monospace; }
.tool.cant .tool-cost { color: var(--danger); }
.tool.locked { opacity: 0.46; }
.tool-lock { color: var(--dim) !important; white-space: nowrap; }

#selected {
  min-height: 45px;
  padding: 6px 8px;
  background: #141614;
  border: 1px solid #3b382f;
  border-left: 2px solid var(--teal);
  font-size: 10px;
}

.sel-empty { padding-top: 7px; color: #777166; font-size: 9px; text-align: center; }
.sel-title { margin-bottom: 2px; color: var(--cream); font-weight: 800; }
.sel-title .lv { color: var(--amber-bright); font-size: 9px; }
.sel-stats { color: var(--muted); line-height: 1.35; }
.sel-stats b { color: var(--cream); }
.oc { color: #81ded9; font-size: 10px; }

#buffs { display: flex; min-height: 23px; flex-wrap: wrap; align-items: center; gap: 3px; }
.buff { padding: 1px 4px; background: #20221f; border: 1px solid #514331; border-radius: 2px; font-size: 14px; cursor: help; }
.buff-empty { color: #706b63; font-size: 9px; }

.hint {
  padding: 6px 7px;
  color: #7d776e;
  background: #121412;
  border-left: 2px solid #5f472d;
  font-size: 8px;
  line-height: 1.45;
}

.hint b { color: #a9967b; }
.mobile-only,
.hint-mobile { display: none; }

/* ---------- Overlays ---------- */

.modal {
  position: fixed;
  inset: 0;
  z-index: 100;
  display: flex;
  align-items: center;
  justify-content: center;
  background: rgba(4, 5, 5, 0.82);
  backdrop-filter: blur(5px);
}

.modal.hidden,
.build-confirm.hidden,
#toast.hidden,
#quickPop.hidden { display: none; }

.modal-box {
  width: min(720px, calc(100vw - 32px));
  max-width: 720px;
  padding: 24px 28px;
  color: var(--cream);
  background: #171917;
  border: 1px solid #8f6536;
  border-radius: 4px;
  box-shadow: 0 24px 80px rgba(0,0,0,0.7), inset 0 0 0 3px #0f100f;
  text-align: center;
}

.modal-box h2 { margin-bottom: 16px; color: var(--amber-bright); font-size: 22px; letter-spacing: 1px; }
#draftCards { display: flex; justify-content: center; gap: 10px; }

.card {
  display: flex;
  width: 180px;
  min-height: 180px;
  flex-direction: column;
  align-items: center;
  gap: 9px;
  padding: 18px 14px;
  border-color: #564630;
  border-radius: 3px;
}

.card:hover { transform: translateY(-3px); background: #302a21; border-color: var(--amber); }
.card-icon { font-size: 34px; }
.card-name { color: var(--cream); font-size: 15px; font-weight: 850; }
.card-desc { color: var(--muted); font-size: 11px; line-height: 1.5; }

#overStats,
.resume-info { display: flex; flex-direction: column; gap: 8px; margin-bottom: 18px; color: var(--muted); font-size: 14px; }
#overStats b { color: var(--amber-bright); }
.resume-actions { justify-content: center; }

#toast {
  position: fixed;
  bottom: 28px;
  left: 50%;
  z-index: 200;
  padding: 9px 18px;
  color: #fff2dc;
  background: #713324;
  border: 1px solid #d97859;
  border-radius: 3px;
  box-shadow: 0 8px 30px rgba(0,0,0,0.45);
  font-size: 12px;
  transform: translateX(-50%);
  animation: toast-in 150ms ease-out;
}

@keyframes toast-in {
  from { opacity: 0; transform: translateX(-50%) translateY(8px); }
  to { opacity: 1; transform: translateX(-50%) translateY(0); }
}

#quickPop {
  position: absolute;
  z-index: 50;
  display: flex;
  flex-direction: column;
  gap: 5px;
  padding: 7px;
  color: var(--cream);
  background: rgba(19, 21, 19, 0.96);
  border: 1px solid #83603a;
  border-radius: 3px;
  box-shadow: 0 8px 24px rgba(0,0,0,0.55);
  white-space: nowrap;
}

#qpTitle { color: var(--amber-bright); font-size: 10px; font-weight: 800; }
.qp-btns button { min-height: 25px; flex: 1; padding: 3px 6px; font-size: 10px; }

.build-confirm {
  position: absolute;
  bottom: 12px;
  left: 50%;
  z-index: 60;
  display: flex;
  width: min(500px, calc(100% - 24px));
  align-items: center;
  gap: 7px;
  padding: 8px;
  color: var(--cream);
  background: rgba(19, 21, 19, 0.96);
  border: 1px solid #83603a;
  border-radius: 4px;
  box-shadow: 0 12px 36px rgba(0, 0, 0, 0.62);
  transform: translateX(-50%);
  backdrop-filter: blur(8px);
}

.build-confirm-copy { display: grid; min-width: 0; flex: 1; gap: 2px; }
.build-confirm-copy span { color: var(--amber-bright); font-size: 9px; font-weight: 800; }
.build-confirm-copy strong { overflow: hidden; color: var(--muted); font-size: 10px; text-overflow: ellipsis; white-space: nowrap; }
.build-confirm button { min-width: 76px; min-height: 42px; }

@media (max-width: 1040px) {
  #app { gap: 8px; padding: 8px; }
  #sidebar { flex-basis: 280px; }
  .tool { grid-template-columns: 27px minmax(0, 1fr); padding-inline: 4px; }
  .tool-icon { width: 27px; height: 27px; font-size: 12px; }
  .tool-icon img { width: 30px; height: 30px; }
}

@media (max-width: 820px) {
  html,
  body {
    min-height: 100%;
    overflow-x: hidden;
    overflow-y: auto;
    overscroll-behavior-y: contain;
    background: #0b0c0c;
  }

  body { -webkit-tap-highlight-color: transparent; }

  #app {
    display: flex;
    min-height: 100dvh;
    height: auto;
    flex-direction: column;
    gap: 7px;
    padding: max(6px, env(safe-area-inset-top)) 6px max(10px, env(safe-area-inset-bottom));
  }

  #stage {
    position: sticky;
    top: 0;
    z-index: 30;
    width: 100%;
    height: auto;
    aspect-ratio: 8 / 5;
    flex: none;
    border-radius: 4px;
    box-shadow: 0 12px 30px rgba(0, 0, 0, 0.66), 0 2px 0 #0b0c0c;
  }

  #game {
    width: 100%;
    height: auto;
    touch-action: manipulation;
  }

  #sidebar {
    width: 100%;
    height: auto;
    flex-basis: auto;
    gap: 8px;
    overflow: visible;
    padding: 10px 9px 14px;
    border-radius: 4px;
    box-shadow: 0 14px 34px rgba(0, 0, 0, 0.4), inset 0 0 0 2px #101110;
  }

  #sidebar > header {
    display: flex;
    min-height: 42px;
    align-items: center;
    justify-content: space-between;
    gap: 8px;
    padding-bottom: 7px;
    border-bottom: 1px solid var(--line);
  }

  #sidebar > header h1 {
    overflow: hidden;
    font-size: 17px;
    font-weight: 850;
    letter-spacing: -0.2px;
    line-height: 1.1;
    text-overflow: ellipsis;
    white-space: nowrap;
  }

  #sidebar > header > div { min-width: 0; flex: 0 1 auto; }
  #sidebar > header .best { max-width: 88px; }
  #resetRun { min-width: 42px; min-height: 42px; padding: 0 !important; }

  .brand {
    grid-template-columns: 34px minmax(0, 1fr) auto;
    min-height: 38px;
    gap: 8px;
    padding-bottom: 7px;
  }

  .brand-mark { width: 32px; height: 32px; }
  .brand-mark span { font-size: 16px; }
  .brand h1 { font-size: 16px; }
  .eyebrow { font-size: 6px; letter-spacing: 1.1px; }
  .best { max-width: 96px; font-size: 8px; }

  #hud { gap: 5px; }
  .stat { min-height: 48px; padding: 7px 8px; }
  .stat .label { font-size: 9px; }
  .stat > span:last-child { font-size: 18px; }

  .core-row { min-height: 26px; gap: 8px; }
  .core-row .label,
  #coreText { font-size: 10px; }
  .core-track { height: 9px; }

  #controls {
    display: grid;
    grid-template-columns: minmax(0, 3fr) minmax(74px, 1.35fr) 44px 44px;
    gap: 5px;
  }

  #speeds { min-width: 0; gap: 4px; }
  #controls button,
  #speeds button {
    min-width: 0;
    min-height: 44px;
    padding: 6px 4px;
    font-size: 11px;
  }

  #autoBtn { white-space: nowrap; }
  #pause,
  #mute { padding-inline: 0 !important; font-size: 14px !important; }

  #startWave { min-height: 48px; font-size: 13px; }
  #wavePreview { max-height: 38px; padding: 7px 8px; font-size: 10px; }
  #repair { min-height: 42px; }
  #repair:disabled { display: none; }

  #skills {
    display: grid;
    grid-template-columns: repeat(3, minmax(0, 1fr));
    gap: 5px;
  }

  #skills .skill {
    position: relative;
    min-width: 0;
    min-height: 44px;
    padding: 6px 4px;
    overflow: hidden;
    font-size: 10px;
    text-overflow: ellipsis;
    white-space: nowrap;
  }

  #skills .tool-key { display: none; }
  #skills .ready { color: #fff3d7; border-color: var(--amber); }

  .section-title { min-height: 18px; font-size: 10px; }
  .section-title small { font-size: 9px; }
  .desktop-only { display: none; }
  .mobile-only { display: inline; }

  .lv-row { display: grid; grid-template-columns: auto minmax(0, 1fr) auto; gap: 7px; }
  .lv-sel { gap: 4px; }
  .lv-sel button { min-height: 42px; font-size: 10px; }
  #blueprintBtn { min-height: 42px; padding-inline: 9px; white-space: nowrap; }

  #palette {
    display: grid;
    grid-auto-columns: minmax(112px, 31vw);
    grid-auto-flow: column;
    grid-template-columns: none;
    grid-template-rows: repeat(2, 64px);
    gap: 6px;
    width: 100%;
    padding: 1px 1px 7px;
    overflow-x: auto;
    overflow-y: hidden;
    overscroll-behavior-x: contain;
    scroll-snap-type: x proximity;
    scrollbar-color: #6d4d2c #111312;
    scrollbar-width: thin;
  }

  .tool {
    min-height: 64px;
    grid-template-columns: 38px minmax(0, 1fr);
    column-gap: 7px;
    padding: 7px 8px;
    scroll-snap-align: start;
  }

  .tool-icon { width: 38px; height: 38px; }
  .tool-icon img { width: 42px; height: 42px; }
  .tool-name { font-size: 10px; }
  .tool-cost { font-size: 9px; }
  .tool-key { display: none; }

  #selected { min-height: 56px; padding: 8px 10px; font-size: 11px; }
  .sel-empty { padding-top: 10px; font-size: 10px; }
  .sel-stats { line-height: 1.45; }

  .btn-row { gap: 6px; }
  .btn-row button,
  #cycleMode { min-height: 44px; }

  #buffs { min-height: 32px; }

  .hint { padding: 9px 10px; font-size: 10px; line-height: 1.5; }
  .hint-desktop { display: none; }
  .hint-mobile { display: inline; }

  #quickPop {
    top: auto !important;
    right: 8px;
    bottom: 8px;
    left: 8px !important;
    flex-direction: row;
    align-items: center;
    gap: 8px;
    padding: 7px;
    background: rgba(15, 17, 15, 0.94);
    backdrop-filter: blur(8px);
  }

  #qpTitle {
    max-width: 112px;
    overflow: hidden;
    font-size: 10px;
    text-overflow: ellipsis;
  }

  .qp-btns { min-width: 0; flex: 1; gap: 5px; }
  .qp-btns button { min-height: 42px; padding: 4px 8px; font-size: 11px; }

  .modal {
    align-items: flex-end;
    padding: 12px 8px max(8px, env(safe-area-inset-bottom));
  }

  .modal-box {
    width: 100%;
    max-height: calc(100dvh - 20px);
    padding: 20px 16px 18px;
    overflow-y: auto;
    border-radius: 8px 8px 4px 4px;
  }

  .modal-box h2 { margin-bottom: 13px; font-size: 19px; }

  #draftCards {
    display: grid;
    grid-template-columns: 1fr;
    gap: 8px;
  }

  .card {
    display: grid;
    width: 100%;
    min-height: 86px;
    grid-template-columns: 46px minmax(0, 1fr);
    grid-template-rows: auto auto;
    gap: 3px 10px;
    padding: 12px 14px;
    text-align: left;
  }

  .card-icon { grid-row: span 2; font-size: 30px; text-align: center; }
  .card-name { font-size: 14px; }
  .card-desc { font-size: 11px; line-height: 1.35; }

  #bpCards {
    display: grid;
    grid-template-columns: 1fr;
    gap: 8px;
  }

  .bp-card {
    display: flex;
    width: 100%;
    min-height: 0;
    align-items: stretch;
    gap: 7px;
    padding: 12px;
    text-align: left;
  }

  .bp-grid {
    display: grid;
    width: 100%;
    aspect-ratio: 8 / 5;
    grid-template-columns: repeat(24, minmax(0, 1fr));
    overflow: hidden;
    background: #0c0e0d;
    border: 1px solid #433a2e;
  }

  .bp-cell { min-width: 0; background: #151816; border: 1px solid rgba(255, 255, 255, 0.015); }
  .bp-wall { background: #8a755b; }
  .bp-rock { background: #3e4542; }
  .bp-mine { background: #a7782f; }
  .bp-hl { background: #244442; }
  .bp-spawn { background: #7652a2; }
  .bp-core { background: #d79236; }
  #bpClose { width: 100%; min-height: 44px; }

  .resume-actions { flex-direction: column; gap: 7px; }
  .resume-actions button,
  #restart { width: 100%; min-height: 46px; }

  #toast {
    right: 12px;
    bottom: max(18px, env(safe-area-inset-bottom));
    left: 12px;
    padding: 10px 14px;
    text-align: center;
    transform: none;
  }

  .build-confirm {
    right: 8px;
    bottom: 8px;
    left: 8px;
    width: auto;
    transform: none;
  }

  @keyframes toast-in {
    from { opacity: 0; transform: translateY(8px); }
    to { opacity: 1; transform: translateY(0); }
  }
}

@media (max-width: 820px) and (orientation: landscape) and (max-height: 500px) {
  html,
  body { overflow: hidden; }

  #app {
    display: grid;
    height: 100dvh;
    min-height: 0;
    grid-template-columns: minmax(0, 1.6fr) minmax(280px, 0.85fr);
    grid-template-rows: 1fr;
  }

  #stage {
    position: relative;
    top: auto;
    height: 100%;
    aspect-ratio: auto;
  }

  #game { width: auto; height: auto; max-width: 100%; max-height: 100%; }

  #sidebar {
    height: 100%;
    overflow-y: auto;
    overscroll-behavior-y: contain;
  }
}

@media (pointer: coarse) and (min-width: 700px) and (max-width: 1180px) {
  #game { touch-action: manipulation; }
  #sidebar { flex-basis: 310px; }
  #controls button,
  #speeds button,
  .lv-sel button,
  #blueprintBtn,
  .btn-row button,
  #cycleMode,
  #repair { min-height: 42px; }
  .tool { min-height: 58px; }
  .qp-btns button { min-height: 44px; }
}

@media (max-height: 700px) and (min-width: 821px) {
  #sidebar { gap: 4px; padding-block: 8px; }
  #wavePreview,
  .hint { display: none !important; }
  .tool { min-height: 44px; }
  .stat { min-height: 36px; }
}

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

/* ============================================================
   新功能组件样式（追加于既有皮肤之后，不覆盖原主题）
   ============================================================ */

/* ---------- 技能栏 ---------- */

#skills {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 4px;
}

.skill {
  position: relative;
  display: grid;
  min-width: 0;
  min-height: 42px;
  grid-template-columns: 34px minmax(0, 1fr);
  align-items: center;
  gap: 3px;
  padding: 3px 5px 3px 3px;
  overflow: hidden;
  text-align: left;
  border-color: color-mix(in srgb, var(--skill-color) 34%, #413b30);
}

.skill::before {
  position: absolute;
  inset: 0 auto 0 0;
  width: 2px;
  background: var(--skill-color);
  content: "";
  opacity: .65;
}

.skill-icon {
  display: block;
  width: 34px;
  height: 34px;
  object-fit: contain;
  filter: saturate(.55) brightness(.72) drop-shadow(0 3px 2px rgba(0,0,0,.35));
}

.skill-copy { display: grid; min-width: 0; gap: 2px; }
.skill-name { overflow: hidden; font-size: 9px; text-overflow: ellipsis; white-space: nowrap; }
.skill-state { color: var(--dim); font: 7px/1 ui-monospace, monospace; }

.skill:disabled { opacity: .72; }
.skill.ready .skill-icon { filter: saturate(1.05) brightness(1.06) drop-shadow(0 0 5px color-mix(in srgb, var(--skill-color) 55%, transparent)); }
.skill.ready .skill-state { color: var(--skill-color); }
.skill.ready::before { opacity: 1; }

#blueprintBtn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
}

.skill.ready {
  border-color: var(--amber-bright, #fbbf24);
  box-shadow: 0 0 6px rgba(251, 191, 36, 0.35);
}

/* 提前开波按钮（战斗中 startWave 变为提前开波） */
#startWave.early {
  background: #7c2d12;
  border-color: #f97316;
}

#startWave.early:hover:not(:disabled) { background: #9a3412; }

/* ---------- 迷宫蓝图 ---------- */

#bpCards {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 12px;
}

.bp-card {
  display: flex;
  width: 100%;
  min-height: 0;
  align-items: stretch;
  gap: 8px;
  padding: 14px 12px;
  text-align: left;
}

.bp-grid {
  display: grid;
  width: 100%;
  aspect-ratio: 8 / 5;
  grid-template-columns: repeat(24, minmax(0, 1fr));
  gap: 1px;
  overflow: hidden;
  background: #0b1020;
  border: 1px solid #1e293b;
  border-radius: 6px;
  padding: 4px;
  margin: 0 auto;
}

.bp-cell { min-width: 0; border-radius: 1px; background: rgba(148, 163, 184, 0.08); }
.bp-cell.bp-wall { background: #60a5fa; }
.bp-cell.bp-rock { background: #475569; }
.bp-cell.bp-mine { background: #fbbf24; }
.bp-cell.bp-spawn { background: #a78bfa; }
.bp-cell.bp-core { background: #22d3ee; }
.bp-cell.bp-hl { background: #31456b; }

/* ---------- 传说增益卡 ---------- */

.card.epic {
  border-color: #f59e0b;
  background: linear-gradient(160deg, #292012 0%, #1a2540 70%);
  box-shadow: 0 0 14px rgba(245, 158, 11, 0.25);
}

.epic-tag {
  font-size: 9px;
  color: #0f172a;
  background: #f59e0b;
  border-radius: 4px;
  padding: 1px 4px;
  vertical-align: middle;
  margin-left: 4px;
}

.buff.epic {
  border-color: #f59e0b;
  box-shadow: 0 0 5px rgba(245, 158, 11, 0.4);
}

/* 克制提示 / 伤害统计 */
.counter { color: #7dd3fc; font-size: 10px; }

/* 组件块位于响应式规则之后，手机端在这里重新收口，避免桌面值反向覆盖。 */
@media (max-width: 820px) {
  #skills {
    display: grid;
    grid-template-columns: repeat(3, minmax(0, 1fr));
    gap: 5px;
  }

  #skills .skill {
    min-height: 48px;
    grid-template-columns: 36px minmax(0, 1fr);
    gap: 4px;
    padding: 4px 5px 4px 3px;
  }

  .skill-icon { width: 36px; height: 36px; }
  .skill-name { font-size: 9px; }
  .skill-state { font-size: 7px; }
  #skills .tool-key { display: none; }

  #blueprintBtn { min-height: 42px; padding-inline: 8px; white-space: nowrap; }
  #bpCards {
    display: grid;
    grid-template-columns: 1fr;
    gap: 8px;
  }

  .bp-card {
    display: flex;
    width: 100%;
    min-height: 0;
    gap: 7px;
    padding: 12px;
  }

  .bp-grid {
    width: 100%;
    aspect-ratio: 8 / 5;
    grid-template-columns: repeat(24, minmax(0, 1fr));
    gap: 0;
    padding: 0;
    background: #0c0e0d;
    border-color: #433a2e;
    border-radius: 3px;
  }

  .bp-cell { min-width: 0; width: auto; height: auto; border: 1px solid rgba(255,255,255,.015); }
}
