:root {
  --bg: #f4f5f7;
  --surface: #ffffff;
  --surface-2: #f9fafb;
  --border: #e3e5e9;
  --text: #1a1d23;
  --text-muted: #6b7280;
  --text-faint: #9ca3af;
  --accent: #3457d5;
  --accent-hover: #2843b0;
  --accent-soft: #eaeefb;
  --danger: #d3402f;
  --danger-soft: #fbeae8;
  --success: #1a8a52;
  --success-soft: #e7f6ee;
  --warning: #b5790a;
  --warning-soft: #fdf3e0;
  --sidebar-bg: #191c24;
  --sidebar-text: #c7cad3;
  --sidebar-text-active: #ffffff;
  --sidebar-border: #2a2e39;
  --radius: 10px;
  --radius-sm: 6px;
  --shadow: 0 1px 2px rgba(20, 21, 26, 0.06), 0 1px 1px rgba(20, 21, 26, 0.04);
  --shadow-md: 0 8px 24px rgba(20, 21, 26, 0.12);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Inter, Roboto, Helvetica, Arial, sans-serif;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #14161b;
    --surface: #1c1f26;
    --surface-2: #20232b;
    --border: #2c2f38;
    --text: #eef0f3;
    --text-muted: #9aa0ac;
    --text-faint: #6b7280;
    --accent: #5c7cfa;
    --accent-hover: #7690fb;
    --accent-soft: #212a4a;
    --danger: #f0695a;
    --danger-soft: #3a201d;
    --success: #3ecf8e;
    --success-soft: #163829;
    --warning: #e0a63a;
    --warning-soft: #3a2e12;
    --sidebar-bg: #101217;
    --sidebar-text: #a6abb6;
    --sidebar-text-active: #ffffff;
    --sidebar-border: #22252d;
    --shadow: 0 1px 2px rgba(0, 0, 0, 0.3), 0 1px 1px rgba(0, 0, 0, 0.2);
    --shadow-md: 0 8px 24px rgba(0, 0, 0, 0.45);
  }
}

* { box-sizing: border-box; }
html, body { height: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-size: 14px;
  line-height: 1.5;
  -webkit-font-smoothing: antialiased;
}
#app { min-height: 100%; }
img { max-width: 100%; }
a { color: var(--accent); text-decoration: none; }
a:hover { text-decoration: underline; }
button { font-family: inherit; }

/* ---------- boot / loading ---------- */
.boot-loader {
  height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
}
.spinner {
  width: 28px;
  height: 28px;
  border-radius: 50%;
  border: 3px solid var(--border);
  border-top-color: var(--accent);
  animation: spin 0.7s linear infinite;
}
.spinner.sm { width: 16px; height: 16px; border-width: 2px; }
@keyframes spin { to { transform: rotate(360deg); } }

/* ---------- auth screen ---------- */
.auth-screen {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 24px;
}
.auth-card {
  width: 100%;
  max-width: 380px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow-md);
  padding: 32px;
}
.auth-card h1 {
  font-size: 19px;
  margin: 0 0 4px;
}
.auth-card p.sub {
  color: var(--text-muted);
  margin: 0 0 24px;
  font-size: 13px;
}
.auth-brand {
  display: flex;
  align-items: center;
  gap: 10px;
  margin-bottom: 20px;
}
.auth-brand .mark {
  width: 36px;
  height: 36px;
  border-radius: 9px;
  background: var(--accent);
  color: #fff;
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: 700;
  font-size: 15px;
  flex-shrink: 0;
}

/* ---------- app shell ---------- */
.shell {
  display: grid;
  grid-template-columns: 232px 1fr;
  min-height: 100vh;
}
.sidebar {
  background: var(--sidebar-bg);
  color: var(--sidebar-text);
  display: flex;
  flex-direction: column;
  padding: 18px 12px;
  position: sticky;
  top: 0;
  height: 100vh;
  overflow-y: auto;
}
.sidebar-brand {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 6px 8px 18px;
}
.sidebar-brand .mark {
  width: 30px;
  height: 30px;
  border-radius: 8px;
  background: var(--accent);
  color: #fff;
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: 700;
  font-size: 13px;
  flex-shrink: 0;
}
.sidebar-brand .name {
  color: #fff;
  font-weight: 600;
  font-size: 14px;
  line-height: 1.2;
}
.sidebar-brand .sub {
  font-size: 11px;
  color: var(--text-faint);
}

