:root {
  --green: #8CC43F; --green-d: #3F9A1B; --green-t: #2F6B12; --green-bg: #EEF6E3; --green-bg2: #F4F9EC;
  --ink: #0F1410; --text: #18201A; --text2: #3D473E; --muted: #5B665C; --faint: #8A948B;
  --ground: #F4F6F1; --card: #FFFFFF; --line: #E3E8DE; --line2: #EDF0E9; --field: #D5DCCF;
  --blue: #1D4ED8; --blue-bg: #F5F8FF; --amber: #9A4A00; --amber-bg: #FFF4E5; --red: #B42318; --red-bg: #FEECEB;
  --r: 10px; --r2: 14px;
  --shadow: 0 12px 32px rgba(0,0,0,.14);
}
* { box-sizing: border-box; }
html, body { margin: 0; background: var(--ground); color: var(--text); }
body, button, input, select, textarea { font-family: 'IBM Plex Sans Arabic', system-ui, sans-serif; }
a { color: var(--green-t); }
code, .mono { font-family: ui-monospace, Menlo, Consolas, monospace; direction: ltr; unicode-bidi: isolate; }
.ltr { direction: ltr; unicode-bidi: isolate; }
.sr { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); }

/* layout */
.app { display: flex; min-height: 100vh; }
.side { width: var(--side-w, 256px); flex: 0 0 var(--side-w, 256px); scrollbar-width: thin; scrollbar-color: #D5DBD0 transparent; background: var(--card); border-left: 1px solid var(--line); padding: 16px 12px; display: flex; flex-direction: column; gap: 12px; position: sticky; top: 0; height: 100vh; overflow-y: auto; overflow-x: hidden; }
.brand { display: flex; align-items: center; gap: 10px; padding: 0 4px; }
.brand img { width: 84px; height: 44px; object-fit: contain; }
.brand b { font-size: 18px; display: block; direction: ltr; text-align: right; }
.brand small { font-size: 12px; color: var(--muted); }
.nav { display: flex; flex-direction: column; gap: 2px; }
.nav a, .nav button.item { display: flex; align-items: center; gap: 11px; padding: 8px 11px; border-radius: var(--r); color: #2B342C; text-decoration: none; font-size: 14px; font-weight: 500; border: 0; background: transparent; cursor: pointer; text-align: right; width: 100%; }
.nav a:hover { background: var(--ground); }
.nav a.on { background: var(--ink); color: #fff; font-weight: 600; }
.nav a.on svg { stroke: var(--green); }
.nav .head { font-size: 11px; font-weight: 700; color: var(--faint); padding: 10px 11px 4px; border-top: 1px solid var(--line2); margin-top: 6px; }
.nav .soon { margin-inline-start: auto; font-size: 10px; font-weight: 700; color: var(--faint); background: var(--ground); border-radius: 999px; padding: 1px 7px; }
.nav a.on .soon { background: #2B342C; color: #B9C4B1; }
.main { flex: 1; min-width: 0; padding: 26px 30px 48px; display: flex; flex-direction: column; gap: 18px; }
.page-head { display: flex; flex-wrap: wrap; justify-content: space-between; align-items: flex-start; gap: 14px; }
.page-head h1 { margin: 0; font-size: 26px; }
.page-head p { margin: 4px 0 0; color: var(--muted); font-size: 14px; }
.kicker { font-size: 12px; font-weight: 700; color: var(--muted); }
/* الفاصل القابل للسحب */
.resizer { flex: 0 0 8px; margin-inline: -4px; position: sticky; top: 0; height: 100vh; z-index: 5; cursor: col-resize; touch-action: none; outline: none; }
.resizer::after { content: ''; position: absolute; inset: 0 3px; border-radius: 2px; background: transparent; transition: background .15s; }
.resizer:hover::after, .resizer:focus-visible::after, .resizer.on::after { background: var(--green); }
body.resizing { cursor: col-resize; user-select: none; }
@media (max-width: 860px) { .resizer { display: none; } .app { flex-direction: column; } .side { width: auto; flex: none; height: auto; position: static; } .main { padding: 18px 16px 40px; } }

/* cards & grids */
.card { background: var(--card); border: 1px solid var(--line); border-radius: var(--r2); }
.pad { padding: 16px 18px; }
.grid { display: grid; gap: 14px; grid-template-columns: repeat(auto-fill, minmax(320px, 1fr)); }
.stats { display: grid; gap: 10px; grid-template-columns: repeat(auto-fit, minmax(160px, 1fr)); }
.stat { background: var(--card); border: 1px solid var(--line); border-radius: 12px; padding: 12px 14px; }
.stat span { font-size: 12px; color: var(--muted); display: block; }
.stat b { font-size: 22px; }
.row { display: flex; align-items: center; gap: 8px; }
.wrap { flex-wrap: wrap; }
.col { display: flex; flex-direction: column; gap: 8px; }
.grow { flex: 1; min-width: 0; }
.muted { color: var(--muted); }
.small { font-size: 12px; }
.sep { height: 1px; background: var(--line2); margin: 4px 0; }

/* buttons & fields */
.btn { font: inherit; font-size: 14px; font-weight: 600; padding: 8px 14px; border-radius: var(--r); border: 1px solid var(--field); background: #fff; color: var(--text); cursor: pointer; display: inline-flex; align-items: center; gap: 6px; text-decoration: none; }
.btn:hover { background: var(--ground); }
.btn.primary { background: var(--ink); color: #fff; border-color: var(--ink); }
.btn.primary:hover { background: #26302A; }
.btn.green { background: var(--green-d); color: #fff; border-color: var(--green-d); }
.btn.danger { color: var(--red); border-color: #F3C5C1; }
.btn.ghost { border-color: transparent; background: transparent; }
.btn.sm { font-size: 12px; padding: 5px 10px; border-radius: 8px; }
.btn:disabled { opacity: .5; cursor: not-allowed; }
.field { display: flex; flex-direction: column; gap: 5px; }
.field label, .field > span { font-size: 13px; font-weight: 600; }
.field .hint { font-size: 12px; color: var(--muted); font-weight: 400; }
.input, select.input, textarea.input { font: inherit; font-size: 14px; padding: 9px 11px; border: 1px solid var(--field); border-radius: var(--r); background: #fff; color: var(--text); width: 100%; }
.input:focus { outline: 2px solid var(--green); outline-offset: 0; border-color: var(--green-d); }
.check { display: flex; align-items: flex-start; gap: 8px; font-size: 13px; cursor: pointer; }
.check input { width: 17px; height: 17px; accent-color: var(--green-d); margin-top: 2px; }
.form-grid { display: grid; gap: 12px; grid-template-columns: repeat(auto-fit, minmax(200px, 1fr)); }

/* chips */
.chip { display: inline-flex; align-items: center; gap: 5px; font-size: 12px; font-weight: 600; padding: 2px 9px; border-radius: 999px; background: var(--ground); color: var(--text2); white-space: nowrap; }
.chip.green { background: var(--green-bg); color: var(--green-t); }
.chip.amber { background: var(--amber-bg); color: var(--amber); }
.chip.red { background: var(--red-bg); color: var(--red); }
.chip.blue { background: var(--blue-bg); color: var(--blue); }
.chip.dark { background: var(--ink); color: var(--green); }
.pick { font: inherit; font-size: 12px; padding: 5px 11px; border-radius: 999px; border: 1.5px solid var(--line); background: #fff; cursor: pointer; display: inline-flex; align-items: center; gap: 6px; color: var(--text); }
.pick.on { border-color: var(--green-d); background: var(--green-bg2); color: var(--green-t); font-weight: 600; }
.badge { width: 28px; height: 28px; border-radius: 8px; display: inline-flex; align-items: center; justify-content: center; font-size: 10px; font-weight: 700; flex: 0 0 auto; }
.badge.lg { width: 44px; height: 44px; border-radius: 12px; font-size: 13px; }
.badge.sm { width: 20px; height: 20px; border-radius: 6px; font-size: 8px; }
.avatar { width: 34px; height: 34px; border-radius: 50%; background: var(--green-bg); color: var(--green-t); display: inline-flex; align-items: center; justify-content: center; font-weight: 700; font-size: 13px; flex: 0 0 auto; }

/* tables */
.table-wrap { overflow-x: auto; }
table.t { width: 100%; border-collapse: collapse; font-size: 14px; }
table.t th { text-align: right; font-size: 13px; font-weight: 500; color: var(--muted); background: #FAFBF8; padding: 11px 14px; white-space: nowrap; }
table.t td { padding: 11px 14px; border-top: 1px solid var(--line2); vertical-align: middle; }

/* tabs */
.tabs { display: flex; gap: 4px; background: #E9EDE5; border-radius: 10px; padding: 3px; width: fit-content; flex-wrap: wrap; }
.tabs button { font: inherit; font-size: 13px; border: 0; border-radius: 8px; padding: 6px 14px; background: transparent; cursor: pointer; color: var(--text2); }
.tabs button.on { background: #fff; font-weight: 700; color: var(--text); box-shadow: 0 1px 3px rgba(0,0,0,.08); }

/* modal & toast */
.overlay { position: fixed; inset: 0; background: rgba(15,20,16,.42); display: flex; align-items: flex-start; justify-content: center; padding: 48px 16px; z-index: 50; overflow-y: auto; }
.modal { width: 560px; max-width: 100%; background: #fff; border-radius: 16px; box-shadow: 0 24px 70px rgba(0,0,0,.3); display: flex; flex-direction: column; }
.modal.wide { width: 860px; }
.modal-head { display: flex; justify-content: space-between; align-items: flex-start; gap: 10px; padding: 18px 20px 10px; }
.modal-head h2 { margin: 0; font-size: 19px; }
.modal-body { padding: 6px 20px 16px; display: flex; flex-direction: column; gap: 14px; }
.modal-foot { display: flex; gap: 8px; justify-content: flex-start; padding: 14px 20px; border-top: 1px solid var(--line2); background: #FAFBF8; border-radius: 0 0 16px 16px; }
.x { width: 32px; height: 32px; border-radius: 8px; border: 0; background: var(--ground); cursor: pointer; font-size: 16px; }
.toasts { position: fixed; bottom: 18px; left: 18px; display: flex; flex-direction: column; gap: 8px; z-index: 60; }
.toast { background: var(--ink); color: #fff; font-size: 13px; border-radius: 10px; padding: 10px 14px; box-shadow: var(--shadow); max-width: 420px; }
.toast.err { background: var(--red); }
.note { font-size: 13px; border-radius: 10px; padding: 10px 12px; line-height: 1.7; }
.note.blue { background: var(--blue-bg); color: var(--blue); }
.note.green { background: var(--green-bg2); color: var(--green-t); }
.note.amber { background: #FFF8EC; color: #6B3A00; }
.note.red { background: var(--red-bg); color: var(--red); }
.secret { font-family: ui-monospace, Menlo, monospace; direction: ltr; text-align: left; background: var(--ink); color: #CFE8B0; padding: 10px 12px; border-radius: 10px; word-break: break-all; font-size: 13px; }

/* agent switcher */
.sw { position: relative; }
.sw-btn { font: inherit; display: flex; align-items: center; gap: 8px; width: 100%; text-align: right; border: 1px solid var(--line); border-radius: 10px; padding: 7px 10px; background: #FAFBF8; cursor: pointer; color: var(--text); }
.sw-pop { position: fixed; z-index: 60; background: #fff; border: 1px solid var(--line); border-radius: 14px; box-shadow: 0 18px 48px rgba(0,0,0,.18); display: flex; flex-direction: column; overflow: hidden; }
.sw-list { display: flex; flex-direction: column; gap: 4px; padding: 8px 12px; max-height: 300px; overflow-y: auto; min-height: 0; flex: 1 1 auto; }
.sw-item > .grow, .sw-btn > .grow { min-width: 0; }
.sw-item { font: inherit; display: flex; align-items: center; gap: 10px; text-align: right; padding: 8px 10px; border-radius: 10px; border: 1.5px solid transparent; background: #fff; cursor: pointer; color: var(--text); }
.sw-item.on { border-color: var(--green); background: var(--green-bg2); }

/* permissions matrix */
.matrix { width: 100%; border-collapse: collapse; font-size: 13px; }
.matrix th { padding: 8px 6px; background: #FAFBF8; font-weight: 600; color: var(--text2); text-align: center; }
.matrix th:first-child, .matrix td:first-child { text-align: right; padding-right: 12px; }
.matrix td { padding: 5px 6px; text-align: center; border-top: 1px solid #F1F3EE; }
.matrix .grp td { font-size: 11px; font-weight: 700; color: var(--faint); background: #fff; text-align: right; padding-top: 10px; }
.box { width: 22px; height: 22px; border-radius: 6px; border: 1.5px solid #B9C4B1; background: #fff; color: #fff; font-size: 13px; font-weight: 700; display: inline-flex; align-items: center; justify-content: center; cursor: pointer; padding: 0; }
.box.on { border-color: var(--green-d); background: var(--green-d); }
.box:disabled { cursor: not-allowed; opacity: .7; }

/* login */
.auth { min-height: 100vh; display: flex; align-items: center; justify-content: center; padding: 24px; }
.auth .card { width: 400px; max-width: 100%; padding: 28px; display: flex; flex-direction: column; gap: 16px; }
.auth .logo { display: flex; align-items: center; gap: 12px; justify-content: center; }
.auth .logo img { width: 110px; height: 56px; object-fit: contain; }
.empty { text-align: center; padding: 36px 16px; color: var(--muted); display: flex; flex-direction: column; align-items: center; gap: 8px; }
.empty b { color: var(--text); font-size: 16px; }
.loading { padding: 40px; text-align: center; color: var(--muted); }

/* ===== v0.2 additions ===== */
.pick:has(> b) { flex-direction: column; align-items: flex-start; gap: 2px; border-radius: 12px; padding: 10px 12px; text-align: right; font-size: 13px; }
.pick:disabled { cursor: default; opacity: .75; }
.tabs { margin-bottom: 16px; }
.box:disabled { cursor: default; }
select.input { appearance: auto; }
details > summary { list-style: revert; }

/* ===== v0.3: القنوات والمحادثات ===== */
.ellipsis { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.avatar { overflow: visible; }
.avatar img { width: 100%; height: 100%; object-fit: cover; border-radius: inherit; }
.chip.sm { font-size: 10px; padding: 1px 7px; }
.input.sm { padding: 6px 8px; font-size: 12.5px; }
.input.warn { border-color: #E8A33D; background: #FFF8EC; }
tr.clickable { cursor: pointer; }
tr.clickable:hover td { background: #FAFBF8; }
.ch-badge { display: inline-flex; align-items: center; gap: 6px; font-size: 12.5px; }
.ch-dot { display: inline-grid; place-items: center; min-width: 22px; height: 18px; padding: 0 4px; border-radius: 6px; font-size: 9px; font-weight: 800; letter-spacing: .2px; direction: ltr; }
.kv { display: grid; grid-template-columns: auto 1fr; gap: 8px 14px; margin: 0; font-size: 13px; }
.kv dt { color: var(--muted); }
.kv dd { margin: 0; min-width: 0; overflow-wrap: anywhere; }

.main.flush { padding: 0; gap: 0; }
.cv { display: grid; grid-template-columns: 340px minmax(0, 1fr) 290px; height: 100vh; min-height: 0; }
.cv-list { display: flex; flex-direction: column; min-height: 0; border-left: 1px solid var(--line); background: var(--card); }
.cv-list-head { padding: 16px 14px 10px; display: flex; flex-direction: column; gap: 10px; border-bottom: 1px solid var(--line2); }
.cv-tabs { display: flex; gap: 4px; flex-wrap: wrap; }
.cv-tabs button { font: inherit; font-size: 12.5px; border: 0; background: transparent; padding: 5px 9px; border-radius: 8px; cursor: pointer; color: var(--text2); white-space: nowrap; display: inline-flex; align-items: center; gap: 5px; }
.cv-tabs button.on { background: var(--ink); color: #fff; font-weight: 600; }
.cv-count { font-size: 10.5px; background: rgba(0,0,0,.07); border-radius: 999px; padding: 0 6px; }
.cv-tabs button.on .cv-count { background: rgba(255,255,255,.18); }
.cv-items { overflow-y: auto; flex: 1; min-height: 0; display: flex; flex-direction: column; }
.cv-item { display: flex; gap: 10px; padding: 11px 14px; text-decoration: none; color: inherit; border-bottom: 1px solid var(--line2); }
.cv-item:hover { background: #FAFBF8; }
.cv-item.on { background: var(--green-bg2); box-shadow: inset -3px 0 0 var(--green); }
.cv-kind { position: absolute; bottom: -4px; left: -6px; transform: scale(.85); }
.cv-unread { background: var(--green); color: var(--ink); font-size: 10.5px; font-weight: 800; border-radius: 999px; min-width: 18px; height: 18px; display: inline-grid; place-items: center; padding: 0 5px; flex: none; }
.cv-thread { display: flex; flex-direction: column; min-height: 0; min-width: 0; background: #F4F6F1; }
.cv-empty { justify-content: center; }
.cv-head { display: flex; align-items: center; gap: 10px; padding: 12px 16px; background: var(--card); border-bottom: 1px solid var(--line); }
.cv-back { display: none; }
.cv-msgs { flex: 1; min-height: 0; overflow-y: auto; padding: 16px 22px; display: flex; flex-direction: column; gap: 6px; }
.cv-reply { display: flex; align-items: center; gap: 10px; padding: 10px 16px; background: var(--card); border-top: 1px solid var(--line); }
.cv-side { border-right: 1px solid var(--line); background: var(--card); padding: 18px 16px; overflow-y: auto; display: flex; flex-direction: column; gap: 18px; min-height: 0; }
.cv-mini { display: flex; align-items: center; gap: 8px; padding: 7px 9px; border: 1px solid var(--line2); border-radius: 9px; text-decoration: none; color: inherit; min-width: 0; }
.cv-mini:hover { background: #FAFBF8; }
.cv-mini.on { border-color: var(--green); background: var(--green-bg2); }

.msg { max-width: min(72%, 560px); padding: 7px 11px 5px; border-radius: 14px; background: #fff; border: 1px solid var(--line2); align-self: flex-start; display: flex; flex-direction: column; gap: 4px; box-shadow: 0 1px 1px rgba(0,0,0,.03); }
.msg.in { border-top-right-radius: 4px; }
.msg.out { align-self: flex-end; background: #E4F2D2; border-color: #D3E8B8; border-top-left-radius: 4px; }
.msg.note { align-self: flex-end; background: #FFF6DB; border-color: #F2E2A9; }
.msg-who { font-size: 11px; font-weight: 700; color: var(--green-t); }
.msg.note .msg-who { color: #8A6A00; }
.msg-text { white-space: pre-wrap; overflow-wrap: anywhere; font-size: 14px; line-height: 1.6; }
.msg-time { font-size: 10.5px; color: var(--faint); align-self: flex-start; }
.msg-img { max-width: 260px; max-height: 260px; border-radius: 10px; display: block; }
.msg-audio { width: 260px; max-width: 100%; height: 38px; }
.msg-file { font-size: 13px; }
.msg-sys { align-self: center; font-size: 11.5px; color: var(--muted); background: rgba(0,0,0,.04); padding: 3px 10px; border-radius: 999px; }
.msg-day { align-self: center; margin: 8px 0 4px; }
.msg-day span { font-size: 11.5px; font-weight: 600; color: var(--text2); background: #fff; border: 1px solid var(--line2); padding: 3px 12px; border-radius: 999px; }

@media (max-width: 1250px) { .cv { grid-template-columns: 320px minmax(0, 1fr); } .cv-side { display: none; } }
@media (max-width: 860px) {
  .cv { grid-template-columns: 1fr; height: auto; min-height: 70vh; }
  .cv.has-sel .cv-list { display: none; }
  .cv:not(.has-sel) .cv-thread { display: none; }
  .cv-thread { height: 80vh; }
  .cv-back { display: inline-flex; }
  .msg { max-width: 88%; }
}
