:root {
  --bg: #111827;
  --bg-soft: #1a2332;
  --bg-panel: rgba(255, 255, 255, 0.06);
  --bg-panel-strong: rgba(255, 255, 255, 0.1);
  --text: #f3f4f6;
  --text-muted: #9ca3af;
  --accent: #22c55e;
  --accent-hover: #16a34a;
  --coral: #ff6b6b;
  --teal: #4ecdc4;
  --danger: #ef4444;
  --border: rgba(255, 255, 255, 0.12);
  --shadow: 0 18px 50px rgba(0, 0, 0, 0.35);
  --radius: 18px;
  --radius-sm: 12px;
  --font: "Tajawal", "Segoe UI", Tahoma, sans-serif;
  --hero-gradient: linear-gradient(135deg, #ff6b6b 0%, #4ecdc4 100%);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
}

*,
*::before,
*::after {
  box-sizing: border-box;
}

html,
body {
  margin: 0;
  min-height: 100%;
  min-height: 100dvh;
}

body {
  font-family: var(--font);
  background:
    radial-gradient(ellipse 80% 50% at 100% 0%, rgba(255, 107, 107, 0.18), transparent 55%),
    radial-gradient(ellipse 70% 45% at 0% 100%, rgba(78, 205, 196, 0.16), transparent 50%),
    var(--bg);
  color: var(--text);
  line-height: 1.7;
  -webkit-tap-highlight-color: transparent;
  overscroll-behavior-y: none;
}

.app-shell {
  min-height: 100vh;
  min-height: 100dvh;
}

button,
textarea,
input {
  font: inherit;
}

a {
  color: var(--teal);
  text-decoration: none;
}

a:hover {
  text-decoration: underline;
}

.sr-only {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  border: 0;
}

.app-shell {
  display: flex;
  flex-direction: column;
}

.app-shell[hidden] {
  display: none !important;
}

/* Topbar */
.topbar {
  display: flex;
  align-items: center;
  gap: 1rem;
  padding: 0.85rem 1.25rem;
  background: rgba(17, 24, 39, 0.88);
  border-bottom: 1px solid var(--border);
  backdrop-filter: blur(14px);
  position: sticky;
  top: 0;
  z-index: 40;
}

.topbar-brand {
  display: flex;
  align-items: center;
  gap: 0.75rem;
  min-width: 0;
}

.brand-logo {
  width: 44px;
  height: 44px;
  border-radius: 50%;
  object-fit: cover;
  background: #fff;
  box-shadow: 0 0 0 2px rgba(255, 255, 255, 0.2);
}

.brand-text {
  display: flex;
  flex-direction: column;
  line-height: 1.25;
}

.brand-text strong {
  font-size: 1.05rem;
  font-weight: 800;
}

.brand-text span {
  font-size: 0.8rem;
  color: var(--text-muted);
}

.topbar-nav {
  display: flex;
  gap: 0.4rem;
  margin-inline-start: auto;
}

.nav-btn {
  appearance: none;
  border: 1px solid transparent;
  background: transparent;
  color: var(--text-muted);
  padding: 0.55rem 0.9rem;
  border-radius: 999px;
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  gap: 0.35rem;
  transition: 0.2s ease;
}

.nav-btn:hover {
  color: var(--text);
  background: var(--bg-panel);
}

.nav-btn.is-active {
  color: #fff;
  background: var(--bg-panel-strong);
  border-color: var(--border);
}

.badge {
  background: var(--accent);
  color: #fff;
  font-size: 0.72rem;
  font-weight: 700;
  min-width: 1.25rem;
  height: 1.25rem;
  border-radius: 999px;
  display: inline-grid;
  place-items: center;
  padding: 0 0.35rem;
}

.badge[hidden] {
  display: none !important;
}

.topbar-wa {
  background: var(--accent);
  color: #fff !important;
  text-decoration: none !important;
  padding: 0.55rem 1rem;
  border-radius: 999px;
  font-weight: 700;
  font-size: 0.9rem;
  transition: background 0.2s ease, transform 0.2s ease;
}

.topbar-wa:hover {
  background: var(--accent-hover);
  transform: translateY(-1px);
}

/* Main */
main {
  flex: 1;
  width: min(1280px, 100%);
  margin: 0 auto;
  padding: 1.25rem;
}

.view[hidden] {
  display: none !important;
}

#view-chat {
  display: grid;
  grid-template-columns: minmax(280px, 340px) 1fr;
  gap: 1.25rem;
  min-height: calc(100vh - 160px);
  animation: fadeRise 0.45s ease both;
}

