@import './core.css';

:root {
  --green: #34a853;
  --yellow: #fbbc04;
  --red: #ea4335;
  --orange: #fa7b17;
}

*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }
body { background: var(--bg0); color: var(--text1); min-height: 100vh; font-size: 14px; }

/* ── Layout ──────────────────────────────────────────────────── */
.app { display: none; min-height: 100vh; }
.app.active { display: flex; }

.sidebar {
  width: 240px; background: var(--bg1); border-right: 1px solid var(--border);
  display: flex; flex-direction: column; position: fixed; inset: 0 auto 0 0; z-index: 50;
  overflow: hidden;
  transition: width 0.35s cubic-bezier(0.4, 0, 0.2, 1);
}
.wm-z { display: inline-block; transform: skewX(-8deg); }
.brand-text { font-family: 'Satoshi', 'Geist', sans-serif; font-weight: 700; letter-spacing: 0.5px; }
.sidebar-brand {
  padding: 18px 20px; font-size: 16px; font-weight: 700; letter-spacing: -0.3px;
  border-bottom: 1px solid var(--border); display: flex; align-items: center; gap: 10px;
  position: relative; flex-shrink: 0;
}
.sidebar-brand svg { width: 48px; height: 24px; flex-shrink: 0; }
.brand-text {
  white-space: nowrap;
  opacity: 1;
  max-width: 200px;
  transition: opacity 0.25s ease, max-width 0.35s cubic-bezier(0.4, 0, 0.2, 1);
}
.sidebar-toggle {
  display: none; margin-left: auto; padding: 4px; border-radius: 6px; border: 1px solid var(--border-hi);
  background: var(--bg2); color: var(--text3); cursor: pointer; transition: all .15s;
  flex-shrink: 0;
}
.sidebar-toggle:hover { background: var(--bg3); color: var(--text1); }
.sidebar-toggle svg {
  width: 14px; height: 14px;
  transition: transform 0.35s cubic-bezier(0.4, 0, 0.2, 1);
}

.sidebar-nav { flex: 1; padding: 12px; display: flex; flex-direction: column; gap: 4px; }
.nav-label {
  white-space: nowrap;
  opacity: 1;
  max-width: 200px;
  transition: opacity 0.2s ease, max-width 0.35s cubic-bezier(0.4, 0, 0.2, 1);
}
.sidebar-item {
  display: flex; align-items: center; gap: 10px;
  padding: 9px 12px; border-radius: var(--radius-sm);
  color: var(--text2); font-size: 13px; font-weight: 500;
  cursor: pointer; transition: background .15s, color .15s, padding .35s cubic-bezier(0.4, 0, 0.2, 1), justify-content .35s; border: none; background: transparent; width: 100%;
  position: relative;
}
.sidebar-item:hover { background: var(--bg2); color: var(--text1); }
.sidebar-item.active {
  background: var(--accent);
  color: #fff;
}
/* Active indicator bar */
.sidebar-item.active::before {
  content: '';
  position: absolute;
  left: 0; top: 4px; bottom: 4px;
  width: 3px;
  background: #fff;
  border-radius: 0 3px 3px 0;
  opacity: 0.5;
}
.sidebar-item svg { width: 18px; height: 18px; flex-shrink: 0; }

/* Sidebar dividers */
.sidebar-divider {
  height: 1px;
  background: var(--border);
  margin: 6px 12px;
  flex-shrink: 0;
}
.sidebar-footer {
  padding: 12px; border-top: 1px solid var(--border);
  display: flex; align-items: center; gap: 10px;
  transition: flex-direction 0.35s, padding 0.35s, gap 0.35s;
}
.sidebar-avatar {
  width: 32px; height: 32px; border-radius: 50%; background: var(--bg4);
  background-size: cover; background-position: center; flex-shrink: 0;
}
.sidebar-user {
  flex: 1; min-width: 0;
  opacity: 1;
  max-width: 200px;
  transition: opacity 0.2s ease, max-width 0.35s cubic-bezier(0.4, 0, 0.2, 1);
}
.sidebar-user-name { font-size: 12px; font-weight: 600; color: var(--text1); }
.sidebar-user-role { font-size: 11px; color: var(--text3); text-transform: capitalize; }
.sidebar-logout {
  padding: 6px; border-radius: 6px; border: none; background: transparent;
  color: var(--text3); cursor: pointer; transition: color .15s, margin .35s cubic-bezier(0.4, 0, 0.2, 1);
  flex-shrink: 0;
}
.sidebar-logout:hover { color: var(--red); }

.main {
  flex: 1; margin-left: 240px; min-height: 100vh;
  display: flex; flex-direction: column;
  transition: margin-left 0.35s cubic-bezier(0.4, 0, 0.2, 1);
}

/* ── Sidebar collapsed (desktop toggle) ──────────────────────── */
.sidebar.collapsed {
  width: 64px;
}
.sidebar.collapsed .sidebar-brand {
  padding: 18px 6px;
  justify-content: center;
  position: relative;
}
.sidebar.collapsed .brand-text {
  opacity: 0;
  max-width: 0;
  overflow: hidden;
}
.sidebar.collapsed .sidebar-toggle {
  position: absolute;
  right: 4px;
  top: 50%;
  transform: translateY(-50%);
  margin: 0;
  display: inline-flex;
  background: rgba(17, 17, 19, 0.18);
  backdrop-filter: blur(6px);
  -webkit-backdrop-filter: blur(6px);
  border-color: rgba(46, 46, 56, 0.35);
  box-shadow: 0 2px 8px rgba(0,0,0,0.15);
}
.sidebar.collapsed .sidebar-toggle:hover {
  background: rgba(17, 17, 19, 0.85);
  border-color: var(--border-hi);
  color: var(--text1);
}
.sidebar.collapsed .sidebar-toggle svg {
  transform: rotate(180deg);
}
.sidebar.collapsed .nav-label {
  opacity: 0;
  max-width: 0;
  overflow: hidden;
  position: absolute;
  pointer-events: none;
}
.sidebar.collapsed .sidebar-item {
  justify-content: center;
  padding: 10px;
  gap: 0;
}
.sidebar.collapsed .sidebar-item.active::before {
  top: 2px; bottom: 2px;
}
.sidebar.collapsed .sidebar-divider {
  margin: 6px 8px;
  opacity: 0.5;
}
.sidebar.collapsed .sidebar-footer {
  flex-direction: column;
  gap: 8px;
  padding: 12px 0;
}
.sidebar.collapsed .sidebar-user {
  opacity: 0;
  max-width: 0;
}
.sidebar.collapsed .sidebar-logout {
  margin: 0 auto;
}

