:root {
  /* Palette più "terminal / Bloomberg style", ora molto più nera */
  --bg: #02040a;
  --bg-panel: #05070d;
  --bg-panel-alt: #060910;
  --accent: #22c55e;
  --accent-soft: #22c55e;
  --accent-soft2: #facc15;
  --text: #e5e7eb;
  --muted: #9ca3af;
  --border: rgba(148, 163, 184, 0.28);
  --radius-lg: 14px;
  --radius-md: 10px;
  --shadow-soft: 0 24px 60px rgba(0, 0, 0, 0.9);
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  font-family: system-ui, -apple-system, BlinkMacSystemFont, 'SF Pro Text', 'Segoe UI', sans-serif;
  background:
    radial-gradient(circle at top left, #020617 0, #02040a 45%, #010208 100%),
    repeating-linear-gradient(
      90deg,
      rgba(15, 23, 42, 0.32) 0,
      rgba(15, 23, 42, 0.32) 1px,
      transparent 1px,
      transparent 8px
    ),
    repeating-linear-gradient(
      180deg,
      rgba(15, 23, 42, 0.28) 0,
      rgba(15, 23, 42, 0.28) 1px,
      transparent 1px,
      transparent 8px
    );
  background-attachment: fixed;
  color: var(--text);
}

@media (hover: none) and (pointer: coarse) {
  /* Su iOS Safari `background-attachment: fixed` può essere pesante/strano. */
  body {
    background-attachment: scroll;
  }
}

.page-header {
  position: sticky;
  top: 0;
  z-index: 10;
  backdrop-filter: blur(18px);
  -webkit-backdrop-filter: blur(18px);
  background: linear-gradient(to right, rgba(3, 7, 18, 0.98), rgba(15, 23, 42, 0.92));
  border-bottom: 1px solid rgba(148, 163, 184, 0.45);
  box-shadow: 0 22px 40px rgba(0, 0, 0, 0.9);
}

.header-inner {
  max-width: 1120px;
  margin: 0 auto;
  padding: calc(16px + env(safe-area-inset-top)) 20px calc(14px + env(safe-area-inset-bottom));
  display: flex;
  align-items: center;
  justify-content: space-between;
}

h1 {
  margin: 0;
  font-size: 1.6rem;
  letter-spacing: 0.16em;
}

.subtitle {
  margin: 2px 0 0;
  font-size: 0.9rem;
  color: var(--muted);
}

.layout {
  max-width: 1120px;
  margin: 24px auto 40px;
  padding: 0 calc(20px + env(safe-area-inset-right)) 20px calc(20px + env(safe-area-inset-left));
  display: grid;
  grid-template-columns: minmax(0, 1fr);
  grid-template-rows: auto auto auto auto auto;
  gap: 18px;
}

.panel {
  background:
    radial-gradient(circle at top left, rgba(56, 189, 248, 0.08), transparent 55%),
    radial-gradient(circle at bottom right, rgba(34, 197, 94, 0.05), transparent 55%),
    linear-gradient(to bottom, rgba(15, 23, 42, 0.96), rgba(15, 23, 42, 0.98));
  border-radius: var(--radius-lg);
  border: 1px solid var(--border);
  box-shadow: var(--shadow-soft);
  padding: 14px 14px 16px;
}

.panel h2 {
  margin: 0 0 10px;
  font-size: 0.95rem;
  letter-spacing: 0.18em;
  text-transform: uppercase;
  color: var(--muted);
}

.panel-table {
  grid-column: 1 / -1;
}

.panel-aperti {
  grid-row: 1 / 2;
}

.panel-aperti .filters-inline,
.panel-negoziali .filters-inline {
  display: flex;
  flex-wrap: wrap;
  gap: 12px 16px;
  align-items: flex-end;
  margin-bottom: 12px;
}

.missing-settori {
  margin: 10px 0 12px;
  padding: 10px 12px;
  border-radius: var(--radius-md);
  border: 1px solid rgba(30, 64, 175, 0.7);
  background: rgba(3, 7, 18, 0.65);
}

.missing-settori summary {
  cursor: pointer;
  color: rgba(229, 231, 235, 0.92);
  letter-spacing: 0.02em;
}

.missing-settori-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
  margin: 10px 0 8px;
}

.missing-settori-list {
  margin: 0;
  padding: 10px 12px;
  max-height: 240px;
  overflow: auto;
  border-radius: 10px;
  border: 1px solid rgba(148, 163, 184, 0.28);
  background: rgba(2, 4, 10, 0.7);
  color: rgba(229, 231, 235, 0.92);
  font-size: 0.78rem;
  line-height: 1.35;
  white-space: pre-wrap;
}

