/* ─── Design tokens ──────────────────────────────────────────────────────── */
:root {
  --primary:       #4f46e5;
  --primary-dark:  #4338ca;
  --primary-light: #e0e7ff;
  --success:       #10b981;
  --success-light: #d1fae5;
  --danger:        #ef4444;
  --danger-light:  #fee2e2;
  --warning:       #f59e0b;
  --warning-light: #fef3c7;
  --indigo:        #6366f1;
  --indigo-light:  #eef2ff;
  --text:          #111827;
  --text-muted:    #6b7280;
  --text-subtle:   #9ca3af;
  --border:        #e5e7eb;
  --border-focus:  #a5b4fc;
  --bg:            #f9fafb;
  --card:          #ffffff;
  --radius:        12px;
  --radius-sm:     8px;
  --radius-xs:     4px;
  --shadow:        0 1px 3px rgba(0,0,0,.08), 0 1px 2px rgba(0,0,0,.05);
  --shadow-md:     0 4px 6px -1px rgba(0,0,0,.08), 0 2px 4px -1px rgba(0,0,0,.04);
  --shadow-lg:     0 10px 15px -3px rgba(0,0,0,.08), 0 4px 6px -2px rgba(0,0,0,.04);
  --transition:    0.18s ease;
}

/* ─── Reset & base ───────────────────────────────────────────────────────── */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }
html { font-size: 16px; }
body {
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', system-ui, sans-serif;
  background: var(--bg);
  color: var(--text);
  line-height: 1.6;
  min-height: 100vh;
  display: flex;
  flex-direction: column;
}
a { color: var(--primary); text-decoration: none; }
a:hover { text-decoration: underline; }
code {
  font-family: 'SF Mono', 'Fira Code', 'Cascadia Code', monospace;
  font-size: .85em;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: var(--radius-xs);
  padding: .1em .35em;
}

/* ─── Layout ─────────────────────────────────────────────────────────────── */
.container { max-width: 1100px; margin: 0 auto; padding: 0 1.5rem; }
.page-main { flex: 1; padding: 2rem 0 4rem; }

/* ─── Header ─────────────────────────────────────────────────────────────── */
.app-header {
  background: var(--card);
  border-bottom: 1px solid var(--border);
  box-shadow: var(--shadow);
  position: sticky;
  top: 0;
  z-index: 100;
}
.header-inner {
  display: flex;
  align-items: center;
  justify-content: space-between;
  height: 60px;
}
.logo {
  display: flex;
  align-items: center;
  gap: .6rem;
  color: var(--text);
  font-size: 1.1rem;
  font-weight: 500;
  text-decoration: none;
}
.logo strong { color: var(--primary); }
.header-nav { display: flex; gap: .25rem; }
.nav-link {
  padding: .4rem .8rem;
  border-radius: var(--radius-sm);
  color: var(--text-muted);
  font-size: .9rem;
  font-weight: 500;
  transition: background var(--transition), color var(--transition);
}
.nav-link:hover, .nav-link.active {
  background: var(--primary-light);
  color: var(--primary);
  text-decoration: none;
}

/* ─── Footer ─────────────────────────────────────────────────────────────── */
.app-footer {
  border-top: 1px solid var(--border);
  padding: 1rem 0;
  font-size: .8rem;
  color: var(--text-subtle);
}
.app-footer .container {
  display: flex;
  justify-content: space-between;
  align-items: center;
  flex-wrap: wrap;
  gap: .5rem;
}