.sidebar.collapsed ~ .main {
  margin-left: 64px;
}
.main-header {
  background: var(--bg1); border-bottom: 1px solid var(--border);
  padding: 14px 28px; display: flex; align-items: center; gap: 16px;
  position: sticky; top: 0; z-index: 40; flex-wrap: wrap;
}
.main-header h1 { font-size: 18px; font-weight: 700; flex: 1; min-width: 0; }

.menu-toggle {
  display: none; padding: 6px; border-radius: 6px; border: 1px solid var(--border-hi);
  background: var(--bg2); color: var(--text2); cursor: pointer;
}
.menu-toggle svg { width: 18px; height: 18px; }
.header-meta { font-size: 12px; color: var(--text3); }
.header-btn {
  padding: 6px 14px; border-radius: 6px; border: 1px solid var(--border-hi);
  background: var(--bg2); color: var(--text2); font-size: 12px; font-weight: 500;
  cursor: pointer; transition: all .15s;
}
.header-btn:hover { background: var(--bg3); color: var(--text1); }

.main-body { padding: 24px 28px; flex: 1; }

/* ── Sections ────────────────────────────────────────────────── */
.section { display: none; }
.section.active { display: block; }

/* ── Stats ───────────────────────────────────────────────────── */
.stats-grid {
  display: grid; grid-template-columns: repeat(auto-fill, minmax(180px, 1fr)); gap: 14px;
  margin-bottom: 28px;
}
.stat-card {
  background: var(--bg1); border: 1px solid var(--border); border-radius: var(--radius);
  padding: 18px 20px; position: relative; overflow: hidden;
}
.stat-card::before {
  content: ''; position: absolute; top: 0; left: 0; right: 0; height: 2px;
}
.stat-card.accent::before { background: var(--accent); }
.stat-card.green::before  { background: var(--green); }
.stat-card.yellow::before { background: var(--yellow); }
.stat-card.red::before    { background: var(--red); }
.stat-card.orange::before { background: var(--orange); }
.stat-card.blue::before   { background: var(--color-blue); }
.stat-card.purple::before { background: var(--color-purple); }
.stat-label {
  font-size: 11px; color: var(--text3); text-transform: uppercase; letter-spacing: .6px;
  font-weight: 600;
}
.stat-value {
  font-size: 32px; font-weight: 700; margin-top: 6px; letter-spacing: -1px;
}
.stat-delta {
  font-size: 11px; color: var(--text3); margin-top: 4px;
}

/* ── Charts ──────────────────────────────────────────────────── */
.chart-grid {
  display: grid; grid-template-columns: repeat(2, 1fr); gap: 16px; margin-bottom: 28px;
}
.chart-card {
  background: var(--bg1); border: 1px solid var(--border); border-radius: var(--radius);
  padding: 20px;
}
.chart-header {
  display: flex; align-items: center; justify-content: space-between; margin-bottom: 16px;
}
.chart-title { font-size: 14px; font-weight: 600; }
.chart-subtitle { font-size: 12px; color: var(--text3); }
.chart-body { min-height: 220px; position: relative; }
.chart-body svg { display: block; width: 100%; }

/* D3 styles */
.axis line, .axis path { stroke: var(--border-hi); }
.axis text { fill: var(--text3); font-size: 11px; }
.grid line { stroke: var(--border); stroke-opacity: .5; }
.grid path { stroke: none; }
.tooltip {
  position: absolute; pointer-events: none;
  background: var(--bg2); border: 1px solid var(--border-hi);
  border-radius: 6px; padding: 6px 10px; font-size: 12px; color: var(--text1);
  box-shadow: 0 4px 16px rgba(0,0,0,.4); opacity: 0; transition: opacity .15s;
  z-index: 100; white-space: nowrap;
}

