/* ── Design tokens ──────────────────────────────────────────── */
:root {
  --bg-base:       #0b0f14;
  --bg-panel:      #111827;
  --bg-card:       #1a2235;
  --bg-input:      #0f1829;
  --accent:        #10b981;
  --accent-hover:  #059669;
  --accent2:       #6366f1;
  --warn:          #f59e0b;
  --danger:        #ef4444;
  --text-primary:  #e2e8f0;
  --text-secondary:#94a3b8;
  --text-meta:     #64748b;
  --border:        rgba(255,255,255,.08);
  --nav-hover:     rgba(255,255,255,.05);
  --nav-active-bg: rgba(16,185,129,.12);
  --btn-ghost-hover: rgba(255,255,255,.06);
  --invite-bg:     rgba(255,255,255,.02);
  --agent-row-hover: rgba(255,255,255,.02);
  --radius:        12px;
  --radius-sm:     8px;
  --shadow:        0 4px 24px rgba(0,0,0,.4);
  --transition:    .2s cubic-bezier(.4,0,.2,1);
  --safe-bottom:   env(safe-area-inset-bottom, 0px);
  --safe-top:      env(safe-area-inset-top, 0px);
}

html[data-theme="light"] {
  --bg-base:       #f1f5f9;
  --bg-panel:      #ffffff;
  --bg-card:       #ffffff;
  --bg-input:      #f8fafc;
  --accent:        #059669;
  --accent-hover:  #047857;
  --accent2:       #4f46e5;
  --warn:          #d97706;
  --danger:        #dc2626;
  --text-primary:  #0f172a;
  --text-secondary:#64748b;
  --text-meta:     #94a3b8;
  --border:        rgba(15, 23, 42, 0.12);
  --nav-hover:     rgba(0, 0, 0, 0.04);
  --nav-active-bg: rgba(5, 150, 105, 0.12);
  --btn-ghost-hover: rgba(0, 0, 0, 0.05);
  --invite-bg:     rgba(0, 0, 0, 0.02);
  --agent-row-hover: rgba(0, 0, 0, 0.03);
  --shadow:        0 4px 24px rgba(15, 23, 42, 0.08);
}

*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }
html, body { height: 100%; }
body {
  font-family: 'Inter', system-ui, sans-serif;
  background: var(--bg-base);
  color: var(--text-primary);
  display: flex;
  font-size: 14px;
  line-height: 1.5;
  min-height: 100vh;
  min-height: 100dvh;
  padding-top: var(--safe-top);
}

/* ── Sidebar ─────────────────────────────────────────────────── */
.sidebar {
  width: 220px; flex-shrink: 0;
  background: var(--bg-panel);
  border-right: 1px solid var(--border);
  display: flex; flex-direction: column;
  padding: 0;
}
.logo {
  display: flex; align-items: center; gap: 10px;
  padding: 20px 16px;
  font-weight: 700; font-size: 1rem;
  border-bottom: 1px solid var(--border);
}
.logo-icon { font-size: 1.4rem; }
.sidebar-nav { flex: 1; padding: 12px 8px; display: flex; flex-direction: column; gap: 4px; }
.nav-item {
  display: flex; align-items: center; gap: 10px;
  padding: 10px 12px; border-radius: var(--radius-sm);
  color: var(--text-secondary); text-decoration: none;
  font-weight: 500; transition: background var(--transition), color var(--transition);
}
.nav-item:hover { background: var(--nav-hover); color: var(--text-primary); }
.nav-item.active { background: var(--nav-active-bg); color: var(--accent); }
.nav-icon { font-size: 1rem; }
.sidebar-footer {
  padding: 12px 16px; border-top: 1px solid var(--border);
  display: flex; align-items: center; gap: 8px;
}
.user-chip {
  flex: 1; font-size: .8rem; font-weight: 500;
  color: var(--text-secondary); overflow: hidden;
  text-overflow: ellipsis; white-space: nowrap;
}
.btn-icon {
  background: none; border: none; cursor: pointer;
  color: var(--text-secondary); font-size: 1.1rem;
  padding: 4px; border-radius: 6px;
  transition: color var(--transition);
}
.btn-icon:hover { color: var(--danger); }

/* ── Main ────────────────────────────────────────────────────── */
.main { flex: 1; display: flex; flex-direction: column; overflow: hidden; }

.page-header {
  padding: 24px 32px 0;
  border-bottom: 1px solid var(--border);
}
.page-header h1 { font-size: 1.4rem; font-weight: 700; margin-bottom: 16px; }
.tabs { display: flex; gap: 4px; }
.tab {
  padding: 10px 20px; border: none; background: none; cursor: pointer;
  color: var(--text-secondary); font-size: .9rem; font-weight: 500;
  border-bottom: 2px solid transparent; font-family: inherit;
  transition: color var(--transition), border-color var(--transition);
}
.tab:hover { color: var(--text-primary); }
.tab.active { color: var(--accent); border-bottom-color: var(--accent); }

/* ── Tab panes ───────────────────────────────────────────────── */
.tab-pane { display: none; flex: 1; overflow-y: auto; padding: 28px 32px; }
.tab-pane.active { display: block; }

