  :root {
    color-scheme: light;
    --page: #eef1ee;
    --surface: #ffffff;
    --surface-2: #f4f6f4;
    --ink: #1b2430;
    --ink-2: #55606b;
    --muted: #8a939c;
    --border: rgba(27, 36, 48, 0.12);
    --border-strong: rgba(27, 36, 48, 0.22);
    --accent: #1f6f63;
    --accent-ink: #ffffff;
    --accent-wash: rgba(31, 111, 99, 0.1);

    --good: #0ca30c;
    --warning: #b8860b;
    --critical: #c22b2b;

    --cat-shopping: #2a78d6;
    --cat-ristoranti: #eb6834;
    --cat-salute: #1baf7a;
    --cat-bollette: #eda100;
    --cat-famiglia: #e87ba4;
    --cat-altro: #9aa2ab;

    --grid: #e1e0d9;
    --shadow: 0 1px 2px rgba(20, 24, 30, 0.04), 0 8px 24px rgba(20, 24, 30, 0.06);
  }

  @media (prefers-color-scheme: dark) {
    :root:where(:not([data-theme="light"])) {
      color-scheme: dark;
      --page: #12151a;
      --surface: #1a1f26;
      --surface-2: #20262e;
      --ink: #eceff3;
      --ink-2: #c3cad1;
      --muted: #8b93a0;
      --border: rgba(255, 255, 255, 0.1);
      --border-strong: rgba(255, 255, 255, 0.18);
      --accent: #52b8a6;
      --accent-ink: #08201c;
      --accent-wash: rgba(82, 184, 166, 0.16);

      --good: #35c94b;
      --warning: #fab219;
      --critical: #ef6a6a;

      --cat-shopping: #3987e5;
      --cat-ristoranti: #d95926;
      --cat-salute: #199e70;
      --cat-bollette: #c98500;
      --cat-famiglia: #d55181;
      --cat-altro: #8b93a0;

      --grid: #2b3038;
      --shadow: 0 1px 2px rgba(0, 0, 0, 0.3), 0 8px 28px rgba(0, 0, 0, 0.35);
    }
  }

  :root[data-theme="dark"] {
    color-scheme: dark;
    --page: #12151a;
    --surface: #1a1f26;
    --surface-2: #20262e;
    --ink: #eceff3;
    --ink-2: #c3cad1;
    --muted: #8b93a0;
    --border: rgba(255, 255, 255, 0.1);
    --border-strong: rgba(255, 255, 255, 0.18);
    --accent: #52b8a6;
    --accent-ink: #08201c;
    --accent-wash: rgba(82, 184, 166, 0.16);

    --good: #35c94b;
    --warning: #fab219;
    --critical: #ef6a6a;

    --cat-shopping: #3987e5;
    --cat-ristoranti: #d95926;
    --cat-salute: #199e70;
    --cat-bollette: #c98500;
    --cat-famiglia: #d55181;
    --cat-altro: #8b93a0;

    --grid: #2b3038;
    --shadow: 0 1px 2px rgba(0, 0, 0, 0.3), 0 8px 28px rgba(0, 0, 0, 0.35);
  }

  :root[data-theme="light"] {
    color-scheme: light;
    --page: #eef1ee;
    --surface: #ffffff;
    --ink: #1b2430;
  }

  * {
    box-sizing: border-box;
  }

  html,
  body {
    margin: 0;
    background: var(--page);
    color: var(--ink);
  }

  body {
    font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
    font-size: 15px;
    line-height: 1.5;
    -webkit-font-smoothing: antialiased;
  }

  h1,
  h2,
  h3 {
    font-family: Iowan Old Style, Palatino Linotype, Georgia, serif;
    font-weight: 600;
    color: var(--ink);
    margin: 0;
    text-wrap: balance;
  }

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

  a {
    color: inherit;
  }

  .hidden {
    display: none !important;
  }

  .wrap {
    max-width: 880px;
    margin: 0 auto;
    padding: 28px 18px 64px;
  }

  @media (min-width: 960px) {
    .wrap {
      max-width: 1100px;
      padding: 36px 32px 80px;
    }
  }
  @media (min-width: 1280px) {
    .wrap {
      max-width: 1320px;
    }
  }
  @media (min-width: 1600px) {
    .wrap {
      max-width: 1440px;
    }
  }

  .masthead {
    display: flex;
    align-items: baseline;
    justify-content: space-between;
    gap: 12px;
    margin-bottom: 22px;
    flex-wrap: wrap;
  }

  .masthead h1 {
    font-size: 26px;
    letter-spacing: -0.2px;
  }

  .masthead-right {
    display: flex;
    align-items: center;
    gap: 10px;
  }

  .masthead .stamp {
    font-size: 11px;
    letter-spacing: 0.08em;
    text-transform: uppercase;
    color: var(--muted);
    border: 1px solid var(--border-strong);
    border-radius: 999px;
    padding: 3px 10px;
    white-space: nowrap;
  }

  /* --- ricerca globale --- */
  .search-bar {
    position: relative;
    margin-bottom: 22px;
  }

  .search-bar #global-search {
    width: 100%;
    font: inherit;
    font-size: 14px;
    padding: 11px 14px;
    border-radius: 10px;
    border: 1px solid var(--border-strong);
    background: var(--surface);
    color: var(--ink);
  }

  .search-results {
    position: absolute;
    z-index: 20;
    top: calc(100% + 6px);
    left: 0;
    right: 0;
    max-height: 340px;
    overflow-y: auto;
    background: var(--surface);
    border: 1px solid var(--border-strong);
    border-radius: 10px;
    box-shadow: var(--shadow);
  }

  .search-result-empty {
    padding: 12px 14px;
    color: var(--muted);
    font-size: 13.5px;
  }

  .search-result-item {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 10px;
    padding: 9px 14px;
    border-bottom: 1px solid var(--border);
    cursor: pointer;
  }

  .search-result-item:last-child {
    border-bottom: none;
  }

  .search-result-item:hover {
    background: var(--surface-2);
  }

  .search-result-main {
    display: flex;
    flex-direction: column;
    gap: 2px;
    min-width: 0;
  }

  .search-result-title {
    font-size: 13.5px;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
  }

  .search-result-meta {
    font-size: 11px;
    color: var(--muted);
    text-transform: uppercase;
    letter-spacing: 0.04em;
  }

  .search-result-amount {
    font-variant-numeric: tabular-nums;
    white-space: nowrap;
    font-weight: 600;
  }

  @keyframes flash-highlight {
    0%, 100% { background: transparent; }
    30% { background: var(--accent-wash); }
  }

  tr.flash-highlight {
    animation: flash-highlight 1.6s ease-out;
  }

  /* --- login --- */
  .login-screen {
    min-height: 100vh;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 20px;
  }

  .login-card {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: 16px;
    box-shadow: var(--shadow);
    padding: 32px 28px;
    width: 100%;
    max-width: 340px;
  }

  .login-card h1 {
    font-size: 22px;
    margin-bottom: 6px;
  }

  .login-card p {
    color: var(--ink-2);
    font-size: 13.5px;
    margin-bottom: 20px;
  }

  .login-error {
    color: var(--critical);
    font-size: 13px;
    margin-top: 10px;
    min-height: 1.2em;
  }

  /* --- hero --- */
  .hero {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: 16px;
    box-shadow: var(--shadow);
    padding: 22px 24px;
    margin-bottom: 16px;
    display: flex;
    justify-content: space-between;
    align-items: flex-end;
    gap: 20px;
    flex-wrap: wrap;
  }

  .hero__cycle {
    font-size: 12px;
    letter-spacing: 0.06em;
    text-transform: uppercase;
    color: var(--muted);
    margin-bottom: 6px;
  }

  .hero__figure {
    font-size: 42px;
    font-weight: 700;
    letter-spacing: -0.5px;
    font-variant-numeric: tabular-nums;
  }

  @media (min-width: 960px) {
    .hero {
      padding: 30px 34px;
    }
    .hero__figure {
      font-size: 58px;
    }
    .hero__cycle {
      font-size: 13px;
    }
    .kpi__value {
      font-size: 23px;
    }
    .kpi {
      padding: 16px 18px;
    }
    .panel {
      padding: 26px 28px;
    }
    .panel h2 {
      font-size: 20px;
    }
  }

  .hero__figure.good {
    color: var(--good);
  }
  .hero__figure.warning {
    color: var(--warning);
  }
  .hero__figure.critical {
    color: var(--critical);
  }

  .hero__label {
    font-size: 13px;
    color: var(--ink-2);
    margin-top: 4px;
  }

  .status-chip {
    display: inline-flex;
    align-items: center;
    gap: 6px;
    font-size: 13px;
    font-weight: 600;
    padding: 6px 12px;
    border-radius: 999px;
    border: 1px solid var(--border-strong);
  }

  .status-chip .dot {
    width: 8px;
    height: 8px;
    border-radius: 50%;
  }
  .status-chip.good .dot {
    background: var(--good);
  }
  .status-chip.good {
    color: var(--good);
  }
  .status-chip.warning .dot {
    background: var(--warning);
  }
  .status-chip.warning {
    color: var(--warning);
  }
  .status-chip.critical .dot {
    background: var(--critical);
  }
  .status-chip.critical {
    color: var(--critical);
  }

  /* --- kpi row --- */
  .kpi-row {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(140px, 1fr));
    gap: 10px;
    margin-bottom: 20px;
  }

  .kpi {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: 12px;
    padding: 12px 14px;
  }

  .kpi__label {
    font-size: 12px;
    color: var(--muted);
    margin-bottom: 4px;
  }

  .kpi__value {
    font-size: 19px;
    font-weight: 700;
    font-variant-numeric: tabular-nums;
  }

  /* --- confronto ciclo su ciclo --- */
  .compare-empty {
    color: var(--muted);
    font-size: 13.5px;
  }

  .compare-categories h3 {
    font-size: 13px;
    color: var(--muted);
    text-transform: uppercase;
    letter-spacing: 0.04em;
    margin-bottom: 8px;
  }

  .compare-cat-row {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 10px;
    padding: 7px 0;
    border-bottom: 1px solid var(--border);
    font-size: 13.5px;
  }

  .compare-cat-row:last-child {
    border-bottom: none;
  }

  .compare-cat-delta {
    font-variant-numeric: tabular-nums;
    white-space: nowrap;
    font-size: 15px;
    font-weight: 700;
  }

  .compare-cat-delta.good { color: var(--good); }
  .compare-cat-delta.bad { color: var(--critical); }
  .compare-cat-delta.flat { color: var(--muted); }

  /* --- panel --- */
  .panel {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: 16px;
    box-shadow: var(--shadow);
    padding: 20px 22px;
    margin-bottom: 16px;
  }

  .panel-head {
    display: flex;
    align-items: baseline;
    justify-content: space-between;
    gap: 10px;
    margin-bottom: 14px;
  }

  .panel h2 {
    font-size: 18px;
  }

  #bank-details summary {
    cursor: pointer;
    list-style: none;
    margin-bottom: 14px;
  }
  #bank-details summary::-webkit-details-marker {
    display: none;
  }
  #bank-details summary::before {
    content: '▸';
    display: inline-block;
    width: 16px;
    color: var(--muted);
    transition: transform 0.15s ease;
  }
  #bank-details[open] summary::before {
    transform: rotate(90deg);
  }
  #bank-details summary h2 {
    display: inline;
  }

  .panel-row {
    display: grid;
    grid-template-columns: 1fr 1fr;
    align-items: start;
    gap: 16px;
  }

  .panel-row--charts {
    grid-template-columns: 1.7fr 1fr;
  }

  @media (max-width: 860px) {
    .panel-row,
    .panel-row--charts {
      grid-template-columns: 1fr;
    }
  }

  /* --- chart --- */
  .chart-wrap {
    position: relative;
  }

  svg.chart text {
    font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
    fill: var(--muted);
    font-size: 11px;
  }

  .grid-line {
    stroke: var(--grid);
    stroke-width: 1;
  }

  .bar-entrate {
    fill: var(--good);
    opacity: 0.85;
  }
  .bar-spese {
    fill: var(--critical);
    opacity: 0.72;
  }
  .line-residuo {
    stroke: var(--accent);
    stroke-width: 2.5;
    fill: none;
  }
  .line-residuo-dot {
    fill: var(--accent);
    stroke: var(--surface);
    stroke-width: 2;
  }

  .col-hit {
    fill: transparent;
    cursor: pointer;
  }

  .legend {
    display: flex;
    gap: 16px;
    flex-wrap: wrap;
    margin-top: 10px;
    font-size: 12.5px;
    color: var(--ink-2);
  }

  .legend__item {
    display: inline-flex;
    align-items: center;
    gap: 6px;
  }

  .legend__swatch {
    width: 10px;
    height: 10px;
    border-radius: 3px;
    display: inline-block;
  }
  .legend__swatch.line {
    border-radius: 50%;
  }

  .tooltip {
    position: absolute;
    pointer-events: none;
    background: var(--ink);
    color: var(--page);
    font-size: 12px;
    padding: 8px 10px;
    border-radius: 8px;
    box-shadow: var(--shadow);
    opacity: 0;
    transform: translate(-50%, -100%);
    transition: opacity 0.1s ease;
    white-space: nowrap;
    z-index: 5;
  }
  .tooltip.visible {
    opacity: 1;
  }
  .tooltip .t-row {
    display: flex;
    gap: 10px;
    justify-content: space-between;
  }
  .tooltip .t-title {
    font-weight: 700;
    margin-bottom: 4px;
  }

  /* --- donut --- */
  .donut-wrap {
    display: flex;
    align-items: center;
    gap: 20px;
    flex-wrap: wrap;
  }

  .donut-center {
    font-size: 12px;
    fill: var(--muted);
  }
  .donut-center-value {
    font-size: 17px;
    font-weight: 700;
    fill: var(--ink);
    font-variant-numeric: tabular-nums;
  }

  .donut-legend {
    display: flex;
    flex-direction: column;
    gap: 8px;
    font-size: 13px;
    flex: 1;
    min-width: 160px;
  }

  .donut-legend__row {
    display: flex;
    align-items: center;
    gap: 8px;
    justify-content: space-between;
    padding: 4px 6px;
    margin: 0 -6px;
    border-radius: 6px;
    cursor: pointer;
  }
  .donut-legend__row:hover {
    background: var(--surface-2);
  }
  .donut-legend__row.is-dim {
    opacity: 0.4;
  }
  .donut-legend__row.is-active {
    background: var(--accent-wash);
  }

  .donut-arc {
    cursor: pointer;
    transition: opacity 0.1s ease;
  }
  .donut-arc.is-dim {
    opacity: 0.35;
  }

  #extra-filter-chip {
    background: var(--accent-wash);
    border-color: var(--accent);
    color: var(--accent);
    font-weight: 600;
  }

  .donut-legend__name {
    display: flex;
    align-items: center;
    gap: 8px;
    color: var(--ink-2);
  }

  .donut-legend__value {
    font-variant-numeric: tabular-nums;
    font-weight: 600;
    color: var(--ink);
  }

  .swatch {
    width: 10px;
    height: 10px;
    border-radius: 3px;
    flex: none;
  }

  /* --- ledger table --- */
  .table-scroll {
    overflow-x: auto;
  }

  table {
    width: 100%;
    border-collapse: collapse;
    min-width: 420px;
  }

  th,
  td {
    text-align: left;
    padding: 8px 10px;
    border-bottom: 1px solid var(--border);
    font-size: 13.5px;
    white-space: nowrap;
  }

  th {
    font-size: 11px;
    letter-spacing: 0.05em;
    text-transform: uppercase;
    color: var(--muted);
    font-weight: 600;
  }

  td.amount,
  th.amount {
    text-align: right;
    font-variant-numeric: tabular-nums;
  }

  tr:last-child td {
    border-bottom: none;
  }

  th.sortable {
    cursor: pointer;
    user-select: none;
  }

  th.sortable:hover {
    color: var(--ink);
  }

  th.sortable .sort-arrow {
    display: inline-block;
    margin-left: 3px;
    opacity: 0.4;
  }

  th.sortable.sort-active .sort-arrow {
    opacity: 1;
  }

  .inactive-row td {
    color: var(--muted);
    text-decoration: line-through;
  }

  .foot-total td {
    border-top: 1px solid var(--border-strong);
    font-weight: 700;
    padding-top: 10px;
  }

  .badge {
    display: inline-block;
    font-size: 11px;
    font-weight: 600;
    padding: 2px 8px;
    border-radius: 999px;
  }
  .badge.ok {
    background: color-mix(in srgb, var(--good) 16%, transparent);
    color: var(--good);
  }
  .badge.warn {
    background: color-mix(in srgb, var(--warning) 18%, transparent);
    color: var(--warning);
  }

  .empty {
    color: var(--muted);
    font-size: 13.5px;
    padding: 6px 0 2px;
  }

  /* --- forms & buttons --- */
  .btn {
    font: inherit;
    font-size: 13.5px;
    font-weight: 600;
    padding: 7px 14px;
    border-radius: 8px;
    border: 1px solid var(--border-strong);
    background: var(--surface);
    color: var(--ink);
    cursor: pointer;
  }
  .btn:hover {
    background: var(--surface-2);
  }
  .btn-primary {
    background: var(--accent);
    border-color: var(--accent);
    color: var(--accent-ink);
  }
  .btn-primary:hover {
    filter: brightness(1.06);
  }
  .btn-ghost {
    border-color: transparent;
    background: transparent;
    padding: 4px 8px;
  }
  .btn-icon {
    font-size: 12px;
    padding: 4px 9px;
    border-radius: 6px;
  }
  .btn:focus-visible,
  input:focus-visible,
  select:focus-visible {
    outline: 2px solid var(--accent);
    outline-offset: 1px;
  }

  .row-actions {
    display: flex;
    gap: 6px;
    justify-content: flex-end;
  }

  .entry-form {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(120px, 1fr));
    gap: 10px;
    margin-top: 14px;
    padding-top: 14px;
    border-top: 1px dashed var(--border-strong);
  }

  .entry-form.editing {
    border-top-color: var(--accent);
  }

  .field {
    display: flex;
    flex-direction: column;
    gap: 4px;
    min-width: 0;
  }

  .field label {
    font-size: 11px;
    letter-spacing: 0.04em;
    text-transform: uppercase;
    color: var(--muted);
  }

  .field input,
  .field select {
    font: inherit;
    font-size: 13.5px;
    padding: 7px 9px;
    border-radius: 8px;
    border: 1px solid var(--border-strong);
    background: var(--surface);
    color: var(--ink);
    width: 100%;
  }

  .field-check {
    flex-direction: row;
    align-items: center;
    gap: 6px;
    justify-content: flex-start;
  }

  .form-actions {
    grid-column: 1 / -1;
    display: flex;
    gap: 8px;
    justify-content: flex-end;
    align-items: center;
  }

  .form-msg {
    font-size: 12.5px;
    color: var(--critical);
    margin-right: auto;
  }

  footer {
    text-align: center;
    color: var(--muted);
    font-size: 12.5px;
    margin-top: 20px;
    line-height: 1.6;
  }

  footer code {
    background: var(--surface-2);
    border: 1px solid var(--border);
    padding: 1px 6px;
    border-radius: 5px;
    font-size: 11.5px;
  }

  @media (prefers-reduced-motion: reduce) {
    * {
      transition: none !important;
    }
  }