.panel-aperti .filters-inline label,
.panel-negoziali .filters-inline label {
  margin-bottom: 0;
}

.panel-aperti .filters-inline input,
.panel-aperti .filters-inline select,
.panel-negoziali .filters-inline input,
.panel-negoziali .filters-inline select {
  width: auto;
  min-width: 140px;
}

.section-intro {
  font-size: 0.85rem;
  color: var(--muted);
  margin: 0 0 12px;
}

.section-notice {
  padding-left: 10px;
  border-left: 3px solid var(--accent-soft);
  margin-bottom: 12px;
}

.section-notice a {
  color: var(--accent-soft2);
  text-decoration: none;
}

.section-notice a:hover {
  text-decoration: underline;
}

.panel-negoziali {
  grid-row: 2 / 3;
}

.view-toggle {
  margin-bottom: 10px;
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
}

.btn-toggle {
  padding: 6px 12px;
  border-radius: var(--radius-md);
  border: 1px solid var(--border);
  background: radial-gradient(circle at top left, rgba(34, 197, 94, 0.08), rgba(15, 23, 42, 1));
  color: var(--text);
  font-size: 0.85rem;
  cursor: pointer;
}

.btn-toggle:hover {
  background: rgba(79, 70, 229, 0.2);
  border-color: var(--accent-soft);
}

.btn-toggle[aria-pressed="true"] {
  background: rgba(79, 70, 229, 0.35);
  border-color: var(--accent-soft);
}

.chart-view {
  display: none;
  margin-top: 0;
}

.chart-view[aria-hidden="false"] {
  display: block;
}

.chart-wrapper-inline {
  height: 420px;
}

.chart-wrapper-inline-scroll {
  max-height: 560px;
  overflow: auto;
  border-radius: 10px;
  border: 1px solid rgba(148, 163, 184, 0.22);
  background: rgba(2, 4, 10, 0.45);
  padding: 8px;
  -webkit-overflow-scrolling: touch;
  touch-action: pan-x pan-y;
}

.panel-aperti .table-wrapper,
.panel-negoziali .table-wrapper,
.panel-pip .table-wrapper {
  max-height: 400px;
  min-width: 0;
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
  touch-action: pan-x pan-y;
}

.link-scheda {
  color: var(--accent-soft2);
  text-decoration: none;
}
.link-scheda:hover {
  text-decoration: underline;
}

.btn-scheda {
  padding: 5px 10px;
  border-radius: var(--radius-md);
  border: 1px solid var(--border);
  background: radial-gradient(circle at top left, rgba(34, 197, 94, 0.12), rgba(15, 23, 42, 1));
  color: var(--text);
  font-size: 0.82rem;
  cursor: pointer;
  white-space: nowrap;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  line-height: 1.1;
}

.btn-scheda:hover {
  border-color: var(--accent-soft);
  background: rgba(79, 70, 229, 0.2);
}

.btn-scheda:active {
  transform: translateY(1px);
}

.scheda-modal {
  position: fixed;
  inset: 0;
  z-index: 1000;
}

.scheda-modal__overlay {
  position: absolute;
  inset: 0;
  background: rgba(0, 0, 0, 0.65);
}

.scheda-modal__content {
  position: relative;
  margin: 4vh auto 0;
  width: min(980px, calc(100% - 28px));
  max-height: 92vh;
  overflow: auto;
  border-radius: var(--radius-lg);
  border: 1px solid var(--border);
  background: linear-gradient(to bottom, rgba(15, 23, 42, 0.98), rgba(15, 23, 42, 0.94));
  box-shadow: var(--shadow-soft);
  padding: 14px;
}

.scheda-modal__header {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 12px;
  padding-bottom: 10px;
  border-bottom: 1px solid rgba(148, 163, 184, 0.28);
}

.scheda-modal__header h3 {
  margin: 0;
  font-size: 1rem;
}

.scheda-modal__subtitle {
  margin: 6px 0 0;
  font-size: 0.86rem;
  color: var(--muted);
}

.scheda-modal__close {
  border: 1px solid rgba(148, 163, 184, 0.35);
  background: rgba(2, 4, 10, 0.35);
  color: var(--text);
  border-radius: 10px;
  width: 36px;
  height: 36px;
  cursor: pointer;
  font-size: 1.2rem;
  line-height: 1;
}

.scheda-modal__close:hover {
  border-color: var(--accent-soft);
}

.scheda-modal__body {
  padding-top: 12px;
}

.scheda-modal__hint {
  margin: 0 0 10px;
  font-size: 0.86rem;
  color: var(--muted);
}

.scheda-modal__actions {
  margin: 0 0 10px;
}

