:root {
  --bg: #f3f5f4;
  --panel: #ffffff;
  --panel-2: #f7f9f8;
  --line: #e2e8e5;
  --text: #16211d;
  --muted: #62716b;
  --brand: #0b8a6f;
  --brand-ink: #ffffff;
  --brand-soft: #e3f4ef;
  --bubble-in: #ffffff;
  --bubble-out: #d9f7e8;
  --chat-bg: #eef2ef;
  --danger: #c6392f;
  --warn-bg: #fff6de;
  --warn-ink: #7a5a00;
  --ai: #6a4bd6;
  --ai-soft: #efeafe;
  --radius: 12px;
  --shadow: 0 1px 2px rgba(0,0,0,.06), 0 4px 16px rgba(0,0,0,.04);
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0f1513;
    --panel: #161e1b;
    --panel-2: #1b2521;
    --line: #26332e;
    --text: #e6eeea;
    --muted: #93a39c;
    --brand: #19b08f;
    --brand-ink: #04130f;
    --brand-soft: #173a31;
    --bubble-in: #1e2925;
    --bubble-out: #174a3c;
    --chat-bg: #111916;
    --warn-bg: #3a300f;
    --warn-ink: #f2d27a;
    --ai: #a996ff;
    --ai-soft: #2a2447;
    --shadow: none;
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body {
  font-family: "Segoe UI", Tahoma, "Noto Sans Arabic", system-ui, -apple-system, sans-serif;
  background: var(--bg);
  color: var(--text);
  font-size: 15px;
  line-height: 1.5;
}
.hidden { display: none !important; }
.muted { color: var(--muted); }
.ltr { direction: ltr; unicode-bidi: plaintext; }
.error { color: var(--danger); min-height: 1.2em; margin: 0; }
h1, h2, h3 { margin: 0 0 .4em; line-height: 1.25; }
h2 { font-size: 1.25rem; }
h3 { font-size: 1.05rem; }

input, select, textarea {
  width: 100%;
  font: inherit;
  color: var(--text);
  background: var(--panel-2);
  border: 1px solid var(--line);
  border-radius: 10px;
  padding: .6em .75em;
  outline: none;
}
input:focus, select:focus, textarea:focus { border-color: var(--brand); box-shadow: 0 0 0 3px var(--brand-soft); }
textarea { resize: vertical; }
label { display: block; font-weight: 600; font-size: .9rem; margin-bottom: .9rem; }
label > input, label > select, label > textarea { margin-top: .35rem; font-weight: 400; }

.btn {
  font: inherit; font-weight: 600;
  border: 1px solid var(--line);
  background: var(--panel);
  color: var(--text);
  border-radius: 10px;
  padding: .55em 1.1em;
  cursor: pointer;
  white-space: nowrap;
}
.btn:hover { background: var(--panel-2); }
.btn.primary { background: var(--brand); color: var(--brand-ink); border-color: transparent; }
.btn.primary:hover { filter: brightness(1.06); }
.btn.danger { color: var(--danger); }
.btn.icon { padding: .4em .65em; line-height: 1; font-size: 1.05rem; }
.btn:disabled { opacity: .55; cursor: not-allowed; }

.row { display: flex; align-items: center; justify-content: space-between; gap: .75rem; }
.row.gap > input { flex: 1; }
.row.end { justify-content: flex-end; }
.gap { gap: .6rem; }
.grid2 { display: grid; grid-template-columns: 1fr 1fr; gap: 0 1rem; }

.brand-mark {
  width: 56px; height: 56px; border-radius: 50%;
  background: var(--brand); color: var(--brand-ink);
  display: grid; place-items: center;
  font-size: 1.6rem; font-weight: 800;
}
.brand-mark.small { width: 38px; height: 38px; font-size: 1.1rem; margin-bottom: .5rem; }
.brand-mark.big { width: 84px; height: 84px; font-size: 2.3rem; opacity: .9; }

/* Login */
.login { min-height: 100%; display: grid; place-items: center; padding: 16px; }
.login-card {
  width: min(380px, 100%);
  background: var(--panel); border: 1px solid var(--line); border-radius: 18px;
  padding: 2rem 1.6rem; box-shadow: var(--shadow);
  display: flex; flex-direction: column; align-items: stretch; text-align: center;
}
.login-card .brand-mark { margin: 0 auto 1rem; }
.login-card label { text-align: start; margin-top: 1rem; }

/* Layout */
.app { display: flex; height: 100vh; height: 100dvh; }
.rail {
  width: 76px; flex-shrink: 0;
  background: var(--panel); border-inline-start: 1px solid var(--line);
  display: flex; flex-direction: column; align-items: center; padding: 12px 6px; gap: 4px;
}
.rail-btn {
  width: 64px; border: 0; background: transparent; color: var(--muted);
  border-radius: 12px; padding: 8px 2px; cursor: pointer;
  display: flex; flex-direction: column; align-items: center; gap: 2px; font: inherit; font-size: .72rem;
}
.rail-btn svg { width: 22px; height: 22px; fill: none; stroke: currentColor; stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round; }
.rail-btn:hover { background: var(--panel-2); color: var(--text); }
.rail-btn.active { background: var(--brand-soft); color: var(--brand); }
.rail-spacer { flex: 1; }
.live-dot { width: 10px; height: 10px; border-radius: 50%; background: var(--danger); margin-bottom: 8px; }
.live-dot.on { background: #22c55e; }

.view { flex: 1; min-width: 0; }

/* Inbox */
.inbox { display: flex; height: 100%; }
.chat-list {
  width: 340px; flex-shrink: 0; background: var(--panel);
  border-inline-start: 1px solid var(--line);
  display: flex; flex-direction: column; min-height: 0;
}
.list-head { display: flex; align-items: center; justify-content: space-between; padding: 14px 14px 6px; }
.list-head h2 { margin: 0; }
.search { margin: 6px 14px 10px; width: auto; }
.contacts { list-style: none; margin: 0; padding: 0; overflow-y: auto; flex: 1; }
.contact {
  display: flex; gap: 10px; align-items: center; padding: 10px 14px; cursor: pointer;
  border-bottom: 1px solid var(--line);
}
.contact:hover { background: var(--panel-2); }
.contact.active { background: var(--brand-soft); }
.avatar {
  width: 44px; height: 44px; border-radius: 50%; flex-shrink: 0;
  display: grid; place-items: center; font-weight: 700; color: #fff;
  background: var(--brand);
}
.contact-body { flex: 1; min-width: 0; }
.contact-top { display: flex; justify-content: space-between; gap: 8px; }
.contact-name { unicode-bidi: plaintext; text-align: start; font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.contact-time { color: var(--muted); font-size: .75rem; flex-shrink: 0; }
.contact-bottom { display: flex; justify-content: space-between; gap: 8px; align-items: center; }
.contact-preview { color: var(--muted); font-size: .85rem; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.badge {
  background: var(--brand); color: var(--brand-ink); font-size: .72rem; font-weight: 700;
  min-width: 20px; height: 20px; border-radius: 10px; padding: 0 6px; display: grid; place-items: center;
}
.tag { font-size: .68rem; font-weight: 700; border-radius: 6px; padding: 1px 6px; }
.tag.ai { background: var(--ai-soft); color: var(--ai); }
.tag.human { background: var(--warn-bg); color: var(--warn-ink); }
.empty-list { padding: 2rem 1rem; text-align: center; color: var(--muted); }

.chat { flex: 1; min-width: 0; display: flex; flex-direction: column; background: var(--chat-bg); }
.chat-empty { margin: auto; text-align: center; color: var(--muted); display: flex; flex-direction: column; align-items: center; gap: .5rem; }
.chat-inner { display: flex; flex-direction: column; height: 100%; min-height: 0; }
.chat-head {
  display: flex; align-items: center; gap: 10px; padding: 10px 14px;
  background: var(--panel); border-bottom: 1px solid var(--line);
}
.chat-title { flex: 1; min-width: 0; display: flex; flex-direction: column; }
.chat-title strong { unicode-bidi: plaintext; text-align: start; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.chat-title .muted { font-size: .8rem; text-align: start; }
.back { display: none; }

.messages { flex: 1; overflow-y: auto; padding: 16px 5%; display: flex; flex-direction: column; gap: 6px; }
.day { align-self: center; font-size: .75rem; color: var(--muted); background: var(--panel); border-radius: 8px; padding: 2px 10px; margin: 8px 0; }
.bubble {
  max-width: min(75%, 560px); padding: 7px 10px 5px; border-radius: 12px;
  box-shadow: 0 1px 1px rgba(0,0,0,.06); word-wrap: break-word;
}
.bubble-text { white-space: pre-wrap; unicode-bidi: plaintext; }
.bubble.in { align-self: flex-start; background: var(--bubble-in); border-start-start-radius: 4px; }
.bubble.out { align-self: flex-end; background: var(--bubble-out); border-start-end-radius: 4px; }
.bubble.failed { outline: 1px solid var(--danger); }
.bubble-meta { display: flex; gap: 6px; justify-content: flex-end; align-items: center; font-size: .7rem; color: var(--muted); margin-top: 2px; }
.ticks { font-weight: 700; letter-spacing: -2px; }
.ticks.read { color: #34b7f1; }
.bubble-error { color: var(--danger); font-size: .75rem; margin-top: 3px; }
.src { font-size: .66rem; font-weight: 700; border-radius: 5px; padding: 0 5px; }
.src.ai { background: var(--ai-soft); color: var(--ai); }
.src.campaign { background: var(--warn-bg); color: var(--warn-ink); }
.src.order { background: var(--brand-soft); color: var(--brand); }

.window-note { background: var(--warn-bg); color: var(--warn-ink); padding: 8px 14px; font-size: .85rem; text-align: center; }
.composer { display: flex; gap: 8px; align-items: flex-end; padding: 10px 12px; background: var(--panel); border-top: 1px solid var(--line); }
.composer textarea { resize: none; max-height: 160px; min-height: 42px; }
.send { padding: .55em .8em; }
.send svg { width: 20px; height: 20px; fill: currentColor; transform: scaleX(-1); display: block; }

/* switch */
.switch { display: inline-flex; align-items: center; gap: 8px; margin: 0; cursor: pointer; font-weight: 600; font-size: .82rem; }
.switch input { display: none; }
.slider { width: 40px; height: 22px; background: var(--line); border-radius: 11px; position: relative; transition: .2s; flex-shrink: 0; }
.slider::after { content: ""; position: absolute; top: 3px; inset-inline-start: 3px; width: 16px; height: 16px; border-radius: 50%; background: #fff; transition: .2s; }
.switch input:checked + .slider { background: var(--ai); }
.switch input:checked + .slider::after { inset-inline-start: 21px; }

/* Pages */
.page { overflow-y: auto; }
.page-inner { max-width: 860px; margin: 0 auto; padding: 24px 16px 60px; }
.card { background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); padding: 1.1rem 1.2rem; margin-bottom: 1rem; box-shadow: var(--shadow); }
.hint { font-size: .82rem; color: var(--muted); margin-top: -.5rem; }
.hint input[type=file] { width: auto; background: none; border: 0; padding: .3em 0; }
.template-preview { background: var(--panel-2); border: 1px dashed var(--line); border-radius: 10px; padding: .7rem .9rem; margin-bottom: .9rem; white-space: pre-wrap; font-size: .9rem; }
.campaign-list { display: flex; flex-direction: column; gap: .7rem; }
.campaign { background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); padding: .9rem 1rem; }
.campaign-head { display: flex; justify-content: space-between; align-items: center; gap: .5rem; flex-wrap: wrap; }
.progress { height: 8px; background: var(--panel-2); border-radius: 4px; overflow: hidden; margin: .6rem 0 .4rem; display: flex; }
.progress span { display: block; height: 100%; }
.progress .p-read { background: #34b7f1; }
.progress .p-deliv { background: var(--brand); }
.progress .p-sent { background: #9fd8c8; }
.progress .p-fail { background: var(--danger); }
.stats { display: flex; gap: 1rem; flex-wrap: wrap; font-size: .82rem; color: var(--muted); }
.stats b { color: var(--text); }
.status-pill { font-size: .72rem; font-weight: 700; padding: 2px 8px; border-radius: 10px; background: var(--panel-2); }
.status-pill.running { background: var(--brand-soft); color: var(--brand); }
.status-pill.stopped { background: var(--warn-bg); color: var(--warn-ink); }
.ai-test { margin-top: .8rem; background: var(--ai-soft); border-radius: 10px; padding: .8rem 1rem; white-space: pre-wrap; }
.code { background: var(--panel-2); border: 1px solid var(--line); border-radius: 10px; padding: .8rem; overflow-x: auto; font-size: .8rem; text-align: left; }
.kv { display: grid; grid-template-columns: max-content 1fr; gap: .4rem 1rem; margin: 0; font-size: .9rem; }
.kv dt { color: var(--muted); }
.kv dd { margin: 0; }

.modal { border: 1px solid var(--line); border-radius: 16px; padding: 1.3rem; width: min(480px, calc(100vw - 32px)); background: var(--panel); color: var(--text); }
.modal::backdrop { background: rgba(0,0,0,.4); }

.toast {
  position: fixed; bottom: 20px; left: 50%; transform: translateX(-50%) translateY(120%);
  background: var(--text); color: var(--bg); padding: .65rem 1.1rem; border-radius: 10px; z-index: 50;
  opacity: 0; transition: transform .25s, opacity .25s; max-width: calc(100vw - 32px); font-size: .9rem; pointer-events: none;
}
.toast.show { transform: translateX(-50%) translateY(0); opacity: 1; }
.toast.err { background: var(--danger); color: #fff; }

/* Mobile */
@media (max-width: 760px) {
  .app { flex-direction: column-reverse; }
  .rail { width: 100%; height: 62px; flex-direction: row; justify-content: space-around; padding: 4px; border: 0; border-top: 1px solid var(--line); }
  .rail .brand-mark, .rail-spacer, .live-dot { display: none; }
  .rail-btn { width: auto; flex: 1; }
  .view { min-height: 0; flex: 1; }
  .chat-list { width: 100%; border: 0; }
  .chat { display: none; }
  .inbox.show-chat .chat-list { display: none; }
  .inbox.show-chat .chat { display: flex; }
  .back { display: inline-block; }
  .switch-label { display: none; }
  .grid2 { grid-template-columns: 1fr; }
  .messages { padding: 12px; }
  .bubble { max-width: 85%; }
}

/* CRM stage tags in the inbox */
.tag.stage-interested { background: #e7f0ff; color: #2456b8; }
.tag.stage-ordered { background: var(--warn-bg); color: var(--warn-ink); }
.tag.stage-customer { background: var(--brand-soft); color: var(--brand); }
.tag.stage-lost { background: #f4e5e4; color: var(--danger); }
