:root {
  --bg: #f4f6fb; --panel: #ffffff; --panel2: #f8f9fc; --line: #e4e8f0; --text: #172033; --muted: #6b7690;
  --brand: #4f46e5; --brand-d: #4338ca; --brand-soft: #eef0ff; --ok: #16a34a; --warn: #d97706; --danger: #dc2626;
  --bubble-out: #e0e7ff; --bubble-in: #ffffff; --shadow: 0 1px 2px rgba(16,24,40,.06), 0 4px 14px rgba(16,24,40,.06);
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0e1320; --panel: #151b2c; --panel2: #1a2135; --line: #263049; --text: #e6eaf5; --muted: #8e9ab8;
    --brand: #6366f1; --brand-d: #818cf8; --brand-soft: #1f2547; --bubble-out: #2a3170; --bubble-in: #1d2538;
    --shadow: 0 1px 2px rgba(0,0,0,.4); color-scheme: dark;
  }
}
* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body { background: var(--bg); color: var(--text); font: 14.5px/1.6 "Segoe UI", Tahoma, "Noto Sans Arabic", "Helvetica Neue", Arial, sans-serif; -webkit-font-smoothing: antialiased; }
button, input, select, textarea { font: inherit; color: inherit; }
.muted { color: var(--muted); }
.hidden, [hidden] { display: none !important; }

