/* Radici — tema "carta e inchiostro" */
* { box-sizing: border-box; margin: 0; padding: 0; }
:root {
  --carta: #f6f1e5;
  --carta-scura: #eee7d6;
  --inchiostro: #2c2822;
  --inchiostro-tenue: #6d655a;
  --linea: #8d846f;
  --bianco: #fffdf8;
  --verde: #3e6b4f;
  --verde-scuro: #2f523c;
  --rosso: #a04545;
  --oro: #b98a2e;
  --ombra: 0 2px 10px rgba(60, 50, 30, .14);
  --raggio: 10px;
}
html, body { height: 100%; overflow: hidden; overscroll-behavior: none; }
body {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  background: var(--carta);
  color: var(--inchiostro);
  /* iOS: il body fissato impedisce alla barra indirizzi in basso di
     spingere l'header fuori dallo schermo */
  position: fixed;
  inset: 0;
  width: 100%;
}
h1, h2, h3, .brand { font-family: Georgia, "Times New Roman", serif; }
button { font: inherit; cursor: pointer; }
input, select, textarea { font: inherit; color: inherit; }
a { color: var(--verde); }

#app { position: fixed; inset: 0; display: flex; flex-direction: column; }

/* ---------------- barra superiore ---------------- */
.topbar {
  display: flex; align-items: center; gap: 10px;
  padding: 8px 14px; background: var(--bianco);
  border-bottom: 1px solid #ddd3bd; box-shadow: var(--ombra);
  z-index: 20; flex-wrap: wrap;
}
.brand { font-size: 20px; font-weight: bold; color: var(--verde-scuro); text-decoration: none; display: flex; align-items: center; gap: 6px; }
.topbar select, .topbar input[type="search"] {
  padding: 7px 10px; border: 1px solid #cfc5ac; border-radius: 8px; background: var(--bianco);
}
.topbar .search-wrap { position: relative; flex: 1; min-width: 140px; max-width: 340px; }
.topbar .search-wrap input { width: 100%; }
.search-results {
  position: absolute; top: 105%; left: 0; right: 0; background: var(--bianco);
  border: 1px solid #cfc5ac; border-radius: 8px; box-shadow: var(--ombra);
  max-height: 260px; overflow: auto; z-index: 50;
}
.search-results button { display: block; width: 100%; text-align: left; padding: 8px 12px; border: 0; background: none; }
.search-results button:hover { background: var(--carta-scura); }
.iconbtn {
  border: 1px solid #cfc5ac; background: var(--bianco); border-radius: 8px;
  padding: 7px 11px; color: var(--inchiostro); position: relative;
}
.iconbtn:hover { background: var(--carta-scura); }
.badge {
  position: absolute; top: -6px; right: -6px; background: var(--rosso); color: #fff;
  font-size: 11px; border-radius: 10px; padding: 1px 5px; min-width: 17px; text-align: center;
}

