/* --- Reset & Base --- */
*,
*::before,
*::after {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

:root {
  --bg: #0f1117;
  --surface: #1a1d27;
  --surface-2: #242836;
  --border: #2e3348;
  --text: #e4e6f0;
  --text-muted: #8b90a5;
  --primary: #013F7C;
  --primary-hover: #0256a8;
  --success: #22c55e;
  --success-hover: #16a34a;
  --danger: #ef4444;
  --radius: 10px;
  --radius-sm: 6px;
}

[data-theme="light"] {
  --bg: #f0f2f5;
  --surface: #ffffff;
  --surface-2: #f5f6f8;
  --border: #d1d5db;
  --text: #1f2937;
  --text-muted: #6b7280;
  --primary: #013F7C;
  --primary-hover: #0256a8;
  --success: #16a34a;
  --success-hover: #15803d;
  --danger: #dc2626;
}

body {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Oxygen, sans-serif;
  background: var(--bg);
  color: var(--text);
  min-height: 100vh;
  line-height: 1.5;
}

.hidden {
  display: none !important;
}

/* --- Login Screen --- */
.screen {
  min-height: 100vh;
}

#login-screen {
  display: flex;
  align-items: center;
  justify-content: center;
  background: linear-gradient(135deg, #0f1117 0%, #1e1b4b 100%);
}

[data-theme="light"] #login-screen {
  background: linear-gradient(135deg, #e0e7ff 0%, #f0f2f5 100%);
}

.login-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 16px;
  padding: 48px 40px;
  width: 100%;
  max-width: 400px;
  text-align: center;
  box-shadow: 0 25px 50px -12px rgba(0, 0, 0, 0.5);
}

.login-logo {
  margin-bottom: 16px;
}

.logo {
  height: 60px;
  width: auto;
}

.login-card h1 {
  font-size: 24px;
  font-weight: 700;
  margin-bottom: 4px;
}

.subtitle {
  color: var(--text-muted);
  font-size: 14px;
  margin-bottom: 28px;
}

.error-text {
  color: var(--danger);
  font-size: 13px;
  margin-top: 12px;
}

/* --- Buttons --- */
.btn {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 10px 20px;
  border: none;
  border-radius: var(--radius-sm);
  font-size: 14px;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.15s ease;
}

.btn-full {
  width: 100%;
  justify-content: center;
}

.btn-primary {
  background: var(--primary);
  color: #fff;
}
.btn-primary:hover {
  background: var(--primary-hover);
}
.btn-primary:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}

.btn-secondary {
  background: var(--surface-2);
  color: var(--text);
  border: 1px solid var(--border);
}
.btn-secondary:hover {
  background: var(--border);
}

.btn-success {
  background: var(--success);
  color: #fff;
}
.btn-success:hover {
  background: var(--success-hover);
}
.btn-success:disabled {
  opacity: 0.4;
  cursor: not-allowed;
}

.btn-ghost {
  background: transparent;
  color: var(--text-muted);
  padding: 8px 14px;
}
.btn-ghost:hover {
  color: var(--text);
  background: var(--surface-2);
}

/* --- Header --- */
header {
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  padding: 0 24px;
  position: sticky;
  top: 0;
  z-index: 100;
}

.header-inner {
  max-width: 1400px;
  margin: 0 auto;
  display: flex;
  align-items: center;
  justify-content: space-between;
  height: 60px;
}

.header-left {
  display: flex;
  align-items: center;
  gap: 12px;
}

.header-logo {
  height: 36px;
  width: auto;
}

.header-right {
  display: flex;
  align-items: center;
  gap: 8px;
}

#theme-toggle {
  font-size: 20px;
  line-height: 1;
}

header h1 {
  font-size: 18px;
  font-weight: 700;
}

/* --- Main --- */
main {
  max-width: 1400px;
  margin: 0 auto;
  padding: 24px;
}

/* --- Search Panel --- */
.search-panel {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 28px;
  margin-bottom: 24px;
}

.search-panel-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 20px;
}

.search-panel-header h2 {
  font-size: 18px;
  font-weight: 700;
}

/* --- Condition Rows --- */
#conditions-container {
  display: flex;
  flex-direction: column;
  gap: 12px;
  margin-bottom: 16px;
}

.condition-row {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 12px 16px;
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  flex-wrap: wrap;
}

.condition-badge {
  /* kept for any legacy references */
  display: none;
}