/* ─── Hero ───────────────────────────────────────────────────────────────── */
.page-hero {
  text-align: center;
  padding: 2.5rem 0 2rem;
}
.hero-title {
  font-size: 2rem;
  font-weight: 700;
  letter-spacing: -.02em;
  background: linear-gradient(135deg, var(--primary) 0%, #818cf8 100%);
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  background-clip: text;
  margin-bottom: .5rem;
}
.hero-sub { color: var(--text-muted); font-size: 1.05rem; }

/* ─── Card ───────────────────────────────────────────────────────────────── */
.card {
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  margin-bottom: 1.25rem;
  overflow: hidden;
}
.card-header {
  display: flex;
  align-items: center;
  gap: .6rem;
  padding: .9rem 1.25rem;
  font-weight: 600;
  font-size: .95rem;
  border-bottom: 1px solid var(--border);
  background: linear-gradient(to right, var(--indigo-light), var(--card));
  color: var(--text);
}
.card-header .icon { width: 18px; height: 18px; color: var(--primary); flex-shrink: 0; }
.card-body { padding: 1.25rem; }

/* ─── Tabs ───────────────────────────────────────────────────────────────── */
.tab-bar {
  display: flex;
  gap: .5rem;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: .3rem;
  width: fit-content;
  margin-bottom: 1.25rem;
}
.tab-btn {
  display: flex;
  align-items: center;
  gap: .4rem;
  padding: .45rem .9rem;
  border: none;
  border-radius: calc(var(--radius-sm) - 2px);
  background: transparent;
  color: var(--text-muted);
  font-size: .9rem;
  font-weight: 500;
  cursor: pointer;
  transition: background var(--transition), color var(--transition), box-shadow var(--transition);
}
.tab-btn:hover { background: var(--card); color: var(--text); }
.tab-btn.active {
  background: var(--card);
  color: var(--primary);
  box-shadow: var(--shadow);
}
.tab-badge {
  font-size: .68rem;
  font-weight: 600;
  background: var(--success-light);
  color: var(--success);
  border-radius: 100px;
  padding: .1em .5em;
}
.tab-panel { animation: fadeIn .18s ease; }
.tab-panel.hidden { display: none; }

/* ─── Upload zones ───────────────────────────────────────────────────────── */
.upload-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 1rem;
}
@media (max-width: 600px) { .upload-grid { grid-template-columns: 1fr; } }

.upload-zone {
  border: 2px dashed var(--border);
  border-radius: var(--radius);
  transition: border-color var(--transition), background var(--transition);
}
.upload-zone:has(.file-input:focus) { border-color: var(--primary); }
.upload-zone.has-file { border-color: var(--success); background: var(--success-light); }
.upload-zone.dragover { border-color: var(--primary); background: var(--primary-light); }

.file-input {
  position: absolute;
  opacity: 0;
  width: 0;
  height: 0;
}
.upload-label {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: .6rem;
  padding: 1.75rem 1rem;
  cursor: pointer;
  color: var(--text-muted);
  text-align: center;
}
.upload-label svg { color: var(--text-subtle); transition: color var(--transition); }
.upload-zone.has-file .upload-label svg { color: var(--success); }
.upload-title { font-weight: 600; color: var(--text); font-size: .95rem; }
.upload-hint { font-size: .8rem; }

/* ─── Forms ──────────────────────────────────────────────────────────────── */
.form-row {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(180px, 1fr));
  gap: 1rem;
}
.form-group { display: flex; flex-direction: column; gap: .35rem; }
.form-group label { font-size: .85rem; font-weight: 600; color: var(--text); }

input[type="text"],
input[type="password"],
input[type="date"],
input[type="search"],
select {
  height: 40px;
  padding: 0 .75rem;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  font-size: .9rem;
  color: var(--text);
  background: var(--card);
  outline: none;
  transition: border-color var(--transition), box-shadow var(--transition);
  width: 100%;
}
input:focus, select:focus {
  border-color: var(--border-focus);
  box-shadow: 0 0 0 3px rgba(99,102,241,.12);
}
.select-wrapper { position: relative; }
.select-wrapper::after {
  content: '';
  position: absolute;
  right: .75rem;
  top: 50%;
  transform: translateY(-50%);
  border: 5px solid transparent;
  border-top-color: var(--text-muted);
  pointer-events: none;
}
select { -webkit-appearance: none; appearance: none; padding-right: 2rem; }

.input-with-toggle { position: relative; }
.input-with-toggle input { padding-right: 2.5rem; }
.toggle-pw {
  position: absolute;
  right: .5rem;
  top: 50%;
  transform: translateY(-50%);
  border: none;
  background: none;
  cursor: pointer;
  color: var(--text-subtle);
  padding: .2rem;
  display: flex;
  transition: color var(--transition);
}
.toggle-pw:hover { color: var(--text-muted); }
.icon-eye { width: 18px; height: 18px; }

