/* =========================================================
   Spyglass — Web Frontend Styles
   ========================================================= */

/* ---------- Design tokens ---------- */
:root {
  --bg-base:        #0d1117;
  --bg-surface:     #161b22;
  --bg-elevated:    #1c2128;
  --bg-hover:       #21262d;

  --border:         #30363d;
  --border-subtle:  #21262d;

  --text-primary:   #e6edf3;
  --text-secondary: #8b949e;
  --text-muted:     #484f58;
  --text-link:      #58a6ff;

  --accent:         #388bfd;
  --accent-muted:   rgba(56, 139, 253, 0.15);
  --accent-glow:    rgba(56, 139, 253, 0.25);

  /* Row classification colours */
  --row-green:      rgba(35, 134, 54,  0.25);
  --row-green-b:    rgba(35, 134, 54,  0.55);
  --row-yellow:     rgba(187, 128, 9,  0.25);
  --row-yellow-b:   rgba(187, 128, 9,  0.55);
  --row-red:        rgba(248, 81,  73, 0.20);
  --row-red-b:      rgba(248, 81,  73, 0.50);
  --cell-red:       rgba(248, 81,  73, 0.30);
  --cell-red-text:  #ff7b72;

  /* Sizing */
  --header-h:       64px;
  --stats-h:        48px;
  --legend-h:       36px;
  --toolbar-h:      52px;
  --footer-h:       44px;
  --radius:         8px;
  --radius-sm:      4px;
}

/* ---------- Reset & base ---------- */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

html, body {
  height: 100%;
  font-family: 'Inter', system-ui, -apple-system, sans-serif;
  font-size: 14px;
  line-height: 1.5;
  background: var(--bg-base);
  color: var(--text-primary);
  overflow-x: hidden;
}

a { color: var(--text-link); text-decoration: none; }
a:hover { text-decoration: underline; }

/* =========================================================
   Header
   ========================================================= */
.site-header {
  position: sticky;
  top: 0;
  z-index: 100;
  height: var(--header-h);
  background: var(--bg-surface);
  border-bottom: 1px solid var(--border);
  backdrop-filter: blur(12px);
  -webkit-backdrop-filter: blur(12px);
}

.header-inner {
  display: flex;
  align-items: center;
  justify-content: space-between;
  height: 100%;
  padding: 0 20px;
  gap: 16px;
}

.brand {
  display: flex;
  align-items: center;
  gap: 12px;
  flex-shrink: 0;
}

.brand-icon { font-size: 28px; line-height: 1; }

.brand-title {
  font-size: 20px;
  font-weight: 700;
  letter-spacing: -0.5px;
  background: linear-gradient(135deg, #e6edf3 0%, #58a6ff 100%);
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  background-clip: text;
  margin: 0;
}

.brand-subtitle {
  font-size: 11px;
  color: var(--text-secondary);
  font-weight: 400;
  letter-spacing: 0.3px;
}

.header-meta {
  display: flex;
  align-items: center;
  gap: 8px;
  flex-wrap: wrap;
  justify-content: flex-end;
}

.meta-skeleton {
  width: 260px;
  height: 18px;
  border-radius: var(--radius-sm);
  background: linear-gradient(90deg, var(--bg-elevated) 25%, var(--bg-hover) 50%, var(--bg-elevated) 75%);
  background-size: 400% 100%;
  animation: shimmer 1.5s infinite;
}

@keyframes shimmer {
  0%   { background-position: 100% 0; }
  100% { background-position: -100% 0; }
}

/* =========================================================
   Stats bar
   ========================================================= */
.stats-bar {
  display: flex;
  align-items: center;
  height: var(--stats-h);
  background: var(--bg-elevated);
  border-bottom: 1px solid var(--border-subtle);
  padding: 0 20px;
  gap: 0;
  overflow-x: auto;
}

.stat-item {
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  padding: 0 16px;
  flex-shrink: 0;
}

.stat-label {
  font-size: 10px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.6px;
  color: var(--text-secondary);
}

.stat-value {
  font-size: 14px;
  font-weight: 600;
  color: var(--text-primary);
  font-family: 'JetBrains Mono', monospace;
}

.stat-divider {
  width: 1px;
  height: 28px;
  background: var(--border);
  flex-shrink: 0;
}

/* =========================================================
   Legend bar
   ========================================================= */
.legend-bar {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  gap: 4px 14px;
  padding: 6px 20px;
  background: var(--bg-base);
  border-bottom: 1px solid var(--border-subtle);
  font-size: 12px;
  color: var(--text-secondary);
}

.legend-label {
  font-weight: 600;
  color: var(--text-secondary);
  white-space: nowrap;
}

.legend-item {
  display: flex;
  align-items: center;
  gap: 5px;
  white-space: nowrap;
}

.legend-swatch {
  display: inline-block;
  width: 12px;
  height: 12px;
  border-radius: 2px;
  border: 1px solid rgba(255,255,255,0.1);
}

.legend-green  .legend-swatch { background: var(--row-green-b); }
.legend-yellow .legend-swatch { background: var(--row-yellow-b); }
.legend-red    .legend-swatch { background: var(--row-red-b); }
.legend-normal .legend-swatch { background: var(--bg-elevated); }

.legend-item code {
  background: var(--bg-elevated);
  border: 1px solid var(--border);
  border-radius: 3px;
  padding: 0 4px;
  font-size: 11px;
  font-family: 'JetBrains Mono', monospace;
  color: var(--text-primary);
}

.legend-sep { color: var(--text-muted); }

/* =========================================================
   Toolbar
   ========================================================= */
.toolbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  height: var(--toolbar-h);
  padding: 0 16px;
  background: var(--bg-surface);
  border-bottom: 1px solid var(--border);
  gap: 12px;
  flex-wrap: wrap;
}