/* ── Filters ─────────────────────────────────────────────────── */
.filters { display: flex; gap: 8px; margin-bottom: 16px; flex-wrap: wrap; align-items: center; }
.filter-btn {
  padding: 5px 14px; border-radius: 20px; font-size: 12px; font-weight: 500;
  background: var(--bg2); color: var(--text2); border: 1px solid var(--border);
  cursor: pointer; transition: all .15s;
}
.filter-btn.active { background: var(--accent); color: #fff; border-color: var(--accent); }

.search-input {
  padding: 7px 12px; border-radius: 6px; border: 1px solid var(--border-hi);
  background: var(--bg2); color: var(--text1); font-size: 13px; outline: none;
  min-width: 200px;
}
.search-input:focus { border-color: var(--accent); }
.search-input::placeholder { color: var(--text4); }

.owner-search-wrap {
  position: relative;
}
.owner-search-dropdown {
  position: absolute;
  top: calc(100% + 4px);
  left: 0;
  right: 0;
  z-index: 50;
  background: var(--bg1);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  box-shadow: 0 8px 24px rgba(0,0,0,.4);
  overflow: hidden;
}
.owner-search-item {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 8px 12px;
  cursor: pointer;
  transition: background .1s;
  border-bottom: 1px solid var(--border);
}
.owner-search-item:last-child { border-bottom: none; }
.owner-search-item:hover { background: var(--bg2); }
.owner-search-item .avatar { width: 24px; height: 24px; }
.owner-search-item .cell-name { font-size: 13px; }
.owner-search-item .cell-email { font-size: 11px; }
.owner-search-empty {
  padding: 10px 12px;
  font-size: 12px;
  color: var(--text3);
}

.form-select {
  padding: 7px 26px 7px 12px; border-radius: 6px; border: 1px solid var(--border-hi);
  background: var(--bg2); color: var(--text1); font-size: 13px; outline: none;
  min-width: 120px; cursor: pointer;
  appearance: none; -webkit-appearance: none;
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='12' height='12' viewBox='0 0 24 24' fill='none' stroke='%238B949E' stroke-width='2' stroke-linecap='round' stroke-linejoin='round'%3E%3Cpath d='m6 9 6 6 6-6'/%3E%3C/svg%3E");
  background-repeat: no-repeat;
  background-position: right 12px center;
}
.form-select:focus { border-color: var(--accent); }
.detail-panel .form-select { background-color: var(--bg0); }

.form-input {
  padding: 7px 12px; border-radius: 6px; border: 1px solid var(--border-hi);
  background: var(--bg2); color: var(--text1); font-size: 13px; outline: none;
  min-width: 120px; width: 100%; box-sizing: border-box;
}
.form-input:focus { border-color: var(--accent); }
.detail-panel .form-input { background-color: var(--bg0); }

/* ── Tables ──────────────────────────────────────────────────── */
.table-wrap { overflow-x: auto; border: 1px solid var(--border); border-radius: var(--radius); }
table { width: 100%; border-collapse: collapse; font-size: 13px; }
th {
  text-align: left; padding: 10px 14px; color: var(--text3); font-size: 11px;
  text-transform: uppercase; letter-spacing: .5px; border-bottom: 1px solid var(--border);
  background: var(--bg2); font-weight: 600; white-space: nowrap;
}
td { padding: 10px 14px; border-bottom: 1px solid var(--border); vertical-align: middle; }
tr:last-child td { border-bottom: none; }
tr:hover td { background: rgba(255,255,255,.02); }

.avatar {
  width: 28px; height: 28px; border-radius: 50%; background: var(--bg4);
  background-size: cover; background-position: center; flex-shrink: 0;
}
.cell-user { display: flex; align-items: center; gap: 10px; }
.cell-name  { font-weight: 500; }
.cell-email { font-size: 11px; color: var(--text3); }
.cell-date  { color: var(--text3); font-size: 12px; }

.project-name { font-weight: 500; }
.project-description { font-size: 11px; color: var(--text3); margin-top: 4px; line-height: 1.4; }
.project-tags { display: flex; flex-wrap: wrap; gap: 4px; margin-top: 6px; }
.project-tag { font-size: 10px; color: var(--accent); background: var(--accent-dim); padding: 1px 6px; border-radius: var(--radius-sm); text-transform: lowercase; }

.badge {
  display: inline-block; padding: 2px 8px; border-radius: 10px;
  font-size: 11px; font-weight: 600;
}
.badge-pending { background: #fbbc0422; color: var(--yellow); }
.badge-active  { background: #22c55e22; color: var(--green); }
.badge-lifetime{ background: #8B5CF622; color: var(--accent); }
.badge-beta    { background: #6366F122; color: var(--accent); }
.badge-pro     { background: #34a85322; color: var(--green); }
.badge-banned  { background: #ea433522; color: var(--red); }

/* AI Status badges */
.ai-status-success {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  padding: 3px 10px;
  border-radius: 20px;
  font-size: 11px;
  font-weight: 600;
  background: rgba(34, 197, 94, 0.12);
  color: #4ade80;
  border: 1px solid rgba(34, 197, 94, 0.25);
}
.ai-status-success::before {
  content: "";
  display: block;
  width: 6px;
  height: 6px;
  border-radius: 50%;
  background: #4ade80;
}

.ai-status-failed {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  padding: 3px 10px;
  border-radius: 20px;
  font-size: 11px;
  font-weight: 600;
  background: rgba(239, 68, 68, 0.12);
  color: #f87171;
  border: 1px solid rgba(239, 68, 68, 0.25);
}
.ai-status-failed::before {
  content: "";
  display: block;
  width: 6px;
  height: 6px;
  border-radius: 50%;
  background: #f87171;
}

.actions { display: flex; gap: 6px; }
.btn {
  cursor: pointer; border: none; border-radius: 6px;
  font-size: 12px; font-weight: 500; transition: opacity .15s;
}
.btn:hover { opacity: .85; }
.btn-sm { padding: 4px 10px; font-size: 11px; }
.btn-primary { background: var(--accent); color: #fff; }
.btn-green   { background: var(--green);  color: #fff; }
.btn-yellow  { background: var(--yellow); color: #111; }
.btn-red     { background: var(--red);    color: #fff; }
.btn-gray    { background: var(--bg4);    color: var(--text2); }
.btn-ghost   { background: transparent;   color: var(--text3); border: 1px solid var(--border-hi); }
.btn-ghost.danger { color: var(--red); border-color: rgba(239,68,68,.35); }
.btn-ghost.danger:hover { background: rgba(239,68,68,.10); }
.btn-icon {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 28px;
  height: 28px;
  padding: 0;
  border-radius: 6px;
}
.btn-icon svg {
  width: 14px;
  height: 14px;
}

/* AI Generation cell content */
.ai-cell-content {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.ai-error-msg {
  font-size: 12px;
  color: var(--text3);
  line-height: 1.5;
}

.ai-empty-msg {
  font-size: 12px;
  color: var(--text3);
  font-style: italic;
}

/* AI Generation action buttons */
.ai-actions {
  display: flex;
  gap: 6px;
  flex-wrap: wrap;
  align-items: center;
}
.ai-btn-raw {
  background: transparent;
  color: var(--text3);
  border: 1px solid var(--border);
  padding: 4px 10px;
  border-radius: 6px;
  font-size: 11px;
  font-weight: 500;
  cursor: pointer;
  transition: all .15s;
  display: inline-flex;
  align-items: center;
  gap: 4px;
}
.ai-btn-raw:hover {
  background: var(--bg3);
  color: var(--text2);
  border-color: var(--border-hi);
}

.ai-btn-snapshot {
  background: linear-gradient(135deg, #8B5CF6, #3B82F6);
  color: #fff;
  border: none;
  padding: 4px 10px;
  border-radius: 6px;
  font-size: 11px;
  font-weight: 500;
  cursor: pointer;
  transition: opacity .15s;
  display: inline-flex;
  align-items: center;
  gap: 4px;
}
.ai-btn-snapshot:hover { opacity: .85; }

.ai-source-badge {
  display: inline-flex;
  align-items: center;
  padding: 2px 8px;
  border-radius: 20px;
  font-size: 10px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.03em;
  background: var(--bg3);
  color: var(--text3);
  border: 1px solid var(--border);
}
.ai-source-chat {
  background: rgba(59, 130, 246, 0.12);
  color: #60a5fa;
  border-color: rgba(59, 130, 246, 0.25);
}
.ai-source-bootstrap {
  background: rgba(139, 92, 246, 0.12);
  color: #a78bfa;
  border-color: rgba(139, 92, 246, 0.25);
}

.ai-error-code {
  display: block;
  margin-top: 4px;
  font-size: 10px;
  font-weight: 600;
  font-family: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, monospace;
  color: #f87171;
}

.ai-btn-context {
  background: transparent;
  color: #f87171;
  border: 1px solid rgba(239, 68, 68, 0.35);
  padding: 4px 10px;
  border-radius: 6px;
  font-size: 11px;
  font-weight: 500;
  cursor: pointer;
  transition: all .15s;
  display: inline-flex;
  align-items: center;
  gap: 4px;
}
.ai-btn-context:hover {
  background: rgba(239, 68, 68, 0.10);
}

.link-user   { background: transparent;   color: var(--accent); border: none; padding: 0; font-size: inherit; cursor: pointer; text-decoration: underline; }
.user-link    { cursor: pointer; text-decoration: none; color: inherit; }
.user-link:hover .cell-name { color: var(--accent); }
.user-link:hover { background: rgba(255,255,255,.03); border-radius: 6px; }

.empty { padding: 40px; text-align: center; color: var(--text3); font-size: 13px; }

/* ── Pagination ──────────────────────────────────────────────── */
.pagination {
  display: flex; align-items: center; justify-content: space-between;
  padding: 14px; border-top: 1px solid var(--border); background: var(--bg2);
}
.pagination-info { font-size: 12px; color: var(--text3); }
.pagination-btns { display: flex; gap: 6px; }

.pg-btn {
  padding: 5px 10px; border-radius: 6px; border: 1px solid var(--border-hi);
  background: var(--bg3); color: var(--text2); font-size: 12px; font-weight: 500;
  cursor: pointer; transition: all .15s;
}
.pg-btn:hover:not(:disabled) { background: var(--bg4); color: var(--text1); }
.pg-btn.active { background: var(--accent); color: #fff; border-color: var(--accent); }
.pg-btn:disabled { opacity: .4; cursor: not-allowed; }
.pg-dots { color: var(--text3); font-size: 12px; padding: 0 4px; }
.pg-info { font-size: 12px; color: var(--text2); font-weight: 500; }

/* ── Login ───────────────────────────────────────────────────── */
.login-screen {
  display: flex; align-items: center; justify-content: center;
  min-height: 100vh; background: var(--bg0);
}
.login-card {
  background: var(--bg1); border: 1px solid var(--border); border-radius: var(--radius);
  padding: 40px 44px; width: 100%; max-width: 380px; display: flex; flex-direction: column; gap: 16px;
}
.login-card h1 { font-size: 20px; font-weight: 700; }
.login-card p  { font-size: 13px; color: var(--text3); }
.login-input {
  width: 100%; padding: 10px 12px; background: var(--bg2); border: 1px solid var(--border-hi);
  border-radius: 7px; color: var(--text1); font-size: 14px; outline: none;
}
.login-input:focus { border-color: var(--accent); }
.login-input::placeholder { color: var(--text4); }
.login-btn {
  width: 100%; padding: 10px; border-radius: 7px; border: none;
  background: var(--accent); color: #fff; font-size: 14px; font-weight: 600;
  cursor: pointer; transition: background .15s;
}
.login-btn:hover { background: var(--accent-dark); }
.login-err { color: var(--red); font-size: 13px; min-height: 18px; }

/* ── Detail panel ────────────────────────────────────────────── */
.detail-panel {
  background: var(--bg1); border: 1px solid var(--border); border-radius: var(--radius);
  padding: 20px; margin-bottom: 20px;
}
.detail-header {
  display: flex; align-items: center; gap: 14px; margin-bottom: 16px;
}
.detail-avatar {
  width: 48px; height: 48px; border-radius: 50%; background: var(--bg4);
  background-size: cover; background-position: center; flex-shrink: 0;
}
.detail-info h3 { font-size: 16px; font-weight: 600; }
.detail-info p { font-size: 12px; color: var(--text3); margin-top: 2px; }
.detail-action {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  margin-top: 6px;
  padding: 0;
  border: none;
  background: transparent;
  color: var(--accent);
  font-size: 12px;
  font-weight: 500;
  cursor: pointer;
  transition: color .15s;
}
.detail-action:hover { color: #79c0ff; }
.detail-action svg { width: 12px; height: 12px; }

/* ── Data cards (logs tables) ───────────────────────────────── */
.data-card {
  background: var(--bg1); border: 1px solid var(--border); border-radius: var(--radius);
}
.data-header {
  display: flex; align-items: center; gap: 12px;
  padding: 14px 16px; border-bottom: 1px solid var(--border);
}
.data-header h3 { font-size: 14px; font-weight: 600; }
.data-header .data-meta {
  font-size: 12px; color: var(--text3); margin-left: auto;
}
.data-header .btn { margin-left: 8px; }

/* User detail drawer */
#user-detail {
  position: fixed;
  top: 0; right: 0; bottom: 0;
  width: 380px; max-width: 90vw;
  border-left: 1px solid var(--border);
  border-radius: 0;
  margin-bottom: 0;
  z-index: 60;
  overflow-y: auto;
  transform: translateX(100%);
  transition: transform .25s ease;
  pointer-events: none;
}
#user-detail.open {
  transform: translateX(0);
  pointer-events: auto;
}
#user-detail-backdrop {
  position: fixed; inset: 0;
  background: rgba(0,0,0,.4);
  z-index: 55;
  opacity: 0;
  pointer-events: none;
  transition: opacity .25s ease;
}
#user-detail-backdrop.open {
  opacity: 1;
  pointer-events: auto;
}

/* Lead notes drawer */
#lead-notes-panel {
  position: fixed;
  top: 0; right: 0; bottom: 0;
  width: 380px; max-width: 90vw;
  border-left: 1px solid var(--border);
  border-radius: 0;
  margin-bottom: 0;
  z-index: 60;
  overflow: hidden;
  transform: translateX(100%);
  transition: transform .25s ease;
  pointer-events: none;
  display: flex;
  flex-direction: column;
}
#lead-notes-panel.open {
  transform: translateX(0);
  pointer-events: auto;
}
#lead-notes-backdrop {
  position: fixed; inset: 0;
  background: rgba(0,0,0,.4);
  z-index: 55;
  opacity: 0;
  pointer-events: none;
  transition: opacity .25s ease;
}
#lead-notes-backdrop.open {
  opacity: 1;
  pointer-events: auto;
}
#lead-notes-panel .detail-header {
  flex-shrink: 0;
}
#lead-notes-panel #lnp-form {
  flex-shrink: 0;
  background: var(--bg1);
}

/* AI raw response drawer */
#ai-raw-panel {
  position: fixed;
  top: 0; right: 0; bottom: 0;
  width: 520px; max-width: 90vw;
  border-left: 1px solid var(--border);
  border-radius: 0;
  margin-bottom: 0;
  z-index: 60;
  overflow: hidden;
  transform: translateX(100%);
  transition: transform .25s ease;
  pointer-events: none;
  display: flex;
  flex-direction: column;
}
#ai-raw-panel.open {
  transform: translateX(0);
  pointer-events: auto;
}
#ai-raw-backdrop {
  position: fixed; inset: 0;
  background: rgba(0,0,0,.4);
  z-index: 55;
  opacity: 0;
  pointer-events: none;
  transition: opacity .25s ease;
}
#ai-raw-backdrop.open {
  opacity: 1;
  pointer-events: auto;
}
#ai-raw-panel .detail-header {
  flex-shrink: 0;
}

