:root {
  --wood-dark: rgba(20, 16, 13, 0.92);
  --wood-mid: rgba(34, 27, 21, 0.94);
  --border-iron: #5a4936;
  --gold: #d4a644;
  --gold-bright: #f2cf6c;
  --parchment: #e7dcc5;
  --muted: #a69882;
  --danger: #b53a2d;
  --ok: #4d8f46;
  --kashub-blue: #2d6cb5;
  --kashub-navy: #1c3a6e;
  --kashub-red: #b8332a;
}

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

html, body, #app {
  width: 100%;
  height: 100%;
  overflow: hidden;
  background: #12100d;
  color: var(--parchment);
  font-family: "Segoe UI", system-ui, -apple-system, sans-serif;
}

#game-canvas {
  display: block;
  width: 100%;
  height: 100%;
  cursor: default;
  touch-action: none;
}

#hp-canvas {
  position: fixed;
  inset: 0;
  width: 100%;
  height: 100%;
  pointer-events: none;
  z-index: 12;
}

#mobile-touch-bar {
  position: fixed;
  left: 8px;
  bottom: 152px;
  z-index: 22;
  display: none;
  gap: 6px;
}

.touch-pill {
  background: rgba(24, 19, 14, 0.92);
  border: 1px solid #7c6242;
  color: var(--parchment);
  border-radius: 999px;
  padding: 6px 11px;
  font-size: 11px;
  font-weight: 700;
  cursor: pointer;
  box-shadow: 0 4px 12px rgba(0,0,0,0.45);
}

.touch-pill.active {
  border-color: var(--gold-bright);
  color: var(--gold-bright);
  background: rgba(212, 166, 68, 0.22);
}

.hud-btn.active {
  border-color: var(--gold-bright);
  color: var(--gold-bright);
  background: rgba(212, 166, 68, 0.2);
}

.hidden {
  display: none !important;
}

.hud-panel {
  background: linear-gradient(180deg, var(--wood-mid), var(--wood-dark));
  border: 1px solid var(--border-iron);
  box-shadow: 0 8px 28px rgba(0, 0, 0, 0.65), inset 0 1px 0 rgba(242, 207, 108, 0.14);
  backdrop-filter: blur(8px);
  border-radius: 8px;
}

/* Subtle Kashubian embroidery stripe */
.kashubian-border {
  height: 4px;
  width: 100%;
  border-radius: 4px 4px 0 0;
  background: repeating-linear-gradient(
    90deg,
    var(--kashub-navy) 0 14px,
    var(--kashub-blue) 14px 26px,
    var(--gold) 26px 36px,
    var(--kashub-red) 36px 46px,
    #3d7a3c 46px 58px
  );
  opacity: 0.85;
  margin-bottom: 8px;
}

/* TOP BAR — strictly single-row on desktop so it NEVER overlaps #objectives-panel or #build-hint */
#top-bar {
  position: fixed;
  top: 8px;
  left: 10px;
  right: 10px;
  z-index: 24;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  padding: 6px 10px;
  flex-wrap: nowrap;
}

.brand-chip {
  display: flex;
  align-items: center;
  gap: 7px;
  flex-shrink: 0;
}

.brand-icon {
  width: 26px;
  height: 26px;
  filter: drop-shadow(0 2px 4px rgba(0, 0, 0, 0.6));
}

.brand-name {
  font-weight: 800;
  letter-spacing: 0.06em;
  color: var(--gold-bright);
  font-size: 13px;
  line-height: 1.05;
  white-space: nowrap;
}

.brand-sub {
  font-size: 10px;
  color: var(--muted);
  white-space: nowrap;
}

.resources-strip {
  display: flex;
  align-items: center;
  gap: 10px;
  background: rgba(10, 8, 6, 0.55);
  border: 1px solid rgba(212, 166, 68, 0.22);
  padding: 4px 10px;
  border-radius: 6px;
  flex-shrink: 0;
}

.res-item {
  display: flex;
  align-items: center;
  gap: 5px;
  font-size: 12px;
  white-space: nowrap;
}

