/* HYBRID admin — design tokens (Linear / Stripe / Vercel-style) */
:root {
  --bg: #fafafa;
  --surface: #ffffff;
  --surface-2: #f5f5f5;
  --text: #18181b;
  --text-muted: #71717a;
  --text-subtle: #a1a1aa;
  --border: #e4e4e7;
  --border-strong: #d4d4d8;
  --accent: #0070f3;
  --accent-hover: #0058c4;
  --success: #16a34a;
  --warn: #f59e0b;
  --danger: #dc2626;
  --radius: 8px;
  --radius-sm: 6px;
  --radius-lg: 12px;
  --shadow-sm: 0 1px 2px rgba(0,0,0,0.04);
  --shadow-md: 0 4px 6px -1px rgba(0,0,0,0.05), 0 2px 4px -2px rgba(0,0,0,0.04);
  --shadow-lg: 0 10px 15px -3px rgba(0,0,0,0.06), 0 4px 6px -4px rgba(0,0,0,0.04);
  --sidebar-w: 240px;
  --topbar-h: 56px;
  --font: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  --mono: ui-monospace, SFMono-Regular, "SF Mono", Menlo, Consolas, monospace;
}

* { box-sizing: border-box; }
html, body { margin: 0; padding: 0; }
body {
  font-family: var(--font);
  font-size: 14px;
  line-height: 1.5;
  color: var(--text);
  background: var(--bg);
  -webkit-font-smoothing: antialiased;
}
[hidden] { display: none !important; }
a { color: var(--accent); text-decoration: none; }
a:hover { color: var(--accent-hover); }
button { font: inherit; }
.muted { color: var(--text-muted); }
.subtle { color: var(--text-subtle); }
.small { font-size: 12px; }
.error { color: var(--danger); margin-top: 8px; min-height: 1em; }
.ok { color: var(--success); }

/* --- login --- */
.login-overlay {
  position: fixed; inset: 0;
  background: var(--bg);
  display: grid; place-items: center;
  z-index: 100;
}
.login-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow-md);
  padding: 32px;
  width: 360px;
  display: flex;
  flex-direction: column;
  gap: 8px;
}
.login-card h1 { margin: 0; font-size: 18px; font-weight: 600; }
.login-card p { margin: 0 0 8px 0; font-size: 13px; }
.login-card label { font-size: 12px; color: var(--text-muted); margin-top: 4px; }
.login-card input {
  padding: 9px 11px;
  border: 1px solid var(--border-strong);
  border-radius: var(--radius-sm);
  background: var(--surface);
  font: inherit;
}
.login-card input:focus { outline: none; border-color: var(--accent); box-shadow: 0 0 0 3px rgba(0,112,243,0.15); }
.logo-mark {
  width: 32px; height: 32px;
  display: grid; place-items: center;
  font-size: 22px; line-height: 1;
  color: var(--text);
}

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

