/* ===== RESET & BASE ===== */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

:root {
  --blue: #2563eb;
  --blue-light: #dbeafe;
  --green: #16a34a;
  --green-light: #dcfce7;
  --amber: #d97706;
  --amber-light: #fef3c7;
  --red: #dc2626;
  --red-light: #fee2e2;
  --gray-50: #f9fafb;
  --gray-100: #f3f4f6;
  --gray-200: #e5e7eb;
  --gray-400: #9ca3af;
  --gray-600: #4b5563;
  --gray-800: #1f2937;
  --gray-900: #111827;
  --radius: 6px;
  --shadow: 0 1px 3px rgba(0,0,0,.1);
}

body {
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
  font-size: 14px;
  color: var(--gray-900);
  background: var(--gray-50);
  min-height: 100vh;
}

/* ===== NAV ===== */
#nav {
  position: sticky;
  top: 0;
  z-index: 100;
  display: flex;
  align-items: center;
  gap: 24px;
  padding: 0 32px;
  height: 56px;
  background: white;
  border-bottom: 1px solid var(--gray-200);
  box-shadow: var(--shadow);
}

.nav-brand {
  font-weight: 700;
  font-size: 16px;
  color: var(--blue);
  flex-shrink: 0;
}

.nav-links {
  display: flex;
  gap: 4px;
  flex: 1;
}

.nav-link {
  padding: 6px 12px;
  border-radius: var(--radius);
  text-decoration: none;
  color: var(--gray-600);
  font-weight: 500;
  transition: background .15s, color .15s;
}

.nav-link:hover, .nav-link.active {
  background: var(--blue-light);
  color: var(--blue);
}

.nav-actions {
  display: flex;
  gap: 8px;
}

/* ===== BUTTONS ===== */
.btn {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 7px 14px;
  border: 1px solid transparent;
  border-radius: var(--radius);
  font-size: 13px;
  font-weight: 500;
  cursor: pointer;
  text-decoration: none;
  white-space: nowrap;
  transition: all .15s;
}

.btn-primary {
  background: var(--blue);
  color: white;
  border-color: var(--blue);
}
.btn-primary:hover { background: #1d4ed8; }

.btn-secondary {
  background: white;
  color: var(--gray-800);
  border-color: var(--gray-200);
}
.btn-secondary:hover { background: var(--gray-50); border-color: var(--gray-400); }

.btn-success {
  background: var(--green);
  color: white;
  border-color: var(--green);
}
.btn-success:hover { background: #15803d; }

.btn-danger {
  background: white;
  color: var(--red);
  border-color: var(--red);
}
.btn-danger:hover { background: var(--red-light); }

.btn-sm { padding: 4px 10px; font-size: 12px; }

.btn:disabled {
  opacity: .5;
  cursor: not-allowed;
}

/* ===== MAIN CONTENT ===== */
#app {
  max-width: 1100px;
  margin: 0 auto;
  padding: 32px 24px;
}

.loading {
  text-align: center;
  color: var(--gray-400);
  padding: 60px;
}

/* ===== PAGE HEADER ===== */
.page-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 24px;
}

.page-title {
  font-size: 22px;
  font-weight: 700;
  color: var(--gray-900);
}

.page-back {
  display: flex;
  align-items: center;
  gap: 8px;
}

/* ===== STATS CARDS ===== */
.stats-row {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 16px;
  margin-bottom: 24px;
}

.stat-card {
  background: white;
  border: 1px solid var(--gray-200);
  border-radius: var(--radius);
  padding: 18px 20px;
  box-shadow: var(--shadow);
}

.stat-label {
  font-size: 12px;
  font-weight: 500;
  color: var(--gray-600);
  text-transform: uppercase;
  letter-spacing: .05em;
  margin-bottom: 6px;
}

.stat-value {
  font-size: 26px;
  font-weight: 700;
  color: var(--gray-900);
}

.stat-card.green .stat-value { color: var(--green); }
.stat-card.amber .stat-value { color: var(--amber); }

/* ===== FILTERS ===== */
.filters-row {
  display: flex;
  align-items: center;
  gap: 12px;
  margin-bottom: 16px;
  flex-wrap: wrap;
}

.filter-tabs {
  display: flex;
  gap: 2px;
  background: var(--gray-100);
  border-radius: var(--radius);
  padding: 3px;
}