/* ─── Buttons ────────────────────────────────────────────────────────────── */
.btn {
  display: inline-flex;
  align-items: center;
  gap: .45rem;
  padding: .55rem 1.1rem;
  border: 1px solid transparent;
  border-radius: var(--radius-sm);
  font-size: .9rem;
  font-weight: 600;
  cursor: pointer;
  transition: background var(--transition), box-shadow var(--transition), opacity var(--transition);
  text-decoration: none;
  white-space: nowrap;
}
.btn:hover { text-decoration: none; }
.btn-primary {
  background: var(--primary);
  color: #fff;
  box-shadow: 0 1px 2px rgba(79,70,229,.3);
}
.btn-primary:hover { background: var(--primary-dark); box-shadow: 0 4px 8px rgba(79,70,229,.3); }
.btn-secondary {
  background: var(--card);
  color: var(--primary);
  border-color: var(--primary-light);
}
.btn-secondary:hover { background: var(--primary-light); }
.btn-ghost {
  background: transparent;
  color: var(--text-muted);
  border-color: var(--border);
}
.btn-ghost:hover { background: var(--bg); color: var(--text); }
.btn-lg { padding: .75rem 1.75rem; font-size: 1rem; border-radius: var(--radius); }
.btn-sm { padding: .35rem .75rem; font-size: .82rem; }
.btn:disabled { opacity: .55; cursor: not-allowed; }

.btn-spinner {
  width: 16px; height: 16px;
  border: 2px solid rgba(255,255,255,.4);
  border-top-color: #fff;
  border-radius: 50%;
  animation: spin .7s linear infinite;
}
.form-actions { display: flex; justify-content: flex-end; margin-top: .5rem; }

/* ─── CIEC notice ────────────────────────────────────────────────────────── */
.ciec-notice {
  display: flex;
  align-items: flex-start;
  gap: .5rem;
  background: var(--warning-light);
  border: 1px solid var(--warning);
  border-radius: var(--radius-sm);
  padding: .75rem 1rem;
  font-size: .85rem;
  color: #78350f;
}

/* ─── Summary bar ────────────────────────────────────────────────────────── */
.summary-bar {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  gap: 1.5rem;
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 1rem 1.25rem;
  margin-bottom: 1.25rem;
  box-shadow: var(--shadow);
}
.summary-stat { display: flex; align-items: baseline; gap: .35rem; }
.summary-num  { font-size: 1.1rem; font-weight: 700; color: var(--text); }
.summary-label { font-size: .8rem; color: var(--text-muted); }
.summary-bar .btn { margin-left: auto; }
.type-counters { display: flex; align-items: center; flex-wrap: wrap; gap: .4rem; }
.type-counters .badge strong { font-weight: 700; margin-left: .2rem; }

/* ─── Table toolbar ──────────────────────────────────────────────────────── */
.table-toolbar {
  display: flex;
  align-items: center;
  gap: .75rem;
  flex-wrap: wrap;
  margin-bottom: .75rem;
}
.search-box {
  display: flex;
  align-items: center;
  gap: .5rem;
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 0 .75rem;
  flex: 1;
  min-width: 200px;
}
.search-box .icon-sm { color: var(--text-subtle); flex-shrink: 0; }
.search-box input {
  border: none;
  box-shadow: none;
  padding: 0;
  height: 38px;
  width: 100%;
  font-size: .88rem;
}
.search-box input:focus { box-shadow: none; border: none; }
.toolbar-actions { display: flex; gap: .5rem; }

