:root {
  --bg: #0e1621; --panel: #17212b; --panel2: #1e2c3a; --line: #263545;
  --text: #e8eef4; --muted: #8b9bab; --accent: #3b82f6; --accent-text: #fff;
  --mine: #2b5278; --theirs: #1f2d3b; --danger: #e5484d; --ok: #3fb950; --warn-bg: #3a2f12; --warn: #f2c14e;
  --radius: 14px;
}
@media (prefers-color-scheme: light) {
  :root {
    --bg: #eef2f6; --panel: #ffffff; --panel2: #f3f6f9; --line: #dbe3ea;
    --text: #14202b; --muted: #5d6f80; --accent: #2563eb; --mine: #d6e8ff; --theirs: #f0f3f6;
    --warn-bg: #fff6dc; --warn: #7a5b00;
  }
}
* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body {
  background: var(--bg); color: var(--text);
  font: 15px/1.6 system-ui, -apple-system, "Segoe UI", Tahoma, "Vazirmatn", sans-serif;
  -webkit-font-smoothing: antialiased;
}
.hidden { display: none !important; }
.muted { color: var(--muted); }
.small { font-size: 12px; }
.pad { padding: 16px; }
.error { color: var(--danger); min-height: 1.4em; margin: 8px 0 0; }
.warn { background: var(--warn-bg); color: var(--warn); padding: 10px 12px; border-radius: 10px; font-size: 13px; margin: 4px 0; }
a { color: var(--accent); }

input, button, select { font: inherit; color: inherit; }
input:not([type=file]):not([type=checkbox]) {
  width: 100%; padding: 10px 12px; border-radius: 10px; border: 1px solid var(--line);
  background: var(--panel2); outline: none;
}
input:focus { border-color: var(--accent); }
label { display: block; font-size: 13px; color: var(--muted); }
label input { margin-top: 4px; color: var(--text); }

