:root {
  /* Brand */
  --brand: #0f766e;
  --brand-dark: #115e59;
  --brand-light: #f0fdfa;
  --brand-ring: rgba(15, 118, 110, 0.16);

  /* Semantic */
  --danger: #dc2626;
  --danger-light: #fef2f2;
  --success: #16a34a;
  --success-light: #f0fdf4;
  --warning: #d97706;
  --warning-light: #fffbeb;
  --info: #7c3aed;
  --info-light: #f5f3ff;

  /* Neutrals */
  --bg: #f8fafc;
  --surface: #ffffff;
  --surface-hover: #f8fafc;
  --border: #e6eaf0;
  --border-strong: #cbd5e1;
  --text: #0f172a;
  --text-muted: #64748b;
  --text-subtle: #94a3b8;

  /* Elevation & shape */
  --shadow-xs: 0 1px 2px rgba(15, 23, 42, 0.04);
  --shadow-sm: 0 1px 3px rgba(15, 23, 42, 0.06), 0 1px 2px rgba(15, 23, 42, 0.04);
  --shadow-md: 0 4px 12px rgba(15, 23, 42, 0.07), 0 1px 3px rgba(15, 23, 42, 0.05);
  --shadow-lg: 0 12px 32px rgba(15, 23, 42, 0.10), 0 2px 6px rgba(15, 23, 42, 0.05);
  --radius-sm: 8px;
  --radius-md: 12px;
  --radius-lg: 16px;

  --sidebar-w: 232px;
  --tabbar-h: 62px;

  font-family: 'Inter', -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
}

* { box-sizing: border-box; }

html { -webkit-text-size-adjust: 100%; }

body {
  background: var(--bg);
  color: var(--text);
  margin: 0;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  letter-spacing: -0.006em;
}

a { text-decoration: none; color: inherit; }

button, .btn { font-family: inherit; }
.btn { transition: background-color .15s ease, border-color .15s ease, color .15s ease, box-shadow .15s ease, transform .1s ease; }
.btn:active { transform: translateY(1px); }

::selection { background: var(--brand-ring); color: var(--brand-dark); }

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

/* ---------------- Sidebar (desktop) ---------------- */
.app-sidebar {
  width: var(--sidebar-w);
  flex-shrink: 0;
  background: var(--surface);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  position: sticky;
  top: 0;
  height: 100vh;
}

.sidebar-brand {
  padding: 22px 20px 16px;
  display: flex;
  align-items: center;
  border-bottom: 1px solid var(--border);
  margin-bottom: 10px;
}
.sidebar-brand img { height: 26px; width: auto; display: block; }
.sidebar-brand .brand-fallback {
  font-weight: 800; font-size: 1.05rem; color: var(--text); display: flex; align-items: center; gap: 8px; letter-spacing: -0.02em;
}
.sidebar-brand .brand-fallback i { color: var(--brand); }

.sidebar-nav {
  list-style: none;
  margin: 0;
  padding: 4px 12px;
  flex: 1;
  overflow-y: auto;
}

.sidebar-nav li a {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 9px 12px;
  border-radius: var(--radius-sm);
  color: var(--text-muted);
  font-weight: 500;
  font-size: 0.88rem;
  margin-bottom: 2px;
  position: relative;
  transition: background-color .15s ease, color .15s ease;
}

.sidebar-nav li a:hover { background: var(--surface-hover); color: var(--text); }
.sidebar-nav li.active a { background: var(--brand-light); color: var(--brand-dark); font-weight: 600; }
.sidebar-nav li.active a i { color: var(--brand); }
.sidebar-nav li a i { font-size: 1rem; width: 20px; text-align: center; flex-shrink: 0; }

.sidebar-section {
  font-size: 0.66rem;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  color: var(--text-subtle);
  padding: 18px 12px 6px;
  font-weight: 700;
}

.nav-badge {
  margin-left: auto;
  background: var(--danger);
  color: #fff;
  font-size: 0.66rem;
  font-weight: 700;
  border-radius: 999px;
  padding: 1px 7px;
  min-width: 18px;
  text-align: center;
  display: none;
}
.nav-badge.show { display: inline-block; }

