:root {
  --bg: #fbf4e8;
  --surface: #fffdf8;
  --text: #2a1d17;
  --muted: #7a685d;
  --line: #eadfce;
  --primary: #b3261e;
  --primary-text: #fff;
  --accent: #1f6f50;
  --gold: #e0a526;
  --radius: 16px;
  color-scheme: light dark;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #1b1412;
    --surface: #271d1a;
    --text: #f6ece2;
    --muted: #b6a498;
    --line: #3c2e29;
    --primary: #e2473d;
    --accent: #4fb48c;
  }
}

* { box-sizing: border-box; }

html { -webkit-text-size-adjust: 100%; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 17px/1.45 ui-rounded, system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  -webkit-tap-highlight-color: transparent;
}

#app {
  max-width: 480px;
  margin: 0 auto;
  padding: 16px 16px calc(32px + env(safe-area-inset-bottom));
  display: flex;
  flex-direction: column;
  gap: 16px;
  min-height: 100dvh;
}

h1, h2, p { margin: 0; }
h1 { font-size: 1.7rem; line-height: 1.15; letter-spacing: -0.01em; overflow-wrap: anywhere; }
h2 { font-size: 1.05rem; }

.muted { color: var(--muted); font-size: 0.95rem; }
.center { text-align: center; }
.stack { display: flex; flex-direction: column; gap: 12px; }
.big-emoji { font-size: 3.2rem; line-height: 1; }

.banner {
  background: color-mix(in srgb, var(--gold) 22%, transparent);
  border-radius: 10px;
  padding: 8px 12px;
  font-size: 0.85rem;
  text-align: center;
}

.hero { text-align: center; display: flex; flex-direction: column; gap: 8px; padding: 20px 0 4px; }

.topbar { display: flex; align-items: center; gap: 8px; padding: 4px 0; }
.back {
  flex: none;
  width: 44px; height: 44px;
  display: grid; place-items: center;
  font-size: 2rem; line-height: 1;
  color: var(--text); text-decoration: none;
  border-radius: 50%;
}
.back:active { background: var(--line); }

.card {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 16px;
}
.card.center { display: flex; flex-direction: column; align-items: center; gap: 12px; }

label { display: flex; flex-direction: column; gap: 6px; font-weight: 600; font-size: 0.95rem; }

input, select {
  font: inherit;
  color: inherit;
  background: var(--bg);
  border: 1.5px solid var(--line);
  border-radius: 12px;
  min-height: 48px;
  padding: 0 12px;
  width: 100%;
  min-width: 0;
}
input:focus-visible, select:focus-visible, .btn:focus-visible, .icon-btn:focus-visible, a:focus-visible {
  outline: 3px solid color-mix(in srgb, var(--accent) 60%, transparent);
  outline-offset: 2px;
}

