/* ClueMail Design System — Postal Dispatch & Letterpress Ledger
   Strictly follows 4px spacing grid, Ink neutral ramps, single Indigo accent,
   tabular figures, and Lucide 18px line icons. */

:root {
  --bg-page: #f5f7fa;
  --bg-card: #ffffff;
  --bg-raised: #eceff4;
  --bg-sidebar: #0f172a;
  --bg-sidebar-active: #1e293b;
  --text-primary: #111827;
  --text-secondary: #4b5565;
  --text-muted: #6b7487;
  --text-sidebar: #cbd5e1;
  --text-sidebar-muted: #94a3b8;
  --text-sidebar-active: #ffffff;
  --border-hairline: rgba(17, 24, 39, 0.09);
  --border-strong: rgba(17, 24, 39, 0.16);
  --border-sidebar: rgba(248, 250, 252, 0.08);
  --accent: #4338ca;
  --accent-hover: #3730a3;
  --accent-soft: rgba(67, 56, 202, 0.09);
  --accent-text: #ffffff;
  --positive: #16a34a;
  --positive-soft: rgba(22, 163, 74, 0.1);
  --negative: #dc2626;
  --negative-soft: rgba(220, 38, 38, 0.1);
  --warning: #d97706;
  --warning-soft: rgba(217, 119, 6, 0.1);
  --shadow-sm: 0 1px 2px rgba(17, 24, 39, 0.05);
  --shadow-md: 0 8px 24px rgba(17, 24, 39, 0.07);
  --radius-sm: 6px;
  --radius-md: 8px;
  --radius-lg: 12px;
  --sidebar-w: 256px;
  --topbar-h: 60px;
  --ease: cubic-bezier(0.22, 1, 0.36, 1);
  --font-sans: 'Inter', -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
  --font-mono: 'JetBrains Mono', ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
}

[data-theme="dark"] {
  --bg-page: #0b0d14;
  --bg-card: #11141c;
  --bg-raised: #1a1e28;
  --bg-sidebar: #07080d;
  --bg-sidebar-active: #151924;
  --text-primary: #eef0f5;
  --text-secondary: #a3a9b8;
  --text-muted: #7a8296;
  --text-sidebar: #cbd5e1;
  --text-sidebar-muted: #7a8296;
  --text-sidebar-active: #ffffff;
  --border-hairline: rgba(238, 240, 245, 0.08);
  --border-strong: rgba(238, 240, 245, 0.16);
  --border-sidebar: rgba(238, 240, 245, 0.07);
  --accent: #4f46e5;
  --accent-hover: #6366f1;
  --accent-soft: rgba(99, 102, 241, 0.16);
  --positive: #4cc38a;
  --positive-soft: rgba(76, 195, 138, 0.12);
  --negative: #e5484d;
  --negative-soft: rgba(229, 72, 77, 0.12);
  --warning: #f59e0b;
  --warning-soft: rgba(245, 158, 11, 0.12);
  --shadow-sm: none;
  --shadow-md: 0 8px 24px rgba(0, 0, 0, 0.35);
}

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

html, body {
  margin: 0;
  padding: 0;
  font-family: var(--font-sans);
  font-size: 14px;
  line-height: 1.5;
  color: var(--text-primary);
  background: var(--bg-page);
  -webkit-font-smoothing: antialiased;
}

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

.mono, .tabular {
  font-variant-numeric: tabular-nums;
}
.mono {
  font-family: var(--font-mono);
}

/* App Shell */
.app-shell {
  display: flex;
  min-height: 100vh;
}

.sidebar {
  width: var(--sidebar-w);
  background: var(--bg-sidebar);
  color: var(--text-sidebar);
  border-right: 1px solid var(--border-sidebar);
  display: flex;
  flex-direction: column;
  position: fixed;
  top: 0;
  bottom: 0;
  left: 0;
  z-index: 40;
  transition: transform 200ms var(--ease), width 200ms var(--ease);
}

.app-shell.sidebar-collapsed .sidebar {
  width: 68px;
}

.app-shell.sidebar-collapsed .sidebar .nav-label,
.app-shell.sidebar-collapsed .sidebar .nav-section-label,
.app-shell.sidebar-collapsed .sidebar .brand-text,
.app-shell.sidebar-collapsed .sidebar .sidebar-footer-text {
  display: none;
}

.sidebar-brand {
  height: var(--topbar-h);
  padding: 0 16px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  border-bottom: 1px solid var(--border-sidebar);
}

.brand-link {
  display: flex;
  align-items: center;
  gap: 10px;
  color: var(--text-sidebar-active);
  font-weight: 600;
  font-size: 16px;
  letter-spacing: -0.01em;
}