/* Profile */
.profile-panel {
  background: var(--hero-gradient);
  border-radius: calc(var(--radius) + 6px);
  padding: 1.5rem;
  color: #fff;
  box-shadow: var(--shadow);
  display: flex;
  flex-direction: column;
  gap: 1.25rem;
  position: relative;
  overflow: hidden;
}

.profile-panel::after {
  content: "";
  position: absolute;
  inset: auto -20% -30% 20%;
  height: 55%;
  background: radial-gradient(circle, rgba(255, 255, 255, 0.18), transparent 65%);
  pointer-events: none;
}

.profile-hero,
.profile-meta,
.profile-trust {
  position: relative;
  z-index: 1;
}

.profile-logo {
  width: 72px;
  height: 72px;
  border-radius: 50%;
  background: #fff;
  object-fit: cover;
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.2);
  animation: softPulse 3.2s ease-in-out infinite;
}

.profile-badge {
  display: inline-block;
  margin: 1rem 0 0.75rem;
  padding: 0.35rem 0.8rem;
  border-radius: 999px;
  background: rgba(255, 255, 255, 0.22);
  backdrop-filter: blur(6px);
  font-size: 0.8rem;
  font-weight: 500;
}

.profile-panel h1 {
  margin: 0;
  font-size: clamp(1.45rem, 2.2vw, 1.85rem);
  font-weight: 800;
  line-height: 1.35;
}

.profile-lead {
  margin: 0.65rem 0 0;
  font-size: 0.95rem;
  opacity: 0.95;
}

.profile-trust {
  list-style: none;
  margin: 0;
  padding: 0;
  display: grid;
  gap: 0.45rem;
}

.profile-trust li {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  font-size: 0.9rem;
  font-weight: 500;
}

.profile-trust li::before {
  content: "✓";
  width: 1.35rem;
  height: 1.35rem;
  border-radius: 50%;
  background: rgba(255, 255, 255, 0.25);
  display: grid;
  place-items: center;
  font-size: 0.75rem;
  flex-shrink: 0;
}

.profile-meta {
  margin-top: auto;
  padding-top: 1rem;
  border-top: 1px solid rgba(255, 255, 255, 0.28);
}

.profile-meta h2 {
  margin: 0;
  font-size: 1.05rem;
}

.profile-meta p {
  margin: 0.35rem 0 0;
  font-size: 0.88rem;
  opacity: 0.92;
}

.profile-contacts {
  display: flex;
  flex-direction: column;
  gap: 0.25rem;
  margin-top: 0.75rem;
}

.profile-contacts a {
  color: #fff;
  font-weight: 600;
  font-size: 0.9rem;
}

.profile-cr {
  opacity: 0.85 !important;
  font-size: 0.8rem !important;
}

/* Chat */
.chat-panel {
  background: var(--bg-soft);
  border: 1px solid var(--border);
  border-radius: calc(var(--radius) + 6px);
  display: flex;
  flex-direction: column;
  min-height: 620px;
  overflow: hidden;
  box-shadow: var(--shadow);
  animation: fadeRise 0.55s ease 0.08s both;
}

.chat-header {
  display: flex;
  align-items: center;
  gap: 0.85rem;
  padding: 1rem 1.15rem;
  border-bottom: 1px solid var(--border);
  background: rgba(255, 255, 255, 0.03);
}

.agent-avatar {
  width: 46px;
  height: 46px;
  border-radius: 50%;
  background: var(--hero-gradient);
  display: grid;
  place-items: center;
  font-weight: 800;
  font-size: 0.85rem;
  flex-shrink: 0;
  box-shadow: 0 0 0 3px rgba(78, 205, 196, 0.25);
}

