:root {
  --wa-green: #075E54;
  --wa-green-light: #128C7E;
  --wa-accent: #25D366;
  --wa-bg: #f0f2f5;
  --wa-panel-bg: #ffffff;
  --wa-chat-bg: #efeae2;
  --wa-bubble-in: #ffffff;
  --wa-bubble-out: #d9fdd3;
  --wa-text: #111b21;
  --wa-text-muted: #667781;
  --wa-border: #e9edef;
  --wa-danger: #d9534f;
}

@media (prefers-color-scheme: dark) {
  :root {
    --wa-bg: #0b141a;
    --wa-panel-bg: #111b21;
    --wa-chat-bg: #0b141a;
    --wa-bubble-in: #202c33;
    --wa-bubble-out: #005c4b;
    --wa-text: #e9edef;
    --wa-text-muted: #8696a0;
    --wa-border: #222d34;
  }
}

* { box-sizing: border-box; }

body {
  margin: 0;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Helvetica, Arial, sans-serif;
  background: var(--wa-bg);
  color: var(--wa-text);
}

/* App shell + side nav */
.app-shell {
  display: flex;
  min-height: 100vh;
}
.side-nav {
  width: 220px;
  flex: 0 0 220px;
  background: var(--wa-green);
  color: white;
  display: flex;
  flex-direction: column;
  position: sticky;
  top: 0;
  height: 100vh;
  transition: width 0.15s ease, flex-basis 0.15s ease;
  overflow: hidden;
}
.side-nav-top {
  display: flex;
  align-items: center;
  justify-content: space-between;
  border-bottom: 1px solid rgba(255,255,255,0.15);
}
.side-nav-brand {
  padding: 20px 18px;
  font-weight: 600;
  font-size: 15px;
  line-height: 1.3;
  white-space: nowrap;
}
.sidebar-toggle-btn {
  background: none;
  border: none;
  color: white;
  opacity: 0.75;
  cursor: pointer;
  font-size: 16px;
  padding: 8px 14px;
}
.sidebar-toggle-btn:hover { opacity: 1; }
.side-nav-links { list-style: none; margin: 0; padding: 10px 0; flex: 1; }
.side-nav-links li { margin: 2px 8px; }
.side-nav-links a {
  display: flex;
  align-items: center;
  gap: 10px;
  color: white;
  text-decoration: none;
  padding: 10px 12px;
  border-radius: 8px;
  font-size: 14px;
  opacity: 0.85;
  white-space: nowrap;
}
.side-nav-links a:hover { opacity: 1; background: rgba(255,255,255,0.08); }
.side-nav-links a.active { opacity: 1; background: rgba(255,255,255,0.16); font-weight: 600; }
.nav-icon { flex: 0 0 auto; font-size: 16px; }
.side-nav-user {
  padding: 14px 18px;
  border-top: 1px solid rgba(255,255,255,0.15);
  font-size: 13px;
  white-space: nowrap;
}
.side-nav-email { opacity: 0.85; margin-bottom: 6px; word-break: break-all; }

.mini-label { display: none; }

/* Collapsed state */
html.sidebar-collapsed .side-nav { width: 64px; flex-basis: 64px; }
html.sidebar-collapsed .side-nav .full-label { display: none; }
html.sidebar-collapsed .side-nav .mini-label { display: inline; }
html.sidebar-collapsed .side-nav-brand { padding: 20px 0; text-align: center; }
html.sidebar-collapsed .side-nav-top { justify-content: center; flex-direction: column; gap: 4px; padding-bottom: 6px; }
html.sidebar-collapsed .sidebar-toggle-btn { transform: rotate(180deg); }
html.sidebar-collapsed .side-nav-links a { justify-content: center; padding: 10px 0; }
html.sidebar-collapsed .side-nav-user { text-align: center; padding: 14px 6px; }

.page-content { flex: 1; min-width: 0; }

.link-btn {
  background: none;
  border: none;
  color: white;
  cursor: pointer;
  text-decoration: underline;
  font-size: 14px;
  padding: 0;
}
.link-btn.danger { color: var(--wa-danger); }

