:root {
  --white: #FFFFFF;
  --red: #D52B1E;
  --red-light: #FDE8E6;
  --bg: #F8F9FA;
  --surface: #FFFFFF;
  --text: #1A1A2E;
  --text-secondary: #6B7280;
  --border: #E5E7EB;
  --shadow-sm: 0 1px 2px rgba(0,0,0,0.05);
  --shadow: 0 1px 3px rgba(0,0,0,0.1), 0 1px 2px rgba(0,0,0,0.06);
  --radius: 8px;
  --radius-sm: 6px;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", sans-serif;
  color: var(--text);
  background: var(--bg);
  font-size: 14px;
  line-height: 1.5;
}

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

/* HEADER */
header {
  background: var(--red);
  color: var(--white);
  padding: 10px 24px;
  box-shadow: 0 2px 6px rgba(0,0,0,0.15);
  display: flex;
  align-items: center;
  justify-content: space-between;
}
header h1 { font-size: 1.15rem; font-weight: 700; }
.btn-header {
  background: rgba(255,255,255,0.15) !important;
  border-color: rgba(255,255,255,0.3) !important;
  color: var(--white) !important;
}
.btn-header:hover {
  background: rgba(255,255,255,0.25);
  border-color: rgba(255,255,255,0.5);
}
header .badge {
  display: inline-block;
  background: rgba(255,255,255,0.2);
  border-radius: 12px;
  padding: 2px 10px;
  font-size: 0.8rem;
  margin-left: 8px;
  font-weight: 500;
}
header .last-scrape {
  font-size: 0.72rem;
  font-weight: 400;
  opacity: 0.7;
  margin-left: 12px;
}

/* MAIN */
main { padding: 20px 24px; }

/* STATS BAR */
#stats-bar { display: flex; gap: 14px; margin-bottom: 16px; flex-wrap: wrap; }
.stat-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 12px 18px;
  box-shadow: var(--shadow-sm);
  min-width: 90px;
}
.stat-card .stat-value { font-size: 1.35rem; font-weight: 700; color: var(--red); line-height: 1.2; }
.stat-card .stat-label { font-size: 0.72rem; color: var(--text-secondary); text-transform: uppercase; letter-spacing: 0.04em; margin-top: 2px; }

/* FILTERS */
#filter-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 16px 20px;
  box-shadow: var(--shadow-sm);
  margin-bottom: 16px;
}
#filter-row {
  display: flex;
  flex-direction: row;
  gap: 20px;
}
.filter-left {
  flex: 1;
  display: flex;
  flex-direction: column;
  gap: 12px;
  min-width: 0;
}
.filter-right {
  display: flex;
  align-items: flex-end;
  gap: 8px;
  padding-bottom: 2px;
}
.filter-section {
  display: flex;
  align-items: center;
  gap: 8px;
  flex-wrap: wrap;
}
.filter-label {
  font-size: 0.75rem;
  font-weight: 600;
  color: var(--text-secondary);
  text-transform: uppercase;
  letter-spacing: 0.04em;
  white-space: nowrap;
}
.filter-range-sep {
  font-size: 0.82rem;
  color: var(--text-secondary);
}

input[type="date"] {
  padding: 6px 10px; border: 1px solid var(--border); border-radius: var(--radius-sm);
  font-size: 0.84rem; background: var(--white);
}
input[type="date"]:focus { outline: none; border-color: var(--red); box-shadow: 0 0 0 3px var(--red-light); }

.filter-check {
  display: flex; align-items: center; gap: 4px; font-size: 0.8rem; cursor: pointer;
  user-select: none; padding: 5px 10px; border: 1px solid var(--border);
  border-radius: var(--radius-sm); transition: all 0.15s;
}
.filter-check:hover { border-color: var(--red); background: var(--red-light); }
.filter-check.checked { border-color: var(--red); background: var(--red); color: var(--white); }

