:root {
  --keys-blue: #103B70;
  --keys-blue-dark: #0B2A51;
  --keys-blue-darker: #071D38;
  --keys-blue-soft: #1E4D8E;
  --keys-yellow: #FDD504;
  --keys-yellow-dark: #E3BE03;
  --bg: #F2F5FA;
  --surface: #FFFFFF;
  --border: #E1E7F0;
  --text: #1B2A41;
  --text-soft: #5A6B85;
  --danger: #C0392B;
  --danger-bg: #FDEBEA;
  --ok: #1E7A46;
  --radius: 14px;
  --shadow: 0 4px 18px rgba(16, 59, 112, 0.08);
  --shadow-lg: 0 12px 34px rgba(16, 59, 112, 0.16);
}

* { box-sizing: border-box; }

[hidden] { display: none !important; }

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

body {
  font-family: "Segoe UI", system-ui, -apple-system, Roboto, "Helvetica Neue", Arial, sans-serif;
  background: var(--bg);
  color: var(--text);
  line-height: 1.5;
  min-height: 100vh;
  display: flex;
  flex-direction: column;
}

.container { width: 100%; max-width: 1200px; margin: 0 auto; padding: 0 20px; }

/* ---------- Header ---------- */
.site-header {
  background: linear-gradient(135deg, var(--keys-blue-darker) 0%, var(--keys-blue) 55%, var(--keys-blue-soft) 100%);
  color: #fff;
  border-bottom: 4px solid var(--keys-yellow);
  padding: 14px 0;
  position: sticky;
  top: 0;
  z-index: 20;
}

.header-inner { display: flex; align-items: center; justify-content: space-between; flex-wrap: wrap; gap: 12px; }

.brand { display: flex; align-items: center; gap: 14px; }

.brand-logo { height: 52px; width: auto; background: #fff; border-radius: 10px; padding: 4px; box-shadow: var(--shadow); }

.brand-text h1 {
  margin: 0;
  font-size: 1.35rem;
  font-weight: 800;
  letter-spacing: 0.5px;
  color: #fff;
}

.brand-text span { font-size: 0.8rem; color: var(--keys-yellow); font-weight: 600; letter-spacing: 1px; text-transform: uppercase; }

.header-nav { display: flex; gap: 10px; }

.stat-chip {
  background: rgba(255, 255, 255, 0.12);
  border: 1px solid rgba(255, 255, 255, 0.22);
  border-radius: 10px;
  padding: 6px 14px;
  display: flex;
  flex-direction: column;
  align-items: center;
  min-width: 96px;
}

.stat-chip .stat-label { font-size: 0.68rem; text-transform: uppercase; letter-spacing: 0.8px; color: rgba(255,255,255,0.85); }

.stat-chip .stat-value { font-size: 1.05rem; font-weight: 700; color: var(--keys-yellow); }

/* ---------- Cards ---------- */
main { flex: 1; padding: 28px 0 40px; display: flex; flex-direction: column; gap: 24px; }

.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 24px;
}

.card-title-row { margin-bottom: 18px; }
.card-title-row.between { display: flex; align-items: flex-start; justify-content: space-between; flex-wrap: wrap; gap: 12px; margin-bottom: 14px; }

.card h2 { margin: 0 0 4px; font-size: 1.25rem; color: var(--keys-blue-dark); }
.card h3 { margin: 0 0 4px; font-size: 1.05rem; color: var(--keys-blue-dark); }
.card-subtitle { margin: 0; font-size: 0.88rem; color: var(--text-soft); }