/* Login */
.login-page {
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: 100vh;
}
.login-card {
  background: var(--wa-panel-bg);
  padding: 40px;
  border-radius: 12px;
  box-shadow: 0 2px 12px rgba(0,0,0,0.1);
  width: 340px;
}
.login-card h1 { font-size: 20px; margin: 0 0 4px; }
.subtitle { color: var(--wa-text-muted); font-size: 14px; margin: 0 0 20px; }

form label {
  display: block;
  font-size: 13px;
  color: var(--wa-text-muted);
  margin-bottom: 14px;
}
form input, form select, form textarea {
  display: block;
  width: 100%;
  margin-top: 4px;
  padding: 10px;
  border: 1px solid var(--wa-border);
  border-radius: 8px;
  background: var(--wa-panel-bg);
  color: var(--wa-text);
  font-size: 14px;
  font-family: inherit;
}
form textarea { resize: vertical; }
.checkbox-row { display: flex; align-items: center; gap: 8px; }
.checkbox-row input { width: auto; display: inline; }

.primary-btn {
  background: var(--wa-green-light);
  color: white;
  border: none;
  padding: 10px 18px;
  border-radius: 8px;
  cursor: pointer;
  font-size: 14px;
  font-weight: 600;
}
.primary-btn:hover { background: var(--wa-green); }

.ghost-btn {
  background: transparent;
  border: 1px solid var(--wa-green-light);
  color: var(--wa-green-light);
  padding: 8px 12px;
  border-radius: 8px;
  cursor: pointer;
  font-size: 13px;
  width: 100%;
}
.ghost-btn:hover { background: var(--wa-green-light); color: white; }