.sidebar-nav {
  flex: 1;
  overflow-y: auto;
  padding: 12px 12px 20px;
}

.nav-section-label {
  font-size: 12px;
  font-weight: 500;
  color: var(--text-sidebar-muted);
  padding: 20px 10px 8px;
  margin: 0;
}

.nav-section-label:first-child {
  padding-top: 8px;
}

.nav-item {
  display: flex;
  align-items: center;
  height: 36px;
  padding: 0 10px;
  margin-bottom: 2px;
  border-radius: var(--radius-md);
  color: var(--text-sidebar);
  font-size: 14px;
  font-weight: 400;
  gap: 10px;
  position: relative;
  transition: background-color 140ms var(--ease), color 140ms var(--ease);
}

.nav-item:hover {
  background: rgba(255, 255, 255, 0.06);
  color: var(--text-sidebar-active);
}

.nav-item.active {
  background: var(--bg-sidebar-active);
  color: var(--text-sidebar-active);
  font-weight: 500;
  box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.06);
}

.nav-item.active::before {
  content: '';
  position: absolute;
  left: 0;
  top: 7px;
  bottom: 7px;
  width: 3px;
  border-radius: 0 2px 2px 0;
  background: #6366f1;
}

.nav-icon {
  width: 20px;
  height: 20px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  color: var(--text-sidebar-muted);
}

.nav-item.active .nav-icon,
.nav-item:hover .nav-icon {
  color: var(--text-sidebar-active);
}

.sidebar-footer {
  padding: 12px 16px;
  border-top: 1px solid var(--border-sidebar);
  font-size: 12px;
  color: var(--text-sidebar-muted);
}

/* Main Area */
.main-wrapper {
  flex: 1;
  margin-left: var(--sidebar-w);
  min-width: 0;
  display: flex;
  flex-direction: column;
  transition: margin-left 200ms var(--ease);
}

.app-shell.sidebar-collapsed .main-wrapper {
  margin-left: 68px;
}

.topbar {
  height: var(--topbar-h);
  background: var(--bg-card);
  border-bottom: 1px solid var(--border-hairline);
  padding: 0 24px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  position: sticky;
  top: 0;
  z-index: 30;
}

.topbar-left {
  display: flex;
  align-items: center;
  gap: 12px;
  flex: 1;
  min-width: 0;
}

.topbar-right {
  display: flex;
  align-items: center;
  gap: 8px;
}

.search-box {
  position: relative;
  width: 100%;
  max-width: 340px;
}

.search-input {
  width: 100%;
  height: 36px;
  padding: 0 36px 0 34px;
  background: var(--bg-page);
  border: 1px solid var(--border-hairline);
  border-radius: var(--radius-md);
  color: var(--text-primary);
  font-size: 13px;
  font-family: inherit;
  transition: border-color 140ms var(--ease), background-color 140ms var(--ease);
}

.search-input:focus {
  outline: none;
  border-color: var(--accent);
  background: var(--bg-card);
}

.search-icon {
  position: absolute;
  left: 10px;
  top: 50%;
  transform: translateY(-50%);
  color: var(--text-muted);
  pointer-events: none;
}

.search-results-dropdown {
  position: absolute;
  top: 42px;
  left: 0;
  right: 0;
  background: var(--bg-card);
  border: 1px solid var(--border-hairline);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow-md);
  max-height: 360px;
  overflow-y: auto;
  z-index: 60;
  display: none;
}

.search-results-dropdown.open { display: block; }

.page-content {
  padding: 24px 32px 32px;
  max-width: 1520px;
  width: 100%;
  margin: 0 auto;
  flex: 1;
}

.page-header {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 16px;
  margin-bottom: 24px;
  flex-wrap: wrap;
}

.page-title {
  margin: 0;
  font-size: 24px;
  font-weight: 500;
  line-height: 1.2;
  letter-spacing: -0.015em;
  color: var(--text-primary);
}

.page-subtitle {
  margin: 4px 0 0;
  font-size: 13px;
  color: var(--text-secondary);
}

.page-actions {
  display: flex;
  align-items: center;
  gap: 8px;
  flex-wrap: wrap;
}

/* Cards & Grids */
.card {
  background: var(--bg-card);
  border: 1px solid var(--border-hairline);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow-sm);
}

.card-header {
  padding: 16px 20px;
  border-bottom: 1px solid var(--border-hairline);
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
}

.card-title {
  margin: 0;
  font-size: 15px;
  font-weight: 500;
  color: var(--text-primary);
}

.card-body {
  padding: 20px;
}

.stats-grid {
  display: grid;
  grid-template-columns: repeat(5, minmax(0, 1fr));
  gap: 16px;
  margin-bottom: 24px;
}