.sidebar-user {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 14px 16px;
  border-top: 1px solid var(--border);
}

.avatar {
  width: 34px; height: 34px;
  border-radius: 50%;
  background: var(--brand);
  color: #fff;
  display: flex; align-items: center; justify-content: center;
  font-weight: 700;
  font-size: 0.85rem;
  flex-shrink: 0;
  overflow: hidden;
}

.sidebar-user-info { min-width: 0; }
.sidebar-user-info .name { font-weight: 600; font-size: 0.85rem; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.logout-link { font-size: 0.76rem; color: var(--text-subtle); }
.logout-link:hover { color: var(--danger); }

/* Mobile bottom tab bar */
.app-tabbar {
  display: none;
  position: fixed;
  bottom: 0; left: 0; right: 0;
  height: var(--tabbar-h);
  background: var(--surface);
  border-top: 1px solid var(--border);
  box-shadow: 0 -2px 12px rgba(15, 23, 42, 0.04);
  z-index: 40;
}

.app-main {
  flex: 1;
  min-width: 0;
  display: flex;
  flex-direction: column;
}

@media (max-width: 900px) {
  .app-sidebar { display: none; }
  .app-tabbar { display: flex; }
  .app-main { padding-bottom: var(--tabbar-h); }
  .app-tabbar a {
    flex: 1;
    display: flex; flex-direction: column; align-items: center; justify-content: center;
    gap: 3px; font-size: 0.66rem; color: var(--text-subtle); font-weight: 500;
  }
  .app-tabbar a i { font-size: 1.25rem; }
  .app-tabbar a.active { color: var(--brand); }
}

.page-header {
  padding: 24px 28px 14px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  flex-wrap: wrap;
}
.page-header h1 { font-size: 1.4rem; font-weight: 700; margin: 0; letter-spacing: -0.02em; }

.page-body { padding: 4px 28px 28px; flex: 1; min-height: 0; display: flex; flex-direction: column; }

.card-surface {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow-xs);
}

/* ---------------- Dialer ---------------- */
.dialer-wrap { max-width: 340px; margin: 10px auto; width: 100%; }
.dialer-display {
  text-align: center; padding: 22px 10px; min-height: 60px;
}
#dialer-number {
  width: 100%; border: none; outline: none; background: transparent; color: inherit;
  text-align: center; font-size: 2.1rem; font-weight: 300; letter-spacing: 0.02em;
  font-family: inherit; padding: 0;
}
#dialer-number::placeholder { color: var(--text-subtle); }
.dialer-keypad { display: grid; grid-template-columns: repeat(3, 1fr); gap: 16px; padding: 10px 16px; }
.dialer-key {
  aspect-ratio: 1; border-radius: 50%; border: none;
  background: var(--surface); box-shadow: var(--shadow-sm);
  font-size: 1.4rem; font-weight: 500; color: var(--text); display: flex; flex-direction: column; align-items: center; justify-content: center;
  cursor: pointer; user-select: none; transition: background-color .12s ease, transform .08s ease, box-shadow .12s ease;
}
.dialer-key:hover { box-shadow: var(--shadow-md); }
.dialer-key:active { background: var(--brand-light); transform: scale(0.96); }
.dialer-key small { font-size: 0.58rem; letter-spacing: 0.1em; color: var(--text-subtle); font-weight: 600; margin-top: 1px; }
.dialer-actions { display: flex; align-items: center; justify-content: center; gap: 20px; padding: 18px 20px 26px; position: relative; }
.call-fab {
  width: 66px; height: 66px; border-radius: 50%; background: var(--success); color: #fff; border: none;
  font-size: 1.5rem; display: flex; align-items: center; justify-content: center; cursor: pointer;
  box-shadow: 0 6px 16px rgba(22, 163, 74, 0.32); transition: transform .1s ease, box-shadow .15s ease;
}
.call-fab:hover { box-shadow: 0 8px 20px rgba(22, 163, 74, 0.4); }
.call-fab:active { transform: scale(0.96); }
.backspace-fab {
  position: absolute; right: 20px; width: 44px; height: 44px; border-radius: 50%; border: none; background: transparent;
  color: var(--text-muted); font-size: 1.3rem; display: flex; align-items: center; justify-content: center; cursor: pointer;
  transition: background-color .12s ease;
}
.backspace-fab:hover { background: var(--surface-hover); }

