/* Structure: mobile-first single column, promoted to a two-column desktop grid.
   The only scrolling container is the panel body — the village and anvil are
   always on screen, because you must never have to scroll to see the siege. */

#app {
  display: grid;
  grid-template-rows: auto minmax(0, 1fr);
  /* dvh tracks mobile browser chrome as it hides; vh is the fallback. */
  height: 100vh;
  height: 100dvh;
  max-width: 1500px;
  margin: 0 auto;
}

#stage {
  display: grid;
  grid-template-rows: auto auto minmax(0, 1fr);
  min-height: 0;
  gap: 8px;
  padding: 8px;
  padding-bottom: calc(8px + env(safe-area-inset-bottom));
}

/* ==========================================================================
   The village scene
   ========================================================================== */

#village {
  position: relative;
  height: clamp(150px, 26vh, 210px);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  overflow: hidden;
  background: linear-gradient(180deg, var(--sky-1) 0%, var(--sky-2) 62%, var(--sky-3) 100%);
  transition: background 1200ms ease;
  isolation: isolate;
}

/* Distant hills */
#village::before {
  content: '';
  position: absolute;
  inset: auto 0 22% 0;
  height: 42%;
  background: color-mix(in srgb, var(--ground) 70%, #000 30%);
  clip-path: polygon(0 68%, 9% 44%, 18% 60%, 28% 32%, 39% 55%, 50% 38%, 61% 58%, 72% 36%, 84% 56%, 93% 42%, 100% 62%, 100% 100%, 0 100%);
  opacity: 0.85;
}

/* Rolling fog, driven by the current Act */
#fog {
  position: absolute;
  inset: 0;
  pointer-events: none;
  background: linear-gradient(180deg, transparent 30%, rgb(20 20 26 / 0.85) 100%);
  opacity: var(--fog);
  transition: opacity 1200ms ease;
  z-index: 4;
}

/* --- The horde ---------------------------------------------------------- */

#horde {
  position: absolute;
  left: 0;
  right: 0;
  bottom: 30%;
  display: flex;
  align-items: flex-end;
  justify-content: center;
  flex-wrap: wrap-reverse;
  gap: 1px 3px;
  padding: 0 6px;
  z-index: 3;
  /* --march runs 0 -> 1 as the wave timer empties. They visibly close in. */
  transform: translateY(calc(var(--march, 0) * 16px)) scale(calc(1 + var(--march, 0) * 0.3));
  transform-origin: bottom center;
  transition: transform 400ms linear;
  pointer-events: none;
}

.foe {
  font-size: 15px;
  line-height: 1;
  filter: drop-shadow(0 1px 1px rgb(0 0 0 / 0.6));
  animation: foe-bob 1.9s ease-in-out infinite;
}

.foe.boss {
  font-size: 30px;
  filter: drop-shadow(0 0 8px var(--danger));
  animation-duration: 2.6s;
}

@keyframes foe-bob {
  0%, 100% { transform: translateY(0); }
  50% { transform: translateY(-3px); }
}

/* --- Walls and ground -------------------------------------------------- */

#wall {
  position: absolute;
  left: 0;
  right: 0;
  bottom: 22%;
  height: 15px;
  z-index: 5;
  background: color-mix(in srgb, var(--ground) 45%, #1a1a1e 55%);
  border-top: 2px solid color-mix(in srgb, var(--ground) 70%, #d8d0c0 30%);
}

/* Crenellations */
#wall::before {
  content: '';
  position: absolute;
  left: 0;
  right: 0;
  top: -7px;
  height: 7px;
  background: repeating-linear-gradient(
    90deg,
    color-mix(in srgb, var(--ground) 45%, #1a1a1e 55%) 0 13px,
    transparent 13px 21px
  );
}

/* Damage cracks scale with --damage (0..1) set from render.js */
#wall::after {
  content: '';
  position: absolute;
  inset: 0;
  opacity: var(--damage, 0);
  background: repeating-linear-gradient(72deg, transparent 0 9px, rgb(0 0 0 / 0.75) 9px 11px);
  transition: opacity 500ms;
}

#ground {
  position: absolute;
  inset: auto 0 0 0;
  height: 22%;
  z-index: 5;
  background: linear-gradient(180deg, var(--ground), color-mix(in srgb, var(--ground) 40%, #000 60%));
  transition: background 1200ms ease;
}

/* --- The forge itself -------------------------------------------------- */

#forge-glow {
  position: absolute;
  left: 50%;
  bottom: 4%;
  width: 130px;
  height: 66px;
  transform: translateX(-50%);
  z-index: 6;
  background: radial-gradient(ellipse at 50% 100%, var(--ember) 0%, transparent 70%);
  opacity: 0.55;
  pointer-events: none;
  animation: forge-breathe 3.4s ease-in-out infinite;
}

