/* Chat web app — same look as the Android app (WhatsApp-like), RTL, phone-first. */
:root {
  --primary: #008069;
  --primary-dark: #005c4b;
  --accent: #00a884;
  --accent-pressed: #008f6f;
  --danger: #ea0038;
  --text: #111b21;
  --text-2: #667781;
  --chat-bg: #efeae2;
  --mine: #d9fdd3;
  --other: #ffffff;
  --input-bar: #f0f2f5;
  --call-bg: #0b141a;
  --call-surface: #1f2c34;
  --shadow: 0 1px 0.5px rgba(11, 20, 26, .13);
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { margin: 0; height: 100%; }
body {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Tahoma, "Noto Sans Arabic", Arial, sans-serif;
  color: var(--text);
  background: var(--chat-bg);
  overflow: hidden;
  overscroll-behavior: none;
}
button { font: inherit; color: inherit; border: 0; background: none; padding: 0; cursor: pointer; }
[hidden] { display: none !important; }
.i { width: 24px; height: 24px; fill: currentColor; flex: none; }
[dir="rtl"] .flip-rtl { transform: scaleX(-1); }

.screen { position: fixed; inset: 0; display: flex; flex-direction: column; height: 100dvh; }

/* ---------------------------------------------------------------- Login */
#login { background: #fff; overflow-y: auto; }
.login-hero {
  background: var(--primary); color: #fff; text-align: center;
  padding: calc(48px + var(--safe-top)) 24px 32px;
}
.login-logo { width: 84px; height: 84px; border-radius: 22px; }
.login-hero h1 { margin: 12px 0 4px; font-size: 28px; }
.login-hero p { margin: 0; opacity: .85; font-size: 15px; }
.login-form { padding: 20px 24px calc(24px + var(--safe-bottom)); max-width: 440px; width: 100%; margin: 0 auto; }
.login-form h2 { font-size: 20px; margin: 0 0 4px; }
.login-form input {
  display: block; width: 100%; height: 52px; margin-top: 12px; padding: 0 16px;
  border: 1px solid #d1d7db; border-radius: 10px; font-size: 16px; color: var(--text); background: #fff;
  outline: none; transition: border-color .15s;
}
.login-form input:focus { border: 2px solid var(--accent); padding: 0 15px; }
.form-error { color: var(--danger); text-align: center; font-size: 14px; margin: 12px 0 0; }
.btn-primary {
  display: block; width: 100%; height: 52px; margin-top: 20px; border-radius: 26px;
  background: var(--accent); color: #fff; font-size: 17px; font-weight: 700;
}
.btn-primary:active { background: var(--accent-pressed); }
.btn-primary:disabled { opacity: .6; }
.install-hint { margin-top: 20px; font-size: 13px; color: var(--text-2); text-align: center; line-height: 1.7; }
.install-hint .i { width: 16px; height: 16px; vertical-align: -3px; fill: #007aff; }

/* ---------------------------------------------------------------- Top bar */
.topbar {
  display: flex; align-items: center; gap: 4px; flex: none;
  background: var(--primary); color: #fff;
  padding: calc(6px + var(--safe-top)) 8px 6px;
  min-height: calc(56px + var(--safe-top));
}
.topbar-logo { width: 38px; height: 38px; border-radius: 50%; margin-inline: 6px 8px; }
.topbar-titles { flex: 1; min-width: 0; }
.topbar-title { font-size: 18px; font-weight: 600; }
.topbar-sub { font-size: 13px; opacity: .85; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.icon-btn { width: 44px; height: 44px; display: inline-flex; align-items: center; justify-content: center; border-radius: 50%; flex: none; }
.icon-btn:active { background: rgba(255, 255, 255, .15); }
.icon-btn.gray { color: #8696a0; }
.icon-btn.gray:active { background: rgba(0, 0, 0, .06); }
.icon-btn.small { width: 36px; height: 36px; }

.call-banner {
  display: flex; align-items: center; gap: 12px; flex: none; width: 100%;
  background: var(--accent); color: #fff; padding: 10px 16px; font-size: 14px; text-align: start;
}
.call-banner .i { width: 20px; height: 20px; }
.call-banner span:first-of-type { flex: 1; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.pill-btn { background: rgba(0, 0, 0, .2); color: #fff; padding: 6px 16px; border-radius: 16px; font-weight: 700; font-size: 14px; text-decoration: none; }

/* ---------------------------------------------------------------- Messages */
.messages {
  flex: 1; overflow-y: auto; -webkit-overflow-scrolling: touch;
  padding: 6px 10px 8px; position: relative;
}
.loading-older { text-align: center; color: var(--text-2); font-size: 13px; padding: 8px; }
.empty {
  margin: 30vh auto 0; width: fit-content; max-width: 80%; text-align: center; line-height: 1.7;
  background: #fff8e1; color: var(--text-2); padding: 14px 18px; border-radius: 8px; font-size: 14px;
}
.day { display: flex; justify-content: center; margin: 12px 0 8px; }
.day span { background: #fff; color: var(--text-2); font-size: 12.5px; padding: 5px 12px; border-radius: 8px; box-shadow: var(--shadow); }
.event { display: flex; justify-content: center; margin: 6px 0; }
.event span {
  display: inline-flex; align-items: center; gap: 8px; background: #fff8e1; color: var(--text);
  font-size: 13px; padding: 7px 14px; border-radius: 8px; box-shadow: var(--shadow); cursor: default;
}
.event.joinable span { cursor: pointer; }
.event .i { width: 16px; height: 16px; fill: var(--accent); }

.row { display: flex; padding-top: 7px; }
.row.grouped { padding-top: 2px; }
.row.mine { justify-content: flex-end; } /* RTL: my messages on the left, like WhatsApp in Arabic */
.row.other { justify-content: flex-start; }
.row.highlight .bubble { box-shadow: 0 0 0 3px rgba(0, 168, 132, .45); }
.bubble {
  max-width: min(78%, 520px); min-width: 90px; border-radius: 10px; padding: 6px 9px 5px;
  box-shadow: var(--shadow); position: relative; user-select: none; -webkit-user-select: none;
  -webkit-touch-callout: none;
}
.mine .bubble { background: var(--mine); }
.other .bubble { background: var(--other); }
.name { font-size: 13px; font-weight: 700; margin-bottom: 2px; }
.text { font-size: 16px; line-height: 1.45; white-space: pre-wrap; overflow-wrap: anywhere; }
.text a { color: #027eb5; }
.quote {
  display: flex; background: rgba(0, 0, 0, .06); border-radius: 6px; overflow: hidden;
  margin-bottom: 4px; cursor: pointer;
}
.quote-bar { width: 4px; flex: none; }
.quote-body { padding: 5px 8px 5px 10px; min-width: 0; }
.quote-name { font-size: 12.5px; font-weight: 700; }
.quote-text { font-size: 13px; color: var(--text-2); display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden; }
.img-msg {
  display: block; width: 240px; max-width: 100%; aspect-ratio: 1; object-fit: cover;
  border-radius: 6px; background: #dde3e6; cursor: zoom-in;
}
.audio-msg { display: flex; align-items: center; gap: 8px; min-width: 210px; padding: 2px 0; }
.audio-msg .play { width: 40px; height: 40px; display: flex; align-items: center; justify-content: center; color: #54656f; }
.audio-msg .play .i { width: 30px; height: 30px; }
.audio-track { flex: 1; height: 4px; border-radius: 2px; background: rgba(0, 0, 0, .15); overflow: hidden; direction: ltr; }
.audio-fill { height: 100%; width: 0; background: var(--accent); transform-origin: right; }
[dir="rtl"] .audio-track { direction: rtl; }
.audio-dur { font-size: 13px; color: var(--text-2); min-width: 34px; }
.meta { display: flex; justify-content: flex-end; align-items: center; gap: 3px; margin-top: 1px; font-size: 11px; color: var(--text-2); direction: ltr; }
.tick { width: 16px; height: 16px; }
.tick.sent, .tick.delivered { fill: #8696a0; }
.tick.read { fill: #34b7f1; }

.fab-down {
  position: absolute; left: 14px; bottom: calc(76px + var(--safe-bottom));
  width: 42px; height: 42px; border-radius: 50%; background: #fff; color: #54656f;
  display: flex; align-items: center; justify-content: center; box-shadow: 0 1px 3px rgba(0, 0, 0, .25);
}
.badge {
  position: absolute; top: -8px; left: 50%; transform: translateX(-50%);
  background: var(--accent); color: #fff; font-size: 11px; font-weight: 700;
  min-width: 20px; height: 20px; border-radius: 10px; padding: 0 5px; display: flex; align-items: center; justify-content: center;
}

/* ---------------------------------------------------------------- Composer */
.composer {
  display: flex; align-items: flex-end; gap: 6px; flex: none;
  background: var(--input-bar); padding: 6px 6px calc(6px + var(--safe-bottom));
}
.composer-box { flex: 1; min-width: 0; background: #fff; border-radius: 24px; overflow: hidden; }
.composer-row { display: flex; align-items: flex-end; }
#input {
  flex: 1; min-width: 0; border: 0; outline: none; resize: none; background: transparent;
  font: inherit; font-size: 16px; line-height: 22px; color: var(--text);
  padding: 13px 16px 13px 4px; max-height: 120px; min-height: 48px;
}
.composer-row .icon-btn { height: 48px; }
.round-btn {
  width: 48px; height: 48px; border-radius: 50%; flex: none; background: var(--accent); color: #fff;
  display: flex; align-items: center; justify-content: center;
}
.round-btn:active { background: var(--accent-pressed); }
.round-btn.recording { background: var(--danger); }
.reply-preview { display: flex; align-items: stretch; margin: 6px 6px 0; background: rgba(0, 0, 0, .05); border-radius: 8px; overflow: hidden; }
.reply-bar { width: 4px; background: var(--accent); flex: none; }
.reply-body { flex: 1; min-width: 0; padding: 6px 10px; }
.reply-name { font-size: 13px; font-weight: 700; color: var(--primary); }
.reply-text { font-size: 13px; color: var(--text-2); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.recording { display: flex; align-items: center; gap: 10px; height: 48px; padding: 0 6px 0 16px; font-size: 16px; }
.rec-dot { width: 10px; height: 10px; border-radius: 50%; background: var(--danger); animation: blink 1s infinite; }
.rec-hint { flex: 1; color: var(--text-2); font-size: 14px; }
@keyframes blink { 50% { opacity: .2; } }

/* ---------------------------------------------------------------- Sheet & dialog */
.sheet-backdrop, .dialog-backdrop {
  position: fixed; inset: 0; background: rgba(11, 20, 26, .45); z-index: 30;
  display: flex; align-items: flex-end; justify-content: center; animation: fade .15s;
}
.dialog-backdrop { align-items: center; padding: 24px; }
.sheet {
  background: #fff; width: 100%; max-width: 520px; border-radius: 16px 16px 0 0;
  padding: 8px 0 calc(8px + var(--safe-bottom)); animation: up .18s ease-out;
}
.sheet button { display: flex; align-items: center; gap: 16px; width: 100%; padding: 14px 24px; font-size: 16px; text-align: start; }
.sheet button:active { background: #f0f2f5; }
.sheet button.danger { color: var(--danger); }
.sheet .i { fill: #54656f; }
.sheet .danger .i { fill: var(--danger); }
.dialog { background: #fff; border-radius: 14px; width: 100%; max-width: 380px; padding: 20px 20px 10px; animation: pop .15s; }
.dialog h3 { margin: 0 0 10px; font-size: 18px; }
#dialog-body { font-size: 15px; color: var(--text); line-height: 1.6; max-height: 60vh; overflow-y: auto; }
.receipt-group { font-size: 13px; color: var(--text-2); margin: 10px 0 4px; }
.receipt { display: flex; justify-content: space-between; padding: 4px 0; }
.receipt time { color: var(--text-2); font-size: 13px; direction: ltr; }
.dialog-actions { display: flex; justify-content: flex-end; gap: 8px; margin-top: 14px; }
.dialog-actions button { padding: 10px 14px; border-radius: 18px; color: var(--accent); font-weight: 700; }
.dialog-actions button.danger { color: var(--danger); }
.dialog-actions button:active { background: #f0f2f5; }
@keyframes fade { from { opacity: 0; } }
@keyframes up { from { transform: translateY(100%); } }
@keyframes pop { from { transform: scale(.94); opacity: 0; } }

.toast {
  position: fixed; left: 50%; bottom: calc(90px + var(--safe-bottom)); transform: translateX(-50%); z-index: 50;
  background: rgba(17, 27, 33, .9); color: #fff; padding: 10px 18px; border-radius: 20px; font-size: 14px;
  max-width: 85%; text-align: center;
}

/* ---------------------------------------------------------------- Gallery & viewer */
.overlay { position: fixed; inset: 0; z-index: 20; background: #fff; display: flex; flex-direction: column; }
.gallery-grid { flex: 1; overflow-y: auto; display: grid; grid-template-columns: repeat(auto-fill, minmax(110px, 1fr)); gap: 2px; align-content: start; }
.gallery-grid img { width: 100%; aspect-ratio: 1; object-fit: cover; display: block; background: #dde3e6; cursor: zoom-in; }
.viewer { position: fixed; inset: 0; z-index: 40; background: #000; display: flex; align-items: center; justify-content: center; }
.viewer img { max-width: 100%; max-height: 100%; object-fit: contain; }
.viewer-bar { position: absolute; bottom: calc(24px + var(--safe-bottom)); display: flex; gap: 12px; }
.pill-btn.dark { background: rgba(0, 0, 0, .55); padding: 10px 22px; font-size: 15px; }

/* ---------------------------------------------------------------- Call */
.call { position: fixed; inset: 0; z-index: 35; background: var(--call-bg); color: #fff; display: flex; flex-direction: column; }
.call-head { text-align: center; padding: calc(16px + var(--safe-top)) 16px 8px; }
.call-title { font-size: 20px; }
.call-status { color: #8696a0; font-size: 14px; margin-top: 4px; direction: ltr; }
.call-stage { flex: 1; min-height: 0; position: relative; }
.call-avatars { display: flex; flex-wrap: wrap; justify-content: center; align-content: center; gap: 8px 24px; height: 100%; padding: 16px; overflow-y: auto; }
.avatar { width: 92px; text-align: center; }
.avatar-circle { width: 80px; height: 80px; border-radius: 50%; margin: 0 auto; display: flex; align-items: center; justify-content: center; font-size: 32px; color: #fff; }
.avatar-name { margin-top: 8px; font-size: 14px; color: #e9edef; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.avatar-name .i { width: 14px; height: 14px; fill: #f15c6d; vertical-align: -2px; }
.waiting { width: 100%; text-align: center; color: #8696a0; font-size: 14px; }
.call-grid { position: absolute; inset: 0; display: grid; gap: 3px; padding: 4px; }
.call-grid.n1 { grid-template: 1fr / 1fr; }
.call-grid.n2 { grid-template: 1fr 1fr / 1fr; }
.call-grid.n3, .call-grid.n4 { grid-template: 1fr 1fr / 1fr 1fr; }
.tile { position: relative; background: #000; overflow: hidden; border-radius: 6px; }
.tile canvas, .tile video { position: absolute; inset: 0; width: 100%; height: 100%; object-fit: cover; }
.tile video.mirror { transform: scaleX(-1); }
.tile-name { position: absolute; bottom: 0; right: 0; background: rgba(0, 0, 0, .55); font-size: 13px; padding: 3px 8px; border-top-left-radius: 6px; }
.tile-wait { position: absolute; inset: 0; display: flex; align-items: center; justify-content: center; color: #8696a0; font-size: 14px; }
.call-people { text-align: center; color: #8696a0; font-size: 13px; padding: 6px 16px 0; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.call-controls { background: var(--call-surface); padding: 16px 12px calc(20px + var(--safe-bottom)); }
.call-row { display: flex; justify-content: space-around; }
.ctl { display: flex; flex-direction: column; align-items: center; gap: 6px; font-size: 12px; color: #e9edef; width: 80px; }
.ctl-btn { width: 56px; height: 56px; border-radius: 50%; background: rgba(255, 255, 255, .2); display: flex; align-items: center; justify-content: center; }
.ctl-btn.on { background: var(--accent); }
.ctl-btn:disabled { opacity: .4; }
.ctl-btn .i { width: 26px; height: 26px; }
.ctl-end { display: flex; align-items: center; justify-content: center; width: 68px; height: 68px; border-radius: 50%; background: var(--danger); margin: 18px auto 0; }
.ctl-end:active { background: #c20030; }
.ctl-end .i { width: 30px; height: 30px; }

@media (min-width: 700px) {
  .bubble { max-width: 60%; }
}

/* Voice note play/pause icon swap */
.audio-msg .ic-pause { display: none; }
.audio-msg.playing .ic-play { display: none; }
.audio-msg.playing .ic-pause { display: block; }
