:root {
  --widget-w: 1920px;
  --widget-h: 1080px;
  --widget-scale: 1;
  --ground-h: 36px; /* 3 × 12px (hauteur d'une grande ligne) */
  --ground-color: #6cde5d;
  --cell-fill: rgba(255, 255, 255, 0.08);
  --cross-fill: rgba(255, 230, 80, 0.85);
  --cross-stroke: rgba(40, 30, 0, 0.55);
  --grid-line: rgba(255, 255, 255, 0.55);
  --night-filter:
    brightness(0.68)
    contrast(1.28)
    saturate(0.88)
    sepia(0.16)
    hue-rotate(-33deg);
  --night-ground-filter:
    brightness(0.64)
    contrast(1.3)
    saturate(0.85)
    sepia(0.18)
    hue-rotate(-33deg);
}

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

html,
body {
  width: 100%;
  height: 100%;
  font-family: "Segoe UI", system-ui, sans-serif;
  background: #ffffff;
  overflow: hidden;
}

/* Mode intégré : document = exactement 1920×1080 (scale géré par l'app) */
html.embed,
html.embed body {
  width: var(--widget-w);
  height: var(--widget-h);
  background: transparent;
}

html.embed .widget-stage {
  width: var(--widget-w);
  height: var(--widget-h);
  border: none;
  border-radius: 0;
  box-shadow: none;
  background: #ffffff;
}

html.embed .widget {
  transform: none;
  border-radius: 0;
}

/* Stage standalone : cadre 16:9 qui scale le widget interne */
.widget-stage {
  position: absolute;
  inset: 0;
  margin: auto;
  width: min(100vw, calc(100vh * 16 / 9));
  height: min(100vh, calc(100vw * 9 / 16));
  overflow: hidden;
  background: #ffffff;
}

/* Widget interne : toujours 1920×1080, réduit via scale */
.widget {
  position: absolute;
  top: 0;
  left: 0;
  width: var(--widget-w);
  height: var(--widget-h);
  flex-shrink: 0;
  background: #ffffff;
  overflow: hidden;
  transform-origin: top left;
  transform: scale(var(--widget-scale));
}

.decor {
  position: absolute;
  inset: 0;
  pointer-events: none;
  z-index: 4;
  overflow: visible;
}

.decor .house,
.decor .plant,
.decor .camp {
  position: absolute;
  display: block;
  image-rendering: pixelated;
  /* z-index posé en JS selon la ligne (profondeur) */
  z-index: 1;
}

.decor .camp {
  pointer-events: auto;
  cursor: pointer;
}

/* Troupes : spritesheets (pas de <video>), derrière la clôture (z-index JS) */
.decor .troop {
  position: absolute;
  display: block;
  width: 72px;
  height: 72px;
  image-rendering: auto;
  pointer-events: none;
  background-color: transparent;
  background-repeat: no-repeat;
  transform-origin: 50% 100%;
}

.decor .troop.troop-wolf {
  width: 44px;
  height: 44px;
}

.decor .troop.troop-enemy {
  filter: saturate(1.15) contrast(1.05);
}

.decor .house,
.decor .camp {
  --burn-darken: 0;
}

.decor .house.building-burning,
.decor .camp.building-burning {
  filter: brightness(calc(1 - var(--burn-darken) * 0.78));
}

/* Feu devant le bâtiment (spritesheet RGBA, ancré aux pieds) */
.decor .building-fire {
  position: absolute;
  display: block;
  pointer-events: none;
  background-color: transparent;
  background-repeat: no-repeat;
  image-rendering: auto;
  mix-blend-mode: screen;
  will-change: background-position, opacity;
}

/* Alert Twitch : webm VP9+alpha natifs (glow derrière + fall devant) */
.decor .building-alert-glow,
.decor .building-alert-fall {
  position: absolute;
  display: block;
  pointer-events: none;
  object-fit: contain;
  background: transparent;
  will-change: opacity, filter;
}

.decor .building-alert-glow {
  mix-blend-mode: screen;
  opacity: 0.95;
}

.decor .building-alert-fall {
  mix-blend-mode: normal;
}