.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  padding: 9px 16px; border-radius: 10px; border: 1px solid var(--line); background: var(--panel2);
  cursor: pointer; text-decoration: none; color: var(--text);
}
.btn:hover { filter: brightness(1.1); }
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-text); }
.btn.danger { background: var(--danger); border-color: var(--danger); color: #fff; }
.btn.small { padding: 4px 10px; font-size: 13px; }
.btn:disabled { opacity: .5; cursor: not-allowed; }
.icon-btn {
  background: none; border: 0; cursor: pointer; font-size: 20px; padding: 6px 8px; border-radius: 8px;
  color: var(--text); text-decoration: none; line-height: 1;
}
.icon-btn:hover { background: var(--panel2); }

/* ---------- auth ---------- */
.auth { min-height: 100%; display: grid; place-items: center; padding: 16px; }
.auth-card { width: 100%; max-width: 400px; background: var(--panel); border: 1px solid var(--line); border-radius: 20px; padding: 28px 24px; text-align: center; }
.auth-card h1 { margin: 6px 0 2px; font-size: 22px; }
.logo { font-size: 40px; }
.logo.big { font-size: 64px; opacity: .6; }
.tabs { display: flex; gap: 6px; margin: 18px 0 14px; background: var(--panel2); padding: 4px; border-radius: 12px; }
.tab { flex: 1; border: 0; background: none; padding: 8px; border-radius: 9px; cursor: pointer; color: var(--muted); }
.tab.active { background: var(--panel); color: var(--text); box-shadow: 0 1px 3px rgba(0,0,0,.25); }
.form { display: grid; gap: 12px; text-align: start; }

/* ---------- app layout ---------- */
.app { display: grid; grid-template-columns: 320px 1fr; height: 100dvh; }
.sidebar { background: var(--panel); border-inline-end: 1px solid var(--line); display: flex; flex-direction: column; min-height: 0; }
.side-head { display: flex; justify-content: space-between; align-items: center; padding: 12px 14px; border-bottom: 1px solid var(--line); }
.head-actions { display: flex; gap: 2px; }
.contacts { list-style: none; margin: 0; padding: 0; overflow-y: auto; flex: 1; }
.contact { display: flex; align-items: center; gap: 12px; padding: 12px 14px; cursor: pointer; border-bottom: 1px solid var(--line); }
.contact:hover, .contact.active { background: var(--panel2); }
.avatar { width: 42px; height: 42px; border-radius: 50%; background: var(--accent); color: #fff; display: grid; place-items: center; font-weight: 700; flex: none; position: relative; }
.avatar .dot { position: absolute; inset-inline-end: -1px; bottom: -1px; width: 12px; height: 12px; border-radius: 50%; background: var(--ok); border: 2px solid var(--panel); }
.contact .meta { flex: 1; min-width: 0; }
.contact .name { font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.badge { background: var(--accent); color: #fff; border-radius: 999px; min-width: 22px; padding: 0 7px; text-align: center; font-size: 12px; line-height: 22px; }

.chat { display: flex; flex-direction: column; min-width: 0; min-height: 0; }
.chat-empty { margin: auto; text-align: center; color: var(--muted); }
.chat-pane { display: flex; flex-direction: column; height: 100%; min-height: 0; }
.chat-head { display: flex; align-items: center; gap: 8px; padding: 10px 14px; background: var(--panel); border-bottom: 1px solid var(--line); }
.chat-title { flex: 1; display: flex; flex-direction: column; line-height: 1.3; }
.messages { flex: 1; overflow-y: auto; padding: 14px; display: flex; flex-direction: column; gap: 6px; }
.load-more { align-self: center; }
.msg { max-width: min(78%, 520px); padding: 8px 12px; border-radius: var(--radius); background: var(--theirs); word-break: break-word; white-space: pre-wrap; }
.msg.mine { align-self: flex-start; background: var(--mine); }  /* RTL: start = right side */
.msg.theirs { align-self: flex-end; }
.msg .time { display: block; font-size: 11px; color: var(--muted); margin-top: 2px; text-align: end; }
.msg .ticks { margin-inline-start: 4px; }
.msg .media-placeholder { display: flex; align-items: center; gap: 8px; padding: 14px 18px; border-radius: 10px; background: rgba(127,127,127,.15); cursor: pointer; border: 0; color: inherit; }
.msg img.thumb, .msg video.thumb { max-width: 100%; max-height: 320px; border-radius: 10px; display: block; cursor: pointer; background: #000; }
.msg video.thumb { cursor: default; }
.msg .deleted { font-style: italic; color: var(--muted); }
.msg .fileinfo { font-size: 12px; color: var(--muted); }
.day-sep { align-self: center; font-size: 12px; color: var(--muted); background: var(--panel); padding: 2px 10px; border-radius: 99px; margin: 8px 0; }

.send-status { padding: 6px 14px; font-size: 13px; color: var(--muted); background: var(--panel2); }
.composer { display: flex; align-items: center; gap: 6px; padding: 10px 12px calc(10px + env(safe-area-inset-bottom)); background: var(--panel); border-top: 1px solid var(--line); }
.composer input#text { flex: 1; }
.composer .send { padding: 9px 18px; }

/* ---------- modals ---------- */
.modal { position: fixed; inset: 0; background: rgba(0,0,0,.65); display: grid; place-items: center; padding: 16px; z-index: 50; }
.modal-card, .viewer-card { background: var(--panel); border: 1px solid var(--line); border-radius: 18px; padding: 18px; width: 100%; max-width: 460px; max-height: 94dvh; overflow: auto; }
.viewer-card { max-width: 900px; }
.viewer-card img, .viewer-card video { max-width: 100%; max-height: 72dvh; display: block; margin: 0 auto; border-radius: 10px; }
.modal-card h2 { margin: 0 0 10px; font-size: 18px; }
.modal-actions { display: flex; gap: 8px; justify-content: center; flex-wrap: wrap; margin-top: 12px; }
.rec-video { width: 100%; max-height: 52dvh; background: #000; border-radius: 12px; transform: scaleX(-1); }
.rec-video.playback { transform: none; }
.rec-timer { text-align: center; font-variant-numeric: tabular-nums; margin-top: 8px; font-size: 18px; }
.rec-timer.live { color: var(--danger); }
.safety { background: var(--panel2); padding: 14px; border-radius: 12px; font-size: 20px; line-height: 1.8; text-align: center; font-family: ui-monospace, Menlo, Consolas, monospace; white-space: pre; margin: 10px 0; }

.toast { position: fixed; bottom: 22px; inset-inline: 0; margin: auto; width: fit-content; max-width: 90vw; background: var(--panel2); border: 1px solid var(--line); padding: 10px 16px; border-radius: 12px; z-index: 80; box-shadow: 0 6px 24px rgba(0,0,0,.35); }

.only-mobile { display: none; }
@media (max-width: 760px) {
  .app { grid-template-columns: 1fr; }
  .chat { display: none; }
  .app.in-chat .sidebar { display: none; }
  .app.in-chat .chat { display: flex; }
  .only-mobile { display: inline-block; }
  .msg { max-width: 88%; }
}

/* ---------- admin ---------- */
.admin { max-width: 1100px; margin: 0 auto; padding: 16px; }
.admin header { display: flex; align-items: center; justify-content: space-between; gap: 10px; flex-wrap: wrap; margin-bottom: 12px; }
.admin h1 { font-size: 20px; margin: 0; }
.panel { background: var(--panel); border: 1px solid var(--line); border-radius: 16px; padding: 14px; margin-bottom: 16px; overflow-x: auto; }
table { width: 100%; border-collapse: collapse; min-width: 560px; }
th, td { text-align: start; padding: 8px 10px; border-bottom: 1px solid var(--line); font-size: 14px; white-space: nowrap; }
th { color: var(--muted); font-weight: 600; }
.row-actions { display: flex; gap: 6px; flex-wrap: wrap; }
.tag { padding: 1px 8px; border-radius: 99px; font-size: 12px; background: var(--panel2); border: 1px solid var(--line); }
.tag.ok { color: var(--ok); } .tag.bad { color: var(--danger); }
.code { font-family: ui-monospace, Menlo, Consolas, monospace; letter-spacing: 1px; direction: ltr; unicode-bidi: embed; }
.toolbar { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; margin-bottom: 10px; }
.toolbar select { padding: 7px 10px; border-radius: 10px; background: var(--panel2); border: 1px solid var(--line); }
.note { background: var(--panel2); border: 1px solid var(--line); border-radius: 12px; padding: 10px 12px; font-size: 13px; color: var(--muted); margin-bottom: 12px; }