/* Support ticket detail drawer */
#support-detail-panel {
  position: fixed;
  top: 0; right: 0; bottom: 0;
  width: 480px; max-width: 90vw;
  border-left: 1px solid var(--border);
  border-radius: 0;
  margin-bottom: 0;
  z-index: 60;
  overflow: hidden;
  transform: translateX(100%);
  transition: transform .25s ease;
  pointer-events: none;
  display: flex;
  flex-direction: column;
}
#support-detail-panel.open {
  transform: translateX(0);
  pointer-events: auto;
}
#support-detail-backdrop {
  position: fixed; inset: 0;
  background: rgba(0,0,0,.4);
  z-index: 55;
  opacity: 0;
  pointer-events: none;
  transition: opacity .25s ease;
}
#support-detail-backdrop.open {
  opacity: 1;
  pointer-events: auto;
}
#support-detail-panel .detail-header {
  flex-shrink: 0;
}
#support-detail-panel .sd-field label {
  display: block;
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: .04em;
  color: var(--text3);
  margin-bottom: 4px;
}
#support-detail-panel .sd-field div,
#support-detail-panel .sd-section label {
  font-size: 13px;
  color: var(--text1);
  line-height: 1.5;
}
#support-detail-panel .sd-section label {
  display: block;
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: .04em;
  color: var(--text3);
  margin-bottom: 8px;
}
#support-detail-panel .sd-section {
  border-top: 1px solid var(--border);
  padding-top: 16px;
}
#support-detail-panel .sd-reply {
  background: var(--bg2);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 10px 12px;
}
#support-detail-panel .sd-reply-time {
  font-size: 11px;
  color: var(--text3);
  margin-bottom: 4px;
}
#support-detail-panel .sd-reply-text {
  font-size: 13px;
  color: var(--text1);
  line-height: 1.5;
}
#support-detail-panel #sd-attachment-link {
  display: block;
  border-radius: 8px;
  overflow: hidden;
  border: 1px solid var(--border);
  transition: border-color .15s;
}
#support-detail-panel #sd-attachment-link:hover {
  border-color: var(--accent);
}
#support-detail-panel #sd-attachment-img {
  display: block;
  width: 100%;
  max-height: 240px;
  object-fit: cover;
}
  white-space: pre-wrap;
  word-break: break-word;
}

