/* =============================================================================
   WADIFA BUILDER - MAIN STYLESHEET v3.0
   World-class design system inspired by Linear, Vercel, Apple.
   - Design Tokens (CSS Custom Properties)
   - Typography (Cairo + Inter)
   - Layout System
   - Dark / Light Theme
   - RTL / LTR Support
   - Animations & Transitions
============================================================================= */

/* === GOOGLE FONTS LOADED VIA <head> LINK === */

/* ─────────────────────────────────────────────
   1. CSS CUSTOM PROPERTIES — DESIGN TOKENS
───────────────────────────────────────────── */
:root {
  /* Brand palette */
  --brand-500: #2563eb;
  --brand-600: #1d4ed8;
  --brand-700: #1e40af;
  --brand-100: #dbeafe;
  --brand-50:  #eff6ff;

  /* Surface */
  --surface-bg:        #f8fafc;
  --surface-card:      #ffffff;
  --surface-hover:     #f1f5f9;
  --surface-active:    #e8f0fe;
  --surface-sidebar:   #ffffff;
  --surface-header:    rgba(255,255,255,0.88);
  --surface-muted:     #f1f5f9;
  --surface-border:    #e2e8f0;
  --surface-border-2:  #cbd5e1;

  /* Text */
  --text-primary:   #0f172a;
  --text-secondary: #475569;
  --text-muted:     #94a3b8;
  --text-inverted:  #ffffff;

  /* State colors */
  --success: #10b981;
  --warning: #f59e0b;
  --error:   #ef4444;
  --info:    #3b82f6;

  /* Radii */
  --radius-sm:  6px;
  --radius-md:  10px;
  --radius-lg:  16px;
  --radius-xl:  24px;
  --radius-full: 9999px;

  /* Shadows */
  --shadow-xs:   0 1px 2px rgba(0,0,0,.05);
  --shadow-sm:   0 2px 8px rgba(0,0,0,.08), 0 1px 2px rgba(0,0,0,.04);
  --shadow-md:   0 4px 24px rgba(0,0,0,.10), 0 2px 8px rgba(0,0,0,.06);
  --shadow-lg:   0 12px 48px rgba(0,0,0,.12), 0 4px 16px rgba(0,0,0,.08);
  --shadow-brand: 0 4px 24px rgba(37,99,235,.28);

  /* Transitions */
  --transition-fast:   120ms cubic-bezier(.4,0,.2,1);
  --transition-mid:    200ms cubic-bezier(.4,0,.2,1);
  --transition-slow:   320ms cubic-bezier(.4,0,.2,1);
  --transition-spring: 450ms cubic-bezier(.175,.885,.32,1.275);

  /* Typography */
  --font-ui:  'Inter', 'Cairo', system-ui, sans-serif;
  --font-ar:  'Cairo', 'Segoe UI', sans-serif;
  --font-doc: 'Outfit', 'Inter', sans-serif;

  /* Spacing */
  --space-1: 0.25rem;  /* 4px */
  --space-2: 0.5rem;   /* 8px */
  --space-3: 0.75rem;  /* 12px */
  --space-4: 1rem;     /* 16px */
  --space-5: 1.25rem;  /* 20px */
  --space-6: 1.5rem;   /* 24px */
  --space-8: 2rem;     /* 32px */
  --space-10: 2.5rem;  /* 40px */
  --space-12: 3rem;    /* 48px */

  /* Layout */
  --header-h: 60px;
  --header-ad-h: 56px;
  --sidebar-w: 420px;
  --mobile-bar-h: 64px;
}

/* ─── DARK THEME ─────────────────────────────── */
[data-theme="dark"] {
  --surface-bg:        #0b0f1a;
  --surface-card:      #141928;
  --surface-hover:     #1c2338;
  --surface-active:    #1e2f5c;
  --surface-sidebar:   #111827;
  --surface-header:    rgba(11,15,26,0.92);
  --surface-muted:     #1e2433;
  --surface-border:    #1e293b;
  --surface-border-2:  #334155;

  --text-primary:   #f1f5f9;
  --text-secondary: #94a3b8;
  --text-muted:     #64748b;
  --text-inverted:  #0f172a;

  --shadow-sm:   0 2px 8px rgba(0,0,0,.4);
  --shadow-md:   0 4px 24px rgba(0,0,0,.5);
  --shadow-lg:   0 12px 48px rgba(0,0,0,.6);
}