.stat-card {
  background: var(--bg-card);
  border: 1px solid var(--border-hairline);
  border-radius: var(--radius-lg);
  padding: 16px 20px;
  box-shadow: var(--shadow-sm);
}

.stat-label {
  font-size: 12px;
  color: var(--text-muted);
  margin-bottom: 8px;
}

.stat-value {
  font-size: 26px;
  font-weight: 500;
  line-height: 1.1;
  color: var(--text-primary);
  font-variant-numeric: tabular-nums;
  margin-bottom: 6px;
}

.stat-meta {
  font-size: 12px;
  color: var(--text-secondary);
  font-variant-numeric: tabular-nums;
}

.stat-meta.positive { color: var(--positive); }
.stat-meta.negative { color: var(--negative); }

.grid-2-1 {
  display: grid;
  grid-template-columns: minmax(0, 2fr) minmax(0, 1fr);
  gap: 16px;
  margin-bottom: 24px;
}

.grid-2 {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 16px;
  margin-bottom: 24px;
}

.grid-3 {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 16px;
  margin-bottom: 24px;
}

/* Buttons */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  height: 36px;
  padding: 0 14px;
  border-radius: var(--radius-md);
  font-family: inherit;
  font-size: 13px;
  font-weight: 500;
  cursor: pointer;
  border: 1px solid transparent;
  transition: background-color 140ms var(--ease), border-color 140ms var(--ease), color 140ms var(--ease), transform 120ms var(--ease);
  white-space: nowrap;
}

.btn:active { transform: scale(0.98); }

.btn-primary {
  background: var(--accent);
  color: var(--accent-text);
}
.btn-primary:hover { background: var(--accent-hover); color: var(--accent-text); }

.btn-secondary {
  background: var(--bg-card);
  color: var(--text-primary);
  border-color: var(--border-strong);
}
.btn-secondary:hover { background: var(--bg-raised); color: var(--text-primary); }

