:root {
  /* Light theme (default) */
  --bg-base: #f8fafc;
  --bg-surface: #ffffff;
  --bg-elevated: #f1f5f9;
  --bg-hover: #e2e8f0;
  --border: #cbd5e1;
  --border-subtle: #e2e8f0;
  --text: #0f172a;
  --text-muted: #475569;
  --text-dim: #64748b;
  --accent: #1e40af;
  --accent-hover: #1e3a8a;
  --accent-glow: rgba(30, 64, 175, 0.12);
  --success: #15803d;
  --warning: #b45309;
  --error: #b91c1c;
  --skipped: #64748b;
  --radius: 10px;
  --radius-sm: 6px;
  --sidebar-width: 260px;
  --font: "DM Sans", system-ui, sans-serif;
  --mono: "JetBrains Mono", ui-monospace, monospace;
}

html[data-theme="dark"] {
  --bg-base: #000000;
  --bg-surface: #0a0a0a;
  --bg-elevated: #111111;
  --bg-hover: #1a1a1a;
  --border: #222222;
  --border-subtle: #181818;
  --text: #f0f0f0;
  --text-muted: #a0a0a0;
  --text-dim: #707070;
  --accent: #a3e635;
  --accent-hover: #84cc16;
  --accent-glow: rgba(163, 230, 53, 0.15);
  --success: #a3e635;
  --warning: #f59e0b;
  --error: #ef4444;
  --skipped: #6b7280;
}

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

html, body {
  height: 100%;
  font-family: var(--font);
  background: var(--bg-base);
  color: var(--text);
  line-height: 1.5;
  -webkit-font-smoothing: antialiased;
}

body {
  background-image:
    radial-gradient(ellipse 80% 50% at 50% -20%, rgba(59, 130, 246, 0.06), transparent),
    radial-gradient(ellipse 60% 40% at 100% 100%, rgba(59, 130, 246, 0.03), transparent);
}

html[data-theme="dark"] body {
  background-image:
    radial-gradient(ellipse 80% 50% at 50% -20%, rgba(163, 230, 53, 0.08), transparent),
    radial-gradient(ellipse 60% 40% at 100% 100%, rgba(163, 230, 53, 0.04), transparent);
}

.app {
  display: flex;
  min-height: 100vh;
}

/* Sidebar */
.sidebar {
  width: var(--sidebar-width);
  background: var(--bg-surface);
  border-right: 1px solid var(--border-subtle);
  display: flex;
  flex-direction: column;
  position: fixed;
  top: 0;
  left: 0;
  bottom: 0;
  z-index: 100;
}

.brand {
  display: flex;
  align-items: center;
  padding: 12px 16px;
  border-bottom: 1px solid var(--border-subtle);
}

.brand-logo {
  height: 48px;
  width: auto;
  max-width: 200px;
  object-fit: contain;
  border-radius: 6px;
  flex-shrink: 0;
}

.nav {
  flex: 1;
  padding: 16px 12px;
  display: flex;
  flex-direction: column;
  gap: 4px;
}

.nav-item {
  display: flex;
  align-items: center;
  gap: 10px;
  width: 100%;
  padding: 10px 14px;
  border: none;
  background: transparent;
  color: var(--text-muted);
  font-family: inherit;
  font-size: 0.9rem;
  font-weight: 500;
  border-radius: var(--radius-sm);
  cursor: pointer;
  transition: background 0.15s, color 0.15s;
  text-align: left;
}

.nav-item svg {
  width: 18px;
  height: 18px;
  flex-shrink: 0;
}

.nav-item:hover {
  background: var(--bg-hover);
  color: var(--text);
}

.nav-item.active {
  background: var(--accent-glow);
  color: var(--accent);
}

.sidebar-footer {
  padding: 16px 20px 20px;
  border-top: 1px solid var(--border-subtle);
}

/* Theme switch in sidebar */
.theme-switch {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 4px 0 10px;
  border-bottom: 1px solid var(--border-subtle);
  margin-bottom: 10px;
}

.theme-label {
  font-size: 0.7rem;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: var(--text-dim);
}

