/* Al-Mashriq CRM — tokens from the brand design system */
:root {
  --surface: #FFFFFF;
  --surface-subtle: #F6F9FA;
  --surface-tint: #EEFAFB;
  --border: #E2EAED;
  --ink: #0F2A33;
  --ink-muted: #566770;
  --ink-on-brand: #FFFFFF;
  --brand-50: #F0FBFC;
  --brand-100: #D6F4F6;
  --brand-300: #7FDDE3;
  --brand-500: #1FB7C2;
  --brand-600: #0B7A85;
  --brand-700: #08626C;
  --brand-900: #08414A;
  --accent: #F59E0B;
  --success: #15803D;
  --warning: #B45309;
  --danger: #C2410C;
  --chart-1: #0B7A85;
  --chart-2: #1E3A5F;
  --chart-3: #F59E0B;
  --chart-4: #7FDDE3;
  --chart-5: #C2410C;
  --chart-6: #94A3B8;
  --channel-whatsapp: #25D366;
  --channel-instagram: #E1306C;
  --channel-messenger: #0084FF;
  --radius-sm: 6px;
  --radius-md: 10px;
  --radius-lg: 16px;
  --shadow-pop: 0 8px 24px rgba(15, 42, 51, 0.10);
  --font: "IBM Plex Sans Arabic", "Segoe UI", Tahoma, system-ui, sans-serif;
  --mono: "IBM Plex Mono", ui-monospace, monospace;
}

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body { font: 400 15px/24px var(--font); color: var(--ink); background: var(--surface-subtle); -webkit-font-smoothing: antialiased; }
a { color: var(--brand-600); text-decoration: none; }
h1 { font-size: 24px; line-height: 32px; margin: 0; font-weight: 700; }
h2 { font-size: 18px; line-height: 28px; margin: 0 0 12px; font-weight: 600; }
h3 { font-size: 13px; line-height: 20px; margin: 0 0 8px; font-weight: 600; color: var(--ink-muted); }
code, .mono { font-family: var(--mono); font-size: 13px; }
code { background: var(--surface-subtle); padding: 1px 6px; border-radius: var(--radius-sm); }
.muted { color: var(--ink-muted); }
.small { font-size: 13px; }
svg:not(.chart) { width: 20px; height: 20px; fill: none; stroke: currentColor; stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round; flex: none; }
table, .kpi b, .badge, time, .count { font-variant-numeric: tabular-nums; }

/* ---------- Controls ---------- */
input, select, textarea {
  font: inherit; color: var(--ink); background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius-sm);
  padding: 8px 12px; width: 100%; min-height: 40px;
}
input:focus, select:focus, textarea:focus, button:focus-visible, a:focus-visible { outline: 2px solid var(--brand-300); outline-offset: 1px; border-color: var(--brand-600); }
textarea { resize: vertical; }
label { display: flex; flex-direction: column; gap: 4px; font-size: 13px; font-weight: 500; color: var(--ink-muted); }
label.check { flex-direction: row; align-items: center; gap: 8px; color: var(--ink); }
label.check input { width: auto; min-height: 0; }
.btn, .btn-ghost {
  font: 600 14px/20px var(--font); border-radius: var(--radius-md); padding: 10px 18px; cursor: pointer; display: inline-flex; align-items: center; gap: 8px; justify-content: center;
  border: 1px solid transparent; transition: background .15s, border-color .15s;
}
.btn { background: var(--brand-600); color: var(--ink-on-brand); }
.btn:hover { background: var(--brand-700); }
.btn:disabled { opacity: .6; cursor: default; }
.btn-ghost { background: var(--surface); color: var(--ink); border-color: var(--border); }
.btn-ghost:hover { border-color: var(--brand-300); background: var(--surface-tint); }
.btn-ghost.sm { padding: 4px 10px; font-size: 13px; }
.icon-btn { display: inline-flex; align-items: center; justify-content: center; width: 36px; height: 36px; border-radius: var(--radius-md); color: var(--ink); }
.link { color: var(--brand-600); font-weight: 500; }
.row { display: flex; gap: 8px; }
.stack { display: flex; flex-direction: column; gap: 14px; }

