:root {
  --brand: #0f3d3e;        /* deep teal */
  --brand-2: #14595a;
  --accent: #c9953b;       /* golden sand */
  --accent-ink: #1b1305;
  --bg: #f1ede4;
  --panel: #ffffff;
  --panel-2: #f7f5f0;
  --line: #e4ded2;
  --ink: #1e2324;
  --muted: #6d7475;
  --mine: #dcefe6;
  --theirs: #ffffff;
  --read: #2a8fd6;
  --danger: #d13b3b;
  --ok: #1fa35c;
  --chat-bg: #ece6da;
  --shadow: 0 1px 1px rgba(0,0,0,.06), 0 2px 8px rgba(0,0,0,.05);
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0c1415; --panel: #111b1c; --panel-2: #172324; --line: #243334;
    --ink: #e6ebeb; --muted: #93a0a1; --mine: #164a3f; --theirs: #1b2728;
    --chat-bg: #0b1213; --shadow: none; color-scheme: dark;
  }
}
* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body { font: 15px/1.4 system-ui, -apple-system, "Segoe UI", Roboto, "Noto Sans Arabic", sans-serif; color: var(--ink); background: var(--bg); overflow: hidden; }
button, input, textarea { font: inherit; color: inherit; }
button { cursor: pointer; }
svg { width: 22px; height: 22px; fill: currentColor; }
.hidden { display: none !important; }
.muted { color: var(--muted); }
.error { color: var(--danger); min-height: 1.2em; margin: 4px 0; font-size: 14px; }

