/* ============================================================================
 * VajraPulse — design system
 *
 * The foundation both pages share: tokens, base elements and the component
 * classes (buttons, inputs, cards, tables, badges, alerts, modals, states).
 * Screen-specific styling lives in subjects.css.
 *
 * Token naming: the semantic names below are the ones to use. The older
 * --accent-* / --bg-dark names are kept as aliases at the end of :root because
 * markup and inline styles elsewhere still reference them.
 * ========================================================================= */

:root {
  /* ---- Neutrals ---------------------------------------------------------- */
  --gray-25:  #fcfcfd;
  --gray-50:  #f7f8fa;
  --gray-100: #f0f1f4;
  --gray-150: #e9ebef;
  --gray-200: #e4e7ec;
  --gray-300: #d0d5dd;
  --gray-400: #98a2b3;
  --gray-500: #667085;
  --gray-600: #475467;
  --gray-700: #344054;
  --gray-800: #1d2939;
  --gray-900: #101828;

  /* ---- Surfaces ---------------------------------------------------------- */
  --surface-page:    var(--gray-50);
  --surface-card:    #ffffff;
  --surface-subtle:  var(--gray-50);
  --surface-hover:   var(--gray-100);
  --surface-sunken:  var(--gray-100);
  --surface-overlay: rgba(255, 255, 255, 0.88);

  --border-subtle: var(--gray-150);
  --border-default: var(--gray-200);
  --border-strong: var(--gray-300);

  /* ---- Text -------------------------------------------------------------- */
  --text-primary:   var(--gray-900);
  --text-secondary: var(--gray-600);
  --text-tertiary:  var(--gray-500);
  --text-disabled:  var(--gray-400);
  --text-inverse:   #ffffff;

  /* ---- Brand / primary --------------------------------------------------- */
  --primary:         #2563eb;
  --primary-hover:   #1d4ed8;
  --primary-active:  #1e40af;
  --primary-subtle:  #eff4ff;
  --primary-border:  #c7d7fe;
  --primary-text:    #1d4ed8;
  --focus-ring:      rgba(37, 99, 235, 0.35);

  /* ---- Status ------------------------------------------------------------ */
  --success:        #059669;
  --success-hover:  #047857;
  --success-subtle: #ecfdf5;
  --success-border: #a7f3d0;
  --success-text:   #047857;

  --warning:        #d97706;
  --warning-hover:  #b45309;
  --warning-subtle: #fffaeb;
  --warning-border: #fde68a;
  --warning-text:   #b45309;

  --danger:         #dc2626;
  --danger-hover:   #b91c1c;
  --danger-subtle:  #fef3f2;
  --danger-border:  #fecdca;
  --danger-text:    #b42318;

  --info:           #2563eb;
  --info-subtle:    #eff4ff;
  --info-border:    #c7d7fe;
  --info-text:      #1d4ed8;

  /* Range of Motion keeps an identity of its own: it is a distinct tool, not a
     status, so it must not read as primary/success/warning. */
  --rom:            #7c3aed;
  --rom-hover:      #6d28d9;
  --rom-subtle:     #f5f3ff;
  --rom-border:     #ddd6fe;
  --rom-text:       #6d28d9;

  /* ---- Typography -------------------------------------------------------- */
  --font-sans: 'Inter', -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
  --font-mono: 'JetBrains Mono', ui-monospace, SFMono-Regular, Menlo, monospace;

  --text-xs:   12px;
  --text-sm:   13px;
  --text-base: 14px;
  --text-md:   16px;
  --text-lg:   20px;
  --text-xl:   24px;

  /* ---- Spacing (4px scale) ----------------------------------------------- */
  --space-1: 4px;
  --space-2: 8px;
  --space-3: 12px;
  --space-4: 16px;
  --space-5: 20px;
  --space-6: 24px;
  --space-8: 32px;
  --space-10: 40px;

  /* ---- Radius ------------------------------------------------------------ */
  --radius-xs:   4px;
  --radius-sm:   6px;
  --radius-md:   10px;
  --radius-lg:   14px;
  --radius-full: 9999px;

  /* ---- Elevation --------------------------------------------------------- */
  --shadow-xs: 0 1px 2px rgba(16, 24, 40, 0.05);
  --shadow-sm: 0 1px 3px rgba(16, 24, 40, 0.08), 0 1px 2px rgba(16, 24, 40, 0.04);
  --shadow-md: 0 4px 8px -2px rgba(16, 24, 40, 0.08), 0 2px 4px -2px rgba(16, 24, 40, 0.04);
  --shadow-overlay: 0 24px 48px -12px rgba(16, 24, 40, 0.18);

  /* ---- Motion ------------------------------------------------------------ */
  --duration-fast: 150ms;
  --duration-base: 200ms;
  --ease: cubic-bezier(0.4, 0, 0.2, 1);
  --transition-colors: color var(--duration-fast) var(--ease),
                       background-color var(--duration-fast) var(--ease),
                       border-color var(--duration-fast) var(--ease),
                       box-shadow var(--duration-fast) var(--ease);

  /* ---- Control sizing ---------------------------------------------------- */
  --control-height: 38px;
  --control-height-sm: 32px;

  /* ---- Legacy aliases ---------------------------------------------------- */
  --bg-dark:        var(--surface-page);
  --bg-card:        var(--surface-card);
  --bg-card-hover:  var(--surface-hover);
  --bg-glass:       var(--surface-overlay);
  --border-color:   var(--border-default);
  --border-hover:   var(--border-strong);
  --text-main:      var(--text-primary);
  --text-muted:     var(--text-secondary);
  --text-subtle:    var(--text-tertiary);
  --accent-cyan:    var(--primary);
  --accent-blue:    var(--primary);
  --accent-purple:  var(--gray-600);
  --accent-green:   var(--success);
  --accent-red:     var(--danger);
  --accent-amber:   var(--warning);
  --font-brand:     var(--font-sans);
  --font-body:      var(--font-sans);
  --shadow-glow:    var(--shadow-sm);
  --transition-fast: var(--transition-colors);
}