.filter-tab {
  padding: 5px 12px;
  border-radius: 4px;
  border: none;
  background: transparent;
  font-size: 13px;
  font-weight: 500;
  color: var(--gray-600);
  cursor: pointer;
  transition: all .15s;
}

.filter-tab.active {
  background: white;
  color: var(--blue);
  box-shadow: var(--shadow);
}

.search-input {
  flex: 1;
  min-width: 200px;
  padding: 7px 12px;
  border: 1px solid var(--gray-200);
  border-radius: var(--radius);
  font-size: 13px;
  outline: none;
  background: white;
}

.search-input:focus { border-color: var(--blue); }

.year-select {
  padding: 7px 10px;
  border: 1px solid var(--gray-200);
  border-radius: var(--radius);
  font-size: 13px;
  background: white;
  cursor: pointer;
}

/* ===== TABLE ===== */
.table-wrap {
  background: white;
  border: 1px solid var(--gray-200);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  overflow: hidden;
}

table {
  width: 100%;
  border-collapse: collapse;
}

thead th {
  padding: 10px 14px;
  text-align: left;
  font-size: 12px;
  font-weight: 600;
  color: var(--gray-600);
  text-transform: uppercase;
  letter-spacing: .05em;
  background: var(--gray-50);
  border-bottom: 1px solid var(--gray-200);
}

tbody tr {
  border-bottom: 1px solid var(--gray-100);
  cursor: pointer;
  transition: background .1s;
}

tbody tr:last-child { border-bottom: none; }
tbody tr:hover { background: var(--gray-50); }

tbody td {
  padding: 12px 14px;
  vertical-align: middle;
}

.td-number { font-weight: 600; color: var(--gray-900); font-variant-numeric: tabular-nums; }
.td-amount { font-weight: 600; text-align: right; font-variant-numeric: tabular-nums; }
.td-actions { text-align: right; white-space: nowrap; }

.empty-row td {
  text-align: center;
  padding: 40px;
  color: var(--gray-400);
  cursor: default;
}
.empty-row:hover { background: white !important; }

/* ===== STATUS BADGES ===== */
.badge {
  display: inline-flex;
  align-items: center;
  padding: 3px 8px;
  border-radius: 999px;
  font-size: 11px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: .05em;
}

.badge-borrador { background: var(--gray-100); color: var(--gray-600); }
.badge-enviada  { background: var(--blue-light); color: var(--blue); }
.badge-pagada   { background: var(--green-light); color: var(--green); }

/* ===== FORM ===== */
.form-card {
  background: white;
  border: 1px solid var(--gray-200);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 28px 32px;
  margin-bottom: 20px;
}

.form-section-title {
  font-size: 13px;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: .06em;
  color: var(--gray-600);
  margin-bottom: 16px;
  padding-bottom: 8px;
  border-bottom: 1px solid var(--gray-100);
}

.form-grid {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 16px;
}

.form-grid-3 {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 16px;
}

.form-group {
  display: flex;
  flex-direction: column;
  gap: 5px;
}

.form-group.full { grid-column: 1 / -1; }

label {
  font-size: 12px;
  font-weight: 600;
  color: var(--gray-600);
  text-transform: uppercase;
  letter-spacing: .04em;
}

input, select, textarea {
  padding: 8px 10px;
  border: 1px solid var(--gray-200);
  border-radius: var(--radius);
  font-size: 14px;
  font-family: inherit;
  color: var(--gray-900);
  background: white;
  outline: none;
  transition: border-color .15s;
}

input:focus, select:focus, textarea:focus { border-color: var(--blue); }
textarea { resize: vertical; min-height: 72px; }

.form-row {
  display: flex;
  gap: 12px;
  align-items: flex-end;
  margin-bottom: 12px;
}

/* ===== TOGGLES ===== */
.toggles-row {
  display: flex;
  gap: 24px;
  margin-bottom: 20px;
}

.toggle-label {
  display: flex;
  align-items: center;
  gap: 8px;
  cursor: pointer;
  font-size: 14px;
  font-weight: 500;
  user-select: none;
}

.toggle-label input[type="checkbox"] {
  width: 16px;
  height: 16px;
  accent-color: var(--blue);
  padding: 0;
  border: none;
}

/* ===== INVOICE LINES TABLE ===== */
.lines-table {
  width: 100%;
  border-collapse: collapse;
  margin-bottom: 12px;
}

