:root {
  --bg: #f1f5f9; --card: #fff; --ink: #0f172a; --muted: #64748b; --line: #e2e8f0;
  --brand: #0f172a; --accent: #f59e0b; --ok: #16a34a; --bad: #dc2626; --warn: #d97706;
  --radius: 14px;
}
@media (prefers-color-scheme: dark) {
  :root { --bg: #0b1120; --card: #111827; --ink: #e5e7eb; --muted: #94a3b8; --line: #1f2937; }
}
* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0; background: var(--bg); color: var(--ink);
  font: 16px/1.45 system-ui, -apple-system, "Segoe UI", Tahoma, "Noto Sans Arabic", sans-serif;
  padding-bottom: calc(72px + env(safe-area-inset-bottom));
}
body.embed { padding-bottom: 8px; }
a { color: inherit; }
main { max-width: 760px; margin: 0 auto; padding: 8px 12px; }
h1, h2, h3, h4 { margin: 0 0 8px; line-height: 1.25; }
h2 { font-size: 1.1rem; } h3 { font-size: 1rem; } h4 { font-size: .9rem; color: var(--muted); margin-top: 12px; }
small, .small { font-size: .82rem; }
.muted { color: var(--muted); }

.top {
  position: sticky; top: 0; z-index: 5; display: flex; justify-content: space-between; align-items: center;
  padding: 10px 14px; padding-top: calc(10px + env(safe-area-inset-top));
  background: var(--brand); color: #fff;
}
.top .brand { font-weight: 700; text-decoration: none; }
.top-actions { display: flex; gap: 14px; align-items: center; }
.top .link { color: #fff; }
.bell { position: relative; text-decoration: none; font-size: 1.2rem; }
.badge {
  position: absolute; top: -6px; inset-inline-end: -10px; background: var(--bad); color: #fff;
  border-radius: 999px; font-size: .7rem; padding: 1px 6px; font-weight: 700;
}

nav.bottom {
  position: fixed; bottom: 0; left: 0; right: 0; z-index: 5; display: flex; justify-content: space-around;
  background: var(--card); border-top: 1px solid var(--line);
  padding: 6px 4px calc(6px + env(safe-area-inset-bottom));
}
nav.bottom a { flex: 1; text-align: center; text-decoration: none; font-size: .7rem; color: var(--muted); }
nav.bottom a span { display: block; font-size: 1.25rem; }
nav.bottom a.on { color: var(--ink); font-weight: 700; }

.card {
  background: var(--card); border-radius: var(--radius); padding: 14px; margin: 10px 0;
  box-shadow: 0 1px 2px rgb(0 0 0 / .06);
}
.card.narrow { max-width: 420px; margin: 40px auto; }
details.card > summary { cursor: pointer; font-weight: 600; }
details.card[open] > summary { margin-bottom: 10px; }
.head { display: flex; justify-content: space-between; align-items: center; gap: 8px; }
.count { background: var(--line); border-radius: 999px; padding: 0 8px; font-size: .85rem; }

.row {
  display: flex; justify-content: space-between; align-items: center; gap: 10px;
  padding: 10px 0; border-bottom: 1px solid var(--line); text-decoration: none;
}
.row:last-child { border-bottom: 0; }
.row.wrap { flex-wrap: wrap; justify-content: flex-start; }
.row.late, tr.late { color: var(--bad); }
.late-card { border-inline-start: 4px solid var(--bad); }
.time { white-space: nowrap; font-variant-numeric: tabular-nums; font-weight: 600; }
.actions { display: flex; gap: 6px; align-items: center; flex-wrap: wrap; justify-content: flex-end; }

.banner { padding: 10px 14px; color: #fff; }
.banner.danger { background: var(--bad); }
.banner.warn { background: var(--warn); }
.flash { max-width: 760px; margin: 10px auto 0; padding: 10px 14px; border-radius: 10px; }
.flash.ok { background: #dcfce7; color: #14532d; }
.flash.error { background: #fee2e2; color: #7f1d1d; }

form.inline { display: inline-flex; gap: 6px; align-items: center; margin: 0; }
.stack { display: flex; flex-direction: column; gap: 10px; }
.stack label { display: flex; flex-direction: column; gap: 4px; font-size: .9rem; }
label.check { flex-direction: row !important; align-items: center; gap: 8px; }
input, select, textarea, button {
  font: inherit; color: inherit; border: 1px solid var(--line); border-radius: 10px; padding: 10px 12px;
  background: var(--card); min-height: 44px;
}
input[type=checkbox], input[type=radio] { min-height: 0; width: 20px; height: 20px; }
textarea { resize: vertical; }
button, .button {
  cursor: pointer; background: var(--line); font-weight: 600; text-decoration: none; display: inline-block;
  text-align: center;
}
button.primary, .button.primary { background: var(--brand); color: #fff; border-color: var(--brand); }
button.secondary, .button.secondary { background: transparent; }
button.danger { background: var(--bad); color: #fff; border-color: var(--bad); }
button.small, .small button { min-height: 34px; padding: 4px 10px; font-size: .85rem; }
button.link { background: none; border: 0; padding: 0; min-height: 0; text-decoration: underline; }

.segmented { display: flex; gap: 6px; }
.segmented form { display: flex; gap: 6px; width: 100%; }
.seg { flex: 1; }
.seg.on { color: #fff; }
.seg.st-online.on { background: var(--ok); border-color: var(--ok); }
.seg.st-busy.on { background: var(--warn); border-color: var(--warn); }
.seg.st-offline.on, .seg.on { background: var(--brand); border-color: var(--brand); }
.seg.st-online.on { background: var(--ok); }

.chips { display: flex; flex-wrap: wrap; gap: 8px; }
.chip { display: inline-flex !important; flex-direction: row !important; align-items: center; gap: 6px;
  border: 1px solid var(--line); border-radius: 999px; padding: 8px 12px; cursor: pointer; }
.chip:has(input:checked) { border-color: var(--ink); background: var(--line); font-weight: 600; }
.js .when { display: none; }
.js .when.show { display: flex; flex-direction: column; gap: 4px; }

.stage { border-radius: 999px; padding: 2px 10px; font-size: .78rem; font-weight: 700; white-space: nowrap;
  background: var(--line); }
.s-new { background: #dbeafe; color: #1e3a8a; }
.s-replied { background: #e0e7ff; color: #3730a3; }
.s-quote_sent { background: #fef3c7; color: #78350f; }
.s-won { background: #dcfce7; color: #14532d; }
.s-lost { background: #fee2e2; color: #7f1d1d; }
.pill { border-radius: 999px; padding: 2px 8px; font-size: .75rem; }
.pill.ok { background: #dcfce7; color: #14532d; }

.facts { display: grid; grid-template-columns: max-content 1fr; gap: 4px 12px; margin: 8px 0; }
.facts dt { color: var(--muted); }
.facts dd { margin: 0; }
.timeline { list-style: none; padding: 0; margin: 0; }
.timeline li { padding: 6px 0; border-bottom: 1px dashed var(--line); }

.alert { display: flex; justify-content: space-between; align-items: center; gap: 8px;
  padding: 10px 0; border-bottom: 1px solid var(--line); }
.alert a { text-decoration: none; flex: 1; }
.alert.unread strong::before { content: "● "; color: var(--accent); }
.k-escalation strong, .k-channel_disconnected strong { color: var(--bad); }

.filters { display: flex; flex-wrap: wrap; gap: 8px; align-items: flex-end; }
.filters label { display: flex; flex-direction: column; font-size: .8rem; gap: 2px; }
.filters input, .filters select { min-width: 0; }
.grid-form { display: grid; grid-template-columns: repeat(auto-fit, minmax(200px, 1fr)); gap: 10px; margin: 10px 0 18px; }
.grid-form label { display: flex; flex-direction: column; gap: 4px; font-size: .85rem; }
.grid2 { display: grid; grid-template-columns: repeat(auto-fit, minmax(240px, 1fr)); gap: 12px; }

.kpis { display: grid; grid-template-columns: repeat(auto-fit, minmax(120px, 1fr)); gap: 8px; margin: 8px 0; }
.kpis > div { background: var(--bg); border-radius: 10px; padding: 10px; text-align: center; }
.kpis b { display: block; font-size: 1.4rem; }
.kpis .bad b { color: var(--bad); } .kpis .good b { color: var(--ok); }
.bars .bar { display: grid; grid-template-columns: minmax(90px, 40%) 1fr; gap: 8px; align-items: center; margin: 4px 0; }
.bars .k { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-size: .85rem; }
.bars .v { display: block; background: linear-gradient(90deg, var(--accent) var(--w), transparent var(--w));
  border-radius: 6px; padding: 2px 6px; font-size: .8rem; font-weight: 700; }
[dir=rtl] .bars .v { background: linear-gradient(270deg, var(--accent) var(--w), transparent var(--w)); }
.spark { display: flex; align-items: flex-end; gap: 2px; height: 70px; }
.spark span { flex: 1; height: var(--h); min-height: 2px; background: var(--accent); border-radius: 2px 2px 0 0; }
table { width: 100%; border-collapse: collapse; font-size: .88rem; margin: 6px 0; }
th, td { text-align: start; padding: 6px 4px; border-bottom: 1px solid var(--line); }
.danger-zone summary { color: var(--bad); }

/* Pipeline: New -> Replied -> Quote sent -> Won / Lost */
.pipeline { display: grid; grid-template-columns: repeat(4, 1fr); gap: 6px; margin: 10px 0; }
.pipeline .step {
  position: relative; display: flex; flex-direction: column; align-items: center; justify-content: center;
  gap: 2px; padding: 10px 4px; border-radius: 12px; text-decoration: none; text-align: center;
  background: var(--card); border: 2px solid transparent; box-shadow: 0 1px 2px rgb(0 0 0 / .06);
}
.pipeline .step b { font-size: 1.35rem; line-height: 1; }
.pipeline .step span { font-size: .72rem; color: var(--muted); }
.pipeline .step:not(:last-child)::after {
  content: "‹"; position: absolute; inset-inline-end: -8px; top: 50%; transform: translateY(-50%);
  color: var(--muted); font-size: 1rem; z-index: 1;
}
[dir=ltr] .pipeline .step:not(:last-child)::after { content: "›"; }
.pipeline .g-new b { color: #1d4ed8; }
.pipeline .g-replied b { color: #4338ca; }
.pipeline .g-quote_sent b { color: #b45309; }
.pipeline .g-won_lost b { color: #15803d; }
.pipeline .step.on { border-color: currentColor; }
.pipeline .g-new.on { border-color: #1d4ed8; }
.pipeline .g-replied.on { border-color: #4338ca; }
.pipeline .g-quote_sent.on { border-color: #b45309; }
.pipeline .g-won_lost.on { border-color: #15803d; }

/* ---------------------------------------------------------------- tabs */
.tabs { display: flex; gap: 6px; overflow-x: auto; margin: 10px 0; scrollbar-width: none; }
.tabs a { flex: 0 0 auto; padding: 8px 14px; border-radius: 999px; background: var(--card);
  text-decoration: none; font-weight: 600; font-size: .9rem; box-shadow: 0 1px 2px rgb(0 0 0 / .06); }
.tabs a.on { background: var(--brand); color: #fff; }
.badge-inline { background: var(--bad); color: #fff; border-radius: 999px; padding: 0 7px; font-size: .75rem; }

/* ---------------------------------------------------------------- chat list */
.chat-list { padding: 4px 10px; }
.chat-item { display: flex; align-items: center; gap: 10px; padding: 10px 2px;
  border-bottom: 1px solid var(--line); text-decoration: none; }
.chat-item:last-child { border-bottom: 0; }
.chat-main { display: flex; align-items: center; gap: 10px; flex: 1; min-width: 0; text-decoration: none; }
.chat-text { display: flex; flex-direction: column; min-width: 0; flex: 1; }
.chat-text b { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.chat-side { display: flex; flex-direction: column; align-items: flex-end; gap: 4px; }
.wait { font-size: .78rem; font-weight: 700; color: var(--warn); white-space: nowrap; }
.wait.late { color: var(--bad); }
.avatar { width: 42px; height: 42px; flex: 0 0 42px; border-radius: 50%; display: grid; place-items: center;
  font-size: 1.1rem; background: #e2e8f0; }
.ch-whatsapp { background: #dcfce7; } .ch-instagram { background: #fae8ff; }
.ch-facebook { background: #dbeafe; } .ch-tiktok { background: #e5e7eb; }

/* ---------------------------------------------------------------- one chat */
.chat-page { padding-bottom: 0; }
.chat-page nav.bottom { display: none; }
.chat-page main { padding-bottom: 0; }
.chat-head { position: sticky; top: 52px; z-index: 4; display: flex; align-items: center; gap: 10px;
  background: var(--card); margin: 0 -12px; padding: 8px 12px; border-bottom: 1px solid var(--line); }
.chat-head .back { font-size: 1.4rem; text-decoration: none; padding: 0 4px; }
.chat-title { display: flex; flex-direction: column; flex: 1; min-width: 0; }
.chat-title small { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.pipeline.mini { margin: 8px 0; gap: 4px; }
.pipeline.mini .step { padding: 6px 2px; border-radius: 10px; }
.pipeline.mini .step span { font-size: .7rem; }
.pipeline.mini .step.past { background: #f1f5f9; opacity: .8; }
.pipeline.mini .step.on { background: var(--brand); border-color: var(--brand); }
.pipeline.mini .step.on span { color: #fff; font-weight: 700; }

.thread { display: flex; flex-direction: column; gap: 6px; padding: 10px 2px 16px; min-height: 40vh;
  background-image: radial-gradient(rgb(148 163 184 / .18) 1px, transparent 1px); background-size: 16px 16px; }
.older { align-self: center; font-size: .85rem; margin-bottom: 6px; }
.msg { max-width: 82%; padding: 8px 12px 6px; border-radius: 16px; box-shadow: 0 1px 1px rgb(0 0 0 / .08);
  word-wrap: break-word; overflow-wrap: anywhere; }
.msg p { margin: 0; line-height: 1.5; }
.msg .who { font-size: .72rem; font-weight: 700; color: var(--muted); margin-bottom: 2px; }
.msg .meta { font-size: .68rem; color: var(--muted); text-align: end; margin-top: 2px; }
.msg img { display: block; max-width: 100%; border-radius: 10px; margin-bottom: 4px; }
.msg video { max-width: 100%; border-radius: 10px; }
.msg audio { width: 230px; max-width: 100%; }
.msg .err { color: var(--bad); font-size: .75rem; }
.msg.in { align-self: flex-start; background: var(--card); border-start-start-radius: 4px; }
.msg.out { align-self: flex-end; background: #d9fdd3; border-start-end-radius: 4px; }
.msg.out.auto { background: #e0f2fe; }
.msg.note { align-self: flex-end; background: #fef9c3; border: 1px dashed #eab308; }
.msg.event { align-self: center; background: none; box-shadow: none; padding: 2px; }
.msg.event span { font-size: .72rem; color: var(--muted); background: var(--card); border-radius: 999px; padding: 2px 10px; }
.st-read { color: #0ea5e9; } .st-failed { color: var(--bad); }
@media (prefers-color-scheme: dark) {
  .msg.out { background: #134e4a; } .msg.out.auto { background: #0c4a6e; } .msg.note { background: #422006; }
}

.quick { display: flex; gap: 6px; overflow-x: auto; padding: 6px 0; scrollbar-width: none; }
.chip-btn { flex: 0 0 auto; min-height: 34px; padding: 4px 12px; border-radius: 999px; font-size: .85rem;
  background: var(--card); border: 1px solid var(--line); font-weight: 500; }
.composer { position: sticky; bottom: 0; z-index: 4; display: flex; flex-wrap: wrap; align-items: flex-end; gap: 6px;
  background: var(--bg); margin: 0 -12px; padding: 8px 12px calc(8px + env(safe-area-inset-bottom)); }
.composer textarea { flex: 1; min-height: 46px; max-height: 160px; border-radius: 22px; padding: 11px 16px; resize: none; }
.composer .send { width: 46px; height: 46px; min-height: 46px; padding: 0; border-radius: 50%; background: #16a34a;
  color: #fff; border: 0; font-size: 1.1rem; }
[dir=rtl] .composer .send { transform: scaleX(-1); }
.composer .attach { width: 42px; height: 46px; display: grid; place-items: center; font-size: 1.3rem; cursor: pointer; }
.composer .note-toggle { width: 100%; display: flex; align-items: center; gap: 6px; font-size: .8rem; color: var(--muted); }
.composer.note-on textarea { background: #fef9c3; }
.composer.sending .send { opacity: .5; }
.composer-closed { border: 1px solid #f59e0b; }
.chat-actions { display: flex; justify-content: space-between; align-items: center; gap: 8px; padding: 6px 0 14px; }
.center { text-align: center; }

.banner-box { margin: 10px 0; padding: 12px 14px; border-radius: var(--radius); font-weight: 600; }

/* ---------------------------------------------------------------- handbook */
.handbook h1 { font-size: 1.3rem; }
.handbook h3 { margin-top: 14px; }
.handbook li { margin: 4px 0; }
.handbook .toc { display: grid; gap: 4px; margin-top: 8px; }
.handbook .toc a { text-decoration: none; padding: 4px 0; border-bottom: 1px dashed var(--line); }
.handbook .tip { background: #fef9c3; border-radius: 10px; padding: 8px 12px; }
.handbook .pipeline-text { font-weight: 700; text-align: center; background: var(--bg); border-radius: 10px; padding: 8px; }
.handbook table.fill td { height: 34px; }
@media print {
  header.top, nav.bottom, .banner, .flash { display: none !important; }
  body { padding: 0; background: #fff; }
  .card { box-shadow: none; border: 1px solid #ddd; break-inside: avoid; }
  .handbook .toc { display: none; }
}

.chat-actions { flex-wrap: wrap; }
.more { flex-basis: 100%; order: 3; }
.more > summary { cursor: pointer; color: var(--muted); }
.more-box { display: grid; gap: 14px; background: var(--card); border-radius: 12px; padding: 12px; margin-top: 8px; }
