/* Component System: Buttons, Badges, Cards, Tabs, Sliders, Tooltips, Citations, Drawer, Modal, Toasts */

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  padding: 8px 14px;
  border-radius: var(--radius-sm);
  border: 1px solid var(--border-strong);
  background: var(--bg-elevated);
  color: var(--text-primary);
  font-family: var(--font-sans);
  font-size: 13px;
  font-weight: 500;
  line-height: 1.25;
  cursor: pointer;
  box-shadow: var(--inner-highlight), var(--shadow-sm);
  transition: background var(--dur-fast) var(--ease-out), border-color var(--dur-fast) var(--ease-out), color var(--dur-fast) var(--ease-out), opacity var(--dur-fast) var(--ease-out), transform var(--dur-press) var(--ease-out);
  text-decoration: none;
  white-space: nowrap;
  user-select: none;
}

@media (hover: hover) and (pointer: fine) {
  .btn:hover:not(:disabled) {
    background: var(--bg-hover);
    border-color: rgba(255, 255, 255, 0.26);
  }
  [data-theme="light"] .btn:hover:not(:disabled) {
    border-color: rgba(9, 9, 11, 0.3);
  }
}

.btn:active:not(:disabled) {
  transform: scale(0.97);
}

.btn:disabled {
  opacity: 0.42;
  background: var(--bg-elevated);
  border-color: var(--border-subtle);
  color: var(--text-muted);
  box-shadow: none;
  cursor: not-allowed;
}

.btn-primary {
  background: var(--accent);
  border-color: var(--accent);
  color: var(--accent-contrast);
  font-weight: 600;
}

.btn-primary:disabled {
  background: var(--bg-elevated);
  border-color: var(--border-subtle);
  color: var(--text-muted);
  opacity: 0.55;
}

@media (hover: hover) and (pointer: fine) {
  .btn-primary:hover:not(:disabled) {
    background: var(--accent-strong);
    border-color: var(--accent-strong);
  }
}

.btn-danger {
  background: var(--status-error-soft);
  border-color: var(--status-error-border);
  color: var(--status-error);
}

.btn-ghost {
  background: transparent;
  border-color: transparent;
  box-shadow: none;
  color: var(--text-secondary);
}

@media (hover: hover) and (pointer: fine) {
  .btn-ghost:hover:not(:disabled) {
    background: var(--bg-elevated);
    border-color: var(--border-subtle);
    color: var(--text-primary);
  }
}

.btn-sm {
  padding: 5px 10px;
  font-size: 12px;
  border-radius: var(--radius-sm);
}

.btn-xs {
  padding: 2px 7px;
  font-size: 11px;
  border-radius: 4px;
}

/* Badges & Chips */
.badge,
.chip {
  display: inline-flex;
  align-items: center;
  gap: 5px;
  padding: 3px 8px;
  border-radius: var(--radius-pill);
  font-family: var(--font-mono);
  font-variant-numeric: tabular-nums;
  font-size: 11px;
  font-weight: 500;
  line-height: 1.35;
  border: 1px solid var(--border-subtle);
  background: var(--bg-elevated);
  color: var(--text-secondary);
  max-width: 100%;
}

.badge-accent,
.badge-running {
  border-color: var(--status-running-border);
  background: var(--status-running-soft);
  color: var(--status-running);
}

.badge-confirmed,
.badge-done {
  border-color: var(--status-done-border);
  background: var(--status-done-soft);
  color: var(--status-done);
}

.badge-uncertain,
.badge-unavailable {
  border-color: var(--status-unavailable-border);
  background: var(--status-unavailable-soft);
  color: var(--status-unavailable);
}

.badge-rejected,
.badge-error {
  border-color: var(--status-error-border);
  background: var(--status-error-soft);
  color: var(--status-error);
}

.badge-waiting {
  border-color: var(--status-waiting-border);
  background: var(--status-waiting-soft);
  color: var(--status-waiting);
}

/* Cards & Surfaces */
.card {
  background: var(--bg-surface);
  border: 1px solid var(--border-subtle);
  border-radius: var(--radius-lg);
  padding: var(--space-5);
  min-width: 0;
  max-width: 100%;
  overflow: hidden;
  box-shadow: var(--inner-highlight), var(--shadow-card);
  transition: border-color var(--dur-fast) var(--ease-out), transform var(--dur-press) var(--ease-out);
}

.card-interactive {
  cursor: pointer;
}

@media (hover: hover) and (pointer: fine) {
  .card-interactive:hover {
    border-color: var(--border-strong);
  }
}