/* ---------- Drop zone ---------- */
.drop-zone {
  border: 2px dashed var(--keys-blue-soft);
  border-radius: var(--radius);
  background: linear-gradient(180deg, #F7FAFF 0%, #EEF3FB 100%);
  padding: 30px 20px 24px;
  text-align: center;
  cursor: pointer;
  transition: all 0.18s ease;
  outline: none;
}

.drop-zone:hover, .drop-zone:focus-visible, .drop-zone.drag {
  border-color: var(--keys-blue);
  background: linear-gradient(180deg, #FFFDF2 0%, #FDF6D7 100%);
  box-shadow: inset 0 0 0 3px rgba(253, 213, 4, 0.35);
}

.drop-icon { color: var(--keys-blue); opacity: 0.85; }

.drop-main { margin: 12px 0 4px; font-size: 1rem; font-weight: 600; color: var(--text); }
.drop-main .link { color: var(--keys-blue); text-decoration: underline; text-underline-offset: 3px; }
.drop-hint { margin: 0; font-size: 0.82rem; color: var(--text-soft); }

.drop-actions { margin-top: 18px; display: flex; justify-content: center; gap: 10px; flex-wrap: wrap; }

/* ---------- Buttons ---------- */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  border: none;
  border-radius: 9px;
  padding: 10px 18px;
  font-size: 0.92rem;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.16s ease;
  font-family: inherit;
}

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

.btn-primary { background: var(--keys-blue); color: #fff; }
.btn-primary:hover:not(:disabled) { background: var(--keys-blue-dark); transform: translateY(-1px); box-shadow: 0 5px 14px rgba(16, 59, 112, 0.35); }

.btn-outline { background: #fff; color: var(--keys-blue); border: 1.5px solid var(--keys-blue); }
.btn-outline:hover:not(:disabled) { background: var(--keys-blue); color: #fff; }

.btn-ghost { background: transparent; color: var(--text-soft); border: 1px solid var(--border); }
.btn-ghost:hover { background: #F0F3F9; color: var(--text); }

.btn-danger { background: var(--danger); color: #fff; }
.btn-danger:hover { background: #A93226; }

.btn-danger-ghost { background: transparent; color: var(--danger); border: 1.5px solid var(--danger); }
.btn-danger-ghost:hover { background: var(--danger-bg); }

.btn-lg { padding: 12px 26px; font-size: 1rem; }
.btn-warning { background: var(--keys-yellow); color: var(--keys-blue-darker); border: 1px solid var(--keys-yellow-dark); }
.btn-warning:hover { background: var(--keys-yellow-dark); }

.btn-icon {
  background: transparent;
  border: none;
  font-size: 1.6rem;
  line-height: 1;
  cursor: pointer;
  color: var(--text-soft);
  padding: 2px 8px;
  border-radius: 6px;
}
.btn-icon:hover { color: var(--text); background: #F0F3F9; }

/* ---------- Progress ---------- */
.ocr-progress { margin-top: 18px; }

.progress-top { display: flex; justify-content: space-between; font-size: 0.85rem; color: var(--text-soft); margin-bottom: 6px; }

.progress-track { height: 8px; background: #E7ECF5; border-radius: 99px; overflow: hidden; }

.progress-bar {
  height: 100%;
  width: 0%;
  border-radius: 99px;
  background: linear-gradient(90deg, var(--keys-blue), var(--keys-blue-soft));
  transition: width 0.2s ease;
}

/* ---------- Alerts ---------- */
.alert { margin-top: 16px; padding: 12px 16px; border-radius: 9px; font-size: 0.9rem; }
.alert-error { background: var(--danger-bg); color: var(--danger); border: 1px solid #F1C4BE; }
.alert-success { background: #EAF6EF; color: var(--ok); border: 1px solid #BFE3CE; margin-top: 12px; }

/* ---------- Result grid ---------- */
.result-grid { display: grid; grid-template-columns: 300px 1fr; gap: 24px; }

.preview-frame {
  border: 1px solid var(--border);
  border-radius: 10px;
  overflow: hidden;
  background: #fff;
  min-height: 180px;
  display: flex;
  align-items: center;
  justify-content: center;
}

.preview-frame img { width: 100%; height: auto; display: block; max-height: 420px; object-fit: contain; }

/* ---------- Forms ---------- */
.result-form .card-subtitle { margin-bottom: 14px; }

.form-row { display: grid; grid-template-columns: repeat(3, 1fr); gap: 12px; }
.form-row .field:nth-child(1) { grid-column: span 2; }
.form-row .field:nth-child(2):last-child { grid-column: span 3; }

.field { margin-bottom: 12px; }

.field label { display: block; font-size: 0.8rem; font-weight: 600; color: var(--text-soft); margin-bottom: 5px; }

.field input, .field textarea, .search-input {
  width: 100%;
  padding: 10px 12px;
  border: 1.5px solid var(--border);
  border-radius: 9px;
  font-size: 0.94rem;
  font-family: inherit;
  color: var(--text);
  background: #fff;
  transition: border-color 0.15s ease, box-shadow 0.15s ease;
}

.field input:focus, .field textarea:focus, .search-input:focus {
  outline: none;
  border-color: var(--keys-blue);
  box-shadow: 0 0 0 3px rgba(16, 59, 112, 0.12);
}

.field textarea { resize: vertical; }

.form-actions { display: flex; gap: 10px; margin-top: 4px; flex-wrap: wrap; }

/* ---------- Table ---------- */
.table-tools { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }

.search-input { width: 220px; }

.table-wrap { overflow-x: auto; border: 1px solid var(--border); border-radius: 10px; }

.invoice-table { width: 100%; border-collapse: collapse; min-width: 860px; }

.invoice-table thead th {
  background: var(--keys-blue);
  color: #fff;
  font-size: 0.78rem;
  text-transform: uppercase;
  letter-spacing: 0.6px;
  text-align: left;
  padding: 12px 14px;
  white-space: nowrap;
}

.invoice-table th.num, .invoice-table td.num { text-align: right; }

.invoice-table tbody td {
  padding: 11px 14px;
  border-bottom: 1px solid var(--border);
  font-size: 0.9rem;
  vertical-align: middle;
}

.invoice-table tbody tr:nth-child(even) { background: #F7FAFD; }
.invoice-table tbody tr:hover { background: #FFFDF0; }
.invoice-table tbody tr:last-child td { border-bottom: none; }

.invoice-table .row-empty td { text-align: center; color: var(--text-soft); padding: 30px 14px; font-size: 0.92rem; }

.fecha-cell { white-space: nowrap; }
.monto-cell { white-space: nowrap; font-variant-numeric: tabular-nums; font-weight: 600; }
.desc-cell { max-width: 260px; }
.desc-cell p { margin: 0; overflow: hidden; display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; }

.actions-col { width: 120px; text-align: center; }

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

.icon-btn {
  background: transparent;
  border: 1px solid var(--border);
  border-radius: 7px;
  cursor: pointer;
  padding: 5px 9px;
  font-size: 0.82rem;
  color: var(--text-soft);
  transition: all 0.15s ease;
}
.icon-btn:hover { border-color: var(--keys-blue); color: var(--keys-blue); background: #EEF3FB; }
.icon-btn.danger:hover { border-color: var(--danger); color: var(--danger); background: var(--danger-bg); }

.table-footer { margin-top: 14px; font-size: 0.88rem; color: var(--text-soft); }

/* ---------- Modal ---------- */
.modal {
  position: fixed;
  inset: 0;
  background: rgba(7, 29, 56, 0.65);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px;
  z-index: 100;
}

.modal[hidden] { display: none; }

.modal-box {
  background: #fff;
  border-radius: var(--radius);
  box-shadow: var(--shadow-lg);
  width: 100%;
  max-width: 640px;
  overflow: hidden;
}

.modal-box.small { max-width: 420px; }

.modal-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 16px 20px;
  border-bottom: 1px solid var(--border);
  background: var(--bg);
}
.modal-header h3 { margin: 0; color: var(--keys-blue-dark); }

.modal-body { padding: 18px 20px; }

.modal-body video { width: 100%; border-radius: 10px; background: #111; display: block; max-height: 420px; object-fit: contain; }

.modal-body .camera-canvas { width: 100%; border-radius: 10px; background: #111; display: block; max-height: 420px; }

.modal-footer { display: flex; justify-content: flex-end; gap: 10px; padding: 14px 20px; border-top: 1px solid var(--border); }

/* ---------- Footer ---------- */
.site-footer {
  background: var(--keys-blue-darker);
  color: rgba(255,255,255,0.8);
  font-size: 0.82rem;
  text-align: center;
  padding: 16px 0;
  border-top: 3px solid var(--keys-yellow);
}

/* ---------- Responsive ---------- */
@media (max-width: 860px) {
  .result-grid { grid-template-columns: 1fr; }
  .form-row { grid-template-columns: 1fr 1fr; }
  .form-row .field:nth-child(1) { grid-column: span 2; }
  .form-row .field:nth-child(2):last-child { grid-column: span 2; }
  .search-input { width: 100%; }
  .header-inner { justify-content: center; }
}

@media (max-width: 560px) {
  .form-row { grid-template-columns: 1fr; }
  .form-row .field:nth-child(1) { grid-column: span 1; }
  .form-row .field:nth-child(2):last-child { grid-column: span 1; }
  .brand-text h1 { font-size: 1.1rem; }
}