/* =====================================================================
   styles.css — ATLAS CITY v2, "The Village"

   Palette (locked, Option B — 16-bit cozy):
     amber #d9a441 · ochre #c98a3d · moss #5a6b3a
     terracotta #b0603f · brown #7a5230 · dusk #4a5a7a

   Sky and tint colours are all mixes of those six. Light falls from the
   upper right; shadows go cool blue-purple, never black.
   ===================================================================== */

:root {
  --amber: #d9a441;
  --ochre: #c98a3d;
  --moss: #5a6b3a;
  --terracotta: #b0603f;
  --brown: #7a5230;
  --dusk: #4a5a7a;

  --ink: #2f2418;
  --parchment: #e7cf9f;

  --u: 2px;              /* one scene pixel, in CSS px — set by village.js */
}

/* ---- Day / night -----------------------------------------------------
   Driven by the viewer's local clock (see applyDaylight). The tint sits
   above the art and multiplies, so the whole village shifts together. */

html[data-phase='day'] {
  --sky-top: #7e93b8;
  --sky-mid: #b8b193;
  --sky-bot: #e3c07e;
  --tint: rgba(217, 164, 65, 0.05);
  --glow-strength: 0.28;
  --window-strength: 0.55;
}
html[data-phase='dawn'] {
  --sky-top: #5d6a92;
  --sky-mid: #a5809a;
  --sky-bot: #d9a441;
  --tint: rgba(176, 96, 63, 0.12);
  --glow-strength: 0.5;
  --window-strength: 0.8;
}
html[data-phase='dusk'] {
  --sky-top: #3f4b69;
  --sky-mid: #7a5a72;
  --sky-bot: #b0603f;
  --tint: rgba(74, 90, 122, 0.24);
  --glow-strength: 0.7;
  --window-strength: 1;
}
html[data-phase='night'] {
  --sky-top: #232c46;
  --sky-mid: #33405f;
  --sky-bot: #4a5a7a;
  --tint: rgba(38, 48, 76, 0.5);
  --glow-strength: 0.95;
  --window-strength: 1;
}

/* ---- Frame ------------------------------------------------------------ */

* { box-sizing: border-box; }

html, body {
  height: 100%;
  margin: 0;
}

body {
  background: var(--sky-top);
  color: var(--parchment);
  font-family: ui-sans-serif, -apple-system, "Segoe UI", Roboto, sans-serif;
  overflow: hidden;
  opacity: 0;
  transition: opacity 400ms ease;
}
body.is-ready { opacity: 1; }

#sky {
  position: fixed;
  inset: 0;
  background: linear-gradient(to bottom,
              var(--sky-top) 0%, var(--sky-mid) 58%, var(--sky-bot) 100%);
  transition: background 1200ms linear;
  z-index: 0;
}

#app {
  position: relative;
  z-index: 1;
  height: 100%;
  display: flex;
  flex-direction: column;
}

.topbar {
  flex: 0 0 auto;
  padding: calc(env(safe-area-inset-top, 0px) + 10px) 14px 8px;
  text-align: center;
  pointer-events: none;
}
.topbar h1 {
  margin: 0;
  font-family: ui-monospace, SFMono-Regular, Menlo, "Courier New", monospace;
  font-size: 13px;
  letter-spacing: 0.22em;
  text-transform: uppercase;
  color: var(--parchment);
  text-shadow: 0 2px 0 rgba(47, 36, 24, 0.55);
}
.topbar p {
  margin: 3px 0 0;
  font-size: 11px;
  letter-spacing: 0.06em;
  color: rgba(231, 207, 159, 0.72);
}

/* ---- Stage & scene ---------------------------------------------------- */

#stage {
  position: relative;
  flex: 1 1 auto;
  overflow: hidden;
  display: flex;
  align-items: center;
  justify-content: center;
  touch-action: pan-y;
}
#stage.can-pan { cursor: grab; touch-action: none; }
#stage.can-pan:active { cursor: grabbing; }

#scene {
  position: relative;
  flex: 0 0 auto;
  overflow: hidden;              /* keep the village inside its own frame */
  will-change: transform;        /* also gives the scene its stacking context */
  transition: transform 220ms cubic-bezier(.22, .61, .36, 1);
  box-shadow: 0 calc(var(--u) * 3) 0 rgba(47, 36, 24, 0.22);
}
#scene.is-dragging { transition: none; }

