:root {
  --bg: #f5f6fb;
  --surface: #ffffff;
  --surface-2: #eef0f8;
  --text: #1c2030;
  --text-dim: #6b7186;
  --accent: #4f46e5;
  --accent-text: #ffffff;
  --mine: #4f46e5;
  --mine-text: #ffffff;
  --danger: #c2373f;
  --border: #dcdfeb;
  --shadow: 0 1px 3px rgb(20 24 40 / 0.08);
  --radius: 14px;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #12141c;
    --surface: #1c1f2b;
    --surface-2: #262a3a;
    --text: #e8eaf2;
    --text-dim: #9aa0b5;
    --accent: #7c75f0;
    --accent-text: #12141c;
    --mine: #5a52e0;
    --mine-text: #ffffff;
    --danger: #e06b72;
    --border: #333849;
    --shadow: 0 1px 3px rgb(0 0 0 / 0.4);
  }
}

* { box-sizing: border-box; }

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

body {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  background: var(--bg);
  color: var(--text);
  -webkit-font-smoothing: antialiased;
  overscroll-behavior-y: none;
}

.hidden { display: none !important; }

/* ---------- generic screens & cards ---------- */

.screen {
  min-height: 100dvh;
  display: flex;
  flex-direction: column;
  align-items: center;
  padding: env(safe-area-inset-top) 16px calc(16px + env(safe-area-inset-bottom));
  gap: 16px;
  max-width: 480px;
  margin: 0 auto;
}

.hero { text-align: center; padding: 40px 8px 8px; }
.hero.small { padding-top: 28px; }
.hero-icon { font-size: 56px; line-height: 1; }
.hero-icon .hero-pigeon { width: 84px; height: 63px; color: #e59fb0; }
.hero h1 { margin: 8px 0 4px; font-size: 28px; }
.hero h1 .wordmark { width: min(290px, 82vw); height: auto; vertical-align: middle; }
.tagline { margin: 0; color: var(--text-dim); font-size: 15px; }

.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 18px;
  width: 100%;
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.card h2 { margin: 0 0 2px; font-size: 18px; }
.card.consent { margin-top: 40px; text-align: center; }
.card.consent p { margin: 0; font-size: 15px; color: var(--text-dim); text-align: left; }
.card.consent .hero-icon { font-size: 40px; }

label { font-size: 13px; font-weight: 600; color: var(--text-dim); }

input, textarea {
  font: inherit;
  color: var(--text);
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 10px 12px;
  width: 100%;
}

input:focus, textarea:focus { outline: 2px solid var(--accent); outline-offset: -1px; }

.btn {
  font: inherit;
  font-weight: 600;
  border: 1px solid var(--border);
  background: var(--surface-2);
  color: var(--text);
  border-radius: 10px;
  padding: 11px 14px;
  cursor: pointer;
}

.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-text); }
.btn.danger { color: var(--danger); }
.btn.small { padding: 6px 10px; font-size: 13px; }
.btn:disabled { opacity: 0.5; cursor: default; }

.form-error { color: var(--danger); font-size: 14px; margin: 0; }
.fineprint { font-size: 12.5px; color: var(--text-dim); margin: 4px 0 0; }
.center { text-align: center; }
.fineprint a { color: var(--accent); }

/* ---------- chat layout ---------- */

#screen-chat {
  max-width: 640px;
  padding: 0;
  height: 100dvh;
  gap: 0;
  align-items: stretch;
}

.chat-header {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: calc(8px + env(safe-area-inset-top)) 10px 8px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  position: sticky;
  top: 0;
  z-index: 5;
}

