:root {
  --sand: #fdf0c8; --ocean: #2ab7ca; --deep: #0b4f6c; --coral: #fe4a49; --sun: #fed766; --ink: #12334a;
  --sat: env(safe-area-inset-top, 0px); --sar: env(safe-area-inset-right, 0px);
  --sab: env(safe-area-inset-bottom, 0px); --sal: env(safe-area-inset-left, 0px);
}
* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body {
  margin: 0; height: 100%; overflow: hidden; position: fixed; inset: 0; width: 100%;
  overscroll-behavior: none; touch-action: none;
  user-select: none; -webkit-user-select: none; -webkit-touch-callout: none;
  background: var(--ocean); color: var(--ink);
  font-family: system-ui, -apple-system, 'Segoe UI', Roboto, sans-serif;
}
canvas { position: fixed; inset: 0; width: 100%; height: 100%; display: block; touch-action: none; }
.hidden { display: none !important; }
button { font: inherit; cursor: pointer; }

/* ---------- HUD ---------- */
#hud { position: fixed; inset: 0; pointer-events: none; }
.pill {
  display: inline-block; background: rgba(11, 79, 108, 0.82); color: #fff; font-weight: 800;
  font-size: 15px; padding: 6px 12px; border-radius: 999px; white-space: nowrap;
  box-shadow: 0 2px 6px rgba(0, 0, 0, 0.25);
}
.hud-tl, .hud-tc, .hud-tr { position: absolute; display: flex; gap: 6px; align-items: center; top: calc(8px + var(--sat)); }
.hud-tl { left: calc(10px + var(--sal)); flex-direction: column; align-items: flex-start; }
.hud-tl .row { display: flex; gap: 6px; }
.small-pill { font-size: 12px; padding: 4px 10px; }
.pill.warn { background: var(--coral); animation: pulse 0.6s infinite; }
.hud-tc { left: 50%; transform: translateX(-50%); }
.hud-tr { right: calc(10px + var(--sar)); }
.btn-icon { pointer-events: auto; border: 0; padding: 6px 12px; }