/* ============================================================================
 * Base
 * ========================================================================= */

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

body {
  background-color: var(--surface-page);
  color: var(--text-primary);
  font-family: var(--font-sans);
  font-size: var(--text-base);
  line-height: 1.5;
  -webkit-font-smoothing: antialiased;
  min-height: 100vh;
  padding-bottom: var(--space-10);
  overflow-x: hidden;
}

h1, h2, h3, h4 {
  font-weight: 600;
  letter-spacing: -0.011em;
  color: var(--text-primary);
}

a { color: var(--primary-text); }

/* One focus treatment for the whole app. :focus-visible so a mouse click does
   not leave a ring behind, but every keyboard path shows one. */
:focus-visible {
  outline: 2px solid var(--primary);
  outline-offset: 2px;
  border-radius: var(--radius-xs);
}

::selection {
  background: var(--primary-subtle);
  color: var(--primary-active);
}

::-webkit-scrollbar { width: 10px; height: 10px; }
::-webkit-scrollbar-track { background: transparent; }
::-webkit-scrollbar-thumb {
  background: var(--gray-300);
  border: 2px solid transparent;
  background-clip: content-box;
  border-radius: var(--radius-full);
}
::-webkit-scrollbar-thumb:hover { background: var(--gray-400); background-clip: content-box; }

.hidden { display: none !important; }

/* ============================================================================
 * App shell — navbar
 * ========================================================================= */

.navbar {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: var(--space-4);
  padding: var(--space-3) var(--space-6);
  min-height: 64px;
  background: var(--surface-card);
  border-bottom: 1px solid var(--border-default);
  position: sticky;
  top: 0;
  z-index: 50;
}

.nav-brand {
  display: flex;
  align-items: center;
  gap: var(--space-3);
  min-width: 0;
}

.brand-icon {
  flex-shrink: 0;
  width: 36px;
  height: 36px;
  border-radius: var(--radius-sm);
  background: var(--gray-900);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 16px;
  color: var(--text-inverse);
}

.brand-text {
  display: flex;
  flex-direction: column;
  min-width: 0;
}

.brand-text h1 {
  font-family: var(--font-sans);
  font-size: var(--text-md);
  font-weight: 600;
  letter-spacing: -0.02em;
  color: var(--text-primary);
  line-height: 1.25;
}

.brand-text .sub-text {
  font-size: var(--text-xs);
  color: var(--text-tertiary);
  line-height: 1.3;
}

/* Where you are. Static text plus the page name — navigation itself stays with
   the existing Back control, this only reports position. */
.breadcrumb {
  display: flex;
  align-items: center;
  gap: var(--space-2);
  min-width: 0;
  margin-left: var(--space-5);
  padding-left: var(--space-5);
  border-left: 1px solid var(--border-default);
  font-size: var(--text-sm);
  color: var(--text-tertiary);
}
.breadcrumb .crumb-sep { color: var(--gray-300); font-size: 10px; }
.breadcrumb .crumb-current {
  color: var(--text-primary);
  font-weight: 500;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  text-transform: capitalize;
}

.nav-actions {
  display: flex;
  align-items: center;
  gap: var(--space-2);
  flex-shrink: 0;
}

/* ============================================================================
 * Page header
 * ========================================================================= */

.page-header {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: var(--space-4);
  flex-wrap: wrap;
  margin-bottom: var(--space-5);
}

.page-header-text { min-width: 0; }

.page-title {
  font-size: var(--text-xl);
  font-weight: 600;
  letter-spacing: -0.02em;
  line-height: 1.25;
}

.page-description {
  margin-top: var(--space-1);
  font-size: var(--text-base);
  color: var(--text-secondary);
}

.page-header-actions {
  display: flex;
  align-items: center;
  gap: var(--space-2);
  flex-shrink: 0;
}

/* ============================================================================
 * Status pill (live system state)
 * ========================================================================= */

.status-pill {
  display: inline-flex;
  align-items: center;
  gap: var(--space-2);
  height: var(--control-height);
  padding: 0 var(--space-3);
  border-radius: var(--radius-sm);
  background: var(--surface-subtle);
  border: 1px solid var(--border-default);
  font-size: var(--text-sm);
  font-weight: 500;
  color: var(--text-secondary);
  white-space: nowrap;
}

.dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  flex-shrink: 0;
}
.dot.green { background: var(--success); box-shadow: 0 0 0 3px var(--success-subtle); }
.dot.amber { background: var(--warning); box-shadow: 0 0 0 3px var(--warning-subtle); animation: pulse 1.8s ease-in-out infinite; }
.dot.red   { background: var(--danger);  box-shadow: 0 0 0 3px var(--danger-subtle); }

@keyframes pulse {
  0%, 100% { opacity: 1; }
  50% { opacity: 0.35; }
}