select {
  padding: 6px 10px; border: 1px solid var(--border); border-radius: var(--radius-sm);
  font-size: 0.84rem; background: var(--white); cursor: pointer;
}
select:focus { outline: none; border-color: var(--red); }

button { cursor: pointer; font-size: 0.82rem; font-weight: 500; }
.btn {
  padding: 6px 14px; border: 1px solid var(--border); border-radius: var(--radius-sm);
  background: var(--white); transition: all 0.15s; white-space: nowrap;
}
.btn:hover { background: var(--bg); border-color: #ccc; }
#clear-btn { color: var(--red); border-color: var(--red); background: none; }
#clear-btn:hover { color: var(--white); background: var(--red); }

/* COLUMN POPUP */
#cols-popup {
  position: absolute; background: var(--surface); border: 1px solid var(--border);
  border-radius: var(--radius); box-shadow: 0 4px 12px rgba(0,0,0,0.12);
  padding: 12px; z-index: 50; display: none; flex-direction: column; gap: 6px; min-width: 200px;
}
#cols-popup.show { display: flex; }
#cols-popup label { display: flex; align-items: center; gap: 8px; font-size: 0.82rem; cursor: pointer; }
#cols-popup label input { accent-color: var(--red); }

/* TABLE WRAPPER */
#table-wrap {
  flex: 1; min-width: 0;
  background: var(--surface); border: 1px solid var(--border);
  border-radius: var(--radius); box-shadow: var(--shadow-sm); overflow: hidden;
}

/* Tabulator overrides — match red theme */
.tabulator { font-size: 0.84rem; border: none; background: transparent; }
.tabulator .tabulator-header {
  background: var(--red) !important;
  border-bottom: 2px solid #B52217;
}
.tabulator .tabulator-header * {
  color: var(--white) !important;
  background: transparent !important;
}
.tabulator .tabulator-header .tabulator-col {
  border-right: 1px solid rgba(255,255,255,0.2);
  font-weight: 600; font-size: 0.76rem; text-transform: uppercase; letter-spacing: 0.03em;
}
.tabulator .tabulator-header .tabulator-col:hover { background: rgba(255,255,255,0.1) !important; }
.tabulator .tabulator-header .tabulator-col .tabulator-col-content { padding: 10px 12px; }
.tabulator .tabulator-header .tabulator-arrow { border-bottom-color: var(--white) !important; }
.tabulator .tabulator-tableholder { border: none; }
.tabulator .tabulator-row { border-bottom: 1px solid var(--border); }
.tabulator .tabulator-row.tabulator-row-even { background: #FAFBFC; }
.tabulator .tabulator-row:hover { background: var(--red-light); cursor: pointer; }
.tabulator .tabulator-row.tabulator-selected { background: rgba(213,43,30,0.12); }
.tabulator .tabulator-cell { padding: 8px 12px; }
.tabulator .tabulator-footer { border-top: 1px solid var(--border); color: var(--text-secondary); background: transparent; font-size: 0.78rem; }
.tabulator .tabulator-footer .tabulator-page.active { background: var(--red); border-color: var(--red); color: var(--white); }
.tabulator .tabulator-loader { display: none; }

/* LOADING / EMPTY / ERROR */
#loading-state, #empty-state, #error-state { text-align: center; padding: 60px 20px; color: var(--text-secondary); }
#loading-state.hidden, #empty-state.hidden, #error-state.hidden { display: none; }
#error-state { color: var(--red); }
#error-state code { background: var(--bg); padding: 2px 6px; border-radius: 3px; font-size: 0.85rem; }
.spinner {
  display: inline-block; width: 32px; height: 32px; border: 3px solid var(--border);
  border-top-color: var(--red); border-radius: 50%; animation: spin 0.8s linear infinite; margin-bottom: 10px;
}
@keyframes spin { to { transform: rotate(360deg); } }

/* SIDE PANEL */
#content { display: flex; gap: 0; }
#detail-panel {
  width: 420px; min-width: 420px; border-left: 1px solid var(--border);
  background: var(--surface); overflow-y: auto; max-height: calc(100vh - 160px);
  position: sticky; top: 76px; box-shadow: -2px 0 8px rgba(0,0,0,0.05);
}
#detail-panel.hidden { display: none; }
#panel-header { display: flex; align-items: center; justify-content: space-between; padding: 14px 18px; border-bottom: 1px solid var(--border); position: sticky; top: 0; background: var(--surface); z-index: 5; }
#panel-header h3 { font-size: 1rem; }
#close-panel { border: none; background: none; font-size: 1.3rem; cursor: pointer; color: var(--text-secondary); width: 32px; height: 32px; border-radius: 50%; display: flex; align-items: center; justify-content: center; }
#close-panel:hover { background: var(--bg); color: var(--red); }
#detail-body { padding: 14px 18px; }
#detail-body .section-title { font-size: 0.7rem; font-weight: 700; color: var(--red); text-transform: uppercase; letter-spacing: 0.05em; margin: 14px 0 6px; padding-bottom: 4px; border-bottom: 1px solid var(--border); }
#detail-body .section-title:first-child { margin-top: 0; }
#detail-body .field-table { width: 100%; font-size: 0.82rem; border-collapse: collapse; }
#detail-body .field-table td { padding: 5px 0; border: none; background: none !important; }
#detail-body .field-table td:first-child { color: var(--text-secondary); font-weight: 400; width: 50%; }
#detail-body .field-table td:last-child { text-align: right; font-weight: 500; }
#detail-body .source-thread { font: 0.78rem/1.5 "SF Mono", "Fira Code", monospace; background: #F3F4F6; padding: 12px; border-radius: var(--radius-sm); white-space: pre-wrap; word-break: break-word; max-height: 350px; overflow-y: auto; color: var(--text); }
#detail-body .notes-text { font-size: 0.85rem; line-height: 1.5; color: var(--text); background: #FFFBEB; padding: 10px 12px; border-radius: var(--radius-sm); border: 1px solid #FDE68A; }
#detail-body .id-tag { color: var(--text-secondary); font-size: 0.78rem; margin-bottom: 2px; }