/* ─── Invoice table ──────────────────────────────────────────────────────── */
.table-wrap {
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  overflow-x: auto;
}
.invoice-table {
  width: 100%;
  border-collapse: collapse;
  font-size: .85rem;
}
.invoice-table thead th {
  padding: .7rem 1rem;
  text-align: left;
  font-weight: 600;
  font-size: .78rem;
  text-transform: uppercase;
  letter-spacing: .04em;
  color: var(--text-muted);
  background: var(--bg);
  border-bottom: 1px solid var(--border);
  white-space: nowrap;
  user-select: none;
}
.invoice-table thead th.sortable { cursor: pointer; }
.invoice-table thead th.sortable:hover { color: var(--primary); }
.invoice-table thead th.num,
.invoice-table tbody td.num { text-align: right; }
.invoice-table tbody tr {
  border-bottom: 1px solid var(--border);
  transition: background var(--transition);
}
.invoice-table tbody tr:last-child { border-bottom: none; }
.invoice-table tbody tr:hover { background: #fafafa; }
.invoice-table tbody tr.hidden-row { display: none; }
.invoice-table tbody td { padding: .65rem 1rem; color: var(--text); vertical-align: middle; }
.col-check { width: 36px; text-align: center; }

.uuid-cell {
  display: inline-flex;
  align-items: center;
  gap: .35rem;
  font-family: 'SF Mono', 'Fira Code', monospace;
  font-size: .78rem;
  color: var(--primary);
  cursor: pointer;
  padding: .2rem .4rem;
  border-radius: var(--radius-xs);
  transition: background var(--transition);
}
.uuid-cell:hover { background: var(--primary-light); }
.copy-icon { width: 12px; height: 12px; opacity: .5; }
.uuid-cell:hover .copy-icon { opacity: 1; }
.rfc { font-size: .8rem; }
.nombre { max-width: 180px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.fecha { white-space: nowrap; font-size: .82rem; color: var(--text-muted); }

/* ─── Badges ─────────────────────────────────────────────────────────────── */
.badge {
  display: inline-block;
  padding: .2em .65em;
  border-radius: 100px;
  font-size: .75rem;
  font-weight: 600;
  white-space: nowrap;
}
.badge-green  { background: var(--success-light);  color: #065f46; }
.badge-red    { background: var(--danger-light);   color: #991b1b; }
.badge-gray   { background: var(--bg);             color: var(--text-muted); border: 1px solid var(--border); }
.badge-indigo { background: var(--indigo-light);   color: var(--primary); }
.badge-amber  { background: #fef3c7; color: #92400e; }
.badge-purple { background: #ede9fe; color: #5b21b6; }
.badge-teal   { background: #ccfbf1; color: #0f766e; }

/* ─── Download buttons ───────────────────────────────────────────────────── */
.col-actions { white-space: nowrap; }
.btn-icon {
  display: inline-flex;
  align-items: center;
  gap: .3rem;
  padding: .3rem .6rem;
  border: 1px solid var(--border);
  border-radius: var(--radius-xs);
  background: var(--card);
  color: var(--primary);
  font-size: .75rem;
  font-weight: 600;
  cursor: pointer;
  transition: background var(--transition), border-color var(--transition);
}
.btn-icon:hover { background: var(--primary-light); border-color: var(--primary); }
.btn-icon-pdf { color: var(--danger); }
.btn-icon-pdf:hover { background: var(--danger-light); border-color: var(--danger); }
.btn-icon:disabled { opacity: .45; cursor: not-allowed; }

/* ─── Toast ──────────────────────────────────────────────────────────────── */
.toast {
  position: fixed;
  bottom: 1.5rem;
  left: 50%;
  transform: translateX(-50%);
  z-index: 999;
  transition: opacity .2s ease;
}
.toast.hidden { opacity: 0; pointer-events: none; }
.toast-inner {
  display: flex;
  align-items: center;
  gap: .75rem;
  background: #1f2937;
  color: #fff;
  padding: .75rem 1.25rem;
  border-radius: var(--radius);
  box-shadow: var(--shadow-lg);
  font-size: .9rem;
  white-space: nowrap;
}
.toast-spinner {
  width: 16px; height: 16px;
  border: 2px solid rgba(255,255,255,.3);
  border-top-color: #fff;
  border-radius: 50%;
  animation: spin .7s linear infinite;
}

/* ─── Empty / error pages ────────────────────────────────────────────────── */
.empty-state, .error-page {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 1rem;
  padding: 4rem 2rem;
  text-align: center;
  color: var(--text-muted);
}
.error-page { min-height: 50vh; }
.error-page .error-icon svg { color: var(--danger); }
.error-page h2 { color: var(--text); font-size: 1.4rem; }
.error-message {
  background: var(--danger-light);
  color: #991b1b;
  border-radius: var(--radius-sm);
  padding: .75rem 1.25rem;
  font-size: .9rem;
  max-width: 500px;
}

/* ─── Icons ──────────────────────────────────────────────────────────────── */
.icon    { width: 18px; height: 18px; flex-shrink: 0; }
.icon-sm { width: 15px; height: 15px; flex-shrink: 0; }

/* ─── Streaming ──────────────────────────────────────────────────────────── */
#streamSection { margin-top: 1.5rem; animation: fadeIn .25s ease; }

/* Live dot pulsing next to "Consultando…" badge */
.stream-dot {
  display: inline-block;
  width: 7px; height: 7px;
  background: var(--primary);
  border-radius: 50%;
  margin-right: .3rem;
  animation: pulse 1.2s ease-in-out infinite;
}
@keyframes pulse {
  0%, 100% { opacity: 1; transform: scale(1); }
  50%       { opacity: .4; transform: scale(.7); }
}

/* New-row highlight when it first appears */
@keyframes rowIn {
  from { background: var(--primary-light); }
  to   { background: transparent; }
}
#streamTableBody tr.row-new { animation: rowIn 1.2s ease-out forwards; }

/* Separator row between emitidas / recibidas in "Ambas" mode */
tr.type-separator-row td {
  padding: .35rem 1rem;
  background: var(--bg);
  border-top: 2px solid var(--border);
  border-bottom: 2px solid var(--border);
  text-align: center;
}
.type-separator-label {
  font-size: .72rem;
  font-weight: 600;
  letter-spacing: .08em;
  text-transform: uppercase;
  color: var(--text-muted);
}

/* Error banner below the table */
.stream-error {
  margin-top: .75rem;
  background: var(--danger-light);
  color: #991b1b;
  border: 1px solid var(--danger);
  border-radius: var(--radius-sm);
  padding: .75rem 1rem;
  font-size: .88rem;
}

/* ─── Captcha modal ──────────────────────────────────────────────────────── */
.modal-backdrop {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,.55);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 1000;
  padding: 1rem;
  animation: fadeIn .15s ease;
}
.modal-box {
  background: var(--card);
  border-radius: var(--radius);
  box-shadow: var(--shadow-lg);
  padding: 2rem;
  width: 100%;
  max-width: 420px;
  display: flex;
  flex-direction: column;
  gap: .75rem;
}
.modal-header {
  display: flex;
  align-items: center;
  gap: .5rem;
  color: var(--primary);
}
.modal-title {
  font-size: 1.1rem;
  font-weight: 700;
  color: var(--text);
  margin: 0;
}
.modal-sub {
  font-size: .88rem;
  color: var(--text-muted);
  line-height: 1.5;
}
.captcha-img-wrap {
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: .75rem;
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: 80px;
}
.captcha-img {
  max-width: 100%;
  height: auto;
  display: block;
  image-rendering: pixelated; /* keep SAT captcha crisp */
}
.modal-actions {
  display: flex;
  justify-content: flex-end;
  padding-top: .5rem;
}

/* ─── Conceptos modal ────────────────────────────────────────────────────── */
.modal-box-wide {
  max-width: 860px;
}
.conceptos-content {
  min-height: 80px;
  overflow: hidden;
}
.conceptos-table-wrap {
  max-height: 60vh;
  overflow-y: auto;
}
.conceptos-table {
  font-size: .82rem;
}
.conceptos-table th,
.conceptos-table td {
  padding: .45rem .6rem;
}
.conceptos-loading {
  display: flex;
  align-items: center;
  gap: .6rem;
  color: var(--text-muted);
  font-size: .9rem;
  padding: 1rem 0;
}
/* ─── Period pill selectors ──────────────────────────────────────────────── */
.period-grid {
  display: grid;
  grid-template-columns: 1fr 1fr auto;
  gap: 1.5rem;
  align-items: start;
}
@media (max-width: 700px) {
  .period-grid { grid-template-columns: 1fr; }
}
.pill-grid {
  display: flex;
  flex-wrap: wrap;
  gap: .4rem;
  margin-top: .35rem;
}
.pill {
  padding: .3rem .75rem;
  border-radius: 999px;
  border: 1.5px solid var(--border);
  background: var(--card);
  color: var(--text);
  font-size: .82rem;
  font-weight: 500;
  cursor: pointer;
  transition: background var(--transition), border-color var(--transition), color var(--transition);
  line-height: 1.4;
}
.pill:hover:not(.pill-disabled) {
  border-color: var(--primary);
  color: var(--primary);
}
.pill-active {
  background: var(--primary);
  border-color: var(--primary);
  color: #fff;
}
.pill-active:hover {
  background: var(--primary-dark);
  border-color: var(--primary-dark);
  color: #fff;
}
.pill-disabled {
  opacity: .35;
  cursor: not-allowed;
  pointer-events: none;
}
.period-hint {
  font-size: .78rem;
  color: var(--text-muted);
  font-weight: 400;
  margin-left: .3rem;
}
.period-summary {
  margin-top: .75rem;
  font-size: .85rem;
  color: var(--primary);
  font-weight: 600;
  min-height: 1.2em;
}

/* ─── Conceptos section title ────────────────────────────────────────────── */
.conceptos-section-title {
  font-size: .85rem;
  font-weight: 700;
  color: var(--text-muted);
  text-transform: uppercase;
  letter-spacing: .04em;
  margin: 1.25rem 0 .4rem;
}
.btn-icon-conceptos {
  color: var(--primary);
  border-color: var(--primary-light);
  background: var(--primary-light);
}
.btn-icon-conceptos:hover {
  background: #c7d2fe;
}

/* ─── Animations ─────────────────────────────────────────────────────────── */
@keyframes spin    { to { transform: rotate(360deg); } }
@keyframes fadeIn  { from { opacity: 0; transform: translateY(4px); } to { opacity: 1; transform: none; } }
.hidden { display: none !important; }

/* ─── Dark mode ──────────────────────────────────────────────────────────── */
[data-theme="dark"] {
  --primary:       #6366f1;
  --primary-dark:  #4f46e5;
  --primary-light: #1e1b4b;
  --success:       #34d399;
  --success-light: #064e3b;
  --danger:        #f87171;
  --danger-light:  #450a0a;
  --warning:       #fbbf24;
  --warning-light: #451a03;
  --indigo:        #818cf8;
  --indigo-light:  #1e1b4b;
  --text:          #f1f5f9;
  --text-muted:    #94a3b8;
  --text-subtle:   #475569;
  --border:        #334155;
  --border-focus:  #818cf8;
  --bg:            #0f172a;
  --card:          #1e293b;
  --shadow:        0 1px 3px rgba(0,0,0,.4),  0 1px 2px rgba(0,0,0,.3);
  --shadow-md:     0 4px 6px -1px rgba(0,0,0,.4), 0 2px 4px -1px rgba(0,0,0,.25);
  --shadow-lg:     0 10px 15px -3px rgba(0,0,0,.5), 0 4px 6px -2px rgba(0,0,0,.3);
}

/* Table hover */
[data-theme="dark"] .invoice-table tbody tr:hover { background: #243449; }

/* Badge text overrides */
[data-theme="dark"] .badge-green  { background: #064e3b; color: #6ee7b7; }
[data-theme="dark"] .badge-red    { background: #450a0a; color: #fca5a5; }
[data-theme="dark"] .badge-gray   { background: #1e293b; color: var(--text-muted); }
[data-theme="dark"] .badge-indigo { background: #1e1b4b; color: #a5b4fc; }
[data-theme="dark"] .badge-amber  { background: #451a03; color: #fcd34d; }
[data-theme="dark"] .badge-purple { background: #2e1065; color: #c4b5fd; }
[data-theme="dark"] .badge-teal   { background: #042f2e; color: #5eead4; }

/* CIEC notice */
[data-theme="dark"] .ciec-notice { background: #451a03; border-color: var(--warning); color: #fcd34d; }

/* Error/stream */
[data-theme="dark"] .stream-error  { background: #450a0a; color: #fca5a5; border-color: var(--danger); }
[data-theme="dark"] .error-message { background: #450a0a; color: #fca5a5; }
[data-theme="dark"] .error-page h2 { color: var(--text); }

/* Input focus ring */
[data-theme="dark"] input:focus,
[data-theme="dark"] select:focus {
  box-shadow: 0 0 0 3px rgba(99,102,241,.25);
}

/* Upload zone file highlight */
[data-theme="dark"] .upload-zone.has-file   { background: #064e3b; }
[data-theme="dark"] .upload-zone.dragover   { background: #1e1b4b; }

/* Row-in animation in dark mode */
[data-theme="dark"] #streamTableBody tr.row-new {
  animation: rowInDark 1.2s ease-out forwards;
}
@keyframes rowInDark {
  from { background: #1e1b4b; }
  to   { background: transparent; }
}

/* Theme toggle button */
.theme-toggle {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 34px;
  height: 34px;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  background: transparent;
  color: var(--text-muted);
  cursor: pointer;
  transition: background var(--transition), color var(--transition), border-color var(--transition);
  flex-shrink: 0;
}
.theme-toggle:hover { background: var(--bg); color: var(--text); }
/* In light mode show moon; in dark mode show sun */
.theme-toggle .icon-sun  { display: none;  }
.theme-toggle .icon-moon { display: block; }
[data-theme="dark"] .theme-toggle .icon-sun  { display: block; }
[data-theme="dark"] .theme-toggle .icon-moon { display: none;  }

/* ─── Analytics page ─────────────────────────────────────────────────────── */
.an-filter-card { margin-bottom: 1.5rem; }
.an-filters {
  display: flex;
  gap: 1rem;
  flex-wrap: wrap;
  align-items: flex-end;
}
.an-filter-rfc { flex: 1 1 160px; }
.an-filter-btn  { flex-shrink: 0; }

.an-empty {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 1rem;
  padding: 4rem 2rem;
  color: var(--text-subtle);
  text-align: center;
}
.an-empty p { font-size: 1rem; }
.an-empty-msg { color: var(--text-subtle); font-size: .875rem; padding: .5rem 0; }

.an-kpi-bar {
  display: flex;
  gap: 1rem;
  flex-wrap: wrap;
}
.an-kpi {
  flex: 1 1 140px;
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: .75rem 1rem;
  display: flex;
  flex-direction: column;
  gap: .25rem;
  box-shadow: var(--shadow);
}
.an-kpi-label { font-size: .75rem; color: var(--text-muted); text-transform: uppercase; letter-spacing: .04em; }
.an-kpi-value { font-size: 1.3rem; font-weight: 700; color: var(--text); line-height: 1.2; }

.an-tab-panel { padding-top: 1rem; }

.an-grid-2 {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 1rem;
}
@media (max-width: 768px) { .an-grid-2 { grid-template-columns: 1fr; } }

.an-card .card-header {
  display: flex;
  align-items: center;
  font-weight: 600;
  font-size: .9rem;
  padding: .75rem 1rem;
  border-bottom: 1px solid var(--border);
}
.an-card .card-body { padding: 1rem; }

.table-wrap { overflow-x: auto; }

.an-norm-form .form-row {
  display: flex;
  gap: .75rem;
  flex-wrap: wrap;
  margin-bottom: .5rem;
}
.an-norm-form .form-row .form-group { flex: 1 1 140px; }

.an-rules-list { display: flex; flex-direction: column; gap: .4rem; }
.an-rule-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: .5rem;
  padding: .4rem .6rem;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  font-size: .85rem;
}
.an-rule-info { display: flex; gap: .4rem; flex-wrap: wrap; align-items: center; }

.cohort-table th, .cohort-table td { font-size: .78rem; white-space: nowrap; }
.cohort-table small { color: var(--text-subtle); }

.an-dl {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(160px,1fr));
  gap: .75rem;
}
.an-dl > div {
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: .5rem .75rem;
}
.an-dl dt { font-size: .75rem; color: var(--text-muted); }
.an-dl dd { font-size: 1rem; font-weight: 600; margin: 0; }

.an-loading-text { color: var(--text-subtle); font-size: .875rem; }

.btn-spinner {
  display: inline-block;
  width: 14px;
  height: 14px;
  border: 2px solid currentColor;
  border-top-color: transparent;
  border-radius: 50%;
  animation: spin .7s linear infinite;
  vertical-align: middle;
}
.btn-spinner.hidden { display: none; }

/* Dark mode analytics */
[data-theme="dark"] .an-kpi { background: var(--card); border-color: var(--border); }
[data-theme="dark"] .an-rule-row { background: #0f172a; }
[data-theme="dark"] .an-dl > div { background: #0f172a; }