.alert {
  background: #fdecea;
  color: #a4342a;
  padding: 10px 14px;
  border-radius: 8px;
  font-size: 13px;
  margin-bottom: 16px;
}
.alert.success { background: #e6f7ec; color: #1a7a3f; }

/* Form pages */
.form-page { display: flex; justify-content: center; padding: 32px 16px; }
.form-card {
  background: var(--wa-panel-bg);
  padding: 32px;
  border-radius: 12px;
  width: 480px;
  max-width: 100%;
  box-shadow: 0 1px 4px rgba(0,0,0,0.08);
}
.form-card.wide { width: 720px; }
.form-card h1 { font-size: 20px; margin: 0 0 4px; }
.form-card h2 { font-size: 16px; margin: 24px 0 12px; }
.hint { color: var(--wa-text-muted); font-size: 13px; }
.callout {
  margin-top: 24px;
  padding: 14px;
  background: var(--wa-bg);
  border-radius: 8px;
  font-size: 13px;
}
.callout p { margin: 6px 0 0; color: var(--wa-text-muted); }

.status-row { display: flex; align-items: center; gap: 6px; font-size: 12px; color: var(--wa-text-muted); }
.status-dot { width: 8px; height: 8px; border-radius: 50%; background: #9aa0a6; display: inline-block; }
.status-dot[data-status="running"], .status-dot[data-status="connected"] { background: var(--wa-accent); }
.status-dot[data-status*="error"] { background: var(--wa-danger); }

.user-table { width: 100%; border-collapse: collapse; font-size: 14px; margin-top: 12px; }
.user-table th, .user-table td { text-align: left; padding: 8px 6px; border-bottom: 1px solid var(--wa-border); }
.role-badge { padding: 2px 8px; border-radius: 10px; font-size: 12px; background: var(--wa-bg); }
.role-super_admin { background: #fde7cf; color: #8a4b00; }
.role-admin { background: #e0eefe; color: #0b5fa5; }
.role-user { background: var(--wa-bg); color: var(--wa-text-muted); }

/* Chat shell */
.chat-shell { padding: 0; }
.chat-app {
  display: grid;
  grid-template-columns: 340px 1fr;
  height: 100vh;
  background: var(--wa-panel-bg);
}
.chat-sidebar {
  border-right: 1px solid var(--wa-border);
  display: flex;
  flex-direction: column;
  overflow: hidden;
}
.sidebar-header { padding: 16px; border-bottom: 1px solid var(--wa-border); }
.sidebar-header h2 { margin: 0 0 6px; font-size: 16px; }
.sidebar-actions { padding: 10px 16px; }
.conversation-list { list-style: none; margin: 0; padding: 0; overflow-y: auto; flex: 1; }
.conversation-item {
  display: block;
  padding: 12px 16px;
  border-bottom: 1px solid var(--wa-border);
  cursor: pointer;
}
.conversation-item:hover { background: var(--wa-bg); }
.conversation-item.active { background: var(--wa-bg); }
.conv-top-row { display: flex; justify-content: space-between; font-size: 14px; font-weight: 600; }
.conv-time { font-weight: 400; font-size: 12px; color: var(--wa-text-muted); }
.conv-preview { font-size: 13px; color: var(--wa-text-muted); margin-top: 2px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.conv-badge { display: inline-block; font-size: 11px; padding: 1px 6px; border-radius: 8px; background: var(--wa-accent); color: white; margin-left: 6px; }

.empty-state { padding: 24px 16px; color: var(--wa-text-muted); font-size: 13px; }
.empty-state a { color: var(--wa-green-light); }

.chat-panel { display: flex; flex-direction: column; background: var(--wa-chat-bg); }
.chat-header { padding: 14px 20px; background: var(--wa-panel-bg); border-bottom: 1px solid var(--wa-border); }
.chat-header-title { font-weight: 600; }
.chat-header-sub { font-size: 12px; color: var(--wa-text-muted); }

.chat-messages { flex: 1; overflow-y: auto; padding: 20px; display: flex; flex-direction: column; gap: 6px; }
.chat-placeholder { margin: auto; color: var(--wa-text-muted); font-size: 14px; }

.bubble-row { display: flex; }
.bubble-row.caller { justify-content: flex-start; }
.bubble-row.agent { justify-content: flex-end; }
.bubble {
  max-width: 60%;
  padding: 8px 12px;
  border-radius: 10px;
  font-size: 14px;
  line-height: 1.4;
  box-shadow: 0 1px 1px rgba(0,0,0,0.08);
  position: relative;
}
.bubble-row.caller .bubble { background: var(--wa-bubble-in); border-top-left-radius: 2px; }
.bubble-row.agent .bubble { background: var(--wa-bubble-out); border-top-right-radius: 2px; }
.bubble .bubble-label { font-size: 11px; font-weight: 600; color: var(--wa-green-light); margin-bottom: 2px; }
.bubble .bubble-time { font-size: 10px; color: var(--wa-text-muted); text-align: right; margin-top: 4px; }

/* Connector cards */
.connector-card {
  background: var(--wa-bg);
  border: 1px solid var(--wa-border);
  border-radius: 10px;
  padding: 20px;
  margin-bottom: 20px;
}
.connector-card h2 { margin: 0 0 4px; font-size: 15px; }
.connector-card .card-sub { color: var(--wa-text-muted); font-size: 12px; margin: 0 0 16px; }
.readonly-field {
  padding: 10px;
  border: 1px dashed var(--wa-border);
  border-radius: 8px;
  font-size: 14px;
  background: var(--wa-panel-bg);
  color: var(--wa-text-muted);
}

/* Logs page */
.logs-page { padding: 0; }
.logs-shell { display: flex; flex-direction: column; height: 100vh; }
.logs-header { padding: 16px 24px; background: var(--wa-panel-bg); border-bottom: 1px solid var(--wa-border); display: flex; justify-content: space-between; align-items: center; }
.logs-header h1 { margin: 0; font-size: 18px; }
.logs-list { flex: 1; overflow-y: auto; padding: 8px 24px; font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace; font-size: 12.5px; }
.log-row { display: flex; gap: 10px; padding: 5px 0; border-bottom: 1px solid var(--wa-border); align-items: baseline; }
.log-ts { color: var(--wa-text-muted); flex: 0 0 150px; }
.log-level { flex: 0 0 52px; font-weight: 700; text-transform: uppercase; font-size: 11px; }
.log-level.info { color: var(--wa-green-light); }
.log-level.warn { color: #b98900; }
.log-level.error { color: var(--wa-danger); }
.log-category { flex: 0 0 100px; color: var(--wa-text-muted); }
.log-message { flex: 1; word-break: break-word; }
.log-callid { color: var(--wa-text-muted); font-size: 11px; }
