/* هوية أنتيكا: رملي #CAB095 على كريمي، بحبر داكن دافئ */
:root {
  --sand: #cab095;
  --bg: #f4ede2;
  --panel: #fffdf9;
  --panel-2: #f8f1e6;
  --ink: #1a1714;
  --ink-2: #7a6c5c;
  --line: #e6dac8;
  --brand: #8a6a45;
  --brand-ink: #6b5134;
  --on-brand: #fffdf9;
  --out: #ece0cd;
  --note: #f6ecd2;
  --note-line: #d9c48d;
  --danger: #a33b2a;
  --radius: 12px;
  --shadow: 0 1px 2px rgba(42, 32, 20, .05), 0 10px 28px rgba(42, 32, 20, .07);
  --wa: #25d366; --ig: #d62976; --sc: #e5b700;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #15120e;
    --panel: #1e1a15;
    --panel-2: #262019;
    --ink: #f0e8dc;
    --ink-2: #a3927c;
    --line: #322a21;
    --brand: #cab095;
    --brand-ink: #dcc7ad;
    --on-brand: #1a1714;
    --out: #3a301f;
    --note: #352c17;
    --note-line: #6b5a2c;
    --danger: #e08270;
    --shadow: 0 1px 2px rgba(0,0,0,.4), 0 10px 28px rgba(0,0,0,.35);
  }
}

* { box-sizing: border-box; }
html, body { height: 100%; }
body {
  margin: 0;
  font-family: "IBM Plex Sans Arabic", "SF Arabic", "Segoe UI", Tahoma, system-ui, sans-serif;
  background: var(--bg);
  color: var(--ink);
  font-size: 15px;
  line-height: 1.6;
  -webkit-text-size-adjust: 100%;
}

/* الأيقونات */
.i { width: 18px; height: 18px; flex: none; stroke: currentColor; fill: none;
     stroke-width: 1.6; stroke-linecap: round; stroke-linejoin: round; }
.i.sm { width: 15px; height: 15px; }
.i.lg { width: 20px; height: 20px; }
button, input, select, textarea { font: inherit; color: inherit; }
.muted { color: var(--ink-2); }
.small { font-size: .82rem; }
[hidden] { display: none !important; }

/* أزرار */
button { cursor: pointer; border-radius: 10px; border: 1px solid transparent;
  transition: background .15s, border-color .15s, color .15s;
  display: inline-flex; align-items: center; justify-content: center; gap: .4rem; }
.primary { background: var(--brand); color: var(--on-brand); border-color: var(--brand); padding: .6rem 1.1rem; font-weight: 600; }
.primary:hover { background: var(--brand-ink); border-color: var(--brand-ink); }
.primary:disabled { opacity: .55; cursor: default; }
.ghost { background: transparent; border-color: var(--line); color: var(--ink-2); padding: .45rem .8rem; }
.ghost:hover { background: var(--panel-2); color: var(--ink); border-color: var(--sand); }
input, select, textarea {
  background: var(--panel); border: 1px solid var(--line); border-radius: 10px; padding: .55rem .7rem; width: 100%;
}
input:focus-visible, select:focus-visible, textarea:focus-visible, button:focus-visible {
  outline: 2px solid var(--brand); outline-offset: 1px;
}
.form-error { color: var(--danger); margin: .4rem 0 0; font-size: .85rem; }

/* الهوية */
.brand { display: flex; align-items: center; gap: .6rem; }
.brand h1 { font-size: 1.25rem; margin: 0; font-weight: 600; letter-spacing: -.01em; }
.brand-mark { width: 32px; height: 32px; flex: none; color: var(--ink); }
.brand-mark svg { width: 100%; height: 100%; stroke: currentColor; fill: none; stroke-width: 5; }
.brand.small .brand-mark { width: 26px; height: 26px; }
.brand-wrap { display: grid; }
.brand-sub { font-size: .68rem; color: var(--ink-2); letter-spacing: .22em; margin-top: -.2rem; }

/* دخول */
.login { min-height: 100%; display: grid; place-items: center; padding: 1.5rem; }
.login-card {
  background: var(--panel); border: 1px solid var(--line); border-radius: 18px; box-shadow: var(--shadow);
  padding: 2rem; width: min(380px, 100%); display: grid; gap: .85rem;
}
.login-card label { display: grid; gap: .3rem; font-size: .88rem; color: var(--ink-2); }
.login-card .primary { margin-top: .4rem; }