.theme-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 32px;
  height: 32px;
  padding: 0;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  background: var(--bg-elevated);
  color: var(--text-muted);
  cursor: pointer;
  transition: all 0.15s ease;
}

.theme-btn:hover {
  background: var(--bg-hover);
  color: var(--text);
  border-color: var(--accent);
}

.theme-btn svg {
  display: block;
}

.theme-btn .hidden {
  display: none;
}

/* Main */
.main {
  flex: 1;
  margin-left: var(--sidebar-width);
  padding: 32px 40px;
  max-width: 1400px;
}

.view { display: none; }
.view.active { display: block; }

.view-header {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  margin-bottom: 28px;
  gap: 16px;
}

.view-header h2 {
  font-size: 1.75rem;
  font-weight: 700;
  letter-spacing: -0.03em;
}

.subtitle {
  color: var(--text-muted);
  font-size: 0.95rem;
  margin-top: 4px;
}

.source-badges {
  display: flex;
  gap: 8px;
  margin-top: 8px;
  flex-wrap: wrap;
}

.src-badge {
  display: inline-block;
  font-size: 0.65rem;
  padding: 2px 8px;
  border-radius: 999px;
  background: var(--bg-elevated);
  border: 1px solid var(--border);
  color: var(--text-dim);
  font-weight: 600;
  letter-spacing: 0.02em;
}

/* Panels */
.panel {
  background: var(--bg-surface);
  border: 1px solid var(--border-subtle);
  border-radius: var(--radius);
  overflow: hidden;
}

.search-layout {
  display: grid;
  grid-template-columns: 1fr 1.1fr;
  gap: 24px;
  align-items: start;
}

@media (max-width: 1100px) {
  .search-layout { grid-template-columns: 1fr; }
  .sidebar { position: relative; width: 100%; }
  .main { margin-left: 0; padding: 20px; }
  .app { flex-direction: column; }
}

.search-panel { padding: 0; }

.search-tabs {
  display: flex;
  border-bottom: 1px solid var(--border-subtle);
}

.tab {
  flex: 1;
  padding: 14px;
  background: transparent;
  border: none;
  color: var(--text-muted);
  font-family: inherit;
  font-size: 0.875rem;
  font-weight: 500;
  cursor: pointer;
  border-bottom: 2px solid transparent;
  transition: color 0.15s, border-color 0.15s;
}

.tab:hover { color: var(--text); }
.tab.active {
  color: var(--accent);
  border-bottom-color: var(--accent);
}

.search-form { padding: 24px; }

.tab-content { display: none; }
.tab-content.active { display: block; }

.field-row {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(140px, 1fr));
  gap: 16px;
  margin-bottom: 16px;
}

label {
  display: flex;
  flex-direction: column;
  gap: 6px;
  font-size: 0.8rem;
  font-weight: 500;
  color: var(--text-muted);
}

.req { color: var(--error); }

input[type="text"],
select {
  padding: 10px 12px;
  background: var(--bg-elevated);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  color: var(--text);
  font-family: inherit;
  font-size: 0.9rem;
  transition: border-color 0.15s, box-shadow 0.15s;
}

input:focus, select:focus {
  outline: none;
  border-color: var(--accent);
  box-shadow: 0 0 0 3px var(--accent-glow);
}

.checkbox-label {
  flex-direction: row;
  align-items: center;
  gap: 8px;
  padding-top: 22px;
  cursor: pointer;
}

.checkbox-label input { width: auto; accent-color: var(--accent); }

.hint {
  font-size: 0.8rem;
  color: var(--text-dim);
  margin-top: -8px;
}

.captcha-section .label {
  display: block;
  font-size: 0.7rem;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: var(--text-dim);
  margin-bottom: 6px;
}

.captcha-section {
  margin-bottom: 16px;
  padding: 16px;
  background: var(--bg-elevated);
  border-radius: var(--radius-sm);
  border: 1px dashed var(--warning);
}

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

#captcha-image {
  border-radius: var(--radius-sm);
  border: 1px solid var(--border);
  background: white;
}