/* Every raster in the village is nearest-neighbour scaled — no blur. */
#scene img {
  position: absolute;
  image-rendering: pixelated;
  image-rendering: crisp-edges;
  -ms-interpolation-mode: nearest-neighbor;
  user-select: none;
  -webkit-user-drag: none;
}

.terrain { z-index: 0; }

.piece {
  position: absolute;
  pointer-events: none;   /* the hit target is the sibling <button> */
}
.piece .art {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
}
.piece .art-lit { opacity: 0; transition: opacity 500ms ease; }

/* Lit windows: only when the feed says work is in flight. */
.piece.is-busy .art-lit { opacity: var(--window-strength, 1); }
@media (prefers-reduced-motion: no-preference) {
  .piece.is-busy .art-lit { animation: flicker 5.5s ease-in-out infinite; }
  .piece.is-busy:nth-of-type(2n) .art-lit { animation-delay: -2.1s; }
  .piece.is-busy:nth-of-type(3n) .art-lit { animation-delay: -3.7s; }
}
@keyframes flicker {
  0%, 100% { opacity: var(--window-strength, 1); }
  42%      { opacity: calc(var(--window-strength, 1) * 0.84); }
  46%      { opacity: var(--window-strength, 1); }
  71%      { opacity: calc(var(--window-strength, 1) * 0.9); }
}

/* Blocked/disabled homes go quiet and cool — never red, never blinking. */
.piece.is-blocked .art { filter: saturate(0.35) brightness(0.86); }
.piece.is-disabled .art { filter: saturate(0.2) brightness(0.74); }
.piece.is-unknown .art { filter: saturate(0.5) brightness(0.86); }

/* ---- Warm spill from lit windows -------------------------------------- */

.home-glow {
  position: absolute;
  pointer-events: none;
  opacity: 0;
  transition: opacity 600ms ease;
  background: radial-gradient(ellipse at 50% 70%,
              rgba(217, 164, 65, 0.85) 0%,
              rgba(217, 164, 65, 0.28) 45%,
              rgba(217, 164, 65, 0) 72%);
  mix-blend-mode: screen;
  z-index: 3;
}
.piece.is-busy .home-glow { opacity: var(--glow-strength, 0.4); }

/* ---- Chimney smoke ----------------------------------------------------- */

.smoke {
  position: absolute;
  pointer-events: none;
  z-index: 2;
}
.puff {
  position: absolute;
  left: 0;
  bottom: 0;
  width: calc(var(--u) * 8);
  height: calc(var(--u) * 8);
  background-repeat: no-repeat;
  background-size: 100% 100%;
  image-rendering: pixelated;
  opacity: 0;
}
@media (prefers-reduced-motion: no-preference) {
  .puff { animation: drift 7s linear infinite; }
  .puff-1 { animation-delay: -2.4s; }
  .puff-2 { animation-delay: -4.8s; }
}
@keyframes drift {
  0%   { transform: translate(0, 0) scale(0.6); opacity: 0; }
  18%  { opacity: 0.42; }
  100% { transform: translate(calc(var(--u) * 9), calc(var(--u) * -22)) scale(1.35); opacity: 0; }
}

/* ---- Trouble cloud ------------------------------------------------------ */

.cloud {
  opacity: 0;
  transition: opacity 500ms ease;
  z-index: 4;
}
.piece.is-blocked .cloud { opacity: 0.92; }
@media (prefers-reduced-motion: no-preference) {
  .piece.is-blocked .cloud { animation: hover 9s ease-in-out infinite; }
}
@keyframes hover {
  0%, 100% { transform: translateX(calc(var(--u) * -1.5)); }
  50%      { transform: translateX(calc(var(--u) * 1.5)); }
}

/* ---- Lighthouse sweep ---------------------------------------------------- */