/* التخطيط */
.app { display: grid; grid-template-columns: 340px 1fr; height: 100dvh; }
.sidebar {
  background: var(--panel); border-inline-end: 1px solid var(--line);
  display: flex; flex-direction: column; min-height: 0;
}
.sidebar-top { display: flex; align-items: center; justify-content: space-between; padding: .85rem 1rem; border-bottom: 1px solid var(--line); }
.live span { display: block; width: 9px; height: 9px; border-radius: 50%; background: var(--ink-2); }
.live.on span { background: var(--brand); box-shadow: 0 0 0 3px rgba(31,157,120,.18); }
.live.off span { background: var(--danger); }

.search-row { padding: .7rem 1rem .4rem; position: relative; }
.search-row .search-icon { position: absolute; inset-inline-start: 1.6rem; top: 50%; transform: translateY(-40%);
  color: var(--ink-2); pointer-events: none; }
.search-row input { padding-inline-start: 2.1rem; }
.search-row input::-webkit-search-cancel-button { filter: grayscale(1) opacity(.5); }
/* تلتفّ الأزرار على سطر ثانٍ: شريط التمرير مخفي، فكانت القناة الأخيرة (سناب شات) تختفي خارج العرض */
.chips { display: flex; flex-wrap: wrap; gap: .35rem; padding: .3rem 1rem .5rem; }
.chip {
  border: 1px solid var(--line); background: var(--panel-2); border-radius: 999px;
  padding: .28rem .7rem; font-size: .83rem; white-space: nowrap; display: flex; align-items: center; gap: .35rem;
}
.chip.active { background: var(--brand); color: var(--on-brand); border-color: var(--brand); }
.chip .n { font-size: .74rem; opacity: .8; }
.dot { width: 7px; height: 7px; border-radius: 50%; display: inline-block; }
.dot.whatsapp { background: var(--wa); } .dot.instagram { background: var(--ig); }
.dot.snapchat { background: var(--sc); }

.filters { display: grid; grid-template-columns: 1fr 1fr; gap: .4rem; padding: .3rem 1rem .6rem; align-items: center; }
.filters .check { grid-column: 1 / -1; }
.check { display: flex; align-items: center; gap: .4rem; font-size: .85rem; color: var(--ink-2); }
.check input { width: auto; }