.decor .camp.camp-selected {
  filter: drop-shadow(0 0 8px rgba(251, 146, 60, 0.85));
}

.decor .camp.camp-selected.building-burning {
  filter:
    brightness(calc(1 - var(--burn-darken) * 0.78))
    drop-shadow(0 0 8px rgba(251, 146, 60, 0.85));
}

.widget.night-mode .decor img.camp.camp-selected {
  filter:
    brightness(0.68)
    contrast(1.28)
    saturate(0.88)
    sepia(0.16)
    hue-rotate(-33deg)
    drop-shadow(0 0 8px rgba(251, 146, 60, 0.85));
}

.widget.night-mode .decor .house.building-burning,
.widget.night-mode .decor .camp.building-burning {
  filter:
    var(--night-filter)
    brightness(calc(1 - var(--burn-darken) * 0.78));
}

.widget.night-mode .decor img.camp.camp-selected.building-burning {
  filter:
    var(--night-filter)
    brightness(calc(1 - var(--burn-darken) * 0.78))
    drop-shadow(0 0 8px rgba(251, 146, 60, 0.85));
}

.decor .plant.plant-growing {
  will-change: transform;
}

/* Coupe d'arbre : pivot sur les racines (bas), chute + fade */
.decor .plant.plant-chopping {
  transform-origin: 50% 100%;
  animation: tree-chop 0.85s cubic-bezier(0.33, 0.1, 0.55, 1) forwards;
  will-change: transform, opacity;
  pointer-events: none;
}

@keyframes tree-chop {
  0% {
    transform: translate(-50%, -100%) rotate(0deg) scale(1, 1);
    opacity: 1;
  }
  10% {
    /* Petit « pop » d'impact, racines plantées */
    transform: translate(-50%, -100%) rotate(calc(var(--chop-dir, 1) * -4deg))
      scale(1.08, 0.92);
  }
  28% {
    transform: translate(-50%, -100%) rotate(calc(var(--chop-dir, 1) * 12deg))
      scale(1.02, 0.98);
  }
  55% {
    transform: translate(-50%, -100%) rotate(calc(var(--chop-dir, 1) * 58deg))
      scale(0.95, 0.95);
    opacity: 1;
  }
  100% {
    transform: translate(-50%, -100%) rotate(calc(var(--chop-dir, 1) * 92deg))
      scale(0.45, 0.45);
    opacity: 0;
  }
}

.decor .decor-preview {
  opacity: 0.85;
  filter: drop-shadow(0 0 6px rgba(255, 230, 80, 0.7));
  /* Au-dessus du décor de la ligne, sous le personnage */
  z-index: 99;
}

/* Villageois : spritesheets RGBA, pieds ancrés sur la case */
.decor .villager {
  position: absolute;
  display: block;
  width: 60px;
  height: 60px;
  image-rendering: auto;
  pointer-events: none;
  background-color: transparent;
  background-repeat: no-repeat;
  transform-origin: 50% 100%;
}

.ground {
  position: absolute;
  left: 0;
  right: 0;
  bottom: 0;
  height: var(--ground-h);
  background: transparent;
  z-index: 3;
  overflow: visible;
}

/* Couleur du sol isolée : les filtres nuit n'affectent pas la grille éditeur */
.ground::before {
  content: "";
  position: absolute;
  inset: 0;
  z-index: 0;
  background: var(--ground-color);
  pointer-events: none;
  transition: filter 0.35s ease;
}

/* Trait noir en haut du sol (séparation ciel / sol) */
.ground::after {
  content: "";
  position: absolute;
  left: 0;
  right: 0;
  top: 0;
  height: 1px;
  background: #000;
  z-index: 1;
  pointer-events: none;
}

/* Grille virtuelle : invisible par défaut */
.ground .cell,
.ground .grid-line,
.ground .cross-cell {
  position: absolute;
  pointer-events: none;
  opacity: 0;
  transition: opacity 0.15s ease;
}

.widget.grid-visible .ground .cell,
.widget.grid-visible .ground .grid-line,
.widget.grid-visible .ground .cross-cell {
  opacity: 1;
}

.widget.grid-visible .ground .cell,
.widget.grid-visible .ground .cross-cell {
  pointer-events: auto;
  cursor: pointer;
}