.lines-table th {
  padding: 8px 10px;
  text-align: left;
  font-size: 11px;
  font-weight: 700;
  color: var(--gray-600);
  text-transform: uppercase;
  letter-spacing: .05em;
  background: var(--gray-50);
  border: 1px solid var(--gray-200);
}

.lines-table td {
  padding: 6px 8px;
  border: 1px solid var(--gray-200);
  vertical-align: top;
}

.lines-table .col-desc { width: 55%; }
.lines-table .col-qty  { width: 10%; }
.lines-table .col-rate { width: 13%; }
.lines-table .col-amt  { width: 14%; }
.lines-table .col-del  { width: 8%; text-align: center; }

.lines-table textarea { min-height: 52px; width: 100%; }
.lines-table input[type="number"] { width: 100%; text-align: right; }

.line-amount-display {
  display: block;
  padding: 8px 10px;
  text-align: right;
  font-weight: 600;
  font-variant-numeric: tabular-nums;
  background: var(--gray-50);
  border-radius: 4px;
  min-height: 36px;
}

/* ===== TOTALS ===== */
.totals-block {
  display: flex;
  justify-content: flex-end;
  margin-top: 8px;
}

.totals-table {
  min-width: 280px;
}

.totals-table tr td {
  padding: 5px 10px;
  font-size: 14px;
}

.totals-table tr td:first-child { color: var(--gray-600); }
.totals-table tr td:last-child  { text-align: right; font-weight: 600; font-variant-numeric: tabular-nums; }

.totals-table .total-row td {
  font-size: 16px;
  font-weight: 700;
  padding-top: 8px;
  border-top: 2px solid var(--gray-900);
}

/* ===== FORM ACTIONS ===== */
.form-actions {
  display: flex;
  gap: 10px;
  justify-content: flex-end;
  padding-top: 20px;
  border-top: 1px solid var(--gray-100);
}

/* ===== INVOICE DETAIL ===== */
.detail-actions {
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
  margin-bottom: 24px;
}

.invoice-preview {
  background: white;
  border: 1px solid var(--gray-200);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 40px 48px;
  max-width: 800px;
}

/* ===== INVOICE DOCUMENT (screen preview + print) ===== */
.inv-doc {
  font-family: Georgia, 'Times New Roman', serif;
  color: #1a1a1a;
  font-size: 13px;
  line-height: 1.5;
}

.inv-header {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 24px;
  margin-bottom: 32px;
}

.inv-emisor { }

.inv-emisor-name {
  font-size: 15px;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: .04em;
  margin-bottom: 4px;
}

.inv-emisor-detail {
  font-size: 12px;
  color: #444;
  line-height: 1.6;
}

.inv-meta {
  text-align: right;
}

.inv-type {
  font-size: 22px;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: .06em;
  margin-bottom: 8px;
}

.inv-meta-row {
  font-size: 13px;
  line-height: 1.7;
}

.inv-meta-label {
  color: #666;
  margin-right: 4px;
}

.inv-client {
  margin-bottom: 28px;
  padding: 16px 20px;
  background: #f8f8f8;
  border-left: 3px solid #1a1a1a;
}

.inv-client-name {
  font-weight: 700;
  font-size: 14px;
  margin-bottom: 4px;
}

.inv-client-detail {
  font-size: 12px;
  color: #444;
  line-height: 1.6;
}

.inv-table {
  width: 100%;
  border-collapse: collapse;
  margin-bottom: 20px;
}

.inv-table th {
  padding: 9px 12px;
  text-align: left;
  font-size: 11px;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: .05em;
  border-bottom: 2px solid #1a1a1a;
  border-top: 2px solid #1a1a1a;
}

.inv-table th:not(:first-child) { text-align: right; }

.inv-table td {
  padding: 10px 12px;
  border-bottom: 1px solid #e0e0e0;
  vertical-align: top;
  font-size: 13px;
  white-space: pre-wrap;
}

.inv-table td:not(:first-child) {
  text-align: right;
  white-space: nowrap;
  font-variant-numeric: tabular-nums;
}

.inv-totals {
  display: flex;
  justify-content: flex-end;
  margin-bottom: 32px;
}

.inv-totals-inner {
  min-width: 240px;
}