.chat-header h2 {
  margin: 0;
  font-size: 1.05rem;
}

.chat-status {
  margin: 0.1rem 0 0;
  font-size: 0.8rem;
  color: var(--accent);
}

.progress-wrap {
  margin-inline-start: auto;
  display: flex;
  align-items: center;
  gap: 0.55rem;
  min-width: 120px;
}

.progress-track {
  flex: 1;
  height: 7px;
  border-radius: 999px;
  background: rgba(255, 255, 255, 0.1);
  overflow: hidden;
}

.progress-bar {
  height: 100%;
  background: var(--hero-gradient);
  border-radius: inherit;
  transition: width 0.45s ease;
}

#progress-text {
  font-size: 0.78rem;
  color: var(--text-muted);
  font-weight: 700;
  min-width: 2.2rem;
}

.chat-messages {
  flex: 1;
  overflow-y: auto;
  padding: 1.25rem;
  display: flex;
  flex-direction: column;
  gap: 0.9rem;
  scroll-behavior: smooth;
}

.msg {
  max-width: min(88%, 540px);
  animation: msgIn 0.35s ease both;
}

.msg-agent {
  align-self: flex-start;
}

.msg-user {
  align-self: flex-end;
}

.msg-bubble {
  padding: 0.85rem 1.05rem;
  border-radius: 18px;
  white-space: pre-wrap;
  word-break: break-word;
}

.msg-agent .msg-bubble {
  background: rgba(255, 255, 255, 0.08);
  border: 1px solid var(--border);
  border-top-right-radius: 6px;
}

.msg-user .msg-bubble {
  background: linear-gradient(135deg, rgba(255, 107, 107, 0.9), rgba(78, 205, 196, 0.9));
  color: #fff;
  border-top-left-radius: 6px;
  font-weight: 500;
}

.msg-meta {
  font-size: 0.72rem;
  color: var(--text-muted);
  margin-top: 0.3rem;
  padding-inline: 0.35rem;
}

.msg-benefit {
  margin-top: 0.65rem;
  padding: 0.7rem 0.85rem;
  border-radius: var(--radius-sm);
  background: rgba(34, 197, 94, 0.12);
  border: 1px solid rgba(34, 197, 94, 0.28);
  color: #bbf7d0;
  font-size: 0.88rem;
}

.typing {
  display: inline-flex;
  gap: 0.3rem;
  padding: 0.95rem 1.15rem;
}

.typing span {
  width: 7px;
  height: 7px;
  border-radius: 50%;
  background: var(--text-muted);
  animation: bounce 1.2s infinite ease-in-out;
}

.typing span:nth-child(2) { animation-delay: 0.15s; }
.typing span:nth-child(3) { animation-delay: 0.3s; }

.quick-replies {
  display: flex;
  flex-wrap: wrap;
  gap: 0.5rem;
  padding: 0 1.15rem 0.85rem;
}

.quick-replies[hidden] {
  display: none !important;
}

.chip {
  appearance: none;
  border: 1px solid var(--border);
  background: rgba(255, 255, 255, 0.05);
  color: var(--text);
  padding: 0.45rem 0.85rem;
  border-radius: 999px;
  cursor: pointer;
  font-size: 0.88rem;
  transition: 0.2s ease;
}

.chip:hover {
  border-color: var(--teal);
  background: rgba(78, 205, 196, 0.12);
  transform: translateY(-1px);
}

.chat-form {
  padding: 0.9rem 1.15rem calc(0.9rem + var(--safe-bottom));
  border-top: 1px solid var(--border);
  background: rgba(0, 0, 0, 0.18);
}

.composer {
  display: flex;
  gap: 0.65rem;
  align-items: flex-end;
}

.composer[hidden] {
  display: none !important;
}

.completion-panel {
  padding: 0.85rem 1.15rem 0.25rem;
}

.completion-panel[hidden] {
  display: none !important;
}