.btn-danger {
  background: var(--negative);
  color: #ffffff;
}
.btn-danger:hover { opacity: 0.92; color: #ffffff; }

.btn-ghost {
  background: transparent;
  color: var(--text-secondary);
  border-color: transparent;
}
.btn-ghost:hover { background: var(--bg-raised); color: var(--text-primary); }

.btn-sm { height: 32px; padding: 0 10px; font-size: 12px; }
.btn-icon { width: 36px; padding: 0; }

/* Forms */
.form-grid {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 16px;
}

.form-group {
  margin-bottom: 16px;
}

.form-label {
  display: block;
  font-size: 13px;
  font-weight: 500;
  color: var(--text-primary);
  margin-bottom: 6px;
}

.form-control, .form-select, .form-textarea {
  width: 100%;
  min-height: 36px;
  padding: 8px 12px;
  background: var(--bg-card);
  border: 1px solid var(--border-strong);
  border-radius: var(--radius-md);
  color: var(--text-primary);
  font-family: inherit;
  font-size: 13px;
  transition: border-color 140ms var(--ease);
}

.form-control:focus, .form-select:focus, .form-textarea:focus {
  outline: none;
  border-color: var(--accent);
  box-shadow: 0 0 0 3px var(--accent-soft);
}

.form-textarea { min-height: 96px; resize: vertical; }
.form-hint { font-size: 12px; color: var(--text-muted); margin-top: 4px; }

/* Tables */
.table-wrap {
  width: 100%;
  overflow-x: auto;
}

.table {
  width: 100%;
  border-collapse: collapse;
  font-size: 13px;
}

.table th {
  text-align: left;
  font-weight: 500;
  color: var(--text-muted);
  font-size: 12px;
  padding: 10px 16px;
  border-bottom: 1px solid var(--border-hairline);
  white-space: nowrap;
}

.table td {
  padding: 12px 16px;
  border-bottom: 1px solid var(--border-hairline);
  color: var(--text-primary);
  vertical-align: middle;
}

.table tbody tr:hover {
  background: var(--bg-page);
}

.table .num {
  text-align: right;
  font-variant-numeric: tabular-nums;
}

/* Badges */
.badge {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  height: 22px;
  padding: 0 8px;
  border-radius: var(--radius-sm);
  font-size: 12px;
  font-weight: 500;
  background: var(--bg-raised);
  color: var(--text-secondary);
}

.badge-success { background: var(--positive-soft); color: var(--positive); }
.badge-danger { background: var(--negative-soft); color: var(--negative); }
.badge-warning { background: var(--warning-soft); color: var(--warning); }
.badge-accent { background: var(--accent-soft); color: var(--accent); }

.status-dot {
  width: 6px;
  height: 6px;
  border-radius: 50%;
  background: currentColor;
  display: inline-block;
}

/* Alerts & Toast */
.alert {
  padding: 12px 16px;
  border-radius: var(--radius-md);
  border: 1px solid var(--border-hairline);
  margin-bottom: 16px;
  font-size: 13px;
}
.alert-info { background: var(--accent-soft); border-color: rgba(67,56,202,0.2); color: var(--text-primary); }
.alert-success { background: var(--positive-soft); border-color: rgba(22,163,74,0.25); color: var(--positive); }
.alert-danger { background: var(--negative-soft); border-color: rgba(220,38,38,0.25); color: var(--negative); }
.alert-warning { background: var(--warning-soft); border-color: rgba(217,119,6,0.25); color: var(--warning); }

.toast-container {
  position: fixed;
  right: 24px;
  bottom: 24px;
  z-index: 100;
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.toast {
  min-width: 280px;
  max-width: 400px;
  padding: 12px 16px;
  background: var(--bg-sidebar);
  color: #ffffff;
  border-radius: var(--radius-md);
  box-shadow: var(--shadow-md);
  font-size: 13px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
}

/* Modals & Dropdowns */
.modal-backdrop {
  position: fixed;
  inset: 0;
  background: rgba(15, 23, 42, 0.52);
  display: none;
  align-items: center;
  justify-content: center;
  padding: 20px;
  z-index: 80;
}
.modal-backdrop.open { display: flex; }

.modal {
  width: 100%;
  max-width: 560px;
  background: var(--bg-card);
  border: 1px solid var(--border-hairline);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow-md);
  overflow: hidden;
  max-height: 90vh;
  display: flex;
  flex-direction: column;
}

.modal-lg { max-width: 820px; }

.modal-header {
  padding: 16px 20px;
  border-bottom: 1px solid var(--border-hairline);
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.modal-body {
  padding: 20px;
  overflow-y: auto;
}

.modal-footer {
  padding: 14px 20px;
  border-top: 1px solid var(--border-hairline);
  display: flex;
  justify-content: flex-end;
  gap: 8px;
}

.dropdown { position: relative; }
.dropdown-menu {
  position: absolute;
  top: calc(100% + 6px);
  right: 0;
  min-width: 220px;
  background: var(--bg-card);
  border: 1px solid var(--border-hairline);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow-md);
  padding: 6px;
  display: none;
  z-index: 60;
}
.dropdown-menu.open { display: block; }
.dropdown-item {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px 10px;
  border-radius: var(--radius-sm);
  color: var(--text-primary);
  font-size: 13px;
  cursor: pointer;
  width: 100%;
  border: 0;
  background: transparent;
  text-align: left;
}
.dropdown-item:hover { background: var(--bg-raised); }

/* Auth & Installer */
.auth-page {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 24px;
  background: var(--bg-page);
}

.auth-card {
  width: 100%;
  max-width: 420px;
  background: var(--bg-card);
  border: 1px solid var(--border-hairline);
  border-radius: var(--radius-lg);
  padding: 28px;
  box-shadow: var(--shadow-sm);
}

.installer-card {
  width: 100%;
  max-width: 780px;
  background: var(--bg-card);
  border: 1px solid var(--border-hairline);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow-sm);
  overflow: hidden;
}

/* Campaign stepper */
.stepper {
  display: flex;
  gap: 8px;
  margin-bottom: 20px;
  flex-wrap: wrap;
}
.step-pill {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  padding: 6px 12px;
  border-radius: var(--radius-md);
  border: 1px solid var(--border-hairline);
  background: var(--bg-card);
  color: var(--text-secondary);
  font-size: 13px;
  cursor: pointer;
}
.step-pill.active {
  border-color: var(--accent);
  background: var(--accent-soft);
  color: var(--accent);
  font-weight: 500;
}

.chart-box {
  position: relative;
  height: 300px;
  width: 100%;
}

.app-footer {
  padding: 16px 32px;
  border-top: 1px solid var(--border-hairline);
  color: var(--text-muted);
  font-size: 12px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  flex-wrap: wrap;
  gap: 8px;
}

.mobile-only { display: none; }

@media (max-width: 1200px) {
  .stats-grid { grid-template-columns: repeat(3, minmax(0, 1fr)); }
  .grid-2-1 { grid-template-columns: 1fr; }
}

@media (max-width: 900px) {
  .sidebar { transform: translateX(-100%); }
  .app-shell.mobile-nav-open .sidebar { transform: translateX(0); }
  .main-wrapper { margin-left: 0 !important; }
  .mobile-only { display: inline-flex; }
  .stats-grid { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .grid-2, .grid-3, .form-grid { grid-template-columns: 1fr; }
  .page-content { padding: 20px 16px; }
}
