:root {
  --bg: #f5f6fb;
  --surface: #ffffff;
  --surface-2: #f0f2f8;
  --border: #e3e6ef;
  --text: #171a2c;
  --text-2: #5b6079;
  --text-3: #8b90a8;
  --primary: #5b4cf0;
  --primary-2: #7a5cff;
  --primary-soft: #eeebff;
  --success: #0f9d6b;
  --success-soft: #e2f6ee;
  --warning: #b7791f;
  --warning-soft: #fdf3e0;
  --danger: #dc3c4f;
  --danger-soft: #fde8eb;
  --info: #2b7de9;
  --info-soft: #e5f0fd;
  --shadow-sm: 0 1px 2px rgba(20, 22, 50, .06);
  --shadow: 0 1px 2px rgba(20, 22, 50, .05), 0 6px 24px rgba(20, 22, 50, .06);
  --shadow-lg: 0 24px 60px rgba(20, 22, 50, .18);
  --radius: 16px;
  --radius-sm: 10px;
  --sidebar: 248px;
  --font: 'Inter', system-ui, -apple-system, 'Segoe UI', Roboto, 'Noto Sans', sans-serif;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0e1020;
    --surface: #171a2e;
    --surface-2: #1f2339;
    --border: #2a2f4a;
    --text: #eef0fb;
    --text-2: #a5aac6;
    --text-3: #757b9b;
    --primary: #8a7bff;
    --primary-2: #a08dff;
    --primary-soft: #2a2550;
    --success: #34d399;
    --success-soft: #133a2e;
    --warning: #f6ad55;
    --warning-soft: #3d2e14;
    --danger: #f87171;
    --danger-soft: #421d24;
    --info: #60a5fa;
    --info-soft: #172c4a;
    --shadow: 0 1px 2px rgba(0, 0, 0, .3), 0 6px 24px rgba(0, 0, 0, .25);
    --shadow-lg: 0 24px 60px rgba(0, 0, 0, .5);
  }
}

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body {
  font-family: var(--font);
  background: var(--bg);
  color: var(--text);
  font-size: 15px;
  line-height: 1.5;
  -webkit-font-smoothing: antialiased;
  -webkit-tap-highlight-color: transparent;
}
a { color: var(--primary); text-decoration: none; }
button, input, select, textarea { font: inherit; color: inherit; }
svg.i { width: 20px; height: 20px; stroke: currentColor; fill: none; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; flex-shrink: 0; }
.hidden { display: none !important; }