/* ---------------- Call overlay / active call bar ---------------- */
.call-overlay {
  position: fixed; inset: 0; background: rgba(8, 15, 25, 0.96); z-index: 200;
  display: flex; align-items: center; justify-content: center; color: #fff;
}
.call-overlay.d-none { display: none; }
.call-card { text-align: center; }
.call-avatar {
  width: 100px; height: 100px; border-radius: 50%; background: rgba(255,255,255,0.1);
  display: flex; align-items: center; justify-content: center; font-size: 2.5rem; margin: 0 auto 20px;
  border: 1px solid rgba(255,255,255,0.14); overflow: hidden;
}
.call-status-label { color: rgba(255,255,255,0.55); font-size: 0.82rem; margin-bottom: 6px; letter-spacing: 0.02em; }
.call-name { font-size: 1.55rem; font-weight: 700; letter-spacing: -0.01em; }
.call-number { color: rgba(255,255,255,0.65); margin-bottom: 44px; }
.call-actions { display: flex; gap: 64px; justify-content: center; }
.call-btn {
  width: 64px; height: 64px; border-radius: 50%; border: none; font-size: 1.4rem; color: #fff; cursor: pointer;
  box-shadow: 0 6px 20px rgba(0,0,0,0.35); transition: transform .1s ease;
}
.call-btn:active { transform: scale(0.94); }
.call-btn.answer { background: var(--success); }
.call-btn.decline { background: var(--danger); }

