:root {
  --brand: #0f766e;
  --brand-dark: #115e59;
  --brand-soft: #ccfbf1;
  --bg: #f1f5f9;
  --panel: #ffffff;
  --line: #e2e8f0;
  --text: #0f172a;
  --muted: #64748b;
  --in: #ffffff;
  --out: #d9fdd3;
  --note: #fef9c3;
  --danger: #dc2626;
  --ok: #16a34a;
  --chat-bg: #efeae2;
  --radius: 10px;
  font-family: "Segoe UI", Tahoma, "Noto Sans Arabic", Arial, sans-serif;
}

* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body { background: var(--bg); color: var(--text); font-size: 14px; }
.hidden { display: none !important; }
.muted { color: var(--muted); }
.small { font-size: 12px; }
.err { color: var(--danger); min-height: 1em; margin: 4px 0 0; }
.ok { color: var(--ok); min-height: 1em; margin: 4px 0 0; }
.spacer { flex: 1; }

button, select, input, textarea { font: inherit; color: inherit; }
input, select, textarea {
  border: 1px solid var(--line); border-radius: 8px; padding: 8px 10px; background: #fff; width: 100%;
}
input:focus, select:focus, textarea:focus { outline: 2px solid var(--brand-soft); border-color: var(--brand); }
button { cursor: pointer; border-radius: 8px; border: 1px solid transparent; padding: 8px 14px; background: #e2e8f0; }
button:disabled { opacity: .5; cursor: not-allowed; }
button.primary { background: var(--brand); color: #fff; }
button.primary:hover:not(:disabled) { background: var(--brand-dark); }
.ghost { background: transparent; border: 1px solid var(--line); padding: 6px 10px; border-radius: 8px; cursor: pointer; }
.ghost.light { color: #fff; border-color: rgba(255,255,255,.35); }
label { display: block; font-size: 13px; color: var(--muted); }
label > input, label > select, label > textarea { margin-top: 4px; color: var(--text); }
.chk { display: inline-flex; align-items: center; gap: 6px; color: var(--text); }
.chk input { width: auto; margin: 0; }

.logo { width: 52px; height: 52px; border-radius: 14px; background: var(--brand); color: #fff; display: grid; place-items: center; font-weight: 700; font-size: 20px; margin: 0 auto 8px; }
.logo.sm { width: 28px; height: 28px; font-size: 12px; border-radius: 8px; margin: 0; display: inline-grid; background: rgba(255,255,255,.18); }

/* auth */
.auth { min-height: 100%; display: grid; place-items: center; padding: 16px; }
.card { background: var(--panel); border-radius: 14px; box-shadow: 0 10px 30px rgba(15,23,42,.08); padding: 24px; }
.auth-card { width: 100%; max-width: 360px; display: flex; flex-direction: column; gap: 12px; text-align: center; }
.auth-card h1 { margin: 0; font-size: 22px; }
.auth-card p { margin: 0; }
.auth-card label { text-align: right; }

/* app shell */
#app { height: 100%; display: flex; flex-direction: column; }
.topbar { display: flex; align-items: center; gap: 8px; padding: 8px 16px; background: var(--brand); color: #fff; }
.brand { display: flex; align-items: center; gap: 8px; font-weight: 700; font-size: 16px; }
.me { opacity: .9; font-size: 13px; }
.layout { flex: 1; display: flex; min-height: 0; }

.sidebar { width: 340px; max-width: 100%; background: var(--panel); border-inline-end: 1px solid var(--line); display: flex; flex-direction: column; min-height: 0; }
.filters { padding: 10px; display: flex; flex-direction: column; gap: 8px; border-bottom: 1px solid var(--line); }
.row { display: flex; gap: 8px; }
.row > * { flex: 1; }
.row.tight { justify-content: space-between; }
.row.tight > * { flex: 0 0 auto; }
.conv-list { list-style: none; margin: 0; padding: 0; overflow-y: auto; flex: 1; }
.conv { display: flex; gap: 10px; padding: 10px 12px; border-bottom: 1px solid var(--line); cursor: pointer; }
.conv:hover { background: #f8fafc; }
.conv.active { background: var(--brand-soft); }
.conv .avatar { flex: 0 0 auto; }
.conv-main { flex: 1; min-width: 0; }
.conv-top { display: flex; gap: 6px; align-items: baseline; }
.conv-name { font-weight: 600; flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.conv-time { color: var(--muted); font-size: 11px; white-space: nowrap; }
.conv-preview { color: var(--muted); font-size: 13px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.conv-tags { display: flex; flex-wrap: wrap; gap: 4px; margin-top: 4px; }
.tag { font-size: 11px; padding: 1px 6px; border-radius: 999px; background: #e2e8f0; color: #334155; white-space: nowrap; }
.tag.line { background: #e0f2fe; color: #075985; }
.tag.branch { background: #fef3c7; color: #92400e; }
.tag.new { background: #dcfce7; color: #166534; }
.tag.closed { background: #f1f5f9; color: #64748b; }
.badge { background: #22c55e; color: #fff; border-radius: 999px; font-size: 11px; min-width: 20px; padding: 1px 6px; text-align: center; }
.avatar { width: 40px; height: 40px; border-radius: 50%; background: #cbd5e1; color: #fff; display: grid; place-items: center; font-weight: 700; }
.list-empty { padding: 24px; text-align: center; color: var(--muted); }

/* chat */
.chat { flex: 1; display: flex; flex-direction: column; min-width: 0; background: var(--chat-bg); }
.empty { margin: auto; color: var(--muted); font-size: 15px; }
.chat-body { flex: 1; display: flex; flex-direction: column; min-height: 0; }
.chat-head { display: flex; align-items: center; gap: 10px; padding: 8px 12px; background: var(--panel); border-bottom: 1px solid var(--line); }
.chat-head select { width: auto; }
.ch-name { font-weight: 700; }
.back { display: none; }
.window-bar { padding: 6px 12px; font-size: 13px; text-align: center; }
.window-bar.open { background: #ecfdf5; color: #065f46; }
.window-bar.closed { background: #fef2f2; color: #991b1b; }
.messages { flex: 1; overflow-y: auto; padding: 12px 16px; display: flex; flex-direction: column; gap: 6px; }
.msg { max-width: min(70%, 560px); padding: 6px 10px 4px; border-radius: 10px; box-shadow: 0 1px 1px rgba(0,0,0,.06); white-space: pre-wrap; word-wrap: break-word; overflow-wrap: anywhere; }
.msg.in { background: var(--in); align-self: flex-start; }
.msg.out { background: var(--out); align-self: flex-end; }
.msg.note { background: var(--note); align-self: center; border: 1px dashed #eab308; max-width: 80%; }
.msg.event { background: transparent; box-shadow: none; align-self: center; color: var(--muted); font-size: 12px; }
.msg .meta { font-size: 11px; color: var(--muted); margin-top: 2px; display: flex; gap: 6px; justify-content: flex-end; }
.msg .who { font-size: 11px; color: var(--brand-dark); font-weight: 600; }
.msg img.media { max-width: 260px; max-height: 320px; border-radius: 8px; display: block; cursor: zoom-in; }
.msg audio, .msg video { max-width: 280px; display: block; }
.msg a.doc { display: inline-flex; gap: 6px; align-items: center; color: var(--brand-dark); }
.tick.read { color: #2563eb; }
.tick.failed { color: var(--danger); }
.day-sep { align-self: center; background: #fff; color: var(--muted); font-size: 12px; padding: 2px 10px; border-radius: 999px; margin: 6px 0; }

.composer { background: var(--panel); border-top: 1px solid var(--line); padding: 8px 12px; display: flex; flex-direction: column; gap: 6px; }
.composer-tools { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }
.composer-tools select { width: auto; max-width: 200px; }
.file-btn { display: inline-flex; color: var(--text); font-size: 14px; }
.composer-row { display: flex; gap: 8px; align-items: flex-end; }
.composer-row textarea { resize: none; max-height: 160px; }
.composer.note-on textarea { background: var(--note); }

/* details */
.details { width: 300px; background: var(--panel); border-inline-start: 1px solid var(--line); padding: 12px; overflow-y: auto; display: flex; flex-direction: column; gap: 16px; }
.details-head { display: flex; justify-content: space-between; align-items: center; }
.details section { display: flex; flex-direction: column; gap: 8px; }
.details h3 { margin: 0; font-size: 14px; }
.label-boxes { display: flex; flex-direction: column; gap: 6px; }
.label-boxes .chk span.dot { width: 10px; height: 10px; border-radius: 50%; display: inline-block; }

/* dialogs */
dialog { border: none; border-radius: 14px; padding: 0; width: min(520px, calc(100% - 32px)); box-shadow: 0 20px 50px rgba(15,23,42,.25); }
dialog.wide { width: min(980px, calc(100% - 32px)); }
dialog::backdrop { background: rgba(15,23,42,.45); }
.dlg { padding: 20px; display: flex; flex-direction: column; gap: 12px; max-height: calc(100vh - 64px); overflow-y: auto; }
.dlg h2 { margin: 0; font-size: 18px; }
.dlg-title { display: flex; justify-content: space-between; align-items: center; }
.dlg-actions { display: flex; justify-content: flex-start; gap: 8px; flex-direction: row-reverse; }
.tpl-preview { background: var(--out); padding: 10px; border-radius: 8px; white-space: pre-wrap; min-height: 2em; }
.tabs { display: flex; gap: 4px; border-bottom: 1px solid var(--line); flex-wrap: wrap; }
.tabs button { background: transparent; border-radius: 8px 8px 0 0; }
.tabs button.active { background: var(--brand-soft); color: var(--brand-dark); font-weight: 600; }
.tbl { width: 100%; border-collapse: collapse; }
.tbl th, .tbl td { padding: 6px; border-bottom: 1px solid var(--line); text-align: right; vertical-align: middle; }
.tbl td input, .tbl td select { padding: 4px 6px; }
.tbl td input[type=checkbox] { width: auto; }
.inline-form { display: grid; grid-template-columns: repeat(auto-fit, minmax(150px, 1fr)); gap: 8px; margin-top: 16px; align-items: end; }
.inline-form h3, .inline-form .err, .inline-form .ok, .inline-form .muted { grid-column: 1 / -1; margin: 0; }
.inline-form.col { display: flex; flex-direction: column; align-items: stretch; }
.qr-list { list-style: none; padding: 0; margin: 0; display: flex; flex-direction: column; gap: 8px; }
.qr-list li { border: 1px solid var(--line); border-radius: 8px; padding: 8px; display: flex; gap: 8px; }
.qr-list li div { flex: 1; white-space: pre-wrap; }

.toast { position: fixed; bottom: 20px; left: 50%; transform: translateX(-50%); background: #0f172a; color: #fff; padding: 10px 16px; border-radius: 10px; z-index: 50; max-width: calc(100% - 32px); }
.toast.error { background: var(--danger); }

/* responsive: phones / small screens */
@media (max-width: 1100px) {
  .details { position: fixed; inset-block: 0; inset-inline-end: 0; z-index: 20; box-shadow: -10px 0 30px rgba(15,23,42,.2); }
}
@media (max-width: 760px) {
  .topbar { padding: 8px 12px; flex-wrap: wrap; }
  .me { display: none; }
  .sidebar { width: 100%; border: none; }
  .chat { display: none; }
  .layout.show-chat .sidebar { display: none; }
  .layout.show-chat .chat { display: flex; }
  .back { display: inline-block; }
  .msg { max-width: 85%; }
  .details { width: 100%; }
  #btnDetails { padding: 6px 8px; font-size: 12px; }
}