.scheda-modal__open-link {
  display: inline-block;
  padding: 8px 12px;
  border-radius: var(--radius-md);
  border: 1px solid rgba(148, 163, 184, 0.35);
  color: var(--accent-soft2);
  text-decoration: none;
}

.scheda-modal__open-link:hover {
  text-decoration: underline;
}

.scheda-modal__url {
  word-break: break-all;
  font-size: 0.82rem;
  color: rgba(229, 231, 235, 0.85);
  margin: 6px 0 10px;
}

.scheda-modal__iframe {
  width: 100%;
  height: 65vh;
  border: 0;
  border-radius: 12px;
  background: #020617;
}

.scheda-modal-open {
  overflow: hidden;
  touch-action: none;
}

.panel-aperti.show-chart #table-wrap-aperti,
.panel-negoziali.show-chart #table-wrap-negoziali {
  display: none;
}

.panel-confronto {
  grid-column: 1 / -1;
  grid-row: 3 / 4;
}

.chart-wrapper-scatter {
  height: 420px;
}

.th-sort {
  cursor: pointer;
  user-select: none;
  white-space: nowrap;
  padding-right: 1.2em;
}

.th-sort:hover {
  color: var(--text);
}

.th-sort .sort-arrow {
  opacity: 0.5;
  font-size: 0.75em;
}

.th-sort.sort-asc .sort-arrow,
.th-sort.sort-desc .sort-arrow {
  opacity: 1;
}

.th-sort .sort-arrow::after {
  content: ' ↕';
  margin-left: 2px;
}

.th-sort.sort-asc .sort-arrow::after {
  content: ' ↑';
}

.th-sort.sort-desc .sort-arrow::after {
  content: ' ↓';
}

.th-col-gestore {
  position: relative;
}

.col-resizer {
  position: absolute;
  right: 0;
  top: 0;
  bottom: 0;
  width: 10px;
  cursor: col-resize;
}

.col-resizer:hover {
  background: rgba(79, 70, 229, 0.25);
}

@media (hover: none) and (pointer: coarse) {
  /* Evita che i tocchi “finiscano” sul resizer invece dell'intestazione. */
  .col-resizer {
    display: none;
  }
}

.col-resizer::after {
  content: '';
  position: absolute;
  right: 4px;
  top: 50%;
  transform: translateY(-50%);
  width: 2px;
  height: 14px;
  background: var(--muted);
  border-radius: 1px;
  opacity: 0.7;
}

.panel-charts {
  grid-column: 1 / -1;
  grid-row: 4 / 5;
}

.panel-charts h2 {
  margin-top: 14px;
}
.panel-charts h2:first-child {
  margin-top: 0;
}

.chart-wrapper {
  position: relative;
  height: 280px;
  margin-top: 8px;
  margin-bottom: 16px;
}

.panel-notes {
  grid-column: 1 / -1;
  grid-row: 5 / 6;
}

label {
  display: block;
  font-size: 0.85rem;
  color: var(--muted);
  margin-bottom: 10px;
}

select,
input[type="search"] {
  width: 100%;
  margin-top: 4px;
  padding: 8px 10px;
  border-radius: var(--radius-md);
  border: 1px solid var(--border);
  background: #020617;
  color: var(--text);
  font-size: 0.9rem;
}

input[type="search"]::-webkit-search-cancel-button {
  filter: invert(0.8);
}

.filter-summary {
  margin: 10px 0 0;
  font-size: 0.8rem;
  color: var(--muted);
}

.table-wrapper {
  max-height: 520px;
  overflow: auto;
  border-radius: 10px;
  border: 1px solid rgba(30, 64, 175, 0.7);
  background:
    linear-gradient(to bottom, rgba(15, 23, 42, 0.98), rgba(15, 23, 42, 0.99)),
    repeating-linear-gradient(
      180deg,
      rgba(15, 23, 42, 1) 0,
      rgba(15, 23, 42, 1) 1px,
      rgba(15, 23, 42, 0.96) 1px,
      rgba(15, 23, 42, 0.96) 28px
    );
}

table {
  width: 100%;
  min-width: 0;
  border-collapse: collapse;
  font-size: 0.82rem;
  table-layout: fixed;
}