.active-call-bar {
  position: fixed; bottom: 0; left: var(--sidebar-w); right: 0; background: #0f172a; color: #fff;
  padding: 10px 20px; display: flex; align-items: center; gap: 20px; z-index: 150; flex-wrap: wrap;
  box-shadow: 0 -4px 20px rgba(0,0,0,0.15);
}
.active-call-bar.d-none { display: none; }
@media (max-width: 900px) { .active-call-bar { left: 0; bottom: var(--tabbar-h); } }
.active-call-name { font-weight: 600; }
.active-call-meta { font-size: 0.78rem; color: rgba(255,255,255,0.55); }
.active-call-controls { display: flex; gap: 10px; margin-left: auto; }
.ctrl-btn {
  width: 40px; height: 40px; border-radius: 50%; border: none; background: rgba(255,255,255,0.1); color: #fff; cursor: pointer;
  transition: background-color .12s ease;
}
.ctrl-btn:hover { background: rgba(255,255,255,0.18); }
.ctrl-btn.active { background: var(--brand); }
.ctrl-btn.end-call { background: var(--danger); }
.in-call-keypad { width: 100%; display: grid; grid-template-columns: repeat(6,1fr); gap: 6px; margin-top: 10px; }
.in-call-keypad button { padding: 8px; border-radius: 8px; border: none; background: rgba(255,255,255,0.08); color: #fff; }

/* ---------------- Messages ---------------- */
.split-view { display: flex; flex: 1; min-height: 0; border: 1px solid var(--border); border-radius: var(--radius-lg); background: var(--surface); overflow: hidden; box-shadow: var(--shadow-xs); }
.split-list { width: 320px; flex-shrink: 0; border-right: 1px solid var(--border); overflow-y: auto; }
.split-detail { flex: 1; min-width: 0; display: flex; flex-direction: column; }
@media (max-width: 900px) {
  .split-view { border: none; border-radius: 0; box-shadow: none; }
  .split-list { width: 100%; border-right: none; }
  .split-detail { display: none; }
  .split-view.show-detail .split-list { display: none; }
  .split-view.show-detail .split-detail { display: flex; }
}

.list-row {
  display: flex; gap: 12px; padding: 12px 16px; border-bottom: 1px solid var(--border); cursor: pointer; align-items: center;
  transition: background-color .12s ease;
}
.list-row:hover { background: var(--surface-hover); }
.list-row.active { background: var(--brand-light); }
.list-row .avatar { width: 42px; height: 42px; font-size: 1rem; }
.list-row-body { min-width: 0; flex: 1; }
.list-row-title { font-weight: 600; font-size: 0.9rem; display: flex; justify-content: space-between; gap: 8px; }
.list-row-sub { font-size: 0.8rem; color: var(--text-muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.list-row-time { font-size: 0.72rem; color: var(--text-subtle); flex-shrink: 0; }
.unread-dot { width: 9px; height: 9px; border-radius: 50%; background: var(--brand); flex-shrink: 0; box-shadow: 0 0 0 3px var(--brand-light); }

.thread-header { padding: 14px 18px; border-bottom: 1px solid var(--border); display: flex; align-items: center; gap: 12px; }
.thread-back { display: none; }
@media (max-width: 900px) { .thread-back { display: inline-flex; } }
.thread-messages { flex: 1; overflow-y: auto; padding: 20px; display: flex; flex-direction: column; gap: 8px; background: var(--bg); }
.msg-bubble { max-width: 68%; padding: 9px 14px; border-radius: 18px; font-size: 0.9rem; line-height: 1.4; position: relative; box-shadow: var(--shadow-xs); }
.msg-bubble.in { align-self: flex-start; background: var(--surface); color: var(--text); border-bottom-left-radius: 4px; }
.msg-bubble.out { align-self: flex-end; background: var(--brand); color: #fff; border-bottom-right-radius: 4px; box-shadow: 0 2px 8px rgba(15, 118, 110, 0.22); }
.msg-bubble img { max-width: 220px; border-radius: 10px; display: block; margin-top: 4px; }
.msg-meta { font-size: 0.68rem; color: var(--text-subtle); margin-top: 2px; text-align: center; }
.msg-bubble.failed { border: 1px solid var(--danger); }
.msg-status-fail { color: var(--danger); font-size: 0.7rem; text-align: right; }
.msg-status-fail a { color: var(--danger); text-decoration: underline; }

.thread-composer { border-top: 1px solid var(--border); padding: 12px 16px; display: flex; gap: 10px; align-items: flex-end; background: var(--surface); }
.thread-composer textarea {
  flex: 1; resize: none; border: 1px solid var(--border); border-radius: 20px; padding: 9px 16px; font-size: 0.9rem; max-height: 120px;
  font-family: inherit; transition: border-color .12s ease, box-shadow .12s ease;
}
.thread-composer textarea:focus { outline: none; border-color: var(--brand); box-shadow: 0 0 0 3px var(--brand-ring); }
.thread-composer button { border: none; background: var(--brand); color: #fff; width: 40px; height: 40px; border-radius: 50%; flex-shrink: 0; transition: background-color .12s ease; }
.thread-composer button:hover:not(:disabled) { background: var(--brand-dark); }
.thread-composer button:disabled { opacity: 0.5; }
.attach-btn { background: transparent !important; color: var(--text-muted) !important; font-size: 1.3rem; }
.attach-btn:hover { color: var(--brand) !important; }

/* ---------------- Generic ---------------- */
.empty-state { text-align: center; color: var(--text-subtle); padding: 60px 20px; }
.empty-state i { font-size: 2.2rem; margin-bottom: 10px; display: block; color: var(--border-strong); }

.table-wrap { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius-lg); overflow: hidden; box-shadow: var(--shadow-xs); }
table.app-table { width: 100%; border-collapse: collapse; }
table.app-table th { text-align: left; font-size: 0.7rem; text-transform: uppercase; letter-spacing: 0.06em; color: var(--text-subtle); font-weight: 700; padding: 12px 16px; border-bottom: 1px solid var(--border); background: var(--surface-hover); }
table.app-table td { padding: 12px 16px; border-bottom: 1px solid var(--border); font-size: 0.88rem; vertical-align: middle; }
table.app-table tr:last-child td { border-bottom: none; }
table.app-table tr.clickable { transition: background-color .12s ease; }
table.app-table tr.clickable:hover { background: var(--surface-hover); cursor: pointer; }

.badge-status { font-size: 0.7rem; padding: 3px 10px; border-radius: 999px; font-weight: 600; letter-spacing: 0.01em; }
.badge-status.missed, .badge-status.failed, .badge-status.disabled, .badge-status.undelivered { background: var(--danger-light); color: var(--danger); }
.badge-status.completed, .badge-status.active, .badge-status.delivered, .badge-status.sent { background: var(--success-light); color: var(--success); }
.badge-status.outbound, .badge-status.ringing, .badge-status.queued { background: var(--brand-light); color: var(--brand-dark); }
.badge-status.inbound { background: var(--info-light); color: var(--info); }
.badge-status.voicemail { background: var(--warning-light); color: var(--warning); }

.filter-pills { display: flex; gap: 8px; padding: 12px 28px 0; }
.filter-pill {
  padding: 6px 15px; border-radius: 999px; font-size: 0.8rem; font-weight: 600; background: var(--surface);
  border: 1px solid var(--border); color: var(--text-muted); cursor: pointer; transition: all .12s ease;
}
.filter-pill:hover { border-color: var(--border-strong); color: var(--text); }
.filter-pill.active { background: var(--brand); border-color: var(--brand); color: #fff; }

.btn-brand { background: var(--brand); border-color: var(--brand); color: #fff; box-shadow: var(--shadow-xs); }
.btn-brand:hover { background: var(--brand-dark); border-color: var(--brand-dark); color: #fff; }
.btn-outline-secondary { border-color: var(--border-strong); color: var(--text-muted); }
.btn-outline-secondary:hover { background: var(--surface-hover); border-color: var(--border-strong); color: var(--text); }

.toast-container-custom { position: fixed; top: 16px; right: 16px; z-index: 300; display: flex; flex-direction: column; gap: 8px; }
.toast-custom {
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius-md); padding: 12px 32px 12px 16px;
  box-shadow: var(--shadow-lg); min-width: 260px; position: relative;
  animation: toast-in .18s ease;
}
@keyframes toast-in { from { opacity: 0; transform: translateY(-6px); } to { opacity: 1; transform: translateY(0); } }
.toast-custom .t-title { font-weight: 700; font-size: 0.85rem; }
.toast-custom .t-body { font-size: 0.8rem; color: var(--text-muted); }
.toast-close {
  position: absolute; top: 8px; right: 8px; width: 20px; height: 20px; border: none; background: transparent;
  color: var(--text-subtle); font-size: 1.1rem; line-height: 1; border-radius: 50%; cursor: pointer;
  display: flex; align-items: center; justify-content: center;
}
.toast-close:hover { background: var(--surface-hover); color: var(--text); }

.masked-secret { font-family: 'SF Mono', ui-monospace, Menlo, monospace; letter-spacing: 1px; }

/* ---------------- Login ---------------- */
.login-page {
  background: radial-gradient(circle at 15% 15%, #134e4a 0%, #0f172a 55%, #0b1120 100%);
  min-height: 100vh; display: flex; align-items: center; justify-content: center; padding: 20px;
}
.login-card {
  background: var(--surface); border-radius: var(--radius-lg); padding: 44px 40px; width: 100%; max-width: 400px;
  box-shadow: 0 30px 80px rgba(0,0,0,0.35), 0 0 0 1px rgba(255,255,255,0.06);
}
.login-brand { text-align: center; margin-bottom: 28px; }
.login-brand img { height: 32px; width: auto; }
.login-brand .brand-fallback { font-weight: 800; font-size: 1.3rem; color: var(--text); }
.login-subtitle { text-align: center; color: var(--text-muted); font-size: 0.85rem; margin-top: -18px; margin-bottom: 26px; }
.login-card .form-control:focus { border-color: var(--brand); box-shadow: 0 0 0 3px var(--brand-ring); }