.res-dot {
  width: 9px;
  height: 9px;
  border-radius: 50%;
  box-shadow: 0 0 6px currentColor;
}
.res-dot.milk { background: #f3efe4; color: #f3efe4; }
.res-dot.wood { background: #8f5f36; color: #8f5f36; }
.res-dot.amber { background: #e89b25; color: #e89b25; }
.res-dot.pop { background: #6aa1d9; color: #6aa1d9; }

.res-label {
  color: var(--muted);
  font-size: 11px;
}

.res-item strong {
  color: #fff6e3;
  font-variant-numeric: tabular-nums;
  min-width: 22px;
}

.top-actions {
  display: flex;
  align-items: center;
  gap: 5px;
  flex-wrap: nowrap;
  flex-shrink: 0;
}

.hud-btn, .hud-select, .mini-btn {
  background: rgba(45, 36, 27, 0.95);
  color: var(--parchment);
  border: 1px solid #6b563e;
  border-radius: 5px;
  padding: 5px 8px;
  font-size: 11.5px;
  font-weight: 600;
  cursor: pointer;
  white-space: nowrap;
  transition: background 0.15s, border-color 0.15s, transform 0.1s;
}

.hud-btn:hover, .hud-select:hover, .mini-btn:hover {
  background: rgba(68, 54, 39, 0.98);
  border-color: var(--gold);
}

.hud-btn:active { transform: translateY(1px); }

.hud-btn.danger {
  background: linear-gradient(180deg, #7f2920, #591c16);
  border-color: #a84237;
}
.hud-btn.danger:hover {
  background: linear-gradient(180deg, #9c3328, #6d211a);
}

.mini-btn {
  padding: 3px 8px;
  font-size: 11px;
}

/* OBJECTIVES PANEL */
#objectives-panel {
  position: fixed;
  top: 66px;
  right: 10px;
  width: 255px;
  z-index: 18;
  padding: 0 12px 10px;
}

.panel-title {
  font-size: 11px;
  font-weight: 800;
  letter-spacing: 0.1em;
  color: var(--gold);
  margin-bottom: 6px;
}

#objectives-list {
  list-style: none;
  display: flex;
  flex-direction: column;
  gap: 5px;
  font-size: 12px;
}

#objectives-list li {
  display: flex;
  align-items: flex-start;
  gap: 6px;
  line-height: 1.3;
  color: var(--parchment);
}

#objectives-list li.done {
  color: #88be7d;
  text-decoration: line-through;
  opacity: 0.85;
}

.wave-timer {
  margin-top: 8px;
  padding: 5px 8px;
  border-radius: 5px;
  background: rgba(181, 58, 45, 0.16);
  border: 1px solid rgba(181, 58, 45, 0.45);
  color: #f3b5ae;
  font-size: 11.5px;
  font-weight: 600;
}

#event-log {
  margin-top: 8px;
  border-top: 1px solid rgba(255, 255, 255, 0.08);
  padding-top: 6px;
  display: flex;
  flex-direction: column;
  gap: 4px;
  max-height: 96px;
  overflow-y: auto;
  font-size: 11px;
  line-height: 1.3;
  color: var(--muted);
}

.log-line.alert { color: #f09286; font-weight: 600; }
.log-line.good { color: #9ed48f; }

/* BOTTOM HUD */
#bottom-hud {
  position: fixed;
  bottom: 10px;
  left: 10px;
  right: 10px;
  z-index: 20;
  display: grid;
  grid-template-columns: 204px minmax(280px, 1fr) 340px;
  gap: 10px;
  align-items: end;
  pointer-events: none;
}

#bottom-hud > * {
  pointer-events: auto;
}

.minimap-wrap {
  padding: 9px;
}

#minimap {
  display: block;
  width: 184px;
  height: 184px;
  border-radius: 5px;
  border: 1px solid #6b563e;
  cursor: crosshair;
  background: #141914;
}

.minimap-footer {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-top: 6px;
  font-size: 11px;
  color: var(--muted);
}

.selection-wrap {
  padding: 0 14px 12px;
  min-height: 142px;
}

.sel-empty {
  font-size: 12.5px;
  line-height: 1.55;
  color: var(--muted);
  padding-top: 12px;
}

.sel-empty code {
  background: rgba(255, 255, 255, 0.08);
  padding: 1px 5px;
  border-radius: 4px;
  color: var(--gold-bright);
  font-size: 11px;
}

.sel-header {
  display: flex;
  align-items: center;
  gap: 10px;
  margin-bottom: 8px;
}