.card-interactive:focus-within {
  border-color: var(--border-strong);
}

.card-interactive:active {
  transform: scale(0.99);
}

.card-interactive.is-selected {
  border-color: var(--accent);
  box-shadow: 0 0 0 1px var(--accent), var(--shadow-card);
}

/* Segmented Controls & Tabs */
.segmented-bar {
  display: inline-flex;
  flex-wrap: wrap;
  gap: 4px;
  padding: 4px;
  border-radius: var(--radius-md);
  background: var(--bg-canvas);
  border: 1px solid var(--border-subtle);
  max-width: 100%;
}

.tab-bar {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  border-bottom: 1px solid var(--border-subtle);
  padding-bottom: var(--space-3);
}

.tab-btn {
  padding: 6px 12px;
  border-radius: var(--radius-sm);
  border: 1px solid transparent;
  background: transparent;
  color: var(--text-muted);
  font-family: var(--font-sans);
  font-size: 12px;
  font-weight: 500;
  cursor: pointer;
  transition: background var(--dur-fast) var(--ease-out), color var(--dur-fast) var(--ease-out), transform var(--dur-press) var(--ease-out);
  user-select: none;
}

@media (hover: hover) and (pointer: fine) {
  .tab-btn:hover {
    color: var(--text-primary);
    background: var(--bg-elevated);
  }
}

.tab-btn:active {
  transform: scale(0.97);
}

.tab-btn.active {
  color: var(--text-primary);
  background: var(--bg-elevated);
  border-color: var(--border-strong);
  box-shadow: var(--inner-highlight), var(--shadow-sm);
  font-weight: 600;
}

/* Sticky Filter Bar */
.sticky-filter-bar {
  position: sticky;
  top: 56px;
  z-index: 25;
  padding: 12px 16px;
  background: var(--bg-surface-glass);
  backdrop-filter: blur(12px);
  -webkit-backdrop-filter: blur(12px);
  border: 1px solid var(--border-strong);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow-card);
}

/* Inputs, Checkboxes & Sliders */
.field-label {
  display: block;
  font-family: var(--font-mono);
  font-size: 11px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--text-muted);
  margin-bottom: 6px;
}

.input,
.select,
.textarea {
  width: 100%;
  padding: 9px 12px;
  border-radius: var(--radius-sm);
  border: 1px solid var(--border-strong);
  background: var(--bg-canvas);
  color: var(--text-primary);
  font-family: var(--font-mono);
  font-variant-numeric: tabular-nums;
  font-size: 13px;
  line-height: 1.45;
  transition: border-color var(--dur-fast) var(--ease-out), box-shadow var(--dur-fast) var(--ease-out);
}

@media (hover: hover) and (pointer: fine) {
  .input:hover,
  .select:hover,
  .textarea:hover {
    border-color: rgba(255, 255, 255, 0.25);
  }
  [data-theme="light"] .input:hover,
  [data-theme="light"] .select:hover,
  [data-theme="light"] .textarea:hover {
    border-color: rgba(9, 9, 11, 0.28);
  }
}

.textarea {
  min-height: 112px;
  resize: vertical;
}

input[type="checkbox"] {
  accent-color: var(--accent);
  width: 16px;
  height: 16px;
  cursor: pointer;
  transition: transform var(--dur-press) var(--ease-out);
}

input[type="checkbox"]:active {
  transform: scale(0.92);
}

input[type="range"] {
  -webkit-appearance: none;
  appearance: none;
  height: 6px;
  border-radius: var(--radius-pill);
  background: var(--bg-elevated);
  border: 1px solid var(--border-subtle);
  cursor: pointer;
}

input[type="range"]::-webkit-slider-thumb {
  -webkit-appearance: none;
  appearance: none;
  width: 14px;
  height: 14px;
  border-radius: 50%;
  background: var(--accent);
  border: 2px solid var(--bg-surface);
  box-shadow: var(--shadow-sm);
  transition: transform var(--dur-press) var(--ease-out);
}

input[type="range"]:active::-webkit-slider-thumb {
  transform: scale(1.22);
}

input[type="range"]::-moz-range-thumb {
  width: 14px;
  height: 14px;
  border-radius: 50%;
  background: var(--accent);
  border: 2px solid var(--bg-surface);
  transition: transform var(--dur-press) var(--ease-out);
}

input[type="range"]:active::-moz-range-thumb {
  transform: scale(1.22);
}

