:root {
  --bg: #0d1117;
  --bg2: #161b22;
  --bg3: #1c2330;
  --border: #2a3242;
  --text: #e6edf3;
  --muted: #8b98a9;
  --accent: #4f8cff;
  --green: #3fb950;
  --amber: #d29922;
  --red: #f85149;
  --blue: #58a6ff;
  --orange: #db6d28;
  --purple: #bc8cff;
  --radius: 12px;
  --radius-sm: 8px;
  --shadow: 0 1px 3px rgba(0, 0, 0, .25);
}

* { box-sizing: border-box; margin: 0; padding: 0; }

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

body {
  background: radial-gradient(1200px 500px at 80% -10%, #17213a 0%, var(--bg) 50%);
  color: var(--text);
  font: 14px/1.5 -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  min-height: 100vh;
  -webkit-font-smoothing: antialiased;
  padding-bottom: env(safe-area-inset-bottom);
}

::selection { background: rgba(79, 140, 255, .35); }

.btn, .tab, .chip-btn, .contact, .key, .round { touch-action: manipulation; -webkit-tap-highlight-color: transparent; }
.btn:focus-visible, .tab:focus-visible, input:focus-visible, select:focus-visible, textarea:focus-visible {
  outline: 2px solid var(--accent); outline-offset: 1px;
}

.topbar {
  display: flex; justify-content: space-between; align-items: center;
  padding: 12px 20px; background: rgba(22, 27, 34, .92); backdrop-filter: blur(8px);
  border-bottom: 1px solid var(--border);
  position: sticky; top: 0; z-index: 10;
}
.brand { display: flex; align-items: center; gap: 10px; min-width: 0; }
.logo { font-size: 22px; color: var(--accent); }
.brand h1 { font-size: 17px; letter-spacing: .2px; }
.subtitle { color: var(--muted); font-size: 12px; }
.topbar-right { display: flex; align-items: center; gap: 10px; flex-shrink: 0; }
.conn { font-size: 12px; }
.conn.ok { color: var(--green); }
.conn.err { color: var(--red); }
.mode-badge {
  font-size: 11px; text-transform: uppercase; letter-spacing: .5px;
  background: var(--bg3); border: 1px solid var(--border); border-radius: 20px; padding: 3px 10px; color: var(--muted);
}
.mode-badge.twilio { color: var(--red); border-color: var(--red); }
.mode-badge.signalwire { color: var(--purple); border-color: var(--purple); }
.mode-badge.amazonconnect { color: var(--orange); border-color: var(--orange); }

.tabs {
  display: flex; gap: 2px; padding: 8px 12px 0; background: rgba(22, 27, 34, .92);
  border-bottom: 1px solid var(--border); position: sticky; top: 63px; z-index: 9;
  overflow-x: auto; scrollbar-width: none; -ms-overflow-style: none;
}
.tabs::-webkit-scrollbar { display: none; }
.tab {
  flex-shrink: 0; background: none; border: 1px solid transparent; color: var(--muted);
  padding: 9px 14px; border-radius: 10px 10px 0 0; cursor: pointer; font-size: 13px;
  transition: color .15s, background .15s;
}
.tab:hover { color: var(--text); }
.tab.active { color: var(--text); background: var(--bg); border-color: var(--border); border-bottom-color: var(--bg); font-weight: 600; }

main { padding: 20px; max-width: 1200px; margin: 0 auto; }
.hidden { display: none !important; }

.stat-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(150px, 1fr)); gap: 12px; margin-bottom: 18px; }
.stat { background: var(--bg2); border: 1px solid var(--border); border-radius: var(--radius); padding: 14px 16px; box-shadow: var(--shadow); }
.stat-label { color: var(--muted); font-size: 12px; display: block; }
.stat-value { font-size: 26px; font-weight: 700; letter-spacing: .3px; }

.card { background: var(--bg2); border: 1px solid var(--border); border-radius: var(--radius); padding: 16px; margin-bottom: 16px; box-shadow: var(--shadow); }
.card-head { display: flex; justify-content: space-between; align-items: center; gap: 8px; margin-bottom: 12px; }
.card-head h2 { font-size: 15px; }

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