.sel-badge {
  width: 40px;
  height: 40px;
  border-radius: 6px;
  background: linear-gradient(135deg, #3c2f21, #1e1711);
  border: 1px solid var(--gold);
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: 800;
  font-size: 17px;
  color: var(--gold-bright);
  flex-shrink: 0;
}

.sel-name {
  font-size: 15px;
  font-weight: 700;
  color: #fff6e5;
}

.sel-role {
  font-size: 11.5px;
  color: var(--muted);
}

.sel-stats {
  margin-left: auto;
  font-size: 12px;
  color: var(--parchment);
  text-align: right;
  line-height: 1.35;
}

.hp-track {
  height: 8px;
  border-radius: 4px;
  background: rgba(0, 0, 0, 0.55);
  border: 1px solid rgba(255, 255, 255, 0.1);
  overflow: hidden;
  margin-bottom: 8px;
}

.hp-fill {
  height: 100%;
  background: linear-gradient(90deg, #4b8f44, #7cc466);
  width: 100%;
  transition: width 0.15s;
}

.sel-multi {
  display: flex;
  flex-wrap: wrap;
  gap: 4px;
}

.sel-chip {
  font-size: 11px;
  padding: 2px 7px;
  border-radius: 4px;
  background: rgba(212, 166, 68, 0.14);
  border: 1px solid rgba(212, 166, 68, 0.32);
  color: var(--parchment);
}

.commands-wrap {
  padding: 0 12px 12px;
  min-height: 142px;
}

.cmd-grid {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 6px;
}

.cmd-btn {
  background: linear-gradient(180deg, rgba(50, 40, 30, 0.95), rgba(29, 23, 18, 0.98));
  border: 1px solid #6b563e;
  border-radius: 6px;
  padding: 7px 6px;
  text-align: left;
  cursor: pointer;
  color: var(--parchment);
  transition: border-color 0.15s, background 0.15s;
}

.cmd-btn:hover:not(:disabled) {
  border-color: var(--gold-bright);
  background: linear-gradient(180deg, rgba(68, 54, 39, 0.98), rgba(38, 30, 22, 0.98));
}

.cmd-btn:disabled {
  opacity: 0.45;
  cursor: not-allowed;
}

.cmd-btn .cmd-name {
  font-size: 11.5px;
  font-weight: 700;
  color: #fff3da;
  display: block;
}

.cmd-btn .cmd-cost {
  font-size: 10px;
  color: var(--gold);
  margin-top: 2px;
  display: block;
}

/* SELECTION MARQUEE & BUBBLES */
#selection-box {
  position: fixed;
  border: 1.5px solid #e6c367;
  background: rgba(212, 166, 68, 0.15);
  pointer-events: none;
  z-index: 15;
  display: none;
}

#bubbles-layer {
  position: fixed;
  inset: 0;
  pointer-events: none;
  z-index: 14;
  overflow: hidden;
}

.speech-bubble {
  position: absolute;
  transform: translate(-50%, -100%);
  background: rgba(23, 19, 15, 0.9);
  border: 1px solid var(--gold);
  color: #fff4db;
  font-size: 11px;
  font-weight: 600;
  padding: 3px 8px;
  border-radius: 10px;
  white-space: nowrap;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.45);
}

#hover-tip {
  position: fixed;
  z-index: 26;
  display: none;
  pointer-events: none;
  background: rgba(16, 13, 10, 0.92);
  border: 1px solid var(--gold);
  color: #fff6e3;
  font-size: 11.5px;
  font-weight: 600;
  padding: 4px 9px;
  border-radius: 6px;
  white-space: nowrap;
  box-shadow: 0 6px 16px rgba(0, 0, 0, 0.55);
  transform: translate(14px, -28px);
}

.float-popup {
  position: absolute;
  transform: translate(-50%, -100%);
  font-size: 12px;
  font-weight: 800;
  text-shadow: 0 2px 6px rgba(0, 0, 0, 0.9);
  pointer-events: none;
  white-space: nowrap;
}

#build-hint {
  position: fixed;
  top: 66px;
  left: 50%;
  transform: translateX(-50%);
  z-index: 25;
  background: rgba(25, 36, 22, 0.95);
  border: 1px solid #6ab05a;
  color: #e4f7df;
  padding: 7px 14px;
  border-radius: 6px;
  font-size: 12px;
  box-shadow: 0 6px 20px rgba(0, 0, 0, 0.5);
}

#protocol-banner {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  z-index: 90;
  background: #8a2c20;
  color: #fff;
  text-align: center;
  padding: 8px 12px;
  font-size: 12.5px;
}

/* DROP OVERLAY */
#drop-overlay {
  position: fixed;
  inset: 0;
  z-index: 80;
  background: rgba(14, 12, 10, 0.78);
  display: none;
  align-items: center;
  justify-content: center;
  pointer-events: none;
}

#drop-overlay.active {
  display: flex;
}

.drop-card {
  border: 2px dashed var(--gold-bright);
  background: rgba(26, 21, 16, 0.95);
  padding: 30px 42px;
  border-radius: 12px;
  text-align: center;
}

.drop-title {
  font-size: 18px;
  font-weight: 800;
  color: var(--gold-bright);
  margin-bottom: 6px;
}