.form-actions {
  display: flex;
  gap: 10px;
  padding-top: 8px;
}

/* Buttons */
.btn {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  padding: 10px 18px;
  border: none;
  border-radius: var(--radius-sm);
  font-family: inherit;
  font-size: 0.875rem;
  font-weight: 600;
  cursor: pointer;
  transition: background 0.15s, transform 0.1s;
}

.btn svg { width: 16px; height: 16px; }
.btn:active { transform: scale(0.98); }
.btn:disabled { opacity: 0.5; cursor: not-allowed; }

.btn-primary {
  background: var(--accent);
  color: white;
}
.btn-primary:hover:not(:disabled) { background: var(--accent-hover); }

.btn-ghost {
  background: transparent;
  color: var(--text-muted);
  border: 1px solid var(--border);
}
.btn-ghost:hover:not(:disabled) {
  background: var(--bg-hover);
  color: var(--text);
}

.btn-sm { padding: 6px 12px; font-size: 0.8rem; }

/* Results */
.results-panel {
  min-height: 480px;
  display: flex;
  flex-direction: column;
}

.results-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 16px 20px;
  border-bottom: 1px solid var(--border-subtle);
}

.results-header h3 { font-size: 1rem; font-weight: 600; }

.results-actions {
  display: flex;
  align-items: center;
  gap: 10px;
}

.badge {
  font-family: var(--mono);
  font-size: 0.75rem;
  padding: 4px 10px;
  background: var(--accent-glow);
  color: var(--accent);
  border-radius: 999px;
}

.empty-state, .loading-state {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 60px 24px;
  color: var(--text-dim);
  text-align: center;
}

.empty-state svg {
  width: 48px;
  height: 48px;
  margin-bottom: 16px;
  opacity: 0.4;
}

.spinner {
  width: 36px;
  height: 36px;
  border: 3px solid var(--border);
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
  margin-bottom: 16px;
}

@keyframes spin { to { transform: rotate(360deg); } }

.results-content {
  flex: 1;
  overflow-y: auto;
  max-height: calc(100vh - 200px);
  padding: 16px;
}

.source-group {
  margin-bottom: 20px;
}

.source-group-header {
  display: flex;
  align-items: center;
  gap: 10px;
  margin-bottom: 10px;
  padding-bottom: 8px;
  border-bottom: 1px solid var(--border-subtle);
}

.source-group-header h4 {
  font-size: 0.85rem;
  font-weight: 600;
}

.status-pill {
  font-size: 0.7rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  padding: 3px 8px;
  border-radius: 999px;
}

.status-pill.success { background: rgba(34, 197, 94, 0.15); color: var(--success); }
.status-pill.partial { background: rgba(245, 158, 11, 0.15); color: var(--warning); }
.status-pill.error { background: rgba(239, 68, 68, 0.15); color: var(--error); }
.status-pill.skipped { background: rgba(107, 114, 128, 0.15); color: var(--skipped); }

.duration {
  font-family: var(--mono);
  font-size: 0.7rem;
  color: var(--text-dim);
  margin-left: auto;
}

.record-card {
  background: var(--bg-elevated);
  border: 1px solid var(--border-subtle);
  border-radius: var(--radius-sm);
  margin-bottom: 10px;
  overflow: hidden;
}

.record-card-header {
  display: flex;
  align-items: center;
  gap: 14px;
  padding: 14px 16px;
  cursor: pointer;
  transition: background 0.15s;
}

.record-card-header:hover { background: var(--bg-hover); }

.record-photo {
  width: 64px;
  height: 64px;
  border-radius: var(--radius-sm);
  object-fit: cover;
  background: var(--bg-base);
  border: 1px solid var(--border);
  flex-shrink: 0;
}

.record-photo-placeholder {
  width: 64px;
  height: 64px;
  border-radius: var(--radius-sm);
  background: var(--bg-base);
  border: 1px solid var(--border);
  display: flex;
  align-items: center;
  justify-content: center;
  color: var(--text-dim);
  font-size: 1.1rem;
  font-weight: 600;
  flex-shrink: 0;
}