.btn {
  font: inherit;
  font-weight: 700;
  display: inline-flex; align-items: center; justify-content: center;
  min-height: 48px;
  padding: 0 18px;
  border-radius: 14px;
  border: 1.5px solid var(--line);
  background: var(--surface);
  color: var(--text);
  text-decoration: none;
  cursor: pointer;
  transition: transform 0.08s;
}
.btn:active { transform: scale(0.97); }
.btn:disabled { opacity: 0.45; cursor: not-allowed; }
.btn.primary { background: var(--primary); border-color: var(--primary); color: var(--primary-text); }
.btn.big { min-height: 58px; font-size: 1.15rem; width: 100%; box-shadow: 0 6px 18px color-mix(in srgb, var(--primary) 30%, transparent); }
.btn.small { min-height: 40px; padding: 0 12px; font-size: 0.9rem; }
.btn.ghost { border-color: transparent; background: none; color: var(--muted); }
.btn.whatsapp { background: #1fa855; border-color: #1fa855; color: #fff; }

.icon-btn {
  flex: none;
  width: 40px; height: 40px;
  border: 0; border-radius: 50%;
  background: none; color: var(--muted);
  font-size: 1rem; cursor: pointer;
}
.icon-btn:active { background: var(--line); }

.list { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 8px; }
.row {
  display: flex; align-items: center; gap: 8px;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 14px;
  padding: 8px 8px 8px 14px;
  min-height: 56px;
}
.row.wrap { flex-wrap: wrap; padding: 12px 14px; }
.row-main { flex: 1 1 auto; min-width: 0; overflow-wrap: anywhere; font-weight: 600; }
.row-actions { display: flex; gap: 8px; flex: 1 1 100%; }
.row-actions .btn { flex: 1; }
.link { color: inherit; text-decoration: none; padding: 8px 0; }

.family { flex: 0 1 150px; min-width: 0; }
.family.editing { flex: 1 1 100%; display: flex; align-items: center; gap: 6px; }
.family.editing input { min-height: 40px; font-size: 0.9rem; }
.family-select { min-height: 40px; font-size: 0.9rem; padding: 0 8px; }
.row:has(.family.editing), .add-row:has(.family.editing) { flex-wrap: wrap; }
.row:has(.family.editing) > .icon-btn { display: none; }

.dialog {
  width: min(360px, calc(100vw - 32px));
  border: 1px solid var(--line); border-radius: var(--radius);
  background: var(--surface); color: var(--text);
  padding: 20px;
}
.dialog::backdrop { background: rgb(0 0 0 / 0.5); }
.dialog p { margin-bottom: 16px; }
.dialog-actions { display: flex; gap: 8px; margin-top: 16px; }
.dialog-actions .btn { flex: 1; }
.add-row { display: flex; gap: 8px; }
.add-name { flex: 1 1 auto; min-width: 0; }
.add-row .family-select { min-height: 48px; }

.chip {
  display: inline-block;
  font-size: 0.78rem; font-weight: 700;
  padding: 2px 9px;
  border-radius: 999px;
  background: hsl(var(--hue) 65% 50% / 0.18);
  color: hsl(var(--hue) 60% 32%);
  vertical-align: middle;
}
@media (prefers-color-scheme: dark) {
  .chip { color: hsl(var(--hue) 70% 76%); }
}

.status { font-size: 0.85rem; font-weight: 500; color: var(--muted); margin-top: 2px; }
.status.ok { color: var(--accent); }

.hint { font-size: 0.9rem; color: var(--primary); text-align: center; }

.sticky-action {
  position: sticky;
  bottom: 0;
  margin-top: auto;
  padding: 12px 0 calc(12px + env(safe-area-inset-bottom));
  background: linear-gradient(to top, var(--bg) 70%, transparent);
}

/* Rodapé */
.footer {
  margin-top: auto;
  padding-top: 24px;
  display: flex; flex-wrap: wrap; justify-content: center; gap: 4px 14px;
  font-size: 0.8rem; color: var(--muted); text-align: center;
}
.footer a { color: inherit; text-decoration: underline; text-underline-offset: 2px; }
.sticky-action { margin-top: 0; }

/* Página pública do sorteio */
.name-btn { width: 100%; min-height: 58px; justify-content: space-between; font-size: 1.1rem; }
.name-btn .status { margin: 0; }
.row-actions { flex-wrap: wrap; }

/* Pesquisa */
.result-btn {
  font: inherit; font-weight: 600; color: inherit; text-align: left;
  background: none; border: 0; padding: 4px 0; cursor: pointer;
  display: flex; flex-direction: column;
}
.result-link { display: flex; flex-direction: column; }
label small { font-weight: 400; }

/* Depuração (?debug) */
.debug { border-style: dashed; border-color: var(--gold); }
.debug h3 { margin: 4px 0 0; font-size: 0.9rem; }
.table-scroll { overflow-x: auto; }
.debug table { border-collapse: collapse; font-size: 0.8rem; font-variant-numeric: tabular-nums; }
.debug th, .debug td { border: 1px solid var(--line); padding: 4px 6px; text-align: center; white-space: nowrap; }
.debug tbody th { text-align: left; }
.debug td.zero { color: var(--muted); background: color-mix(in srgb, var(--line) 50%, transparent); }
.pairs { margin: 0; padding-left: 20px; font-size: 0.9rem; }

/* Roda */
.wheel-box { position: relative; width: min(100%, 360px); margin: 8px auto; }
.wheel { display: block; width: 100%; aspect-ratio: 1; cursor: pointer; touch-action: manipulation; }
.pointer {
  position: absolute; z-index: 1;
  top: -6px; left: 50%;
  transform: translateX(-50%);
  width: 0; height: 0;
  border-left: 15px solid transparent;
  border-right: 15px solid transparent;
  border-top: 30px solid var(--text);
  filter: drop-shadow(0 2px 2px rgb(0 0 0 / 0.3));
}

.result { border: 2px solid var(--gold); animation: pop 0.45s cubic-bezier(0.2, 1.4, 0.4, 1); }
.result[hidden] { display: none; }
.result-name { font-size: 2.3rem; font-weight: 800; line-height: 1.1; color: var(--primary); overflow-wrap: anywhere; }
@keyframes pop { from { transform: scale(0.7); opacity: 0; } }

.confetti { position: fixed; inset: 0; width: 100%; height: 100%; pointer-events: none; z-index: 10; }

.toast {
  position: fixed; z-index: 20;
  left: 50%; top: calc(16px + env(safe-area-inset-top));
  transform: translateX(-50%);
  max-width: calc(100vw - 32px);
  background: var(--text); color: var(--bg);
  padding: 10px 16px;
  border-radius: 12px;
  font-size: 0.95rem;
  box-shadow: 0 8px 24px rgb(0 0 0 / 0.25);
}

@media (prefers-reduced-motion: reduce) {
  .result { animation: none; }
  .btn { transition: none; }
}