.beam {
  position: absolute;
  pointer-events: none;
  z-index: 3;
  background: conic-gradient(from 200deg at 50% 100%,
              rgba(217, 164, 65, 0) 0deg,
              rgba(217, 164, 65, 0.45) 14deg,
              rgba(217, 164, 65, 0) 30deg);
  mix-blend-mode: screen;
  opacity: calc(var(--glow-strength, 0.4) * 0.8);
}
@media (prefers-reduced-motion: no-preference) {
  .beam { animation: sweep 11s linear infinite; }
}
@keyframes sweep {
  0%   { transform: rotate(-26deg); }
  50%  { transform: rotate(26deg); }
  100% { transform: rotate(-26deg); }
}
.beam { transform-origin: 50% 100%; }

/* ---- Approval flags ------------------------------------------------------ */

.flags { position: absolute; pointer-events: none; z-index: 6; }
.flags::before {
  /* the little roof pole the flags fly from */
  content: '';
  position: absolute;
  left: 0;
  top: 0;
  width: calc(var(--u) * 1);
  height: 100%;
  background: var(--amber);
}
.flag {
  position: absolute;
  left: 0;
  width: 86%;
  height: 30%;
  background-repeat: no-repeat;
  background-size: 100% 100%;
  image-rendering: pixelated;
  transform-origin: 0% 50%;
}
.flag-0 { top: 0; }
.flag-1 { top: 34%; }
.flag-2 { top: 68%; }
@media (prefers-reduced-motion: no-preference) {
  .flag { animation: wave 3.4s ease-in-out infinite; }
  .flag-1 { animation-delay: -1.1s; }
  .flag-2 { animation-delay: -2.2s; }
}
@keyframes wave {
  0%, 100% { transform: skewY(0deg) scaleX(1); }
  50%      { transform: skewY(-4deg) scaleX(0.92); }
}
.flag-more {
  position: absolute;
  left: 0;
  top: 104%;
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
  font-size: calc(var(--u) * 5);
  color: var(--parchment);
  background: var(--brown);
  border: calc(var(--u) * 0.5) solid var(--amber);
  padding: 0 calc(var(--u) * 1.5);
  white-space: nowrap;
}

/* ---- Well glow (something is waiting on you) ----------------------------- */

.well-glow {
  position: absolute;
  pointer-events: none;
  z-index: 300;
  opacity: 0;
  transition: opacity 800ms ease;
  background: radial-gradient(ellipse at 50% 70%,
              rgba(217, 164, 65, 0.7) 0%,
              rgba(217, 164, 65, 0.2) 48%,
              rgba(217, 164, 65, 0) 74%);
  mix-blend-mode: screen;
}
.well-glow.is-on { opacity: calc(var(--glow-strength, 0.4) * 0.9); }
@media (prefers-reduced-motion: no-preference) {
  .well-glow.is-on { animation: breathe 5s ease-in-out infinite; }
}
@keyframes breathe {
  0%, 100% { opacity: calc(var(--glow-strength, 0.4) * 0.55); }
  50%      { opacity: calc(var(--glow-strength, 0.4) * 1); }
}

/* ---- Signs --------------------------------------------------------------- */

.sign-text {
  position: absolute;
  pointer-events: none;
  text-align: center;
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
  font-size: calc(var(--u) * 4);
  line-height: calc(var(--u) * 5);
  letter-spacing: 0.02em;
  color: var(--ink);
  text-transform: uppercase;
}
.sign-text.is-stale-text { text-transform: none; }

.home-hit {
  position: absolute;
  appearance: none;
  background: transparent;
  border: 0;
  padding: 0;
  margin: 0;
  cursor: pointer;
  z-index: 500;
}
.home-hit:focus { outline: none; }
.home-hit:focus-visible::before,
.home-hit.is-selected::before {
  content: '';
  position: absolute;
  inset: calc(var(--u) * -2);
  border: calc(var(--u) * 1) solid var(--amber);
  box-shadow: 0 0 0 calc(var(--u) * 1) rgba(47, 36, 24, 0.5);
}

/* Josiah's nameplate: same sign, but no button wrapped around it. */
.name-plate { position: absolute; pointer-events: none; }
.name-plate .door-sign { top: 0; }