/* ---------------- tela dell'albero ---------------- */
#canvas { flex: 1; position: relative; overflow: hidden; cursor: grab; touch-action: none;
  background-image: radial-gradient(#d9cfb8 1px, transparent 1.2px); background-size: 26px 26px; }
#canvas.grabbing { cursor: grabbing; }
#world { position: absolute; top: 0; left: 0; transform-origin: 0 0; }
#edges { position: absolute; top: 0; left: 0; overflow: visible; pointer-events: none; }
#edges path { stroke: var(--linea); stroke-width: 2; fill: none; }
#edges path.stub, #edges path.rail, #edges path.drop { stroke-width: 1.8; }
#edges circle.unknown { fill: none; stroke: var(--linea); stroke-dasharray: 3 3; }
#edges path.bridge { stroke: var(--oro); stroke-width: 2.4; stroke-dasharray: 7 6; }
#edges circle.famdot { fill: var(--linea); stroke: var(--carta); stroke-width: 1.5; }
#edges g.ghost path, #edges g.ghost circle { opacity: .38; }

.card {
  position: absolute; width: 172px; height: 72px; background: var(--bianco);
  border: 1.5px solid #c9bfa6; border-radius: var(--raggio); box-shadow: var(--ombra);
  display: flex; align-items: center; gap: 9px; padding: 8px 10px; user-select: none;
  transition: box-shadow .15s, border-color .15s, opacity .3s;
}
.card:hover { border-color: var(--verde); }
.card.selected { border-color: var(--verde); box-shadow: 0 0 0 3px rgba(62, 107, 79, .25), var(--ombra); }
.card.me { border-color: var(--oro); }
.card.ghost { opacity: .45; }
.card.ghost:hover { opacity: .85; }
.card.dim { opacity: .16; }
.card.reordering { box-shadow: 0 10px 26px rgba(60, 50, 30, .35); border-color: var(--oro); z-index: 5; cursor: grabbing; }
.card.sib-target { border-style: dashed; border-color: var(--oro); }
/* vista compatta: pacchetti di fratelli senza discendenza */
.card.pack { border-style: dashed; background: #fbf7ec; opacity: .8; cursor: pointer; }
.card.pack:hover { opacity: 1; border-color: var(--verde); }
.pack-fan { display: flex; flex: none; }
.pack-fan .avatar {
  width: 34px; height: 34px; border-radius: 50%; flex: none;
  display: flex; align-items: center; justify-content: center;
  color: #fff; font-size: 12px; font-weight: 700; border: 2px solid var(--bianco);
}
.pack-fan .avatar + .avatar { margin-left: -14px; }
.card .unpack {
  position: absolute; top: -9px; right: -9px; width: 21px; height: 21px;
  border-radius: 50%; border: 1px solid #cfc5ac; background: var(--bianco);
  color: var(--inchiostro-tenue); font-size: 14px; line-height: 1; padding: 0;
  box-shadow: var(--ombra); cursor: pointer;
}
.card .unpack:hover { color: var(--rosso); border-color: var(--rosso); }
.drawer-head .parentela { color: var(--verde-scuro); font-weight: 600; }
.card .avatar {
  width: 44px; height: 44px; border-radius: 50%; flex: none; overflow: hidden;
  display: flex; align-items: center; justify-content: center;
  font-weight: 700; font-size: 15px; color: #fff; background: var(--verde);
}
.card .avatar img { width: 100%; height: 100%; object-fit: cover; }
.card .info { min-width: 0; }
.card .nome { font-weight: 650; font-size: 13.5px; line-height: 1.15; overflow: hidden; text-overflow: ellipsis; display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; }
.card .sub { font-size: 11px; color: var(--inchiostro-tenue); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.card .lock { position: absolute; top: 4px; right: 7px; font-size: 11px; }

.canvas-controls {
  position: absolute; right: 14px; bottom: calc(18px + env(safe-area-inset-bottom, 0px));
  display: flex; flex-direction: column; gap: 7px; z-index: 10;
}
.canvas-controls button {
  width: 40px; height: 40px; border-radius: 50%; border: 1px solid #cfc5ac;
  background: var(--bianco); box-shadow: var(--ombra); font-size: 17px;
}
.canvas-hint {
  position: absolute; left: 14px; bottom: calc(16px + env(safe-area-inset-bottom, 0px)); font-size: 12px; color: var(--inchiostro-tenue);
  background: rgba(255, 253, 248, .85); padding: 5px 10px; border-radius: 8px; z-index: 10;
}
.ghost-label { position: absolute; font-size: 12px; color: var(--oro); font-style: italic; }

/* ---------------- pannello laterale (scheda persona) ---------------- */
#drawer {
  position: absolute; top: 0; right: 0; bottom: 0; width: 380px; max-width: 100vw;
  background: var(--bianco); border-left: 1px solid #ddd3bd; box-shadow: -6px 0 18px rgba(60, 50, 30, .12);
  transform: translateX(102%); transition: transform .22s ease; z-index: 30;
  display: flex; flex-direction: column;
}
#drawer.open { transform: translateX(0); }
.drawer-head { padding: 16px; border-bottom: 1px solid #eee4cf; display: flex; gap: 12px; align-items: center; }
.drawer-head .avatar {
  width: 64px; height: 64px; border-radius: 50%; overflow: hidden; flex: none;
  display: flex; align-items: center; justify-content: center; font-size: 22px; font-weight: 700; color: #fff; background: var(--verde);
}
.drawer-head .avatar img { width: 100%; height: 100%; object-fit: cover; }
.drawer-head h2 { font-size: 20px; line-height: 1.15; }
.drawer-head .vita { color: var(--inchiostro-tenue); font-size: 13px; margin-top: 2px; }
.drawer-close { margin-left: auto; border: 0; background: none; font-size: 22px; color: var(--inchiostro-tenue); align-self: flex-start; }
.drawer-body { flex: 1; overflow-y: auto; padding: 14px 16px 26px; }
.drawer-body h3 { font-size: 14px; margin: 18px 0 7px; color: var(--verde-scuro); border-bottom: 1px solid #eee4cf; padding-bottom: 4px; }
.drawer-body h3:first-child { margin-top: 0; }
.riga { display: flex; gap: 8px; font-size: 13.5px; padding: 3px 0; }
.riga .et { color: var(--inchiostro-tenue); min-width: 92px; flex: none; }
.bio { font-size: 13.5px; white-space: pre-wrap; line-height: 1.45; }
.nota-avviso { font-size: 12.5px; background: #fdf6e3; border: 1px solid #ecd9a8; padding: 7px 9px; border-radius: 8px; margin: 8px 0; }
.chips { display: flex; flex-wrap: wrap; gap: 6px; }
.chip {
  border: 1px solid #cfc5ac; background: var(--carta); border-radius: 16px;
  padding: 4px 11px; font-size: 12.5px; color: var(--inchiostro);
}
.chip:hover { background: var(--carta-scura); }
.chip.ghost { opacity: .6; font-style: italic; }
.unione { margin-bottom: 10px; padding: 8px 10px; background: var(--carta); border-radius: 8px; position: relative; }
.unione .unione-del {
  position: absolute; top: 5px; right: 7px; border: 0; background: none;
  color: var(--inchiostro-tenue); font-size: 13px; cursor: pointer; padding: 2px 4px;
}
.unione .unione-del:hover { color: var(--rosso); }
.iconbtn:disabled { opacity: .35; cursor: default; }
.unione .tipo { font-size: 11.5px; color: var(--inchiostro-tenue); margin-bottom: 4px; }
.drawer-actions { display: flex; flex-wrap: wrap; gap: 7px; margin-top: 14px; }

.fonte { padding: 7px 9px; background: var(--carta); border-radius: 8px; margin-bottom: 6px; font-size: 13px; }
.fonte .tipo { color: var(--inchiostro-tenue); font-size: 11.5px; }
.fonte .del { float: right; border: 0; background: none; color: var(--rosso); }

/* ---------------- bottoni e form ---------------- */
.btn {
  border: 1px solid var(--verde); background: var(--verde); color: #fff;
  border-radius: 8px; padding: 8px 14px; font-size: 14px;
}
.btn:hover { background: var(--verde-scuro); }
.btn.secondario { background: var(--bianco); color: var(--verde-scuro); border-color: #b3a888; }
.btn.secondario:hover { background: var(--carta-scura); }
.btn.pericolo { background: var(--bianco); color: var(--rosso); border-color: var(--rosso); }
.btn.pericolo:hover { background: #fbeeee; }
.btn.mini { padding: 5px 10px; font-size: 12.5px; }

.form-griglia { display: grid; grid-template-columns: 1fr 1fr; gap: 9px; }
.form-griglia .full { grid-column: 1 / -1; }
label.campo { display: block; font-size: 12px; color: var(--inchiostro-tenue); }
label.campo input, label.campo select, label.campo textarea {
  width: 100%; margin-top: 3px; padding: 8px 9px; border: 1px solid #cfc5ac; border-radius: 8px; background: var(--bianco);
  font-size: 14px;
}
label.campo textarea { min-height: 74px; resize: vertical; }
.form-azioni { display: flex; gap: 8px; margin-top: 12px; flex-wrap: wrap; }
.errore { color: var(--rosso); font-size: 13px; margin-top: 8px; }
.ok-msg { color: var(--verde-scuro); font-size: 13px; margin-top: 8px; }

/* ---------------- modali ---------------- */
.modal-overlay {
  position: fixed; inset: 0; background: rgba(44, 40, 34, .45); z-index: 60;
  display: flex; align-items: center; justify-content: center; padding: 16px;
}
.modal {
  background: var(--bianco); border-radius: 14px; box-shadow: 0 12px 40px rgba(0, 0, 0, .25);
  width: 100%; max-width: 460px; max-height: 90vh; overflow-y: auto; padding: 20px;
}
.modal h2 { font-size: 19px; margin-bottom: 12px; color: var(--verde-scuro); }
.modal .copia-box { background: var(--carta); border-radius: 8px; padding: 9px 11px; font-size: 12.5px; word-break: break-all; margin: 8px 0; }

/* ---------------- carta d'invito ---------------- */
.invito-card {
  position: relative; text-align: center;
  background: linear-gradient(160deg, #fdf9ee, #f6efdc);
  border: 1.5px solid #cdbf9d; border-radius: 12px;
  box-shadow: var(--ombra);
  padding: 18px 16px 14px; margin: 6px 0 16px;
}
.invito-card::before {
  content: ''; position: absolute; inset: 5px;
  border: 1px solid #decfa8; border-radius: 8px; pointer-events: none;
}
.invito-fregio { color: var(--oro); font-size: 20px; line-height: 1; margin-bottom: 6px; }
.invito-avatar {
  width: 58px; height: 58px; border-radius: 50%; margin: 0 auto 9px; overflow: hidden;
  display: flex; align-items: center; justify-content: center;
  color: #fff; font-weight: 700; font-size: 21px;
  box-shadow: 0 0 0 3px var(--bianco), 0 0 0 4.5px #cdbf9d;
}
.invito-avatar img { width: 100%; height: 100%; object-fit: cover; }
.invito-titolo { font-family: Georgia, "Times New Roman", serif; font-size: 14.5px; line-height: 1.35; }
.invito-titolo b { font-size: 19px; color: var(--verde-scuro); }
.invito-sotto { font-size: 12.5px; color: var(--inchiostro-tenue); font-style: italic; margin-top: 5px; }
.invito-mitt { font-size: 11.5px; color: var(--oro); margin-top: 9px; letter-spacing: .05em; }
.condividi-riga { display: flex; flex-wrap: wrap; gap: 7px; margin-top: 10px; }
.btn.whatsapp { background: #25d366; border-color: #1faa52; color: #fff; text-decoration: none; }
.btn.whatsapp:hover { background: #1faa52; }

/* ---------------- pagine (login, pannello) ---------------- */
.pagina { flex: 1; overflow-y: auto; }
.pagina-inner { max-width: 760px; margin: 0 auto; padding: 28px 18px 60px; }
.login-wrap { min-height: 100%; display: flex; align-items: center; justify-content: center; padding: 20px; }
.login-box {
  background: var(--bianco); border-radius: 16px; box-shadow: var(--ombra);
  width: 100%; max-width: 400px; padding: 28px;
}
.login-box .brand { font-size: 28px; justify-content: center; margin-bottom: 4px; }
.login-box .slogan { text-align: center; color: var(--inchiostro-tenue); font-size: 13.5px; margin-bottom: 18px; }
.tabs { display: flex; gap: 0; margin-bottom: 16px; border: 1px solid #cfc5ac; border-radius: 9px; overflow: hidden; }
.tabs button { flex: 1; padding: 9px; border: 0; background: var(--bianco); color: var(--inchiostro-tenue); }
.tabs button.attivo { background: var(--verde); color: #fff; }
.login-box .campo { margin-bottom: 10px; }
.invito-banner { background: #eef5ef; border: 1px solid #bcd4c2; border-radius: 10px; padding: 10px 13px; font-size: 13.5px; margin-bottom: 14px; }

.sezione { background: var(--bianco); border-radius: 14px; box-shadow: var(--ombra); padding: 18px 20px; margin-bottom: 18px; }
.sezione h2 { font-size: 18px; color: var(--verde-scuro); margin-bottom: 10px; }
.sezione .descr { font-size: 13.5px; color: var(--inchiostro-tenue); margin-bottom: 12px; }
.elenco-item { padding: 10px 0; border-bottom: 1px solid #f0e8d5; font-size: 13.5px; }
.elenco-item:last-child { border-bottom: 0; }
.elenco-item .meta { color: var(--inchiostro-tenue); font-size: 12px; margin-top: 2px; }
.elenco-item .azioni { margin-top: 7px; display: flex; gap: 7px; flex-wrap: wrap; }
.elenco-item.non-letta { background: #fdf9ec; margin: 0 -8px; padding: 10px 8px; border-radius: 8px; }
.stato { display: inline-block; font-size: 11px; padding: 2px 8px; border-radius: 10px; background: var(--carta-scura); }
.stato.confermato, .stato.accettata { background: #dceae0; color: var(--verde-scuro); }
.stato.suggested, .stato.aperta { background: #fdf0d4; color: #7a5b16; }

.privacy-box { font-size: 13px; line-height: 1.5; background: var(--carta); border-radius: 10px; padding: 12px 14px; }

/* ---------------- icone ---------------- */
svg.ic { vertical-align: -3px; flex: none; }
.sezione h2 svg.ic, .modal h2 svg.ic { vertical-align: -2.5px; margin-right: 3px; }
.brand svg.ic { vertical-align: -4px; }
.card .lock svg.ic { vertical-align: 0; }

/* logo vettoriale (albero con radici a rete) */
.brand { display: inline-flex; align-items: center; gap: 7px; }
.brand .logo { width: 30px; height: auto; flex: none; }
.login-box .brand { justify-content: center; }
.login-box .brand .logo { width: 52px; }

/* ---------------- responsive ---------------- */
@media (max-width: 700px) {
  /* header dell'albero su due righe ordinate:
     riga 1 → logo · selettore albero · pannello
     riga 2 → ricerca · vista compatta */
  .topbar.albero {
    display: grid;
    grid-template-columns: auto 1fr auto auto;
    gap: 7px;
    padding: 8px 10px;
    align-items: center;
  }
  .topbar.albero .brand { grid-row: 1; grid-column: 1; }
  .topbar.albero select { grid-row: 1; grid-column: 2; width: 100%; min-width: 0; }
  .topbar.albero #btn-undo { grid-row: 1; grid-column: 3; justify-self: end; }
  .topbar.albero #btn-panel { grid-row: 1; grid-column: 4; justify-self: end; }
  .topbar.albero .search-wrap { grid-row: 2; grid-column: 1 / 3; max-width: none; }
  .topbar.albero #btn-mode { grid-row: 2; grid-column: 3 / 5; white-space: nowrap; font-size: 12px; padding: 7px 8px; }
  .topbar.albero #btn-fit { display: none; } /* il tasto ⛶ resta nei controlli in basso */
  .topbar { padding: 7px 9px; gap: 7px; }
  #drawer { width: 100vw; border-left: 0; }
  .form-griglia { grid-template-columns: 1fr; }
  .canvas-hint { display: none; }
}