.completion-card {
  background: linear-gradient(135deg, rgba(34, 197, 94, 0.14), rgba(78, 205, 196, 0.12));
  border: 1px solid rgba(34, 197, 94, 0.35);
  border-radius: var(--radius);
  padding: 1.15rem 1.1rem;
  text-align: center;
  animation: fadeRise 0.4s ease both;
}

.completion-icon {
  width: 42px;
  height: 42px;
  margin: 0 auto 0.65rem;
  border-radius: 50%;
  display: grid;
  place-items: center;
  background: var(--accent);
  color: #fff;
  font-weight: 800;
  font-size: 1.2rem;
}

.completion-card h3 {
  margin: 0;
  font-size: 1.1rem;
}

.completion-card p {
  margin: 0.45rem 0 0;
  color: var(--text-muted);
  white-space: pre-line;
  font-size: 0.92rem;
}

.completion-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 0.55rem;
  justify-content: center;
  margin-top: 1rem;
}

.completion-actions .btn {
  min-width: 140px;
}

#chat-input {
  flex: 1;
  resize: none;
  min-height: 48px;
  max-height: 140px;
  border-radius: 14px;
  border: 1px solid var(--border);
  background: rgba(255, 255, 255, 0.05);
  color: var(--text);
  padding: 0.75rem 1rem;
  outline: none;
  font-size: 16px; /* يمنع تكبير iOS عند التركيز */
  transition: border-color 0.2s ease, box-shadow 0.2s ease;
}

#chat-input:focus {
  border-color: rgba(78, 205, 196, 0.55);
  box-shadow: 0 0 0 3px rgba(78, 205, 196, 0.15);
}

.send-btn {
  width: 48px;
  height: 48px;
  border: none;
  border-radius: 14px;
  background: var(--accent);
  color: #fff;
  cursor: pointer;
  display: grid;
  place-items: center;
  transition: background 0.2s ease, transform 0.2s ease;
  flex-shrink: 0;
}

.send-btn:hover {
  background: var(--accent-hover);
  transform: translateY(-1px);
}

.send-btn:disabled {
  opacity: 0.5;
  cursor: not-allowed;
  transform: none;
}

/* Requirements */
#view-requirements {
  animation: fadeRise 0.4s ease both;
}

.req-header {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 1rem;
  margin-bottom: 1.25rem;
}

.req-header h2 {
  margin: 0;
  font-size: 1.5rem;
}

.req-header p {
  margin: 0.35rem 0 0;
  color: var(--text-muted);
}

.req-list {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(300px, 1fr));
  gap: 1rem;
}

.req-card {
  background: var(--bg-soft);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 1.15rem;
  cursor: pointer;
  transition: transform 0.2s ease, border-color 0.2s ease, box-shadow 0.2s ease;
}

.req-card:hover {
  transform: translateY(-3px);
  border-color: rgba(78, 205, 196, 0.45);
  box-shadow: var(--shadow);
}

.req-card h3 {
  margin: 0 0 0.4rem;
  font-size: 1.1rem;
}

.req-card .req-meta {
  color: var(--text-muted);
  font-size: 0.85rem;
  margin: 0;
}

.req-tags {
  display: flex;
  flex-wrap: wrap;
  gap: 0.4rem;
  margin-top: 0.85rem;
}

.tag {
  font-size: 0.75rem;
  padding: 0.25rem 0.6rem;
  border-radius: 999px;
  background: rgba(78, 205, 196, 0.12);
  color: #99f6e4;
  border: 1px solid rgba(78, 205, 196, 0.25);
}

.req-empty {
  text-align: center;
  padding: 4rem 1rem;
  background: var(--bg-soft);
  border: 1px dashed var(--border);
  border-radius: var(--radius);
}

.req-empty p {
  color: var(--text-muted);
  margin: 0 0 1rem;
}

/* Buttons */
.btn {
  appearance: none;
  border: 1px solid transparent;
  border-radius: 999px;
  padding: 0.65rem 1.15rem;
  cursor: pointer;
  font-weight: 700;
  transition: 0.2s ease;
}

.btn-accent {
  background: var(--accent);
  color: #fff;
}

.btn-accent:hover {
  background: var(--accent-hover);
}