.door-sign {
  position: absolute;
  left: 50%;
  top: 100%;
  transform: translate(-50%, calc(var(--u) * -1));
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
  font-size: calc(var(--u) * 5);
  line-height: 1;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  white-space: nowrap;
  color: var(--parchment);
  background: var(--brown);
  padding: calc(var(--u) * 1.4) calc(var(--u) * 2);
  border-top: calc(var(--u)) solid #9a6c42;
  border-bottom: calc(var(--u)) solid #593b22;
  box-shadow: 0 calc(var(--u)) 0 rgba(47, 36, 24, 0.4);
}
.home-hit:hover .door-sign,
.home-hit:focus-visible .door-sign,
.home-hit.is-selected .door-sign {
  background: var(--amber);
  color: var(--ink);
}
.home-hit.is-disabled .door-sign { opacity: 0.55; }

/* ---- Global tint & staleness --------------------------------------------- */

#tint {
  position: absolute;
  inset: 0;
  z-index: 900;
  pointer-events: none;
  background: var(--tint);
  transition: background 1200ms linear;
}

/* The anti-lie rule made visible: old data means a dim village. */
#scene.is-stale { filter: saturate(0.5) brightness(0.82) contrast(0.96); }
#scene.is-stale .home-glow,
#scene.is-stale .beam { opacity: 0 !important; }

/* ---- Card ------------------------------------------------------------------ */

#card {
  position: fixed;
  z-index: 1000;
  left: 50%;
  bottom: calc(env(safe-area-inset-bottom, 0px) + 16px);
  transform: translate(-50%, 12px);
  width: min(360px, calc(100vw - 28px));
  background: var(--brown);
  color: var(--parchment);
  border-top: 3px solid #9a6c42;
  border-bottom: 3px solid #4d3220;
  box-shadow: 0 10px 0 rgba(47, 36, 24, 0.35), 0 0 0 3px #593b22;
  padding: 14px 16px 16px;
  opacity: 0;
  pointer-events: none;
  transition: opacity 180ms ease, transform 180ms ease;
}
#card.is-open {
  opacity: 1;
  transform: translate(-50%, 0);
  pointer-events: auto;
}
#card::before {
  /* two nail heads, like a real wooden sign */
  content: '';
  position: absolute;
  top: 8px; left: 10px; right: 10px;
  height: 4px;
  background:
    radial-gradient(circle at 3px 2px, var(--amber) 0 2px, transparent 2px) left center / 20px 4px no-repeat,
    radial-gradient(circle at 17px 2px, var(--amber) 0 2px, transparent 2px) right center / 20px 4px no-repeat;
  opacity: 0.8;
}

.card-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  margin-top: 6px;
}
#card-title {
  margin: 0;
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
  font-size: 17px;
  letter-spacing: 0.14em;
  text-transform: uppercase;
  color: var(--amber);
}
#card-close {
  appearance: none;
  background: #593b22;
  color: var(--parchment);
  border: 2px solid #9a6c42;
  width: 30px;
  height: 30px;
  font-size: 15px;
  line-height: 1;
  cursor: pointer;
  flex: 0 0 auto;
}
#card-close:hover { background: var(--terracotta); }
#card-close:focus-visible { outline: 2px solid var(--amber); outline-offset: 2px; }

.card-line {
  margin: 10px 0 0;
  font-size: 14.5px;
  line-height: 1.45;
}
.card-facts {
  margin: 12px 0 0;
  display: grid;
  grid-template-columns: auto 1fr;
  gap: 4px 14px;
  font-size: 12.5px;
  border-top: 2px solid #593b22;
  padding-top: 10px;
}
.card-facts dt {
  color: rgba(231, 207, 159, 0.62);
  text-transform: uppercase;
  letter-spacing: 0.08em;
  font-size: 11px;
  align-self: center;
}
.card-facts dd { margin: 0; text-align: right; }
.card-note {
  margin: 10px 0 0;
  font-size: 12px;
  color: var(--amber);
}

/* ---- Desktop ---------------------------------------------------------------- */

@media (min-width: 760px) {
  .topbar h1 { font-size: 15px; }
  #card {
    left: auto;
    right: 24px;
    bottom: 24px;
    transform: translate(0, 12px);
    width: 340px;
  }
  #card.is-open { transform: translate(0, 0); }
}

/* ---- Motion preferences ------------------------------------------------------ */

@media (prefers-reduced-motion: reduce) {
  * { animation: none !important; }
  #scene, #sky, .piece .art-lit, .home-glow, .well-glow, #card, #tint {
    transition: none !important;
  }
}