/* ============================================================================
 * Buttons
 *
 * One family, four intents. Every variant carries hover, active, focus,
 * disabled and loading states so an action always says what it is doing.
 * ========================================================================= */

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: var(--space-2);
  height: var(--control-height);
  padding: 0 var(--space-4);
  border-radius: var(--radius-sm);
  border: 1px solid transparent;
  font-family: var(--font-sans);
  font-size: var(--text-base);
  font-weight: 500;
  line-height: 1;
  white-space: nowrap;
  text-decoration: none;
  cursor: pointer;
  transition: var(--transition-colors), transform var(--duration-fast) var(--ease);
}
.btn i { font-size: 13px; }

.btn:disabled,
.btn[disabled] {
  cursor: not-allowed;
  opacity: 0.5;
  box-shadow: none;
}

.btn:not(:disabled):active { transform: translateY(1px); }

.btn-primary {
  background: var(--primary);
  border-color: var(--primary);
  color: var(--text-inverse);
  box-shadow: var(--shadow-xs);
}
.btn-primary:not(:disabled):hover { background: var(--primary-hover); border-color: var(--primary-hover); }
.btn-primary:not(:disabled):active { background: var(--primary-active); border-color: var(--primary-active); }

.btn-secondary {
  background: var(--surface-card);
  border-color: var(--border-strong);
  color: var(--text-primary);
  box-shadow: var(--shadow-xs);
}
.btn-secondary:not(:disabled):hover { background: var(--surface-hover); border-color: var(--gray-400); }
.btn-secondary:not(:disabled):active { background: var(--gray-150); }

.btn-ghost {
  background: transparent;
  border-color: transparent;
  color: var(--text-secondary);
}
.btn-ghost:not(:disabled):hover { background: var(--surface-hover); color: var(--text-primary); }

.btn-danger {
  background: var(--danger);
  border-color: var(--danger);
  color: var(--text-inverse);
  box-shadow: var(--shadow-xs);
}
.btn-danger:not(:disabled):hover { background: var(--danger-hover); border-color: var(--danger-hover); }

.btn-small {
  height: var(--control-height-sm);
  padding: 0 var(--space-3);
  font-size: var(--text-sm);
  background: var(--surface-card);
  border-color: var(--border-strong);
  color: var(--text-primary);
}
.btn-small:not(:disabled):hover { background: var(--surface-hover); }

/* Icon-only: square, and every instance needs a tooltip + aria-label. */
.btn-icon {
  width: var(--control-height);
  padding: 0;
  flex-shrink: 0;
}
.btn-icon.btn-small { width: var(--control-height-sm); }

.btn-link {
  background: none;
  border: none;
  padding: 0;
  font-family: var(--font-sans);
  font-size: var(--text-sm);
  font-weight: 500;
  color: var(--primary-text);
  cursor: pointer;
  transition: var(--transition-colors);
}
.btn-link:hover { color: var(--primary-active); text-decoration: underline; }

/* ============================================================================
 * Forms
 * ========================================================================= */

.field-label {
  display: block;
  margin-bottom: var(--space-2);
  font-size: var(--text-sm);
  font-weight: 500;
  color: var(--text-primary);
}
.field-label .required { color: var(--danger); margin-left: 2px; }
.field-hint {
  margin-top: var(--space-1);
  font-size: var(--text-xs);
  color: var(--text-tertiary);
}
.field-error {
  display: flex;
  align-items: center;
  gap: var(--space-1);
  margin-top: var(--space-1);
  font-size: var(--text-xs);
  color: var(--danger-text);
}

.input,
.search-box input,
.select-dropdown {
  width: 100%;
  height: var(--control-height);
  padding: 0 var(--space-3);
  background: var(--surface-card);
  border: 1px solid var(--border-strong);
  border-radius: var(--radius-sm);
  color: var(--text-primary);
  font-family: var(--font-sans);
  font-size: var(--text-base);
  outline: none;
  transition: var(--transition-colors);
}
.input::placeholder,
.search-box input::placeholder { color: var(--text-disabled); }

.input:hover,
.search-box input:hover,
.select-dropdown:hover { border-color: var(--gray-400); }

.input:focus,
.search-box input:focus,
.select-dropdown:focus {
  border-color: var(--primary);
  box-shadow: 0 0 0 3px var(--focus-ring);
}
.input.has-error { border-color: var(--danger); }
.input.has-error:focus { box-shadow: 0 0 0 3px var(--danger-border); }

.search-box {
  position: relative;
  flex: 1;
  min-width: 240px;
  max-width: 420px;
}
.search-box > i {
  position: absolute;
  left: var(--space-3);
  top: 50%;
  transform: translateY(-50%);
  color: var(--text-disabled);
  font-size: 13px;
  pointer-events: none;
}
.search-box input { padding-left: 36px; padding-right: 36px; }

.btn-clear {
  position: absolute;
  right: 6px;
  top: 50%;
  transform: translateY(-50%);
  display: flex;
  align-items: center;
  justify-content: center;
  width: 26px;
  height: 26px;
  border-radius: var(--radius-xs);
  background: none;
  border: none;
  color: var(--text-tertiary);
  font-size: 13px;
  cursor: pointer;
  transition: var(--transition-colors);
}
.btn-clear:hover { background: var(--surface-hover); color: var(--text-primary); }

.select-dropdown { cursor: pointer; width: auto; }

/* Toolbar above a table: search on the left, filters/actions on the right. */
.filter-panel {
  display: flex;
  flex-wrap: wrap;
  gap: var(--space-3);
  justify-content: space-between;
  align-items: center;
  margin-bottom: var(--space-4);
}