/* Form Section Grouping */
.form-section {
  display: flex;
  flex-direction: column;
  gap: var(--space-3);
  padding-top: var(--space-4);
  border-top: 1px solid var(--border-subtle);
}

.form-section:first-child {
  padding-top: 0;
  border-top: none;
}

.form-section-title {
  font-family: var(--font-mono);
  font-size: 10px;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  color: var(--accent);
}

/* Wikipedia-style superscript citations */
.cite-ref {
  font-size: 9px;
  line-height: 0;
  vertical-align: super;
  margin-left: 2px;
}

.cite-link {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-width: 15px;
  padding: 1px 3px;
  border-radius: 3px;
  color: var(--accent);
  background: var(--accent-soft);
  font-family: var(--font-mono);
  font-size: 9px;
  font-weight: 600;
  line-height: 1.15;
  text-decoration: none;
  transition: background var(--dur-fast) var(--ease-out), color var(--dur-fast) var(--ease-out), transform var(--dur-press) var(--ease-out);
}

@media (hover: hover) and (pointer: fine) {
  .cite-link:hover {
    background: var(--accent);
    color: var(--accent-contrast);
  }
}

.cite-link:active {
  transform: scale(0.95);
}

.cite-target-pulse {
  border-color: var(--accent) !important;
  box-shadow: 0 0 0 2px var(--accent), var(--shadow-card) !important;
}

/* Dossier & Result Card Layout Primitives */
.dossier-card {
  padding: var(--space-6);
}

.dossier-layout {
  display: grid;
  grid-template-columns: 250px minmax(0, 1fr);
  gap: var(--space-6);
  align-items: start;
}

.dossier-profile-col {
  display: flex;
  flex-direction: column;
  gap: var(--space-3);
  padding-right: var(--space-5);
  border-right: 1px solid var(--border-subtle);
}

.dossier-avatar {
  width: 68px;
  height: 68px;
  border-radius: 50%;
  object-fit: cover;
  border: 1px solid var(--border-strong);
  box-shadow: var(--shadow-sm);
}

.dossier-facts-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(230px, 1fr));
  gap: var(--space-4) var(--space-6);
}

.dossier-fact-cell {
  padding-bottom: var(--space-3);
  border-bottom: 1px solid var(--border-subtle);
  min-width: 0;
}

.dossier-val-row {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: 8px;
  padding: 2px 0;
}

.dossier-val-row.is-low-conf {
  opacity: 0.72;
}

.dismiss-btn {
  border: none;
  background: transparent;
  color: var(--text-muted);
  font-family: var(--font-mono);
  font-size: 11px;
  cursor: pointer;
  padding: 1px 4px;
  border-radius: 4px;
  opacity: 0.65;
  flex-shrink: 0;
  transition: opacity var(--dur-fast) var(--ease-out), color var(--dur-fast) var(--ease-out), background var(--dur-fast) var(--ease-out);
}

@media (hover: hover) and (pointer: fine) {
  .dismiss-btn:hover {
    opacity: 1;
    color: var(--status-error);
    background: var(--status-error-soft);
  }
}

.result-card {
  display: flex;
  flex-direction: column;
  gap: var(--space-3);
  padding: 18px;
  height: 100%;
}

.result-kv-list {
  display: flex;
  flex-direction: column;
}

.result-kv-row {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  gap: 12px;
  padding: 6px 0;
  border-bottom: 1px solid var(--border-subtle);
  font-family: var(--font-mono);
  font-size: 11px;
  line-height: 1.45;
}

.result-kv-row:last-child {
  border-bottom: none;
}

/* Progress Bar (scaleX GPU-only, never width) */
.progress-track {
  width: 100%;
  height: 5px;
  border-radius: var(--radius-pill);
  background: var(--bg-elevated);
  overflow: hidden;
}

.progress-fill {
  width: 100%;
  height: 100%;
  background: var(--accent);
  transform-origin: left center;
  transform: scaleX(var(--progress, 0));
  transition: transform 220ms var(--ease-out);
}

/* Tables & Sticky Headers */
.data-table-wrap {
  width: 100%;
  max-height: 520px;
  overflow: auto;
  border: 1px solid var(--border-subtle);
  border-radius: var(--radius-md);
}

.data-table {
  width: 100%;
  border-collapse: collapse;
  font-family: var(--font-mono);
  font-variant-numeric: tabular-nums;
  font-size: 12px;
}

.data-table th,
.data-table td {
  padding: 10px 14px;
  text-align: left;
  border-bottom: 1px solid var(--border-subtle);
  vertical-align: top;
}

