*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

:root {
  --primary: #1e40af;
  --primary-dark: #1e3a8a;
  --primary-light: #3b82f6;
  --success: #16a34a;
  --success-light: #dcfce7;
  --danger: #dc2626;
  --danger-light: #fee2e2;
  --warning: #d97706;
  --warning-light: #fef3c7;
  --info-light: #eff6ff;
  --gray-50: #f9fafb;
  --gray-100: #f3f4f6;
  --gray-200: #e5e7eb;
  --gray-300: #d1d5db;
  --gray-400: #9ca3af;
  --gray-500: #6b7280;
  --gray-600: #4b5563;
  --gray-700: #374151;
  --gray-800: #1f2937;
  --gray-900: #111827;
  --radius: 10px;
  --shadow: 0 4px 6px -1px rgba(0,0,0,.1), 0 2px 4px -2px rgba(0,0,0,.1);
  --shadow-lg: 0 10px 15px -3px rgba(0,0,0,.1), 0 4px 6px -4px rgba(0,0,0,.1);
}

body {
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
  background: var(--gray-100);
  color: var(--gray-800);
  min-height: 100vh;
}

/* ── NAV ── */
nav {
  background: var(--primary);
  color: #fff;
  padding: 0 1.5rem;
  height: 56px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  box-shadow: var(--shadow);
  position: sticky; top: 0; z-index: 100;
}
nav .brand { font-weight: 700; font-size: 1.05rem; letter-spacing: -.3px; white-space: nowrap; }
nav .brand span { opacity: .6; font-weight: 400; font-size: .78rem; margin-left: .4rem; }

#nav-right { display: none; align-items: center; gap: .5rem; }
.nav-tab {
  background: rgba(255,255,255,.1);
  border: 1.5px solid rgba(255,255,255,.2);
  color: #fff; padding: .3rem .85rem;
  border-radius: 6px; cursor: pointer;
  font-size: .8rem; font-weight: 600;
  transition: background .2s;
}
.nav-tab:hover       { background: rgba(255,255,255,.22); }
.nav-tab.active-tab  { background: rgba(255,255,255,.25); border-color: rgba(255,255,255,.5); }
#nav-greeting { font-size: .82rem; opacity: .85; margin-right: .25rem; }
.nav-sep { width: 1px; height: 20px; background: rgba(255,255,255,.25); margin: 0 .25rem; }
#btn-logout {
  background: transparent; border: 1.5px solid rgba(255,255,255,.3);
  color: #fff; padding: .3rem .75rem; border-radius: 6px;
  cursor: pointer; font-size: .78rem; transition: background .2s;
}
#btn-logout:hover { background: rgba(255,255,255,.15); }

/* ── AUTH PAGES ── */
.auth-page {
  min-height: calc(100vh - 56px);
  display: none; align-items: center;
  justify-content: center; padding: 2rem 1rem;
}
.auth-page.active { display: flex; }

.auth-card {
  background: #fff; border-radius: var(--radius);
  box-shadow: var(--shadow-lg);
  padding: 2.25rem 2.5rem; width: 100%; max-width: 430px;
}
.auth-logo { text-align: center; margin-bottom: 1.75rem; }
.auth-logo h2 { font-size: 1.4rem; font-weight: 700; color: var(--primary); }
.auth-logo p  { color: var(--gray-500); font-size: .85rem; margin-top: .3rem; }
.auth-switch  {
  text-align: center; margin-top: 1.25rem;
  font-size: .82rem; color: var(--gray-500);
}
.auth-switch a { color: var(--primary); text-decoration: none; font-weight: 600; cursor: pointer; }
.auth-switch a:hover { text-decoration: underline; }

/* ── CONTENT PAGES ── */
.content-page { display: none; }
.content-page.active { display: block; }

.page-wrap {
  max-width: 960px;
  margin: 2rem auto;
  padding: 0 1rem;
}

/* ── CARDS ── */
.card {
  background: #fff; border-radius: var(--radius);
  box-shadow: var(--shadow); padding: 1.5rem;
  margin-bottom: 1.25rem;
}
.card-title {
  font-size: 1rem; font-weight: 600; color: var(--gray-800);
  margin-bottom: 1.25rem; display: flex; align-items: center; gap: .5rem;
}

