:root {
  --ink: #f7f3e8;
  --ink-soft: rgba(247, 243, 232, 0.88);
  --glass: rgba(16, 24, 18, 0.46);
  --glass-strong: rgba(13, 19, 15, 0.66);
  --line: rgba(255, 255, 255, 0.18);
  --accent: #f3c56b;
  --accent-ink: #231a0b;
  --error: #ffb9ad;
  --gutter: clamp(12px, 3vw, 28px);
  --radius: 18px;
  --shadow: 0 12px 40px rgba(0, 0, 0, 0.28);
  --font-display: "Iowan Old Style", "Palatino Linotype", Palatino, "Book Antiqua", Georgia, serif;
  --font-ui: ui-sans-serif, system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  color-scheme: dark;
}

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

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

body {
  overflow: hidden;
  overscroll-behavior: none;
  background: linear-gradient(#8fb0c9, #cdbf9f);
  color: var(--ink);
  font-family: var(--font-ui);
  -webkit-font-smoothing: antialiased;
  -webkit-tap-highlight-color: transparent;
}

[hidden] { display: none !important; }

#scene {
  position: fixed;
  inset: 0;
  display: block;
  width: 100vw;
  height: 100vh;
  height: 100dvh;
  touch-action: none;
  outline: none;
}

/* header */
.hud-header {
  position: fixed;
  z-index: 2;
  top: calc(var(--gutter) + env(safe-area-inset-top));
  left: calc(var(--gutter) + env(safe-area-inset-left));
  max-width: min(460px, calc(100vw - 2 * var(--gutter)));
  pointer-events: none;
  text-shadow: 0 1px 2px rgba(0, 0, 0, 0.35), 0 2px 18px rgba(0, 0, 0, 0.3);
}

/* Shade behind the header text so it stays readable over bright sky (at least 4.5:1,
   even with the sun behind it): a deeper sky blue from the top-left corner that fades out
   downward and to the right, like the sky darkening rather than a blob. Stops count from
   the bottom, so the darkest part reaches past the subtitle however the text wraps. */