/* ── CRM sub-sections ──────────────────────────────────────── */
.crm-sub { display: block; }

/* ── Template / Campaign editor ────────────────────────────── */
.detail-panel input, .detail-panel textarea, .detail-panel select {
  background: var(--bg0);
  border: 1px solid var(--border-hi);
  color: var(--text1);
}
.detail-panel select option {
  background: var(--bg0);
  color: var(--text1);
}

/* ── Responsive ──────────────────────────────────────────────── */
@media (min-width: 901px) {
  .sidebar-toggle { display: inline-flex; }
}
@media (max-width: 900px) {
  .chart-grid { grid-template-columns: 1fr; }
}
@media (max-width: 900px) and (min-width: 681px) {
  .sidebar { width: 64px; }
  .sidebar .brand-text { opacity: 0; max-width: 0; }
  .sidebar .sidebar-toggle { display: none; }
  .sidebar .nav-label { opacity: 0; max-width: 0; }
  .sidebar .sidebar-item { justify-content: center; padding: 10px; }
  .sidebar .sidebar-footer { flex-direction: column; gap: 8px; padding: 12px 0; }
  .sidebar .sidebar-user { opacity: 0; max-width: 0; }
  .sidebar .sidebar-logout { margin: 0 auto; }
  .main { margin-left: 64px; }
}
@media (max-width: 680px) {
  .sidebar { width: 240px; transform: translateX(-100%); transition: transform .35s cubic-bezier(0.4, 0, 0.2, 1), width .35s cubic-bezier(0.4, 0, 0.2, 1); }
  .sidebar.open { transform: translateX(0); }
  .sidebar .brand-text { opacity: 1; max-width: 200px; }
  .sidebar .sidebar-toggle { display: none; }
  .sidebar .nav-label { opacity: 1; max-width: 200px; }
  .sidebar .sidebar-item { justify-content: flex-start; padding: 9px 12px; }
  .sidebar .sidebar-footer { flex-direction: row; padding: 12px; }
  .sidebar .sidebar-user { opacity: 1; max-width: 200px; }
  .sidebar .sidebar-logout { margin: 0; }
  .app.active { display: block; }
  .main { margin-left: 0; min-height: auto; display: block; }
  .main-body { flex: none; }
  .menu-toggle { display: inline-flex; }
  .main-header { padding: 12px 16px; gap: 10px; }
  .main-body { padding: 16px; }
  .stats-grid { grid-template-columns: repeat(2, 1fr); gap: 10px; margin-bottom: 20px; }
  .stat-card { padding: 12px 14px; }
  .stat-value { font-size: 22px; }
  .stat-label { font-size: 10px; letter-spacing: .4px; }
  .login-card { padding: 32px 24px; margin: 0 16px; }
  .search-input { min-width: 0; flex: 1; }

  /* Filters horizontal scroll */
  .filters {
    flex-wrap: nowrap;
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
    padding-bottom: 4px;
    scrollbar-width: none;
  }
  .filters::-webkit-scrollbar { display: none; }
  .filter-btn { white-space: nowrap; flex-shrink: 0; }
  .search-input { flex-shrink: 0; }

  /* Compact badges & buttons */
  .badge { padding: 1px 6px; font-size: 10px; }
  .btn-sm { padding: 3px 8px; font-size: 10px; }
  .actions { gap: 4px; }

  /* Responsive charts */
  .chart-body { min-height: 160px; }
  .chart-body svg { width: 100%; height: auto; }

  /* Pagination compact */
  .pagination { flex-direction: column; gap: 8px; align-items: stretch; }

  /* Table cards */
  .table-wrap {
    border: none;
    background: transparent;
    overflow-x: visible;
  }
  .table-wrap table {
    display: block;
    border: none;
    background: transparent;
  }
  .table-wrap thead { display: none; }
  .table-wrap tbody { display: block; }
  .table-wrap tbody tr {
    display: block;
    background: var(--bg1);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    margin-bottom: 12px;
    padding: 16px;
    box-shadow: 0 1px 2px rgba(0,0,0,0.04);
  }
  .table-wrap tbody tr:hover td { background: transparent; }
  .table-wrap td {
    display: flex;
    flex-direction: column;
    align-items: flex-start;
    padding: 10px 0;
    border-bottom: 1px solid var(--border);
    border-left: none;
    border-right: none;
    gap: 4px;
  }
  .table-wrap td:last-child {
    border-bottom: none;
    padding-top: 12px;
  }
  .table-wrap td::before {
    content: attr(data-label);
    font-weight: 600;
    color: var(--text3);
    font-size: 10px;
    text-transform: uppercase;
    letter-spacing: 0.5px;
  }
  .table-wrap td .cell-user {
    justify-content: flex-start;
    width: 100%;
    gap: 10px;
  }
  .table-wrap td .cell-user .cell-name {
    font-size: 15px;
    font-weight: 600;
  }
  .table-wrap td .actions {
    justify-content: flex-start;
    width: 100%;
    gap: 6px;
    flex-wrap: wrap;
  }
  .table-wrap td .actions .btn-sm {
    flex: 1 1 auto;
    min-width: 56px;
  }

  /* Touch-friendly tooltips */
  .tooltip {
    font-size: 14px;
    padding: 10px 14px;
    border-radius: 8px;
  }
}