/* ---------- Layout ---------- */
.app { display: flex; min-height: 100vh; }
.sidebar {
  width: var(--sidebar);
  position: fixed; inset: 0 auto 0 0;
  background: var(--surface);
  border-right: 1px solid var(--border);
  display: flex; flex-direction: column;
  padding: 20px 14px;
  z-index: 20;
}
.brand { display: flex; align-items: center; gap: 12px; padding: 4px 10px 24px; font-weight: 700; font-size: 17px; letter-spacing: -.01em; }
.brand-logo {
  width: 38px; height: 38px; border-radius: 12px;
  background: linear-gradient(135deg, var(--primary), #c053ff);
  display: grid; place-items: center; color: #fff;
  box-shadow: 0 6px 18px rgba(91, 76, 240, .35);
}
.brand-logo svg.i { width: 20px; height: 20px; }
.brand small { display: block; font-weight: 500; font-size: 12px; color: var(--text-3); }
.nav { display: flex; flex-direction: column; gap: 4px; }
.nav a {
  display: flex; align-items: center; gap: 12px;
  padding: 10px 12px; border-radius: 12px;
  color: var(--text-2); font-weight: 500;
  transition: background .15s, color .15s;
}
.nav a:hover { background: var(--surface-2); color: var(--text); }
.nav a.active { background: var(--primary-soft); color: var(--primary); }
.sidebar-foot { margin-top: auto; display: flex; flex-direction: column; gap: 8px; }
.device-pill {
  display: flex; align-items: center; gap: 10px;
  padding: 10px 12px; border-radius: 12px; background: var(--surface-2);
  font-size: 13px; color: var(--text-2);
}
.dot { width: 9px; height: 9px; border-radius: 50%; background: var(--text-3); flex-shrink: 0; }
.dot.ok { background: var(--success); box-shadow: 0 0 0 4px var(--success-soft); }
.dot.bad { background: var(--danger); box-shadow: 0 0 0 4px var(--danger-soft); }

.main { flex: 1; margin-left: var(--sidebar); min-width: 0; }
.topbar {
  position: sticky; top: 0; z-index: 10;
  display: flex; align-items: center; gap: 12px;
  padding: 18px 32px;
  background: color-mix(in srgb, var(--bg) 85%, transparent);
  backdrop-filter: saturate(1.4) blur(12px);
  -webkit-backdrop-filter: saturate(1.4) blur(12px);
}
.topbar h1 { font-size: 22px; margin: 0; letter-spacing: -.02em; flex: 1; }
.topbar .mobile-brand { display: none; }
.content { padding: 8px 32px 48px; max-width: 1180px; }

.bottom-nav { display: none; }

/* ---------- Cards & grid ---------- */
.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow-sm);
}
.card-pad { padding: 20px; }
.card-head { display: flex; align-items: center; gap: 12px; padding: 18px 20px; border-bottom: 1px solid var(--border); }
.card-head h2, .card-head h3 { margin: 0; font-size: 16px; flex: 1; }
.grid { display: grid; gap: 16px; }
.grid-4 { grid-template-columns: repeat(4, 1fr); }
.grid-2 { grid-template-columns: 1fr 1fr; }
.grid-3 { grid-template-columns: repeat(3, 1fr); }
.stack { display: flex; flex-direction: column; gap: 16px; }
.row { display: flex; align-items: center; gap: 10px; flex-wrap: wrap; }
.spacer { flex: 1; }
.muted { color: var(--text-2); }
.small { font-size: 13px; }
.tiny { font-size: 12px; color: var(--text-3); }

.stat { padding: 18px 20px; display: flex; gap: 14px; align-items: center; }
.stat-icon { width: 44px; height: 44px; border-radius: 14px; display: grid; place-items: center; }
.stat-icon.p { background: var(--primary-soft); color: var(--primary); }
.stat-icon.s { background: var(--success-soft); color: var(--success); }
.stat-icon.w { background: var(--warning-soft); color: var(--warning); }
.stat-icon.d { background: var(--danger-soft); color: var(--danger); }
.stat b { display: block; font-size: 24px; letter-spacing: -.02em; line-height: 1.2; }
.stat span { font-size: 13px; color: var(--text-2); }