.data-table th {
  position: sticky;
  top: 0;
  z-index: 2;
  background: var(--bg-elevated);
  color: var(--text-muted);
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  border-bottom: 1px solid var(--border-strong);
}

/* Syntax-colored JSON block */
.json-pretty {
  margin: 0;
  padding: 10px 12px;
  border-radius: var(--radius-sm);
  background: var(--bg-canvas);
  border: 1px solid var(--border-subtle);
  font-family: var(--font-mono);
  font-size: 11px;
  line-height: 1.5;
  overflow-x: auto;
  white-space: pre-wrap;
  word-break: break-word;
}

.json-key { color: var(--accent); }
.json-str { color: var(--status-done); }
.json-num { color: var(--status-unavailable); }
.json-bool { color: var(--status-error); }

/* Timeline Year Grouping */
.tl-year-group {
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.tl-year-header {
  position: sticky;
  top: 0;
  z-index: 2;
  padding: 6px 10px;
  background: var(--bg-elevated);
  border: 1px solid var(--border-subtle);
  border-radius: var(--radius-sm);
  font-family: var(--font-mono);
  font-size: 11px;
  font-weight: 700;
  color: var(--accent);
}

.tl-row {
  display: flex;
  align-items: center;
  gap: 10px;
  width: 100%;
  padding: 8px 12px;
  border-radius: var(--radius-sm);
  border: 1px solid var(--border-subtle);
  background: var(--bg-surface);
  color: var(--text-primary);
  text-align: left;
  cursor: pointer;
  transition: background var(--dur-fast) var(--ease-out), border-color var(--dur-fast) var(--ease-out), transform var(--dur-press) var(--ease-out);
}

@media (hover: hover) and (pointer: fine) {
  .tl-row:hover {
    background: var(--bg-elevated);
    border-color: var(--border-strong);
  }
}

.tl-row:active {
  transform: scale(0.99);
}

/* Knowledge Graph Layout & Legend */
.graph-layout {
  display: grid;
  grid-template-columns: minmax(0, 1fr) 280px;
  gap: var(--space-4);
  align-items: stretch;
}

.graph-layout.no-detail {
  grid-template-columns: minmax(0, 1fr);
}

.graph-legend {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 10px 14px;
  padding: 10px 14px;
  border-top: 1px solid var(--border-subtle);
  background: var(--bg-surface);
  font-family: var(--font-mono);
  font-size: 11px;
  color: var(--text-secondary);
}

.graph-legend-item {
  display: inline-flex;
  align-items: center;
  gap: 6px;
}

.graph-legend-dot {
  width: 9px;
  height: 9px;
  border-radius: 50%;
  display: inline-block;
}

/* SSE Hit Stream */
.sse-hit-row {
  font-family: var(--font-mono);
  font-size: 12px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  flex-wrap: wrap;
  gap: 8px;
  padding: 7px 10px;
  background: var(--bg-elevated);
  border: 1px solid var(--border-subtle);
  border-radius: var(--radius-sm);
}

.sse-hit-enter {
  opacity: 0;
  transform: translateY(4px);
  transition: opacity 150ms var(--ease-out), transform 150ms var(--ease-out);
}

.sse-hit-enter.is-shown {
  opacity: 1;
  transform: translateY(0);
}

/* Shared Origin-Aware Tooltip Popover */
.tooltip-popover {
  position: fixed;
  z-index: 95;
  max-width: 280px;
  padding: 6px 10px;
  border-radius: var(--radius-sm);
  background: var(--bg-elevated);
  color: var(--text-primary);
  font-family: var(--font-mono);
  font-size: 11px;
  line-height: 1.4;
  box-shadow: var(--shadow-popover);
  pointer-events: none;
  transform-origin: var(--transform-origin, top center);
  opacity: 0;
  transform: scale(0.95);
  transition: opacity var(--dur-tooltip) var(--ease-out), transform var(--dur-tooltip) var(--ease-out);
}

.tooltip-popover.open {
  opacity: 1;
  transform: scale(1);
}

.tooltip-popover[data-instant="true"] {
  transition-duration: 0ms;
}

/* Drawer & Backdrop */
.drawer-backdrop {
  position: fixed;
  inset: 0;
  z-index: 55;
  background: rgba(0, 0, 0, 0.5);
  backdrop-filter: blur(3px);
  opacity: 0;
  pointer-events: none;
  transition: opacity var(--dur-med) var(--ease-out);
}

.drawer-backdrop.open {
  opacity: 1;
  pointer-events: auto;
}

.drawer {
  position: fixed;
  top: 0;
  right: 0;
  bottom: 0;
  width: min(560px, 100vw);
  background: var(--bg-surface);
  border-left: 1px solid var(--border-strong);
  box-shadow: var(--shadow-drawer);
  z-index: 60;
  display: flex;
  flex-direction: column;
  transform: translateX(100%);
  transition: transform var(--dur-drawer) var(--ease-drawer);
}

.drawer.open {
  transform: translateX(0);
}

.drawer-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: var(--space-3);
  padding: var(--space-4) var(--space-6);
  background: var(--bg-elevated);
  border-bottom: 1px solid var(--border-strong);
  flex-shrink: 0;
}