.btn-danger {
  background: rgba(239, 68, 68, 0.15);
  color: #fecaca;
  border-color: rgba(239, 68, 68, 0.35);
}

.btn-danger:hover {
  background: rgba(239, 68, 68, 0.28);
}

.btn-ghost {
  background: transparent;
  color: var(--text-muted);
  border-color: var(--border);
}

.btn-ghost:hover {
  color: var(--text);
  background: var(--bg-panel);
}

.icon-btn {
  appearance: none;
  border: none;
  background: transparent;
  color: var(--text-muted);
  font-size: 1.6rem;
  line-height: 1;
  cursor: pointer;
  padding: 0.2rem 0.45rem;
}

.icon-btn:hover {
  color: var(--text);
}

/* Modal */
.modal[hidden] {
  display: none !important;
}

.modal {
  position: fixed;
  inset: 0;
  z-index: 80;
  display: grid;
  place-items: center;
  padding: 1rem;
}

.modal-backdrop {
  position: absolute;
  inset: 0;
  background: rgba(0, 0, 0, 0.65);
  backdrop-filter: blur(4px);
}

.modal-card {
  position: relative;
  width: min(720px, 100%);
  max-height: min(88vh, 900px);
  background: #152033;
  border: 1px solid var(--border);
  border-radius: calc(var(--radius) + 4px);
  box-shadow: var(--shadow);
  display: flex;
  flex-direction: column;
  overflow: hidden;
  animation: fadeRise 0.3s ease;
}

.modal-header,
.modal-footer {
  display: flex;
  align-items: center;
  gap: 0.65rem;
  padding: 1rem 1.15rem;
  border-bottom: 1px solid var(--border);
}

.modal-footer {
  border-bottom: none;
  border-top: 1px solid var(--border);
  flex-wrap: wrap;
}

.modal-header h3 {
  margin: 0;
  flex: 1;
}

.modal-body {
  padding: 1.15rem;
  overflow-y: auto;
}

.detail-grid {
  display: grid;
  gap: 0.85rem;
}

.detail-item {
  padding: 0.85rem 1rem;
  border-radius: var(--radius-sm);
  background: rgba(255, 255, 255, 0.04);
  border: 1px solid var(--border);
}

.detail-item dt {
  margin: 0;
  font-size: 0.78rem;
  color: var(--text-muted);
  font-weight: 700;
}

.detail-item dd {
  margin: 0.25rem 0 0;
  font-size: 0.98rem;
}

/* Footer */
.app-footer {
  display: flex;
  justify-content: space-between;
  gap: 1rem;
  flex-wrap: wrap;
  padding: 1rem 1.25rem calc(1rem + var(--safe-bottom));
  border-top: 1px solid var(--border);
  color: var(--text-muted);
  font-size: 0.85rem;
  background: rgba(17, 24, 39, 0.75);
}

/* PDF template */
.pdf-root {
  position: fixed;
  left: -9999px;
  top: 0;
  width: 794px;
  background: #fff;
  color: #111;
  font-family: var(--font);
  direction: rtl;
  padding: 36px 40px;
}

.pdf-doc h1 {
  margin: 0 0 0.35rem;
  color: #111827;
  font-size: 22px;
}

.pdf-doc .pdf-sub {
  margin: 0 0 1.25rem;
  color: #4b5563;
  font-size: 13px;
}