/* ── Panel card ──────────────────────────────────────────────── */
.panel {
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  overflow: hidden;
  box-shadow: var(--shadow);
}
.panel-header {
  display: flex; align-items: center; justify-content: space-between;
  padding: 20px 24px;
  border-bottom: 1px solid var(--border);
}
.panel-header h2 { font-size: 1rem; font-weight: 600; }

/* ── Forms ───────────────────────────────────────────────────── */
.settings-form { padding: 24px; display: flex; flex-direction: column; gap: 20px; }
.form-row { display: grid; grid-template-columns: repeat(auto-fit, minmax(200px, 1fr)); gap: 16px; }
.form-group { display: flex; flex-direction: column; gap: 6px; }
label { font-size: .82rem; font-weight: 500; color: var(--text-secondary); }
input[type="text"], input[type="email"], input[type="password"],
input[type="number"], select, textarea {
  background: var(--bg-input);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  color: var(--text-primary);
  font-family: inherit;
  font-size: .9rem;
  padding: 10px 12px;
  outline: none;
  transition: border-color var(--transition);
  width: 100%;
}
input:focus, select:focus, textarea:focus { border-color: var(--accent); }
textarea { resize: vertical; min-height: 100px; }
select { cursor: pointer; }
.hint { font-size: .78rem; color: var(--text-secondary); margin-top: 4px; }

.input-reveal { position: relative; }
.input-reveal input { padding-right: 44px; }
.btn-reveal {
  position: absolute; right: 10px; top: 50%; transform: translateY(-50%);
  background: none; border: none; cursor: pointer; font-size: 1rem;
  color: var(--text-secondary); transition: color var(--transition);
}
.btn-reveal:hover { color: var(--text-primary); }

.form-actions { display: flex; gap: 10px; justify-content: flex-end; }

/* ── Buttons ─────────────────────────────────────────────────── */
.btn {
  padding: 10px 20px; border-radius: var(--radius-sm); border: none;
  font-family: inherit; font-size: .88rem; font-weight: 500;
  cursor: pointer; transition: all var(--transition);
}
.btn-primary { background: var(--accent); color: #fff; }
.btn-primary:hover { background: var(--accent-hover); }
.btn-ghost {
  background: transparent; color: var(--text-secondary);
  border: 1px solid var(--border);
}
.btn-ghost:hover { background: var(--btn-ghost-hover); color: var(--text-primary); }
.btn-danger { background: var(--danger); color: #fff; }
.btn-warn  { background: var(--warn); color: #000; }

/* ── Badges ──────────────────────────────────────────────────── */
.badge { font-size: .72rem; padding: 2px 7px; border-radius: 20px; font-weight: 500; }
.badge-warn { background: rgba(245,158,11,.15); color: var(--warn); }
.badge-ok   { background: rgba(16,185,129,.15); color: var(--accent); }
.badge-role { background: rgba(99,102,241,.15); color: var(--accent2); }

/* ── Info box (webhook URL) ──────────────────────────────────── */
.settings-info { padding: 0; }
.info-box {
  background: rgba(255,255,255,.03);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 14px 16px;
  display: flex; flex-direction: column; gap: 6px;
}
.info-box strong { font-size: .82rem; color: var(--text-secondary); }
.info-box code {
  font-family: 'Courier New', monospace; font-size: .82rem;
  color: var(--accent); word-break: break-all;
}

/* ── Provider info banner ────────────────────────────────────── */
.provider-info {
  background: rgba(99,102,241,.08);
  border: 1px solid rgba(99,102,241,.2);
  border-radius: var(--radius-sm);
  padding: 12px 16px;
  font-size: .83rem; color: var(--text-secondary);
  line-height: 1.6;
}
.provider-info:empty { display: none; }

.mono {
  font-family: ui-monospace, 'Courier New', monospace;
}

/* ── Agents list ─────────────────────────────────────────────── */
.agents-list { padding: 8px 0; }
.agent-row {
  display: flex; align-items: center; gap: 12px;
  padding: 14px 24px;
  border-bottom: 1px solid var(--border);
  transition: background var(--transition);
}
.agent-row:last-child { border-bottom: none; }
.agent-row:hover { background: var(--agent-row-hover); }
.agent-avatar {
  width: 38px; height: 38px; border-radius: 50%;
  display: flex; align-items: center; justify-content: center;
  font-weight: 700; font-size: .95rem; flex-shrink: 0;
}
.agent-info { flex: 1; }
.agent-name { font-weight: 600; font-size: .9rem; }
.agent-email { font-size: .78rem; color: var(--text-secondary); }
.agent-actions { display: flex; gap: 6px; align-items: center; }
.agent-inactive { opacity: .5; }

/* WhatsApp numbers — default agent */
.wa-number-row { align-items: flex-start; }
@media (min-width: 960px) {
  .wa-number-row { align-items: center; }
}
.wa-default-agent-col {
  flex: 0 1 240px;
  min-width: 200px;
  display: flex; flex-direction: column; gap: 6px;
}
.wa-agent-label {
  font-size: .68rem; font-weight: 600; color: var(--text-meta);
  text-transform: uppercase; letter-spacing: .04em;
}
.wa-number-agent-select {
  width: 100%;
  max-width: 280px;
  padding: 8px 10px;
  background: var(--bg-input);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  color: var(--text-primary);
  font-size: .85rem;
  font-family: inherit;
  cursor: pointer;
}
.wa-number-agent-select:focus {
  outline: none;
  border-color: var(--accent);
}

/* ── Invite form ─────────────────────────────────────────────── */
.invite-form {
  padding: 20px 24px;
  border-bottom: 1px solid var(--border);
  background: var(--invite-bg);
  animation: slideDown .2s ease;
}
@keyframes slideDown { from { opacity: 0; transform: translateY(-8px); } to { opacity: 1; transform: none; } }

/* ── AI test result ──────────────────────────────────────────── */
#aiTestResult {
  padding: 14px 16px;
  border-radius: var(--radius-sm);
  font-size: .85rem; line-height: 1.6;
}
#aiTestResult.ok   { background: rgba(16,185,129,.1); border: 1px solid rgba(16,185,129,.3); color: var(--accent); }
#aiTestResult.fail { background: rgba(239,68,68,.1);  border: 1px solid rgba(239,68,68,.3);  color: var(--danger); }

html[data-theme="light"] #aiTestResult.ok {
  color: #047857;
}
html[data-theme="light"] #aiTestResult.fail {
  color: #b91c1c;
}

