*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

:root {
  --bg: #f0f2f5;
  --sidebar: #1a1d2e;
  --sidebar-hover: #252840;
  --sidebar-active: #3b5bfd;
  --card: #ffffff;
  --border: #e8ecf1;
  --text: #1a1d2e;
  --text2: #6b7280;
  --primary: #3b5bfd;
  --primary-light: #eef1ff;
  --success: #10b981;
  --success-light: #ecfdf5;
  --warning: #f59e0b;
  --warning-light: #fffbeb;
  --danger: #ef4444;
  --danger-light: #fef2f2;
  --purple: #8b5cf6;
  --purple-light: #f5f3ff;
  --radius: 12px;
  --shadow: 0 1px 3px rgba(0,0,0,.06), 0 4px 12px rgba(0,0,0,.04);
  --shadow-lg: 0 8px 30px rgba(0,0,0,.12);
  --sidebar-w: 240px;
}

html { font-size: 14px; }
body {
  font-family: 'Inter', -apple-system, BlinkMacSystemFont, sans-serif;
  background: var(--bg);
  color: var(--text);
  line-height: 1.5;
  -webkit-font-smoothing: antialiased;
}

.field-hint { font-size: .75rem; color: var(--text2); margin-top: 6px; line-height: 1.5; }
.field-hint a { color: var(--primary); }
.panel-tip { padding: 0 20px 12px; font-size: .8125rem; color: var(--text2); margin: 0; border-bottom: 1px solid var(--border); }

.retention-notice {
  display: flex;
  align-items: center;
  gap: 10px;
  background: #eef1ff;
  border: 1px solid #c7d2fe;
  color: #3730a3;
  padding: 12px 16px;
  border-radius: 10px;
  margin-bottom: 20px;
  font-size: .8125rem;
  line-height: 1.5;
}

.retention-notice.compact { padding: 8px 14px; margin-bottom: 16px; }
.retention-notice i { font-size: 1.125rem; flex-shrink: 0; }

.chart-empty {
  text-align: center;
  padding: 40px;
  color: var(--text2);
  font-size: .875rem;
}

.hidden { display: none !important; }
.text-muted { color: var(--text2); font-weight: 400; font-size: .875rem; }

/* ===== Login ===== */
.login-wrap {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  position: relative;
  overflow: hidden;
}