/* ── Settings ────────────────────────────────────────────────── */
.settings-tabs {
  display: flex;
  gap: 4px;
  margin-bottom: 20px;
  border-bottom: 1px solid var(--border);
  flex-wrap: nowrap;
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
  scrollbar-width: none;
}
.settings-tabs::-webkit-scrollbar { display: none; }

.settings-tab {
  padding: 8px 16px;
  border: none;
  border-bottom: 2px solid transparent;
  background: transparent;
  color: var(--text3);
  font-size: 13px;
  font-weight: 500;
  cursor: pointer;
  white-space: nowrap;
  transition: all .15s;
  position: relative;
}
.settings-tab:hover { color: var(--text1); }
.settings-tab.active {
  color: var(--accent);
  border-bottom-color: var(--accent);
}
.settings-tab.unsaved::after {
  content: '';
  position: absolute;
  top: 6px;
  right: 4px;
  width: 6px;
  height: 6px;
  background: var(--yellow);
  border-radius: 50%;
}

.settings-pane { display: none; }
.settings-pane.active { display: block; }

.settings-group {
  background: var(--bg1);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  margin-bottom: 16px;
  overflow: hidden;
}
.settings-group-header {
  padding: 14px 18px;
  font-size: 13px;
  font-weight: 600;
  color: var(--text2);
  text-transform: uppercase;
  letter-spacing: 0.5px;
  border-bottom: 1px solid var(--border);
  background: var(--bg2);
  display: flex;
  justify-content: space-between;
  align-items: center;
  user-select: none;
}
.settings-group-header:hover { color: var(--text1); }
.settings-group-body {
  padding: 18px;
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 16px;
}
.settings-field {
  display: flex;
  flex-direction: column;
  gap: 6px;
}
.settings-field.full { grid-column: 1 / -1; }
.settings-field label {
  font-size: 12px;
  font-weight: 500;
  color: var(--text2);
  display: flex;
  justify-content: space-between;
}
.settings-field .meta {
  font-size: 11px;
  color: var(--text4);
  font-weight: 400;
}
.settings-field input,
.settings-field select,
.settings-field textarea {
  background: var(--bg0);
  border: 1px solid var(--border-hi);
  border-radius: var(--radius-sm);
  padding: 8px 12px;
  color: var(--text1);
  font-size: 13px;
  outline: none;
  transition: border-color .15s;
}
.settings-field textarea {
  resize: vertical;
  min-height: 80px;
}
.settings-field select {
  appearance: none;
  -webkit-appearance: none;
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='12' height='12' viewBox='0 0 24 24' fill='none' stroke='%238B949E' stroke-width='2' stroke-linecap='round' stroke-linejoin='round'%3E%3Cpath d='m6 9 6 6 6-6'/%3E%3C/svg%3E");
  background-repeat: no-repeat;
  background-position: right 12px center;
  padding-right: 26px;
}
.settings-field input:focus,
.settings-field select:focus,
.settings-field textarea:focus { border-color: var(--accent); }
.settings-field input.modified,
.settings-field select.modified,
.settings-field textarea.modified { border-color: var(--yellow); }
.settings-field .hint {
  font-size: 11px;
  color: var(--text4);
}
.settings-field .hint.restart { color: var(--orange); }
.input-with-btn { display: flex; align-items: stretch; }
.input-with-btn select {
  flex: 1;
  border-top-right-radius: 0;
  border-bottom-right-radius: 0;
}
.input-with-btn button {
  border-top-left-radius: 0;
  border-bottom-left-radius: 0;
  margin-left: -1px;
  padding: 8px 10px;
  font-size: 13px;
  line-height: 1;
}