.tenant-switch {
  margin: 0 4px 16px;
  padding: 10px 10px;
  background: rgba(255,255,255,0.04);
  border: 1px solid var(--sidebar-border);
  border-radius: var(--radius-sm);
  cursor: pointer;
  display: flex;
  align-items: center;
  gap: 8px;
}
.tenant-switch:hover { background: rgba(255,255,255,0.07); }
.tenant-switch .logo {
  width: 26px; height: 26px; border-radius: 6px; object-fit: cover;
  background: rgba(255,255,255,0.08); flex-shrink: 0;
}
.tenant-switch .info { min-width: 0; flex: 1; }
.tenant-switch .tname {
  color: #fff; font-size: 13px; font-weight: 600;
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.tenant-switch .tsector { font-size: 11px; color: var(--text-faint); }
.tenant-switch .chev { color: var(--text-faint); font-size: 11px; }

.nav-group-label {
  font-size: 10.5px;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--text-faint);
  padding: 14px 12px 6px;
}
.nav-link {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 8px 12px;
  border-radius: var(--radius-sm);
  color: var(--sidebar-text);
  font-size: 13.5px;
  cursor: pointer;
  margin-bottom: 2px;
}
.nav-link .ic { width: 16px; height: 16px; flex-shrink: 0; opacity: 0.85; }
.nav-link:hover { background: rgba(255,255,255,0.06); text-decoration: none; color: #fff; }
.nav-link.active { background: var(--accent); color: #fff; }
.nav-link .count {
  margin-left: auto;
  font-size: 11px;
  background: rgba(255,255,255,0.12);
  padding: 1px 6px;
  border-radius: 10px;
}

.sidebar-footer {
  margin-top: auto;
  padding-top: 12px;
  border-top: 1px solid var(--sidebar-border);
}
.sidebar-footer .logout {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px 12px;
  border-radius: var(--radius-sm);
  color: var(--text-faint);
  font-size: 12.5px;
  cursor: pointer;
}
.sidebar-footer .logout:hover { background: rgba(255,255,255,0.06); color: #fff; }

.main {
  min-width: 0;
  display: flex;
  flex-direction: column;
}
.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 16px 28px;
  border-bottom: 1px solid var(--border);
  background: var(--surface);
  position: sticky;
  top: 0;
  z-index: 5;
}
.topbar h2 { margin: 0; font-size: 17px; }
.topbar .crumbs { font-size: 12px; color: var(--text-faint); margin-top: 2px; }
.content { padding: 24px 28px 60px; max-width: 1080px; width: 100%; }

/* ---------- primitives ---------- */
.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
}
.card + .card { margin-top: 18px; }
.card-header {
  padding: 16px 20px;
  border-bottom: 1px solid var(--border);
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
}
.card-header h3 { margin: 0; font-size: 14.5px; }
.card-header p { margin: 2px 0 0; font-size: 12.5px; color: var(--text-muted); }
.card-body { padding: 20px; }
.card-body.pad-sm { padding: 10px; }

.btn {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 8px 14px;
  border-radius: var(--radius-sm);
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text);
  font-size: 13px;
  font-weight: 500;
  cursor: pointer;
  white-space: nowrap;
}
.btn:hover { background: var(--surface-2); text-decoration: none; }
.btn:disabled { opacity: 0.55; cursor: not-allowed; }
.btn.primary { background: var(--accent); border-color: var(--accent); color: #fff; }
.btn.primary:hover { background: var(--accent-hover); border-color: var(--accent-hover); }
.btn.danger { background: var(--surface); border-color: var(--danger); color: var(--danger); }
.btn.danger:hover { background: var(--danger-soft); }
.btn.ghost { border-color: transparent; background: transparent; }
.btn.ghost:hover { background: var(--surface-2); }
.btn.sm { padding: 5px 10px; font-size: 12px; }
.btn.icon-only { padding: 6px; }

.badge {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  padding: 2px 8px;
  border-radius: 100px;
  font-size: 11.5px;
  font-weight: 600;
  line-height: 1.7;
}
.badge.neutral { background: var(--surface-2); color: var(--text-muted); border: 1px solid var(--border); }
.badge.success { background: var(--success-soft); color: var(--success); }
.badge.danger { background: var(--danger-soft); color: var(--danger); }
.badge.warning { background: var(--warning-soft); color: var(--warning); }
.badge.accent { background: var(--accent-soft); color: var(--accent); }

.form-grid {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 16px 18px;
}
.form-grid.cols-1 { grid-template-columns: 1fr; }
.field { display: flex; flex-direction: column; gap: 6px; }
.field.span-2 { grid-column: span 2; }
.field label { font-size: 12.5px; font-weight: 600; color: var(--text-muted); }
.field .hint { font-size: 11.5px; color: var(--text-faint); }
.field .error { font-size: 11.5px; color: var(--danger); }

input[type=text], input[type=email], input[type=tel], input[type=password],
input[type=date], input[type=time], input[type=number], input[type=search],
select, textarea {
  width: 100%;
  padding: 8px 10px;
  border-radius: var(--radius-sm);
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text);
  font-size: 13.5px;
  font-family: inherit;
}
textarea { resize: vertical; min-height: 64px; }
input:focus, select:focus, textarea:focus {
  outline: none;
  border-color: var(--accent);
  box-shadow: 0 0 0 3px var(--accent-soft);
}
input.invalid, textarea.invalid { border-color: var(--danger); }
input:disabled, select:disabled { opacity: 0.6; }