/* --- Per-condition AND/OR segmented toggle --- */
.join-toggle {
  display: inline-flex;
  border: 1px solid var(--border);
  border-radius: 6px;
  overflow: hidden;
  flex-shrink: 0;
  user-select: none;
}

.join-btn {
  padding: 4px 10px;
  font-size: 11px;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.4px;
  border: none;
  background: var(--surface-2);
  color: var(--text-muted);
  cursor: pointer;
  transition: background 0.12s, color 0.12s;
  line-height: 1.4;
}

.join-btn:first-child {
  border-right: 1px solid var(--border);
}

.join-btn.active[data-value="AND"] {
  background: var(--primary);
  color: #fff;
}

.join-btn.active[data-value="OR"] {
  background: var(--primary);
  color: #fff;
}

.join-btn:hover:not(.active) {
  background: var(--border);
  color: var(--text);
}

.condition-type-label {
  font-size: 13px;
  font-weight: 600;
  color: var(--text);
  min-width: 110px;
}

.condition-operator {
  padding: 6px 10px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  color: var(--text);
  font-size: 13px;
  cursor: pointer;
  min-width: 140px;
}

.condition-value-wrap {
  flex: 1;
  display: flex;
  align-items: center;
  gap: 8px;
  min-width: 200px;
}

.condition-value-wrap input[type="text"],
.condition-value-wrap input[type="date"],
.condition-value-wrap select {
  width: 100%;
  padding: 8px 12px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  color: var(--text);
  font-size: 13px;
}

.condition-value-wrap input:focus,
.condition-value-wrap select:focus {
  outline: none;
  border-color: var(--primary);
}

.condition-type-value {
  cursor: pointer;
}

.date-separator {
  color: var(--text-muted);
  font-size: 12px;
  white-space: nowrap;
}

.condition-date-preset {
  padding: 6px 10px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  color: var(--text);
  font-size: 13px;
  cursor: pointer;
  min-width: 140px;
  flex: 0 0 auto;
}

.condition-remove {
  background: none;
  border: none;
  color: var(--text-muted);
  font-size: 20px;
  cursor: pointer;
  padding: 4px 8px;
  line-height: 1;
  border-radius: 4px;
  transition: all 0.15s;
}

.condition-remove:hover {
  color: var(--danger);
  background: rgba(239, 68, 68, 0.1);
}

/* --- Tag Input (Participants) --- */
.tag-input-wrap {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 6px;
  padding: 6px 10px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  min-height: 38px;
  flex: 1;
  position: relative;
}

.tag-input-wrap:focus-within {
  border-color: var(--primary);
}

.tag-input-tags {
  display: flex;
  flex-wrap: wrap;
  gap: 4px;
}

.tag {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  background: var(--primary);
  color: #fff;
  padding: 2px 10px;
  border-radius: 12px;
  font-size: 12px;
  white-space: nowrap;
}

.tag .tag-remove {
  cursor: pointer;
  font-weight: bold;
  opacity: 0.7;
  margin-left: 2px;
}

.tag .tag-remove:hover {
  opacity: 1;
}

.tag-input {
  border: none !important;
  background: transparent !important;
  padding: 4px 0 !important;
  font-size: 13px !important;
  min-width: 120px;
  flex: 1;
  color: var(--text) !important;
  outline: none !important;
}

/* --- Autocomplete Dropdown --- */
.ac-dropdown {
  position: absolute;
  top: 100%;
  left: 0;
  right: 0;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  margin-top: 4px;
  max-height: 220px;
  overflow-y: auto;
  z-index: 1000;
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.3);
}

.ac-item {
  display: flex;
  flex-direction: column;
  padding: 8px 12px;
  cursor: pointer;
  border-bottom: 1px solid var(--border);
}

.ac-item:last-child {
  border-bottom: none;
}

.ac-item:hover,
.ac-item.ac-active {
  background: var(--surface-2);
}

.ac-name {
  font-size: 13px;
  font-weight: 500;
  color: var(--text);
}

.ac-email {
  font-size: 11px;
  color: var(--text-muted);
}

/* --- Add Condition --- */
.add-condition-row {
  margin-bottom: 20px;
}

.add-condition-dropdown-wrap {
  position: relative;
  display: inline-block;
}