@media (max-width: 680px) {
  .settings-group-body { grid-template-columns: 1fr; }
}

/* ── Sidebar overlay ─────────────────────────────────────────── */
.sidebar-overlay {
  position: fixed; inset: 0; background: rgba(0,0,0,.5);
  opacity: 0; pointer-events: none; transition: opacity .25s;
  z-index: 45;
}
.sidebar-overlay.active { opacity: 1; pointer-events: auto; }

/* ── Email Template Editor ──────────────────────────────────── */
.email-toolbar {
  display: flex;
  align-items: center;
  gap: 4px;
  padding: 6px 8px;
  background: var(--bg2);
  border: 1px solid var(--border-hi);
  border-radius: 6px;
  margin-bottom: 8px;
  flex-wrap: wrap;
}
.email-toolbar-btn {
  padding: 4px 10px;
  border-radius: 4px;
  border: 1px solid transparent;
  background: transparent;
  color: var(--text2);
  font-size: 12px;
  cursor: pointer;
  transition: all .15s;
  font-weight: 600;
}
.email-toolbar-btn:hover {
  background: var(--bg1);
  border-color: var(--border-hi);
  color: var(--text1);
}
.email-toolbar-btn.var {
  font-weight: 400;
  font-family: monospace;
  font-size: 11px;
  color: var(--accent);
}
.email-toolbar-btn.var:hover {
  color: #79c0ff;
}
.email-toolbar-sep {
  width: 1px;
  height: 18px;
  background: var(--border);
  margin: 0 4px;
}

.email-preview {
  margin-top: 16px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  overflow: hidden;
}
.email-preview-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 8px 12px;
  background: var(--bg2);
  border-bottom: 1px solid var(--border);
}
.email-preview-label {
  font-size: 11px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: .5px;
  color: var(--text3);
}
.email-preview-frame {
  padding: 16px;
  background: #fff;
  color: #1a1a1a;
  min-height: 120px;
}