/* ─────────────────────────────────────────────
   2. RESET & BASE
───────────────────────────────────────────── */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

html {
  font-size: 16px;
  -webkit-text-size-adjust: 100%;
  scroll-behavior: smooth;
  height: 100%;
}

body {
  font-family: var(--font-ui);
  background: var(--surface-bg);
  color: var(--text-primary);
  line-height: 1.6;
  min-height: 100%;
  overflow-x: hidden;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  transition: background-color var(--transition-mid), color var(--transition-mid);
}

/* Arabic body */
[lang="ar"] body, [lang="ar"] .builder-sidebar, [lang="ar"] .doc-tab span {
  font-family: var(--font-ar);
}

img { max-width: 100%; height: auto; display: block; }
a { text-decoration: none; color: inherit; }
button { cursor: pointer; border: none; background: none; font: inherit; }
select { font: inherit; }

/* Focus ring */
:focus-visible {
  outline: 2px solid var(--brand-500);
  outline-offset: 2px;
  border-radius: 4px;
}

/* Utility */
.hidden { display: none !important; }
.sr-only { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); }

/* ─────────────────────────────────────────────
   3. APP HEADER
───────────────────────────────────────────── */
.app-header {
  position: sticky;
  top: 0;
  z-index: 100;
  height: var(--header-h);
  background: var(--surface-header);
  backdrop-filter: blur(16px) saturate(180%);
  -webkit-backdrop-filter: blur(16px) saturate(180%);
  border-bottom: 1px solid var(--surface-border);
  display: flex;
  align-items: center;
  gap: var(--space-4);
  padding: 0 var(--space-5);
  transition: background var(--transition-mid), border-color var(--transition-mid);
}

.brand-link {
  display: flex;
  align-items: center;
  gap: 10px;
  flex-shrink: 0;
  text-decoration: none;
}
.brand-icon {
  width: 36px;
  height: 36px;
  object-fit: contain;
  border-radius: 8px;
}
.brand-logo-img {
  height: 26px;
  object-fit: contain;
}

/* Doc-type tabs */
.header-doc-tabs {
  display: flex;
  gap: 4px;
  background: var(--surface-muted);
  border-radius: var(--radius-md);
  padding: 3px;
}
.doc-tab {
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 5px 14px;
  border-radius: 7px;
  font-size: 13px;
  font-weight: 500;
  color: var(--text-secondary);
  transition: all var(--transition-fast);
  white-space: nowrap;
}
.doc-tab svg { flex-shrink: 0; }
.doc-tab:hover { color: var(--text-primary); background: var(--surface-hover); }
.doc-tab.active {
  background: var(--surface-card);
  color: var(--brand-500);
  box-shadow: var(--shadow-xs);
  font-weight: 600;
}
[data-theme="dark"] .doc-tab.active { background: var(--surface-hover); }

/* Header Nav */
.header-nav { flex: 1; display: flex; align-items: center; }

/* Header Actions */
.header-actions {
  display: flex;
  align-items: center;
  gap: 6px;
  flex-shrink: 0;
}

.btn-group { display: flex; }
.btn-group .btn-icon-sm:first-child { border-radius: var(--radius-sm) 0 0 var(--radius-sm); }
.btn-group .btn-icon-sm:last-child  { border-radius: 0 var(--radius-sm) var(--radius-sm) 0; border-left: 1px solid var(--surface-border); }

.btn-icon-sm {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 32px;
  height: 32px;
  border-radius: var(--radius-sm);
  border: 1px solid var(--surface-border);
  color: var(--text-secondary);
  background: var(--surface-card);
  transition: all var(--transition-fast);
}
.btn-icon-sm:hover { background: var(--surface-hover); color: var(--text-primary); border-color: var(--surface-border-2); }