.btn {
  background: var(--bg3); color: var(--text); border: 1px solid var(--border);
  padding: 7px 14px; border-radius: var(--radius-sm); cursor: pointer; font-size: 13px;
  transition: border-color .15s, background .15s, filter .15s;
}
.btn:hover { border-color: var(--accent); }
.btn.primary { background: var(--accent); border-color: var(--accent); color: #fff; }
.btn.primary:hover { filter: brightness(1.12); }
.btn.danger { background: transparent; border-color: var(--red); color: var(--red); }
.btn.ghost { background: transparent; }
.btn.small { padding: 3px 9px; font-size: 12px; }
.btn:disabled { opacity: .5; cursor: not-allowed; }

.table { width: 100%; border-collapse: collapse; font-size: 13px; }
.table th {
  text-align: left; color: var(--muted); font-weight: 600; font-size: 11px;
  text-transform: uppercase; letter-spacing: .4px; padding: 8px 10px; border-bottom: 1px solid var(--border);
  white-space: nowrap;
}
.table td { padding: 8px 10px; border-bottom: 1px solid var(--border); }
.table tr:last-child td { border-bottom: none; }
.table tr:hover td { background: rgba(79, 140, 255, .05); }

.badge {
  display: inline-block; padding: 2px 8px; border-radius: 20px; font-size: 11px; font-weight: 600;
}
.badge.gray { background: rgba(139,152,169,.15); color: var(--muted); }
.badge.blue { background: rgba(88,166,255,.15); color: var(--blue); }
.badge.green { background: rgba(63,185,80,.15); color: var(--green); }
.badge.amber { background: rgba(210,153,34,.15); color: var(--amber); }
.badge.red { background: rgba(248,81,73,.15); color: var(--red); }
.badge.orange { background: rgba(219,109,40,.15); color: var(--orange); }
.badge.purple { background: rgba(188,140,255,.15); color: var(--purple); }

.chip { padding: 2px 9px; border-radius: 20px; font-size: 12px; font-weight: 700; }
.chip.blue { background: rgba(88,166,255,.15); color: var(--blue); }
.chip.green { background: rgba(63,185,80,.15); color: var(--green); }
.chip.amber { background: rgba(210,153,34,.15); color: var(--amber); }

.muted { color: var(--muted); }
.small { font-size: 12px; }
.error { color: var(--red); }
code { background: var(--bg3); padding: 1px 5px; border-radius: 4px; font-size: 12px; }

.campaign-card { background: var(--bg2); border: 1px solid var(--border); border-radius: var(--radius); padding: 16px; margin-bottom: 12px; box-shadow: var(--shadow); }
.campaign-head { display: flex; justify-content: space-between; align-items: center; gap: 10px; flex-wrap: wrap; }
.campaign-title { font-size: 15px; font-weight: 600; }
.campaign-meta { color: var(--muted); font-size: 12px; margin-top: 4px; }
.campaign-actions { display: flex; gap: 6px; flex-wrap: wrap; }
.progress { margin-top: 10px; height: 6px; background: var(--bg3); border-radius: 4px; overflow: hidden; }
.progress > div { height: 100%; background: linear-gradient(90deg, var(--accent), var(--blue)); transition: width .4s; }

.agent-strip { display: flex; gap: 10px; flex-wrap: wrap; }
.agent-card {
  background: var(--bg3); border: 1px solid var(--border); border-radius: var(--radius);
  padding: 10px 14px; min-width: 170px;
}
.agent-card.busy { border-color: var(--orange); }
.agent-card.off { opacity: .5; }
.agent-name { font-weight: 600; }
.agent-dot { display: inline-block; width: 8px; height: 8px; border-radius: 50%; margin-right: 6px; }
.agent-dot.idle { background: var(--green); }
.agent-dot.busy { background: var(--orange); animation: pulse 1.2s infinite; }
@keyframes pulse { 50% { opacity: .35; } }

.queue-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(280px, 1fr)); gap: 14px; }
.queue-list { max-height: 420px; overflow-y: auto; }
.queue-item {
  display: flex; justify-content: space-between; align-items: center;
  padding: 8px 10px; border-bottom: 1px solid var(--border); font-size: 13px;
}
.queue-item:last-child { border-bottom: none; }
.q-phone { font-weight: 600; }
.q-meta { color: var(--muted); font-size: 12px; }
.q-attempt { color: var(--muted); font-size: 11px; }

.settings-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(320px, 1fr)); gap: 14px; }