.drop-sub {
  font-size: 13px;
  color: var(--parchment);
}

/* ASSET DRAWER */
#asset-drawer {
  position: fixed;
  top: 66px;
  left: 10px;
  width: 330px;
  max-height: calc(100vh - 235px);
  z-index: 30;
  padding: 12px;
  display: flex;
  flex-direction: column;
  gap: 10px;
  overflow: hidden;
}

.drawer-head {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  gap: 8px;
}

.drawer-sub {
  font-size: 11px;
  color: var(--muted);
}

.drawer-controls {
  display: flex;
  flex-direction: column;
  gap: 8px;
  font-size: 12px;
}

.drawer-controls label {
  display: flex;
  flex-direction: column;
  gap: 4px;
  color: var(--muted);
}

.drawer-file-row {
  display: flex;
  gap: 6px;
  align-items: center;
}

#asset-file-input {
  display: none;
}

.sliders-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px;
}

.sliders-grid input[type="range"] {
  width: 100%;
  accent-color: var(--gold);
}

.slot-list {
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  gap: 4px;
  padding-right: 4px;
  border-top: 1px solid rgba(255, 255, 255, 0.08);
  padding-top: 8px;
}

.slot-row {
  display: flex;
  justify-content: space-between;
  align-items: center;
  font-size: 11.5px;
  padding: 4px 7px;
  border-radius: 4px;
  background: rgba(0, 0, 0, 0.25);
  cursor: pointer;
}

.slot-row:hover { background: rgba(212, 166, 68, 0.14); }
.slot-row .tag-proxy { color: var(--muted); font-size: 10px; }
.slot-row .tag-glb { color: #8be07e; font-weight: 700; font-size: 10px; }

/* START & END SCREENS */
#start-screen, #endgame-screen {
  position: fixed;
  inset: 0;
  z-index: 50;
  background: radial-gradient(circle at center, rgba(16, 14, 11, 0.35), rgba(10, 8, 6, 0.82));
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 16px;
}

.start-card {
  width: min(540px, 95vw);
  background: linear-gradient(180deg, rgba(31, 25, 19, 0.94), rgba(18, 14, 11, 0.96));
  border: 1px solid #7c6242;
  border-radius: 12px;
  padding: 0 28px 24px;
  text-align: center;
  box-shadow: 0 24px 70px rgba(0, 0, 0, 0.78);
  backdrop-filter: blur(10px);
}

.start-emblem img {
  width: 60px;
  height: 60px;
  margin: 14px 0 8px;
  filter: drop-shadow(0 6px 14px rgba(0, 0, 0, 0.6));
}

.start-card h1, .start-card h2 {
  font-size: 34px;
  letter-spacing: 0.1em;
  color: var(--gold-bright);
  text-shadow: 0 3px 16px rgba(0, 0, 0, 0.7);
}

.subtitle {
  font-size: 14px;
  color: var(--gold);
  letter-spacing: 0.14em;
  text-transform: uppercase;
  margin-bottom: 14px;
}

.start-desc {
  font-size: 13.5px;
  line-height: 1.55;
  color: var(--parchment);
  margin-bottom: 18px;
}

.start-options {
  display: flex;
  flex-direction: column;
  gap: 12px;
  margin-bottom: 20px;
}

.opt-group label {
  display: block;
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: 0.1em;
  color: var(--muted);
  margin-bottom: 6px;
}

.pill-row {
  display: flex;
  justify-content: center;
  gap: 8px;
  flex-wrap: wrap;
}

.pill {
  background: rgba(45, 36, 27, 0.9);
  border: 1px solid #5e4c39;
  color: var(--parchment);
  padding: 7px 14px;
  border-radius: 6px;
  font-size: 12.5px;
  font-weight: 600;
  cursor: pointer;
}

.pill.active {
  background: rgba(212, 166, 68, 0.24);
  border-color: var(--gold-bright);
  color: #fff6e0;
}

.start-cta-row {
  display: flex;
  justify-content: center;
  gap: 10px;
  margin-bottom: 16px;
}