.switch {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  cursor: pointer;
  font-size: 13px;
}
.switch input { display: none; }
.switch .track {
  width: 34px; height: 20px; border-radius: 20px;
  background: var(--border);
  position: relative;
  transition: background 0.15s;
  flex-shrink: 0;
}
.switch .track::after {
  content: "";
  position: absolute;
  top: 2px; left: 2px;
  width: 16px; height: 16px;
  border-radius: 50%;
  background: #fff;
  box-shadow: 0 1px 2px rgba(0,0,0,0.3);
  transition: transform 0.15s;
}
.switch input:checked + .track { background: var(--accent); }
.switch input:checked + .track::after { transform: translateX(14px); }

.table-wrap { overflow-x: auto; }
table.data { width: 100%; border-collapse: collapse; font-size: 13.5px; }
table.data th {
  text-align: left;
  font-size: 11.5px;
  text-transform: uppercase;
  letter-spacing: 0.03em;
  color: var(--text-faint);
  padding: 10px 20px;
  border-bottom: 1px solid var(--border);
  white-space: nowrap;
}
table.data td {
  padding: 12px 20px;
  border-bottom: 1px solid var(--border);
  vertical-align: top;
}
table.data tr:last-child td { border-bottom: none; }
table.data tr:hover td { background: var(--surface-2); }
.cell-muted { color: var(--text-muted); }
.cell-mono { font-variant-numeric: tabular-nums; }

.empty-state {
  text-align: center;
  padding: 48px 20px;
  color: var(--text-muted);
}
.empty-state .ic { font-size: 30px; margin-bottom: 8px; opacity: 0.5; }
.empty-state h4 { margin: 0 0 4px; color: var(--text); font-size: 14px; }
.empty-state p { margin: 0 0 16px; font-size: 12.5px; }

.inline-error {
  background: var(--danger-soft);
  color: var(--danger);
  padding: 10px 14px;
  border-radius: var(--radius-sm);
  font-size: 12.5px;
  margin-bottom: 16px;
}

.actions-row { display: flex; gap: 8px; align-items: center; justify-content: flex-end; }
.toolbar { display: flex; align-items: center; justify-content: space-between; gap: 12px; margin-bottom: 16px; flex-wrap: wrap; }
.toolbar .search { max-width: 260px; }

.chip-list { display: flex; flex-wrap: wrap; gap: 6px; }
.chip {
  background: var(--surface-2);
  border: 1px solid var(--border);
  padding: 3px 9px;
  border-radius: 100px;
  font-size: 12px;
}