/* MODAL */
.modal-overlay {
  position: fixed; inset: 0; background: rgba(0,0,0,0.4);
  display: flex; align-items: center; justify-content: center;
  z-index: 300;
}
.modal-overlay.hidden { display: none; }
.modal {
  background: var(--surface); border-radius: var(--radius);
  box-shadow: 0 8px 32px rgba(0,0,0,0.2); max-width: 480px; width: 90%;
  max-height: 80vh; overflow-y: auto;
}
.modal-header {
  display: flex; align-items: center; justify-content: space-between;
  padding: 16px 20px; border-bottom: 1px solid var(--border);
}
.modal-header h2 { font-size: 1.1rem; font-weight: 600; }
.modal-header button {
  border: none; background: none; font-size: 1.2rem; cursor: pointer;
  color: var(--text-secondary); width: 32px; height: 32px; border-radius: 50%;
  display: flex; align-items: center; justify-content: center;
}
.modal-header button:hover { background: var(--bg); color: var(--red); }
.modal-body { padding: 20px; font-size: 0.9rem; line-height: 1.6; }
.modal-body p { margin-bottom: 12px; }
.modal-body a { color: var(--red); text-decoration: none; }
.modal-body a:hover { text-decoration: underline; }

/* FOOTER */
footer {
  text-align: center;
  padding: 20px 24px;
}
footer a {
  font-size: 0.72rem;
  color: var(--text-secondary);
  text-decoration: none;
}
footer a:hover { color: var(--red); }

@media (max-width: 960px) {
  #detail-panel { position: fixed; top: 0; right: 0; width: 100%; max-width: 420px; height: 100vh; max-height: 100vh; z-index: 200; }
}

@media (max-width: 780px) {
  #filter-row {
    flex-direction: column;
    gap: 12px;
  }
  .filter-right {
    align-items: flex-start;
    padding-bottom: 0;
  }
}