@keyframes forge-breathe {
  0%, 100% { opacity: 0.42; }
  50% { opacity: 0.72; }
}

#act-caption {
  position: absolute;
  left: 10px;
  right: 10px;
  bottom: 6px;
  z-index: 7;
  font-size: 11px;
  font-style: italic;
  text-align: center;
  color: rgb(255 255 255 / 0.72);
  text-shadow: 0 1px 3px rgb(0 0 0 / 0.9);
  pointer-events: none;
}

/* --- Siege HUD, floated over the scene --------------------------------- */

#siege-hud {
  position: absolute;
  inset: 6px 6px auto 6px;
  z-index: 8;
  padding: 8px 10px;
  background: rgb(10 11 15 / 0.72);
  backdrop-filter: blur(3px);
  border: 1px solid rgb(255 255 255 / 0.09);
  border-radius: var(--radius-sm);
}

#wave-top {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-bottom: 5px;
}

#wave-name {
  flex: 1;
  min-width: 0;
  font-size: 13px;
  font-weight: 700;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

#wave-num {
  font-size: 10px;
  letter-spacing: 0.09em;
  text-transform: uppercase;
  color: var(--text-faint);
  flex: none;
}

#wave-clock {
  font-family: var(--font-num);
  font-size: 15px;
  font-weight: 700;
  font-variant-numeric: tabular-nums;
  flex: none;
}

#wave-clock.urgent {
  color: var(--danger);
  animation: pulse-urgent 700ms ease-in-out infinite;
}

@keyframes pulse-urgent {
  0%, 100% { opacity: 1; }
  50% { opacity: 0.45; }
}

#wave-bar {
  position: relative;
  height: 5px;
  background: rgb(0 0 0 / 0.55);
  border-radius: 99px;
  overflow: hidden;
}

#wave-bar-fill {
  height: 100%;
  background: linear-gradient(90deg, var(--warn), var(--danger));
  transition: width 200ms linear;
}

/* Power vs Threat readout */
#matchup {
  display: flex;
  align-items: center;
  gap: 7px;
  margin-top: 6px;
  font-size: 12px;
  font-variant-numeric: tabular-nums;
}

#matchup-power { color: var(--power); font-weight: 700; }
#matchup-threat { color: var(--danger); font-weight: 700; }

/* When this wave's traits are cutting your Power, the readout becomes
   "18.2K -> 10.7K". Colour the whole thing as a warning so it is obvious the
   wave is doing it rather than your army having changed. */
#matchup-power.penalised { color: var(--warn); }
#matchup-verdict {
  margin-left: auto;
  font-size: 11px;
  font-weight: 700;
  padding: 1px 7px;
  border-radius: 99px;
}

#matchup-verdict.win { color: #cfe8b8; background: #1e2a16; border: 1px solid #3f5a2c; }
#matchup-verdict.close { color: #f2d79a; background: #2a2210; border: 1px solid #5f4a18; }
#matchup-verdict.lose { color: #f2a79c; background: #2a1310; border: 1px solid #6b2b24; }

#wave-traits {
  display: flex;
  flex-wrap: wrap;
  gap: 4px;
  margin-top: 6px;
}

#wave-traits:empty {
  display: none;
}

#village-hp-wrap {
  position: absolute;
  left: 6px;
  right: 6px;
  bottom: 6px;
  z-index: 8;
  display: none; /* shown by render.js once the village has taken a hit */
  align-items: center;
  gap: 7px;
}

#village-hp-wrap.visible {
  display: flex;
}

#village-hp-wrap .meter {
  flex: 1;
}

#village-hp-label {
  font-size: 10px;
  font-weight: 700;
  color: var(--text-dim);
  font-variant-numeric: tabular-nums;
  white-space: nowrap;
}

/* ==========================================================================
   Anvil
   ========================================================================== */

#anvil-area {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 4px 2px;
}

#anvil {
  position: relative;
  flex: none;
  width: clamp(88px, 22vw, 116px);
  height: clamp(88px, 22vw, 116px);
  border-radius: 50%;
  background:
    radial-gradient(circle at 50% 32%, #4a4f5c 0%, #2a2e38 55%, #1a1d24 100%);
  border: 2px solid #4c5361;
  box-shadow:
    0 0 0 5px rgb(255 122 47 / 0.09),
    0 6px 20px rgb(0 0 0 / 0.55),
    inset 0 2px 6px rgb(255 255 255 / 0.07);
  font-size: clamp(38px, 10vw, 50px);
  line-height: 1;
  display: grid;
  place-items: center;
  transition: transform 70ms ease-out, box-shadow 140ms;
}

