:root {
  --bg: #f3f4f8;
  --surface: #ffffff;
  --ink: #1b1d29;
  --muted: #6b6f82;
  --line: #e4e6ee;
  --primary: #5b3df5;
  --primary-2: #7b61ff;
  --ok: #12a37a;
  --warn: #e39a12;
  --bad: #e5484d;
  --radius: 20px;
  --g-violet: linear-gradient(135deg, #5b3df5 0%, #8d6bff 100%);
  --g-teal: linear-gradient(135deg, #0f9d8a 0%, #36c6a8 100%);
  --g-amber: linear-gradient(135deg, #e37a12 0%, #f5b43c 100%);
  --g-rose: linear-gradient(135deg, #d6336c 0%, #f06595 100%);
  --g-ink: linear-gradient(135deg, #25283a 0%, #454a66 100%);
  --g-blue: linear-gradient(135deg, #1c6fe0 0%, #4ea3ff 100%);
}
* { box-sizing: border-box; }
html, body { margin: 0; }
body {
  font-family: 'IBM Plex Sans Arabic', system-ui, -apple-system, 'Segoe UI', Tahoma, sans-serif;
  background: var(--bg); color: var(--ink); font-size: 15px; line-height: 1.6;
}
.hidden { display: none !important; }
.muted { color: var(--muted); }
.error { color: var(--bad); min-height: 1.4em; margin: 8px 0 0; font-size: 14px; }
svg { width: 22px; height: 22px; fill: none; stroke: currentColor; stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round; }

/* login */
.login { min-height: 100vh; display: grid; place-items: center; padding: 16px; background: var(--g-violet); }
.login-card { background: var(--surface); padding: 32px; border-radius: 24px; width: 100%; max-width: 360px; text-align: center; box-shadow: 0 20px 60px rgba(30,10,120,.25); }
.login-card h1 { margin: 12px 0 4px; font-size: 22px; }
.login-card input { width: 100%; margin: 18px 0 12px; }
.login-card .btn { width: 100%; }
.brand-mark { width: 56px; height: 56px; border-radius: 16px; background: var(--g-violet); color: #fff; display: grid; place-items: center; margin: 0 auto; }
.brand-mark svg { width: 28px; height: 28px; }
.brand-mark.small { width: 40px; height: 40px; border-radius: 12px; margin: 0; }
.brand-mark.small svg { width: 20px; height: 20px; }

/* topbar */
.topbar { display: flex; align-items: center; gap: 16px; padding: 14px 24px; background: var(--surface); border-bottom: 1px solid var(--line); position: sticky; top: 0; z-index: 5; flex-wrap: wrap; }
.brand { display: flex; align-items: center; gap: 10px; }
.brand-title { font-weight: 700; }
.brand-sub { font-size: 12px; color: var(--muted); }
.tabs { display: flex; gap: 4px; background: var(--bg); padding: 4px; border-radius: 14px; margin-inline-start: auto; }
.tabs button { border: 0; background: transparent; font: inherit; padding: 7px 16px; border-radius: 10px; cursor: pointer; color: var(--muted); font-weight: 500; }
.tabs button.active { background: var(--surface); color: var(--ink); box-shadow: 0 1px 3px rgba(0,0,0,.08); }
.top-actions { display: flex; align-items: center; gap: 8px; }
.banner { background: #fff4db; color: #7a5200; text-align: center; padding: 8px 16px; font-size: 14px; }

main { padding: 24px; max-width: 1280px; margin: 0 auto; }
h2 { font-size: 20px; margin: 0; }
h3 { font-size: 16px; margin: 0 0 12px; }
.section-head { display: flex; align-items: center; justify-content: space-between; gap: 12px; margin: 28px 0 14px; flex-wrap: wrap; }
.section-head:first-child { margin-top: 0; }

/* KPI cards (Turki's style) */
.kpis { display: grid; grid-template-columns: repeat(auto-fit, minmax(230px, 1fr)); gap: 16px; }
.kpi { position: relative; border-radius: var(--radius); padding: 18px 20px 20px; color: #fff; overflow: hidden; min-height: 150px; box-shadow: 0 10px 30px rgba(20,20,60,.12); }
.kpi.violet { background: var(--g-violet); } .kpi.teal { background: var(--g-teal); } .kpi.amber { background: var(--g-amber); }
.kpi.rose { background: var(--g-rose); } .kpi.ink { background: var(--g-ink); } .kpi.blue { background: var(--g-blue); }
.kpi-head { display: flex; justify-content: space-between; align-items: flex-start; }
.kpi-title { font-weight: 600; font-size: 15px; opacity: .95; }
.kpi-info { width: 22px; height: 22px; border-radius: 50%; border: 1.5px solid rgba(255,255,255,.7); display: grid; place-items: center; font-size: 12px; font-weight: 700; cursor: help; flex: none; }
.kpi-value { font-size: 38px; font-weight: 700; line-height: 1.1; margin-top: 14px; }
.kpi-unit { font-size: 14px; font-weight: 500; opacity: .85; margin-inline-start: 6px; }
.kpi-badge { display: inline-block; margin-top: 12px; padding: 3px 10px; border-radius: 999px; font-size: 12px; font-weight: 600; background: rgba(255,255,255,.22); }
.kpi-icon { position: absolute; left: 16px; bottom: 14px; opacity: .35; }
.kpi-icon svg { width: 44px; height: 44px; stroke-width: 1.4; }

/* surfaces */
.card { background: var(--surface); border-radius: var(--radius); padding: 20px; border: 1px solid var(--line); }
.grid-2 { display: grid; grid-template-columns: 2fr 1fr; gap: 16px; }
@media (max-width: 900px) { .grid-2 { grid-template-columns: 1fr; } main { padding: 16px; } .topbar { padding: 12px 16px; } .tabs { order: 3; width: 100%; margin: 0; overflow-x: auto; } }

.btn { text-decoration: none; font: inherit; border: 1px solid var(--line); background: var(--surface); color: var(--ink); padding: 9px 16px; border-radius: 12px; cursor: pointer; font-weight: 600; display: inline-flex; align-items: center; gap: 6px; }
.btn.primary { background: var(--primary); border-color: var(--primary); color: #fff; }
.btn.primary:hover { background: #4c2fe6; }
.btn.danger { color: var(--bad); }
.btn.ghost { background: transparent; }
.btn.small { padding: 5px 12px; font-size: 13px; border-radius: 10px; }
.btn:disabled { opacity: .5; cursor: not-allowed; }
.btn svg { width: 18px; height: 18px; }
.row { display: flex; gap: 8px; flex-wrap: wrap; align-items: center; }

input, select, textarea { font: inherit; padding: 9px 12px; border: 1px solid var(--line); border-radius: 12px; background: #fff; color: var(--ink); width: 100%; }
input:focus, select:focus { outline: 2px solid rgba(91,61,245,.25); border-color: var(--primary); }
label.field { display: block; margin-bottom: 12px; }
label.field > span { display: block; font-size: 13px; color: var(--muted); margin-bottom: 4px; font-weight: 500; }
.fields-2 { display: grid; grid-template-columns: 1fr 1fr; gap: 0 12px; }
.check { display: flex; gap: 8px; align-items: flex-start; font-size: 14px; background: #f7f5ff; border: 1px solid #e5deff; padding: 10px 12px; border-radius: 12px; margin-bottom: 12px; }
.check input { width: auto; margin-top: 4px; }

/* tables */
.table-wrap { overflow-x: auto; }
table { width: 100%; border-collapse: collapse; font-size: 14px; }
th { text-align: right; color: var(--muted); font-weight: 500; font-size: 13px; padding: 10px 12px; border-bottom: 1px solid var(--line); white-space: nowrap; }
td { padding: 10px 12px; border-bottom: 1px solid var(--line); vertical-align: middle; }
tr:last-child td { border-bottom: 0; }
td.num, .ltr { direction: ltr; unicode-bidi: embed; text-align: right; font-variant-numeric: tabular-nums; }
.pill { display: inline-block; padding: 3px 10px; border-radius: 999px; font-size: 12px; font-weight: 600; background: var(--bg); color: var(--muted); white-space: nowrap; }
.pill.ok { background: #e3f7f0; color: var(--ok); } .pill.warn { background: #fff2dc; color: #b06f00; }
.pill.bad { background: #fde8e8; color: var(--bad); } .pill.info { background: #ece8ff; color: var(--primary); }

/* campaigns list */
.camp-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(300px, 1fr)); gap: 16px; }
.camp { cursor: pointer; transition: box-shadow .15s; }
.camp:hover { box-shadow: 0 8px 24px rgba(20,20,60,.08); }
.camp-top { display: flex; justify-content: space-between; align-items: flex-start; gap: 8px; }
.camp-name { font-weight: 700; font-size: 16px; }
.camp-meta { color: var(--muted); font-size: 13px; margin-top: 2px; }
.progress { height: 8px; background: var(--bg); border-radius: 999px; overflow: hidden; margin: 14px 0 8px; display: flex; }
.progress > span { height: 100%; }
.camp-stats { display: flex; gap: 14px; font-size: 13px; color: var(--muted); flex-wrap: wrap; }
.camp-stats b { color: var(--ink); }
.empty { text-align: center; padding: 40px 16px; color: var(--muted); }

.events { list-style: none; padding: 0; margin: 0; font-size: 14px; }
.events li { padding: 8px 0; border-bottom: 1px dashed var(--line); display: flex; gap: 8px; }
.events li:last-child { border-bottom: 0; }
.events time { color: var(--muted); font-size: 12px; white-space: nowrap; direction: ltr; }
.events .err { color: var(--bad); }

/* modal */
.modal { position: fixed; inset: 0; background: rgba(20,20,40,.45); display: grid; place-items: center; padding: 16px; z-index: 20; }
.modal-card { background: var(--surface); border-radius: 24px; padding: 24px; width: 100%; max-width: 560px; max-height: 90vh; overflow: auto; }
.modal-card h2 { margin-bottom: 16px; }
.modal-actions { display: flex; gap: 8px; justify-content: flex-start; margin-top: 8px; }
.seg { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; margin-bottom: 14px; }
.seg label { border: 1.5px solid var(--line); border-radius: 14px; padding: 12px; cursor: pointer; display: block; }
.seg label:has(input:checked) { border-color: var(--primary); background: #f7f5ff; }
.seg input { display: none; }
.seg b { display: block; }
.seg small { color: var(--muted); }

.toast { position: fixed; bottom: 20px; left: 50%; transform: translateX(-50%); background: var(--ink); color: #fff; padding: 10px 18px; border-radius: 12px; z-index: 30; font-size: 14px; max-width: 90vw; }
.toast.bad { background: var(--bad); }
.status-list { display: grid; gap: 10px; }
.status-item { display: flex; justify-content: space-between; align-items: center; padding: 10px 12px; background: var(--bg); border-radius: 12px; font-size: 14px; }
code { background: var(--bg); padding: 2px 6px; border-radius: 6px; font-size: 13px; direction: ltr; unicode-bidi: embed; }
audio { width: 100%; margin-top: 8px; }

/* agent script editor */
textarea.script { min-height: 380px; resize: vertical; line-height: 1.9; font-size: 15px; }
.chips { display: flex; flex-wrap: wrap; gap: 6px; margin: 4px 0 12px; }
.chip { border: 1px dashed #c9bfff; background: #f7f5ff; color: var(--primary); border-radius: 999px; padding: 4px 12px; font: inherit; font-size: 13px; cursor: pointer; }
.chip:hover { background: #ece8ff; }
.chip code { background: transparent; padding: 0; font-size: 12px; }
.preview { background: var(--bg); border-radius: 14px; padding: 14px 16px; white-space: pre-wrap; font-size: 14px; line-height: 1.9; max-height: 520px; overflow: auto; }
.preview mark { background: #ece8ff; color: var(--primary); border-radius: 6px; padding: 0 4px; font-weight: 600; }
.bubble { background: var(--g-violet); color: #fff; border-radius: 18px 18px 4px 18px; padding: 12px 16px; margin-bottom: 14px; font-weight: 500; }
.hint { font-size: 13px; color: var(--muted); }
.counter { font-size: 12px; color: var(--muted); text-align: left; margin-top: -6px; margin-bottom: 8px; }
.script-note { display: flex; justify-content: space-between; align-items: center; gap: 12px; flex-wrap: wrap; }
.modal-card.wide { max-width: 860px; }
.bubble mark { background: rgba(255,255,255,.25); color: #fff; border-radius: 6px; padding: 0 4px; }

/* call details */
.call-meta { display: flex; flex-wrap: wrap; gap: 10px; align-items: center; margin-bottom: 14px; }
.call-summary { background: var(--bg); border-radius: 12px; padding: 12px 14px; margin: 0 0 14px; line-height: 1.8; }
.chat { display: flex; flex-direction: column; gap: 10px; max-height: 420px; overflow-y: auto; padding: 4px 2px; }
.msg { display: flex; flex-direction: column; max-width: 82%; }
.msg.agent { align-self: flex-start; }
.msg.user { align-self: flex-end; }
.msg .who { font-size: 12px; color: var(--muted); margin-bottom: 3px; display: flex; gap: 8px; }
.msg.user .who { justify-content: flex-end; }
.msg .bubble { padding: 10px 14px; border-radius: 14px; line-height: 1.7; }
.msg.agent .bubble { background: #ece8ff; color: #2d2459; border-top-right-radius: 4px; }
.msg.user .bubble { background: #e3f7f0; color: #0f4a3a; border-top-left-radius: 4px; }
.msg .cut { font-size: 11px; color: var(--muted); }

.cost { white-space: nowrap; font-weight: 600; }
.cost small { font-weight: 400; }
.row-actions { display: flex; flex-direction: column; gap: 6px; align-items: stretch; }

.cost-grid { display: grid; grid-template-columns: repeat(3, 1fr); gap: 12px; }
.cost-part { background: var(--bg); border-radius: 12px; padding: 12px 14px; }
.cost-big { font-size: 22px; font-weight: 700; margin-top: 4px; }
.cost-big small { font-size: 12px; font-weight: 400; }
@media (max-width: 640px) { .cost-grid { grid-template-columns: 1fr; } }

/* ---------- mobile ---------- */
.tabs button { white-space: nowrap; }
@media (max-width: 720px) {
  main { padding: 12px; }
  .topbar { gap: 10px; }
  .tabs { display: flex; overflow-x: auto; scrollbar-width: none; -webkit-overflow-scrolling: touch; }
  .tabs::-webkit-scrollbar { display: none; }
  .tabs button { padding: 7px 12px; flex: 0 0 auto; }
  .section-head { margin: 18px 0 12px; }
  .section-head > .row { width: 100%; }
  .section-head > .row .btn { flex: 1 1 auto; justify-content: center; }
  .kpis { grid-template-columns: 1fr 1fr; gap: 10px; }
  .kpi { padding: 14px; min-height: 0; }
  .kpi-value { font-size: 28px; }
  .kpi-icon { display: none; }
  .card { padding: 14px; }
  .table-wrap { overflow: visible; }
  table.rtable, table.rtable tbody { display: block; width: 100%; }
  table.rtable thead { display: none; }
  table.rtable tr { display: block; border: 1px solid var(--line); border-radius: 16px; padding: 6px 14px; margin-bottom: 12px; background: var(--surface); }
  table.rtable td { display: flex; justify-content: space-between; align-items: center; gap: 12px; padding: 8px 0; border-bottom: 1px dashed var(--line); max-width: none !important; text-align: left; }
  table.rtable td:last-child { border-bottom: 0; }
  table.rtable td::before { content: attr(data-label); color: var(--muted); font-size: 13px; font-weight: 500; flex: 0 0 auto; text-align: right; }
  table.rtable td.num { direction: rtl; }
  table.rtable td.num > * , table.rtable td.num { unicode-bidi: plaintext; }
  table.rtable td[data-label="ملاحظة"] { display: block; text-align: right; line-height: 1.7; }
  table.rtable td[data-label="ملاحظة"]::before { display: block; margin-bottom: 4px; }
  table.rtable td[data-label="ملاحظة"]:empty { display: none; }
  table.rtable td.cell-actions::before { content: none; }
  table.rtable td.cell-actions .row-actions { flex-direction: row; width: 100%; }
  table.rtable td.cell-actions .row-actions .btn, table.rtable td.cell-actions > .btn { flex: 1; justify-content: center; }
  table.rtable td.cell-actions:not(:has(*)) { display: none; }
  .modal { padding: 0; place-items: end stretch; }
  .modal-card, .modal-card.wide { max-width: none; border-radius: 22px 22px 0 0; max-height: 92vh; padding: 18px; }
  .msg { max-width: 92%; }
  .cost-grid { grid-template-columns: repeat(3, 1fr); gap: 8px; }
  .cost-part { padding: 10px; }
  .cost-part .muted { font-size: 12px; }
  .cost-big { font-size: 16px; }
  table.rtable td[data-label="ElevenLabs"], table.rtable td[data-label="Twilio"] { border-bottom-style: dotted; font-size: 13px; }
  table.rtable td[data-label="ElevenLabs"]::before { content: "ElevenLabs (الوكيل)"; }
  table.rtable td[data-label="Twilio"]::before { content: "Twilio (الخط)"; }
  table.rtable td[data-label="المجموع"]::before { content: "مجموع التكلفة"; font-weight: 700; color: var(--ink); }
  .events li { flex-wrap: wrap; }
}
@media (max-width: 380px) { .kpis { grid-template-columns: 1fr; } }