.sidebar {
  width: var(--sidebar-w);
  background: var(--surface);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  position: sticky;
  top: 0;
  height: 100vh;
}
.sidebar-head {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 16px;
  height: var(--topbar-h);
  border-bottom: 1px solid var(--border);
}
.brand { font-weight: 600; font-size: 14px; letter-spacing: 0.02em; }
.nav { padding: 12px 8px; flex: 1; overflow-y: auto; }
.nav-section {
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--text-subtle);
  padding: 16px 12px 6px 12px;
}
.nav-item {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 7px 10px;
  border-radius: var(--radius-sm);
  color: var(--text);
  font-size: 13px;
  cursor: pointer;
  user-select: none;
  margin: 1px 0;
}
.nav-item i { width: 16px; height: 16px; color: var(--text-muted); }
.nav-item:hover { background: var(--surface-2); }
.nav-item.active { background: var(--surface-2); color: var(--text); font-weight: 500; }
.nav-item.active i { color: var(--accent); }
.nav-sub { padding-left: 12px; }
.sidebar-foot {
  padding: 12px;
  border-top: 1px solid var(--border);
  display: flex;
  align-items: center;
  gap: 8px;
}
.tenant-pill {
  flex: 1;
  font-size: 12px;
  color: var(--text-muted);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

/* --- main + topbar --- */
.main { flex: 1; display: flex; flex-direction: column; min-width: 0; }
.topbar {
  height: var(--topbar-h);
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 0 24px;
  border-bottom: 1px solid var(--border);
  background: var(--surface);
  position: sticky;
  top: 0;
  z-index: 10;
}
.crumbs { flex: 1; font-size: 13px; color: var(--text-muted); }
.crumbs strong { color: var(--text); font-weight: 500; }
.topbar-right { display: flex; gap: 12px; align-items: center; }
.view { padding: 24px 32px; max-width: 1200px; }

/* --- buttons --- */
.btn {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 7px 12px;
  border-radius: var(--radius-sm);
  border: 1px solid var(--border-strong);
  background: var(--surface);
  color: var(--text);
  font-size: 13px;
  font-weight: 500;
  cursor: pointer;
  transition: background .12s, border-color .12s;
}
.btn:hover { background: var(--surface-2); }
.btn.primary { background: var(--text); color: white; border-color: var(--text); }
.btn.primary:hover { background: #000; }
.btn.danger { background: var(--surface); color: var(--danger); border-color: var(--border-strong); }
.btn.danger:hover { background: #fef2f2; border-color: var(--danger); }
.btn.ghost { background: transparent; border-color: transparent; }
.btn.ghost:hover { background: var(--surface-2); }
.btn.icon { padding: 6px; width: 32px; height: 32px; justify-content: center; }
.btn.small { padding: 4px 8px; font-size: 12px; }
.btn:disabled { opacity: 0.5; cursor: not-allowed; }

/* --- cards & sections --- */
.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow-sm);
  padding: 20px;
}
.card h2, .card h3 { margin-top: 0; }
.card-head {
  display: flex; justify-content: space-between; align-items: center;
  margin-bottom: 16px;
}
.card-head h2 { margin: 0; font-size: 15px; font-weight: 600; }
.section { margin-bottom: 24px; }
.section-title { font-size: 18px; font-weight: 600; margin: 0 0 4px 0; }
.section-desc { color: var(--text-muted); margin: 0 0 16px 0; }

/* --- KPI grid --- */
.kpi-grid {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 16px;
  margin-bottom: 24px;
}
.kpi {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 16px;
  box-shadow: var(--shadow-sm);
}
.kpi-label { font-size: 12px; color: var(--text-muted); text-transform: uppercase; letter-spacing: 0.04em; }
.kpi-value { font-size: 24px; font-weight: 600; margin: 6px 0 4px 0; }
.kpi-sub { font-size: 12px; color: var(--text-muted); }
.kpi-spark { height: 32px; margin-top: 8px; }

/* --- forms --- */
.form-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 16px; }
.form-row { display: flex; flex-direction: column; gap: 4px; margin-bottom: 12px; }
.form-row label { font-size: 12px; color: var(--text-muted); }
.form-row input, .form-row select, .form-row textarea {
  padding: 7px 10px;
  border: 1px solid var(--border-strong);
  border-radius: var(--radius-sm);
  background: var(--surface);
  font: inherit;
  color: var(--text);
}
.form-row input:focus, .form-row select:focus, .form-row textarea:focus {
  outline: none; border-color: var(--accent); box-shadow: 0 0 0 3px rgba(0,112,243,0.15);
}
.form-row textarea { min-height: 80px; resize: vertical; font-family: var(--mono); font-size: 12px; }
.checkbox-row {
  display: flex; align-items: center; gap: 8px; padding: 4px 0;
}
.checkbox-row input { width: 16px; height: 16px; }

/* --- table --- */
table.tbl { width: 100%; border-collapse: collapse; font-size: 13px; }
table.tbl th, table.tbl td {
  padding: 8px 10px;
  border-bottom: 1px solid var(--border);
  text-align: left;
}
table.tbl th { font-size: 11px; text-transform: uppercase; letter-spacing: 0.04em; color: var(--text-muted); font-weight: 500; background: var(--surface-2); }
table.tbl tr:hover td { background: var(--surface-2); }
.tbl-wrap { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); overflow: hidden; }