#anvil:hover {
  box-shadow:
    0 0 0 7px rgb(255 122 47 / 0.14),
    0 6px 24px rgb(0 0 0 / 0.6),
    inset 0 2px 6px rgb(255 255 255 / 0.07);
}

#anvil.struck {
  transform: scale(0.9);
}

#anvil-info {
  min-width: 0;
  flex: 1;
}

#anvil-hint {
  font-size: 12px;
  color: var(--text-faint);
  margin-top: 2px;
}

/* An idle bank is the one thing here worth interrupting for — see idleBankHint
   in ui/render.js. Pulsed rather than merely coloured because a player watching
   the wave counter is not looking at this corner of the screen. */
#anvil-hint.urgent-hint {
  color: var(--gold);
  font-weight: 600;
  animation: hint-pulse 2s ease-in-out infinite;
}

@keyframes hint-pulse {
  0%, 100% { opacity: 0.72; }
  50% { opacity: 1; }
}

@media (prefers-reduced-motion: reduce) {
  #anvil-hint.urgent-hint { animation: none; opacity: 1; }
}

#tap-value {
  font-size: clamp(17px, 4.4vw, 21px);
  font-weight: 800;
  color: var(--ember-soft);
  font-variant-numeric: tabular-nums;
}

#early-call {
  flex: none;
}

/* ==========================================================================
   Panels
   ========================================================================== */

#panels {
  display: grid;
  grid-template-rows: auto minmax(0, 1fr);
  min-height: 0;
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  overflow: hidden;
}

#tabs {
  display: flex;
  background: #12141a;
  border-bottom: 1px solid var(--line);
  overflow-x: auto;
  scrollbar-width: none;
}

#tabs::-webkit-scrollbar {
  display: none;
}

.tab-btn {
  flex: 1 1 auto;
  min-width: 66px;
  min-height: 46px;
  padding: 6px 8px 5px;
  display: grid;
  justify-items: center;
  gap: 1px;
  font-size: 10px;
  font-weight: 700;
  letter-spacing: 0.04em;
  text-transform: uppercase;
  color: var(--text-faint);
  border-bottom: 2px solid transparent;
  transition: color 120ms, border-color 120ms, background 120ms;
  white-space: nowrap;
}

.tab-btn .tab-icon {
  font-size: 17px;
  line-height: 1;
}

.tab-btn:hover {
  color: var(--text-dim);
  background: rgb(255 255 255 / 0.03);
}

.tab-btn.active {
  color: var(--ember-soft);
  border-bottom-color: var(--ember);
  background: rgb(255 122 47 / 0.07);
}

/* A dot on tabs holding something newly affordable — the "go look" nudge. */
.tab-btn .tab-dot {
  position: absolute;
  margin: -2px 0 0 26px;
  width: 6px;
  height: 6px;
  border-radius: 50%;
  background: var(--ember);
  opacity: 0;
  transition: opacity 200ms;
}

.tab-btn.has-new .tab-dot {
  opacity: 1;
}

#panel-body {
  min-height: 0;
  overflow-y: auto;
}

.tab-pane {
  display: none;
}

.tab-pane.active {
  display: block;
}

/* Bulk-buy mode selector */
#buy-modes {
  display: flex;
  gap: 4px;
}

.mode-btn {
  min-width: 38px;
  min-height: 28px;
  padding: 2px 8px;
  font-size: 12px;
  font-weight: 700;
  color: var(--text-faint);
  background: var(--panel-2);
  border: 1px solid var(--line);
  border-radius: var(--radius-sm);
}

.mode-btn.active {
  color: var(--ember-soft);
  border-color: var(--ember);
  background: rgb(255 122 47 / 0.1);
}

/* Army composition bars */
.comp {
  display: grid;
  gap: 6px;
}

.comp-row {
  display: grid;
  grid-template-columns: 74px 1fr 52px;
  gap: 8px;
  align-items: center;
  font-size: 12px;
}

.comp-bar {
  height: 7px;
  background: #0d0e12;
  border-radius: 99px;
  overflow: hidden;
}

.comp-fill {
  height: 100%;
  border-radius: 99px;
  transition: width 300ms;
}

.comp-val {
  text-align: right;
  font-variant-numeric: tabular-nums;
  color: var(--text-dim);
}