.toolbar-left, .toolbar-right {
  display: flex;
  align-items: center;
  gap: 8px;
  flex-wrap: wrap;
}

/* Search */
.search-wrapper {
  position: relative;
  display: flex;
  align-items: center;
}

.search-icon {
  position: absolute;
  left: 10px;
  width: 15px;
  height: 15px;
  color: var(--text-secondary);
  pointer-events: none;
}

.search-input {
  width: 280px;
  padding: 7px 10px 7px 32px;
  background: var(--bg-base);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  color: var(--text-primary);
  font-family: inherit;
  font-size: 13px;
  outline: none;
  transition: border-color 0.15s, box-shadow 0.15s;
}

.search-input::placeholder { color: var(--text-muted); }

.search-input:focus {
  border-color: var(--accent);
  box-shadow: 0 0 0 3px var(--accent-glow);
}

/* Toggle group */
.toggle-group {
  display: flex;
  align-items: center;
  gap: 0;
  background: var(--bg-base);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  overflow: hidden;
}

.toggle-label {
  padding: 0 8px;
  font-size: 12px;
  color: var(--text-secondary);
  white-space: nowrap;
  border-right: 1px solid var(--border);
  line-height: 30px;
}

.toggle-btn {
  padding: 5px 12px;
  background: transparent;
  border: none;
  color: var(--text-secondary);
  font-family: inherit;
  font-size: 13px;
  cursor: pointer;
  transition: background 0.15s, color 0.15s;
}

.toggle-btn:not(:last-child) { border-right: 1px solid var(--border); }

.toggle-btn.active, .toggle-btn[aria-pressed="true"] {
  background: var(--accent-muted);
  color: var(--accent);
  font-weight: 500;
}

.toggle-btn:hover:not(.active) { background: var(--bg-hover); color: var(--text-primary); }

/* Page size select */
.page-size-select {
  padding: 6px 8px;
  background: var(--bg-base);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  color: var(--text-primary);
  font-family: inherit;
  font-size: 13px;
  outline: none;
  cursor: pointer;
  transition: border-color 0.15s;
}

.page-size-select:focus { border-color: var(--accent); }

/* Action buttons */
.action-btn {
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 6px 12px;
  background: var(--bg-elevated);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  color: var(--text-primary);
  font-family: inherit;
  font-size: 13px;
  cursor: pointer;
  transition: background 0.15s, border-color 0.15s, box-shadow 0.15s;
  white-space: nowrap;
}

.action-btn svg { width: 14px; height: 14px; flex-shrink: 0; }

.action-btn:hover {
  background: var(--bg-hover);
  border-color: var(--accent);
  box-shadow: 0 0 0 3px var(--accent-glow);
}

/* =========================================================
   AG Grid container
   ========================================================= */
.spyglass-grid {
  flex: 1;
  width: 100%;
  height: calc(
    100vh
    - var(--header-h)
    - var(--stats-h)
    - var(--legend-h)
    - var(--toolbar-h)
    - var(--footer-h)
    - 37px /* legend bar padding */
  );
  min-height: 400px;
}