.drawer-body {
  flex: 1;
  min-height: 0;
  overflow-y: auto;
  padding: var(--space-5) var(--space-6);
  display: flex;
  flex-direction: column;
  gap: var(--space-4);
}

.drawer-body > * {
  flex-shrink: 0;
}

/* Centered Modal */
.modal-backdrop {
  position: fixed;
  inset: 0;
  z-index: 70;
  background: rgba(0, 0, 0, 0.72);
  backdrop-filter: blur(5px);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 24px;
  opacity: 0;
  pointer-events: none;
  transition: opacity 180ms var(--ease-out);
}

.modal-backdrop.open {
  opacity: 1;
  pointer-events: auto;
}

.modal-dialog {
  width: min(1020px, 100%);
  max-height: 88vh;
  display: flex;
  flex-direction: column;
  padding: var(--space-5);
  overflow: hidden;
  transform-origin: center;
  transform: scale(0.95);
  opacity: 0;
  transition: transform 200ms var(--ease-out), opacity 180ms var(--ease-out);
}

.modal-dialog.inspector-modal {
  width: min(780px, 100%);
  max-height: 86vh;
  padding: 0;
  border: 1px solid var(--border-strong);
}

.modal-backdrop.open .modal-dialog {
  transform: scale(1);
  opacity: 1;
}

/* Keyboard Shortcut Chip */
kbd {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-width: 22px;
  padding: 2px 6px;
  border-radius: 4px;
  border: 1px solid var(--border-strong);
  background: var(--bg-elevated);
  font-family: var(--font-mono);
  font-size: 11px;
  font-weight: 600;
  color: var(--text-primary);
}

/* Sonner-style Toast Stack */
.toast-stack {
  position: fixed;
  right: var(--space-5);
  bottom: var(--space-5);
  z-index: 80;
  display: flex;
  flex-direction: column;
  gap: var(--space-2);
  width: min(380px, calc(100vw - 32px));
  pointer-events: none;
}

.toast {
  pointer-events: auto;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: var(--space-3);
  padding: 12px 14px;
  border-radius: var(--radius-md);
  border: 1px solid var(--border-strong);
  background: var(--bg-surface);
  color: var(--text-primary);
  font-family: var(--font-mono);
  font-size: 12px;
  box-shadow: var(--shadow-card);
  opacity: 0;
  transform: translateY(12px) scale(0.96);
  transition: transform 240ms var(--ease-out), opacity 200ms var(--ease-out);
  touch-action: pan-x;
}

.toast.open {
  opacity: 1;
  transform: translateY(0) scale(1);
}

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

/* Empty / Error States */
.state-panel {
  border: 1px dashed var(--border-strong);
  border-radius: var(--radius-md);
  padding: var(--space-5);
  text-align: center;
  color: var(--text-muted);
  font-family: var(--font-mono);
  font-size: 12px;
}

.state-panel-error {
  border-color: var(--status-error-border);
  background: var(--status-error-soft);
  color: var(--status-error);
}

/* Responsive Dossier, Graph & Touch Targets */
@media (max-width: 960px) {
  .graph-layout {
    grid-template-columns: 1fr;
  }
}

@media (max-width: 820px) {
  .dossier-layout {
    grid-template-columns: 1fr;
    gap: var(--space-4);
  }
  .dossier-profile-col {
    padding-right: 0;
    padding-bottom: var(--space-4);
    border-right: none;
    border-bottom: 1px solid var(--border-subtle);
  }
  .sticky-filter-bar {
    position: static;
  }
}

@media (pointer: coarse) {
  .btn,
  .tab-btn,
  .input,
  .select,
  .tl-row {
    min-height: 44px;
  }
  .cite-link {
    min-width: 28px;
    min-height: 28px;
    padding: 4px 6px;
  }
}