.filter-group {
  display: flex;
  align-items: center;
  gap: var(--space-2);
  flex-wrap: wrap;
}

.filter-pills {
  display: flex;
  gap: 2px;
  background: var(--surface-sunken);
  padding: 3px;
  border-radius: var(--radius-sm);
}

.pill {
  height: 30px;
  padding: 0 var(--space-3);
  border-radius: var(--radius-xs);
  background: none;
  border: none;
  color: var(--text-secondary);
  font-family: var(--font-sans);
  font-size: var(--text-sm);
  font-weight: 500;
  cursor: pointer;
  transition: var(--transition-colors);
}
.pill:hover { color: var(--text-primary); }
.pill.active {
  background: var(--surface-card);
  color: var(--text-primary);
  box-shadow: var(--shadow-xs);
}

/* ============================================================================
 * Cards
 * ========================================================================= */

.card,
.table-card {
  background: var(--surface-card);
  border: 1px solid var(--border-default);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow-xs);
  overflow: hidden;
}

.card-header {
  display: flex;
  align-items: center;
  gap: var(--space-2);
  padding: var(--space-4) var(--space-5);
  border-bottom: 1px solid var(--border-subtle);
}
.card-title {
  font-size: var(--text-base);
  font-weight: 600;
}
.card-body { padding: var(--space-5); }

/* ============================================================================
 * Stats
 * ========================================================================= */

.stats-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(240px, 1fr));
  gap: var(--space-4);
  margin-bottom: var(--space-6);
}

.stat-card {
  background: var(--surface-card);
  border: 1px solid var(--border-default);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow-xs);
  padding: var(--space-5);
  display: flex;
  align-items: center;
  gap: var(--space-4);
  transition: var(--transition-colors);
}
.stat-card:hover { border-color: var(--border-strong); box-shadow: var(--shadow-sm); }

.stat-icon {
  flex-shrink: 0;
  width: 44px;
  height: 44px;
  border-radius: var(--radius-md);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 18px;
}
.stat-icon.purple { background: var(--primary-subtle); color: var(--primary-text); }
.stat-icon.green  { background: var(--success-subtle); color: var(--success-text); }
.stat-icon.red    { background: var(--danger-subtle);  color: var(--danger-text); }
.stat-icon.blue   { background: var(--primary-subtle); color: var(--primary-text); }

.stat-info { min-width: 0; }
.stat-info .label {
  font-size: var(--text-sm);
  font-weight: 500;
  color: var(--text-secondary);
}
.stat-info h2 {
  font-family: var(--font-sans);
  font-size: var(--text-xl);
  font-weight: 600;
  letter-spacing: -0.02em;
  margin-top: 2px;
  font-variant-numeric: tabular-nums;
}

.breakdown-text {
  display: flex;
  gap: var(--space-4);
  font-size: var(--text-sm);
  margin-top: var(--space-1);
  color: var(--text-secondary);
}
.breakdown-text i { color: var(--text-disabled); margin-right: 2px; }
.breakdown-text strong { color: var(--text-primary); font-variant-numeric: tabular-nums; }

/* ============================================================================
 * Tables
 * ========================================================================= */

.table-container {
  position: relative;
  min-height: 320px;
  overflow-x: auto;
}

.data-table {
  width: 100%;
  border-collapse: collapse;
  text-align: left;
  font-size: var(--text-base);
}

.data-table th {
  padding: var(--space-3) var(--space-5);
  background: var(--surface-subtle);
  color: var(--text-secondary);
  font-weight: 600;
  font-size: var(--text-xs);
  text-transform: uppercase;
  letter-spacing: 0.04em;
  white-space: nowrap;
  border-bottom: 1px solid var(--border-default);
}

.data-table td {
  padding: var(--space-4) var(--space-5);
  border-bottom: 1px solid var(--border-subtle);
  vertical-align: middle;
  color: var(--text-secondary);
}
.data-table tbody tr:last-child td { border-bottom: none; }

.data-table tbody tr { transition: background-color var(--duration-fast) var(--ease); }
.data-table tbody tr:hover { background: var(--surface-subtle); }

.num-cell { font-variant-numeric: tabular-nums; }
.muted-cell { color: var(--text-tertiary); }

.file-cell { display: flex; flex-direction: column; gap: 2px; }
.file-name { font-weight: 500; color: var(--text-primary); }
.file-id {
  font-size: var(--text-xs);
  color: var(--text-tertiary);
  font-family: var(--font-mono);
}

/* ============================================================================
 * Badges
 * ========================================================================= */

.badge {
  display: inline-flex;
  align-items: center;
  gap: 5px;
  padding: 3px var(--space-2);
  border-radius: var(--radius-full);
  font-size: var(--text-xs);
  font-weight: 500;
  line-height: 1.4;
  white-space: nowrap;
  border: 1px solid transparent;
}
.badge i { font-size: 10px; }
.badge-neutral { background: var(--surface-sunken); color: var(--text-secondary); border-color: var(--border-default); }
.badge-primary { background: var(--primary-subtle); color: var(--primary-text); border-color: var(--primary-border); }
.badge-success { background: var(--success-subtle); color: var(--success-text); border-color: var(--success-border); }
.badge-warning { background: var(--warning-subtle); color: var(--warning-text); border-color: var(--warning-border); }
.badge-danger  { background: var(--danger-subtle);  color: var(--danger-text);  border-color: var(--danger-border); }
.badge-mono { font-family: var(--font-mono); font-size: 11px; letter-spacing: -0.01em; }