.chat-title { flex: 1; min-width: 0; text-align: center; }
.chat-title h1 {
  margin: 0;
  font-size: 17px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.chat-title p { margin: 0; font-size: 12px; color: var(--text-dim); }

.icon-btn {
  font: inherit;
  font-size: 18px;
  line-height: 1;
  background: none;
  border: none;
  color: var(--text);
  padding: 10px;
  border-radius: 10px;
  cursor: pointer;
  flex-shrink: 0;
}
.icon-btn:active { background: var(--surface-2); }

#feed {
  flex: 1;
  overflow-y: auto;
  padding: 12px 12px 4px;
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.day-divider {
  align-self: center;
  font-size: 12px;
  color: var(--text-dim);
  background: var(--surface-2);
  border-radius: 999px;
  padding: 3px 10px;
  margin: 6px 0 2px;
}

/* messages */

.msg { display: flex; flex-direction: column; align-items: flex-start; max-width: 85%; }
.msg.mine { align-self: flex-end; align-items: flex-end; }

.msg .meta {
  font-size: 12px;
  color: var(--text-dim);
  margin: 0 6px 2px;
  display: flex;
  gap: 6px;
}

.bubble {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 16px;
  border-bottom-left-radius: 6px;
  padding: 9px 13px;
  font-size: 15.5px;
  white-space: pre-wrap;
  overflow-wrap: anywhere;
  box-shadow: var(--shadow);
}

.msg.mine .bubble {
  background: var(--mine);
  border-color: var(--mine);
  color: var(--mine-text);
  border-radius: 16px;
  border-bottom-right-radius: 6px;
}

.reactions {
  display: flex;
  flex-wrap: wrap;
  gap: 4px;
  margin: 4px 2px 0;
  align-items: center;
}

.reaction-pill {
  font: inherit;
  font-size: 13px;
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text);
  border-radius: 999px;
  padding: 3px 8px;
  cursor: pointer;
}
.reaction-pill.mine { border-color: var(--accent); background: color-mix(in srgb, var(--accent) 14%, var(--surface)); }

.react-add {
  font-size: 13px;
  border: 1px dashed var(--border);
  background: none;
  color: var(--text-dim);
  border-radius: 999px;
  padding: 3px 8px;
  cursor: pointer;
}

.react-picker { display: flex; gap: 2px; flex-wrap: wrap; }
.react-picker button {
  font-size: 18px;
  background: none;
  border: none;
  padding: 3px 5px;
  cursor: pointer;
  border-radius: 8px;
}
.react-picker button:active { background: var(--surface-2); }

/* polls */

.poll-card {
  align-self: stretch;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 12px 14px;
  max-width: 100%;
}

.poll-card .meta { font-size: 12px; color: var(--text-dim); margin-bottom: 2px; }
.poll-card h3 { margin: 2px 0 8px; font-size: 15.5px; }

.poll-option {
  display: block;
  width: 100%;
  text-align: left;
  font: inherit;
  font-size: 14.5px;
  border: 1px solid var(--border);
  background: var(--surface-2);
  color: var(--text);
  border-radius: 10px;
  padding: 8px 10px;
  margin-top: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}

.poll-option .bar {
  position: absolute;
  inset: 0 auto 0 0;
  background: color-mix(in srgb, var(--accent) 18%, transparent);
  z-index: 0;
  transition: width 0.25s ease;
}

.poll-option .opt-label { position: relative; z-index: 1; display: flex; justify-content: space-between; gap: 8px; }
.poll-option .count { color: var(--text-dim); font-variant-numeric: tabular-nums; }
.poll-option.mine { border-color: var(--accent); }
.poll-option.mine .opt-label::after { content: " ✓"; color: var(--accent); }

.poll-total { font-size: 12px; color: var(--text-dim); margin: 8px 0 0; }

/* composer */

.composer {
  background: var(--surface);
  border-top: 1px solid var(--border);
  padding: 8px 10px calc(8px + env(safe-area-inset-bottom));
  position: sticky;
  bottom: 0;
}

.pigeon-status {
  max-height: 72px;
  overflow: hidden;
  padding: 2px 6px 6px;
  transition: max-height 0.25s ease, opacity 0.25s ease, padding 0.25s ease;
}

.pigeon-status.collapsed { max-height: 0; opacity: 0; padding-top: 0; padding-bottom: 0; }

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

.pigeon-status strong { color: var(--text); font-variant-numeric: tabular-nums; }

.composer-row { display: flex; align-items: flex-end; gap: 4px; }

#message-input {
  flex: 1;
  resize: none;
  max-height: 120px;
  border-radius: 18px;
}

#message-input:disabled { opacity: 0.6; }

.icon-btn.send { color: var(--accent); font-size: 20px; }
.icon-btn.send:disabled { opacity: 0.4; cursor: default; }
.icon-btn.send.blocked { opacity: 0.4; }

/* ---------- Stage 2: the pigeon hook ---------- */

.pigeon { display: block; width: 32px; height: 24px; }

.pigeon .wing {
  transform-origin: 13px 12px;
  animation: flap 0.4s ease-in-out infinite alternate;
}
@keyframes flap {
  from { transform: rotate(8deg); }
  to { transform: rotate(-38deg); }
}
.pigeon.hustle .wing { animation-duration: 0.22s; }