/* ---------- toasts ---------- */
.toast-root {
  position: fixed;
  top: 18px;
  right: 18px;
  z-index: 200;
  display: flex;
  flex-direction: column;
  gap: 8px;
}
.toast {
  min-width: 240px;
  max-width: 340px;
  background: var(--text);
  color: var(--bg);
  padding: 11px 14px;
  border-radius: var(--radius-sm);
  box-shadow: var(--shadow-md);
  font-size: 13px;
  display: flex;
  align-items: flex-start;
  gap: 8px;
  animation: toast-in 0.15s ease-out;
}
.toast.error { background: var(--danger); color: #fff; }
.toast.success { background: var(--success); color: #fff; }
@keyframes toast-in { from { opacity: 0; transform: translateY(-6px); } to { opacity: 1; transform: translateY(0); } }

/* ---------- modal ---------- */
.modal-overlay {
  position: fixed; inset: 0;
  background: rgba(10, 11, 14, 0.5);
  display: flex; align-items: center; justify-content: center;
  padding: 20px;
  z-index: 150;
}
.modal {
  background: var(--surface);
  border-radius: var(--radius);
  box-shadow: var(--shadow-md);
  width: 100%;
  max-width: 520px;
  max-height: 88vh;
  display: flex;
  flex-direction: column;
}
.modal.wide { max-width: 640px; }
.modal-header {
  padding: 16px 20px;
  border-bottom: 1px solid var(--border);
  display: flex; align-items: center; justify-content: space-between;
}
.modal-header h3 { margin: 0; font-size: 15px; }
.modal-body { padding: 20px; overflow-y: auto; }
.modal-footer {
  padding: 14px 20px;
  border-top: 1px solid var(--border);
  display: flex; justify-content: flex-end; gap: 8px;
}
.modal-close {
  background: none; border: none; cursor: pointer;
  color: var(--text-faint); font-size: 18px; line-height: 1; padding: 4px;
}
.modal-close:hover { color: var(--text); }

/* transcript bubbles */
.transcript { display: flex; flex-direction: column; gap: 8px; }
.bubble { max-width: 78%; padding: 8px 12px; border-radius: 12px; font-size: 13px; }
.bubble.caller { align-self: flex-start; background: var(--surface-2); border: 1px solid var(--border); border-bottom-left-radius: 3px; }
.bubble.ai { align-self: flex-end; background: var(--accent-soft); color: var(--text); border-bottom-right-radius: 3px; }
.bubble .role { font-size: 10.5px; text-transform: uppercase; letter-spacing: 0.04em; color: var(--text-faint); margin-bottom: 2px; }

/* ---------- picker (tenant list) ---------- */
.picker-screen { min-height: 100vh; padding: 40px 24px; }
.picker-header { max-width: 880px; margin: 0 auto 24px; display: flex; align-items: center; justify-content: space-between; }
.picker-grid { max-width: 880px; margin: 0 auto; display: grid; grid-template-columns: repeat(auto-fill, minmax(220px, 1fr)); gap: 14px; }
.tenant-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 18px;
  cursor: pointer;
  box-shadow: var(--shadow);
}
.tenant-card:hover { border-color: var(--accent); }
.tenant-card .logo {
  width: 44px; height: 44px; border-radius: 10px; object-fit: cover;
  background: var(--surface-2); margin-bottom: 12px;
}
.tenant-card h4 { margin: 0 0 4px; font-size: 14.5px; }
.tenant-card .meta { font-size: 12px; color: var(--text-muted); }

/* logo uploader */
.logo-uploader { display: flex; align-items: center; gap: 16px; }
.logo-uploader .preview {
  width: 72px; height: 72px; border-radius: 12px;
  background: var(--surface-2); border: 1px solid var(--border);
  object-fit: cover; flex-shrink: 0;
}

@media (max-width: 860px) {
  .shell { grid-template-columns: 1fr; }
  .sidebar { position: static; height: auto; }
  .form-grid { grid-template-columns: 1fr; }
  .field.span-2 { grid-column: span 1; }
}