.record-summary { flex: 1; min-width: 0; }
.record-name {
  font-weight: 600;
  font-size: 0.95rem;
}
.record-meta {
  font-size: 0.8rem;
  color: var(--text-muted);
  margin-top: 2px;
}

.record-charge {
  font-size: 0.72rem;
  color: var(--warning);
  margin-top: 3px;
  line-height: 1.2;
  font-family: var(--mono);
}

.record-expand {
  color: var(--text-dim);
  transition: transform 0.2s;
}
.record-card.open .record-expand { transform: rotate(180deg); }

.record-details {
  display: none;
  padding: 0 16px 16px;
  border-top: 1px solid var(--border-subtle);
}

.record-card.open .record-details { display: block; }

.detail-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(180px, 1fr));
  gap: 12px;
  padding-top: 14px;
}

.detail-item label {
  font-size: 0.65rem;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: var(--text-dim);
}
.detail-item p {
  font-size: 0.85rem;
  color: var(--text);
  margin-top: 2px;
}

.offense-list {
  margin-top: 12px;
  padding-top: 12px;
  border-top: 1px solid var(--border-subtle);
}

.offense-list h5 {
  font-size: 0.7rem;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: var(--text-dim);
  margin-bottom: 8px;
}

.offense-item {
  font-size: 0.8rem;
  color: var(--text-muted);
  padding: 6px 0;
  border-bottom: 1px solid var(--border-subtle);
}
.offense-item:last-child { border-bottom: none; }

.record-links {
  display: flex;
  gap: 10px;
  margin-top: 12px;
}

.record-links a {
  font-size: 0.8rem;
  color: var(--accent);
  text-decoration: none;
}
.record-links a:hover { text-decoration: underline; }

.record-links .copy-btn {
  font-size: 0.7rem;
  padding: 4px 9px;
  border-radius: 999px;
}
.record-links .copy-btn:hover {
  background: var(--bg-hover);
}

.source-message {
  font-size: 0.85rem;
  color: var(--text-muted);
  padding: 12px 16px;
  background: var(--bg-elevated);
  border-radius: var(--radius-sm);
  margin-bottom: 10px;
}

/* Sources grid */
.sources-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(320px, 1fr));
  gap: 16px;
}

.source-card {
  background: var(--bg-surface);
  border: 1px solid var(--border-subtle);
  border-radius: var(--radius);
  padding: 20px;
}

.source-card-header {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  margin-bottom: 12px;
}

.source-card h3 {
  font-size: 1rem;
  font-weight: 600;
}

.source-card .agency {
  font-size: 0.8rem;
  color: var(--text-muted);
  margin-top: 2px;
}

.health-dot {
  width: 10px;
  height: 10px;
  border-radius: 50%;
  flex-shrink: 0;
}
.health-dot.ok { background: var(--success); box-shadow: 0 0 8px rgba(34, 197, 94, 0.4); }
.health-dot.down { background: var(--error); }
.health-dot.unknown { background: var(--text-dim); }

.source-card p.desc {
  font-size: 0.85rem;
  color: var(--text-muted);
  margin-bottom: 12px;
  line-height: 1.5;
}

.capability-tags {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
}

.cap-tag {
  font-family: var(--mono);
  font-size: 0.65rem;
  padding: 3px 8px;
  background: var(--bg-elevated);
  border: 1px solid var(--border-subtle);
  border-radius: 999px;
  color: var(--text-dim);
}

.source-card-footer {
  margin-top: 14px;
  padding-top: 14px;
  border-top: 1px solid var(--border-subtle);
}

.source-card-footer a {
  font-size: 0.8rem;
  color: var(--accent);
  text-decoration: none;
}

/* Generic table used for results / lists */
.audit-table {
  width: 100%;
  border-collapse: collapse;
  font-size: 0.85rem;
}

.audit-table th {
  text-align: left;
  padding: 12px 16px;
  font-size: 0.7rem;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: var(--text-dim);
  border-bottom: 1px solid var(--border-subtle);
  background: var(--bg-elevated);
}

.audit-table td {
  padding: 12px 16px;
  border-bottom: 1px solid var(--border-subtle);
  color: var(--text-muted);
  vertical-align: top;
}