.ground .cell {
  background: var(--cell-fill);
  z-index: 3;
}

/* Ligne du milieu décalée : contour plus lisible dans l'éditeur */
.ground .cell.cell-row-mid {
  box-shadow: inset 0 0 0 1px rgba(255, 255, 255, 0.45);
}

.ground .cell:hover {
  background: rgba(255, 255, 255, 0.18);
}

.ground .cell.cell-selected,
.ground .cross-cell.cell-selected {
  outline: 2px solid rgba(255, 255, 255, 0.95);
  outline-offset: -2px;
}

.ground .cell.cell-selected {
  background: rgba(255, 230, 80, 0.28);
}

.ground .grid-line {
  background: var(--grid-line);
  z-index: 1;
}

.ground .grid-line-h {
  height: 1px;
}

.ground .grid-line-v {
  width: 1px;
}

.ground .cross-cell {
  border: 1px solid var(--cross-stroke);
  background: var(--cross-fill);
  transform: translate(-50%, -50%);
  /* Au-dessus des grandes cases pour rester cliquables */
  z-index: 5;
}

.ground .cross-cell:hover {
  filter: brightness(1.15);
}

.ground .cross-cell.cell-selected {
  background: rgba(255, 255, 255, 0.95);
}

/* Sélecteur ancré à la case : offset fixe (pas selon l'image), derrière le décor */
.house-picker {
  position: absolute;
  z-index: 2;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 8px;
  transform: translate(-50%, -100%);
  min-width: 200px;
  padding: 12px 14px;
  background: #f6f8fa;
  border: 1px solid #d0d7de;
  border-radius: 14px;
  box-shadow: 0 4px 16px rgba(0, 0, 0, 0.06);
  pointer-events: auto;
  white-space: nowrap;
}

.house-picker[hidden] {
  display: none;
}

.picker-title {
  margin: 0;
  color: #1f2328;
  font-size: 16px;
  font-weight: 700;
  letter-spacing: 0.02em;
}

.picker-row {
  display: flex;
  align-items: center;
  gap: 14px;
}

.picker-hint {
  margin: 0;
  max-width: 280px;
  color: #cf222e;
  font-size: 13px;
  text-align: center;
  white-space: normal;
}

.picker-hint[hidden] {
  display: none;
}

.picker-ok:disabled {
  opacity: 0.45;
  cursor: not-allowed;
  transform: none;
}

.picker-facing {
  display: flex;
  align-items: center;
  gap: 10px;
}

.picker-facing[hidden] {
  display: none;
}

.facing-label {
  color: #656d76;
  font-size: 12px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.04em;
}

.facing-btn {
  width: 40px;
  height: 40px;
  border: 2px solid #d0d7de;
  border-radius: 12px;
  background: #ffffff;
  color: #1f2328;
  font-size: 18px;
  line-height: 1;
  cursor: pointer;
  transition: background 0.15s ease, border-color 0.15s ease, transform 0.12s ease;
}

.facing-btn:hover {
  background: #eef1f4;
  border-color: #afb8c1;
  transform: translateY(-1px);
}

.facing-btn.is-active {
  background: #0969da;
  border-color: #0969da;
  color: #ffffff;
}

.picker-arrow {
  width: 48px;
  height: 48px;
  border: 2px solid #d0d7de;
  border-radius: 12px;
  background: #ffffff;
  color: #1f2328;
  font-size: 28px;
  line-height: 1;
  cursor: pointer;
  transition: background 0.15s ease, border-color 0.15s ease, transform 0.12s ease;
}

.picker-arrow:hover {
  background: #eef1f4;
  border-color: #afb8c1;
  transform: translateY(-1px);
}

.picker-ok {
  min-width: 88px;
  height: 48px;
  padding: 0 24px;
  border: 2px solid #1a7f37;
  border-radius: 12px;
  background: #1a7f37;
  color: #ffffff;
  font-size: 16px;
  font-weight: 700;
  letter-spacing: 0.02em;
  cursor: pointer;
  transition: background 0.15s ease, border-color 0.15s ease, transform 0.12s ease;
}

