.invoices-page {
  .stats-bar {
    display: flex;
    gap: 1rem;
    margin-bottom: 1rem;

    .stat-card {
      display: flex;
      align-items: center;
      gap: 0.75rem;
      padding: 0.75rem 1.25rem;
      border-radius: 8px;
      background: var(--color-surface-muted);
      border: 1px solid var(--color-surface-border);

      .stat-icon {
        font-size: 1.25rem;
        width: 36px;
        height: 36px;
        border-radius: 50%;
        display: flex;
        align-items: center;
        justify-content: center;
      }

      &.outstanding .stat-icon {
        background: rgba(59, 130, 246, 0.15);
        color: #3b82f6;
      }

      &.overdue .stat-icon {
        background: rgba(239, 68, 68, 0.15);
        color: #ef4444;
      }

      .stat-info {
        display: flex;
        flex-direction: column;
        gap: 0.15rem;

        .stat-label {
          font-size: 0.75rem;
          color: var(--color-text-muted);
          font-weight: 600;
          text-transform: uppercase;
        }

        .stat-value {
          font-family: 'Courier New', monospace;
          font-size: 1.1rem;
          font-weight: 700;
          color: var(--color-text-primary);
        }
      }
    }
  }

  .action-buttons {
    display: flex;
    gap: 0.1rem;
  }

  .amount {
    font-family: 'Courier New', monospace;
    font-weight: 600;
    text-align: end;
  }

  .mono {
    font-family: 'Courier New', monospace;
    font-weight: 600;
    font-size: 0.8rem;
    color: var(--color-text-secondary);
  }

  .text-danger {
    color: var(--color-danger, #ef4444) !important;
    font-weight: 600;
  }

  .overdue-row {
    background: rgba(239, 68, 68, 0.05) !important;
  }
}

// Near full-screen invoice dialog
:host ::ng-deep {
  .p-dialog:not(.p-dialog-maximized) {
    max-width: 80vw;
    max-height: 92vh;

    .p-dialog-content {
      max-height: calc(92vh - 60px);
      overflow-y: auto;
    }
  }

  // Light button styling in the dark lines-header
  .header-add-btn {
    border-color: rgba(255,255,255,0.4) !important;
    color: #fff !important;
    font-size: 0.72rem !important;
    padding: 0.2rem 0.5rem !important;
    &:hover {
      background: rgba(255,255,255,0.15) !important;
      border-color: rgba(255,255,255,0.6) !important;
    }
  }
}

.invoice-form {
  overflow-x: hidden;

  // ── Global compact overrides for PrimeNG inside the form ──
  :host ::ng-deep {
    .p-select .p-select-label { padding: 0.35rem 0.5rem; font-size: 0.8rem; }
    .p-inputtext { padding: 0.35rem 0.5rem; font-size: 0.8rem; }
    .p-inputnumber-input { padding: 0.35rem 0.5rem; font-size: 0.8rem; }
    .p-datepicker .p-inputtext { padding: 0.35rem 0.5rem; font-size: 0.8rem; }
  }

  // ── Primary form strip — the "header band" ──
  .form-strip {
    display: flex;
    flex-wrap: wrap;
    gap: 0.5rem 0.75rem;
    margin-bottom: 0.5rem;
    padding: 0.6rem 0.75rem;
    background: var(--color-surface-muted);
    border-radius: 8px;
    border: 1px solid var(--color-surface-border);
    border-inline-start: 3px solid var(--color-accent, #f5a623);

    &.secondary {
      background: transparent;
      border: none;
      border-inline-start: none;
      padding: 0.35rem 0.75rem;
      margin-bottom: 0.25rem;
    }
  }

  .form-strip-field {
    display: flex;
    flex-direction: column;
    gap: 0.2rem;
    flex: 1;
    min-width: 120px;
    max-width: 240px;

    &.grow-2 { flex: 2; min-width: 180px; max-width: 360px; }

    label {
      font-weight: 700;
      font-size: 0.65rem;
      color: var(--color-text-secondary);
      text-transform: uppercase;
      letter-spacing: 0.03em;
      white-space: nowrap;
    }

    input, textarea { width: 100%; }
  }

  .field-settings-panel {
    display: flex;
    align-items: center;
    gap: 1rem;
    padding: 0.4rem 0.75rem;
    margin-bottom: 0.5rem;
    background: var(--color-surface-hover);
    border: 1px dashed var(--color-surface-border);
    border-radius: 8px;

    .field-settings-grid {
      display: flex;
      flex-wrap: wrap;
      gap: 0.75rem;
      flex: 1;
    }

    .field-toggle-item {
      display: flex;
      align-items: center;
      gap: 0.35rem;
      font-size: 0.8rem;
      white-space: nowrap;
    }
  }

  .form-grid {
    display: grid;
    grid-template-columns: repeat(4, 1fr);
    gap: 0.75rem;
    margin-bottom: 0.75rem;

    .form-field {
      display: flex;
      flex-direction: column;
      gap: 0.25rem;

      label {
        font-weight: 600;
        font-size: 0.75rem;
        color: var(--color-text-primary);
      }

      input,
      textarea {
        width: 100%;
      }

      &.span-2 {
        grid-column: span 2;
      }
    }
  }

  .customer-balance-badge {
    display: inline-flex;
    align-items: center;
    gap: 2px;
    font-size: 0.6rem;
    font-weight: 700;
    padding: 1px 4px;
    border-radius: 3px;
    margin-inline-start: 4px;
    vertical-align: middle;

    &.danger {
      background: rgba(239, 68, 68, 0.15);
      color: #ef4444;
    }
    &.success {
      background: rgba(34, 197, 94, 0.15);
      color: #22c55e;
    }
    &.loading {
      color: #94a3b8;
    }
    i { font-size: 0.55rem; }
  }

  // ── Line Items Section ──
  .lines-section {
    margin-top: 0.25rem;
    border: 1px solid var(--color-surface-border);
    border-radius: 8px;
    overflow: hidden;

    .lines-header {
      display: flex;
      justify-content: space-between;
      align-items: center;
      padding: 0.4rem 0.6rem;
      background: var(--module-color, var(--color-brand));

      h3 {
        margin: 0;
        font-size: 0.8rem;
        font-weight: 700;
        color: #ffffff;
        letter-spacing: 0.02em;
      }
    }

    .barcode-row-inline {
      display: flex;
      align-items: center;
      gap: 0.35rem;
      .barcode-icon { font-size: 1rem; color: rgba(255,255,255,0.6); }
      .barcode-input {
        width: 180px;
        font-family: monospace;
        font-size: 0.75rem;
        background: rgba(255,255,255,0.12);
        border: 1px solid rgba(255,255,255,0.2);
        color: #fff;
        border-radius: 4px;
        padding: 0.25rem 0.4rem;
        &::placeholder { color: rgba(255,255,255,0.4); }
      }
    }

    .barcode-row {
      display: flex;
      align-items: center;
      gap: 0.5rem;
      margin-bottom: 0.5rem;
      .barcode-icon { font-size: 1.2rem; color: #64748b; }
      .barcode-input { flex: 1; max-width: 350px; font-family: monospace; }
    }

    .row-barcode {
      font-size: 0.65rem;
      font-family: monospace;
      color: #94a3b8;
      margin-top: 1px;
    }

    .stock-badge {
      font-size: 0.7rem;
      color: #22c55e;
      margin-top: 2px;
      display: flex;
      align-items: center;
      gap: 3px;
      &.stock-zero { color: #ef4444; }
    }

    .lines-table {
      width: 100%;
      border-collapse: collapse;
      font-size: 0.75rem;
      table-layout: fixed;

      thead th {
        background: var(--color-surface-hover);
        padding: 0.4rem 0.3rem;
        text-align: start;
        font-weight: 700;
        font-size: 0.68rem;
        color: var(--color-text-secondary);
        border-bottom: 2px solid var(--color-surface-border);
        text-transform: uppercase;
        letter-spacing: 0.03em;
        white-space: nowrap;
      }

      tbody td {
        padding: 0.2rem 0.2rem;
        border-bottom: 1px solid var(--color-surface-hover);
        vertical-align: middle;
        overflow: hidden;

        input { width: 100%; }
      }

      tbody tr:hover td {
        background: rgba(245, 166, 35, 0.04);
      }

      :host ::ng-deep {
        .p-select { font-size: 0.75rem; }
        .p-select .p-select-label { padding: 0.25rem 0.35rem; font-size: 0.75rem; }
        .p-inputnumber-input { padding: 0.25rem 0.3rem; font-size: 0.75rem; }
      }

      .col-num { width: 28px; text-align: center; }
      .col-product { width: 22%; }
      .col-warehouse { width: 13%; }
      .col-unit { width: 10%; }
      .col-qty { width: 8%; }
      .col-price { width: 10%; }
      .col-discount { width: 8%; }
      .col-tax { width: 13%; }
      .col-total { width: 11%; text-align: end; }
      .col-action { width: 32px; }

      .row-num {
        text-align: center;
        font-weight: 700;
        font-size: 0.7rem;
        color: var(--color-text-muted);
      }

      .line-total-cell {
        font-family: 'Courier New', monospace;
        font-weight: 600;
        text-align: end;
        padding-inline-end: 0.75rem;
        color: var(--module-color, var(--color-brand));
      }

      .amount {
        font-family: 'Courier New', monospace;
        font-weight: 600;
        text-align: end;
      }
    }

    .no-items {
      text-align: center;
      padding: 1.5rem;
      color: var(--color-text-muted);
      font-size: 0.875rem;
    }
  }

  .bottom-row {
    display: flex;
    gap: 1rem;
    margin: 0.5rem 0;
    align-items: flex-start;

    .notes-compact {
      flex: 1;
      display: flex;
      flex-direction: column;
      gap: 0.4rem;
    }
  }

  .totals-section {
    display: flex;
    flex-direction: column;
    align-items: flex-end;
    gap: 0.35rem;
    padding: 0.6rem 0.75rem;
    background: var(--color-surface-muted);
    border-radius: 8px;
    border: 1px solid var(--color-surface-border);
    min-width: 240px;

    .totals-row {
      display: flex;
      justify-content: space-between;
      width: 100%;
      font-size: 0.8rem;

      .totals-label {
        color: var(--color-text-secondary);
        font-weight: 600;
      }

      .totals-value {
        font-family: 'Courier New', monospace;
        font-weight: 600;
        color: var(--color-text-primary);

        &.discount {
          color: var(--color-danger);
        }
      }

      &.grand-total {
        border-top: 2px solid var(--module-color, var(--color-brand));
        padding-top: 0.5rem;
        margin-top: 0.15rem;
        font-size: 0.95rem;

        .totals-label {
          color: var(--module-color, var(--color-brand));
          font-weight: 700;
        }

        .totals-value {
          color: var(--module-color, var(--color-brand));
          font-size: 1.05rem;
          font-weight: 700;
        }
      }

      &.overall-discount-row {
        align-items: center;
        padding: 0.25rem 0;

        .overall-discount-input {
          display: flex;
          align-items: center;
          gap: 0.25rem;

          :host ::ng-deep {
            .discount-input-sm {
              padding: 0.2rem 0.3rem;
              font-size: 0.75rem;
              text-align: end;
            }
            .p-button {
              padding: 0.2rem 0.4rem;
              font-size: 0.7rem;
            }
          }
        }
      }
    }
  }

  // ── Inline Payment Section ──
  .inline-pay-section {
    margin-top: 0.5rem;
    padding: 0.5rem 0.75rem;
    border-radius: 8px;
    border: 1px dashed var(--color-surface-border);
    background: rgba(34, 197, 94, 0.04);
    transition: all 0.2s;

    &:has(p-toggleSwitch .p-toggleswitch-checked) {
      border-color: rgba(34, 197, 94, 0.3);
      background: rgba(34, 197, 94, 0.06);
    }
  }

  .inline-pay-toggle {
    display: flex;
    align-items: center;
    gap: 0.5rem;

    .inline-pay-label {
      font-weight: 600;
      font-size: 0.8rem;
      color: var(--color-text-primary);
    }

    .inline-pay-quick-btns {
      margin-inline-start: auto;
    }
  }

  .inline-pay-fields {
    display: flex;
    flex-wrap: wrap;
    gap: 0.5rem;
    margin-top: 0.5rem;
  }

  .inline-pay-field {
    display: flex;
    flex-direction: column;
    gap: 0.2rem;
    flex: 1;
    min-width: 140px;
    max-width: 220px;

    &.grow { flex: 2; min-width: 200px; max-width: 360px; }

    label {
      font-weight: 700;
      font-size: 0.65rem;
      color: var(--color-text-secondary);
      text-transform: uppercase;
      letter-spacing: 0.03em;
    }

    input { width: 100%; }
  }
}

.dialog-custom-footer {
  display: flex;
  justify-content: flex-end;
  gap: 0.5rem;
  padding-top: 1rem;
  border-top: 1px solid var(--color-surface-border);
}

// ─── View Invoice Detail ──────────────────────────────────────

.view-invoice {
  .view-header-info {
    display: flex;
    flex-wrap: wrap;
    gap: 1.5rem;
    margin-bottom: 1rem;
    padding-bottom: 1rem;
    border-bottom: 1px solid var(--color-surface-border);

    .info-item {
      display: flex;
      flex-direction: column;
      gap: 0.25rem;

      label {
        font-size: 0.75rem;
        color: var(--color-text-muted);
        font-weight: 600;
        text-transform: uppercase;
      }
      span {
        font-size: 0.8125rem;
      }
      .mono {
        font-family: 'Courier New', monospace;
        font-weight: 600;
      }
    }
  }

  .summary-cards {
    display: grid;
    grid-template-columns: repeat(4, 1fr);
    gap: 0.75rem;
    margin-bottom: 1rem;

    .summary-card {
      padding: 0.75rem;
      border-radius: 8px;
      background: var(--color-surface-muted);
      border: 1px solid var(--color-surface-border);
      display: flex;
      flex-direction: column;
      gap: 0.25rem;

      .card-label {
        font-size: 0.7rem;
        color: var(--color-text-muted);
        font-weight: 600;
        text-transform: uppercase;
      }

      .card-value {
        font-family: 'Courier New', monospace;
        font-size: 1.1rem;
        font-weight: 700;
        color: var(--color-text-primary);
      }

      &.paid .card-value {
        color: #22c55e;
      }

      &.overdue-card {
        border-color: #ef4444;
        background: rgba(239, 68, 68, 0.08);

        .card-value {
          color: #ef4444;
        }
      }
    }
  }

  .post-action-bar {
    display: flex;
    justify-content: center;
    margin-bottom: 1rem;
    padding: 0.75rem;
    background: var(--color-surface-muted);
    border-radius: 8px;
    border: 1px dashed var(--color-surface-border);
  }

  .view-lines-table {
    width: 100%;
    border-collapse: collapse;
    font-size: 0.8125rem;

    thead th {
      background: var(--color-surface-muted);
      padding: 0.5rem;
      text-align: start;
      font-weight: 600;
      font-size: 0.8rem;
      color: var(--color-text-secondary);
      border-bottom: 1px solid var(--color-surface-border);
    }

    tbody td {
      padding: 0.4rem 0.5rem;
      border-bottom: 1px solid var(--color-surface-hover);
    }

    tfoot td {
      padding: 0.4rem 0.5rem;
      border-top: none;
    }

    .grand-total-row td {
      border-top: 1px solid var(--color-surface-border);
      background: var(--color-surface-muted);
      padding: 0.5rem;
    }

    .amount {
      text-align: end;
      font-family: 'Courier New', monospace;
    }

    .discount {
      color: var(--color-danger);
    }

    .mono {
      font-family: 'Courier New', monospace;
      font-weight: 600;
      font-size: 0.8rem;
    }
  }

  .loading-spinner {
    display: flex;
    justify-content: center;
    padding: 2rem;
    color: var(--color-text-muted);
  }

  .empty-tab {
    text-align: center;
    padding: 2rem;
    color: var(--color-text-muted);
    font-size: 0.875rem;
  }

  .je-section {
    display: flex;
    flex-direction: column;
    gap: 1rem;
    padding: 0.5rem 0;

    .je-block {
      background: var(--surface-ground);
      border-radius: 8px;
      padding: 0.75rem;

      .je-block-header {
        display: flex;
        align-items: center;
        gap: 0.5rem;
        margin-bottom: 0.5rem;
        i { color: var(--primary-color); font-size: 1.1rem; }
        .je-block-title { font-weight: 600; font-size: 0.85rem; }
        .je-number { font-family: monospace; font-weight: 700; font-size: 0.85rem; }
      }

      .je-lines-table {
        width: 100%;
        border-collapse: collapse;
        font-size: 0.85rem;
        th, td { padding: 0.35rem 0.5rem; text-align: start; }
        th { background: var(--surface-hover); font-weight: 600; }
        td { border-bottom: 1px solid var(--surface-border); }
        .text-end { text-align: end; }
      }
    }

    .je-meta {
      font-size: 0.75rem;
      color: var(--text-color-secondary);
      padding-inline-start: 0.5rem;
    }
  }

  .view-description {
    margin-top: 1rem;
    label {
      font-size: 0.75rem;
      color: var(--color-text-muted);
      font-weight: 600;
      text-transform: uppercase;
      display: block;
      margin-bottom: 0.25rem;
    }
    p {
      margin: 0 0 0.5rem;
      color: var(--color-text-primary);
    }
  }

  .cancellation-banner {
    display: flex;
    align-items: center;
    gap: 0.75rem;
    padding: 0.75rem 1rem;
    margin-top: 1rem;
    border-radius: 8px;
    background: rgba(239, 68, 68, 0.08);
    border: 1px solid rgba(239, 68, 68, 0.3);

    > i {
      font-size: 1.5rem;
      color: #ef4444;
    }

    .cancel-info {
      display: flex;
      flex-direction: column;
      gap: 0.15rem;
    }

    .cancel-title {
      font-weight: 700;
      color: #ef4444;
      font-size: 0.875rem;
    }

    .cancel-meta {
      font-size: 0.75rem;
      color: var(--color-text-muted);
    }

    .cancel-reason {
      font-size: 0.8rem;
      color: var(--color-text-secondary);
    }
  }

  .text-danger {
    color: var(--color-danger, #ef4444) !important;
    font-weight: 600;
  }
}

// ─── Payment Dialog ──────────────────────────────────────

.payment-dialog {
  .payment-summary {
    background: var(--color-surface-muted);
    border-radius: 8px;
    padding: 0.75rem 1rem;
    margin-bottom: 1rem;
    border: 1px solid var(--color-surface-border);

    .pay-info-row {
      display: flex;
      justify-content: space-between;
      padding: 0.25rem 0;
      font-size: 0.8125rem;

      .mono {
        font-family: 'Courier New', monospace;
        font-weight: 600;
      }

      .amount {
        font-family: 'Courier New', monospace;
        font-weight: 600;
      }

      .highlight {
        color: var(--module-color, var(--color-brand));
        font-size: 1rem;
      }
    }
  }

  .payment-form {
    .pay-grid {
      display: grid;
      grid-template-columns: 1fr 1fr;
      gap: 1rem;
      margin-bottom: 1rem;

      .pay-field {
        display: flex;
        flex-direction: column;
        gap: 0.375rem;

        label {
          font-weight: 600;
          font-size: 0.8125rem;
          color: var(--color-text-primary);
        }

        input, textarea {
          width: 100%;
        }

        &.full {
          grid-column: span 2;
        }
      }
    }
  }
}