.audit-table td.mono {
  font-family: var(--mono);
  font-size: 0.8rem;
}

.audit-table tr:hover td { background: var(--bg-hover); }

.loading-text {
  color: var(--text-dim);
  font-size: 0.9rem;
  padding: 24px;
}

.hidden { display: none !important; }

.login-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.85);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 9999;
}
.login-box {
  background: var(--bg-surface);
  padding: 2rem;
  border-radius: var(--radius);
  width: 100%;
  max-width: 320px;
  border: 1px solid var(--border-subtle);
}
.login-box h2 {
  margin: 0 0 1rem;
  font-size: 1.25rem;
}
.login-box label {
  display: block;
  margin-bottom: 0.75rem;
}
.login-box input {
  width: 100%;
  padding: 0.5rem;
  background: var(--bg-elevated);
  border: 1px solid var(--border);
  border-radius: 4px;
  color: var(--text);
  margin-top: 0.25rem;
}
.login-box .error {
  color: var(--error);
  font-size: 0.8rem;
  margin-top: 0.5rem;
}

/* Barcode scanner modal */
#barcode-modal .modal-content {
  background: var(--bg-surface);
  border: 1px solid var(--border-subtle);
}
#qr-reader {
  background: #000;
}
#qr-reader video {
  width: 100% !important;
  height: auto !important;
}

/* Toast */
.toast {
  position: fixed;
  bottom: 24px;
  right: 24px;
  padding: 12px 20px;
  background: var(--bg-elevated);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  color: var(--text);
  font-size: 0.875rem;
  box-shadow: 0 8px 32px rgba(0, 0, 0, 0.4);
  z-index: 1000;
  animation: slideIn 0.25s ease;
}

.toast.error { border-color: var(--error); color: var(--error); }

@keyframes slideIn {
  from { transform: translateY(12px); opacity: 0; }
  to { transform: translateY(0); opacity: 1; }
}

/* Light theme refinements */
html[data-theme="light"] .panel,
html[data-theme="light"] .record-card,
html[data-theme="light"] .source-card {
  box-shadow: 0 1px 3px rgba(15, 23, 42, 0.06);
}

html[data-theme="light"] .btn-ghost {
  background: #ffffff;
  border-color: var(--border);
}

html[data-theme="light"] .btn-ghost:hover:not(:disabled) {
  background: var(--bg-hover);
}

html[data-theme="light"] #captcha-image {
  background: #fff;
  border-color: var(--border);
}

html[data-theme="light"] .record-photo-placeholder {
  background: var(--bg-base);
  border-color: var(--border);
}

html[data-theme="light"] .status-pill.success { background: rgba(21, 128, 61, 0.12); }
html[data-theme="light"] .status-pill.partial { background: rgba(180, 83, 9, 0.12); }
html[data-theme="light"] .status-pill.error { background: rgba(185, 28, 28, 0.12); }
html[data-theme="light"] .status-pill.skipped { background: rgba(100, 116, 139, 0.12); }

html[data-theme="light"] .nav-item.active {
  background: rgba(30, 64, 175, 0.1);
}

/* FOIL mugshot expandable */
.record-photo {
  cursor: zoom-in;
  transition: box-shadow 0.15s ease, transform 0.1s ease;
}
.record-photo:hover {
  box-shadow: 0 0 0 3px var(--accent);
  transform: scale(1.03);
}

.photo-lightbox {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.88);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 9999;
  padding: 24px;
}
.photo-lightbox img {
  max-width: 92vw;
  max-height: 92vh;
  border-radius: 10px;
  box-shadow: 0 20px 60px rgba(0, 0, 0, 0.7);
  border: 1px solid var(--border);
  background: #0a0c10;
  object-fit: contain;
}
.photo-lightbox .close {
  position: absolute;
  top: 16px;
  right: 24px;
  font-size: 32px;
  line-height: 1;
  color: #fff;
  cursor: pointer;
  opacity: 0.85;
  user-select: none;
}
.photo-lightbox .close:hover {
  opacity: 1;
}
