:root {
  --bg: #0b0e1a;
  --bg2: #11162a;
  --card: #161b30;
  --card-soft: rgba(255,255,255,0.06);
  --border: rgba(255,255,255,0.08);
  --text: #f2f3f8;
  --text-dim: #9aa0b4;
  --accent: #7c6cff;
  --accent2: #ff6ec7;
  --accent-grad: linear-gradient(135deg, #7c6cff 0%, #ff6ec7 100%);
  --bubble-me: linear-gradient(135deg, #7c6cff 0%, #6a5cf0 100%);
  --bubble-other: #1d2340;
  --success: #34d399;
  --danger: #ff5470;
  --radius: 18px;
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
}

* { box-sizing: border-box; }

html, body {
  height: 100%;
  margin: 0;
  background: radial-gradient(circle at 20% 0%, #1a1440 0%, var(--bg) 45%) fixed;
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "SF Arabic", "Cairo", Tahoma, sans-serif;
  overscroll-behavior: none;
  -webkit-tap-highlight-color: transparent;
}

body { display: flex; flex-direction: column; height: 100dvh; overflow: hidden; }

.view { flex: 1; display: flex; flex-direction: column; min-height: 0; }
.hidden { display: none !important; }

/* ---------------- شاشة الترحيب ---------------- */
#view-auth {
  align-items: center;
  justify-content: center;
  padding: 24px;
  background:
    radial-gradient(circle at 15% 10%, rgba(124,108,255,0.35), transparent 40%),
    radial-gradient(circle at 85% 90%, rgba(255,110,199,0.25), transparent 40%),
    var(--bg);
}

.auth-card {
  width: 100%;
  max-width: 380px;
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: 24px;
  padding: 28px 24px;
  box-shadow: 0 20px 60px rgba(0,0,0,0.45);
  backdrop-filter: blur(12px);
}

.brand { text-align: center; margin-bottom: 22px; }
.brand-orb {
  width: 64px; height: 64px; margin: 0 auto 14px;
  border-radius: 50%;
  background: var(--accent-grad);
  box-shadow: 0 0 40px rgba(124,108,255,0.55);
  animation: pulse 3s ease-in-out infinite;
}
@keyframes pulse {
  0%, 100% { transform: scale(1); }
  50% { transform: scale(1.06); }
}
.brand h1 { margin: 0; font-size: 26px; font-weight: 800; letter-spacing: -0.5px; }
.brand .sub { margin: 6px 0 0; color: var(--text-dim); font-size: 14px; }

.tabs { display: flex; gap: 6px; background: var(--card-soft); padding: 4px; border-radius: 12px; margin-bottom: 18px; }
.tab-btn {
  flex: 1; padding: 10px; border: none; border-radius: 9px; background: transparent;
  color: var(--text-dim); font-weight: 600; font-size: 13.5px; cursor: pointer; transition: all .2s;
}
.tab-btn.active { background: var(--accent-grad); color: #fff; box-shadow: 0 4px 14px rgba(124,108,255,0.35); }

.auth-form { display: flex; flex-direction: column; gap: 6px; }
.auth-form label { font-size: 12.5px; color: var(--text-dim); margin-top: 8px; }
.auth-form input {
  background: #0d1124; border: 1px solid var(--border); border-radius: 12px;
  padding: 13px 14px; color: var(--text); font-size: 15px; outline: none; transition: border .2s;
}
.auth-form input:focus { border-color: var(--accent); }
.btn-primary {
  margin-top: 16px; padding: 14px; border: none; border-radius: 12px; background: var(--accent-grad);
  color: #fff; font-weight: 700; font-size: 15px; cursor: pointer; box-shadow: 0 8px 24px rgba(124,108,255,0.35);
  transition: transform .15s;
}
.btn-primary:active { transform: scale(0.97); }
.hint { font-size: 12px; color: var(--text-dim); text-align: center; margin: 14px 0 0; line-height: 1.6; }
.auth-error {
  margin-top: 14px; background: rgba(255,84,112,0.12); border: 1px solid rgba(255,84,112,0.35);
  color: #ffb2c0; padding: 10px 12px; border-radius: 10px; font-size: 13px; text-align: center;
}

/* ---------------- شاشة الدردشة ---------------- */
.chat-header {
  display: flex; align-items: center; justify-content: space-between;
  padding: calc(12px + var(--safe-top)) 16px 12px;
  background: rgba(17,22,42,0.85); backdrop-filter: blur(14px);
  border-bottom: 1px solid var(--border);
  position: sticky; top: 0; z-index: 5;
}
.peer-info { display: flex; align-items: center; gap: 10px; }
.avatar {
  width: 42px; height: 42px; border-radius: 50%; background: var(--accent-grad);
  display: flex; align-items: center; justify-content: center; font-weight: 700; color: #fff;
  overflow: hidden; flex-shrink: 0; font-size: 16px;
}
.avatar img { width: 100%; height: 100%; object-fit: cover; }
.avatar.big { width: 96px; height: 96px; font-size: 34px; }
.peer-name { font-weight: 700; font-size: 15.5px; }
.peer-status { font-size: 12px; color: var(--text-dim); }
.peer-status.online { color: var(--success); }

.header-actions { display: flex; gap: 8px; }
.icon-btn {
  width: 40px; height: 40px; border-radius: 12px; border: 1px solid var(--border);
  background: var(--card-soft); color: var(--text); display: flex; align-items: center; justify-content: center;
  cursor: pointer; transition: background .15s, transform .1s;
}
.icon-btn svg { width: 19px; height: 19px; }
.icon-btn:active { transform: scale(0.92); }
.icon-btn.accent { background: var(--accent-grad); border-color: transparent; color: #fff; }

.messages {
  flex: 1; overflow-y: auto; padding: 14px 12px 10px; display: flex; flex-direction: column; gap: 6px;
  scroll-behavior: smooth;
}
.load-more {
  align-self: center; margin: 8px auto; padding: 8px 16px; border-radius: 20px; border: 1px solid var(--border);
  background: var(--card-soft); color: var(--text-dim); font-size: 12.5px; cursor: pointer;
}

.msg-row { display: flex; margin: 2px 0; max-width: 100%; }
.msg-row.me { justify-content: flex-start; flex-direction: row-reverse; }
.msg-row.other { justify-content: flex-start; }

.bubble {
  max-width: 72%; padding: 10px 13px; border-radius: 17px; font-size: 14.5px; line-height: 1.55;
  word-wrap: break-word; white-space: pre-wrap; box-shadow: 0 2px 10px rgba(0,0,0,0.15);
  animation: pop .18s ease;
}
@keyframes pop { from { transform: scale(0.92); opacity: 0; } to { transform: scale(1); opacity: 1; } }
.msg-row.me .bubble { background: var(--bubble-me); color: #fff; border-bottom-left-radius: 5px; }
.msg-row.other .bubble { background: var(--bubble-other); color: var(--text); border-bottom-right-radius: 5px; }

.bubble.media { padding: 4px; overflow: hidden; }
.bubble.media img, .bubble.media video { display: block; width: 100%; max-width: 260px; max-height: 320px; border-radius: 14px; object-fit: cover; cursor: pointer; background: #000; }

.msg-time { font-size: 10.5px; color: var(--text-dim); margin-top: 3px; text-align: center; }

.typing-indicator { padding: 4px 18px; font-size: 12px; color: var(--text-dim); }

.composer {
  display: flex; align-items: center; gap: 8px; padding: 10px 12px calc(10px + var(--safe-bottom));
  background: rgba(17,22,42,0.9); backdrop-filter: blur(14px); border-top: 1px solid var(--border);
}
.composer input[type="text"] {
  flex: 1; background: #0d1124; border: 1px solid var(--border); border-radius: 22px;
  padding: 12px 16px; color: var(--text); font-size: 15px; outline: none;
}
.composer input[type="text"]:focus { border-color: var(--accent); }
.send-btn {
  width: 42px; height: 42px; border-radius: 50%; border: none; background: var(--accent-grad);
  color: #fff; display: flex; align-items: center; justify-content: center; cursor: pointer; flex-shrink: 0;
  box-shadow: 0 6px 16px rgba(124,108,255,0.4);
}
.send-btn svg { width: 18px; height: 18px; transform: scaleX(-1); }

/* ---------------- المعرض ---------------- */
.gallery-header {
  display: flex; align-items: center; gap: 10px; padding: calc(12px + var(--safe-top)) 14px 10px;
  border-bottom: 1px solid var(--border);
}
.gallery-header h2 { margin: 0; font-size: 17px; }
.gallery-tabs { display: flex; gap: 8px; padding: 12px 14px 0; }
.gtab {
  padding: 8px 18px; border-radius: 18px; border: 1px solid var(--border); background: var(--card-soft);
  color: var(--text-dim); font-size: 13px; font-weight: 600; cursor: pointer;
}
.gtab.active { background: var(--accent-grad); color: #fff; border-color: transparent; }
.gallery-grid {
  flex: 1; overflow-y: auto; display: grid; grid-template-columns: repeat(3, 1fr); gap: 3px; padding: 12px;
}
.gallery-grid .g-item {
  aspect-ratio: 1; overflow: hidden; border-radius: 8px; position: relative; background: #10142a; cursor: pointer;
}
.gallery-grid .g-item img, .gallery-grid .g-item video { width: 100%; height: 100%; object-fit: cover; }
.gallery-grid .g-item.video::after {
  content: ""; position: absolute; inset: 0; margin: auto; width: 30px; height: 30px; border-radius: 50%;
  background: rgba(0,0,0,0.45);
}
.gallery-grid .g-item.video::before {
  content: ""; position: absolute; top: 50%; left: 50%; transform: translate(-40%, -50%);
  border-style: solid; border-width: 7px 0 7px 11px; border-color: transparent transparent transparent #fff; z-index: 1;
}

/* ---------------- شاشة المكالمة ---------------- */
.call-overlay {
  position: fixed; inset: 0; background: #05060d; z-index: 50; display: flex; flex-direction: column;
  align-items: center; justify-content: space-between;
}
.remote-video { position: absolute; inset: 0; width: 100%; height: 100%; object-fit: cover; background: #05060d; }
.local-video {
  position: absolute; bottom: calc(140px + var(--safe-bottom)); left: 16px; width: 96px; height: 128px;
  border-radius: 14px; object-fit: cover; border: 2px solid rgba(255,255,255,0.25); background: #111; z-index: 2;
  box-shadow: 0 8px 24px rgba(0,0,0,0.5);
}
.audio-call-face {
  position: absolute; inset: 0; display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 14px;
  background: radial-gradient(circle at 50% 30%, #1a1440 0%, #05060d 70%);
}
.audio-call-face .avatar.big { font-size: 40px; }
.call-peer-name { font-size: 20px; font-weight: 700; color: #fff; }
.call-top { position: absolute; top: calc(20px + var(--safe-top)); text-align: center; width: 100%; z-index: 2; }
.call-status { color: rgba(255,255,255,0.85); font-size: 14px; background: rgba(0,0,0,0.35); display: inline-block; padding: 6px 16px; border-radius: 20px; }

.incoming-panel, .call-controls {
  position: absolute; bottom: calc(40px + var(--safe-bottom)); z-index: 3; display: flex; flex-direction: column; align-items: center; gap: 10px;
}
.incoming-panel { width: 100%; }
.incoming-sub { color: var(--text-dim); font-size: 13px; }
.incoming-actions, .call-controls { flex-direction: row; gap: 26px; margin-top: 10px; }
.call-controls { bottom: calc(40px + var(--safe-bottom)); gap: 20px; }

.round-btn {
  width: 60px; height: 60px; border-radius: 50%; border: none; display: flex; align-items: center; justify-content: center;
  cursor: pointer; box-shadow: 0 8px 20px rgba(0,0,0,0.4);
}
.round-btn svg { width: 24px; height: 24px; color: #fff; }
.round-btn.accept { background: var(--success); }
.round-btn.decline { background: var(--danger); }
.round-btn.ghost { background: rgba(255,255,255,0.14); backdrop-filter: blur(6px); width: 54px; height: 54px; }
.round-btn.ghost.active-off { background: var(--danger); }

/* ---------------- عارض الوسائط ---------------- */
.media-viewer {
  position: fixed; inset: 0; background: rgba(0,0,0,0.94); z-index: 60; display: flex; align-items: center; justify-content: center;
}
.viewer-content img, .viewer-content video { max-width: 94vw; max-height: 88vh; border-radius: 8px; }
.viewer-close { position: absolute; top: calc(16px + var(--safe-top)); left: 16px; background: rgba(255,255,255,0.12); }

.toast {
  position: fixed; bottom: calc(24px + var(--safe-bottom)); left: 50%; transform: translateX(-50%);
  background: #1d2340; color: #fff; padding: 10px 18px; border-radius: 22px; font-size: 13.5px; z-index: 100;
  box-shadow: 0 10px 30px rgba(0,0,0,0.4); border: 1px solid var(--border);
}

::-webkit-scrollbar { width: 6px; }
::-webkit-scrollbar-thumb { background: rgba(255,255,255,0.15); border-radius: 3px; }