.badge-type {
  display: inline-flex;
  align-items: center;
  gap: 5px;
  padding: 3px var(--space-2);
  border-radius: var(--radius-full);
  font-size: var(--text-xs);
  font-weight: 500;
  background: var(--danger-subtle);
  color: var(--danger-text);
  border: 1px solid var(--danger-border);
}
.badge-type.image {
  background: var(--primary-subtle);
  color: var(--primary-text);
  border-color: var(--primary-border);
}

.status-badge {
  display: inline-flex;
  align-items: center;
  gap: 5px;
  padding: 3px var(--space-2);
  border-radius: var(--radius-full);
  font-size: var(--text-xs);
  font-weight: 500;
  text-transform: capitalize;
  border: 1px solid transparent;
}
.status-badge::before {
  content: '';
  width: 6px;
  height: 6px;
  border-radius: 50%;
  background: currentColor;
}
.status-badge.green { background: var(--success-subtle); color: var(--success-text); border-color: var(--success-border); }
.status-badge.red   { background: var(--danger-subtle);  color: var(--danger-text);  border-color: var(--danger-border); }
.status-badge.amber { background: var(--warning-subtle); color: var(--warning-text); border-color: var(--warning-border); }

/* Confidence meter */
.confidence-wrapper {
  display: flex;
  align-items: center;
  gap: var(--space-2);
  font-variant-numeric: tabular-nums;
}
.confidence-bar {
  width: 64px;
  height: 6px;
  background: var(--gray-150);
  border-radius: var(--radius-full);
  overflow: hidden;
}
.confidence-fill {
  height: 100%;
  background: var(--success);
  border-radius: var(--radius-full);
}

/* ============================================================================
 * Alerts
 * ========================================================================= */

.alert {
  display: flex;
  align-items: flex-start;
  gap: var(--space-3);
  padding: var(--space-3) var(--space-4);
  border-radius: var(--radius-md);
  border: 1px solid var(--border-default);
  background: var(--surface-subtle);
  color: var(--text-secondary);
  font-size: var(--text-sm);
  line-height: 1.5;
}
.alert > i:first-child { margin-top: 2px; font-size: 14px; flex-shrink: 0; }
.alert strong { color: var(--text-primary); font-weight: 600; }
.alert-title { display: block; font-weight: 600; margin-bottom: 2px; color: var(--text-primary); }

.alert-info    { background: var(--info-subtle);    border-color: var(--info-border);    color: var(--info-text); }
.alert-success { background: var(--success-subtle); border-color: var(--success-border); color: var(--success-text); }
.alert-warning { background: var(--warning-subtle); border-color: var(--warning-border); color: var(--warning-text); }
.alert-error   { background: var(--danger-subtle);  border-color: var(--danger-border);  color: var(--danger-text); }
.alert-info strong, .alert-success strong,
.alert-warning strong, .alert-error strong { color: inherit; }

/* Legacy error box (extractions detail modal) */
.error-box {
  display: flex;
  gap: var(--space-3);
  background: var(--danger-subtle);
  border: 1px solid var(--danger-border);
  padding: var(--space-4);
  border-radius: var(--radius-md);
  color: var(--danger-text);
  font-size: var(--text-sm);
}
.error-box > i { margin-top: 2px; }
.error-box strong { display: block; margin-bottom: 2px; }
.error-stack {
  margin-top: var(--space-2);
  padding: var(--space-2);
  background: var(--surface-card);
  border-radius: var(--radius-sm);
  font-family: var(--font-mono);
  font-size: 11px;
  color: var(--text-secondary);
  max-height: 120px;
  overflow: auto;
  white-space: pre-wrap;
}

/* ============================================================================
 * States — loading, empty
 * ========================================================================= */

.table-overlay {
  position: absolute;
  inset: 0;
  background: var(--surface-overlay);
  backdrop-filter: blur(2px);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: var(--space-3);
  padding: var(--space-6);
  text-align: center;
  z-index: 10;
  font-size: var(--text-sm);
  color: var(--text-secondary);
}
.table-overlay h3 {
  font-size: var(--text-md);
  font-weight: 600;
  color: var(--text-primary);
}
.table-overlay p {
  max-width: 380px;
  font-size: var(--text-sm);
  color: var(--text-secondary);
}