/* flight bar: the story reads right-to-left — send button to coop */

.flight-track { position: relative; height: 28px; }

.flight-track .coop {
  position: absolute;
  left: 0;
  top: 3px;
  font-size: 16px;
  line-height: 1;
}

.flight-track .wire {
  position: absolute;
  left: 24px;
  right: 4px;
  top: 15px;
  border-top: 2px dashed var(--border);
}

/* reduced-motion progress fill (hidden otherwise) */
.flight-track .wire-fill {
  display: none;
  position: absolute;
  left: 24px;
  top: 14px;
  height: 4px;
  width: var(--p, 0%);
  background: var(--accent);
  border-radius: 2px;
}

.flight-pigeon {
  position: absolute;
  top: 1px;
  left: 92%;
  width: 30px;
  height: 22px;
  scale: -1 1; /* face left, toward the coop */
  transition: left 0.5s linear;
  animation: bob 1.6s ease-in-out infinite;
}
.flight-pigeon.hustle { animation-duration: 0.7s; }

.flight-pigeon.shrug { animation: shrug 0.4s ease-in-out; }
.flight-pigeon.shrug .wing { animation-duration: 0.15s; }
@keyframes shrug {
  0%, 100% { transform: translateY(0); }
  40% { transform: translateY(-2px); }
  70% { transform: translateY(2px); }
}

.pigeon-status.landing .flight-pigeon { animation: settle 0.25s ease-out; }
.pigeon-status.landing .wing { animation: none; }
@keyframes settle {
  from { transform: translateY(-4px); }
  60% { transform: translateY(1px) scaleY(0.85); }
  to { transform: none; }
}

.flight-pigeon-static {
  display: none;
  position: absolute;
  top: 3px;
  left: 24px;
  width: 26px;
  height: 20px;
}

.flight-text {
  margin: 2px 0 0;
  font-size: 13px;
  color: var(--text-dim);
  display: flex;
  gap: 6px;
  align-items: baseline;
}

/* the roost: one perch per member on a shared wire */

#roost {
  display: flex;
  align-items: flex-end;
  gap: 2px;
  padding: 4px 10px 0;
  min-height: 44px;
  overflow-x: auto;
  background: var(--surface);
  border-bottom: 2px solid var(--border); /* the wire */
  flex-shrink: 0;
  cursor: pointer;
  scrollbar-width: none;
}
#roost::-webkit-scrollbar { display: none; }

.perch {
  display: flex;
  flex-direction: column;
  align-items: center;
  min-width: 46px;
  flex-shrink: 0;
  position: relative; /* anchor the out-state countdown chip */
}

.perch .pigeon { width: 26px; height: 20px; }
.perch .pigeon.resting { display: none; } /* only shown while out (on cooldown) */

.perch-timer {
  display: none;
  font-size: 11px;
  color: var(--text-dim);
  font-variant-numeric: tabular-nums;
  height: 20px;
  line-height: 20px;
}

.perch-name {
  font-size: 10px;
  color: var(--text-dim);
  max-width: 7ch;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  padding-bottom: 2px;
}

/* while out (on cooldown): the pig-geon rests, countdown chip floats over it */
.perch.out .pigeon.idle { display: none; }
.perch.out .pigeon.resting { display: block; }
.perch.out .perch-timer {
  display: block;
  position: absolute;
  top: 9px;
  left: 50%;
  transform: translateX(-50%);
  height: auto;
  line-height: 1;
  padding: 1px 5px;
  border-radius: 7px;
  font-size: 10px;
  color: var(--text-dim);
  background: var(--surface);
  border: 1px solid var(--border);
  box-shadow: 0 1px 2px rgba(0, 0, 0, 0.12);
}
.perch.out .perch-name { opacity: 0.7; }

/* idle life: 95% still, one tiny head-bob, staggered per member */
.perch .pigeon.idle {
  animation: shuffle 6s ease-in-out infinite;
  animation-delay: var(--shuffle-delay, 0s);
}
@keyframes shuffle {
  0%, 94%, 100% { transform: none; }
  96% { transform: translateY(-1.5px) rotate(-2deg); }
  98% { transform: translateY(0); }
}

.perch .pigeon.hop { animation: hop 0.3s ease-in forwards; }
@keyframes hop {
  to { transform: translateY(-10px); opacity: 0; }
}

