:root {
  --bg: linear-gradient(180deg, #f3f5fa 0%, #ffffff 40%);
  --surface: #ffffff;
  --surface-muted: #f0f2f7;
  --text: #0f172a;
  --text-muted: #64748b;
  --border: #e2e8f0;
  --shadow: 0 18px 40px rgba(15, 23, 42, 0.08);
  --radius: 14px;
  --radius-lg: 20px;
  --spacing-1: 6px;
  --spacing-2: 10px;
  --spacing-3: 16px;
  --spacing-4: 24px;
  --spacing-5: 32px;
  --spacing-6: 40px;
  --font-sans: "Space Grotesk", "Segoe UI", system-ui, -apple-system, sans-serif;
  --font-body: "Source Sans 3", "Segoe UI", system-ui, -apple-system, sans-serif;
  --accent-planning: #4c7ff0;
  --accent-probate: #6b5b8a;
  --accent-contracts: #2f8a66;
}

* { box-sizing: border-box; }
html, body { margin: 0; padding: 0; }
.theme-dark {
  --bg: radial-gradient(circle at top, #0f172a 0%, #0b1220 45%, #070b14 100%);
  --surface: #0f172a;
  --surface-muted: #111827;
  --text: #e5e7eb;
  --text-muted: #94a3b8;
  --border: #1f2937;
  --shadow: 0 20px 45px rgba(0, 0, 0, 0.45);
  --accent-planning: #7aa6ff;
  --accent-probate: #9a7fb4;
  --accent-contracts: #4fb38a;
}

body {
  font-family: var(--font-body);
  color: var(--text);
  background: var(--bg);
}
a { color: inherit; }

.app-shell {
  min-height: 100vh;
  display: flex;
  flex-direction: column;
}

.app-header {
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  padding: var(--spacing-3) var(--spacing-5);
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: var(--spacing-3);
  position: sticky;
  top: 0;
  z-index: 20;
}

.brand {
  display: flex;
  align-items: center;
  gap: 10px;
  font-family: var(--font-sans);
  font-weight: 700;
  letter-spacing: -0.02em;
}

.brand-mark {
  width: 34px;
  height: 34px;
  border-radius: 12px;
  background: linear-gradient(135deg, #4c7ff0, #7aa6ff);
  display: inline-flex;
  align-items: center;
  justify-content: center;
  color: #fff;
  font-size: 16px;
}

.brand-logo {
  height: 100px;
  width: auto;
  max-width: 140px;
  border-radius: 10px;
  object-fit: contain;
  border: 1px solid var(--border);
  background: var(--surface);
  padding: 2px;
}

.user-chip {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  padding: 6px 12px;
  border-radius: 999px;
  background: var(--surface-muted);
  font-size: 13px;
  color: var(--text-muted);
}

.app-tabs {
  display: flex;
  gap: var(--spacing-3);
  padding: 8px var(--spacing-5);
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  overflow-x: auto;
}

.tab {
  position: relative;
  display: inline-flex;
  align-items: center;
  gap: 8px;
  padding: 10px 16px;
  border-radius: 999px;
  color: var(--text-muted);
  text-decoration: none;
  font-weight: 600;
  white-space: nowrap;
}

.tab::after {
  content: "";
  position: absolute;
  left: 16px;
  right: 16px;
  bottom: 4px;
  height: 3px;
  border-radius: 999px;
  opacity: 0;
}

.tab--active { color: var(--text); }
.tab--active::after { opacity: 1; }
.tab--planning::after { background: var(--accent-planning); }
.tab--probate::after { background: var(--accent-probate); }
.tab--contracts::after { background: var(--accent-contracts); }

.app-main {
  padding: var(--spacing-5);
}

.page {
  display: flex;
  flex-direction: column;
  gap: var(--spacing-4);
}

.page-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: var(--spacing-3);
  flex-wrap: wrap;
}

.page-title {
  font-family: var(--font-sans);
  font-size: 28px;
  margin: 0;
}

.page-subtitle { color: var(--text-muted); margin-top: 6px; }

.toolbar {
  display: flex;
  gap: var(--spacing-2);
  align-items: center;
  flex-wrap: wrap;
}

.btn {
  border: none;
  padding: 10px 16px;
  border-radius: 10px;
  background: #111827;
  color: #fff;
  font-weight: 600;
  cursor: pointer;
}

button {
  border: none;
  padding: 10px 16px;
  border-radius: 10px;
  background: #111827;
  color: #fff;
  font-weight: 600;
  cursor: pointer;
  font-family: inherit;
}

button.secondary {
  background: transparent;
  border: 1px solid var(--border);
  color: var(--text);
}

.btn--ghost {
  background: transparent;
  border: 1px solid var(--border);
  color: var(--text);
}

.btn--muted {
  background: var(--surface-muted);
  color: var(--text);
}

.btn-ghost {
  background: transparent;
  border: 1px solid var(--border);
  color: var(--text);
  padding: 10px 16px;
  border-radius: 10px;
  font-weight: 600;
  cursor: pointer;
  text-decoration: none;
}

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

.panel {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: var(--spacing-4);
  box-shadow: var(--shadow);
}

.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: var(--spacing-4);
  box-shadow: var(--shadow);
}

.card-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(260px, 1fr));
  gap: var(--spacing-3);
}