.picker-ok:hover:not(:disabled) {
  background: #2da44e;
  border-color: #2da44e;
  transform: translateY(-1px);
}

/* Bulle récap ressources au-dessus d'une maison */
.resource-bubble {
  position: absolute;
  z-index: 14;
  min-width: 148px;
  padding: 10px 14px 12px;
  background: rgba(13, 17, 23, 0.94);
  border: 1px solid #3d4450;
  border-radius: 10px;
  color: #e6edf3;
  font-size: 13px;
  line-height: 1.35;
  pointer-events: none;
  transform: translate(-50%, calc(-100% - 10px));
  box-shadow: 0 6px 18px rgba(0, 0, 0, 0.35);
}

.resource-bubble[hidden] {
  display: none;
}

.resource-bubble::after {
  content: "";
  position: absolute;
  left: 50%;
  bottom: -8px;
  width: 14px;
  height: 14px;
  background: rgba(13, 17, 23, 0.94);
  border-right: 1px solid #3d4450;
  border-bottom: 1px solid #3d4450;
  transform: translateX(-50%) rotate(45deg);
}

.resource-bubble-line {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 18px;
  margin: 0;
}

.resource-bubble-line + .resource-bubble-line {
  margin-top: 4px;
}

.resource-bubble-label {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  color: #8b949e;
}

.resource-bubble-icon {
  width: 16px;
  height: 16px;
  object-fit: contain;
  flex-shrink: 0;
  image-rendering: auto;
}

.resource-bubble-line strong {
  font-variant-numeric: tabular-nums;
  color: #f0f3f6;
}

/* Mode nuit : décors, sprites et sol */
.widget.night-mode .decor :is(img, .villager, .troop) {
  filter: var(--night-filter);
  transition: filter 0.35s ease;
}

.widget.night-mode .decor .troop.troop-enemy {
  filter: var(--night-filter) saturate(1.15) contrast(1.05);
}

.widget.night-mode .decor img.decor-preview {
  filter: var(--night-filter) drop-shadow(0 0 6px rgba(255, 230, 80, 0.7));
}

.widget.night-mode .ground::before {
  filter: var(--night-ground-filter);
}

.widget:not(.night-mode) .decor :is(img, .villager, .troop) {
  transition: filter 0.35s ease;
}

/* ——— Intro : pan du sol + hop des décors ——— */
.widget.intro-pending .ground,
.widget.intro-playing .ground {
  will-change: transform;
}

.widget.intro-pending .ground {
  transform: translateY(calc(var(--widget-h) * 0.22));
}

.widget.intro-playing .ground {
  animation: ground-pan-in 0.42s cubic-bezier(0.22, 1, 0.36, 1) forwards;
}

@keyframes ground-pan-in {
  from { transform: translateY(calc(var(--widget-h) * 0.22)); }
  to { transform: translateY(0); }
}

.widget.intro-pending .decor :is(.house, .plant, .camp, .villager, .troop),
.decor :is(.house, .plant, .camp, .villager, .troop).intro-wait {
  translate: 0 110%;
  opacity: 0;
  pointer-events: none;
  will-change: translate, opacity;
}

.decor :is(.house, .plant, .camp, .villager, .troop).intro-hop {
  animation: decor-hop-in 0.38s cubic-bezier(0.34, 1.45, 0.64, 1) forwards;
  will-change: translate, opacity;
  pointer-events: none;
}

@keyframes decor-hop-in {
  0% { translate: 0 110%; opacity: 0; }
  58% { translate: 0 -16%; opacity: 1; }
  78% { translate: 0 5%; opacity: 1; }
  100% { translate: 0 0; opacity: 1; }
}

@media (prefers-reduced-motion: reduce) {
  .widget.intro-pending .ground,
  .widget.intro-playing .ground {
    transform: none;
    animation: none;
  }

  .widget.intro-pending .decor :is(.house, .plant, .camp, .villager, .troop),
  .decor :is(.house, .plant, .camp, .villager, .troop).intro-wait,
  .decor :is(.house, .plant, .camp, .villager, .troop).intro-hop {
    translate: none;
    opacity: 1;
    animation: none;
    pointer-events: auto;
  }
}