.spinner {
  width: 28px;
  height: 28px;
  border: 2.5px solid var(--gray-200);
  border-top-color: var(--primary);
  border-radius: 50%;
  animation: spin 0.7s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

.empty-icon {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 48px;
  height: 48px;
  border-radius: var(--radius-full);
  background: var(--surface-sunken);
  font-size: 20px;
  color: var(--text-disabled);
}

/* Skeleton rows: the shape of the table arrives before the data does, so the
   page does not jump when it lands. */
.skeleton-row td { padding: var(--space-4) var(--space-5); }
.skeleton {
  display: block;
  height: 12px;
  border-radius: var(--radius-full);
  background: linear-gradient(90deg, var(--gray-100) 25%, var(--gray-150) 37%, var(--gray-100) 63%);
  background-size: 400% 100%;
  animation: shimmer 1.4s ease infinite;
}
.skeleton-circle { width: 32px; height: 32px; border-radius: 50%; }
@keyframes shimmer {
  0% { background-position: 100% 50%; }
  100% { background-position: 0 50%; }
}

/* ============================================================================
 * Pagination
 * ========================================================================= */

.pagination-footer {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: var(--space-3) var(--space-5);
  background: var(--surface-subtle);
  border-top: 1px solid var(--border-subtle);
  flex-wrap: wrap;
  gap: var(--space-3);
}

.pagination-info {
  font-size: var(--text-sm);
  color: var(--text-secondary);
}
.pagination-info strong {
  color: var(--text-primary);
  font-weight: 600;
  font-variant-numeric: tabular-nums;
}

.pagination-controls { display: flex; gap: var(--space-1); }

.page-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  min-width: 32px;
  height: 32px;
  padding: 0 var(--space-2);
  background: var(--surface-card);
  border: 1px solid var(--border-default);
  color: var(--text-secondary);
  border-radius: var(--radius-sm);
  font-family: var(--font-sans);
  font-size: var(--text-sm);
  font-weight: 500;
  font-variant-numeric: tabular-nums;
  cursor: pointer;
  transition: var(--transition-colors);
}
.page-btn:hover:not(:disabled) {
  background: var(--surface-hover);
  border-color: var(--border-strong);
  color: var(--text-primary);
}
.page-btn.active {
  background: var(--primary);
  border-color: var(--primary);
  color: var(--text-inverse);
}
.page-btn:disabled { opacity: 0.45; cursor: not-allowed; }
.page-ellipsis {
  display: inline-flex;
  align-items: center;
  padding: 0 var(--space-1);
  color: var(--text-disabled);
  font-size: var(--text-sm);
}

/* ============================================================================
 * Modals
 * ========================================================================= */

.modal-backdrop {
  position: fixed;
  inset: 0;
  background: rgba(16, 24, 40, 0.55);
  backdrop-filter: blur(3px);
  z-index: 100;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: var(--space-6);
  animation: fadeIn var(--duration-base) var(--ease);
}
@keyframes fadeIn { from { opacity: 0; } to { opacity: 1; } }

.modal-content {
  background: var(--surface-card);
  border: 1px solid var(--border-default);
  border-radius: var(--radius-lg);
  width: 100%;
  max-width: 900px;
  max-height: 90vh;
  display: flex;
  flex-direction: column;
  box-shadow: var(--shadow-overlay);
  animation: modalIn var(--duration-base) var(--ease);
}
@keyframes modalIn {
  from { opacity: 0; transform: translateY(8px) scale(0.99); }
  to { opacity: 1; transform: none; }
}

.modal-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: var(--space-4);
  padding: var(--space-4) var(--space-5);
  border-bottom: 1px solid var(--border-subtle);
}

.modal-title-group {
  display: flex;
  align-items: center;
  gap: var(--space-3);
  min-width: 0;
}

.modal-title-group h2 {
  font-family: var(--font-sans);
  font-size: var(--text-md);
  font-weight: 600;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.btn-close {
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  width: 32px;
  height: 32px;
  border-radius: var(--radius-sm);
  background: none;
  border: none;
  color: var(--text-tertiary);
  font-size: 16px;
  cursor: pointer;
  transition: var(--transition-colors);
}
.btn-close:hover { background: var(--surface-hover); color: var(--text-primary); }

.modal-body {
  padding: var(--space-5);
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  gap: var(--space-5);
}

/* Metadata strip */
.meta-strip {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(150px, 1fr));
  gap: var(--space-4);
  background: var(--surface-subtle);
  padding: var(--space-4);
  border-radius: var(--radius-md);
  border: 1px solid var(--border-subtle);
}

.meta-label {
  font-size: 11px;
  color: var(--text-tertiary);
  text-transform: uppercase;
  letter-spacing: 0.04em;
  font-weight: 500;
  display: block;
}

.meta-val {
  display: block;
  font-size: var(--text-sm);
  font-weight: 500;
  color: var(--text-primary);
  margin-top: 3px;
  overflow-wrap: anywhere;
}
code.meta-val { font-family: var(--font-mono); font-size: var(--text-xs); }

/* Section header inside a modal */
.section-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: var(--space-3);
}
.section-header h3 {
  font-size: var(--text-base);
  font-weight: 600;
  display: flex;
  align-items: center;
  gap: var(--space-2);
}
.section-header h3 i { color: var(--text-disabled); font-size: 13px; }

.text-view-container { min-width: 0; }

.text-box {
  background: var(--surface-subtle);
  border: 1px solid var(--border-default);
  border-radius: var(--radius-md);
  padding: var(--space-4);
  font-family: var(--font-mono);
  font-size: var(--text-sm);
  color: var(--text-primary);
  max-height: 240px;
  overflow: auto;
  white-space: pre-wrap;
  line-height: 1.7;
}

/* Page inspector */
.pages-container {
  background: var(--surface-card);
  border: 1px solid var(--border-default);
  border-radius: var(--radius-md);
  overflow: hidden;
}

.page-tabs {
  display: flex;
  gap: var(--space-1);
  background: var(--surface-subtle);
  padding: var(--space-2) var(--space-3);
  border-bottom: 1px solid var(--border-subtle);
  overflow-x: auto;
}

.page-tab {
  height: 28px;
  padding: 0 var(--space-3);
  border-radius: var(--radius-xs);
  background: none;
  border: 1px solid transparent;
  color: var(--text-secondary);
  font-family: var(--font-sans);
  font-size: var(--text-xs);
  font-weight: 500;
  white-space: nowrap;
  cursor: pointer;
  transition: var(--transition-colors);
}
.page-tab:hover { background: var(--surface-hover); color: var(--text-primary); }
.page-tab.active {
  background: var(--surface-card);
  color: var(--primary-text);
  border-color: var(--border-default);
  box-shadow: var(--shadow-xs);
}