/* Achievement + relic grids */
.grid-icons {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(52px, 1fr));
  gap: 6px;
}

.icon-cell {
  position: relative;
  aspect-ratio: 1;
  display: grid;
  place-items: center;
  font-size: 22px;
  background: var(--panel-2);
  border: 1px solid var(--line-soft);
  border-radius: var(--radius-sm);
  transition: transform 100ms, border-color 120ms;
}

.icon-cell.locked {
  opacity: 0.22;
  filter: grayscale(1);
}

.icon-cell:hover:not(.locked) {
  transform: translateY(-2px);
  border-color: var(--ember);
}

.icon-cell .stacks {
  position: absolute;
  right: 2px;
  bottom: 1px;
  font-size: 10px;
  font-weight: 800;
  color: var(--gold);
  text-shadow: 0 1px 2px #000;
  font-variant-numeric: tabular-nums;
}

/* Battle log */
.log-row {
  display: grid;
  grid-template-columns: 22px 1fr auto;
  gap: 8px;
  align-items: center;
  padding: 5px 7px;
  font-size: 12px;
  border-radius: var(--radius-sm);
  background: var(--panel);
  border-left: 2px solid var(--line);
}

.log-row + .log-row {
  margin-top: 4px;
}

.log-row.win { border-left-color: var(--good); }
.log-row.loss { border-left-color: var(--danger); }
.log-row .log-meta {
  color: var(--text-faint);
  font-size: 11px;
  font-variant-numeric: tabular-nums;
  white-space: nowrap;
}

/* Save / settings */
.field {
  display: grid;
  gap: 5px;
  margin-bottom: 10px;
}

.field label {
  font-size: 11px;
  font-weight: 700;
  letter-spacing: 0.06em;
  text-transform: uppercase;
  color: var(--text-faint);
}

.field textarea {
  width: 100%;
  min-height: 74px;
  padding: 8px;
  font-family: var(--font-num);
  font-size: 11px;
  line-height: 1.4;
  background: #0c0d11;
  border: 1px solid var(--line);
  border-radius: var(--radius-sm);
  resize: vertical;
  word-break: break-all;
}

.toggle-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  padding: 8px 0;
  border-bottom: 1px solid var(--line-soft);
}

.toggle-row:last-child {
  border-bottom: 0;
}

.switch {
  flex: none;
  width: 44px;
  height: 25px;
  padding: 3px;
  border-radius: 99px;
  background: var(--panel-3);
  border: 1px solid var(--line);
  display: flex;
  transition: background 160ms;
}

.switch::after {
  content: '';
  width: 17px;
  height: 17px;
  border-radius: 50%;
  background: var(--text-faint);
  transition: transform 160ms, background 160ms;
}

.switch[aria-checked='true'] {
  background: rgb(255 122 47 / 0.28);
  border-color: var(--ember);
}

.switch[aria-checked='true']::after {
  transform: translateX(19px);
  background: var(--ember);
}

.privacy-link {
  display: inline-block;
  color: var(--power);
  font-size: 12px;
  text-decoration: underline;
  text-underline-offset: 2px;
}

.privacy-link:hover {
  color: var(--text);
}

/* ==========================================================================
   Desktop: village + anvil on the left, panels a fixed rail on the right
   ========================================================================== */

@media (min-width: 900px) {
  #stage {
    grid-template-columns: minmax(0, 1fr) 390px;
    /* The village takes the slack, not the anvil. With `auto 1fr` the anvil row
       grew to fill the column and left a few hundred pixels of dead space under
       a 130px button; giving the scene the free height makes it cinematic on a
       widescreen instead. */
    grid-template-rows: minmax(0, 1fr) auto;
    grid-template-areas:
      'village panels'
      'anvil   panels';
    gap: 10px;
    padding: 10px;
  }

  #village {
    grid-area: village;
    height: auto;
    min-height: 240px;
  }

  #anvil-area {
    grid-area: anvil;
    align-items: center;
    padding-top: 4px;
  }

  #panels {
    grid-area: panels;
  }

  #anvil {
    width: 132px;
    height: 132px;
    font-size: 56px;
  }
}

@media (min-width: 1250px) {
  #stage {
    grid-template-columns: minmax(0, 1fr) 430px;
  }
}

/* Landscape phones: the village would eat the whole viewport otherwise. */
@media (max-height: 480px) and (orientation: landscape) {
  #village {
    height: 42vh;
  }

  #anvil {
    width: 72px;
    height: 72px;
    font-size: 32px;
  }

  #act-caption {
    display: none;
  }
}