/* --- pills / badges --- */
.pill {
  display: inline-block;
  padding: 2px 8px;
  border-radius: 999px;
  background: var(--surface-2);
  color: var(--text-muted);
  font-size: 11px;
  font-weight: 500;
}
.pill.success { background: #dcfce7; color: #166534; }
.pill.warn { background: #fef3c7; color: #92400e; }
.pill.danger { background: #fee2e2; color: #991b1b; }

/* --- heatmap --- */
.heatmap { display: grid; grid-template-columns: 24px repeat(24, 1fr); gap: 2px; }
.hm-h { font-size: 10px; color: var(--text-subtle); text-align: center; }
.hm-cell { height: 14px; background: var(--surface-2); border-radius: 2px; }

/* --- toasts --- */
.toasts { position: fixed; bottom: 16px; right: 16px; display: flex; flex-direction: column; gap: 8px; z-index: 50; }
.toast {
  background: var(--text); color: white; padding: 10px 14px;
  border-radius: var(--radius-sm); font-size: 13px;
  box-shadow: var(--shadow-lg); animation: toast-in .15s ease-out;
}
.toast.error { background: var(--danger); }
.toast.success { background: var(--success); }
@keyframes toast-in { from { transform: translateY(8px); opacity: 0; } to { transform: translateY(0); opacity: 1; } }

/* --- alerts panel --- */
.alert {
  padding: 10px 14px; border-left: 3px solid var(--border-strong);
  background: var(--surface-2); margin-bottom: 8px; border-radius: 0 var(--radius-sm) var(--radius-sm) 0;
  font-size: 13px;
}
.alert.warning { border-left-color: var(--warn); background: #fffbeb; }
.alert.critical { border-left-color: var(--danger); background: #fef2f2; }

/* --- restart overlay --- */
#hx-restart-overlay {
  position: fixed; inset: 0;
  background: rgba(24, 24, 27, 0.55);
  display: grid; place-items: center;
  z-index: 200;
  backdrop-filter: blur(2px);
  animation: hx-fade-in .15s ease-out;
}
.hx-restart-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow-lg);
  padding: 28px 32px;
  width: min(420px, calc(100% - 32px));
  text-align: center;
}
.hx-restart-card h3 { margin: 12px 0 6px; font-size: 16px; font-weight: 600; }
.hx-restart-card p { margin: 0; color: var(--text-muted); font-size: 13px; line-height: 1.5; }
.hx-restart-status {
  margin-top: 14px; font-size: 12px; color: var(--text-subtle);
  font-family: var(--mono);
}
.hx-restart-spin {
  width: 32px; height: 32px;
  margin: 0 auto;
  border: 3px solid var(--border);
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: hx-spin 0.9s linear infinite;
}
@keyframes hx-spin { to { transform: rotate(360deg); } }
@keyframes hx-fade-in { from { opacity: 0; } to { opacity: 1; } }

/* --- defensive: prevent any error container from ever rendering as raw HTML --- */
.error, #login-error {
  white-space: pre-wrap;
  word-break: break-word;
  max-height: 8em;
  overflow: hidden;
}

/* --- expandable call rows (overview + calls views) --- */
.tbl-clickable .tbl-row-click { cursor: pointer; transition: background .1s; }
.tbl-clickable .tbl-row-click:hover { background: var(--surface-2); }
.tbl-clickable .hx-row-open { background: var(--surface-2); }
.hx-expand-row > td { background: var(--surface-2); padding: 16px 20px; border-top: 1px solid var(--border); }
.hx-expand-panel { display: flex; flex-direction: column; gap: 12px; }
.hx-expand-meta { display: flex; flex-wrap: wrap; gap: 8px; align-items: center; font-size: 13px; color: var(--text-muted); }
.hx-expand-summary { font-size: 14px; line-height: 1.5; }
.hx-transcript {
  background: var(--surface); border: 1px solid var(--border); border-radius: 8px;
  padding: 12px 14px; font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
  font-size: 12.5px; line-height: 1.55; max-height: 360px; overflow: auto;
  white-space: pre-wrap; word-break: break-word; margin: 0;
}
.hx-expand-audio audio { width: 100%; max-width: 480px; }
.pill-good { background: rgba(22, 163, 74, .12); color: #16a34a; }
.pill-bad  { background: rgba(220, 38, 38, .12); color: #dc2626; }

/* --- responsive --- */
@media (max-width: 900px) {
  .sidebar { position: fixed; transform: translateX(-100%); transition: transform .15s; z-index: 30; }
  .sidebar.open { transform: translateX(0); box-shadow: var(--shadow-lg); }
  /* Dim the content behind an open sidebar so it reads as an overlay, not a
     split screen. The .main click handler dismisses it. */
  .sidebar.open ~ .main::before {
    content: ""; position: fixed; inset: 0; z-index: 20;
    background: rgba(24, 24, 27, 0.4);
  }
  .kpi-grid { grid-template-columns: repeat(2, 1fr); }
  .form-grid { grid-template-columns: 1fr; }
  .view { padding: 16px; }
}