/* ── FORMS ── */
.form-group { margin-bottom: 1rem; }
label { display: block; font-size: .8rem; font-weight: 600; color: var(--gray-700); margin-bottom: .35rem; }
input[type="text"],
input[type="email"],
input[type="password"],
input[type="tel"],
input[type="date"] {
  width: 100%; padding: .6rem .85rem;
  border: 1.5px solid var(--gray-200);
  border-radius: 7px; font-size: .9rem; color: var(--gray-800);
  transition: border-color .15s, box-shadow .15s; outline: none;
}
input:focus {
  border-color: var(--primary-light);
  box-shadow: 0 0 0 3px rgba(59,130,246,.15);
}

/* ── BUTTONS ── */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: .45rem;
  padding: .6rem 1.25rem; border-radius: 7px; border: none;
  font-size: .875rem; font-weight: 600; cursor: pointer;
  transition: background .15s, transform .1s;
}
.btn:active { transform: scale(.97); }
.btn:disabled { opacity: .5; cursor: not-allowed; }
.btn-primary { background: var(--primary); color: #fff; }
.btn-primary:hover:not(:disabled) { background: var(--primary-dark); }
.btn-secondary { background: var(--gray-100); color: var(--gray-700); border: 1.5px solid var(--gray-200); }
.btn-secondary:hover:not(:disabled) { background: var(--gray-200); }
.btn-success { background: var(--success); color: #fff; }
.btn-success:hover:not(:disabled) { background: #15803d; }
.btn-danger-outline { background: transparent; color: var(--danger); border: 1.5px solid var(--danger); }
.btn-danger-outline:hover:not(:disabled) { background: var(--danger-light); }
.btn-full { width: 100%; }
.btn-sm { padding: .35rem .75rem; font-size: .78rem; }

/* ── ALERTS ── */
.alert {
  padding: .75rem 1rem; border-radius: 7px; font-size: .85rem;
  margin-bottom: 1rem; display: flex; align-items: flex-start; gap: .6rem;
}
.alert-error   { background: var(--danger-light);  color: #991b1b; border: 1px solid #fecaca; }
.alert-success { background: var(--success-light); color: #166534; border: 1px solid #bbf7d0; }
.alert-info    { background: var(--info-light);    color: var(--primary); border: 1px solid #bfdbfe; }
.alert-warning { background: var(--warning-light); color: #92400e; border: 1px solid #fde68a; }

/* ── CONTRATO ROWS ── */
.contratos-list { display: flex; flex-direction: column; gap: .75rem; }
.contrato-row {
  display: grid; grid-template-columns: 1fr 1fr 1fr auto;
  gap: .6rem; align-items: end;
  background: var(--gray-50); border: 1.5px solid var(--gray-200);
  border-radius: 8px; padding: .85rem;
}
.contrato-row input { background: #fff; }
.contrato-row .row-num {
  font-size: .7rem; font-weight: 700; color: var(--primary);
  grid-column: 1 / -1; margin-bottom: -.2rem;
}

/* ── STATUS ── */
#status-section { display: none; }
.status-card {
  border: 2px solid var(--gray-200); border-radius: var(--radius);
  padding: 1.5rem; text-align: center;
}
.status-card.pending { border-color: #fbbf24; background: var(--warning-light); }
.status-card.success { border-color: #4ade80; background: var(--success-light); }
.status-card.failure { border-color: #f87171; background: var(--danger-light); }

.status-badge {
  display: inline-flex; align-items: center; gap: .4rem;
  padding: .3rem .9rem; border-radius: 999px;
  font-size: .78rem; font-weight: 700;
  text-transform: uppercase; letter-spacing: .05em; margin-bottom: .75rem;
}
.badge-pending { background: #fef3c7; color: #92400e; }
.badge-started { background: #dbeafe; color: #1e40af; }
.badge-success { background: #dcfce7; color: #166534; }
.badge-failure { background: #fee2e2; color: #991b1b; }

.spinner {
  display: inline-block; width: 20px; height: 20px;
  border: 2.5px solid var(--gray-300);
  border-top-color: var(--primary-light);
  border-radius: 50%; animation: spin .7s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

.progress-info { font-size: .8rem; color: var(--gray-500); margin-top: .5rem; }
.progress-bar-wrap { background: var(--gray-200); border-radius: 999px; height: 6px; margin: .75rem 0; overflow: hidden; }
.progress-bar { height: 100%; background: var(--primary-light); border-radius: 999px; width: 0%; transition: width .5s; }

/* ── RESULTS ── */
#results-section { display: none; }
.result-contract { border: 1.5px solid var(--gray-200); border-radius: 8px; margin-bottom: 1rem; overflow: hidden; }
.result-contract-header {
  background: var(--gray-50); padding: .75rem 1rem;
  display: flex; align-items: center; justify-content: space-between;
  border-bottom: 1.5px solid var(--gray-200);
}
.result-contract-header .contract-id { font-weight: 700; font-size: .95rem; }
.inscrito-badge { font-size: .7rem; font-weight: 700; padding: .2rem .6rem; border-radius: 999px; }
.inscrito-si { background: #dcfce7; color: #166534; }
.inscrito-no { background: #fee2e2; color: #991b1b; }

.facturas-grid {
  display: grid; grid-template-columns: repeat(auto-fill, minmax(190px, 1fr));
  gap: .75rem; padding: 1rem;
}
.factura-item {
  background: var(--gray-50); border: 1.5px solid var(--gray-200);
  border-radius: 8px; padding: .85rem; text-align: center;
}
.factura-item .factura-icon { font-size: 2rem; margin-bottom: .5rem; }
.factura-item .factura-label { font-size: .75rem; color: var(--gray-500); margin-bottom: .6rem; }
.factura-item .btn-group { display: flex; flex-direction: column; gap: .4rem; }

/* ── HISTORY TABLE ── */
.filter-row {
  display: flex; gap: .75rem; align-items: flex-end; flex-wrap: wrap;
  margin-bottom: 1.25rem;
}
.filter-row .form-group { margin: 0; flex: 1; min-width: 140px; }

.hist-table-wrap { overflow-x: auto; }
table {
  width: 100%; border-collapse: collapse;
  font-size: .83rem;
}
thead tr { background: var(--gray-50); }
th {
  text-align: left; padding: .65rem .85rem;
  font-size: .75rem; font-weight: 700;
  color: var(--gray-500); text-transform: uppercase;
  letter-spacing: .05em;
  border-bottom: 2px solid var(--gray-200);
}
td {
  padding: .65rem .85rem;
  border-bottom: 1px solid var(--gray-100);
  vertical-align: middle;
}
tr:last-child td { border-bottom: none; }
tr:hover td { background: var(--gray-50); }

.pill {
  display: inline-block; padding: .2rem .6rem;
  border-radius: 999px; font-size: .72rem; font-weight: 700;
}
.pill-pending  { background: #fef3c7; color: #92400e; }
.pill-started  { background: #dbeafe; color: #1e40af; }
.pill-success  { background: #dcfce7; color: #166534; }
.pill-failure  { background: #fee2e2; color: #991b1b; }

.hist-contracts { font-size: .75rem; color: var(--gray-600); }
.hist-contracts code {
  background: var(--gray-100); padding: .1rem .35rem;
  border-radius: 4px; font-family: monospace;
}

.empty-state {
  text-align: center; padding: 3rem 1rem;
  color: var(--gray-400); font-size: .9rem;
}
.empty-state .empty-icon { font-size: 2.5rem; margin-bottom: .75rem; }

/* ── STATS ROW ── */
.stats-row { display: grid; grid-template-columns: repeat(auto-fit, minmax(150px, 1fr)); gap: .75rem; margin-bottom: 1.25rem; }
.stat-box {
  background: var(--gray-50); border: 1.5px solid var(--gray-200);
  border-radius: 8px; padding: 1rem; text-align: center;
}
.stat-box .stat-val { font-size: 1.6rem; font-weight: 700; color: var(--primary); }
.stat-box .stat-lbl { font-size: .75rem; color: var(--gray-500); margin-top: .2rem; }

/* ── ADMIN BADGE ── */
.admin-badge {
  background: #fef3c7; color: #92400e;
  font-size: .65rem; font-weight: 700; padding: .15rem .45rem;
  border-radius: 4px; text-transform: uppercase; letter-spacing: .05em;
  margin-left: .4rem;
}

/* ── LOADING OVERLAY ── */
#loading-overlay {
  display: none; position: fixed; inset: 0;
  background: rgba(0,0,0,.35); z-index: 999;
  align-items: center; justify-content: center;
}
#loading-overlay.show { display: flex; }
.loading-box {
  background: #fff; border-radius: var(--radius);
  padding: 2rem 2.5rem; text-align: center; box-shadow: var(--shadow-lg);
}
.loading-box p { margin-top: .75rem; color: var(--gray-600); font-size: .9rem; }

/* ── PWD STRENGTH ── */
.pwd-strength { margin-top: .35rem; height: 4px; border-radius: 999px; background: var(--gray-200); overflow: hidden; }
.pwd-strength-bar { height: 100%; width: 0%; border-radius: 999px; transition: width .3s, background .3s; }

.input-pwd-wrap { position: relative; display: flex; align-items: center; }
.input-pwd-wrap input { padding-right: 2.5rem; width: 100%; }
.pwd-toggle {
  position: absolute; right: .65rem;
  background: none; border: none; padding: .2rem; cursor: pointer;
  color: var(--gray-400); display: flex; align-items: center;
  transition: color .15s;
}
.pwd-toggle:hover { color: var(--primary); }
.pwd-toggle.active { color: var(--primary); }
.eye-icon { width: 18px; height: 18px; }

.footer-note { text-align: center; font-size: .75rem; color: var(--gray-400); margin-top: 2rem; padding-bottom: 2rem; }

/* ── ROW CLICKABLE ── */
.hist-row-clickable {
  cursor: pointer;
  transition: background .15s;
}
.hist-row-clickable:hover td { background: #eff6ff !important; }
.hist-row-clickable td:first-child {
  border-left: 3px solid transparent;
  transition: border-color .15s;
}
.hist-row-clickable:hover td:first-child { border-left-color: var(--primary-light); }

/* ── DRAWER ── */
#detail-backdrop {
  display: none; position: fixed; inset: 0;
  background: rgba(0,0,0,.3); z-index: 200;
  animation: fadeIn .2s ease;
}
#detail-backdrop.open { display: block; }

#detail-drawer {
  position: fixed; top: 0; right: -480px; width: 460px; max-width: 95vw;
  height: 100vh; background: #fff;
  box-shadow: -4px 0 24px rgba(0,0,0,.18);
  z-index: 201; display: flex; flex-direction: column;
  transition: right .28s cubic-bezier(.4,0,.2,1);
}
#detail-drawer.open { right: 0; }

.drawer-header {
  padding: 1.1rem 1.25rem;
  border-bottom: 1.5px solid var(--gray-100);
  display: flex; align-items: center; justify-content: space-between;
  background: var(--primary); color: #fff;
}
.drawer-header h3 { font-size: .95rem; font-weight: 700; }
.drawer-header .drawer-sub { font-size: .75rem; opacity: .75; margin-top: .15rem; }
.drawer-close {
  background: rgba(255,255,255,.15); border: none; color: #fff;
  width: 30px; height: 30px; border-radius: 6px; cursor: pointer;
  font-size: 1.1rem; display: flex; align-items: center; justify-content: center;
  transition: background .15s; flex-shrink: 0;
}
.drawer-close:hover { background: rgba(255,255,255,.3); }

.drawer-body {
  flex: 1; overflow-y: auto; padding: 1.25rem;
}

.drawer-meta {
  display: grid; grid-template-columns: 1fr 1fr;
  gap: .65rem; margin-bottom: 1.25rem;
}
.meta-item {
  background: var(--gray-50); border: 1px solid var(--gray-200);
  border-radius: 7px; padding: .6rem .75rem;
}
.meta-item .meta-lbl { font-size: .68rem; font-weight: 700; color: var(--gray-400); text-transform: uppercase; letter-spacing: .05em; }
.meta-item .meta-val { font-size: .85rem; font-weight: 600; color: var(--gray-800); margin-top: .2rem; word-break: break-all; }

.drawer-section-title {
  font-size: .78rem; font-weight: 700; color: var(--gray-500);
  text-transform: uppercase; letter-spacing: .06em;
  margin-bottom: .65rem; margin-top: .25rem;
  display: flex; align-items: center; gap: .4rem;
}

.drawer-trace {
  background: var(--gray-50); border: 1.5px solid var(--gray-200);
  border-radius: 8px; padding: .65rem .85rem; margin-bottom: .85rem;
  display: flex; flex-direction: column; gap: .4rem;
}
.trace-row { display: flex; gap: .75rem; align-items: baseline; font-size: .8rem; }
.trace-lbl { color: var(--gray-400); font-weight: 600; min-width: 70px; flex-shrink: 0; }
.trace-val { color: var(--gray-700); word-break: break-all; }
.trace-ua  { font-size: .72rem; color: var(--gray-500); }

.contrato-detail-card {
  border: 1.5px solid var(--gray-200); border-radius: 8px;
  margin-bottom: .65rem; overflow: hidden;
}
.contrato-detail-header {
  display: flex; align-items: center; justify-content: space-between;
  padding: .65rem .85rem;
  background: var(--gray-50);
  border-bottom: 1px solid var(--gray-100);
}
.contrato-detail-num {
  font-family: monospace; font-size: .9rem;
  font-weight: 700; color: var(--gray-800);
}
.contrato-detail-body {
  padding: .65rem .85rem;
  display: grid; grid-template-columns: 1fr 1fr;
  gap: .4rem .75rem; font-size: .8rem; color: var(--gray-600);
}
.contrato-detail-body span { font-weight: 600; color: var(--gray-800); }

.drawer-loading {
  display: flex; flex-direction: column;
  align-items: center; justify-content: center;
  padding: 3rem 1rem; gap: 1rem; color: var(--gray-400);
}
.drawer-error {
  background: var(--danger-light); color: #991b1b;
  border: 1px solid #fecaca; border-radius: 7px;
  padding: .75rem 1rem; font-size: .85rem; margin-top: .5rem;
}

@keyframes fadeIn { from { opacity: 0; } to { opacity: 1; } }

/* ── TASK QUERY PAGE ── */
.task-input-wrap {
  display: flex; gap: .65rem; align-items: flex-end; flex-wrap: wrap;
}
.task-input-wrap .form-group { flex: 1; margin: 0; min-width: 220px; }
.task-input-wrap input[type="text"] {
  font-family: monospace; font-size: .88rem; letter-spacing: .03em;
}

.task-status-panel {
  border: 2px solid var(--gray-200); border-radius: var(--radius);
  overflow: hidden; margin-top: 1.25rem;
}
.task-status-header {
  padding: 1rem 1.25rem;
  display: flex; align-items: center; justify-content: space-between;
  flex-wrap: wrap; gap: .5rem;
}
.task-status-header.hdr-pending { background: var(--warning-light); border-bottom: 2px solid #fbbf24; }
.task-status-header.hdr-running { background: #eff6ff; border-bottom: 2px solid #93c5fd; }
.task-status-header.hdr-success { background: var(--success-light); border-bottom: 2px solid #4ade80; }
.task-status-header.hdr-failure { background: var(--danger-light);  border-bottom: 2px solid #f87171; }

.task-status-header .ts-left h3 {
  font-size: .95rem; font-weight: 700; margin-bottom: .25rem;
}
.task-status-header .ts-left .ts-id {
  font-family: monospace; font-size: .72rem; color: var(--gray-500); word-break: break-all;
}
.task-status-header .ts-right {
  display: flex; align-items: center; gap: .65rem;
}

.poll-badge {
  display: inline-flex; align-items: center; gap: .4rem;
  font-size: .72rem; color: var(--gray-500); background: rgba(255,255,255,.6);
  padding: .25rem .6rem; border-radius: 999px; border: 1px solid var(--gray-200);
}

.task-body { padding: 1.25rem; }

.task-result-grid {
  display: grid; grid-template-columns: repeat(auto-fill, minmax(200px, 1fr));
  gap: .75rem; margin-bottom: 1rem;
}

.result-contract-mini {
  border: 1.5px solid var(--gray-200); border-radius: 8px; overflow: hidden;
}
.result-contract-mini .rcm-head {
  background: var(--gray-50); padding: .55rem .75rem;
  display: flex; align-items: center; justify-content: space-between;
  border-bottom: 1px solid var(--gray-100);
}
.result-contract-mini .rcm-num {
  font-family: monospace; font-size: .85rem; font-weight: 700;
}
.result-contract-mini .rcm-body {
  padding: .6rem .75rem; font-size: .8rem; color: var(--gray-600);
  display: flex; flex-direction: column; gap: .4rem;
}
.result-contract-mini .rcm-btns {
  padding: .5rem .75rem; display: flex; flex-direction: column; gap: .35rem;
  border-top: 1px solid var(--gray-100);
}

.countdown-ring {
  display: inline-flex; align-items: center; gap: .35rem;
  font-size: .72rem; color: var(--gray-400);
}
.countdown-num {
  font-weight: 700; color: var(--primary-light);
  min-width: 18px; display: inline-block; text-align: center;
}

@media (max-width: 640px) {
  .contrato-row { grid-template-columns: 1fr 1fr; }
  .contrato-row > div:last-child { grid-column: 1 / -1; }
  .auth-card { padding: 1.75rem 1.25rem; }
  nav .brand span { display: none; }
  .drawer-meta { grid-template-columns: 1fr; }
}