.add-condition-menu {
  position: absolute;
  top: 100%;
  left: 0;
  margin-top: 4px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.3);
  z-index: 200;
  min-width: 180px;
  overflow: hidden;
}

[data-theme="light"] .add-condition-menu {
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.12);
}

.add-condition-menu .menu-item {
  padding: 10px 16px;
  font-size: 13px;
  cursor: pointer;
  transition: background 0.1s;
}

.add-condition-menu .menu-item:hover {
  background: var(--surface-2);
}

.add-condition-menu .menu-item + .menu-item {
  border-top: 1px solid var(--border);
}

/* --- Form Actions --- */
.form-actions {
  display: flex;
  gap: 12px;
}

/* --- Form Group --- */
.form-group label {
  display: block;
  font-size: 13px;
  font-weight: 600;
  margin-bottom: 6px;
  color: var(--text);
}

.search-title-group {
  margin-bottom: 16px;
}

.form-group small {
  display: block;
  font-size: 12px;
  color: var(--text-muted);
  margin-top: 4px;
}

.form-group input {
  width: 100%;
  padding: 10px 14px;
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  color: var(--text);
  font-size: 14px;
  transition: border-color 0.2s;
}

.form-group input:focus {
  outline: none;
  border-color: var(--primary);
}

/* Make date picker calendar icon visible in dark mode */
input[type="date"]::-webkit-calendar-picker-indicator {
  cursor: pointer;
  filter: invert(0.7);
  opacity: 1;
}

[data-theme="light"] input[type="date"]::-webkit-calendar-picker-indicator {
  filter: none;
}

/* --- Results Panel --- */
.results-panel {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 28px;
}

.results-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 20px;
  flex-wrap: wrap;
  gap: 12px;
}

.results-header h2 {
  font-size: 18px;
  font-weight: 700;
}

.results-actions {
  display: flex;
  align-items: center;
  gap: 12px;
}

.sender-filter-wrap {
  display: flex;
  align-items: center;
  gap: 8px;
  color: var(--text-muted);
  font-size: 12px;
}

.sender-filter-wrap input {
  min-width: 220px;
  height: 34px;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 6px 10px;
  background: var(--surface-2);
  color: var(--text);
}

.sender-filter-wrap input:focus {
  outline: none;
  border-color: var(--primary);
}

.badge {
  background: var(--primary);
  color: #fff;
  padding: 2px 10px;
  border-radius: 20px;
  font-size: 13px;
  font-weight: 600;
  margin-left: 8px;
}

.type-badge {
  font-size: 11px;
  padding: 1px 8px;
  text-transform: capitalize;
  white-space: nowrap;
  display: inline-block;
}

/* --- Loading --- */
.loading {
  text-align: center;
  padding: 40px 20px;
}

.spinner {
  width: 36px;
  height: 36px;
  border: 3px solid var(--border);
  border-top-color: var(--primary);
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
  margin: 0 auto 16px;
}

@keyframes spin {
  to {
    transform: rotate(360deg);
  }
}

.loading p {
  color: var(--text-muted);
  font-size: 14px;
}

/* --- Empty --- */
.empty-state {
  text-align: center;
  padding: 40px 20px;
  color: var(--text-muted);
}

/* --- Table --- */
.table-wrap {
  overflow-x: auto;
}

table {
  width: 100%;
  border-collapse: collapse;
  font-size: 13px;
}

thead th {
  background: var(--surface-2);
  padding: 10px 14px;
  text-align: left;
  font-weight: 600;
  font-size: 12px;
  text-transform: uppercase;
  letter-spacing: 0.5px;
  color: var(--text-muted);
  border-bottom: 1px solid var(--border);
  white-space: nowrap;
}

tbody tr {
  border-bottom: 1px solid var(--border);
  transition: background 0.1s;
}

tbody tr:hover {
  background: var(--surface-2);
}

tbody td {
  padding: 10px 14px;
  vertical-align: top;
}

.thread-group-row td {
  background: var(--surface-2);
  color: var(--text-muted);
  font-size: 12px;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.4px;
  padding-top: 12px;
  padding-bottom: 12px;
}

.thread-group-row:hover {
  background: transparent;
}

.thread-participants-cell {
  text-align: right;
  font-weight: 400;
  text-transform: none;
  letter-spacing: 0;
  color: var(--text-muted);
  font-size: 12px;
  opacity: 0.8;
}

.msg-text-cell {
  max-width: 400px;
  word-break: break-word;
}