.inv-total-row {
  display: flex;
  justify-content: space-between;
  gap: 32px;
  padding: 4px 0;
  font-size: 13px;
}

.inv-total-row .lbl { color: #555; }
.inv-total-row .amt { font-weight: 600; font-variant-numeric: tabular-nums; }

.inv-total-final {
  display: flex;
  justify-content: space-between;
  gap: 32px;
  padding: 8px 0 4px;
  border-top: 2px solid #1a1a1a;
  margin-top: 4px;
  font-size: 15px;
  font-weight: 700;
}

.inv-footer {
  padding-top: 20px;
  border-top: 1px solid #ccc;
  font-size: 11.5px;
  color: #444;
  line-height: 1.7;
}

.inv-footer-title {
  font-weight: 700;
  color: #1a1a1a;
  margin-bottom: 4px;
  font-size: 12px;
  text-transform: uppercase;
  letter-spacing: .04em;
}

.inv-iban {
  font-weight: 600;
  color: #1a1a1a;
  margin-bottom: 8px;
}

.inv-igic {
  margin-top: 8px;
  padding: 10px 12px;
  background: #f5f5f5;
  border-left: 2px solid #999;
  font-size: 11px;
  color: #555;
  font-style: italic;
}

/* ===== CLIENT LIST ===== */
.client-detail {
  font-size: 12px;
  color: var(--gray-400);
}

/* ===== BACKUP VIEW ===== */
.backup-card {
  background: white;
  border: 1px solid var(--gray-200);
  border-radius: var(--radius);
  padding: 24px;
  margin-bottom: 16px;
  box-shadow: var(--shadow);
}

.backup-card h3 {
  font-size: 15px;
  font-weight: 600;
  margin-bottom: 8px;
}

.backup-card p {
  font-size: 13px;
  color: var(--gray-600);
  margin-bottom: 16px;
}

/* ===== MODAL ===== */
.modal-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,.45);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 200;
}

.modal-overlay.hidden { display: none; }

.modal-box {
  background: white;
  border-radius: 8px;
  padding: 28px 32px;
  width: 400px;
  max-width: 90vw;
  box-shadow: 0 20px 40px rgba(0,0,0,.2);
}

.modal-box h3 {
  font-size: 16px;
  font-weight: 700;
  margin-bottom: 16px;
}

.modal-actions {
  display: flex;
  gap: 10px;
  justify-content: flex-end;
  margin-top: 20px;
}

/* ===== MISC ===== */
.hidden { display: none !important; }

.text-right { text-align: right; }
.text-gray   { color: var(--gray-400); }
.font-mono   { font-family: 'SF Mono', Monaco, monospace; }

.alert {
  padding: 12px 16px;
  border-radius: var(--radius);
  font-size: 13px;
  margin-bottom: 16px;
}
.alert-error { background: var(--red-light); color: var(--red); }
.alert-success { background: var(--green-light); color: var(--green); }

/* Inline new client toggle */
.inline-client-form {
  margin-top: 12px;
  padding: 16px;
  background: var(--gray-50);
  border: 1px solid var(--gray-200);
  border-radius: var(--radius);
}

/* ===== NOTA VISIBLE EN FACTURA ===== */
.inv-nota {
  margin-top: 12px;
  padding: 10px 12px;
  background: #f9f9f9;
  border-left: 2px solid #ccc;
  font-size: 12px;
  color: #444;
  white-space: pre-wrap;
}

/* ===== PRINT STYLES ===== */
@media screen {
  #print-area { display: none; }
}

@media print {
  /* margin:0 en @page elimina cabecera y pie del navegador (URL, fecha, pagina) */
  @page {
    size: A4 portrait;
    margin: 0;
  }

  * { -webkit-print-color-adjust: exact; print-color-adjust: exact; }

  #nav, #app, #modal-overlay { display: none !important; }

  html, body {
    margin: 0;
    padding: 0;
    background: white;
  }

  #print-area {
    display: block !important;
    width: 210mm;
    min-height: 297mm;
    padding: 18mm 22mm 18mm 22mm;
    margin: 0;
    background: white;
    box-sizing: border-box;
  }

  #print-area .inv-doc { font-size: 11.5pt; }
  #print-area .inv-type { font-size: 19pt; }
  #print-area .inv-table th,
  #print-area .inv-table td { font-size: 10.5pt; }
  #print-area .inv-footer { font-size: 10pt; }
}
