:root {
  color-scheme: light;
  --bg: #f6f7f9;
  --surface: #ffffff;
  --surface-2: #f9fafb;
  --text: #15171a;
  --muted: #6b7280;
  --muted-2: #9ca3af;
  --border: #e5e7eb;
  --border-strong: #d1d5db;
  --accent: #111827;
  --green: #16a34a;
  --green-bg: #f0fdf4;
  --yellow: #d6a700;
  --yellow-bg: #fffbea;
  --fb: #1877f2;
  --linkedin: #0a66c2;
  --shadow: 0 1px 2px rgba(0,0,0,.03), 0 10px 30px rgba(0,0,0,.035);
  --radius: 16px;
  --radius-sm: 10px;
}

html[data-theme="dark"] {
  color-scheme: dark;
  --bg: #0f1115;
  --surface: #15181e;
  --surface-2: #1a1e25;
  --text: #f3f4f6;
  --muted: #a5acb8;
  --muted-2: #7b8492;
  --border: #292e37;
  --border-strong: #383f4b;
  --accent: #f9fafb;
  --green-bg: rgba(22,163,74,.10);
  --yellow: #facc15;
  --yellow-bg: rgba(250,204,21,.10);
  --shadow: 0 1px 2px rgba(0,0,0,.20), 0 14px 34px rgba(0,0,0,.15);
}

* { box-sizing: border-box; }
html { scroll-behavior: smooth; }
body {
  margin: 0;
  min-width: 320px;
  background: var(--bg);
  color: var(--text);
  font-family: Inter, ui-sans-serif, -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
}
button, input { font: inherit; }
button { color: inherit; }
a { color: inherit; }

.app-shell { width: min(2200px, 100%); margin: 0 auto; padding: 0 20px 44px; }
.topbar {
  height: 78px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 24px;
  border-bottom: 1px solid var(--border);
}
.brand-wrap { display: flex; align-items: center; gap: 12px; min-width: 0; }
.brand-mark {
  width: 38px; height: 38px; border-radius: 11px; display: grid; place-items: center;
  background: var(--accent); color: var(--bg); font-size: 12px; font-weight: 800; letter-spacing: -.04em;
}
.brand-wrap h1 { margin: 0; font-size: 16px; line-height: 1.2; letter-spacing: -.02em; }
.brand-wrap p { margin: 3px 0 0; color: var(--muted); font-size: 12px; }
.top-actions { display: flex; align-items: center; gap: 8px; }
.search-box {
  width: min(380px, 36vw); height: 40px; display: flex; align-items: center; gap: 9px;
  background: var(--surface); border: 1px solid var(--border); border-radius: 11px; padding: 0 12px;
  transition: border-color .16s ease, box-shadow .16s ease;
}
.search-box:focus-within { border-color: var(--border-strong); box-shadow: 0 0 0 3px rgba(107,114,128,.08); }
.search-box svg, .secondary-btn svg, .icon-btn svg, .public-access-link svg { width: 17px; height: 17px; fill: none; stroke: currentColor; stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round; }
.search-box svg { color: var(--muted); flex: 0 0 auto; }
.search-box input { border: 0; outline: 0; background: transparent; color: var(--text); width: 100%; font-size: 13px; }
.search-box input::placeholder { color: var(--muted-2); }
.icon-btn, .secondary-btn {
  border: 1px solid var(--border); background: var(--surface); border-radius: 10px; cursor: pointer;
  transition: background .16s, border-color .16s, transform .16s;
}
.icon-btn:hover, .secondary-btn:hover { background: var(--surface-2); border-color: var(--border-strong); }
.icon-btn:active, .secondary-btn:active { transform: translateY(1px); }
.icon-btn { width: 40px; height: 40px; display: grid; place-items: center; }
.public-access-link {
  min-height: 40px; display: inline-flex; align-items: center; gap: 7px; padding: 0 11px;
  border: 1px solid color-mix(in srgb, var(--green) 30%, var(--border)); background: var(--surface);
  border-radius: 10px; color: var(--green); text-decoration: none; font-size: 12px; font-weight: 800;
  transition: background .16s, border-color .16s, transform .16s; white-space: nowrap;
}
.public-access-link:hover { background: var(--green-bg); border-color: var(--green); }
.public-access-link:active { transform: translateY(1px); }
.public-access-link svg { width: 16px; height: 16px; }