.perch .pigeon.land { animation: land 0.45s ease-out; }
@keyframes land {
  0% { transform: translateX(-14px) translateY(-6px); opacity: 0; }
  45% { transform: none; opacity: 1; }
  70% { transform: scaleY(0.85); }
  100% { transform: none; }
}

/* launch: the pigeon leaves the send button (nested translate = arc) */

.launch-pigeon {
  position: fixed;
  z-index: 60;
  pointer-events: none;
  animation: launch-x 0.9s linear forwards;
}
.launch-pigeon .launch-y { animation: launch-y 0.9s ease-out forwards; }
.launch-pigeon .pigeon { scale: -1 1; }
@keyframes launch-x {
  to { transform: translateX(-78vw); }
}
@keyframes launch-y {
  to { transform: translateY(-26vh) rotate(10deg); opacity: 0; }
}

.feather {
  position: fixed;
  z-index: 59;
  width: 8px;
  height: 5px;
  border-radius: 3px;
  background: var(--text-dim);
  opacity: 0.5;
  pointer-events: none;
  animation: feather 0.5s ease-out forwards;
}
.feather.f0 { --fx: -24px; --fy: -10px; }
.feather.f1 { --fx: 18px; --fy: -22px; }
.feather.f2 { --fx: -14px; --fy: 16px; }
.feather.f3 { --fx: 22px; --fy: 8px; }
@keyframes feather {
  to { transform: translate(var(--fx), var(--fy)) rotate(80deg); opacity: 0; }
}

/* landing payoff: send button pulse */
.icon-btn.send.pulse { animation: pulse 0.3s ease-in-out; }
@keyframes pulse {
  50% { transform: scale(1.15); }
}

/* message cost tag */
.msg .meta .cost { font-variant-numeric: tabular-nums; }

/* reduced motion: static bird + plain progress fill; countdowns unchanged */
@media (prefers-reduced-motion: reduce) {
  .pigeon .wing,
  .flight-pigeon,
  .perch .pigeon.idle,
  .perch .pigeon.hop,
  .perch .pigeon.land,
  .icon-btn.send.pulse { animation: none !important; }
  .launch-pigeon, .feather { display: none; }
  .flight-pigeon { display: none; }
  .flight-pigeon-static { display: block; }
  .flight-track .wire-fill { display: block; }
  .pigeon-status { transition: none; }
}

/* ---------- sheets (dialogs) ---------- */

.sheet {
  border: none;
  border-radius: 18px 18px 0 0;
  background: var(--surface);
  color: var(--text);
  width: 100%;
  max-width: 640px;
  margin: auto auto 0;
  padding: 14px 16px calc(18px + env(safe-area-inset-bottom));
  box-shadow: 0 -4px 30px rgb(0 0 0 / 0.25);
}

.sheet::backdrop { background: rgb(10 12 20 / 0.45); }

.sheet-head { display: flex; align-items: center; justify-content: space-between; }
.sheet-head h2 { margin: 0; font-size: 18px; }

.sheet h3 { font-size: 14px; color: var(--text-dim); margin: 16px 0 6px; }

.sheet form { display: flex; flex-direction: column; gap: 8px; }

.share-row {
  display: flex;
  align-items: center;
  gap: 8px;
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 8px 10px;
  margin-top: 10px;
}

.share-row code {
  flex: 1;
  font-size: 12.5px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

#members-list { list-style: none; margin: 0 0 8px; padding: 0; }

#members-list li {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 8px;
  padding: 8px 2px;
  border-bottom: 1px solid var(--border);
  font-size: 15px;
}

#members-list li:last-child { border-bottom: none; }

.member-pigeon { font-size: 13px; color: var(--text-dim); font-variant-numeric: tabular-nums; }
.member-pigeon.out { color: var(--accent); }

#my-location-row { display: flex; flex-direction: column; gap: 6px; margin: 4px 0 12px; }
#btn-leave { margin-top: 8px; width: 100%; }

#poll-options { display: flex; flex-direction: column; gap: 6px; }

/* ---------- toast ---------- */

.toast {
  position: fixed;
  bottom: calc(90px + env(safe-area-inset-bottom));
  left: 50%;
  transform: translateX(-50%);
  background: var(--text);
  color: var(--bg);
  border-radius: 999px;
  padding: 9px 16px;
  font-size: 14px;
  max-width: 90vw;
  z-index: 50;
  box-shadow: var(--shadow);
}