#buildbar {
  position: absolute; right: calc(8px + var(--sar)); top: 50px; bottom: calc(8px + var(--sab));
  display: flex; flex-flow: column wrap-reverse; justify-content: center; align-content: flex-end; gap: 6px;
  pointer-events: none;                 /* only the buttons catch taps; the rest of the strip is still the map */
}
#buildbar .bb { pointer-events: auto; }
#palette { display: contents; }         /* tall screens: the buildings are just buttons in the bar */
.bb {
  width: 54px; height: 54px; border-radius: 16px; border: 3px solid #fff; background: var(--deep); color: #fff;
  display: flex; flex-direction: column; align-items: center; justify-content: center; line-height: 1.1;
  box-shadow: 0 3px 8px rgba(0, 0, 0, 0.3); padding: 0;
}
.bb .e { font-size: 24px; } .bb .c { font-size: 11px; font-weight: 800; }
.bb.active { background: var(--coral); transform: scale(1.08); }
.bb.poor:not(.active) { background: #4f7183; border-color: #cfe3ea; }
.bb.poor:not(.active) .e, .bb.poor:not(.active) .c { opacity: 0.6; }
.bb.cancel { background: #6b7280; display: none; }
.bb.cancel.on { display: flex; }
.bb.ok { background: #5d8a6f; border-color: #cfe3ea; }
.bb.ok .e, .bb.ok .c { opacity: 0.5; }
.bb.ok.ready { background: #1f9d55; border-color: #fff; box-shadow: 0 0 0 3px rgba(255,255,255,0.55); }
.bb.ok.ready .e, .bb.ok.ready .c { opacity: 1; }
@keyframes pulse { 50% { transform: scale(1.1); } }

#tip {
  position: absolute; display: none; width: 230px; padding: 10px 12px; border-radius: 12px; pointer-events: none;
  background: rgba(11, 79, 108, 0.96); color: #fff; border: 2px solid #fff; box-shadow: 0 4px 14px rgba(0, 0, 0, 0.4);
  font-size: 13px; line-height: 1.35;
}
#tip.show { display: block; }
#tip h4 { margin: 0 0 2px; font-size: 15px; color: var(--sun); }
#tip .cost { font-weight: 800; margin-bottom: 4px; }
#tip .cost .ic { height: 1.1em; }
#tip ul { margin: 6px 0 0; padding-left: 16px; opacity: 0.9; }
#tip .need { margin-top: 6px; color: #ffb4b4; font-weight: 700; }

.ability {
  position: absolute; left: calc(152px + var(--sal)); bottom: calc(12px + var(--sab)); pointer-events: auto;
  width: 66px; height: 66px; border-radius: 50%; border: 4px solid #fff; background: var(--deep); color: #fff;
  display: flex; flex-direction: column; align-items: center; justify-content: center; padding: 0;
  box-shadow: 0 3px 8px rgba(0, 0, 0, 0.35); opacity: 0.6;
}
.ability .e { font-size: 28px; line-height: 1; } .ability .cd { font-size: 12px; font-weight: 800; min-height: 14px; }
.ability.ready { background: var(--coral); opacity: 1; box-shadow: 0 0 0 4px rgba(255,255,255,0.6); }

#toast {
  position: absolute; left: 50%; bottom: calc(18px + var(--sab)); transform: translate(-50%, 20px);
  background: rgba(18, 51, 74, 0.92); color: #fff; padding: 10px 18px; border-radius: 12px; font-weight: 700;
  opacity: 0; transition: opacity 0.2s, transform 0.2s; max-width: 70vw; text-align: center;
}
#toast.show { opacity: 1; transform: translate(-50%, 0); }

.ic { height: 1.35em; width: auto; vertical-align: middle; margin-right: 4px; }
.pill b { font-weight: 800; vertical-align: middle; }
#hLives .ic { margin-right: 1px; height: 1.25em; }
.bb .thumb { height: 30px; width: 40px; object-fit: contain; }
.bb .ic.sm { height: 11px; margin: 0 1px 0 0; }

#menu { background: linear-gradient(rgba(10, 60, 90, 0.25), rgba(10, 60, 90, 0.45)), url(../assets/scene_sunset.jpg?v=9) center / cover; backdrop-filter: none; }
.intro-card { max-width: min(92vw, 520px); cursor: pointer; }
.big-scene { max-height: 40vh; }
.intro-card p { margin: 4px 0; font-weight: 600; }
.logo { display: block; margin: -4px auto 0; max-height: 120px; max-width: 70%; }
.card h1 { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); }
.scene { display: block; width: 100%; max-height: 150px; object-fit: cover; border-radius: 12px; margin-bottom: 8px; border: 3px solid #fff; }

/* ---------- screens ---------- */
.screen {
  position: fixed; inset: 0; display: flex; align-items: center; justify-content: center;
  background: rgba(10, 60, 90, 0.55); backdrop-filter: blur(2px); padding: 10px;
}
.card {
  background: var(--sand); border: 4px solid #fff; border-radius: 22px; padding: 16px 22px; text-align: center;
  box-shadow: 0 8px 30px rgba(0, 0, 0, 0.35); max-width: min(92vw, 420px); max-height: 96vh; overflow: auto;
}
.card.wide { max-width: min(96vw, 820px); }
h1 { margin: 0; font-size: clamp(26px, 6vh, 40px); color: var(--coral); }
h2 { margin: 0 0 8px; font-size: 28px; color: var(--deep); }
.sub { margin: 2px 0 10px; font-weight: 600; }
.small { font-size: 12px; opacity: 0.75; margin: 8px 0 0; }
.score { font-size: 20px; margin: 6px 0; }
#heroes { display: flex; gap: 8px; justify-content: center; flex-wrap: wrap; margin-bottom: 10px; }
.hero {
  width: 140px; padding: 8px 6px; border-radius: 14px; border: 3px solid transparent; background: #fff;
  display: flex; flex-direction: column; align-items: center; gap: 2px; color: var(--ink);
}
.hero .he { font-size: 30px; height: 84px; display: flex; align-items: flex-end; }
.hero .he img { height: 84px; width: auto; max-width: 110px; object-fit: contain; }
@media (max-height: 420px) { .hero .he, .hero .he img { height: 56px; } } .hero b { font-size: 13px; } .hero small { font-size: 11px; opacity: 0.75; line-height: 1.2; }
.hero.sel { border-color: var(--coral); background: #fff7df; transform: translateY(-2px); }
.big {
  display: block; width: 100%; margin: 8px auto 0; padding: 12px 18px; border: 0; border-radius: 14px;
  background: var(--coral); color: #fff; font-size: 20px; font-weight: 900; box-shadow: 0 4px 0 #b32f2e;
}
.big:active { transform: translateY(2px); box-shadow: 0 2px 0 #b32f2e; }
.ghost { display: block; width: 100%; margin: 8px auto 0; padding: 9px; border-radius: 12px; border: 2px solid var(--deep); background: transparent; color: var(--deep); font-weight: 800; }

/* ---------- rotate prompt (touch devices in portrait) ---------- */
#rotate { display: none; }
@media (orientation: portrait) and (pointer: coarse) {
  #rotate {
    display: flex; position: fixed; inset: 0; z-index: 99; align-items: center; justify-content: center;
    background: var(--deep); color: #fff; text-align: center; font-size: 24px; font-weight: 800;
  }
  #rotate .phone-img { width: 180px; display: block; margin: 0 auto 8px; }
  #rotate small { font-size: 14px; font-weight: 600; opacity: 0.8; }
}
@keyframes rot { 0%, 20% { transform: rotate(0); } 60%, 100% { transform: rotate(-90deg); } }

@media (max-height: 420px) {
  .card { padding: 8px 16px; }
  .hero { width: 118px; padding: 5px 4px; } .hero small { display: none; }
  .sub { margin-bottom: 6px; font-size: 13px; }
  .logo { max-height: 70px; } .scene { max-height: 80px; margin-bottom: 4px; }
  .bb .thumb { height: 26px; width: 34px; }
  .big { padding: 8px 18px; font-size: 18px; margin-top: 6px; }
  .bb { width: 52px; height: 52px; } .bb .e { font-size: 20px; } .bb .c { font-size: 9px; }
  .pill { font-size: 13px; padding: 5px 10px; }
}

/* ---------- multiplayer ---------- */
.online { margin-top: 10px; padding-top: 8px; border-top: 2px dashed rgba(11, 79, 108, 0.25); display: flex; flex-wrap: wrap; gap: 6px; justify-content: center; }
.online input { flex: 1 1 150px; min-width: 120px; padding: 9px 10px; border-radius: 10px; border: 2px solid var(--deep); background: #fff; color: var(--ink); font: inherit; font-weight: 700; }
.big.alt { background: var(--deep); box-shadow: 0 4px 0 #062e40; flex: 1 1 100%; }
#onlineMsg { flex: 1 1 100%; margin: 0; min-height: 14px; color: #b32f2e; font-weight: 700; opacity: 1; }
#team { display: flex; flex-direction: column; gap: 3px; margin-top: 4px; }
.mate { display: flex; align-items: center; gap: 5px; background: rgba(11, 79, 108, 0.82); color: #fff; border-radius: 999px; padding: 2px 10px 2px 4px; font-size: 12px; font-weight: 800; box-shadow: 0 2px 6px rgba(0, 0, 0, 0.25); }
.mate img { height: 22px; width: 22px; object-fit: contain; border-radius: 50%; background: rgba(255, 255, 255, 0.85); }
.mate span { color: #ff8f8f; letter-spacing: -1px; }
.mate.out { opacity: 0.6; }
@media (max-height: 420px) { .online { margin-top: 4px; padding-top: 4px; } .online input { padding: 6px 8px; } }

/* ---------- lobby, countdown, scoreboard, emotes ---------- */
#lobbyCode { color: var(--coral); letter-spacing: 1px; }
#lobbyPlayers { display: flex; flex-wrap: wrap; gap: 8px; justify-content: center; margin: 6px 0; }
.lp { display: flex; align-items: center; gap: 6px; background: #fff; border: 3px solid transparent; border-radius: 14px; padding: 4px 12px 4px 6px; font-weight: 800; }
.lp img { height: 54px; width: 44px; object-fit: contain; }
.lp.ready { border-color: #1f9d55; background: #eafaf0; }
.lp.me b::after { content: ' (you)'; font-weight: 600; opacity: 0.6; }
#lobbyHeroes { display: flex; gap: 6px; justify-content: center; flex-wrap: wrap; margin-bottom: 6px; }
#lobbyHeroes .hero { width: 74px; padding: 4px; } #lobbyHeroes .hero small, #lobbyHeroes .hero b { display: none; }
#lobbyHeroes .hero .he, #lobbyHeroes .hero .he img { height: 52px; }
.btnrow { display: flex; gap: 8px; } .btnrow > * { flex: 1; }
.btnrow .big { margin-top: 6px; }
#countdown { position: fixed; inset: 0; display: flex; align-items: center; justify-content: center; pointer-events: none; z-index: 50; }
#countdown span { font-size: min(34vh, 220px); font-weight: 900; color: #fff; -webkit-text-stroke: 6px var(--deep); paint-order: stroke fill; text-shadow: 0 8px 0 rgba(0, 0, 0, 0.25); animation: pop 1s infinite; }
@keyframes pop { 0% { transform: scale(1.4); opacity: 0; } 25% { transform: scale(1); opacity: 1; } 100% { transform: scale(0.9); opacity: 0.7; } }
.board { margin: 6px 0; display: flex; flex-direction: column; gap: 4px; text-align: left; }
.board:empty { display: none; }
.brow { display: flex; align-items: center; gap: 8px; background: #fff; border-radius: 12px; padding: 3px 10px 3px 4px; font-weight: 700; font-size: 13px; }
.brow img { height: 34px; width: 28px; object-fit: contain; } .brow b { flex: 1; } .brow span { opacity: 0.8; white-space: nowrap; }
.brow.mvp { background: #fff3c4; }
#emotes { position: absolute; left: 50%; bottom: calc(12px + var(--sab)); transform: translateX(-50%); display: flex; gap: 6px; pointer-events: auto; }
#emotes button { width: 44px; height: 44px; border-radius: 50%; border: 3px solid #fff; background: var(--deep); font-size: 22px; padding: 0; box-shadow: 0 3px 8px rgba(0, 0, 0, 0.3); }
#emotes button:active { background: var(--coral); }
@media (max-height: 420px) { #lobbyHeroes .hero { width: 60px; } .lp img { height: 40px; } .brow img { height: 26px; } }

/* ---------- reconnect banner ---------- */
#reconnect { position: fixed; inset: 0; z-index: 90; display: flex; align-items: center; justify-content: center; background: rgba(10, 60, 90, 0.55); }
#reconnect .card { padding: 14px 22px; }
#reconnect p { margin: 6px 0 0; font-weight: 800; }
.spin { width: 38px; height: 38px; margin: 0 auto; border-radius: 50%; border: 5px solid rgba(11, 79, 108, 0.25); border-top-color: var(--coral); animation: spin 0.9s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }

/* ---------- short screens (phones held sideways): one Build button + a palette with names ---------- */
.bb.menu { display: none; }
.bb .n { display: none; }
@media (max-height: 560px) {
  #buildbar { flex-flow: column nowrap; align-items: flex-end; }
  .bb.menu { display: flex; }
  #palette {
    display: none; position: fixed; right: calc(72px + var(--sar)); top: 50px; max-height: calc(100vh - 60px);
    max-height: calc(100dvh - 60px); overflow: auto; grid-template-columns: repeat(4, 76px); gap: 6px; padding: 8px;
    background: rgba(11, 79, 108, 0.96); border: 3px solid #fff; border-radius: 16px; box-shadow: 0 6px 18px rgba(0, 0, 0, 0.45);
    pointer-events: auto; z-index: 6;
  }
  #palette.open { display: grid; }
  #palette .bb { width: 76px; height: 76px; }
  #palette .bb .n { display: block; font-size: 10px; font-weight: 800; line-height: 1.05; max-width: 68px; text-align: center; margin: 1px 0; }
  #palette .bb .thumb { height: 32px; width: 46px; }
  #palette .bb .c { font-size: 10px; }
}

/* narrow screens: the top bar must fit, so "days to Easter" shrinks to the number */
@media (max-width: 900px) { .long { display: none; } .pill { font-size: 13px; padding: 5px 9px; } .hud-tc, .hud-tr { gap: 4px; } }