/* ── Email preview popover (table actions) ─────────────────── */
.email-preview-popover {
  position: absolute;
  z-index: 100;
  width: 480px;
  max-width: 90vw;
  background: var(--bg1);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: 0 12px 40px rgba(0,0,0,.5);
  overflow: hidden;
}
.email-preview-popover-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 10px 14px;
  background: var(--bg2);
  border-bottom: 1px solid var(--border);
}
.email-preview-popover-header span {
  font-size: 12px;
  font-weight: 600;
  color: var(--text1);
}
.email-preview-popover-header button {
  background: transparent;
  border: none;
  color: var(--text3);
  cursor: pointer;
  padding: 2px;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: 4px;
}
.email-preview-popover-header button:hover { color: var(--text1); }
.email-preview-popover-header button svg { width: 14px; height: 14px; }
.email-preview-popover-subject {
  padding: 10px 14px;
  border-bottom: 1px solid var(--border);
  font-size: 13px;
  color: var(--text1);
  word-break: break-word;
}
.email-preview-popover-subject strong { color: var(--accent); }
.email-preview-popover-body {
  padding: 16px;
  background: #fff;
  color: #1a1a1a;
  min-height: 120px;
  max-height: 400px;
  overflow-y: auto;
}
.email-preview-popover-body img { max-width: 100%; height: auto; }
.email-preview-frame table {
  border-collapse: collapse;
  width: 100%;
}
.email-preview-frame a { color: #0969da; }

/* ── Toast Notifications ─────────────────────────────────────── */
.toast-container {
  position: fixed;
  top: 20px;
  right: 20px;
  z-index: 9999;
  display: flex;
  flex-direction: column;
  gap: 0;
  width: 320px;
  pointer-events: none;
}
.toast-container .toast { pointer-events: auto; }

.toast {
  position: relative;
  background: var(--bg1);
  border: 1px solid var(--border-hi);
  border-radius: 10px;
  padding: 14px 40px 14px 16px;
  margin-bottom: 8px;
  box-shadow: 0 8px 32px rgba(0,0,0,.35);
  backdrop-filter: blur(12px);
  -webkit-backdrop-filter: blur(12px);
  display: flex;
  align-items: flex-start;
  gap: 10px;
  cursor: pointer;
  transform-origin: top right;
  transition: transform .3s cubic-bezier(.34,1.56,.64,1), opacity .3s ease, filter .3s ease;
  overflow: hidden;
  min-height: 52px;
}
.toast-front {
  transform: translateY(0) scale(1);
  opacity: 1;
  filter: blur(0);
  z-index: 3;
}
.toast-mid {
  transform: translateY(8px) scale(0.96);
  opacity: 0.65;
  filter: blur(1px);
  z-index: 2;
}
.toast-back {
  transform: translateY(16px) scale(0.92);
  opacity: 0.35;
  filter: blur(2px);
  z-index: 1;
}
.toast-hidden {
  transform: translateY(24px) scale(0.88);
  opacity: 0;
  pointer-events: none;
  filter: blur(3px);
}

.toast-icon {
  flex-shrink: 0;
  width: 20px;
  height: 20px;
  margin-top: 1px;
}
.toast-icon svg {
  width: 100%;
  height: 100%;
  stroke-width: 2;
}

.toast-body {
  flex: 1;
  min-width: 0;
}
.toast-message {
  font-size: 13px;
  line-height: 1.45;
  color: var(--text1);
  word-break: break-word;
}

.toast-close {
  position: absolute;
  top: 10px;
  right: 10px;
  width: 22px;
  height: 22px;
  border: none;
  background: transparent;
  color: var(--text3);
  cursor: pointer;
  border-radius: 4px;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 0;
  transition: color .15s, background .15s;
}
.toast-close:hover { color: var(--text1); background: rgba(255,255,255,.06); }
.toast-close svg { width: 14px; height: 14px; stroke-width: 2; }

/* Type accents */
.toast-success { border-left: 3px solid #238636; }
.toast-success .toast-icon { color: #3fb950; }
.toast-error   { border-left: 3px solid #F85149; }
.toast-error   .toast-icon { color: #F85149; }
.toast-warning { border-left: 3px solid #D29922; }
.toast-warning .toast-icon { color: #D29922; }
.toast-info    { border-left: 3px solid #58A6FF; }
.toast-info    .toast-icon { color: #58A6FF; }

/* Progress bar */
.toast-progress {
  position: absolute;
  bottom: 0;
  left: 0;
  height: 2px;
  border-radius: 0 0 0 10px;
  transition: width linear;
}
.toast-success .toast-progress { background: #238636; }
.toast-error   .toast-progress { background: #F85149; }
.toast-warning .toast-progress { background: #D29922; }
.toast-info    .toast-progress { background: #58A6FF; }

/* Animations */
@keyframes toast-in {
  from { transform: translateX(120%) scale(.9); opacity: 0; }
  to   { transform: translateX(0) scale(1); opacity: 1; }
}
@keyframes toast-out-right {
  from { transform: translateX(0); opacity: 1; }
  to   { transform: translateX(120%); opacity: 0; }
}
@keyframes toast-out-down {
  from { transform: translateY(0); opacity: 1; }
  to   { transform: translateY(-20px); opacity: 0; }
}

.toast-enter {
  animation: toast-in .35s cubic-bezier(.34,1.56,.64,1) forwards;
}
.toast-exit-right {
  animation: toast-out-right .25s ease forwards;
}
.toast-exit-down {
  animation: toast-out-down .25s ease forwards;
}

/* Swipe state */
.toast-swiping {
  transition: none;
  user-select: none;
}

/* ── Project State Modal ────────────────────────────────────── */
.state-modal {
  position: fixed;
  inset: 0;
  z-index: 200;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 24px;
}
.state-modal-backdrop {
  position: absolute;
  inset: 0;
  background: rgba(0,0,0,.55);
  backdrop-filter: blur(4px);
}
.state-modal-content {
  position: relative;
  background: var(--bg1);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  width: 720px;
  max-width: 100%;
  max-height: 85vh;
  display: flex;
  flex-direction: column;
  overflow: hidden;
  box-shadow: 0 24px 64px rgba(0,0,0,.5);
}
.state-modal-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 18px 24px;
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
}
.state-modal-header h3 { font-size: 16px; font-weight: 700; }
.state-modal-body {
  padding: 20px 24px;
  overflow-y: auto;
  flex: 1;
}
.state-stats {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(140px, 1fr));
  gap: 12px;
}
.state-stat {
  background: var(--bg2);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 14px 16px;
  text-align: center;
}
.state-stat-value {
  font-size: 24px;
  font-weight: 700;
  color: var(--accent);
  line-height: 1;
}
.state-stat-label {
  font-size: 11px;
  color: var(--text3);
  margin-top: 6px;
  text-transform: uppercase;
  letter-spacing: .5px;
}
.state-json {
  margin-top: 16px;
  background: var(--bg0);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 16px;
  font-family: 'SF Mono', Monaco, 'Cascadia Code', monospace;
  font-size: 12px;
  line-height: 1.5;
  color: var(--text2);
  overflow-x: auto;
  white-space: pre-wrap;
  word-break: break-word;
}
.state-json .key   { color: #79c0ff; }
.state-json .str   { color: #a5d6ff; }
.state-json .num   { color: #79c0ff; }
.state-json .bool  { color: #ff7b72; }
.state-json .null  { color: #ff7b72; }

@media (max-width: 680px) {
  .state-modal { padding: 12px; }
  .state-modal-content { max-height: 92vh; }
  .state-modal-header { padding: 14px 16px; }
  .state-modal-body { padding: 14px 16px; }
  .state-stats { grid-template-columns: repeat(2, 1fr); }
}

/* Mobile */
@media (max-width: 680px) {
  .toast-container {
    right: 12px;
    left: 12px;
    width: auto;
    top: 12px;
  }
  .toast {
    padding: 12px 36px 12px 14px;
    margin-bottom: 6px;
  }
}