.msg-text-cell a {
  color: var(--primary);
  word-break: break-all;
}

.participants-cell {
  max-width: 200px;
  font-size: 12px;
  color: var(--text-muted);
  word-break: break-word;
}

td a {
  color: var(--primary);
  text-decoration: none;
}
td a:hover {
  text-decoration: underline;
}

.msg-files {
  margin-top: 6px;
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.msg-file {
  font-size: 12px;
}

.msg-file img {
  display: block;
  border: 1px solid var(--border);
}

.msg-media {
  margin-top: 4px;
}

.msg-thread-badge {
  font-size: 11px;
  font-weight: 600;
  color: var(--text-muted);
  margin-bottom: 4px;
}

.msg-forwarded {
  margin-top: 6px;
  padding: 8px 10px;
  border-left: 3px solid var(--primary);
  background: var(--surface-2);
  border-radius: 4px;
}

.msg-forwarded-label {
  font-size: 11px;
  font-weight: 600;
  color: var(--text-muted);
  margin-bottom: 2px;
}

.msg-forwarded-author {
  font-size: 12px;
  font-weight: 600;
  margin-bottom: 2px;
}

.msg-forwarded-text {
  font-size: 13px;
  word-break: break-word;
}

.msg-img {
  max-width: 320px;
  max-height: 280px;
  border-radius: var(--radius-sm);
  border: 1px solid var(--border);
  cursor: pointer;
  transition: transform 0.15s;
}

.msg-img:hover {
  transform: scale(1.02);
}

.msg-video {
  max-width: 400px;
  max-height: 300px;
  border-radius: var(--radius-sm);
  border: 1px solid var(--border);
}

.msg-audio {
  max-width: 300px;
  vertical-align: middle;
  margin-right: 8px;
}

.msg-file-name {
  font-size: 12px;
  color: var(--text-muted);
}

.msg-text-cell {
  max-width: 500px;
  word-break: break-word;
}

/* --- Responsive --- */
@media (max-width: 768px) {
  .condition-row {
    flex-direction: column;
    align-items: stretch;
  }
  .condition-value-wrap {
    min-width: unset;
  }
  .results-header {
    flex-direction: column;
    align-items: flex-start;
  }
  .results-actions {
    width: 100%;
    flex-direction: column;
    align-items: stretch;
  }
  .sender-filter-wrap {
    width: 100%;
    justify-content: space-between;
  }
  .sender-filter-wrap input {
    min-width: 0;
    width: 100%;
  }
  main {
    padding: 16px;
  }
  .search-panel,
  .results-panel {
    padding: 20px;
  }
}

.badge-running {
  background: var(--primary);
  color: #fff;
  margin-left: 6px;
}

/* ── Reports table ──────────────────────────────────────────────────────── */
.report-status.running { color: var(--primary); font-weight: 600; }
.report-status.complete { color: #16a34a; font-weight: 600; }
.report-status.failed { color: #dc2626; font-weight: 600; }

.report-actions {
  display: flex;
  gap: 6px;
  flex-wrap: wrap;
}

.btn-sm {
  padding: 4px 10px;
  font-size: 12px;
}

.report-started-note {
  align-self: center;
  color: var(--text-muted);
  font-size: 13px;
  margin-left: 10px;
}

/* ── Running search progress cards (under Search/Clear buttons) ─────────── */
.running-searches {
  display: flex;
  flex-direction: column;
  gap: 10px;
  margin-top: 14px;
}

.running-search-card {
  background: var(--surface-2);
  border-radius: 10px;
  padding: 12px 16px;
}

.running-search-top {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 8px;
  gap: 12px;
}

.running-search-title-group {
  display: flex;
  align-items: center;
  gap: 6px;
  min-width: 0;
  flex: 1;
}

.running-search-query {
  font-size: 13px;
  font-weight: 600;
  color: var(--text);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  min-width: 0;
}

.running-search-rename-btn {
  flex-shrink: 0;
  padding: 2px 8px;
  font-size: 11px;
  opacity: 0.7;
}

.running-search-rename-btn:hover {
  opacity: 1;
}

.running-search-rename-input {
  font-size: 13px;
  font-weight: 600;
  color: var(--text);
  background: var(--surface);
  border: 1px solid var(--primary);
  border-radius: var(--radius-sm);
  padding: 2px 6px;
  min-width: 0;
  flex: 1;
}

.running-search-rename-input:focus {
  outline: none;
}

.running-search-elapsed {
  font-size: 12px;
  color: var(--text-muted);
  flex-shrink: 0;
}

.progress-bar {
  height: 6px;
  border-radius: 3px;
  background: rgba(128, 128, 128, 0.25);
  overflow: hidden;
  position: relative;
}

.progress-bar-fill {
  position: absolute;
  height: 100%;
  width: 35%;
  border-radius: 3px;
  background: var(--primary, #6366f1);
  animation: progress-slide 1.4s ease-in-out infinite;
}

@keyframes progress-slide {
  0%   { left: -35%; }
  100% { left: 100%; }
}

.running-search-card.failed {
  border: 1px solid #dc2626;
}

.running-search-error {
  font-size: 13px;
  color: #dc2626;
  display: flex;
  align-items: center;
  gap: 8px;
}

.running-search-error.cancelled {
  color: var(--text-muted);
}

.running-search-cancel-btn {
  margin-top: 8px;
}

/* ── Completed running-search card ──────────────────────────────────────── */
.running-search-card.done {
  opacity: 0.9;
}

.running-search-status {
  font-size: 13px;
  color: var(--text-muted);
}

.running-search-criteria {
  font-size: 12px;
  color: var(--text-muted);
  margin-top: -4px;
  margin-bottom: 8px;
}

.results-header-left {
  display: flex;
  align-items: center;
  gap: 12px;
}

/* ── Home: Cases list ────────────────────────────────────────────────────── */
.home-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 20px;
}

.home-header h2 {
  font-size: 18px;
  font-weight: 700;
}

/* ── Audit log ───────────────────────────────────────────────────────────── */
.audit-filter-row {
  display: flex;
  align-items: flex-end;
  gap: 14px;
  flex-wrap: wrap;
  margin-bottom: 20px;
  padding-bottom: 20px;
  border-bottom: 1px solid var(--border);
}

.audit-filter-row .form-group {
  margin-bottom: 0;
  min-width: 180px;
}

.audit-filter-row #audit-filter-user {
  min-width: 220px;
}

.mono-cell {
  font-family: "SF Mono", "Cascadia Code", Consolas, ui-monospace, monospace;
  font-size: 12px;
  color: var(--text-muted);
}

#cases-table th:nth-child(1) { width: 26%; }
#cases-table th:nth-child(2) { width: 44%; }
#cases-table th:nth-child(3) { width: 15%; }
#cases-table th:nth-child(4) { width: 15%; }

.case-row {
  cursor: pointer;
}

.case-name-cell {
  font-weight: 600;
  color: var(--text);
  white-space: nowrap;
}

.case-name-cell .badge-running {
  margin-left: 8px;
}

.case-desc-cell {
  color: var(--text-muted);
  max-width: 420px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

/* ── Case detail header ──────────────────────────────────────────────────── */
.case-header-panel {
  margin-bottom: 24px;
}

.case-header {
  display: flex;
  align-items: flex-start;
  gap: 16px;
}

.case-header-text {
  flex: 1;
}

.case-header-text h2 {
  font-size: 20px;
  font-weight: 700;
  margin-bottom: 4px;
}


.case-description {
  color: var(--text-muted);
  font-size: 14px;
}

/* ── Completed search card action row (View / Export / Delete) ─────────── */
.running-search-actions {
  display: flex;
  gap: 6px;
  flex-wrap: wrap;
  margin-top: 8px;
}

/* ── Create Case modal ──────────────────────────────────────────────────── */
.modal-backdrop {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.55);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 1000;
  padding: 16px;
}

.modal-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 28px;
  width: 100%;
  max-width: 440px;
  box-shadow: 0 25px 50px -12px rgba(0, 0, 0, 0.5);
}

.modal-card h2 {
  font-size: 18px;
  font-weight: 700;
  margin-bottom: 18px;
}

.modal-card .form-group {
  margin-bottom: 16px;
}

.form-group textarea {
  width: 100%;
  padding: 10px 14px;
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  color: var(--text);
  font-size: 14px;
  font-family: inherit;
  resize: vertical;
  transition: border-color 0.2s;
}

.form-group textarea:focus {
  outline: none;
  border-color: var(--primary);
}

.modal-actions {
  display: flex;
  justify-content: flex-end;
  gap: 10px;
  margin-top: 8px;
}
