*,
*::before,
*::after {
  box-sizing: border-box;
}

html,
body {
  margin: 0;
  height: 100%;
  overflow: hidden;
  touch-action: none; /* no browser pinch or double-tap zoom anywhere (the game zooms itself) */
  -webkit-text-size-adjust: 100%;
  background: #1d2330;
  color: #e8ecf3;
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
}

#game-container {
  position: fixed;
  inset: 0;
  touch-action: none; /* game handles pinch and drag itself */
  user-select: none;
  -webkit-user-select: none;
}

#game-container canvas {
  display: block;
  width: 100%;
  height: 100%;
}

/* Overlay for HTML labels rendered by CSS2DRenderer */
.label-layer {
  position: absolute;
  inset: 0;
  pointer-events: none;
  transition: opacity 0.3s ease;
}

/* Hidden during the match intro; fades in afterwards */
.label-layer.is-hidden {
  opacity: 0;
  visibility: hidden;
}

/* CSS2DRenderer sets the transform on this element; style only, no transform here */
.unit-label {
  --owner-color: #9a9a9a;
  display: flex;
  flex-direction: column;
  align-items: center;
  min-width: 2.2em;
  padding: 1px 6px 2px;
  border: 2px solid var(--owner-color);
  border-radius: 10px;
  background: rgba(15, 18, 26, 0.75);
  color: #ffffff;
  font-size: 13px;
  font-weight: 700;
  line-height: 1.2;
  font-variant-numeric: tabular-nums;
}

.unit-label__upgrade-bar {
  display: none;
  width: 100%;
  height: 3px;
  margin-top: 2px;
  border-radius: 2px;
  background: rgba(255, 255, 255, 0.2);
  overflow: hidden;
}

.unit-label.is-upgrading .unit-label__upgrade-bar {
  display: block;
}

.unit-label__upgrade-fill {
  width: 100%;
  height: 100%;
  background: var(--owner-color);
  transform: scaleX(0);
  transform-origin: left center;
}

/* Shrine (README 7.3): units it still needs, gold frame */
.unit-label--shrine {
  --owner-color: #f2c14e;
  box-shadow: 0 0 8px rgba(255, 200, 69, 0.55);
}