.hero {
  border-radius: var(--radius);
  padding: 24px;
  color: #fff;
  background: radial-gradient(120% 140% at 100% 0%, #c053ff 0%, transparent 55%), linear-gradient(135deg, #4f46e5, #6d4cf5);
  display: flex; align-items: center; gap: 20px; flex-wrap: wrap;
  box-shadow: 0 14px 40px rgba(91, 76, 240, .3);
}
.hero h2 { margin: 0 0 4px; font-size: 20px; }
.hero p { margin: 0; opacity: .85; }
.hero .btn { background: #fff; color: #4f46e5; }
.hero .btn.ghost { background: rgba(255, 255, 255, .16); color: #fff; }

/* ---------- Buttons ---------- */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 8px;
  height: 42px; padding: 0 16px;
  border-radius: 12px; border: 1px solid transparent;
  background: var(--primary); color: #fff;
  font-weight: 600; font-size: 14px; cursor: pointer;
  transition: transform .08s, background .15s, box-shadow .15s, opacity .15s;
  white-space: nowrap; text-decoration: none;
}
.btn:hover { background: var(--primary-2); }
.btn:active { transform: scale(.98); }
.btn:disabled { opacity: .5; cursor: not-allowed; }
.btn.secondary { background: var(--surface); color: var(--text); border-color: var(--border); box-shadow: var(--shadow-sm); }
.btn.secondary:hover { background: var(--surface-2); }
.btn.ghost { background: transparent; color: var(--text-2); }
.btn.ghost:hover { background: var(--surface-2); color: var(--text); }
.btn.danger { background: var(--danger); }
.btn.danger-soft { background: var(--danger-soft); color: var(--danger); }
.btn.sm { height: 34px; padding: 0 12px; font-size: 13px; border-radius: 10px; }
.btn.icon { width: 40px; padding: 0; }
.btn.sm.icon { width: 34px; }
.btn.lg { height: 52px; padding: 0 22px; font-size: 15px; border-radius: 14px; }
.btn.block { width: 100%; }

/* ---------- Forms ---------- */
label.field { display: flex; flex-direction: column; gap: 6px; font-size: 13px; font-weight: 600; color: var(--text-2); }
.input, select.input, textarea.input {
  width: 100%;
  height: 44px; padding: 0 14px;
  border-radius: 12px; border: 1px solid var(--border);
  background: var(--surface); color: var(--text);
  font-size: 15px; font-weight: 400;
  outline: none; transition: border .15s, box-shadow .15s;
}
textarea.input { height: auto; min-height: 130px; padding: 12px 14px; resize: vertical; line-height: 1.55; }
.input:focus { border-color: var(--primary); box-shadow: 0 0 0 4px var(--primary-soft); }
.input-icon { position: relative; flex: 1; min-width: 180px; }
.input-icon svg.i { position: absolute; left: 13px; top: 50%; transform: translateY(-50%); color: var(--text-3); width: 18px; height: 18px; }
.input-icon .input { padding-left: 40px; }
.hint { font-size: 12px; font-weight: 400; color: var(--text-3); }

.switch { display: flex; align-items: flex-start; gap: 12px; cursor: pointer; user-select: none; }
.switch input { display: none; }
.switch .track { width: 44px; height: 26px; border-radius: 99px; background: var(--border); position: relative; flex-shrink: 0; transition: background .2s; margin-top: 1px; }
.switch .track::after { content: ''; position: absolute; top: 3px; left: 3px; width: 20px; height: 20px; border-radius: 50%; background: #fff; box-shadow: var(--shadow-sm); transition: transform .2s; }
.switch input:checked + .track { background: var(--primary); }
.switch input:checked + .track::after { transform: translateX(18px); }
.switch b { display: block; font-size: 14px; }

.check { width: 20px; height: 20px; accent-color: var(--primary); cursor: pointer; flex-shrink: 0; }

/* ---------- Tabs / chips / badges ---------- */
.tabs { display: inline-flex; padding: 4px; background: var(--surface-2); border-radius: 12px; gap: 4px; flex-wrap: wrap; }
.tabs button { border: 0; background: transparent; padding: 8px 14px; border-radius: 9px; font-weight: 600; font-size: 13px; color: var(--text-2); cursor: pointer; display: inline-flex; gap: 6px; align-items: center; }
.tabs button.active { background: var(--surface); color: var(--text); box-shadow: var(--shadow-sm); }
.tabs .count { background: var(--primary); color: #fff; border-radius: 99px; font-size: 11px; padding: 0 7px; line-height: 18px; }

.chips { display: flex; flex-wrap: wrap; gap: 8px; }
.chip {
  display: inline-flex; align-items: center; gap: 6px;
  padding: 7px 12px; border-radius: 99px;
  border: 1px solid var(--border); background: var(--surface);
  font-size: 13px; font-weight: 500; cursor: pointer; color: var(--text-2);
  transition: all .15s;
}
.chip:hover { border-color: var(--primary); color: var(--text); }
.chip.on { background: var(--primary-soft); border-color: var(--primary); color: var(--primary); }
.chip .x { cursor: pointer; opacity: .6; display: inline-flex; }
.chip .x:hover { opacity: 1; }
.chip small { color: var(--text-3); }

.badge { display: inline-flex; align-items: center; gap: 5px; padding: 3px 10px; border-radius: 99px; font-size: 12px; font-weight: 600; white-space: nowrap; }
.badge::before { content: ''; width: 6px; height: 6px; border-radius: 50%; background: currentColor; }
.badge.queued, .badge.pending { background: var(--warning-soft); color: var(--warning); }
.badge.sent { background: var(--info-soft); color: var(--info); }
.badge.delivered { background: var(--success-soft); color: var(--success); }
.badge.failed { background: var(--danger-soft); color: var(--danger); }
.tag { display: inline-block; padding: 2px 9px; border-radius: 7px; background: var(--surface-2); color: var(--text-2); font-size: 12px; font-weight: 500; }

/* ---------- Lists ---------- */
.list { display: flex; flex-direction: column; }
.list-item {
  display: flex; align-items: center; gap: 14px;
  padding: 14px 20px; border-bottom: 1px solid var(--border);
  transition: background .12s;
}
.list-item:last-child { border-bottom: 0; }
.list-item:hover { background: var(--surface-2); }
.list-item.selectable { cursor: pointer; }
.avatar {
  width: 40px; height: 40px; border-radius: 12px; flex-shrink: 0;
  display: grid; place-items: center; font-weight: 700; font-size: 14px;
  color: #fff;
}
.li-main { flex: 1; min-width: 0; }
.li-title { font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.li-sub { font-size: 13px; color: var(--text-2); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.li-meta { display: flex; align-items: center; gap: 8px; flex-shrink: 0; }
.li-body { font-size: 13px; color: var(--text-2); margin-top: 2px; display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden; }
.pick-list { max-height: 360px; overflow: auto; border-top: 1px solid var(--border); }
.pick-list .list-item { padding: 10px 16px; }

.empty { text-align: center; padding: 48px 20px; color: var(--text-2); }
.empty .em-icon { width: 64px; height: 64px; margin: 0 auto 14px; border-radius: 20px; background: var(--primary-soft); color: var(--primary); display: grid; place-items: center; }
.empty .em-icon svg.i { width: 28px; height: 28px; }
.empty h3 { color: var(--text); margin: 0 0 6px; }
.empty p { margin: 0 0 18px; }

.toolbar { display: flex; gap: 10px; flex-wrap: wrap; align-items: center; margin-bottom: 16px; }

/* ---------- Templates ---------- */
.tpl-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(280px, 1fr)); gap: 16px; }
.tpl { padding: 18px; display: flex; flex-direction: column; gap: 10px; transition: transform .15s, box-shadow .15s; }
.tpl:hover { transform: translateY(-2px); box-shadow: var(--shadow); }
.tpl h3 { margin: 0; font-size: 15px; }
.tpl p { margin: 0; color: var(--text-2); font-size: 14px; white-space: pre-wrap; word-break: break-word; flex: 1; }
.tpl .row { margin-top: 4px; }

/* ---------- Composer ---------- */
.composer { display: grid; grid-template-columns: minmax(0, 1.25fr) minmax(0, 1fr); gap: 16px; align-items: start; }
.step-n { width: 26px; height: 26px; border-radius: 8px; background: var(--primary-soft); color: var(--primary); display: grid; place-items: center; font-weight: 700; font-size: 13px; }
.counter { display: flex; gap: 8px; flex-wrap: wrap; align-items: center; font-size: 13px; }
.counter .pill { padding: 4px 10px; border-radius: 99px; background: var(--surface-2); color: var(--text-2); font-weight: 600; }
.counter .pill.good { background: var(--success-soft); color: var(--success); }
.counter .pill.warn { background: var(--warning-soft); color: var(--warning); }
.phone-preview {
  background: var(--surface-2); border-radius: 22px; padding: 18px;
  border: 1px solid var(--border);
}
.bubble {
  background: var(--surface); border: 1px solid var(--border);
  border-radius: 18px 18px 18px 6px; padding: 12px 14px;
  font-size: 14px; white-space: pre-wrap; word-break: break-word; box-shadow: var(--shadow-sm);
  min-height: 46px;
}
.bubble.empty-b { color: var(--text-3); font-style: italic; }
.summary-box { position: sticky; top: 84px; }
.sum-line { display: flex; justify-content: space-between; padding: 8px 0; border-bottom: 1px dashed var(--border); font-size: 14px; }
.sum-line:last-of-type { border-bottom: 0; }
.sum-line b { font-size: 15px; }

/* ---------- Provider choice ---------- */
.choice { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }
.choice label {
  border: 2px solid var(--border); border-radius: 14px; padding: 16px;
  cursor: pointer; display: flex; gap: 12px; align-items: flex-start; transition: border .15s, background .15s;
}
.choice label:hover { border-color: var(--primary-2); }
.choice input { display: none; }
.choice label.on { border-color: var(--primary); background: var(--primary-soft); }
.choice b { display: block; }
.steps { margin: 0; padding-left: 20px; color: var(--text-2); font-size: 14px; }
.steps li { margin-bottom: 6px; }
.alert { padding: 12px 14px; border-radius: 12px; font-size: 14px; display: flex; gap: 10px; align-items: flex-start; }
.alert.ok { background: var(--success-soft); color: var(--success); }
.alert.bad { background: var(--danger-soft); color: var(--danger); }
.alert.info { background: var(--info-soft); color: var(--info); }
.alert.warn { background: var(--warning-soft); color: var(--warning); }

/* ---------- Modal & toast ---------- */
.modal-wrap {
  position: fixed; inset: 0; z-index: 100;
  background: rgba(12, 14, 30, .5);
  display: flex; align-items: center; justify-content: center; padding: 16px;
  animation: fade .15s ease-out;
}
.modal {
  width: 100%; max-width: 520px; max-height: calc(100vh - 32px); overflow: auto;
  background: var(--surface); border-radius: 20px; box-shadow: var(--shadow-lg);
  animation: pop .18s ease-out;
}
.modal-head { display: flex; align-items: center; padding: 20px 22px 0; gap: 10px; }
.modal-head h3 { margin: 0; font-size: 18px; flex: 1; }
.modal-body { padding: 18px 22px; display: flex; flex-direction: column; gap: 14px; }
.modal-foot { padding: 0 22px 22px; display: flex; gap: 10px; justify-content: flex-end; flex-wrap: wrap; }
@keyframes fade { from { opacity: 0; } }
@keyframes pop { from { opacity: 0; transform: translateY(10px) scale(.98); } }

.toasts { position: fixed; z-index: 200; right: 20px; bottom: 20px; display: flex; flex-direction: column; gap: 10px; max-width: calc(100vw - 40px); }
.toast {
  background: var(--text); color: var(--bg);
  padding: 12px 16px; border-radius: 12px; box-shadow: var(--shadow-lg);
  font-size: 14px; font-weight: 500; display: flex; gap: 10px; align-items: center;
  animation: pop .2s ease-out; max-width: 420px;
}
.toast.err { background: var(--danger); color: #fff; }
.toast.ok { background: var(--success); color: #fff; }

.spinner { width: 20px; height: 20px; border: 2.5px solid var(--border); border-top-color: var(--primary); border-radius: 50%; animation: spin .7s linear infinite; }
.loading { display: grid; place-items: center; padding: 60px; }
@keyframes spin { to { transform: rotate(360deg); } }

.pager { display: flex; gap: 8px; align-items: center; justify-content: center; padding: 14px; }

/* ---------- Login ---------- */
.login-page {
  min-height: 100vh; display: grid; place-items: center; padding: 20px;
  background:
    radial-gradient(60% 50% at 15% 10%, rgba(122, 92, 255, .25), transparent 70%),
    radial-gradient(50% 40% at 90% 90%, rgba(192, 83, 255, .2), transparent 70%),
    var(--bg);
}
.login-card { width: 100%; max-width: 400px; padding: 32px; border-radius: 24px; background: var(--surface); box-shadow: var(--shadow-lg); border: 1px solid var(--border); }
.login-card .brand { padding: 0 0 8px; justify-content: center; flex-direction: column; text-align: center; }
.login-card .brand-logo { width: 56px; height: 56px; border-radius: 18px; }
.login-card .brand-logo svg.i { width: 28px; height: 28px; }
.login-card h1 { font-size: 22px; margin: 4px 0 0; letter-spacing: -.02em; }

/* ---------- Responsive ---------- */
@media (max-width: 1100px) {
  .grid-4 { grid-template-columns: repeat(2, 1fr); }
  .composer { grid-template-columns: 1fr; }
  .summary-box { position: static; }
}

@media (max-width: 860px) {
  .sidebar { display: none; }
  .main { margin-left: 0; padding-bottom: calc(76px + env(safe-area-inset-bottom)); }
  .topbar { padding: 14px 16px; }
  .topbar h1 { font-size: 19px; }
  .topbar .mobile-brand { display: grid; width: 34px; height: 34px; border-radius: 11px; }
  .topbar .mobile-brand svg.i { width: 18px; height: 18px; }
  .content { padding: 4px 16px 32px; }
  .grid-2, .grid-3 { grid-template-columns: 1fr; }
  .choice { grid-template-columns: 1fr; }
  .bottom-nav {
    display: flex; position: fixed; left: 0; right: 0; bottom: 0; z-index: 30;
    background: color-mix(in srgb, var(--surface) 92%, transparent);
    backdrop-filter: blur(14px); -webkit-backdrop-filter: blur(14px);
    border-top: 1px solid var(--border);
    padding: 6px 6px calc(6px + env(safe-area-inset-bottom));
  }
  .bottom-nav a {
    flex: 1; display: flex; flex-direction: column; align-items: center; gap: 2px;
    padding: 6px 2px; border-radius: 12px; font-size: 11px; font-weight: 600; color: var(--text-3);
  }
  .bottom-nav a.active { color: var(--primary); }
  .bottom-nav a.send-btn svg.i { background: linear-gradient(135deg, var(--primary), #c053ff); color: #fff; border-radius: 12px; padding: 7px; width: 38px; height: 38px; margin-top: -14px; box-shadow: 0 8px 20px rgba(91, 76, 240, .4); }
  .list-item { padding: 12px 16px; }
  .card-head { padding: 14px 16px; }
  .card-pad { padding: 16px; }
  .hero { padding: 20px; }
  .toasts { left: 16px; right: 16px; bottom: calc(86px + env(safe-area-inset-bottom)); max-width: none; }
  .toast { max-width: none; }
  .hide-m { display: none !important; }
  .modal-wrap { align-items: flex-end; padding: 0; }
  .modal { border-radius: 22px 22px 0 0; max-height: 92vh; }
  .modal-foot .btn { flex: 1; }
}

@media (max-width: 480px) {
  .grid-4 { grid-template-columns: 1fr 1fr; gap: 10px; }
  .stat { padding: 14px; gap: 10px; flex-direction: column; align-items: flex-start; }
  .stat b { font-size: 21px; }
  .stat-icon { width: 38px; height: 38px; border-radius: 12px; }
  .toolbar .btn .lbl { display: none; }
  .toolbar .btn { padding: 0 12px; }
}

@media (min-width: 861px) { .show-m { display: none !important; } }