/* ---------- عناصر عامة ---------- */
.btn { border: 1px solid var(--line); background: var(--panel); border-radius: 10px; padding: 7px 14px; cursor: pointer; transition: .15s; }
.btn:hover { border-color: var(--brand); }
.btn:disabled { opacity: .5; cursor: not-allowed; }
.btn.primary { background: var(--brand); border-color: var(--brand); color: #fff; }
.btn.primary:hover { background: var(--brand-d); }
.btn.danger { color: var(--danger); }
.btn.sm { padding: 3px 10px; font-size: 13px; border-radius: 8px; }
.btn.block { width: 100%; padding: 10px; }
input, select, textarea { width: 100%; border: 1px solid var(--line); background: var(--panel); border-radius: 10px; padding: 8px 12px; outline: none; }
input:focus, select:focus, textarea:focus { border-color: var(--brand); box-shadow: 0 0 0 3px var(--brand-soft); }
label { display: block; font-size: 13px; color: var(--muted); margin-bottom: 12px; }
label > input, label > select, label > textarea { margin-top: 4px; color: var(--text); }
.error { background: #fee2e2; color: #991b1b; border-radius: 10px; padding: 8px 12px; margin-bottom: 12px; font-size: 13px; }
.badge { display: inline-block; padding: 1px 9px; border-radius: 999px; font-size: 12px; background: var(--brand-soft); color: var(--brand-d); }
.badge.ok { background: #dcfce7; color: #166534; } .badge.warn { background: #fef3c7; color: #92400e; }
.badge.danger { background: #fee2e2; color: #991b1b; } .badge.gray { background: var(--panel2); color: var(--muted); border: 1px solid var(--line); }
.toast { position: fixed; bottom: 24px; left: 50%; transform: translateX(-50%); background: #111827; color: #fff; padding: 10px 18px; border-radius: 12px; z-index: 99; box-shadow: var(--shadow); max-width: 90vw; }
.toast.err { background: var(--danger); }

/* ---------- الدخول ---------- */
.login-page { display: grid; place-items: center; min-height: 100%; padding: 20px; }
.login-card { width: 100%; max-width: 380px; background: var(--panel); border: 1px solid var(--line); border-radius: 20px; padding: 32px 28px; box-shadow: var(--shadow); }
.login-card h1 { font-size: 20px; margin: 8px 0 2px; text-align: center; } .login-card p { text-align: center; margin: 0 0 22px; }
.logo { width: 56px; height: 56px; margin: 0 auto; border-radius: 16px; display: grid; place-items: center; font-size: 28px; background: var(--brand-soft); }

/* ---------- الهيكل ---------- */
.app { display: grid; grid-template-columns: 76px 1fr; height: 100%; }
.nav { background: var(--panel); border-inline-end: 1px solid var(--line); display: flex; flex-direction: column; align-items: center; padding: 10px 0; gap: 4px; overflow-y: auto; }
.nav .brand { width: 42px; height: 42px; border-radius: 12px; background: var(--brand); color: #fff; display: grid; place-items: center; font-size: 20px; margin-bottom: 8px; }
.nav button { width: 64px; border: 0; background: none; border-radius: 12px; padding: 8px 2px; cursor: pointer; color: var(--muted); font-size: 11.5px; position: relative; }
.nav button .ic { display: block; font-size: 20px; line-height: 1.3; }
.nav button.active { background: var(--brand-soft); color: var(--brand-d); font-weight: 600; }
.nav button:hover:not(.active) { background: var(--panel2); }
.nav .spacer { flex: 1; }
.nav .dot { position: absolute; top: 4px; inset-inline-start: 10px; min-width: 18px; height: 18px; border-radius: 9px; background: var(--danger); color: #fff; font-size: 11px; display: grid; place-items: center; padding: 0 4px; }
.main { min-width: 0; height: 100%; display: flex; flex-direction: column; }
.banner { background: #fef3c7; color: #92400e; padding: 8px 16px; font-size: 13px; text-align: center; }
.page { flex: 1; overflow-y: auto; padding: 22px; }
.page h2 { margin: 0 0 16px; font-size: 20px; }
.card { background: var(--panel); border: 1px solid var(--line); border-radius: 16px; padding: 16px; box-shadow: var(--shadow); }
.grid { display: grid; gap: 14px; }
.kpis { grid-template-columns: repeat(auto-fit, minmax(170px, 1fr)); margin-bottom: 14px; }
.kpi .v { font-size: 26px; font-weight: 700; } .kpi .l { color: var(--muted); font-size: 13px; }
table { width: 100%; border-collapse: collapse; font-size: 13.5px; }
th, td { padding: 9px 10px; text-align: start; border-bottom: 1px solid var(--line); white-space: nowrap; }
th { color: var(--muted); font-weight: 600; background: var(--panel2); }
.tablewrap { overflow-x: auto; border-radius: 12px; border: 1px solid var(--line); }
.toolbar { display: flex; gap: 8px; flex-wrap: wrap; align-items: center; margin-bottom: 14px; }
.toolbar input, .toolbar select { width: auto; }

/* ---------- الصندوق ---------- */
.inbox { flex: 1; display: grid; grid-template-columns: 340px minmax(0, 1fr) 320px; min-height: 0; }
.list { border-inline-end: 1px solid var(--line); background: var(--panel); display: flex; flex-direction: column; min-height: 0; }
.list-head { padding: 12px; border-bottom: 1px solid var(--line); display: grid; gap: 8px; }
.tabs { display: flex; gap: 4px; background: var(--panel2); padding: 3px; border-radius: 10px; }
.tabs button { flex: 1; border: 0; background: none; padding: 5px 4px; border-radius: 8px; cursor: pointer; color: var(--muted); font-size: 13px; }
.tabs button.active { background: var(--panel); color: var(--brand-d); font-weight: 600; box-shadow: var(--shadow); }
.chips { display: flex; gap: 6px; overflow-x: auto; }
.chip { border: 1px solid var(--line); background: var(--panel); border-radius: 999px; padding: 2px 11px; font-size: 12.5px; cursor: pointer; white-space: nowrap; color: var(--muted); }
.chip.active { background: var(--brand); border-color: var(--brand); color: #fff; }
.items { flex: 1; overflow-y: auto; }
.item { display: flex; gap: 10px; padding: 11px 12px; border-bottom: 1px solid var(--line); cursor: pointer; }
.item:hover { background: var(--panel2); } .item.active { background: var(--brand-soft); }
.avatar { flex: none; width: 42px; height: 42px; border-radius: 50%; display: grid; place-items: center; color: #fff; font-weight: 700; }
.item .body { min-width: 0; flex: 1; }
.item .row { display: flex; justify-content: space-between; gap: 8px; align-items: center; }
.item .name { font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.item .time { font-size: 11.5px; color: var(--muted); flex: none; }
.item .prev { color: var(--muted); font-size: 13px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.item .meta { display: flex; gap: 5px; margin-top: 3px; flex-wrap: wrap; }
.unread { background: var(--brand); color: #fff; border-radius: 999px; min-width: 20px; height: 20px; font-size: 12px; display: grid; place-items: center; padding: 0 6px; }
.empty { padding: 40px 20px; text-align: center; color: var(--muted); }

.chat { display: flex; flex-direction: column; min-width: 0; min-height: 0; background: var(--bg); }
.chat-head { background: var(--panel); border-bottom: 1px solid var(--line); padding: 10px 14px; display: flex; align-items: center; gap: 10px; }
.chat-head .t { font-weight: 700; } .chat-head .s { font-size: 12.5px; color: var(--muted); }
.chat-head .back { display: none; }
.msgs { flex: 1; overflow-y: auto; padding: 16px; display: flex; flex-direction: column; gap: 6px; }
.day { align-self: center; background: var(--panel); border: 1px solid var(--line); border-radius: 999px; padding: 1px 12px; font-size: 12px; color: var(--muted); margin: 8px 0; }
.bubble { max-width: min(560px, 82%); padding: 7px 11px; border-radius: 14px; box-shadow: var(--shadow); word-wrap: break-word; overflow-wrap: anywhere; white-space: pre-wrap; }
.bubble.in { align-self: flex-end; background: var(--bubble-in); border: 1px solid var(--line); border-end-end-radius: 4px; }
.bubble.out { align-self: flex-start; background: var(--bubble-out); border-end-start-radius: 4px; }
.bubble .by { font-size: 11.5px; color: var(--brand-d); font-weight: 600; }
.bubble .ft { font-size: 11px; color: var(--muted); margin-top: 2px; display: flex; gap: 6px; justify-content: flex-end; }
.bubble img { max-width: 100%; max-height: 320px; border-radius: 10px; display: block; cursor: zoom-in; margin-bottom: 4px; }
.bubble video { max-width: 100%; max-height: 320px; border-radius: 10px; }
.bubble audio { max-width: 100%; }
.bubble a.file { display: flex; gap: 8px; align-items: center; padding: 6px 10px; background: var(--panel2); border-radius: 10px; color: var(--text); text-decoration: none; }
.sysmsg { align-self: center; font-size: 12px; color: var(--muted); }
.composer { background: var(--panel); border-top: 1px solid var(--line); padding: 10px 12px; display: flex; gap: 8px; align-items: flex-end; position: relative; }
.composer textarea { resize: none; max-height: 140px; min-height: 40px; }
.composer .iconbtn { width: 40px; height: 40px; padding: 0; flex: none; font-size: 18px; }
.qpop { position: absolute; bottom: 62px; inset-inline-start: 12px; width: min(420px, calc(100% - 24px)); max-height: 260px; overflow-y: auto; background: var(--panel); border: 1px solid var(--line); border-radius: 14px; box-shadow: var(--shadow); z-index: 5; }
.qpop div { padding: 9px 12px; border-bottom: 1px solid var(--line); cursor: pointer; }
.qpop div:hover { background: var(--panel2); } .qpop b { display: block; font-size: 13px; } .qpop span { color: var(--muted); font-size: 12.5px; }

.details { border-inline-start: 1px solid var(--line); background: var(--panel); overflow-y: auto; padding: 14px; display: flex; flex-direction: column; gap: 14px; }
.details h4 { margin: 0 0 6px; font-size: 13px; color: var(--muted); font-weight: 600; }
.seg { display: flex; gap: 4px; flex-wrap: wrap; }
.note { background: #fffbeb; border: 1px solid #fde68a; color: #78350f; border-radius: 10px; padding: 7px 10px; margin-bottom: 6px; font-size: 13px; white-space: pre-wrap; }
@media (prefers-color-scheme: dark) { .note { background: #2d2610; border-color: #5b4b14; color: #fcd34d; } }
.note small { display: block; color: inherit; opacity: .7; font-size: 11.5px; }
.timeline div { font-size: 12.5px; color: var(--muted); padding: 2px 0; }
.tagpill { display: inline-block; border-radius: 999px; padding: 0 9px; font-size: 12px; color: #fff; }
.tagpick { cursor: pointer; opacity: .45; border: 2px solid transparent; } .tagpick.on { opacity: 1; }
.placeholder { flex: 1; display: grid; place-items: center; color: var(--muted); text-align: center; }

/* ---------- المخططات ---------- */
.bars { display: flex; align-items: flex-end; gap: 3px; height: 150px; padding-top: 10px; }
.bars .col { flex: 1; min-width: 4px; display: flex; flex-direction: column; justify-content: flex-end; align-items: center; height: 100%; gap: 2px; }
.bars .b { width: 100%; background: var(--brand); border-radius: 4px 4px 0 0; min-height: 2px; }
.bars .b.b2 { background: #14b8a6; }
.axis { display: flex; justify-content: space-between; font-size: 11px; color: var(--muted); margin-top: 4px; }
.legend { display: flex; gap: 14px; font-size: 12.5px; color: var(--muted); margin-bottom: 4px; }
.legend i { display: inline-block; width: 10px; height: 10px; border-radius: 3px; margin-inline-end: 5px; }
.switch { display: flex; justify-content: space-between; align-items: center; padding: 12px 0; border-bottom: 1px solid var(--line); gap: 12px; }
.switch small { display: block; color: var(--muted); }
.switch input { width: 44px; height: 24px; accent-color: var(--brand); flex: none; }
.qr { display: grid; place-items: center; padding: 20px; } .qr img { width: 260px; max-width: 100%; border-radius: 12px; background: #fff; padding: 8px; }
.modal { position: fixed; inset: 0; background: rgba(0,0,0,.45); display: grid; place-items: center; z-index: 50; padding: 16px; }
.modal .box { background: var(--panel); border-radius: 16px; padding: 20px; width: min(440px, 100%); max-height: 90vh; overflow-y: auto; }
.modal h3 { margin: 0 0 14px; }

/* ---------- الجوال ---------- */
@media (max-width: 1100px) { .inbox { grid-template-columns: 320px minmax(0, 1fr); } .details { display: none; } .inbox.show-details .details { display: flex; position: fixed; inset: 0; z-index: 30; border: 0; } }
@media (max-width: 760px) {
  .app { grid-template-columns: 1fr; grid-template-rows: 1fr 62px; }
  .nav { flex-direction: row; order: 2; border-inline-end: 0; border-top: 1px solid var(--line); justify-content: space-around; padding: 4px; overflow-x: auto; gap: 0; }
  .nav .brand, .nav .spacer { display: none; } .nav button { width: auto; min-width: 58px; flex: 1; padding: 4px 2px; }
  .inbox { grid-template-columns: 1fr; } .inbox .chat { display: none; } .inbox.has-chat .list { display: none; } .inbox.has-chat .chat { display: flex; }
  .chat-head .back { display: inline-block; } .page { padding: 14px; } .bubble { max-width: 90%; }
}

/* ---------- إضافات: الاقتباس، السجل، المزيد ---------- */
.bubble { position: relative; }
.bubble .replybtn { position: absolute; top: 2px; inset-inline-end: 4px; border: 0; background: var(--panel); color: var(--muted); border-radius: 8px; width: 24px; height: 24px; cursor: pointer; opacity: 0; transition: .15s; box-shadow: var(--shadow); }
.bubble:hover .replybtn, .bubble .replybtn:focus { opacity: 1; }
@media (hover: none) { .bubble .replybtn { opacity: .6; } }
.quote { border-inline-start: 3px solid var(--brand); background: rgba(99,102,241,.1); border-radius: 8px; padding: 3px 8px; margin-bottom: 4px; font-size: 12.5px; display: grid; }
.quote b { color: var(--brand-d); font-size: 11.5px; } .quote span { color: var(--muted); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; max-width: 320px; }
.composer-wrap { background: var(--panel); border-top: 1px solid var(--line); }
.composer-wrap .composer { border-top: 0; }
.replybar { display: flex; justify-content: space-between; align-items: center; gap: 8px; padding: 6px 14px; background: var(--brand-soft); font-size: 13px; }
.older { align-self: center; }
.hist { padding: 7px 9px; border: 1px solid var(--line); border-radius: 10px; margin-bottom: 6px; cursor: pointer; display: grid; font-size: 13px; }
.hist:hover, .hist.sel { border-color: var(--brand); background: var(--brand-soft); } .hist span { color: var(--muted); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.chip b { margin-inline-start: 4px; font-weight: 600; }