.form { display: flex; flex-direction: column; gap: 12px; }
.form.row { flex-direction: row; align-items: flex-end; flex-wrap: wrap; }
.form label { display: flex; flex-direction: column; gap: 4px; font-size: 13px; color: var(--muted); flex: 1; min-width: 180px; }
.form label.flex-row { flex-direction: row; align-items: center; gap: 8px; }
.form input, .form select, .form textarea {
  background: var(--bg); color: var(--text); border: 1px solid var(--border);
  border-radius: var(--radius-sm); padding: 9px 11px; font-size: 13px; width: 100%;
  transition: border-color .15s;
}
.form input:focus, .form select:focus, .form textarea:focus { outline: none; border-color: var(--accent); }
.check-row { display: flex; gap: 16px; }
.check-row label { flex-direction: row; align-items: center; gap: 6px; color: var(--text); min-width: 0; }
.check-row input { width: auto; }
hr { border: none; border-top: 1px solid var(--border); margin: 4px 0; }
h3 { font-size: 13px; color: var(--muted); text-transform: uppercase; letter-spacing: .5px; }

.modal {
  position: fixed; inset: 0; background: rgba(0,0,0,.6); display: flex;
  align-items: center; justify-content: center; z-index: 50; padding: 20px;
}
.modal-box {
  background: var(--bg2); border: 1px solid var(--border); border-radius: 12px;
  padding: 20px; width: 100%; max-width: 560px; max-height: 90vh; overflow-y: auto;
}
.modal-box .form { margin-top: 8px; }

.script-box {
  margin-top: 8px; padding: 12px 14px; background: var(--bg3);
  border: 1px solid var(--border); border-left: 3px solid var(--accent);
  border-radius: 8px; font-size: 14px; line-height: 1.6; white-space: pre-wrap;
}
.live-call { min-height: 40px; }
.agent-chips { display: flex; gap: 8px; flex-wrap: wrap; margin: 12px 0 4px; }
.chip-btn {
  background: var(--bg3); border: 1px solid var(--border); color: var(--muted);
  padding: 5px 13px; border-radius: 20px; font-size: 12px; cursor: pointer;
}
.chip-btn:hover { border-color: var(--accent); color: var(--text); }
.chip-btn.active { background: var(--accent); border-color: var(--accent); color: #fff; }
.tx-card { background: var(--bg3); border: 1px solid var(--border); border-radius: 10px; padding: 12px 14px; margin-bottom: 10px; }
.tx-head { display: flex; justify-content: space-between; align-items: center; gap: 10px; flex-wrap: wrap; }
.tx-actions { display: flex; gap: 6px; }
.tx-meta { margin-top: 4px; }
.tx-preview { margin-top: 8px; color: var(--muted); font-size: 13px; line-height: 1.5; }
.import-result { margin-top: 12px; padding: 10px; border-radius: 8px; background: var(--bg3); font-size: 13px; }
.import-result table { width: 100%; border-collapse: collapse; margin-top: 8px; font-size: 12px; }
.import-result td, .import-result th { padding: 4px 8px; border-bottom: 1px solid var(--border); text-align: left; }

audio { height: 30px; max-width: 180px; }

@media (max-width: 720px) {
  .tabs { top: 60px; }
  .stat-grid { grid-template-columns: repeat(2, 1fr); }
  main { padding: 14px; }
  .topbar { padding: 10px 14px; }
  .brand h1 { font-size: 16px; }
  .stat-value { font-size: 22px; }

  /* Tables become horizontally scrollable cards; hide lower-priority columns */
  .table { display: block; overflow-x: auto; white-space: nowrap; -webkit-overflow-scrolling: touch; }
  .table th:nth-child(n+7), .table td:nth-child(n+7) { display: none; }

  /* Bigger touch targets */
  .btn { min-height: 40px; }
  .btn.small { min-height: 32px; }
  .tab { padding: 10px 12px; }

  /* 16px inputs prevent iOS auto-zoom when focusing */
  .form input, .form select, .form textarea { font-size: 16px; }

  /* Modals become bottom sheets */
  .modal { padding: 10px; align-items: flex-end; }
  .modal-box { max-width: 100%; max-height: 92vh; border-radius: 18px 18px 0 0; }

  .settings-grid { grid-template-columns: 1fr; }
  .queue-grid { grid-template-columns: 1fr; }
  .stat-grid { gap: 10px; }
}

@media (max-width: 520px) {
  .subtitle { display: none; }
  .conn { display: none; }
  .stat-grid { grid-template-columns: repeat(2, 1fr); }
  .form.row > * { min-width: 100%; }
  audio { max-width: 140px; }
}