/* ---------- auth ---------- */
.auth { height: 100%; display: grid; place-items: center; padding: 16px; background: linear-gradient(160deg, var(--brand) 0 38%, var(--bg) 38%); }
.auth-card { width: min(400px, 100%); background: var(--panel); border-radius: 14px; padding: 28px; box-shadow: 0 10px 40px rgba(0,0,0,.15); display: grid; gap: 12px; }
.brand { display: flex; gap: 14px; align-items: center; margin-bottom: 6px; }
.brand img { width: 52px; height: 52px; }
.brand h1 { font-size: 20px; margin: 0; }
.brand p { margin: 2px 0 0; color: var(--muted); font-size: 14px; }
label { display: grid; gap: 5px; font-size: 13px; font-weight: 600; color: var(--muted); }
input:not([type=checkbox]):not([type=file]), textarea.field { border: 1px solid var(--line); background: var(--panel-2); border-radius: 8px; padding: 10px 12px; font-weight: 400; color: var(--ink); outline: none; width: 100%; }
input:focus, textarea.field:focus { border-color: var(--brand-2); box-shadow: 0 0 0 3px color-mix(in srgb, var(--brand-2) 20%, transparent); }
.btn { border: 1px solid var(--line); background: var(--panel-2); border-radius: 8px; padding: 10px 16px; font-weight: 600; text-decoration: none; display: inline-flex; justify-content: center; }
.btn.primary { background: var(--brand); border-color: var(--brand); color: #fff; }
.btn.primary:hover { background: var(--brand-2); }
.btn.danger { color: var(--danger); }
.btn.small { padding: 6px 10px; font-size: 13px; }
.btn:disabled { opacity: .5; cursor: default; }

/* ---------- layout ---------- */
.app { display: grid; grid-template-columns: minmax(300px, 380px) 1fr; height: 100dvh; max-width: 1500px; margin: 0 auto; background: var(--panel); box-shadow: 0 0 30px rgba(0,0,0,.08); }
.sidebar { display: flex; flex-direction: column; border-right: 1px solid var(--line); min-width: 0; min-height: 0; background: var(--panel); }
.pane { position: relative; min-width: 0; min-height: 0; background: var(--chat-bg); }
.bar { display: flex; align-items: center; gap: 10px; padding: 10px 12px; background: var(--brand); color: #fff; min-height: 60px; position: relative; padding-top: max(10px, env(safe-area-inset-top)); }
.bar-title { flex: 1; min-width: 0; display: flex; flex-direction: column; line-height: 1.2; text-align: left; }
.bar-title strong, .bar-title small { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.bar-title small { opacity: .8; font-size: 12.5px; }
.as-btn { background: none; border: 0; padding: 0; color: inherit; }
.icon-btn { background: none; border: 0; color: inherit; width: 40px; height: 40px; border-radius: 50%; display: grid; place-items: center; flex: none; }
.icon-btn:hover { background: rgba(255,255,255,.12); }
.pane .composer .icon-btn:hover, .reply-bar .icon-btn:hover, .record-bar .icon-btn:hover, .modal .icon-btn:hover { background: color-mix(in srgb, var(--ink) 8%, transparent); }
.back { display: none; }
.menu { position: absolute; right: 10px; top: 54px; background: var(--panel); color: var(--ink); border-radius: 10px; box-shadow: 0 8px 30px rgba(0,0,0,.2); z-index: 20; padding: 6px 0; min-width: 200px; }
.menu button { display: block; width: 100%; text-align: left; padding: 10px 16px; border: 0; background: none; }
.menu button:hover { background: var(--panel-2); }
body:not(.is-admin) .admin-only { display: none; }

.avatar { width: 42px; height: 42px; border-radius: 50%; display: grid; place-items: center; font-weight: 700; color: #fff; flex: none; font-size: 15px; letter-spacing: .5px; position: relative; }
.avatar.sm { width: 34px; height: 34px; font-size: 13px; }
.avatar.xl { width: 110px; height: 110px; font-size: 38px; }
.avatar .dot { position: absolute; right: 0; bottom: 0; width: 12px; height: 12px; border-radius: 50%; background: var(--ok); border: 2px solid var(--panel); }

.search { padding: 8px 10px; border-bottom: 1px solid var(--line); }
.search input { border-radius: 20px !important; padding: 8px 14px !important; }
.chat-list { list-style: none; margin: 0; padding: 0; overflow-y: auto; flex: 1; }
.chat-item { display: flex; gap: 12px; align-items: center; padding: 10px 14px; cursor: pointer; border-bottom: 1px solid color-mix(in srgb, var(--line) 60%, transparent); }
.chat-item:hover { background: var(--panel-2); }
.chat-item.active { background: color-mix(in srgb, var(--accent) 16%, var(--panel)); }
.chat-item .meta { flex: 1; min-width: 0; }
.chat-item .row { display: flex; justify-content: space-between; gap: 8px; align-items: baseline; }
.chat-item .name { font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.chat-item .time { font-size: 12px; color: var(--muted); flex: none; }
.chat-item .preview { font-size: 13.5px; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; display: flex; gap: 4px; align-items: center; }
.chat-item.unread .time { color: var(--accent); font-weight: 600; }
.badge { background: var(--accent); color: var(--accent-ink); border-radius: 12px; min-width: 20px; height: 20px; padding: 0 6px; font-size: 12px; font-weight: 700; display: grid; place-items: center; flex: none; }
.empty { text-align: center; color: var(--muted); padding: 30px 20px; }

.welcome { height: 100%; display: grid; place-content: center; text-align: center; color: var(--muted); padding: 20px; border-bottom: 6px solid var(--accent); }
.welcome img { width: 90px; margin: 0 auto 10px; opacity: .9; }
.welcome h2 { color: var(--ink); margin: 6px 0; font-weight: 500; }

/* ---------- chat ---------- */
.chat-view { display: flex; flex-direction: column; height: 100%; }
.messages { flex: 1; overflow-y: auto; padding: 14px 6% 10px; display: flex; flex-direction: column; gap: 2px;
  background-image: radial-gradient(color-mix(in srgb, var(--accent) 14%, transparent) 1px, transparent 1px); background-size: 22px 22px; }
.day { align-self: center; background: var(--panel); color: var(--muted); font-size: 12px; padding: 4px 12px; border-radius: 8px; margin: 10px 0 6px; box-shadow: var(--shadow); }
.sys { align-self: center; background: color-mix(in srgb, var(--accent) 22%, var(--panel)); font-size: 12.5px; padding: 5px 12px; border-radius: 8px; margin: 4px 0; text-align: center; max-width: 85%; }
.msg { max-width: min(75%, 560px); padding: 6px 8px 4px 9px; border-radius: 10px; background: var(--theirs); box-shadow: var(--shadow); align-self: flex-start; position: relative; word-wrap: break-word; overflow-wrap: anywhere; margin-top: 1px; }
.msg.mine { background: var(--mine); align-self: flex-end; }
.msg.first { margin-top: 8px; }
.msg.first:not(.mine) { border-top-left-radius: 2px; }
.msg.first.mine { border-top-right-radius: 2px; }
.msg .who { font-size: 12.5px; font-weight: 700; margin-bottom: 2px; }
.msg .text { white-space: pre-wrap; }
.msg .text a { color: var(--read); }
.msg .foot { float: right; margin: 6px 0 -2px 10px; font-size: 11px; color: var(--muted); display: flex; gap: 3px; align-items: center; line-height: 1; }
.ticks { width: 17px; height: 12px; }
.ticks.read { color: var(--read); }
.msg .reply-btn { position: absolute; top: 4px; right: -34px; opacity: 0; transition: opacity .15s; width: 28px; height: 28px; border-radius: 50%; border: 0; background: var(--panel); box-shadow: var(--shadow); display: grid; place-items: center; color: var(--muted); }
.msg.mine .reply-btn { right: auto; left: -34px; }
.msg:hover .reply-btn { opacity: 1; }
.msg .reply-btn svg { width: 16px; height: 16px; }
.quote { border-left: 4px solid var(--accent); background: color-mix(in srgb, var(--ink) 6%, transparent); border-radius: 6px; padding: 4px 8px; margin-bottom: 4px; font-size: 13px; cursor: pointer; }
.quote strong { display: block; font-size: 12px; color: var(--brand-2); }
.quote span { color: var(--muted); display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden; }
.msg img.photo { display: block; max-width: 100%; max-height: 340px; border-radius: 6px; margin: 2px 0 4px; cursor: zoom-in; background: var(--panel-2); min-width: 120px; min-height: 80px; object-fit: cover; }
.msg video { max-width: 100%; max-height: 340px; border-radius: 6px; display: block; }
.file-card { display: flex; gap: 10px; align-items: center; background: color-mix(in srgb, var(--ink) 6%, transparent); border-radius: 8px; padding: 8px 10px; text-decoration: none; color: inherit; margin: 2px 0 4px; min-width: 200px; }
.file-card .ext { width: 38px; height: 44px; border-radius: 4px; background: var(--brand-2); color: #fff; font-size: 10px; font-weight: 700; display: grid; place-items: center; text-transform: uppercase; flex: none; }
.file-card .fname { font-weight: 600; font-size: 14px; word-break: break-all; }
.file-card small { color: var(--muted); }
.msg audio { width: 250px; max-width: 100%; height: 40px; display: block; margin: 2px 0; }
.call-log { display: flex; gap: 8px; align-items: center; }
.call-log svg { width: 18px; height: 18px; }
.call-log.missed svg { color: var(--danger); }
.flash { animation: flash 1.2s; }
@keyframes flash { 30% { box-shadow: 0 0 0 4px var(--accent); } }

.reply-bar, .upload-bar { display: flex; align-items: center; gap: 10px; padding: 8px 12px; background: var(--panel-2); border-top: 1px solid var(--line); }
.reply-bar > div { flex: 1; min-width: 0; border-left: 4px solid var(--accent); padding-left: 8px; display: flex; flex-direction: column; }
.reply-bar span { color: var(--muted); font-size: 13px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.upload-bar { font-size: 13px; color: var(--muted); }
.progress { flex: 1; height: 4px; background: var(--line); border-radius: 2px; overflow: hidden; }
.progress i { display: block; height: 100%; background: var(--accent); width: 0; transition: width .2s; }

.composer, .record-bar { display: flex; align-items: flex-end; gap: 6px; padding: 8px 10px; background: var(--panel-2); padding-bottom: max(8px, env(safe-area-inset-bottom)); }
.composer .icon-btn { color: var(--muted); }
.composer textarea { flex: 1; resize: none; border: 0; border-radius: 22px; padding: 10px 16px; background: var(--panel); outline: none; max-height: 140px; line-height: 1.35; }
.send-btn { width: 44px; height: 44px; border-radius: 50%; border: 0; background: var(--brand); color: #fff; display: grid; place-items: center; flex: none; }
.send-btn:hover { background: var(--brand-2); }
.send-btn .i-send { display: none; }
.composer.has-text .send-btn .i-send { display: block; }
.composer.has-text .send-btn .i-mic { display: none; }
.record-bar { align-items: center; }
.record-bar .muted { flex: 1; font-size: 13px; }
.rec-dot { width: 10px; height: 10px; border-radius: 50%; background: var(--danger); animation: pulse 1s infinite; }
@keyframes pulse { 50% { opacity: .3; } }
#recTime { font-variant-numeric: tabular-nums; font-weight: 600; }

/* ---------- modal ---------- */
.modal { position: fixed; inset: 0; background: rgba(0,0,0,.45); display: grid; place-items: center; z-index: 50; padding: 16px; }
.modal-card { background: var(--panel); border-radius: 14px; width: min(480px, 100%); max-height: 88dvh; display: flex; flex-direction: column; overflow: hidden; box-shadow: 0 20px 60px rgba(0,0,0,.3); }
.modal-card header { display: flex; align-items: center; justify-content: space-between; padding: 12px 12px 12px 20px; border-bottom: 1px solid var(--line); }
.modal-card h3 { margin: 0; font-size: 17px; }
.modal-body { padding: 16px 20px 20px; overflow-y: auto; display: grid; gap: 12px; }
.pick-list { list-style: none; margin: 0; padding: 0; display: grid; }
.pick-list li { display: flex; align-items: center; gap: 12px; padding: 8px 6px; border-radius: 8px; cursor: pointer; }
.pick-list li:hover { background: var(--panel-2); }
.pick-list li .meta { flex: 1; min-width: 0; }
.pick-list li small { display: block; color: var(--muted); }
.pick-list input[type=checkbox] { width: 20px; height: 20px; accent-color: var(--brand); }
.tag { font-size: 11px; font-weight: 700; padding: 2px 7px; border-radius: 10px; background: color-mix(in srgb, var(--accent) 25%, transparent); color: var(--ink); }
.tag.off { background: color-mix(in srgb, var(--danger) 18%, transparent); }
.row-actions { display: flex; gap: 8px; flex-wrap: wrap; justify-content: flex-end; }
.section-title { font-size: 12px; text-transform: uppercase; letter-spacing: .06em; color: var(--muted); margin: 6px 0 -4px; font-weight: 700; }
.check { display: flex; align-items: center; gap: 8px; font-weight: 500; color: var(--ink); }

/* ---------- call ---------- */
.call { position: fixed; inset: 0; z-index: 60; background: radial-gradient(circle at 50% 30%, var(--brand-2), #061c1d 70%); color: #fff; display: grid; place-items: center; text-align: center; }
.call-inner { display: grid; justify-items: center; gap: 10px; padding: 20px; }
.call h2 { margin: 16px 0 0; font-weight: 500; font-size: 26px; }
.call p { margin: 0; opacity: .8; font-variant-numeric: tabular-nums; }
.call-actions { display: flex; gap: 34px; margin-top: 60px; }
.round { width: 66px; height: 66px; border-radius: 50%; border: 0; display: grid; place-items: center; color: #fff; }
.round svg { width: 28px; height: 28px; }
.round.hang { background: var(--danger); }
.round.accept { background: var(--ok); animation: ring 1.2s infinite; }
.round.mute { background: rgba(255,255,255,.15); }
.round.mute .slash { display: none; }
.round.mute.on { background: #fff; color: var(--brand); }
.round.mute.on .slash { display: inline; }
@keyframes ring { 0%,100% { transform: rotate(0); } 10%,30% { transform: rotate(-14deg); } 20%,40% { transform: rotate(14deg); } 50% { transform: rotate(0); } }

.lightbox { position: fixed; inset: 0; background: rgba(0,0,0,.9); z-index: 70; display: grid; place-items: center; padding: 16px; gap: 12px; grid-template-rows: 1fr auto; }
.lightbox img { max-width: 100%; max-height: 82dvh; object-fit: contain; }
.toast { position: fixed; bottom: 90px; left: 50%; transform: translateX(-50%); background: #222; color: #fff; padding: 10px 18px; border-radius: 20px; z-index: 80; font-size: 14px; max-width: 90vw; text-align: center; }

/* ---------- mobile ---------- */
@media (max-width: 760px) {
  .app { grid-template-columns: 1fr; box-shadow: none; }
  .pane { display: none; }
  .app.chat-open .sidebar { display: none; }
  .app.chat-open .pane { display: block; }
  .back { display: grid; margin-left: -8px; }
  .messages { padding: 10px 10px 8px; }
  .msg { max-width: 86%; }
  .msg .reply-btn { display: none; }
}

.auth-note { margin: 0; background: color-mix(in srgb, var(--accent) 16%, var(--panel)); border-radius: 8px; padding: 10px 12px; font-size: 14px; }
.lock-note { text-align: center; color: var(--muted); font-size: 12.5px; margin: 4px 0 0; }
.link-btn { background: none; border: 0; color: var(--brand-2); text-decoration: underline; font-size: 14px; }
.e2e-banner { align-self: center; background: color-mix(in srgb, var(--accent) 22%, var(--panel)); font-size: 12.5px; padding: 6px 12px; border-radius: 8px; margin: 4px 0 8px; text-align: center; max-width: 440px; }
.msg .locked { font-style: italic; color: var(--muted); }
.safety { display: grid; grid-template-columns: repeat(4, 1fr); gap: 6px 14px; font: 600 17px/1.3 ui-monospace, Menlo, monospace; letter-spacing: .06em; background: var(--panel-2); border-radius: 10px; padding: 14px; justify-items: center; }
.file-card.loading { opacity: .6; }