.page-detail-box { padding: var(--space-4); }
.page-detail-box h4 {
  font-size: var(--text-sm);
  font-weight: 600;
  color: var(--text-primary);
}
.page-meta-line {
  font-size: var(--text-sm);
  color: var(--text-secondary);
  margin-bottom: var(--space-2);
}
.page-empty { color: var(--text-tertiary); font-size: var(--text-sm); font-style: italic; }

.block-table {
  width: 100%;
  border-collapse: collapse;
  font-size: var(--text-xs);
  margin-top: var(--space-3);
  border: 1px solid var(--border-subtle);
  border-radius: var(--radius-sm);
  overflow: hidden;
}
.block-table th {
  padding: var(--space-2) var(--space-3);
  background: var(--surface-subtle);
  color: var(--text-secondary);
  font-weight: 600;
  text-align: left;
  text-transform: uppercase;
  letter-spacing: 0.04em;
}
.block-table td {
  padding: var(--space-2) var(--space-3);
  border-top: 1px solid var(--border-subtle);
  font-family: var(--font-mono);
  color: var(--text-secondary);
  overflow-wrap: anywhere;
}
.block-table .kv-key { color: var(--text-primary); font-weight: 600; }
.block-table .kv-value { color: var(--text-primary); }
.block-table .block-conf { color: var(--success-text); font-variant-numeric: tabular-nums; }
.block-table .block-bbox { color: var(--text-tertiary); font-size: 11px; }

/* ============================================================================
 * Layout container
 * ========================================================================= */

.container {
  max-width: none;
  margin: 0 auto;
  padding: var(--space-6) var(--space-6);
}
.container-narrow { max-width: 1280px; }

/* ============================================================================
 * Tooltips (see ui.js — one floating element, reused)
 * ========================================================================= */

.ui-tooltip {
  position: fixed;
  z-index: 9999;
  max-width: 260px;
  padding: 6px var(--space-2);
  border-radius: var(--radius-sm);
  background: var(--gray-900);
  color: var(--text-inverse);
  font-family: var(--font-sans);
  font-size: var(--text-xs);
  font-weight: 450;
  line-height: 1.45;
  box-shadow: var(--shadow-md);
  pointer-events: none;
  opacity: 0;
  transform: translateY(2px);
  transition: opacity var(--duration-fast) var(--ease), transform var(--duration-fast) var(--ease);
}
.ui-tooltip.visible { opacity: 1; transform: none; }

/* ============================================================================
 * Responsive
 * ========================================================================= */

@media (max-width: 900px) {
  .navbar { padding: var(--space-3) var(--space-4); }
  .breadcrumb { display: none; }
  .container { padding: var(--space-5) var(--space-4); }
  .modal-backdrop { padding: var(--space-3); }
  .modal-body { padding: var(--space-4); }
}

@media (max-width: 640px) {
  .navbar { flex-wrap: wrap; }
  .nav-actions { flex-wrap: wrap; }
  .search-box { max-width: none; }
  .page-title { font-size: var(--text-lg); }
  .data-table th, .data-table td { padding: var(--space-3) var(--space-4); }
  .pagination-footer { justify-content: center; }
}

/* ============================================================================
 * Reduced motion
 * ========================================================================= */

@media (prefers-reduced-motion: reduce) {
  *,
  *::before,
  *::after {
    animation-duration: 0.01ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: 0.01ms !important;
    scroll-behavior: auto !important;
  }
}

/* Visually hidden, still read by screen readers. */
.sr-only {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  white-space: nowrap;
  border: 0;
}

/* ============================================================================
 * OCR extraction dashboard (extractions.html)
 * ========================================================================= */

.doc-row { cursor: pointer; }
.doc-row:focus-visible { outline: 2px solid var(--primary); outline-offset: -2px; }
.doc-name-cell { min-width: 220px; }

.text-snippet {
  font-family: var(--font-mono);
  font-size: var(--text-sm);
  color: var(--text-secondary);
  line-height: 1.6;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
}
.no-text { color: var(--text-disabled); font-style: italic; font-family: var(--font-sans); }

.block-heading {
  display: flex;
  align-items: center;
  gap: var(--space-2);
  margin: var(--space-4) 0 var(--space-2);
  font-size: var(--text-sm);
  font-weight: 600;
  color: var(--text-primary);
}
.block-heading i { color: var(--text-disabled); font-size: 12px; }

.kv-table { margin-bottom: var(--space-4); }
.kv-table th:first-child { width: 40%; }

.page-text-box { max-height: 180px; }

.table-error-cell {
  padding: var(--space-6) var(--space-5) !important;
  text-align: center;
  color: var(--danger-text);
}

/* ============================================================================
 * Account menu (navbar)
 *
 * Who is signed in, and the way out. Present on every protected page; the
 * behaviour lives in auth.js alongside the session it reports on.
 * ========================================================================= */

.account-menu { position: relative; }

.account-trigger {
  display: inline-flex;
  align-items: center;
  gap: var(--space-2);
  max-width: 220px;
}

.account-avatar {
  flex-shrink: 0;
  width: 22px;
  height: 22px;
  border-radius: var(--radius-full);
  background: var(--primary);
  color: var(--text-inverse);
  font-size: 10px;
  font-weight: 600;
  letter-spacing: 0.02em;
  display: flex;
  align-items: center;
  justify-content: center;
}