.overview { display: flex; justify-content: space-between; align-items: end; gap: 28px; padding: 36px 0 24px; }
.eyebrow { display: inline-block; font-size: 10px; font-weight: 800; letter-spacing: .11em; color: var(--muted); margin-bottom: 7px; }
.overview h2 { margin: 0; font-size: clamp(24px, 3vw, 36px); letter-spacing: -.045em; line-height: 1.05; }
.overview p { margin: 9px 0 0; color: var(--muted); font-size: 13px; }
.stats { display: grid; grid-auto-flow: column; gap: 8px; }
.stat { min-width: 116px; background: var(--surface); border: 1px solid var(--border); border-radius: 12px; padding: 12px 14px; }
.stat strong { display: block; font-size: 20px; letter-spacing: -.035em; line-height: 1; }
.stat span { display: block; margin-top: 5px; color: var(--muted); font-size: 11px; }

.control-panel { display: flex; justify-content: space-between; align-items: center; gap: 14px; padding: 14px 0; border-top: 1px solid var(--border); }
.segmented { display: inline-flex; background: var(--surface); border: 1px solid var(--border); border-radius: 10px; padding: 3px; }
.segment { border: 0; background: transparent; color: var(--muted); padding: 7px 12px; border-radius: 7px; font-size: 12px; font-weight: 700; cursor: pointer; }
.segment.active { background: var(--text); color: var(--bg); }
.segment.target-segment.active { background: var(--yellow); color: #171717; }
.segment span { opacity: .68; margin-left: 3px; }
.secondary-btn { min-height: 36px; padding: 0 11px; display: inline-flex; align-items: center; justify-content: center; gap: 7px; font-size: 12px; font-weight: 700; }
.small-btn { min-height: 34px; }
.profile-card .hide-profile-btn { width: 34px; min-width: 34px; padding: 0; font-size: 0; }
.profile-card .hide-profile-btn svg { margin: 0; }

.button-count { color: var(--muted); font-weight: 600; }
.control-right { display: flex; align-items: center; gap: 8px; }
.popover-wrap { position: relative; }
.profile-popover {
  position: absolute; right: 0; top: calc(100% + 8px); z-index: 30; width: 285px;
  background: var(--surface); border: 1px solid var(--border); border-radius: 12px; box-shadow: var(--shadow); padding: 8px;
}
.popover-head { display: flex; align-items: center; justify-content: space-between; padding: 6px 7px 8px; font-size: 12px; }
.link-btn { border: 0; background: none; color: var(--muted); cursor: pointer; font-size: 11px; padding: 3px; }
.visibility-item { display: flex; align-items: center; gap: 10px; padding: 8px 7px; border-radius: 8px; cursor: pointer; }
.visibility-item:hover { background: var(--surface-2); }
.visibility-item input { accent-color: var(--green); }
.visibility-mini-avatar { width: 25px; height: 25px; border-radius: 50%; background: var(--surface-2); border: 1px solid var(--border); display: grid; place-items: center; font-size: 9px; font-weight: 800; overflow: hidden; }
.visibility-mini-avatar img { width: 100%; height: 100%; object-fit: cover; }
.visibility-item strong { font-size: 12px; font-weight: 700; flex: 1; }
.visibility-item small { color: var(--muted); font-size: 10px; text-transform: capitalize; }

.profile-tabs { display: flex; gap: 7px; overflow-x: auto; padding: 2px 0 14px; scrollbar-width: thin; }
.profile-tab {
  flex: 0 0 auto; display: inline-flex; align-items: center; gap: 8px; height: 40px; padding: 0 11px 0 6px;
  border: 1px solid var(--border); background: var(--surface); border-radius: 999px; cursor: pointer; font-size: 12px; font-weight: 700;
}
.profile-tab:hover { border-color: var(--border-strong); }
.profile-tab.hidden-profile { opacity: .45; }
.tab-avatar { width: 28px; height: 28px; border-radius: 50%; overflow: hidden; background: var(--surface-2); border: 1px solid var(--border); display: grid; place-items: center; font-size: 9px; }
.tab-avatar img { width: 100%; height: 100%; object-fit: cover; }
.tab-common { font-size: 9px; color: var(--green); background: var(--green-bg); border: 1px solid color-mix(in srgb, var(--green) 35%, transparent); border-radius: 999px; padding: 2px 5px; }

.profiles-grid {
  display: grid;
  grid-template-columns: repeat(var(--visible-profile-count, 5), minmax(0, 1fr));
  gap: 12px;
  align-items: start;
  width: 100%;
}
.profile-card { min-width: 0; }

.profile-card { scroll-margin-top: 16px; background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); box-shadow: var(--shadow); overflow: clip; }
.profile-card.flash { animation: focusFlash 1.1s ease; }
@keyframes focusFlash { 0%,100% { box-shadow: var(--shadow); } 35% { box-shadow: 0 0 0 3px rgba(22,163,74,.14), var(--shadow); } }
.profile-card-header { display: flex; align-items: center; justify-content: space-between; gap: 10px; padding: 13px; border-bottom: 1px solid var(--border); }
.profile-heading { display: flex; align-items: center; gap: 12px; min-width: 0; }
.owner-avatar-wrap { position: relative; flex: 0 0 auto; width: 44px; height: 44px; }
.owner-avatar, .owner-fallback { width: 44px; height: 44px; border-radius: 50%; border: 1px solid var(--border); background: var(--surface-2); }
.owner-avatar { display: block; object-fit: cover; }
.owner-fallback { display: none; place-items: center; font-size: 12px; font-weight: 800; }
.social-badge { position: absolute; right: -2px; bottom: -2px; width: 19px; height: 19px; border-radius: 50%; border: 2px solid var(--surface); display: grid; place-items: center; color: #fff; font-size: 9px; font-weight: 900; }
.social-badge.facebook { background: var(--fb); }
.social-badge.linkedin { background: var(--linkedin); }
.profile-title-wrap { min-width: 0; }
.profile-title-row { display: flex; align-items: center; gap: 8px; min-width: 0; }
.profile-name { margin: 0; font-size: 14px; letter-spacing: -.025em; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.network-label { flex: 0 0 auto; color: var(--muted); border: 1px solid var(--border); background: var(--surface-2); border-radius: 999px; padding: 3px 7px; font-size: 9px; font-weight: 800; text-transform: uppercase; letter-spacing: .05em; }
.profile-subtitle { margin: 5px 0 0; color: var(--muted); font-size: 11px; }

.table-shell { overflow: auto; height: clamp(520px, 62vh, 760px); max-height: 760px; scrollbar-width: thin; }
table { width: 100%; border-collapse: separate; border-spacing: 0; min-width: max-content; }
thead { position: sticky; top: 0; z-index: 5; background: var(--surface-2); }
th { text-align: left; color: var(--muted); font-size: 10px; font-weight: 800; text-transform: uppercase; letter-spacing: .055em; padding: 10px 12px; border-bottom: 1px solid var(--border); white-space: nowrap; }
td { padding: 10px 12px; border-bottom: 1px solid var(--border); font-size: 12px; vertical-align: middle; max-width: 340px; }
tbody tr:last-child td { border-bottom: 0; }
tbody tr:hover td { background: var(--surface-2); }
.contact-cell { display: flex; align-items: center; gap: 9px; min-width: 175px; }
.contact-avatar { width: 32px; height: 32px; border-radius: 50%; object-fit: cover; background: var(--surface-2); border: 1px solid var(--border); flex: 0 0 auto; }
.contact-avatar-fallback { width: 32px; height: 32px; border-radius: 50%; background: var(--surface-2); border: 1px solid var(--border); display: grid; place-items: center; font-size: 9px; font-weight: 800; flex: 0 0 auto; }
.contact-name { font-weight: 720; min-width: 0; }
.contact-name a { text-decoration: none; }
.contact-name a:hover { text-decoration: underline; }
.common-pill { display: inline-flex; align-items: center; gap: 5px; color: var(--green); font-size: 9px; font-weight: 800; margin-top: 3px; }
.common-pill i { width: 5px; height: 5px; border-radius: 50%; background: currentColor; }
.target-pill { display: inline-flex; align-items: center; gap: 5px; color: #9a7600; font-size: 9px; font-weight: 850; margin-top: 3px; margin-left: 6px; }
html[data-theme="dark"] .target-pill { color: var(--yellow); }
.target-pill i { width: 5px; height: 5px; border-radius: 50%; background: currentColor; }
.url-cell a { color: #2563eb; text-decoration: none; display: inline-flex; align-items: center; gap: 5px; font-weight: 800; max-width: 260px; }
.url-cell a:hover { color: #1d4ed8; text-decoration: underline; }
.url-cell span { max-width: 230px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.cell-text { display: block; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; max-width: 340px; }
tr.common-row td { background: color-mix(in srgb, var(--green-bg) 62%, var(--surface)); }
tr.common-row td:first-child { box-shadow: inset 3px 0 0 var(--green); }
tr.common-row:hover td { background: var(--green-bg); }
tr.target-row td { background: color-mix(in srgb, var(--yellow-bg) 76%, var(--surface)); }
tr.target-row:hover td { background: var(--yellow-bg); }
tr.common-row.target-row td { background: color-mix(in srgb, var(--yellow-bg) 72%, var(--green-bg)); }
.card-footer { min-height: 44px; padding: 10px 14px; display: flex; align-items: center; justify-content: space-between; gap: 12px; border-top: 1px solid var(--border); color: var(--muted); font-size: 10px; }
.common-legend { display: flex; align-items: center; gap: 6px; }
.common-legend i { width: 8px; height: 8px; border-radius: 2px; border: 2px solid var(--green); }
.row-legends { display: inline-flex; align-items: center; gap: 10px; }
.target-legend { display: flex; align-items: center; gap: 6px; }
.target-legend i { width: 8px; height: 8px; border-radius: 2px; border: 2px solid var(--yellow); }
.empty-state { text-align: center; border: 1px dashed var(--border-strong); border-radius: 16px; padding: 55px 20px; background: var(--surface); }
.empty-icon { font-size: 30px; color: var(--muted); }
.empty-state h3 { margin: 9px 0 4px; font-size: 15px; }
.empty-state p { margin: 0; color: var(--muted); font-size: 12px; }

@media (max-width: 1180px) {
  .profiles-grid {
    grid-template-columns: none;
    grid-auto-flow: column;
    grid-auto-columns: minmax(300px, 72vw);
    overflow-x: auto;
    padding-bottom: 8px;
    scroll-snap-type: x proximity;
    scrollbar-width: thin;
  }
  .profile-card { scroll-snap-align: start; }
}

@media (max-width: 850px) {
  .app-shell { padding: 0 14px 30px; }
  .topbar { height: auto; padding: 14px 0; align-items: flex-start; }
  .top-actions { flex: 1; justify-content: flex-end; }
  .search-box { width: min(330px, 52vw); }
  .overview { align-items: flex-start; flex-direction: column; padding-top: 26px; }
  .stats { width: 100%; grid-auto-flow: column; grid-auto-columns: minmax(95px, 1fr); overflow-x: auto; }
  .stat { min-width: 95px; }
}

@media (max-width: 620px) {
  .brand-wrap p { display: none; }
  .public-access-link { width: 40px; padding: 0; justify-content: center; }
  .public-access-link span { display: none; }
  .brand-mark { width: 34px; height: 34px; border-radius: 10px; }
  .search-box { width: 44px; padding: 0 12px; transition: width .18s ease; }
  .search-box input { width: 0; opacity: 0; }
  .search-box:focus-within { width: min(260px, 68vw); position: absolute; right: 60px; background: var(--surface); z-index: 20; }
  .search-box:focus-within input { width: 100%; opacity: 1; }
  .overview p { max-width: 92%; }
  .control-panel { align-items: stretch; }
  .segmented { flex: 1; }
  .segment { flex: 1; white-space: nowrap; }
  .profile-popover { right: 0; width: min(300px, calc(100vw - 28px)); }
  .profile-card-header { align-items: flex-start; padding: 13px; }
  .hide-profile-btn { width: 34px; padding: 0; font-size: 0; }
  .hide-profile-btn svg { margin: 0; }
  .owner-avatar-wrap, .owner-avatar, .owner-fallback { width: 44px; height: 44px; }
  .profile-title-row { align-items: flex-start; flex-direction: column; gap: 4px; }
  .profile-name { max-width: 54vw; }
  .profiles-grid { grid-auto-columns: minmax(286px, 88vw); }
  .table-shell { height: min(62vh, 620px); max-height: 620px; overflow: auto; padding: 0; }
  table { display: table; min-width: max-content; width: 100%; }
  thead { display: table-header-group; }
  tbody { display: table-row-group; padding: 0; }
  tr { display: table-row; border: 0; border-radius: 0; background: transparent; }
  th, td { display: table-cell; width: auto; max-width: 260px; padding: 9px 10px; white-space: nowrap; }
  td::before { content: none; }
  .contact-cell { min-width: 155px; }
  .cell-text, .url-cell a, .url-cell span { white-space: nowrap; max-width: 220px; }
  .card-footer { padding: 10px 12px; }
}

/* Il profilo comune ha un contorno verde completo su desktop. */
@media (min-width: 621px) {
  tr.common-row td { border-top: 1px solid var(--green); border-bottom: 1px solid var(--green); }
  tr.common-row td:first-child { border-left: 1px solid var(--green); box-shadow: inset 3px 0 0 var(--green); }
  tr.common-row td:last-child { border-right: 1px solid var(--green); }

  tr.target-row td { border-top: 2px solid var(--yellow); border-bottom: 2px solid var(--yellow); }
  tr.target-row td:first-child { border-left: 2px solid var(--yellow); box-shadow: inset 3px 0 0 var(--yellow); }
  tr.target-row td:last-child { border-right: 2px solid var(--yellow); }
  tr.common-row.target-row td:first-child { box-shadow: inset 3px 0 0 var(--green); }
}

/* Su schermi stretti le schede restano in colonne orizzontali; il bordo Target resta visibile. */
@media (max-width: 620px) {
  tr.target-row td { border-top: 2px solid var(--yellow); border-bottom: 2px solid var(--yellow); }
  tr.target-row td:first-child { border-left: 2px solid var(--yellow); }
  tr.target-row td:last-child { border-right: 2px solid var(--yellow); }
}