/* ---- AG Grid theme overrides ---- */
.ag-theme-quartz-dark {
  --ag-background-color:              var(--bg-base);
  --ag-foreground-color:              var(--text-primary);
  --ag-header-background-color:       var(--bg-surface);
  --ag-header-foreground-color:       var(--text-primary);
  --ag-border-color:                  var(--border);
  --ag-row-border-color:              var(--border-subtle);
  --ag-odd-row-background-color:      var(--bg-base);
  --ag-row-hover-color:               var(--bg-hover);
  --ag-selected-row-background-color: var(--accent-muted);
  --ag-font-family:                   'Inter', system-ui, sans-serif;
  --ag-font-size:                     13px;
  --ag-cell-horizontal-padding:       10px;
  --ag-header-height:                 42px;
  --ag-row-height:                    34px;
  --ag-filter-tool-panel-header-height: 28px;
  --ag-icon-font-color:               var(--text-secondary);
  --ag-input-border-color:            var(--border);
  --ag-input-focus-border-color:      var(--accent);
  --ag-checkbox-checked-color:        var(--accent);
  --ag-range-selection-border-color:  var(--accent);
  --ag-panel-background-color:        var(--bg-elevated);
  --ag-control-panel-background-color: var(--bg-surface);
  --ag-side-button-selected-background-color: var(--accent-muted);
}

/* Header cells */
.ag-theme-quartz-dark .ag-header-cell {
  font-weight: 600;
  font-size: 12px;
  letter-spacing: 0.3px;
  text-transform: uppercase;
  color: var(--text-secondary);
  border-right: 1px solid var(--border-subtle);
}

/* Monospace cells (times, numbers) */
.ag-theme-quartz-dark .cell-mono {
  font-family: 'JetBrains Mono', monospace;
  font-size: 12px;
}

/* Row classification colours */
.ag-theme-quartz-dark .row-green {
  background-color: var(--row-green) !important;
}
.ag-theme-quartz-dark .row-green:hover {
  background-color: var(--row-green-b) !important;
}
.ag-theme-quartz-dark .row-yellow {
  background-color: var(--row-yellow) !important;
}
.ag-theme-quartz-dark .row-yellow:hover {
  background-color: var(--row-yellow-b) !important;
}
.ag-theme-quartz-dark .row-red {
  background-color: var(--row-red) !important;
}
.ag-theme-quartz-dark .row-red:hover {
  background-color: var(--row-red-b) !important;
}

/* Red cell for zero endorsements */
.ag-theme-quartz-dark .cell-no-endos {
  background-color: var(--cell-red) !important;
  color: var(--cell-red-text) !important;
  font-weight: 600;
}

/* Pagination bar */
.ag-theme-quartz-dark .ag-paging-panel {
  background: var(--bg-surface);
  border-top: 1px solid var(--border);
  color: var(--text-secondary);
  font-size: 12px;
}

/* =========================================================
   Loading overlay
   ========================================================= */
.loading-overlay {
  position: fixed;
  inset: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  background: var(--bg-base);
  z-index: 200;
  gap: 16px;
  color: var(--text-secondary);
  font-size: 15px;
}

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

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

/* =========================================================
   Error overlay
   ========================================================= */
.error-overlay {
  position: fixed;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  background: rgba(13, 17, 23, 0.8);
  z-index: 200;
  backdrop-filter: blur(4px);
}

.error-box {
  background: var(--bg-surface);
  border: 1px solid #f8514940;
  border-radius: var(--radius);
  padding: 32px 40px;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 14px;
  text-align: center;
  max-width: 360px;
  box-shadow: 0 8px 32px rgba(0,0,0,0.5);
}

.error-icon { font-size: 36px; }

/* =========================================================
   Footer
   ========================================================= */
.site-footer {
  height: var(--footer-h);
  display: flex;
  align-items: center;
  justify-content: center;
  background: var(--bg-surface);
  border-top: 1px solid var(--border);
  color: var(--text-muted);
  font-size: 12px;
  text-align: center;
  padding: 0 16px;
}

.site-footer a { color: var(--text-secondary); }
.site-footer a:hover { color: var(--text-link); }

/* =========================================================
   Responsive tweaks
   ========================================================= */
@media (max-width: 768px) {
  .search-input { width: 180px; }
  .stat-item { padding: 0 10px; }
  .toolbar { height: auto; padding: 8px 12px; }
  .legend-bar { font-size: 11px; gap: 4px 8px; }
  .brand-subtitle { display: none; }
}