.conv-list { list-style: none; margin: 0; padding: 0; overflow-y: auto; flex: 1; min-height: 0; border-top: 1px solid var(--line); }
.conv {
  display: grid; grid-template-columns: 38px 1fr auto; gap: .6rem; align-items: start;
  padding: .7rem 1rem; border-bottom: 1px solid var(--line); cursor: pointer; text-align: start; width: 100%;
  background: transparent; border-radius: 0;
}
.conv:hover { background: var(--panel-2); }
.conv.active { background: color-mix(in srgb, var(--brand) 10%, transparent); }
.avatar {
  width: 38px; height: 38px; border-radius: 50%; background: var(--panel-2);
  border: 1px solid var(--sand);
  display: grid; place-items: center; font-weight: 600; font-size: .9rem; color: var(--brand); flex: none;
}
.conv-main { min-width: 0; }
.conv-name { display: flex; align-items: center; gap: .35rem; font-weight: 600; }
.conv-name span.t { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.conv-prev { color: var(--ink-2); font-size: .84rem; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.conv-side { display: grid; gap: .3rem; justify-items: end; }
.badge { background: var(--brand); color: var(--on-brand); border-radius: 999px; font-size: .72rem; padding: .05rem .4rem; min-width: 18px; text-align: center; }
.tag { font-size: .7rem; border: 1px solid var(--line); border-radius: 6px; padding: 0 .3rem; color: var(--ink-2); }
.time { font-size: .72rem; color: var(--ink-2); white-space: nowrap; }
.sidebar-bottom { display: flex; gap: .4rem; padding: .6rem 1rem; border-top: 1px solid var(--line); }
.sidebar-bottom .ghost { flex: 1; }

/* المحادثة */
.thread { display: flex; min-height: 0; background: var(--bg); }
.empty { margin: auto; text-align: center; padding: 2rem; }
.empty h2 { margin: .8rem 0 .3rem; font-size: 1.1rem; }
.empty-art { width: 64px; height: 64px; margin: 0 auto; border-radius: 18px; background: var(--panel);
  border: 1px solid var(--line); box-shadow: var(--shadow); display: grid; place-items: center; color: var(--sand); }
.empty-art .i { width: 28px; height: 28px; stroke-width: 1.3; }
.thread-inner { display: flex; flex-direction: column; flex: 1; min-height: 0; min-width: 0; }
.thread-head {
  display: flex; align-items: center; gap: .7rem; padding: .7rem 1rem;
  background: var(--panel); border-bottom: 1px solid var(--line); flex-wrap: wrap;
}
.who { display: flex; align-items: center; gap: .6rem; flex: 1; min-width: 0; }
.who div { min-width: 0; }
.who strong { display: block; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.thread-actions { display: flex; gap: .4rem; }
.thread-actions select { width: auto; min-width: 118px; font-size: .85rem; padding: .35rem .5rem; }
.back { display: none; }

.messages { flex: 1; overflow-y: auto; padding: 1rem; display: flex; flex-direction: column; gap: .5rem; min-height: 0; }
.day { align-self: center; font-size: .75rem; color: var(--ink-2); background: var(--panel);
  border: 1px solid var(--line); border-radius: 999px; padding: .1rem .7rem; margin: .4rem 0; }
.msg { max-width: min(640px, 82%); padding: .55rem .8rem; border-radius: 14px; background: var(--panel);
  border: 1px solid var(--line); box-shadow: 0 1px 1px rgba(16,33,43,.04); }
.msg.in { align-self: flex-start; border-start-start-radius: 4px; }
.msg.out { align-self: flex-end; background: var(--out); border-color: transparent; border-start-end-radius: 4px; }
.msg.note { align-self: center; background: var(--note); border-color: var(--note-line); max-width: min(560px, 90%); }
.msg-body { white-space: pre-wrap; overflow-wrap: anywhere; }
.msg-meta { display: flex; gap: .4rem; align-items: center; font-size: .72rem; color: var(--ink-2); margin-top: .2rem; justify-content: flex-end; }
.msg.in .msg-meta { justify-content: flex-start; }
.msg-media { display: block; margin-bottom: .3rem; font-size: .8rem; color: var(--ink-2); }
.msg-media a { color: var(--brand-ink); }
.st-failed { color: var(--danger); font-weight: 600; }

.composer { background: var(--panel); border-top: 1px solid var(--line); padding: .6rem 1rem .8rem; }
.composer-bar { display: flex; align-items: center; gap: .6rem; margin-bottom: .45rem; flex-wrap: wrap; }
.composer-row { display: flex; gap: .5rem; align-items: flex-end; }
.composer textarea { resize: none; max-height: 180px; }
.composer .send { flex: none; }
.note-toggle { white-space: nowrap; }
.composer.note-on textarea { background: var(--note); }
.incoming-toggle { white-space: nowrap; }
/* وضع تدوين رسالة العميل: إطار بلون العلامة حتى لا يُخلط بالرد */
.composer.incoming-on textarea { border-color: var(--brand); box-shadow: inset 0 0 0 1px var(--brand); }

/* نوافذ */
.sheet {
  border: 1px solid var(--line); border-radius: 16px; background: var(--panel); color: var(--ink);
  padding: 1.2rem; width: min(460px, 94vw); box-shadow: var(--shadow); max-height: 86dvh; overflow: auto;
}
.sheet.wide { width: min(600px, 94vw); }
.sheet::backdrop { background: rgba(10, 18, 24, .5); }
.sheet h3 { margin: 0 0 .7rem; }
.sheet h4 { margin: 1rem 0 .4rem; font-size: .9rem; color: var(--ink-2); }
.sheet form { display: grid; gap: .55rem; }
.sheet form label { display: grid; gap: .25rem; font-size: .85rem; color: var(--ink-2); }
.sheet .close { margin-top: .9rem; }
.sheet-actions { display: flex; gap: .5rem; justify-content: space-between; margin-top: 1rem; }
.sheet-actions .close { margin: 0; }

.qr-list, .status-list { list-style: none; margin: 0 0 .7rem; padding: 0; display: grid; gap: .4rem; }
.qr-list li, .status-list li {
  display: flex; align-items: center; gap: .5rem; justify-content: space-between;
  border: 1px solid var(--line); border-radius: 10px; padding: .45rem .6rem; background: var(--panel-2);
}
.qr-list .t { font-weight: 600; }
.qr-list .b { color: var(--ink-2); font-size: .82rem; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.qr-list button.use { margin-inline-start: auto; }
.qr-form { border-top: 1px solid var(--line); padding-top: .7rem; }
.user-form { grid-template-columns: 1fr 1fr; display: grid; }
.user-form button { grid-column: 1 / -1; }
.pill { font-size: .75rem; border-radius: 999px; padding: .1rem .55rem; border: 1px solid var(--line); }
.pill.ok { background: color-mix(in srgb, var(--brand) 16%, transparent); color: var(--brand-ink); border-color: transparent; }
.pill.no { background: color-mix(in srgb, var(--danger) 12%, transparent); color: var(--danger); border-color: transparent; }
.pill.manual { background: var(--note); color: #7a5c00; border-color: transparent; }
.pill.warn { background: var(--note); color: #7a5c00; border-color: var(--note-line); }

.toast {
  position: fixed; inset-block-end: 1.2rem; inset-inline-start: 50%; transform: translateX(50%);
  background: var(--ink); color: var(--panel); padding: .55rem 1rem; border-radius: 10px; z-index: 50; box-shadow: var(--shadow);
}

/* عناصر الإنتاجية */
.top-right { display: flex; align-items: center; gap: .5rem; }
.ghost.icon { padding: .35rem; line-height: 1; border-color: transparent; }
.ghost.icon:hover { border-color: var(--line); }
.tiny { padding: .3rem .6rem; font-size: .8rem; }
.waiting-bar {
  display: flex; align-items: center; justify-content: space-between; gap: .5rem;
  padding: .5rem 1rem; border-top: 1px solid var(--line); background: var(--panel-2); font-size: .85rem;
}
.sla { font-size: .75rem; border-radius: 999px; padding: .1rem .55rem; white-space: nowrap; flex: none; }
.sla.ok { background: color-mix(in srgb, var(--brand) 16%, transparent); color: var(--brand-ink); }
.sla.warn { background: var(--note); color: #7a5c00; }
.sla.late { background: color-mix(in srgb, var(--danger) 14%, transparent); color: var(--danger); font-weight: 600; }
.sla.follow { background: transparent; color: var(--brand-ink); border: 1px dashed var(--brand); }
.conv .sla { margin-top: .1rem; }
.follow-row { padding-top: 0; }
.follow-row .check { grid-column: auto; }
.follow-count { background: var(--brand); color: var(--on-brand); border-radius: 999px; font-size: .72rem; padding: .02rem .4rem; min-width: 18px; text-align: center; }
.tiny.ghost { padding: .2rem .6rem; font-size: .78rem; }
.qr-strip { display: flex; gap: .3rem; overflow-x: auto; scrollbar-width: none; flex: 1; min-width: 0; }
.qr-strip::-webkit-scrollbar { display: none; }
.qr-strip button {
  border: 1px solid var(--line); background: var(--panel-2); border-radius: 999px;
  padding: .18rem .6rem; font-size: .78rem; white-space: nowrap; color: var(--ink-2);
}
.qr-strip button:hover { background: var(--bg); color: var(--ink); }
.qr-strip kbd { font-size: .68rem; opacity: .6; margin-inline-start: .25rem; }

/* التحليلات */
.analytics { grid-column: 1 / -1; display: flex; flex-direction: column; min-height: 0; background: var(--bg); }
.app.on-analytics .sidebar, .app.on-analytics .thread { display: none; }
.an-head {
  display: flex; align-items: center; justify-content: space-between; gap: .7rem; flex-wrap: wrap;
  padding: .85rem 1.2rem; background: var(--panel); border-bottom: 1px solid var(--line);
}
.an-head h2 { margin: 0; font-size: 1.1rem; }
.an-head-actions { display: flex; gap: .4rem; flex-wrap: wrap; }
.an-head-actions select { width: auto; padding: .35rem .5rem; font-size: .85rem; }
.an-body { overflow-y: auto; padding: 1.2rem; min-height: 0; }
.an-section { margin: .2rem 0 .8rem; font-size: .95rem; color: var(--ink-2); font-weight: 600;
  border-inline-start: 3px solid var(--brand); padding-inline-start: .55rem; }
.kpis { display: grid; grid-template-columns: repeat(auto-fit, minmax(150px, 1fr)); gap: .7rem; margin-bottom: 1.2rem; }
.kpi { background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); padding: .8rem .9rem; box-shadow: var(--shadow); }
.kpi .v { font-size: 1.5rem; font-weight: 700; line-height: 1.2; font-variant-numeric: tabular-nums; }
.kpi .k { font-size: .8rem; color: var(--ink-2); }
.kpi .sub { font-size: .74rem; color: var(--ink-2); margin-top: .15rem; }
.kpi.good .v { color: var(--brand-ink); }
.kpi.bad .v { color: var(--danger); }
.cards { display: grid; grid-template-columns: repeat(auto-fit, minmax(300px, 1fr)); gap: .8rem; margin-bottom: 1.6rem; }
.card { background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); padding: .9rem 1rem; box-shadow: var(--shadow); }
.card h4 { margin: 0 0 .7rem; font-size: .9rem; }
.chart svg { width: 100%; height: auto; display: block; overflow: visible; }
.table-wrap { overflow-x: auto; }
.card table { width: 100%; border-collapse: collapse; font-size: .85rem; }
.card th, .card td { text-align: start; padding: .35rem .4rem; border-bottom: 1px solid var(--line); white-space: nowrap; }
.card th { color: var(--ink-2); font-weight: 600; font-size: .78rem; }
.card td { font-variant-numeric: tabular-nums; }
.card tr:last-child td { border-bottom: none; }
.metric-form { display: grid; grid-template-columns: 1fr 1fr; gap: .5rem; }
.metric-form label { display: grid; gap: .2rem; font-size: .78rem; color: var(--ink-2); }
.metric-form button { grid-column: 1 / -1; }
code { background: var(--panel-2); border: 1px solid var(--line); border-radius: 5px; padding: 0 .3rem; font-size: .8em; }
.perm-form { display: grid; gap: .8rem; }
.perm-form label { display: grid; gap: .3rem; }
.perm-row { display: grid; grid-template-columns: 1fr 1fr; gap: .7rem; }
.perm-form fieldset { border: 1px solid var(--line); border-radius: 12px; padding: .4rem .8rem .8rem; margin: 0; display: grid; gap: .4rem; }
.perm-form fieldset[disabled] { opacity: .45; }
.perm-form legend { font-weight: 600; font-size: .85rem; padding: .5rem 0 0; float: inline-start; width: 100%; }
.perm-options { display: flex; flex-wrap: wrap; gap: .4rem; }
.perm-form .perm-options label { display: inline-flex; flex-direction: row; align-items: center; gap: .35rem; padding: .3rem .65rem; border: 1px solid var(--line); border-radius: 999px; font-size: .85rem; cursor: pointer; }
.perm-form .perm-options input { margin: 0; width: auto; }
.perm-options label:has(input:checked) { background: color-mix(in srgb, var(--brand) 14%, transparent); border-color: transparent; }
.perm-summary { font-size: .75rem; }
.no-perm { padding: .9rem 1rem; text-align: center; }
@media (max-width: 560px) { .perm-row { grid-template-columns: 1fr; } }
.copy-row { display: flex; align-items: center; gap: .5rem; flex-wrap: wrap; min-width: 0; }
.copy-row code { overflow-wrap: anywhere; }
.small { font-size: .82rem; }

/* جوال */
@media (max-width: 760px) {
  .app { grid-template-columns: 1fr; }
  .sidebar { border-inline-end: none; }
  .app.on-thread .sidebar { display: none; }
  .app:not(.on-thread) .thread { display: none; }
  .back { display: block; }
  .thread-actions { width: 100%; }
  .thread-actions select { flex: 1; min-width: 0; }
  .msg { max-width: 88%; }
}