th,
td {
  padding: 6px 8px;
  border-bottom: 1px solid rgba(15, 23, 42, 1);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

th {
  position: sticky;
  top: 0;
  background:
    linear-gradient(to right, rgba(15, 23, 42, 0.98), rgba(15, 23, 42, 0.97));
  text-align: left;
  font-weight: 500;
  color: rgba(209, 213, 219, 0.9);
  border-bottom: 1px solid rgba(30, 64, 175, 0.9);
}

tbody tr:nth-child(2n) {
  background-color: rgba(15, 23, 42, 0.95);
}

tbody tr:hover {
  background:
    linear-gradient(to right, rgba(34, 197, 94, 0.16), rgba(56, 189, 248, 0.12));
}

.num {
  text-align: right;
  font-variant-numeric: tabular-nums;
}

.note-block {
  margin: 14px 0;
  padding: 12px 14px;
  border-radius: var(--radius-md);
  border: 1px solid var(--border);
  background: rgba(15, 23, 42, 0.6);
}

.note-block h3 {
  margin: 0 0 8px;
  font-size: 0.9rem;
  font-weight: 600;
  color: var(--text);
  text-transform: none;
  letter-spacing: 0;
}

.note-block p {
  margin: 0 0 6px;
  font-size: 0.85rem;
  line-height: 1.45;
}

.note-block p:last-child {
  margin-bottom: 0;
}

.panel-notes ul {
  padding-left: 1.1rem;
  margin: 6px 0 0;
}

.panel-notes li {
  margin-bottom: 4px;
  font-size: 0.85rem;
}

@media (max-width: 520px) {
  html,
  body {
    overflow-x: hidden;
  }

  .filters-inline {
    flex-direction: column;
    align-items: stretch;
    width: 100%;
  }

  .panel-aperti .filters-inline label,
  .panel-negoziali .filters-inline label,
  .panel-pip .filters-inline label {
    width: 100%;
  }

  .btn-toggle {
    padding: 10px 14px;
    font-size: 0.95rem;
  }

  .btn-scheda {
    padding: 4px 7px;
    font-size: 0.78rem;
  }

  /* Su iPhone la colonna "Scheda costi" puo' essere stretta:
     evita clipping del bottone. */
  #fondi-table-negoziali th:last-child,
  #fondi-table-negoziali td:last-child {
    overflow: visible;
  }

  .panel-aperti .filters-inline input,
  .panel-aperti .filters-inline select,
  .panel-negoziali .filters-inline input,
  .panel-negoziali .filters-inline select,
  .panel-pip .filters-inline input,
  .panel-pip .filters-inline select {
    width: 100%;
    min-width: 0;
  }

  table {
    font-size: 0.94rem;
  }

  /* iPhone: riduci la densita' delle tabelle nascondendo alcune colonne */
  /* Fondi aperti: Gestore(1), Comparto(3), Rend.5a(5), ISC10a(7) */
  #fondi-table th:nth-child(1),
  #fondi-table td:nth-child(1),
  #fondi-table th:nth-child(3),
  #fondi-table td:nth-child(3),
  #fondi-table th:nth-child(5),
  #fondi-table td:nth-child(5),
  #fondi-table th:nth-child(7),
  #fondi-table td:nth-child(7) {
    display: none;
  }
  #fondi-table col:nth-child(1),
  #fondi-table col:nth-child(3),
  #fondi-table col:nth-child(5),
  #fondi-table col:nth-child(7) {
    display: none;
  }

  /* Fondi negoziali: Rend.5a(5), ISC10a(7) */
  #fondi-table-negoziali th:nth-child(5),
  #fondi-table-negoziali td:nth-child(5),
  #fondi-table-negoziali th:nth-child(7),
  #fondi-table-negoziali td:nth-child(7) {
    display: none;
  }
  #fondi-table-negoziali col:nth-child(5),
  #fondi-table-negoziali col:nth-child(7) {
    display: none;
  }

  /* PIP: Gestore(1), Comparto(3), Rend.5a(5), ISC10a(7) */
  #fondi-table-pip th:nth-child(1),
  #fondi-table-pip td:nth-child(1),
  #fondi-table-pip th:nth-child(3),
  #fondi-table-pip td:nth-child(3),
  #fondi-table-pip th:nth-child(5),
  #fondi-table-pip td:nth-child(5),
  #fondi-table-pip th:nth-child(7),
  #fondi-table-pip td:nth-child(7) {
    display: none;
  }
  #fondi-table-pip col:nth-child(1),
  #fondi-table-pip col:nth-child(3),
  #fondi-table-pip col:nth-child(5),
  #fondi-table-pip col:nth-child(7) {
    display: none;
  }
}

@media (max-width: 900px) {
  .layout {
    grid-template-columns: minmax(0, 1fr);
  }

  .panel-aperti { grid-row: 1 / 2; }
  .panel-negoziali { grid-row: 2 / 3; }
  .panel-confronto { grid-row: 3 / 4; }
  .panel-charts { grid-row: 4 / 5; }
  .panel-notes { grid-row: 5 / 6; }
}