.pdf-banner {
  background: linear-gradient(135deg, #ff6b6b, #4ecdc4);
  color: #fff;
  border-radius: 14px;
  padding: 18px 20px;
  margin-bottom: 18px;
  display: flex;
  gap: 14px;
  align-items: center;
}

.pdf-banner img {
  width: 54px;
  height: 54px;
  border-radius: 50%;
  background: #fff;
}

.pdf-banner strong {
  display: block;
  font-size: 18px;
}

.pdf-banner span {
  font-size: 12px;
  opacity: 0.95;
}

.pdf-table {
  width: 100%;
  border-collapse: collapse;
  font-size: 13px;
}

.pdf-table th,
.pdf-table td {
  border: 1px solid #e5e7eb;
  padding: 10px 12px;
  text-align: right;
  vertical-align: top;
}

.pdf-table th {
  width: 28%;
  background: #f3f4f6;
  color: #374151;
  font-weight: 700;
}

.pdf-footer {
  margin-top: 18px;
  padding-top: 12px;
  border-top: 1px solid #e5e7eb;
  font-size: 11px;
  color: #6b7280;
}

/* Animations */
@keyframes fadeRise {
  from {
    opacity: 0;
    transform: translateY(10px);
  }
  to {
    opacity: 1;
    transform: none;
  }
}

@keyframes msgIn {
  from {
    opacity: 0;
    transform: translateY(8px) scale(0.98);
  }
  to {
    opacity: 1;
    transform: none;
  }
}

@keyframes bounce {
  0%, 80%, 100% { transform: translateY(0); opacity: 0.5; }
  40% { transform: translateY(-5px); opacity: 1; }
}

@keyframes softPulse {
  0%, 100% { transform: scale(1); }
  50% { transform: scale(1.04); }
}

/* Responsive */
@media (max-width: 960px) {
  #view-chat {
    grid-template-columns: 1fr;
  }

  .profile-panel {
    min-height: auto;
    padding: 1.15rem;
  }

  .profile-panel h1 {
    font-size: 1.35rem;
  }

  .chat-panel {
    min-height: calc(100dvh - 280px);
  }
}

@media (max-width: 720px) {
  main {
    padding: 0.65rem;
    width: 100%;
  }

  .topbar {
    flex-wrap: wrap;
    gap: 0.55rem;
    padding: 0.65rem 0.75rem;
  }

  .brand-logo {
    width: 36px;
    height: 36px;
  }

  .brand-text strong {
    font-size: 0.95rem;
  }

  .brand-text span {
    font-size: 0.72rem;
  }

  .topbar-nav {
    order: 3;
    width: 100%;
    margin: 0;
  }

  .nav-btn {
    flex: 1;
    justify-content: center;
    min-height: 42px;
    padding: 0.5rem 0.65rem;
    font-size: 0.9rem;
  }

  .topbar-wa {
    margin-inline-start: auto;
    padding: 0.45rem 0.85rem;
    font-size: 0.85rem;
  }

  .progress-wrap {
    min-width: 90px;
  }

  #progress-text {
    display: none;
  }

  /* على الجوال: بطاقة تعريف مختصرة ثم المحادثة كاملة */
  .profile-panel {
    display: none;
  }

  #view-chat {
    min-height: calc(100dvh - 130px);
  }

  .chat-panel {
    min-height: calc(100dvh - 130px);
    border-radius: 16px;
  }

  .chat-header {
    padding: 0.75rem 0.85rem;
  }

  .agent-avatar {
    width: 40px;
    height: 40px;
  }

  .chat-messages {
    padding: 0.85rem;
    gap: 0.75rem;
  }

  .msg {
    max-width: 94%;
  }

  .msg-bubble {
    padding: 0.75rem 0.9rem;
    font-size: 0.95rem;
  }

  .chip {
    min-height: 40px;
    padding: 0.5rem 0.9rem;
    font-size: 0.88rem;
  }

  .chat-form {
    padding: 0.7rem 0.75rem calc(0.7rem + var(--safe-bottom));
    gap: 0.5rem;
  }

  .send-btn {
    width: 48px;
    height: 48px;
  }

  .req-header {
    flex-direction: column;
  }

  .req-list {
    grid-template-columns: 1fr;
  }

  .app-footer {
    font-size: 0.75rem;
    padding: 0.75rem;
  }

  .modal {
    padding: 0.5rem;
    align-items: flex-end;
  }

  .modal-card {
    width: 100%;
    max-height: 92dvh;
    border-radius: 18px 18px 12px 12px;
  }

  .modal-footer .btn {
    flex: 1;
    text-align: center;
    min-height: 44px;
  }
}

@media (max-width: 420px) {
  .brand-text span {
    display: none;
  }

  .chat-header h2 {
    font-size: 0.95rem;
  }
}