.btn-header-sm {
  display: flex;
  align-items: center;
  gap: 5px;
  height: 32px;
  padding: 0 10px;
  border-radius: var(--radius-sm);
  border: 1px solid var(--surface-border);
  background: var(--surface-card);
  color: var(--text-secondary);
  font-size: 12px;
  font-weight: 500;
  transition: all var(--transition-fast);
}
.btn-header-sm:hover { background: var(--surface-hover); color: var(--text-primary); border-color: var(--surface-border-2); }

.btn-ai-header {
  display: flex;
  align-items: center;
  gap: 6px;
  height: 32px;
  padding: 0 12px;
  border-radius: var(--radius-sm);
  background: linear-gradient(135deg, #7c3aed 0%, #2563eb 100%);
  color: #fff;
  font-size: 12px;
  font-weight: 600;
  border: none;
  box-shadow: 0 2px 8px rgba(124,58,237,0.4);
  transition: all var(--transition-fast);
}
.btn-ai-header:hover { transform: translateY(-1px); box-shadow: 0 4px 16px rgba(124,58,237,0.5); }

.header-divider {
  width: 1px; height: 22px;
  background: var(--surface-border);
  flex-shrink: 0;
}

/* Language selector */
.lang-select-wrapper {
  position: relative;
  display: flex;
  align-items: center;
}
.lang-select-wrapper > svg {
  position: absolute;
  left: 8px;
  color: var(--text-muted);
  pointer-events: none;
  z-index: 1;
}
[dir="rtl"] .lang-select-wrapper > svg { left: auto; right: 8px; }
.lang-select {
  appearance: none;
  height: 32px;
  padding: 0 10px 0 28px;
  border-radius: var(--radius-sm);
  border: 1px solid var(--surface-border);
  background: var(--surface-card);
  color: var(--text-primary);
  font-size: 12.5px;
  font-weight: 500;
  cursor: pointer;
  transition: all var(--transition-fast);
}
[dir="rtl"] .lang-select { padding: 0 28px 0 10px; }
.lang-select:hover { border-color: var(--surface-border-2); }

/* PDF Download CTA */
.btn-download-pdf {
  display: flex;
  align-items: center;
  gap: 6px;
  height: 34px;
  padding: 0 16px;
  border-radius: var(--radius-sm);
  background: var(--brand-500);
  color: #fff;
  font-size: 13px;
  font-weight: 600;
  border: none;
  box-shadow: var(--shadow-brand);
  transition: all var(--transition-fast);
}
.btn-download-pdf:hover {
  background: var(--brand-600);
  transform: translateY(-1px);
  box-shadow: 0 6px 28px rgba(37,99,235,.4);
}
.btn-download-pdf:active { transform: translateY(0); }

/* Theme toggle */
.theme-btn { position: relative; }
.theme-btn svg { transition: opacity var(--transition-fast); }

/* Header Ad Banner */
.header-ad-banner {
  background: var(--surface-card);
  border-bottom: 1px solid var(--surface-border);
  min-height: 56px;
  display: flex;
  align-items: center;
  justify-content: center;
  overflow: hidden;
}

/* ─────────────────────────────────────────────
   4. MAIN LAYOUT
───────────────────────────────────────────── */
.app-main {
  display: grid;
  grid-template-columns: var(--sidebar-w) 1fr;
  height: calc(100vh - var(--header-h) - var(--header-ad-h));
  overflow: hidden;
}

.builder-sidebar {
  display: flex;
  flex-direction: column;
  background: var(--surface-sidebar);
  border-inline-end: 1px solid var(--surface-border);
  overflow: hidden;
  transition: background var(--transition-mid);
}

.sidebar-scroll {
  flex: 1;
  overflow-y: auto;
  overflow-x: hidden;
  padding: var(--space-4);
  scrollbar-width: thin;
  scrollbar-color: var(--surface-border-2) transparent;
}
.sidebar-scroll::-webkit-scrollbar { width: 4px; }
.sidebar-scroll::-webkit-scrollbar-track { background: transparent; }
.sidebar-scroll::-webkit-scrollbar-thumb { background: var(--surface-border-2); border-radius: 2px; }

.preview-pane {
  overflow-y: auto;
  overflow-x: hidden;
  background: var(--surface-bg);
  position: relative;
  scrollbar-width: thin;
  scrollbar-color: var(--surface-border-2) transparent;
}
.preview-pane::-webkit-scrollbar { width: 6px; }
.preview-pane::-webkit-scrollbar-track { background: transparent; }
.preview-pane::-webkit-scrollbar-thumb { background: var(--surface-border-2); border-radius: 3px; }

/* Sidebar Ad Slot */
.sidebar-ad-slot {
  padding: var(--space-3) var(--space-4);
  background: var(--surface-muted);
  border-top: 1px solid var(--surface-border);
  min-height: 90px;
}

/* Loading skeleton */
.sidebar-loading { padding: var(--space-4); display: flex; flex-direction: column; gap: 12px; }
.loading-pulse {
  height: 40px;
  background: linear-gradient(90deg, var(--surface-border) 25%, var(--surface-hover) 50%, var(--surface-border) 75%);
  background-size: 200% 100%;
  border-radius: var(--radius-md);
  animation: skeleton-pulse 1.4s ease-in-out infinite;
}
@keyframes skeleton-pulse {
  0%   { background-position: 200% 0; }
  100% { background-position: -200% 0; }
}

/* ─────────────────────────────────────────────
   5. MOBILE BOTTOM NAV
───────────────────────────────────────────── */
.mobile-bottom-bar {
  display: none;
  position: fixed;
  bottom: 0; left: 0; right: 0;
  height: var(--mobile-bar-h);
  background: var(--surface-card);
  border-top: 1px solid var(--surface-border);
  z-index: 200;
  padding: 0 var(--space-4);
  justify-content: space-around;
  align-items: center;
  backdrop-filter: blur(12px);
}
.mobile-tab {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 3px;
  padding: var(--space-2) var(--space-4);
  border-radius: var(--radius-md);
  color: var(--text-secondary);
  font-size: 11px;
  font-weight: 500;
  transition: all var(--transition-fast);
}
.mobile-tab.active { color: var(--brand-500); }
.mobile-tab:hover { background: var(--surface-hover); }

/* ─────────────────────────────────────────────
   6. ANIMATIONS
───────────────────────────────────────────── */
@keyframes fade-in {
  from { opacity: 0; transform: translateY(6px); }
  to   { opacity: 1; transform: translateY(0); }
}
@keyframes slide-up {
  from { opacity: 0; transform: translateY(20px); }
  to   { opacity: 1; transform: translateY(0); }
}
@keyframes spin {
  to { transform: rotate(360deg); }
}

.animate-fade-in { animation: fade-in var(--transition-mid) both; }

/* ─────────────────────────────────────────────
   7. RESPONSIVE
───────────────────────────────────────────── */
@media (max-width: 1024px) {
  :root { --sidebar-w: 360px; }
}

@media (max-width: 768px) {
  .app-main {
    grid-template-columns: 1fr;
    height: calc(100vh - var(--header-h) - var(--header-ad-h) - var(--mobile-bar-h));
  }
  .mobile-bottom-bar { display: flex; }
  .builder-sidebar { border-inline-end: none; }
  .header-nav { display: none; }
  .header-actions .btn-ai-header { display: none; }
  .header-actions .btn-group { display: none; }
  .header-actions .btn-header-sm { display: none; }
  .brand-logo-img { display: none; }
}

@media (max-width: 480px) {
  .app-header { padding: 0 var(--space-3); gap: var(--space-2); }
  .lang-select { max-width: 90px; }
  .btn-download-pdf span { display: none; }
  .btn-download-pdf { width: 34px; padding: 0; justify-content: center; }
}