.login-bg {
  position: absolute;
  inset: 0;
  background: linear-gradient(135deg, #1a1d2e 0%, #2d3561 50%, #3b5bfd 100%);
}

.login-bg::after {
  content: '';
  position: absolute;
  inset: 0;
  background: url("data:image/svg+xml,%3Csvg width='60' height='60' viewBox='0 0 60 60' xmlns='http://www.w3.org/2000/svg'%3E%3Cg fill='none' fill-rule='evenodd'%3E%3Cg fill='%23ffffff' fill-opacity='0.03'%3E%3Cpath d='M36 34v-4h-2v4h-4v2h4v4h2v-4h4v-2h-4zm0-30V0h-2v4h-4v2h4v4h2V6h4V4h-4zM6 34v-4H4v4H0v2h4v4h2v-4h4v-2H6zM6 4V0H4v4H0v2h4v4h2V6h4V4H6z'/%3E%3C/g%3E%3C/g%3E%3C/svg%3E");
}

.login-box {
  position: relative;
  background: var(--card);
  border-radius: 16px;
  padding: 40px;
  width: 400px;
  max-width: 92vw;
  box-shadow: var(--shadow-lg);
}

.login-logo { text-align: center; margin-bottom: 32px; }
.logo-icon, .brand-icon {
  width: 56px; height: 56px;
  background: linear-gradient(135deg, var(--primary), #6366f1);
  border-radius: 14px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  color: #fff;
  font-size: 28px;
  margin-bottom: 16px;
  box-shadow: 0 4px 14px rgba(59,91,253,.4);
}

.login-logo h1 { font-size: 1.375rem; font-weight: 700; margin-bottom: 6px; }
.login-logo p { color: var(--text2); font-size: .8125rem; }

.captcha-row { display: flex; align-items: center; gap: 8px; }
.captcha-row input { flex: 1; min-width: 0; }
.captcha-img {
  height: 44px;
  border-radius: 8px;
  border: 1px solid var(--border);
  cursor: pointer;
  flex-shrink: 0;
}
.captcha-refresh { flex-shrink: 0; padding: 8px 10px; }

.field { margin-bottom: 16px; }
.field label { display: block; font-size: .8125rem; font-weight: 500; color: var(--text2); margin-bottom: 6px; }
.form-error { color: var(--danger); text-align: center; margin-top: 12px; font-size: .8125rem; }

/* ===== Inputs & Buttons ===== */
.input, select.input, .field input, .field select, .modal-body input, .modal-body select {
  width: 100%;
  padding: 10px 14px;
  border: 1.5px solid var(--border);
  border-radius: 8px;
  font-size: .875rem;
  font-family: inherit;
  color: var(--text);
  background: #fff;
  transition: border-color .15s, box-shadow .15s;
  outline: none;
}

.input:focus, .field input:focus, .field select:focus, .modal-body input:focus {
  border-color: var(--primary);
  box-shadow: 0 0 0 3px rgba(59,91,253,.12);
}

.input-sm { padding: 7px 12px; font-size: .8125rem; width: auto; }

.btn {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 9px 18px;
  border: none;
  border-radius: 8px;
  font-size: .875rem;
  font-weight: 500;
  font-family: inherit;
  cursor: pointer;
  transition: all .15s;
  white-space: nowrap;
}

.btn-primary { background: var(--primary); color: #fff; }
.btn-primary:hover { background: #2d4ae0; transform: translateY(-1px); box-shadow: 0 4px 12px rgba(59,91,253,.3); }
.btn-outline { background: #fff; color: var(--primary); border: 1.5px solid var(--border); }
.btn-outline:hover { border-color: var(--primary); background: var(--primary-light); }
.btn-ghost { background: transparent; color: var(--text2); }
.btn-ghost:hover { background: var(--bg); color: var(--text); }
.btn-danger { background: var(--danger-light); color: var(--danger); }
.btn-danger:hover { background: var(--danger); color: #fff; }
.btn-sm { padding: 6px 12px; font-size: .8125rem; }
.btn-block { width: 100%; justify-content: center; }
.btn:disabled { opacity: .5; cursor: not-allowed; transform: none !important; }

/* ===== App Layout ===== */
.app { display: flex; min-height: 100vh; }

.sidebar {
  width: var(--sidebar-w);
  background: var(--sidebar);
  display: flex;
  flex-direction: column;
  position: fixed;
  top: 0; left: 0; bottom: 0;
  z-index: 100;
  transition: transform .25s;
}

.sidebar-brand {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 20px 20px 24px;
  color: #fff;
  font-weight: 700;
  font-size: 1.0625rem;
}

.brand-icon { width: 36px; height: 36px; font-size: 18px; margin-bottom: 0; border-radius: 10px; }

.sidebar-nav { flex: 1; padding: 0 12px; overflow-y: auto; }

.nav-link {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 11px 14px;
  color: rgba(255,255,255,.55);
  text-decoration: none;
  border-radius: 8px;
  margin-bottom: 2px;
  font-size: .875rem;
  font-weight: 500;
  transition: all .15s;
}

.nav-link i { font-size: 1.125rem; width: 20px; text-align: center; }
.nav-link:hover { color: #fff; background: var(--sidebar-hover); }
.nav-link.active { color: #fff; background: var(--sidebar-active); box-shadow: 0 2px 8px rgba(59,91,253,.4); }

.sidebar-footer { padding: 16px 12px; border-top: 1px solid rgba(255,255,255,.08); }
.sidebar-footer .btn { color: rgba(255,255,255,.5); justify-content: center; }
.sidebar-footer .btn:hover { color: #fff; background: rgba(255,255,255,.08); }

.main {
  flex: 1;
  margin-left: var(--sidebar-w);
  display: flex;
  flex-direction: column;
  min-height: 100vh;
}

.topbar {
  height: 60px;
  background: var(--card);
  border-bottom: 1px solid var(--border);
  display: flex;
  align-items: center;
  padding: 0 24px;
  gap: 16px;
  position: sticky;
  top: 0;
  z-index: 50;
}

.sidebar-toggle {
  display: none;
  background: none;
  border: none;
  font-size: 1.25rem;
  cursor: pointer;
  color: var(--text);
  padding: 4px;
}

.topbar-title { font-size: 1.0625rem; font-weight: 600; }
.topbar-actions { margin-left: auto; display: flex; gap: 8px; align-items: center; }

.content { padding: 24px; flex: 1; }

/* ===== Stats Cards ===== */
.stats-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(180px, 1fr));
  gap: 16px;
  margin-bottom: 24px;
}

.stat-card {
  background: var(--card);
  border-radius: var(--radius);
  padding: 20px;
  box-shadow: var(--shadow);
  display: flex;
  align-items: flex-start;
  gap: 14px;
  transition: transform .15s, box-shadow .15s;
}

.stat-card:hover { transform: translateY(-2px); box-shadow: var(--shadow-lg); }

.stat-icon {
  width: 44px; height: 44px;
  border-radius: 10px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 1.25rem;
  flex-shrink: 0;
}

.stat-icon.blue { background: var(--primary-light); color: var(--primary); }
.stat-icon.green { background: var(--success-light); color: var(--success); }
.stat-icon.yellow { background: var(--warning-light); color: var(--warning); }
.stat-icon.orange { background: #fff7ed; color: #ea580c; }
.stat-icon.red { background: var(--danger-light); color: var(--danger); }
.stat-icon.purple { background: var(--purple-light); color: var(--purple); }

.stat-info { flex: 1; min-width: 0; }
.stat-label { font-size: .8125rem; color: var(--text2); margin-bottom: 4px; }
.stat-value { font-size: 1.625rem; font-weight: 700; line-height: 1.2; }

/* ===== Panel & Table ===== */
.panel {
  background: var(--card);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  margin-bottom: 20px;
  overflow: hidden;
}

.panel-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 16px 20px;
  border-bottom: 1px solid var(--border);
  gap: 12px;
  flex-wrap: wrap;
}

.panel-head h3 { font-size: .9375rem; font-weight: 600; }
.panel-body { padding: 20px; }

.filter-panel { padding: 20px; }
.filter-row { display: flex; gap: 16px; align-items: flex-end; flex-wrap: wrap; }
.filter-row .field { margin-bottom: 0; min-width: 140px; flex: 1; }

.table-wrap { overflow-x: auto; }

.table {
  width: 100%;
  border-collapse: collapse;
  font-size: .8125rem;
}

.table th {
  text-align: left;
  padding: 12px 20px;
  color: var(--text2);
  font-weight: 500;
  font-size: .75rem;
  text-transform: uppercase;
  letter-spacing: .03em;
  background: #fafbfc;
  border-bottom: 1px solid var(--border);
  white-space: nowrap;
}

.table td {
  padding: 14px 20px;
  border-bottom: 1px solid var(--border);
  vertical-align: middle;
}

.table tbody tr { transition: background .1s; }
.table tbody tr:hover { background: #fafbfc; }
.table tbody tr:last-child td { border-bottom: none; }
.table tbody tr.row-unattributed td { background: #f8fafc; color: var(--text2); }
.table tbody tr.row-unattributed:hover td { background: #f1f5f9; }
.table tbody tr.row-unattributed strong { color: #64748b; font-weight: 600; }

.table code {
  background: #f3f4f6;
  padding: 2px 8px;
  border-radius: 4px;
  font-size: .8125rem;
}

/* ===== Badges ===== */
.badge {
  display: inline-flex;
  align-items: center;
  padding: 3px 10px;
  border-radius: 20px;
  font-size: .75rem;
  font-weight: 600;
}

.badge-green { background: var(--success-light); color: var(--success); }
.badge-yellow { background: var(--warning-light); color: var(--warning); }
.badge-red { background: var(--danger-light); color: var(--danger); }
.badge-blue { background: var(--primary-light); color: var(--primary); }
.badge-purple { background: var(--purple-light); color: var(--purple); }
.badge-gray { background: #f3f4f6; color: var(--text2); }
.badge-orange { background: #fff7ed; color: #ea580c; }

/* ===== Empty State ===== */
.empty-state {
  text-align: center;
  padding: 48px 24px;
  color: var(--text2);
}

.empty-state i { font-size: 3rem; opacity: .3; margin-bottom: 12px; display: block; }
.empty-state p { margin-bottom: 16px; font-size: .875rem; }

/* ===== Modal ===== */
.modal-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,.45);
  backdrop-filter: blur(4px);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 200;
  padding: 20px;
  animation: fadeIn .15s;
}

.modal-box {
  background: var(--card);
  border-radius: 14px;
  width: 440px;
  max-width: 100%;
  max-height: 90vh;
  display: flex;
  flex-direction: column;
  box-shadow: var(--shadow-lg);
  animation: slideUp .2s;
}

.modal-lg { width: 720px; }

.modal-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 18px 24px;
  border-bottom: 1px solid var(--border);
}

.modal-head h3 { font-size: 1rem; font-weight: 600; }

.modal-close {
  background: none;
  border: none;
  font-size: 1.125rem;
  cursor: pointer;
  color: var(--text2);
  padding: 4px;
  border-radius: 6px;
}

.modal-close:hover { background: var(--bg); color: var(--text); }

.modal-body { padding: 24px; overflow-y: auto; flex: 1; }
.modal-body .field { margin-bottom: 16px; }

.modal-foot {
  display: flex;
  justify-content: flex-end;
  gap: 8px;
  padding: 16px 24px;
  border-top: 1px solid var(--border);
}

/* ===== Chart ===== */
.chart-wrap { padding: 20px; height: 280px; }

/* ===== Tags ===== */
.tags { display: flex; flex-wrap: wrap; gap: 8px; margin-top: 12px; }

.tag {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  background: #f3f4f6;
  padding: 5px 12px;
  border-radius: 20px;
  font-size: .8125rem;
}

.tag-remove {
  cursor: pointer;
  color: var(--text2);
  font-size: 1rem;
  line-height: 1;
  border: none;
  background: none;
  padding: 0;
}

.tag-remove:hover { color: var(--danger); }

.rules-quick-add { border-top: 1px solid var(--border); margin-top: 8px; padding-top: 16px; }
.field-label { display: block; font-size: .8125rem; color: var(--text2); margin-bottom: 8px; }

.keyword-input { display: flex; gap: 8px; max-width: 420px; }

.link-copy {
  color: var(--primary);
  cursor: pointer;
  max-width: 200px;
  display: inline-block;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  font-size: .8125rem;
}

.link-copy:hover { text-decoration: underline; }

.msg-preview {
  max-width: 240px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  cursor: pointer;
  color: var(--text2);
}

.msg-preview:hover { color: var(--text); }

.actions { display: flex; gap: 6px; }

/* ===== Toast ===== */
.toast-container {
  position: fixed;
  top: 20px;
  right: 20px;
  z-index: 300;
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.toast {
  background: var(--card);
  border-radius: 10px;
  padding: 12px 18px;
  box-shadow: var(--shadow-lg);
  font-size: .8125rem;
  display: flex;
  align-items: center;
  gap: 10px;
  animation: slideIn .25s;
  min-width: 240px;
  border-left: 3px solid var(--primary);
}

.toast.success { border-left-color: var(--success); }
.toast.error { border-left-color: var(--danger); }

/* ===== Loading ===== */
.loading {
  position: fixed;
  inset: 0;
  background: rgba(255,255,255,.6);
  backdrop-filter: blur(2px);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 400;
}

.spinner {
  width: 36px; height: 36px;
  border: 3px solid var(--border);
  border-top-color: var(--primary);
  border-radius: 50%;
  animation: spin .6s linear infinite;
}

@keyframes fadeIn { from { opacity: 0; } to { opacity: 1; } }
@keyframes slideUp { from { opacity: 0; transform: translateY(16px); } to { opacity: 1; transform: none; } }
@keyframes slideIn { from { opacity: 0; transform: translateX(40px); } to { opacity: 1; transform: none; } }
@keyframes spin { to { transform: rotate(360deg); } }

/* ===== Responsive ===== */
@media (max-width: 768px) {
  .sidebar { transform: translateX(-100%); }
  .sidebar.open { transform: translateX(0); }
  .main { margin-left: 0; }
  .sidebar-toggle { display: block; }
  .content { padding: 16px; }
  .stats-grid { grid-template-columns: repeat(2, 1fr); }
  .filter-row { flex-direction: column; }
  .filter-row .field { width: 100%; }
}