.lead-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: var(--spacing-3);
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.lead-card__title {
  font-weight: 700;
  font-size: 16px;
}

.meta {
  color: var(--text-muted);
  font-size: 13px;
}

.tag {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 4px 10px;
  border-radius: 999px;
  background: #eef2ff;
  color: #1e3a8a;
  font-size: 12px;
  font-weight: 600;
}

.table-wrap {
  border: 1px solid var(--border);
  border-radius: var(--radius);
  overflow: auto;
  background: var(--surface);
}

.table {
  width: 100%;
  border-collapse: collapse;
}

.table th, .table td {
  padding: 12px 14px;
  border-bottom: 1px solid var(--border);
  text-align: left;
  font-size: 13px;
}

.table th { background: #f8fafc; position: sticky; top: 0; z-index: 1; }

.input, select, textarea {
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 10px 12px;
  font-family: inherit;
  background: #fff;
}

.theme-dark .input,
.theme-dark select,
.theme-dark textarea {
  background: #0b1220;
  color: var(--text);
}

.theme-dark .table th { background: #0b1220; }
.theme-dark .table tr.is-clickable:hover { background: #0f172a; }
.theme-dark .tag { background: rgba(99, 102, 241, 0.18); color: #c7d2fe; }
.theme-dark .notice { background: rgba(99, 102, 241, 0.18); color: #c7d2fe; }
.theme-dark .badge { background: rgba(59, 130, 246, 0.18); color: #bfdbfe; }
.theme-dark .pill { background: rgba(148, 163, 184, 0.18); color: #e2e8f0; }

.support-widget {
  position: fixed;
  right: 24px;
  bottom: 24px;
  z-index: 9998;
}

.support-toggle {
  background: #111827;
  color: #fff;
  border-radius: 999px;
  padding: 12px 16px;
  font-weight: 700;
  box-shadow: var(--shadow);
}

.support-panel {
  width: 320px;
  max-height: 60vh;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 16px;
  box-shadow: var(--shadow);
  margin-bottom: 12px;
  display: flex;
  flex-direction: column;
}

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

.support-messages {
  padding: 12px 14px;
  overflow: auto;
  display: grid;
  gap: 10px;
  font-size: 13px;
}

.support-form {
  display: flex;
  gap: 8px;
  padding: 12px 14px;
  border-top: 1px solid var(--border);
}

.support-form .input {
  flex: 1;
}

.support-bubble {
  background: var(--surface-muted);
  border-radius: 12px;
  padding: 10px 12px;
  line-height: 1.4;
}

.support-bubble--user {
  background: rgba(17, 24, 39, 0.08);
}

.theme-dark .support-toggle { background: #0b1220; }
.theme-dark .support-bubble--user { background: rgba(148, 163, 184, 0.2); }

form.filters { display: none; }
form.filters.is-open { display: block; }
.filters-grid,
form.filters > .filters-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
  gap: var(--spacing-3);
}

.filter-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
  gap: var(--spacing-3);
}

.row {
  display: flex;
  gap: var(--spacing-3);
  flex-wrap: wrap;
}

.row > * {
  flex: 1;
  min-width: 0;
}

.section-title {
  font-family: var(--font-sans);
  margin-top: 0;
}

.empty {
  text-align: center;
  padding: var(--spacing-5);
  color: var(--text-muted);
}

.notice {
  padding: 12px 16px;
  border-radius: 12px;
  background: #eef2ff;
  color: #1e3a8a;
  font-weight: 600;
}

.drawer-like {
  max-width: 720px;
  margin-left: auto;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  padding: var(--spacing-5);
  box-shadow: var(--shadow);
}

.modal-overlay {
  display: none;
  position: fixed;
  inset: 0;
  background: rgba(15, 23, 42, 0.45);
  z-index: 9999;
  align-items: center;
  justify-content: center;
  padding: 20px;
}

.modal-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 18px;
  width: 520px;
  max-width: 95%;
  max-height: 90vh;
  overflow: auto;
  box-shadow: var(--shadow);
}

.modal-card.wide { width: 820px; }

.letter-sheet { background: #fff; color: #111827; border: 1px solid #e5e7eb; border-radius: 12px; padding: 28px 32px; font-size: 14px; line-height: 1.6; box-shadow: 0 18px 40px rgba(15, 23, 42, 0.12); }
.letter-header { display: flex; justify-content: space-between; gap: 16px; align-items: flex-start; }
.letter-logo { width: 120px; height: 80px; object-fit: contain; border: 1px solid #e5e7eb; border-radius: 10px; background: #f8fafc; padding: 6px; }
.letter-meta { text-align: right; font-size: 12px; color: #475569; }
.letter-title { font-size: 16px; font-weight: 700; margin-top: 18px; }
.letter-section { margin-top: 16px; }
.letter-address-block { margin-top: 22mm; margin-left: 20mm; width: 85mm; font-size: 13px; }
.letter-body { margin-top: 18mm; }
.letter-signoff { margin-top: 20px; }
.letter-footer { margin-top: 24px; padding-top: 10px; border-top: 1px solid #e5e7eb; font-size: 11px; color: #6b7280; display: flex; justify-content: space-between; gap: 12px; flex-wrap: wrap; }

@media print {
  body { background: #fff; color: #111827; }
  .app-header, .app-tabs, .btn, .btn--ghost, .btn--muted, .btn-ghost { display: none !important; }
  body.print-letter .app-main > * { display: none !important; }
  body.print-letter #letterModal { display: block !important; position: static !important; background: none !important; padding: 0 !important; }
  body.print-letter #letterCard { box-shadow: none !important; border: none !important; padding: 0 !important; width: auto !important; }
  body.print-letter .support-widget { display: none !important; }
  body.print-letter .letter-ui-header { display: none !important; }
  .modal-overlay { display: none !important; }
  .modal-overlay.printing { display: block !important; position: static; background: none; padding: 0; }
  .modal-card.printing { box-shadow: none; border: none; padding: 0; width: auto; }
  .letter-sheet { border: none; box-shadow: none; padding: 0; }
}

.module-accent--planning { color: var(--accent-planning); }
.module-accent--probate { color: var(--accent-probate); }
.module-accent--contracts { color: var(--accent-contracts); }

.auth-shell {
  min-height: 100vh;
  display: grid;
  grid-template-columns: minmax(320px, 1fr) minmax(320px, 520px);
  gap: var(--spacing-6);
  align-items: center;
}

.auth-hero {
  padding: var(--spacing-6);
}

.login-logo-wrap {
  display: flex;
  align-items: center;
  justify-content: flex-start;
}

.login-logo {
  width: min(66vw, 720px);
  max-width: 100%;
  height: auto;
  border: none;
  background: transparent;
  box-shadow: none;
}

.auth-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  padding: var(--spacing-6);
  box-shadow: var(--shadow);
}

.auth-card h1 { margin-top: 0; font-family: var(--font-sans); }

.alert {
  padding: 12px 16px;
  border-radius: 12px;
  font-weight: 600;
  margin-bottom: var(--spacing-3);
}

.alert--error { background: #fee2e2; color: #991b1b; }
.alert--success { background: #dcfce7; color: #166534; }

.badge {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 4px 10px;
  border-radius: 999px;
  background: rgba(30, 58, 138, 0.12);
  color: #1e3a8a;
  font-size: 12px;
  font-weight: 700;
}

.pill {
  display: inline-flex;
  align-items: center;
  padding: 6px 10px;
  border-radius: 999px;
  background: rgba(15, 23, 42, 0.06);
  color: var(--text-muted);
  font-size: 12px;
}

.letter-pill { background: #eef2ff; color: #1e3a8a; }

.table tr.is-clickable { cursor: pointer; }
.table tr.is-clickable:hover { background: #f8fafc; }

.insight-list {
  list-style: none;
  padding: 0;
  margin: 12px 0 0;
  display: grid;
  gap: 8px;
}

.insight-list li {
  background: #f8fafc;
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 10px 12px;
  font-size: 13px;
  color: var(--text);
}

@media (max-width: 900px) {
  .app-main { padding: var(--spacing-3); }
  .app-header, .app-tabs { padding-left: var(--spacing-3); padding-right: var(--spacing-3); }
  .page-title { font-size: 24px; }
  .auth-shell { grid-template-columns: 1fr; }
}