/* Light orb flying from a taken shrine to the HUD entry of the taker */
.buff-orb {
  position: fixed;
  left: 0;
  top: 0;
  z-index: 30;
  width: var(--orb-size);
  height: var(--orb-size);
  border-radius: 50%;
  background: radial-gradient(circle, #fffbe8 0%, #ffd66b 35%, rgba(255, 200, 69, 0) 70%);
  box-shadow: 0 0 14px 6px rgba(255, 200, 69, 0.55);
  pointer-events: none;
}

/* Building menu next to the roof of an own building (hold or right click). CSS2DRenderer sets the
   transform on .building-menu-anchor (centered on the anchor point); the menu then moves so that its
   inner bottom corner touches that point. */
.building-menu-anchor {
  pointer-events: none;
}

/* Side classes (set by BuildingMenu, towards the map center) pick the corner that touches the anchor;
   --shift-x / --shift-y keep the menu inside the visible area */
.building-menu {
  display: flex;
  flex-direction: column;
  gap: 6px;
  --side-x: 50%;
  --side-y: 50%;
  transform: translate(calc(var(--side-x) + var(--shift-x, 0px)), calc(var(--side-y) + var(--shift-y, 0px)));
}

.building-menu--left {
  --side-x: -50%;
  align-items: flex-end;
}

.building-menu--right {
  align-items: flex-start;
}

.building-menu--above {
  --side-y: -50%;
}

.building-menu__row {
  display: flex;
  gap: 6px;
}

/* The confirm button appears on the outer side, so the troops button stays in place */
.building-menu--left .building-menu__row {
  flex-direction: row-reverse;
}

.building-menu__button {
  display: flex;
  align-items: center;
  gap: 6px;
  min-height: 40px;
  padding: 6px 10px;
  border: 2px solid #f5c542;
  border-radius: 12px;
  background: rgba(15, 18, 26, 0.9);
  color: #ffffff;
  font: inherit;
  font-size: 14px;
  font-weight: 700;
  white-space: nowrap;
  cursor: pointer;
  pointer-events: auto; /* the label layer itself ignores pointer events */
  touch-action: manipulation;
}

.building-menu__button[hidden] {
  display: none;
}

.building-menu__button svg {
  flex: none;
  width: 22px;
  height: 22px;
}

.building-menu__upgrade svg {
  width: 26px; /* wide arrow pointing up */
}

.building-menu__badge {
  padding: 0 6px;
  border-radius: 8px;
  background: #f5c542;
  color: #1d2330;
  font-variant-numeric: tabular-nums;
}

.building-menu__value {
  min-width: 3.2em;
  text-align: right;
  font-variant-numeric: tabular-nums;
}

.building-menu__confirm {
  border-color: #22c55e;
  color: #22c55e;
}

.building-menu__button[aria-disabled="true"] {
  border-color: #6b7280;
  color: #9ca3af;
  cursor: default;
}

.building-menu__button[aria-disabled="true"] .building-menu__badge {
  background: #6b7280;
}

/* ---------- UI layer (HUD and overlay screens) above the game board ---------- */
.ui-root {
  position: fixed;
  inset: 0;
  z-index: 10;
  pointer-events: none; /* only interactive children receive input */
  user-select: none;
  -webkit-user-select: none;
}

/* Three groups along the top edge: local player | hide + time + pause | opponents */
.hud {
  position: absolute;
  top: max(8px, env(safe-area-inset-top));
  left: max(8px, env(safe-area-inset-left));
  right: max(8px, env(safe-area-inset-right));
  display: grid;
  grid-template-columns: 1fr auto 1fr;
  align-items: start;
  gap: 8px;
  font-size: 13px;
  font-weight: 700;
  font-variant-numeric: tabular-nums;
  white-space: nowrap;
  transition: opacity 0.3s ease;
}

.hud__group {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 6px;
  border-radius: 14px;
  background: rgba(15, 18, 26, 0.8);
}

.hud__group--local {
  justify-self: start;
}

.hud__group--center {
  padding-left: 10px;
}

.hud__group--opponents {
  justify-self: end;
}

.hud__group--opponents:empty {
  display: none;
}

.hud-player {
  --owner-color: #9a9a9a;
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 2px 8px;
  border-left: 4px solid var(--owner-color);
  border-radius: 8px;
  background: rgba(255, 255, 255, 0.06);
}

.hud-player__name {
  color: var(--owner-color);
}

.hud-player__stat {
  display: inline-flex;
  align-items: center;
  gap: 3px;
}

.hud-player__stat svg {
  width: 12px;
  height: 12px;
  fill: currentColor;
  opacity: 0.7;
}

/* Buff icons of a player (temple, shrine buffs): gold plaques, shown only while a buff runs or waits */
.hud-buffs {
  display: inline-flex;
  align-items: center;
  gap: 3px;
}

.hud-buffs:not(:has(.hud-buff:not([hidden]))) {
  display: none; /* no gap after the unit count without buffs */
}

.hud-buff {
  display: inline-flex;
  align-items: center;
  gap: 2px;
  min-width: 20px;
  height: 20px;
  padding: 0 3px;
  box-sizing: border-box;
  justify-content: center;
  border: 1.5px solid #f2c14e;
  border-radius: 10px;
  background: rgba(255, 200, 69, 0.18);
  color: #ffe08a;
  font-size: 11px;
}

.hud-buff[hidden] {
  display: none;
}

.hud-buff svg {
  width: 12px;
  height: 12px;
  fill: currentColor;
}

.hud-buff__value:empty {
  display: none;
}

.hud-buff--temple {
  border-color: #b9c6e0;
  background: rgba(185, 198, 224, 0.16);
  color: #dfe7f5;
}

.hud-buff.is-awaiting {
  visibility: hidden; /* keeps its place for the light orb */
}

.hud-buff.is-ending {
  animation: hud-buff-blink 0.6s ease-in-out infinite alternate;
}

@keyframes hud-buff-blink {
  to {
    opacity: 0.25;
  }
}

@media (prefers-reduced-motion: reduce) {
  .hud-buff.is-ending {
    animation: none;
    opacity: 0.5;
  }
}

.hud-player.is-eliminated {
  opacity: 0.4;
  text-decoration: line-through;
}

.hud__time {
  min-width: 3.2em;
  text-align: center;
  color: #c9d1e0;
}

.hud__button {
  display: grid;
  place-items: center;
  width: 32px;
  height: 32px;
  padding: 0;
  border: 0;
  border-radius: 10px;
  background: rgba(255, 255, 255, 0.12);
  color: #ffffff;
  cursor: pointer;
  pointer-events: auto;
  touch-action: manipulation;
}

.hud__button svg {
  width: 14px;
  height: 14px;
  fill: currentColor;
}

/* Match intro: the HUD keeps its layout (the camera inset stays) and fades in afterwards */
.hud.is-intro {
  opacity: 0;
  visibility: hidden;
}

/* Level name and skip hint at the bottom center during the match intro */
.intro-title {
  position: absolute;
  left: 50%;
  bottom: max(28px, calc(env(safe-area-inset-bottom) + 16px));
  transform: translateX(-50%);
  text-align: center;
  white-space: nowrap;
  opacity: 0;
  visibility: hidden;
  transition: opacity 0.4s ease, visibility 0s linear 0.4s;
}

.intro-title.is-visible {
  opacity: 1;
  visibility: visible;
  transition: opacity 0.4s ease;
}

.intro-title__name {
  font-size: clamp(22px, 5vw, 38px);
  font-weight: 800;
  letter-spacing: 0.04em;
  color: #ffffff;
  text-shadow: 0 2px 12px rgba(0, 0, 0, 0.75);
}

.intro-title__hint {
  margin-top: 4px;
  font-size: 13px;
  font-weight: 600;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  color: rgba(255, 255, 255, 0.65);
  text-shadow: 0 1px 6px rgba(0, 0, 0, 0.75);
}

/* Hidden HUD: the groups keep their layout space (visibility, not display), so the camera inset stays.
   Only the show button remains, at the place of the hide button's group in the top center. */
.hud.is-collapsed .hud__group {
  visibility: hidden;
}

.hud__show {
  display: none;
  position: absolute;
  top: 0;
  left: 50%;
  transform: translateX(-50%);
  background: rgba(15, 18, 26, 0.6);
  box-shadow: inset 0 0 0 1px rgba(255, 255, 255, 0.25);
  opacity: 0.8;
}

.hud.is-collapsed .hud__show {
  display: grid;
}

.overlay-screen {
  position: absolute;
  inset: 0;
  display: grid;
  place-items: center;
  padding: 16px;
  background: rgba(10, 12, 18, 0.65);
  pointer-events: auto; /* blocks input to the game below */
}

.overlay-screen[hidden] {
  display: none;
}

.overlay-screen__panel {
  --accent-color: #f5c542;
  width: min(360px, 100%);
  padding: 24px 20px 20px;
  border-top: 4px solid var(--accent-color);
  border-radius: 16px;
  background: #232a3a;
  box-shadow: 0 16px 40px rgba(0, 0, 0, 0.45);
  max-height: calc(100vh - 32px);
  max-height: calc(100dvh - 32px); /* mobile: excludes the browser bars */
  overflow-y: auto;
  touch-action: pan-y; /* long level lists stay scrollable */
  text-align: center;
}

.overlay-screen__panel[data-variant="victory"] {
  --accent-color: #22c55e;
}

.overlay-screen__panel[data-variant="defeat"] {
  --accent-color: #ef4444;
}

.overlay-screen__title {
  margin: 0 0 8px;
  color: var(--accent-color);
  font-size: 28px;
}

.overlay-screen__text {
  margin: 0 0 20px;
  color: #c9d1e0;
  font-size: 15px;
  line-height: 1.4;
}

.overlay-screen__buttons {
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.overlay-screen__button {
  padding: 12px 16px;
  border: 2px solid rgba(255, 255, 255, 0.2);
  border-radius: 12px;
  background: transparent;
  color: #ffffff;
  font: inherit;
  font-size: 16px;
  font-weight: 700;
  cursor: pointer;
  touch-action: manipulation;
}

.overlay-screen__button.is-primary {
  border-color: var(--accent-color);
  background: var(--accent-color);
  color: #1d2330;
}

.overlay-screen__button-label,
.overlay-screen__button-detail {
  display: block;
}

.overlay-screen__button-detail {
  margin-top: 2px;
  font-size: 12px;
  font-weight: 600;
  opacity: 0.75;
}

.overlay-screen__button:disabled {
  opacity: 0.45;
  cursor: not-allowed;
}

.overlay-screen__button:focus-visible,
.hud__button:focus-visible {
  outline: 3px solid #ffffff;
  outline-offset: 2px;
}

/* Phones (portrait and landscape): compact HUD, the color stripe identifies the player */
@media (max-width: 480px), (max-height: 480px) {
  .hud {
    gap: 6px;
    font-size: 12px;
  }

  .hud__group {
    gap: 6px;
    padding: 4px;
    border-radius: 12px;
  }

  .hud__group--center {
    padding-left: 8px;
  }

  .hud-player {
    gap: 4px;
    padding: 2px 6px;
  }

  .hud-player__name {
    display: none;
  }

  .hud-buff {
    min-width: 18px;
    height: 18px;
    font-size: 10px;
  }

  .hud-buff svg {
    width: 11px;
    height: 11px;
  }

  .hud__button {
    width: 28px;
    height: 28px;
    border-radius: 8px;
  }
}

/* Narrow portrait screens: several opponents stack on the right */
@media (max-width: 480px) {
  .hud__group--opponents {
    flex-direction: column;
    align-items: stretch;
    gap: 4px;
  }
}

/* Custom Maps: wider panel with one row per map */
.overlay-screen__panel.custom-maps {
  width: min(640px, 100%);
  text-align: left;
}

.custom-maps .overlay-screen__title {
  text-align: center;
}

.custom-maps__actions {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 10px;
  margin-bottom: 14px;
}

.custom-maps__list {
  display: grid;
  gap: 8px;
  margin: 0 0 14px;
  padding: 0;
  list-style: none;
}

.custom-maps .overlay-screen__button[data-action="back"] {
  width: 100%;
}

.custom-maps__row {
  display: grid;
  gap: 8px;
  padding: 10px 12px;
  border-radius: 12px;
  background: rgba(255, 255, 255, 0.05);
}

.custom-maps__info {
  display: grid;
  gap: 2px;
  min-width: 0;
}

.custom-maps__name {
  overflow: hidden;
  color: #ffffff;
  font-weight: 700;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.custom-maps__detail {
  color: #9aa4b8;
  font-size: 12px;
}

.custom-maps__detail.is-problem {
  color: #fca5a5;
}

.custom-maps__buttons {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
}

.custom-maps__button {
  padding: 6px 12px;
  border: 1.5px solid rgba(255, 255, 255, 0.2);
  border-radius: 8px;
  background: transparent;
  color: #ffffff;
  font: inherit;
  font-size: 13px;
  font-weight: 700;
  cursor: pointer;
  touch-action: manipulation;
}

.custom-maps__button.is-primary {
  border-color: #f5c542;
  background: #f5c542;
  color: #1d2330;
}

.custom-maps__button.is-danger {
  margin-left: auto;
  border-color: rgba(239, 68, 68, 0.6);
  color: #fca5a5;
}

.custom-maps__button:disabled {
  opacity: 0.45;
  cursor: not-allowed;
}

.custom-maps__button:focus-visible {
  outline: 3px solid #ffffff;
  outline-offset: 2px;
}

.custom-maps__note {
  padding: 12px;
  color: #9aa4b8;
  text-align: center;
}

/* ---------- Logo intro when the page opens (placeholder: the game name as text) ---------- */
.logo-intro {
  position: absolute;
  inset: 0;
  z-index: 50;
  display: grid;
  place-items: center;
  background: radial-gradient(ellipse at 50% 45%, #1b2233 0%, #0b0e14 70%);
  pointer-events: auto;
  cursor: pointer;
  user-select: none;
  -webkit-user-select: none;
  transition: opacity var(--logo-fade-out) ease-in;
}

.logo-intro.is-leaving {
  opacity: 0;
}

.logo-intro__logo {
  display: grid;
  justify-items: center;
  gap: 14px;
  padding: 0 16px;
  animation: logo-intro-in var(--logo-fade-in) ease-out both;
}

.logo-intro__title {
  font-size: clamp(36px, 9vw, 92px);
  font-weight: 800;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  text-align: center;
  line-height: 1.05;
  color: #f5c542;
  background: linear-gradient(100deg, #c9932c 0%, #f5c542 35%, #fff4c8 50%, #f5c542 65%, #c9932c 100%);
  background-size: 250% 100%;
  -webkit-background-clip: text;
  background-clip: text;
  -webkit-text-fill-color: transparent;
  filter: drop-shadow(0 4px 18px rgba(245, 197, 66, 0.35));
  animation: logo-intro-shimmer 2.4s ease-in-out 0.4s both;
}

.logo-intro__rule {
  width: min(60%, 360px);
  height: 2px;
  background: linear-gradient(90deg, transparent, #f5c542, transparent);
  animation: logo-intro-rule 1.2s ease-out 0.3s both;
}

.logo-intro__hint,
.logo-intro__skip {
  position: absolute;
  left: 0;
  right: 0;
  text-align: center;
  color: #8f9bb3;
  font-size: 13px;
  font-weight: 600;
}

.logo-intro__hint {
  bottom: max(56px, calc(env(safe-area-inset-bottom) + 48px));
  opacity: 0;
  transition: opacity 0.4s ease;
}

.logo-intro__hint.is-visible {
  opacity: 1;
}

.logo-intro__skip {
  bottom: max(20px, env(safe-area-inset-bottom));
  opacity: 0.6;
}

@keyframes logo-intro-in {
  from {
    opacity: 0;
    transform: scale(0.92);
  }
  to {
    opacity: 1;
    transform: scale(1);
  }
}

@keyframes logo-intro-shimmer {
  from {
    background-position: 100% 0;
  }
  to {
    background-position: 0 0;
  }
}

@keyframes logo-intro-rule {
  from {
    transform: scaleX(0);
    opacity: 0;
  }
  to {
    transform: scaleX(1);
    opacity: 1;
  }
}

@media (prefers-reduced-motion: reduce) {
  .logo-intro__logo,
  .logo-intro__rule {
    animation-name: logo-intro-fade;
  }

  .logo-intro__title {
    animation: none;
  }
}

@keyframes logo-intro-fade {
  from {
    opacity: 0;
  }
  to {
    opacity: 1;
  }
}

/* ---------- Rotate hint: covers everything while a touch device is held upright ---------- */
.rotate-hint {
  position: absolute;
  inset: 0;
  z-index: 100;
  display: none;
  place-content: center;
  justify-items: center;
  gap: 12px;
  padding: 24px;
  background: #1d2330;
  color: #e8ecf3;
  text-align: center;
  pointer-events: auto; /* blocks input to the game and menus below */
}

.rotate-hint svg {
  width: 72px;
  height: 72px;
  animation: rotate-hint-turn 2s ease-in-out infinite;
}

.rotate-hint__text {
  margin: 0;
  font-size: 18px;
  font-weight: 700;
}

@media (pointer: coarse) and (orientation: portrait) {
  .rotate-hint {
    display: grid;
  }
}

@keyframes rotate-hint-turn {
  0%, 30% { transform: rotate(0deg); }
  60%, 100% { transform: rotate(-90deg); }
}

@media (prefers-reduced-motion: reduce) {
  .rotate-hint svg {
    animation: none;
  }
}