.primary-cta {
  background: linear-gradient(180deg, #deb14e, #a57720);
  border: 1px solid #f7d983;
  color: #1a1309;
  font-weight: 800;
  letter-spacing: 0.08em;
  padding: 12px 28px;
  border-radius: 7px;
  font-size: 14px;
  cursor: pointer;
  box-shadow: 0 6px 20px rgba(212, 166, 68, 0.3);
}

.primary-cta:hover {
  filter: brightness(1.08);
}

.secondary-cta {
  background: rgba(45, 36, 27, 0.95);
  border: 1px solid var(--gold);
  color: var(--gold-bright);
  font-weight: 700;
  padding: 12px 18px;
  border-radius: 7px;
  font-size: 13px;
  cursor: pointer;
}

.start-footer {
  display: flex;
  justify-content: center;
  gap: 8px;
  font-size: 11px;
  color: var(--muted);
  flex-wrap: wrap;
}

.end-stats {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px;
  margin: 16px 0 20px;
  text-align: left;
}

.end-stat-box {
  background: rgba(0, 0, 0, 0.3);
  border: 1px solid rgba(212, 166, 68, 0.22);
  border-radius: 6px;
  padding: 8px 12px;
  font-size: 12px;
}

.end-stat-box strong {
  display: block;
  font-size: 18px;
  color: var(--gold-bright);
}

.touch-hint { display: none; }
.mini-btn { min-height: 28px; padding: 4px 10px; }

@media (max-width: 1120px) {
  .brand-sub, .res-label { display: none; }
}
@media (max-width: 960px) {
  #top-bar { flex-wrap: wrap; }
  #objectives-panel { top: 102px; width: 215px; }
  #build-hint { top: 102px; }
  #bottom-hud {
    grid-template-columns: 168px 1fr;
  }
  .commands-wrap {
    grid-column: span 2;
  }
}

/* Responsive Mobile & Touch HUD polish (320px–680px + iOS safe areas) */
@media (max-width: 768px), (pointer: coarse) {
  .desk-hint { display: none; }
  .touch-hint { display: block; }
}
@media (max-width: 680px) {
  html, body, #app, #game-canvas { height: 100dvh; overflow: hidden; }
  #top-bar {
    top: max(6px, env(safe-area-inset-top));
    left: 6px; right: 6px;
    flex-wrap: wrap; gap: 5px; padding: 6px 8px; height: auto;
  }
  .brand-chip { gap: 6px; }
  .brand-icon { width: 22px; height: 22px; }
  .brand-name { font-size: 11.5px; }
  .brand-sub, .res-label, #btn-assets, #tod-select { display: none; }
  .resources-strip { gap: 9px; padding: 4px 9px; font-size: 12px; max-width: 100%; }
  .res-item { gap: 4px; font-size: 12px; white-space: nowrap; }
  .top-actions { flex-wrap: nowrap; overflow-x: auto; scrollbar-width: none; gap: 5px; width: 100%; justify-content: flex-start; padding-bottom: 1px; }
  .top-actions::-webkit-scrollbar { display: none; }
  .hud-btn, .hud-select { min-height: 32px; padding: 5px 10px; font-size: 11px; flex: 0 0 auto; }
  #objectives-panel {
    top: calc(max(6px, env(safe-area-inset-top)) + 82px) !important;
    bottom: auto !important; right: 6px !important; left: auto !important;
    width: 188px !important; max-height: 114px !important;
    overflow-y: auto; scrollbar-width: none;
    padding: 6px 9px !important; font-size: 10.5px;
  }
  #objectives-panel::-webkit-scrollbar { display: none; }
  #objectives-panel #event-log { display: none; }
  #objectives-list li { font-size: 10.5px; line-height: 1.25; }
  .wave-timer { margin-top: 5px; padding: 3px 6px; font-size: 10px; }
  #bottom-hud {
    padding: 6px; gap: 6px; grid-template-columns: 1fr;
    bottom: max(6px, env(safe-area-inset-bottom)); left: 6px; right: 6px;
  }
  .commands-wrap { grid-column: auto; min-height: 0; padding: 0 8px 8px; }
  .cmd-btn { min-height: 44px; padding: 7px 8px; }
  .selection-wrap { min-height: 0; padding: 0 10px 7px; }
  .sel-empty { font-size: 11px; line-height: 1.35; padding-top: 4px; }
  .minimap-wrap { display: none; }
  #mobile-touch-bar {
    display: flex; flex-direction: column;
    top: calc(max(6px, env(safe-area-inset-top)) + 82px);
    left: 6px; bottom: auto; gap: 5px;
  }
  .touch-pill { min-height: 34px; padding: 6px 11px; font-size: 11px; text-align: left; }
}

/* Mobile battle controls: larger targets, sharp contrast over the scene. */
@media(max-width:680px){
  .hud-btn,.hud-select,.touch-pill{min-height:44px;font-size:12px}
  .hud-panel{border-color:rgba(222,239,235,.35);background:rgba(12,21,24,.94)}
  #objectives-panel{font-size:13px;line-height:1.28}
}