.tag { display: inline-block; font-size: 12px; line-height: 16px; font-weight: 600; padding: 2px 8px; border-radius: 999px; white-space: nowrap; }
.tag-brand { background: var(--brand-100); color: var(--brand-700); }
.tag-danger { background: #FDECE4; color: var(--danger); }
.tag-success { background: #E3F4E8; color: var(--success); }
.tag-muted { background: var(--surface-subtle); color: var(--ink-muted); border: 1px solid var(--border); }
.tag-navy { background: #E6ECF3; color: var(--chart-2); }
.tag-accent { background: #FEF1D7; color: #8A5A00; }
.badge { background: var(--brand-600); color: #fff; font-size: 12px; font-weight: 700; border-radius: 999px; padding: 0 7px; line-height: 20px; min-width: 20px; text-align: center; }

/* ---------- Toast ---------- */
#toast { position: fixed; bottom: 24px; left: 50%; transform: translate(-50%, 20px); opacity: 0; pointer-events: none; z-index: 50;
  background: var(--ink); color: #fff; padding: 10px 18px; border-radius: var(--radius-md); box-shadow: var(--shadow-pop); transition: .2s; font-weight: 500; max-width: 90vw; }
#toast.show { opacity: 1; transform: translate(-50%, 0); }
#toast.error { background: var(--danger); }
#toast.success { background: var(--success); }

/* ---------- Boot & auth ---------- */
.boot { height: 100vh; display: grid; place-items: center; }
.boot img { animation: pulse 1.2s ease-in-out infinite; }
@keyframes pulse { 50% { opacity: .5; transform: scale(.96); } }
.auth-view { min-height: 100vh; display: grid; place-items: center; padding: 24px; background: linear-gradient(180deg, var(--brand-50), var(--surface) 60%); }
.auth-card { width: 100%; max-width: 400px; background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius-lg); padding: 32px; display: flex; flex-direction: column; gap: 16px; }
.auth-logo { width: 100%; max-width: 300px; align-self: center; }

/* ---------- Layout ---------- */
.layout { display: grid; grid-template-columns: 232px 1fr; height: 100vh; }
.sidebar { background: var(--surface); border-inline-end: 1px solid var(--border); display: flex; flex-direction: column; padding: 16px 12px; gap: 16px; }
.brand { display: flex; align-items: center; gap: 10px; color: var(--ink); padding: 4px 8px; direction: ltr; justify-content: flex-end; }
.brand b { font-weight: 700; }
.brand em { font-style: normal; color: var(--brand-600); font-weight: 700; }
.sidebar nav { display: flex; flex-direction: column; gap: 2px; flex: 1; }
.sidebar nav a { display: flex; align-items: center; gap: 10px; padding: 10px 12px; border-radius: var(--radius-md); color: var(--ink-muted); font-weight: 500; }
.sidebar nav a:hover { background: var(--surface-subtle); color: var(--ink); }
.sidebar nav a.active { background: var(--brand-100); color: var(--brand-700); font-weight: 600; }
.me { border-top: 1px solid var(--border); padding: 12px 8px 0; display: flex; flex-direction: column; gap: 6px; }
.me-name { font-weight: 600; }
.me-sub { font-size: 13px; color: var(--ink-muted); }
main#view { overflow: auto; min-width: 0; }
.bottomnav { display: none; }

.page { padding: 24px 32px 48px; max-width: 1280px; display: flex; flex-direction: column; gap: 20px; }
.page-head { display: flex; justify-content: space-between; align-items: flex-end; gap: 16px; flex-wrap: wrap; }
.panel { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius-lg); padding: 20px 24px; }
.empty-state { display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 12px; color: var(--ink-muted); padding: 48px 16px; height: 100%; text-align: center; }
.form-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 14px 16px; }
.span2 { grid-column: 1 / -1; }
.kv { display: flex; justify-content: space-between; gap: 12px; padding: 6px 0; border-bottom: 1px dashed var(--border); font-size: 14px; }
.kv:last-child { border-bottom: 0; }
.kv > span:first-child { color: var(--ink-muted); }

/* ---------- Tables ---------- */
.table { width: 100%; border-collapse: collapse; font-size: 14px; }
.table th { text-align: start; font-size: 12px; font-weight: 600; color: var(--ink-muted); background: var(--surface-subtle); padding: 8px 12px; border-bottom: 1px solid var(--border); }
.table td { padding: 10px 12px; border-bottom: 1px solid var(--border); vertical-align: middle; }
.table tr:last-child td { border-bottom: 0; }
.table .actions { display: flex; gap: 6px; justify-content: flex-end; }

/* ---------- KPIs & charts ---------- */
.kpis { display: grid; grid-template-columns: repeat(auto-fit, minmax(170px, 1fr)); gap: 12px; }
.kpi { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius-lg); padding: 16px 20px; display: flex; flex-direction: column; gap: 4px; }
.kpi span { font-size: 12px; font-weight: 600; color: var(--ink-muted); }
.kpi b { font-size: 30px; line-height: 36px; font-weight: 700; }
.kpi b.txt { font-size: 20px; line-height: 28px; }
.kpi small { color: var(--ink-muted); font-size: 12px; }
.kpi.hot { border-color: var(--accent); box-shadow: inset 0 -4px 0 var(--accent); }
.chart { width: 100%; height: auto; }
.chart .grid { stroke: var(--border); stroke-width: 1; }
.chart .axis { font: 11px var(--font); fill: var(--ink-muted); stroke: none; }
.grid2 { display: grid; grid-template-columns: 1fr 1fr; gap: 20px; }
.hbars { display: flex; flex-direction: column; gap: 10px; }
.hbar { display: grid; grid-template-columns: 110px 1fr 48px; align-items: center; gap: 10px; font-size: 14px; }
.hb-track { height: 12px; background: var(--surface-subtle); border-radius: 999px; overflow: hidden; }
.hb-fill { display: block; height: 100%; border-radius: 999px; min-width: 2px; }
.hbar b { text-align: end; }
.seg { display: inline-flex; border: 1px solid var(--border); border-radius: var(--radius-md); overflow: hidden; background: var(--surface); }
.seg button { font: 500 14px var(--font); border: 0; background: none; padding: 8px 14px; cursor: pointer; color: var(--ink-muted); }
.seg button.on { background: var(--brand-600); color: #fff; }

/* ---------- Inbox ---------- */
.inbox { display: grid; grid-template-columns: 360px 1fr 300px; height: 100vh; background: var(--surface); }
.list-pane { border-inline-end: 1px solid var(--border); display: flex; flex-direction: column; min-height: 0; }
.list-head { display: flex; gap: 8px; padding: 12px; border-bottom: 1px solid var(--border); }
.list-head select { width: 120px; }
.search { position: relative; flex: 1; }
.search svg { position: absolute; inset-inline-start: 10px; top: 10px; color: var(--ink-muted); }
.search input { padding-inline-start: 36px; }
.filters { display: flex; gap: 6px; padding: 10px 12px; overflow-x: auto; border-bottom: 1px solid var(--border); scrollbar-width: none; }
.chip { font: 500 13px var(--font); white-space: nowrap; border: 1px solid var(--border); background: var(--surface); color: var(--ink-muted); border-radius: 999px; padding: 4px 12px; cursor: pointer; }
.chip b { font-weight: 700; color: var(--ink); margin-inline-start: 2px; }
.chip.on { background: var(--brand-600); border-color: var(--brand-600); color: #fff; }
.chip.on b { color: #fff; }
.chip.alert:not(.on) b { color: var(--danger); }
.conv-list { list-style: none; margin: 0; padding: 0; overflow-y: auto; flex: 1; }
.conv-list .empty { padding: 32px 16px; text-align: center; color: var(--ink-muted); }
.conv-item { display: flex; gap: 12px; padding: 12px 14px; color: var(--ink); border-bottom: 1px solid var(--surface-subtle); }
.conv-item:hover { background: var(--surface-subtle); }
.conv-item.active { background: var(--surface-tint); box-shadow: inset -3px 0 0 var(--brand-600); }
.conv-item.unread .ci-name, .conv-item.unread .ci-preview { font-weight: 600; color: var(--ink); }
.avatar { position: relative; width: 44px; height: 44px; border-radius: 999px; background: var(--brand-100); color: var(--brand-700); display: grid; place-items: center; font-weight: 700; font-size: 17px; flex: none; }
.avatar.sm { width: 32px; height: 32px; font-size: 14px; }
.pdot { position: absolute; bottom: -1px; inset-inline-end: -1px; width: 14px; height: 14px; border-radius: 999px; border: 2px solid var(--surface); }
.ci-body { flex: 1; min-width: 0; display: flex; flex-direction: column; gap: 2px; }
.ci-top { display: flex; justify-content: space-between; gap: 8px; }
.ci-name { font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.ci-top time { font-size: 12px; color: var(--ink-muted); flex: none; }
.ci-preview { font-size: 13px; color: var(--ink-muted); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.ci-preview .you { color: var(--brand-600); }
.ci-tags { display: flex; gap: 4px; flex-wrap: wrap; align-items: center; min-height: 4px; }
.ci-tags .badge { margin-inline-start: auto; }

.conv-pane { display: flex; flex-direction: column; min-height: 0; min-width: 0; background: var(--surface-subtle); }
.conv-head { display: flex; align-items: center; gap: 12px; padding: 10px 16px; background: var(--surface); border-bottom: 1px solid var(--border); }
.conv-head .back { display: none; }
.conv-head select { width: 150px; min-height: 36px; padding: 4px 8px; }
.ch-title { flex: 1; min-width: 0; }
.ch-name { font-weight: 700; }
.ch-sub { font-size: 13px; color: var(--ink-muted); }
.messages { flex: 1; overflow-y: auto; padding: 16px 20px; display: flex; flex-direction: column; gap: 8px; }
.msg { display: flex; }
.msg.out { justify-content: flex-end; }
.bubble { max-width: min(560px, 78%); background: var(--surface); border: 1px solid var(--border); border-radius: 14px; border-start-start-radius: 4px; padding: 8px 12px; }
.msg.out .bubble { background: var(--brand-100); border-color: transparent; border-start-start-radius: 14px; border-start-end-radius: 4px; }
.bubble .text { white-space: pre-wrap; overflow-wrap: anywhere; }
.bubble .meta { display: flex; gap: 6px; justify-content: flex-end; align-items: center; font-size: 11px; color: var(--ink-muted); margin-top: 2px; }
.bubble .err { font-size: 12px; color: var(--danger); margin-top: 4px; }
.st { font-weight: 700; letter-spacing: -2px; }
.st.read { color: var(--brand-600); }
.st.failed { color: var(--danger); letter-spacing: 0; }
.media img, .media video { max-width: 100%; max-height: 320px; border-radius: var(--radius-md); display: block; }
.media audio { width: 260px; max-width: 100%; }
.composer { background: var(--surface); border-top: 1px solid var(--border); padding: 8px 12px 12px; display: flex; flex-direction: column; gap: 6px; }
.window { font-size: 12px; color: var(--ink-muted); }
.window.warn { color: var(--warning); font-weight: 600; }
.reply { display: flex; gap: 8px; align-items: flex-end; }
.reply textarea { resize: none; min-height: 44px; max-height: 160px; }
.reply .send { width: 44px; height: 44px; padding: 0; flex: none; }
.reply .send svg { transform: scaleX(-1); }
.tpl-form { display: flex; gap: 8px; }
.tpl-form .lang { width: 70px; }

.contact-pane { border-inline-start: 1px solid var(--border); overflow-y: auto; padding: 12px; display: flex; flex-direction: column; gap: 12px; background: var(--surface); }
.contact-pane:empty { display: none; }
.contact-pane .panel { padding: 14px 16px; border-radius: var(--radius-md); display: flex; flex-direction: column; gap: 10px; }
.contact-pane .panel.ad { border-color: #FAD9A0; background: #FFFBF2; }
.tags-row { display: flex; gap: 4px; flex-wrap: wrap; }
.inbox:not(.has-conv) { grid-template-columns: 360px 1fr; }
.inbox:not(.has-conv) .contact-pane { display: none; }

/* ---------- Pipeline ---------- */
.board { display: grid; grid-auto-flow: column; grid-auto-columns: minmax(200px, 1fr); gap: 12px; overflow-x: auto; padding-bottom: 8px; }
.column { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius-lg); padding: 12px; display: flex; flex-direction: column; gap: 10px; min-height: 300px; }
.column h2 { font-size: 15px; margin: 0; display: flex; justify-content: space-between; }
.column .count { background: var(--surface-subtle); border-radius: 999px; padding: 0 8px; font-size: 13px; }
.column.kind-won { border-top: 3px solid var(--success); }
.column.kind-lost { border-top: 3px solid var(--chart-6); }
.column.kind-open { border-top: 3px solid var(--brand-500); }
.cards { display: flex; flex-direction: column; gap: 8px; }
.card { border: 1px solid var(--border); border-radius: var(--radius-md); padding: 10px; display: flex; flex-direction: column; gap: 8px; background: var(--surface); }
.card-top { display: flex; align-items: center; gap: 8px; }
.card-name { flex: 1; font-weight: 600; color: var(--ink); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.card time { font-size: 12px; color: var(--ink-muted); }
.card select { min-height: 32px; padding: 2px 8px; font-size: 13px; }
.stage-row { display: grid; grid-template-columns: 1fr 140px; gap: 8px; margin-bottom: 8px; }

/* ---------- Mobile ---------- */
@media (max-width: 1100px) {
  .inbox { grid-template-columns: 320px 1fr; }
  .inbox .contact-pane { display: none; }
}
@media (max-width: 860px) {
  .layout { grid-template-columns: 1fr; height: 100dvh; }
  .sidebar { display: none; }
  main#view { padding-bottom: 64px; }
  .bottomnav { display: flex; position: fixed; bottom: 0; inset-inline: 0; height: calc(60px + env(safe-area-inset-bottom)); padding-bottom: env(safe-area-inset-bottom);
    background: var(--surface); border-top: 1px solid var(--border); z-index: 20; }
  .bottomnav a { flex: 1; display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 2px; font-size: 11px; color: var(--ink-muted); }
  .bottomnav a.active { color: var(--brand-600); font-weight: 600; }
  .inbox, .inbox:not(.has-conv) { grid-template-columns: 1fr; height: calc(100dvh - 60px); }
  .inbox.has-conv .list-pane { display: none; }
  .inbox:not(.has-conv) .conv-pane { display: none; }
  .conv-head .back { display: inline-flex; }
  .conv-head select { width: 110px; }
  .page { padding: 16px; }
  .form-grid, .grid2 { grid-template-columns: 1fr; }
  .table { display: block; overflow-x: auto; white-space: nowrap; }
  .bubble { max-width: 88%; }
}