.hud-header::before {
  --shade: 10, 24, 44;
  content: "";
  position: absolute;
  z-index: -1;
  inset: calc(-1 * (var(--gutter) + env(safe-area-inset-top))) auto -100px calc(-1 * (var(--gutter) + env(safe-area-inset-left)));
  width: calc(100% + var(--gutter) + env(safe-area-inset-left) + 360px);
  background: linear-gradient(rgba(var(--shade), 0.72), rgba(var(--shade), 0.68) calc(100% - 140px), rgba(var(--shade), 0.34) calc(100% - 88px), rgba(var(--shade), 0.1) calc(100% - 34px), rgba(var(--shade), 0));
  -webkit-mask-image: linear-gradient(to right, #000 calc(100% - 360px), transparent);
  mask-image: linear-gradient(to right, #000 calc(100% - 360px), transparent);
  pointer-events: none;
}

/* phones: the same shade across the whole top edge */
@media (max-width: 639px) {
  .hud-header::before {
    width: 100vw;
    -webkit-mask-image: none;
    mask-image: none;
  }
}

.title {
  margin: 0;
  font-family: var(--font-display);
  font-weight: 600;
  font-size: clamp(1.65rem, 1.1rem + 2.2vw, 2.8rem);
  line-height: 1.05;
  letter-spacing: -0.01em;
  text-wrap: balance;
}

.subtitle {
  margin: 0.4rem 0 0;
  font-size: clamp(0.92rem, 0.86rem + 0.3vw, 1.06rem);
  line-height: 1.4;
  color: var(--ink-soft);
}

.count {
  display: inline-flex;
  align-items: center;
  gap: 0.5rem;
  margin: 0;
  padding: 0.42rem 0.85rem 0.42rem 0.62rem;
  border: 1px solid var(--line);
  border-radius: 999px;
  background: var(--glass);
  -webkit-backdrop-filter: blur(12px) saturate(1.2);
  backdrop-filter: blur(12px) saturate(1.2);
  font-size: 0.93rem;
  text-shadow: none;
}

.count-icon { flex: none; width: 16px; height: 16px; color: #a8d672; }
.count strong { font-weight: 700; font-variant-numeric: tabular-nums; }
.count.bump { animation: bump 650ms cubic-bezier(0.2, 0.8, 0.2, 1); }

@keyframes bump {
  30% { transform: scale(1.08); }
}

.count-row {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 8px;
  margin: 0.85rem 0 0;
}

/* which tree of the grove the camera is at */
.tree-label { margin: 6px 0 0 4px; font-size: 0.86rem; font-weight: 600; color: var(--ink-soft); }

/* Find a name */
.find-button {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  min-width: 44px;
  min-height: 44px;
  padding: 0 14px 0 11px;
  border: 1px solid var(--line);
  border-radius: 999px;
  background: var(--glass);
  -webkit-backdrop-filter: blur(12px) saturate(1.2);
  backdrop-filter: blur(12px) saturate(1.2);
  color: var(--ink);
  font: inherit;
  font-size: 0.93rem;
  font-weight: 600;
  text-shadow: none;
  cursor: pointer;
  pointer-events: auto;
}

.find-button:hover, .find-button[aria-expanded="true"], .find-button[aria-pressed="true"] { background: var(--glass-strong); }
.find-button:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
.find-icon { flex: none; width: 18px; height: 18px; }

.find-panel {
  width: min(360px, 100%);
  margin-top: 10px;
  padding: 12px;
  border: 1px solid var(--line);
  border-radius: 14px;
  background: var(--glass-strong);
  -webkit-backdrop-filter: blur(16px) saturate(1.25);
  backdrop-filter: blur(16px) saturate(1.25);
  box-shadow: var(--shadow);
  text-shadow: none;
  pointer-events: auto;
}

.find-label { display: block; margin-bottom: 6px; font-size: 0.86rem; font-weight: 600; color: var(--ink-soft); }

.find-panel input {
  width: 100%;
  min-height: 44px;
  padding: 0 12px;
  border: 1px solid rgba(255, 255, 255, 0.26);
  border-radius: 10px;
  background: rgba(255, 255, 255, 0.1);
  color: var(--ink);
  font: inherit;
  font-size: 16px; /* 16px stops iOS from zooming into the field */
}

.find-panel input::placeholder { color: rgba(247, 243, 232, 0.58); }
.find-panel input:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; border-color: transparent; }
.find-results { margin: 6px 0 0; padding: 0; list-style: none; }

.find-result {
  display: block;
  width: 100%;
  min-height: 40px;
  padding: 8px 10px;
  border: 0;
  border-radius: 8px;
  background: none;
  color: var(--ink);
  font: inherit;
  font-size: 0.95rem;
  text-align: left;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  cursor: pointer;
}

.find-result:hover { background: rgba(255, 255, 255, 0.12); }
.find-result:focus-visible { background: rgba(255, 255, 255, 0.12); outline: 2px solid var(--accent); outline-offset: -2px; }
.find-empty { margin: 8px 2px 0; font-size: 0.9rem; color: var(--ink-soft); }

/* Why we're growing: a button like Find's (a shorter label where the header is narrow; see the
   phone rules), and a panel like Find's, which scrolls if its text is long */
.label-short { display: none; }
.about-panel { width: min(420px, 100%); max-height: min(60vh, 460px); overflow-y: auto; }
.about-head { display: flex; align-items: flex-start; justify-content: space-between; gap: 8px; }
.about-title { margin: 6px 0 0 2px; font-family: var(--font-display); font-size: 1.2rem; font-weight: 600; line-height: 1.25; color: var(--ink); }

.about-close {
  flex: none;
  display: inline-grid;
  place-items: center;
  width: 44px;
  height: 44px;
  margin: -6px -6px 0 0;
  border: 0;
  border-radius: 999px;
  background: transparent;
  color: var(--ink-soft);
  cursor: pointer;
}

.about-close svg { width: 20px; height: 20px; }
.about-close:hover { background: rgba(255, 255, 255, 0.12); color: var(--ink); }
.about-close:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
.about-text p { margin: 10px 2px 0; font-size: 0.95rem; line-height: 1.5; color: var(--ink-soft); }

/* join card */
.join-card {
  position: fixed;
  z-index: 3;
  left: calc(var(--gutter) + env(safe-area-inset-left));
  bottom: calc(var(--gutter) + env(safe-area-inset-bottom));
  width: min(380px, calc(100vw - 2 * var(--gutter)));
  margin: 0;
  padding: 16px 16px 14px;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: var(--glass-strong);
  -webkit-backdrop-filter: blur(16px) saturate(1.25);
  backdrop-filter: blur(16px) saturate(1.25);
  box-shadow: var(--shadow);
}

/* the card's buttons: Add your name, then Contribute, Share and Find my leaf */
.add-button { width: 100%; }
.join-actions { display: flex; gap: 8px; margin-top: 8px; }
.join-actions > * { flex: 1; }

.ghost-button {
  min-height: 44px;
  padding: 0 12px;
  border: 1px solid rgba(255, 255, 255, 0.36);
  border-radius: 12px;
  background: transparent;
  color: var(--ink);
  font: inherit;
  font-weight: 600;
  cursor: pointer;
}

.ghost-button:hover { background: rgba(255, 255, 255, 0.1); }
.ghost-button:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
#mine-button { border-color: rgba(243, 197, 107, 0.6); color: var(--accent); }

.link-button {
  display: block;
  margin: 10px auto 0;
  padding: 2px 4px;
  border: 0;
  background: none;
  color: inherit;
  font: inherit;
  font-size: 0.9rem;
  text-decoration: underline;
  text-underline-offset: 3px;
  cursor: pointer;
}

.link-button:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; border-radius: 4px; }

/* after adding a name: the whole tree, the new leaf ringed and named, and its numbers */
.leaf-card .sheet-title { color: var(--ink); }
.leaf-card .sheet-close { color: var(--ink-soft); }
.leaf-numbers { display: flex; gap: 8px; margin: 10px 0 2px; }
.leaf-numbers p {
  flex: 1;
  margin: 0;
  padding: 6px 8px;
  border-radius: 12px;
  background: rgba(255, 255, 255, 0.08);
  text-align: center;
}

.leaf-numbers strong { display: block; color: var(--accent); font-size: 1.25rem; font-variant-numeric: tabular-nums; }
.leaf-numbers span { font-size: 0.8rem; color: var(--ink-soft); }

.leaf-marker { position: fixed; z-index: 2; left: 0; top: 0; pointer-events: none; }
.leaf-ring {
  position: absolute;
  left: -16px;
  top: -16px;
  width: 32px;
  height: 32px;
  border: 2px solid #ffe48a;
  border-radius: 50%;
  box-shadow: 0 0 14px 4px rgba(255, 228, 138, 0.75);
}

.leaf-tag {
  position: absolute;
  left: 0;
  bottom: 24px;
  transform: translateX(-50%);
  padding: 3px 9px;
  border-radius: 8px;
  background: #f7f4ea;
  color: #1f2a1e;
  font-size: 0.84rem;
  font-weight: 700;
  white-space: nowrap;
}

.leaf-tag::after {
  content: '';
  position: absolute;
  left: 50%;
  bottom: -5px;
  margin-left: -5px;
  border: 5px solid transparent;
  border-bottom: 0;
  border-top-color: #f7f4ea;
}

/* the pop-ups: Add your leaf, Help the tree grow. Bottom sheets on phones, centred on wider screens. */
.sheet {
  width: min(420px, calc(100vw - 2 * var(--gutter)));
  max-height: calc(100dvh - 2 * var(--gutter));
  margin: auto;
  padding: 16px 18px 18px;
  overflow-y: auto;
  border: 0;
  border-radius: var(--radius);
  background: #f7f4ea;
  color: #1f2a1e;
  box-shadow: var(--shadow);
}

.sheet::backdrop { background: rgba(8, 14, 10, 0.55); }

.sheet-head { display: flex; align-items: flex-start; justify-content: space-between; gap: 8px; }
.sheet-title { margin: 2px 0 0; font-family: var(--font-display); font-size: 1.3rem; font-weight: 600; line-height: 1.25; }
.sheet-close {
  flex: none;
  display: grid;
  place-items: center;
  width: 36px;
  height: 36px;
  margin: -4px -6px 0 0;
  border: 0;
  border-radius: 50%;
  background: transparent;
  color: #5e6b5a;
  cursor: pointer;
}

.sheet-close svg { width: 20px; height: 20px; }
.sheet-close:hover { background: rgba(0, 0, 0, 0.06); }
.sheet-close:focus-visible { outline: 2px solid #3f6a2a; outline-offset: 2px; }
.sheet-lead { margin: 4px 0 6px; font-size: 0.92rem; line-height: 1.45; color: #4d5a49; }

.field-label { display: block; margin: 12px 0 4px; font-size: 0.86rem; font-weight: 700; }
.optional { font-weight: 400; color: #5e6b5a; }
.sheet input {
  width: 100%;
  min-height: 46px;
  padding: 0 12px;
  border: 1px solid #c4bfae;
  border-radius: 10px;
  background: #fff;
  color: #1f2a1e;
  font: inherit;
  font-size: 16px; /* 16px stops iOS from zooming into the field */
}

.sheet input::placeholder { color: #8b8a80; }
.sheet input:focus-visible { outline: 2px solid #3f6a2a; outline-offset: 1px; border-color: transparent; }
.sheet input[aria-invalid="true"] { border-color: #b3261e; }
.phone-row { display: flex; gap: 8px; }
.phone-row #join-code { flex: none; width: 76px; text-align: center; }
.field-error, .form-error { margin: 5px 2px 0; font-size: 0.86rem; color: #b3261e; }
.form-error { margin-top: 10px; font-weight: 600; }
.trust { margin: 14px 0 0; padding: 8px 10px; border-radius: 10px; background: #e3eed5; color: #2f4426; font-size: 0.86rem; line-height: 1.45; }
.removal { margin: 8px 2px 0; font-size: 0.8rem; color: #5e6b5a; }
.removal a { color: inherit; }
.sheet .join-button { margin-top: 14px; }
.sheet .join-button[aria-busy="true"] { opacity: 0.7; cursor: progress; }
.wide { display: block; width: 100%; text-align: center; }
.sheet .ghost-button { margin-top: 10px; border-color: #1f2a1e; color: #1f2a1e; }
.sheet .link-button { display: inline; margin: 0; }

.amounts { display: flex; gap: 6px; margin-top: 10px; }
.amounts button {
  flex: 1;
  min-height: 40px;
  border: 1px solid #1f2a1e;
  border-radius: 999px;
  background: transparent;
  color: #1f2a1e;
  font: inherit;
  font-weight: 700;
  cursor: pointer;
}

.amounts button[aria-pressed="true"] { background: #1f2a1e; color: #f7f4ea; }
.amounts button:focus-visible { outline: 2px solid #3f6a2a; outline-offset: 2px; }
.other-row { display: flex; align-items: center; gap: 6px; margin-top: 8px; font-weight: 700; }
.pay-link { display: grid; place-items: center; text-decoration: none; }
.qr { display: block; width: 180px; height: 180px; margin: 12px auto 6px; border: 8px solid #fff; border-radius: 6px; object-fit: contain; background: #fff; }
.upi-line { margin: 4px 0 0; text-align: center; font-size: 0.86rem; color: #4d5a49; overflow-wrap: anywhere; }

@media (max-width: 639px) {
  .sheet {
    width: 100%;
    max-width: none;
    max-height: 92dvh;
    margin: auto 0 0;
    border-radius: var(--radius) var(--radius) 0 0;
    padding-bottom: calc(18px + env(safe-area-inset-bottom));
  }
}

.join-button {
  min-width: 92px;
  min-height: 46px;
  padding: 0 18px;
  border: 0;
  border-radius: 12px;
  background: var(--accent);
  color: var(--accent-ink);
  font: inherit;
  font-weight: 700;
  cursor: pointer;
  transition: transform 120ms ease, filter 120ms ease, background-color 200ms ease;
}

.join-button:hover { filter: brightness(1.06); }
.join-button:active { transform: translateY(1px) scale(0.98); }
.join-button:focus-visible { outline: 2px solid #fff; outline-offset: 2px; }

/* toast */
.toast {
  position: fixed;
  z-index: 4;
  top: calc(var(--gutter) + env(safe-area-inset-top));
  left: 50%;
  max-width: calc(100vw - 2 * var(--gutter));
  padding: 10px 18px;
  border: 1px solid var(--line);
  border-radius: 999px;
  background: var(--glass-strong);
  -webkit-backdrop-filter: blur(14px);
  backdrop-filter: blur(14px);
  box-shadow: var(--shadow);
  font-size: 0.95rem;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  opacity: 0;
  transform: translate(-50%, -10px);
  transition: opacity 280ms ease, transform 280ms ease;
  pointer-events: none;
}

.toast.show { opacity: 1; transform: translate(-50%, 0); }

/* back to the whole tree, while exploring */
.back-button {
  position: fixed;
  z-index: 3;
  top: calc(var(--gutter) + env(safe-area-inset-top) + 54px);
  left: 50%;
  display: inline-flex;
  align-items: center;
  gap: 6px;
  min-height: 44px;
  padding: 0 18px 0 12px;
  border: 1px solid var(--line);
  border-radius: 999px;
  background: var(--glass-strong);
  -webkit-backdrop-filter: blur(14px);
  backdrop-filter: blur(14px);
  box-shadow: var(--shadow);
  color: var(--ink);
  font: inherit;
  font-size: 0.95rem;
  font-weight: 600;
  cursor: pointer;
  transform: translateX(-50%);
}

.back-button:hover { background: rgba(13, 19, 15, 0.8); }
.back-button:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
.back-icon { flex: none; width: 18px; height: 18px; }

/* whose leaf: the name tooltip */
.name-tip {
  position: fixed;
  z-index: 5;
  left: 0;
  top: 0;
  padding: 7px 12px;
  border: 1px solid var(--line);
  border-radius: 12px;
  background: rgba(12, 18, 14, 0.8);
  box-shadow: var(--shadow);
  font-size: 0.92rem;
  white-space: nowrap;
  transform: translate(-50%, calc(-100% - 14px));
  pointer-events: none;
}

.name-tip strong { display: block; font-weight: 700; }
.name-tip span { font-size: 0.8rem; color: var(--ink-soft); }

.preview-badge {
  position: fixed;
  z-index: 3;
  top: calc(var(--gutter) + env(safe-area-inset-top));
  right: calc(var(--gutter) + env(safe-area-inset-right));
  padding: 6px 12px;
  border-radius: 999px;
  background: rgba(243, 197, 107, 0.94);
  color: var(--accent-ink);
  font-size: 0.8rem;
  font-weight: 700;
}

/* loading and fallback */
.loading {
  position: fixed;
  z-index: 10;
  inset: 0;
  display: grid;
  place-items: center;
  background: linear-gradient(#8fb3cf, #dccaa6 70%, #b39f78);
  transition: opacity 900ms ease;
}

.loading.hidden { opacity: 0; pointer-events: none; }
.loading-inner { text-align: center; color: #1f2a1a; font-family: var(--font-display); font-size: 1.2rem; }
.loading svg { width: 56px; height: 56px; transform-origin: 50% 100%; animation: sway 2.4s ease-in-out infinite; }

@keyframes sway {
  0%, 100% { transform: rotate(-4deg); }
  50% { transform: rotate(4deg); }
}

.fallback {
  position: fixed;
  z-index: 1;
  inset: 0;
  display: grid;
  place-items: center;
  padding: var(--gutter);
  pointer-events: none;
}

.fallback p {
  max-width: 420px;
  margin: 0;
  padding: 18px 20px;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: var(--glass-strong);
  line-height: 1.5;
  text-align: center;
}

/* phones: header across the top, join card as a bottom sheet */
@media (max-width: 639px) {
  .hud-header { max-width: calc(100vw - 2 * var(--gutter)); }
  .is-preview .hud-header { max-width: calc(100vw - 2 * var(--gutter) - 150px); }
  /* the header centred over the tree (beside the preview badge it keeps to the left); the Find and Why
     panels centred under it, their text still reading from the left */
  body:not(.is-preview) .hud-header { right: calc(var(--gutter) + env(safe-area-inset-right)); text-align: center; }
  body:not(.is-preview) .count-row { justify-content: center; }
  body:not(.is-preview) .tree-label { margin-left: 0; }
  body:not(.is-preview) .find-panel { margin-right: auto; margin-left: auto; text-align: left; }
  .join-card {
    left: calc(10px + env(safe-area-inset-left));
    right: calc(10px + env(safe-area-inset-right));
    bottom: calc(10px + env(safe-area-inset-bottom));
    width: auto;
    padding: 14px;
    border-radius: 20px;
  }
  .toast { top: auto; bottom: calc(136px + env(safe-area-inset-bottom)); }
  .back-button { top: auto; bottom: calc(188px + env(safe-area-inset-bottom)); }
  /* beside the preview badge the header is narrow: "Why?" keeps the info button next to Find */
  .is-preview .label-long { display: none; }
  .is-preview .label-short { display: inline; }
}

/* small phones: the short label, so Find and the info button share a row */
@media (max-width: 379px) {
  .label-long { display: none; }
  .label-short { display: inline; }
}

/* tablets: top-centre would sit on the header's Find button, so Back to tree keeps to the
   right, below the preview badge */
@media (min-width: 640px) and (max-width: 899px) {
  .back-button { left: auto; right: calc(var(--gutter) + env(safe-area-inset-right)); transform: none; }
}

/* phones held sideways: compact header and a side card */
@media (max-height: 520px) and (orientation: landscape) {
  .subtitle { display: none; }
  .title { font-size: 1.4rem; }
  .count-row { margin-top: 0.5rem; }
  .join-card { left: auto; right: calc(var(--gutter) + env(safe-area-inset-right)); width: min(340px, 44vw); padding: 12px; }
  .preview-badge { top: auto; right: auto; bottom: calc(var(--gutter) + env(safe-area-inset-bottom)); left: calc(var(--gutter) + env(safe-area-inset-left)); }
  .back-button { top: calc(var(--gutter) + env(safe-area-inset-top)); right: calc(var(--gutter) + env(safe-area-inset-right)); bottom: auto; left: auto; transform: none; }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation: none !important; transition-duration: 1ms !important; }
}