.account-email {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.account-caret {
  font-size: 10px;
  color: var(--text-tertiary);
  transition: transform var(--duration-fast) var(--ease);
}

.account-menu.is-open .account-caret { transform: rotate(180deg); }

.account-dropdown {
  position: absolute;
  top: calc(100% + var(--space-2));
  right: 0;
  z-index: 60;
  min-width: 232px;
  padding: var(--space-2);
  background: var(--surface-card);
  border: 1px solid var(--border-default);
  border-radius: var(--radius-md);
  box-shadow: var(--shadow-overlay);
}

.account-dropdown-head {
  display: flex;
  flex-direction: column;
  gap: 2px;
  padding: var(--space-2) var(--space-3) var(--space-3);
  margin-bottom: var(--space-2);
  border-bottom: 1px solid var(--border-subtle);
}

.account-dropdown-name {
  font-size: var(--text-sm);
  font-weight: 600;
  color: var(--text-primary);
}

.account-dropdown-email {
  font-size: var(--text-xs);
  color: var(--text-tertiary);
  overflow: hidden;
  text-overflow: ellipsis;
}

.account-dropdown-item {
  display: flex;
  align-items: center;
  gap: var(--space-2);
  width: 100%;
  padding: var(--space-2) var(--space-3);
  background: none;
  border: none;
  border-radius: var(--radius-sm);
  color: var(--text-secondary);
  font-family: var(--font-sans);
  font-size: var(--text-sm);
  text-align: left;
  cursor: pointer;
  transition: var(--transition-colors);
}

.account-dropdown-item:hover { background: var(--surface-hover); color: var(--text-primary); }
.account-dropdown-item:focus-visible { outline: none; box-shadow: 0 0 0 3px var(--focus-ring); }

@media (max-width: 720px) {
  .account-email { display: none; }
  .account-trigger { padding-left: var(--space-2); padding-right: var(--space-2); }
}

/* ============================================================================
 * Touch targets
 *
 * The dashboard is reviewed on tablets as well as desktops, and the two want
 * different things: a mouse is pixel-accurate and rewards density, a fingertip
 * covers roughly 9mm and does not. So this is split in two.
 *
 * 1. Hit-area expansion, applied everywhere. A pseudo-element gives the small
 *    icon buttons a 44px target without changing how they look, so a desktop
 *    user sees the same dense UI while every pointer gets an honest target.
 *
 * 2. A `pointer: coarse` block, which is the tablet layout proper: controls
 *    actually grow, and anything that was revealed on hover becomes permanent,
 *    because a touch screen has no hover to reveal it with.
 *
 * 44px is the floor throughout (Apple HIG; Material asks 48, which the primary
 * controls below meet). Adjacent targets are kept at least 8px apart — a big
 * target that overlaps its neighbour just means confidently hitting the wrong
 * thing.
 * ========================================================================= */

/* ---- Applies to every pointer --------------------------------------------- */

/* Removes the ~300ms double-tap-to-zoom delay and the grey flash on tap, both
   of which make a correctly sized button still feel unresponsive. */
.btn,
.pill,
.page-btn,
.btn-clear,
.btn-link,
.account-trigger,
.account-dropdown-item {
  touch-action: manipulation;
  -webkit-tap-highlight-color: transparent;
}

/* The search field's clear button is 26px of visible circle. The pseudo-element
   below makes it a 44px target while leaving those 26px exactly as they were. */
.btn-clear { position: absolute; }
.btn-clear::after {
  content: '';
  position: absolute;
  top: 50%;
  left: 50%;
  width: 44px;
  height: 44px;
  transform: translate(-50%, -50%);
}

/* ---- Touch devices -------------------------------------------------------- */

@media (pointer: coarse) {
  /* The "small" variant does not survive a fingertip: 40px would put every
     .btn-small, .ocr-copy and .lb-crop-back under the floor. On touch it keeps
     its lighter styling but not its smaller target. */
  :root {
    --control-height: 46px;
    --control-height-sm: 44px;
  }

  /* Text grows with the target: a 46px button with 14px text reads as a slab
     with a label lost in the middle of it. */
  .btn { padding: 0 var(--space-5); font-size: var(--text-md); }
  .btn i { font-size: 15px; }
  .btn-small { padding: 0 var(--space-4); font-size: var(--text-base); }

  /* Icon-only buttons are square, so both height tokens already widen them. */

  /* Inline text buttons have no box of their own to grow. */
  .btn-link {
    display: inline-flex;
    align-items: center;
    min-height: 44px;
    font-size: var(--text-base);
  }

  .pill { height: 44px; padding: 0 var(--space-4); font-size: var(--text-base); }
  .filter-pills { gap: var(--space-1); }

  .page-btn {
    min-width: 44px;
    height: 44px;
    padding: 0 var(--space-3);
    font-size: var(--text-base);
  }
  .pagination { gap: var(--space-2); }

  .btn-clear { width: 36px; height: 36px; right: 8px; }

  /* Comfortably inside the taller input, and clear of its right edge. */
  .search-box input { padding-right: 52px; }

  .account-trigger { max-width: none; }
  .account-dropdown { min-width: 260px; }
  .account-dropdown-item {
    min-height: 48px;
    padding: var(--space-3) var(--space-4);
    font-size: var(--text-base);
  }

  /* Table rows are themselves tap targets on the subjects list. */
  .data-table td { padding-top: var(--space-4); padding-bottom: var(--space-4); }
}