/* ── Toast ───────────────────────────────────────────────────── */
.toast {
  position: fixed; bottom: 24px; right: 24px;
  background: var(--bg-card); border: 1px solid var(--border);
  padding: 12px 20px; border-radius: var(--radius-sm);
  box-shadow: var(--shadow); z-index: 9999;
  font-size: .88rem; font-weight: 500;
  animation: fadeUp .25s ease;
}
.toast.ok   { border-color: rgba(16,185,129,.4); color: var(--accent); }
.toast.fail { border-color: rgba(239,68,68,.4);  color: var(--danger); }
@keyframes fadeUp { from { opacity: 0; transform: translateY(10px); } to { opacity: 1; transform: none; } }

/* ── Misc ────────────────────────────────────────────────────── */
.hidden { display: none !important; }
.loading-spin { padding: 32px; text-align: center; color: var(--text-secondary); font-size: .9rem; }

/* Theme toggle (settings layout) */
.theme-toggle-btn {
  background: var(--bg-input);
  border: 1px solid var(--border);
  color: var(--text-primary);
  width: 36px;
  height: 36px;
  border-radius: 8px;
  cursor: pointer;
  font-size: 1.05rem;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: background var(--transition), border-color var(--transition);
  flex-shrink: 0;
}
.theme-toggle-btn:hover {
  border-color: var(--accent);
  background: var(--nav-hover);
}
.theme-toggle-icon { line-height: 1; }

/* ── Responsive (settings / admin) ─────────────────────────── */
@media (max-width: 768px) {
  body {
    flex-direction: column;
    overflow-x: hidden;
  }

  .sidebar {
    width: 100%;
    flex-shrink: 0;
    flex-direction: row;
    flex-wrap: wrap;
    align-items: center;
    border-right: none;
    border-bottom: 1px solid var(--border);
    padding: 8px 12px;
  }

  .logo {
    border-bottom: none;
    padding: 8px 12px;
    flex: 0 0 auto;
  }

  .sidebar-nav {
    flex: 1 1 auto;
    flex-direction: row;
    flex-wrap: wrap;
    padding: 4px 8px;
    justify-content: flex-start;
    gap: 4px;
  }

  .nav-item {
    padding: 8px 12px;
    font-size: 0.85rem;
  }

  .sidebar-footer {
    border-top: none;
    padding: 8px;
    margin-left: auto;
    width: auto;
    flex-wrap: wrap;
    justify-content: flex-end;
    gap: 6px;
  }

  .main {
    min-height: 0;
    flex: 1;
    overflow: visible;
  }

  .page-header {
    padding: 16px 16px 0;
  }

  .page-header h1 {
    font-size: 1.15rem;
    margin-bottom: 12px;
  }

  .tabs {
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
    flex-wrap: nowrap;
    padding-bottom: 4px;
    gap: 0;
    scrollbar-width: thin;
  }

  .tab {
    padding: 10px 14px;
    font-size: 0.82rem;
    white-space: nowrap;
  }

  .tab-pane {
    padding: 16px;
  }

  .panel-header {
    flex-wrap: wrap;
    gap: 10px;
  }

  .form-row {
    grid-template-columns: 1fr;
  }

  .toast {
    left: 12px;
    right: 12px;
    bottom: calc(16px + var(--safe-bottom));
  }

  .info-box code {
    font-size: 0.75rem;
  }
}

.checkbox-row {
  display: flex;
  align-items: flex-start;
  gap: 10px;
  cursor: pointer;
  font-size: 0.9rem;
  color: var(--text-primary);
}

.checkbox-row input {
  margin-top: 3px;
  flex-shrink: 0;
}
