:root {
    color-scheme: only light;
    --bg: #0B1220; --surface: rgba(30,41,59,0.6); --surface-solid: #141B2C;
    --border: rgba(212,180,120,0.22); --border-soft: rgba(255,255,255,0.07);
    --text: #E6EAF3; --dim: #93A0B5;
    --gold: #C9A45E; --gold-deep: #B08D4F; --gold-soft: rgba(212,180,120,0.14);
    --violet: #A67C52; --danger: #FF7A7A;
  }
  body.light {
    --bg: #F1F5F9; --surface: #FFFFFF; --surface-solid: #FFFFFF;
    --border: rgba(15,23,42,0.08); --border-soft: rgba(15,23,42,0.08);
    --text: #1E293B; --dim: #64748B;
    --gold: #9C742F; --gold-deep: #B08D4F; --gold-soft: #EBF4FF;
    --header-bg: rgba(255,255,255,0.92);
    --code-bg: #1E293B;
    --card-bg: #FFFFFF;
    --overlay-bg: rgba(15,23,42,0.45);
    --sidebar-bg: #FFFFFF;
    --shell-bg: #F1F5F9;
  }
  :root { --sidebar-bg: #141B2C; --shell-bg: #0B1220; }
  body { transition: background .35s, color .35s; }
  body.light .cta:disabled { background: rgba(15,23,42,0.08); color: var(--dim); }
  body.light .orb1 { background: radial-gradient(circle, rgba(212,180,120,0.06), transparent 65%); }
  body.light .orb2 { background: radial-gradient(circle, rgba(176,141,79,0.06), transparent 65%); }
  body.light .wordmark { background: linear-gradient(90deg, #B08D4F, #9C742F); -webkit-background-clip: text; background-clip: text; }
  body.light .bubble, body.light .majlis-card { box-shadow: 0 3px 14px rgba(30,41,59,0.06); }
  body.light .bubble pre { background: #0F172A; border-color: rgba(15,23,42,0.1); }
  body.light .bubble pre code { background: transparent; }
  body.light .copy-btn { background: rgba(255,255,255,0.08); }

  * { box-sizing: border-box; margin: 0; }
  html, body { height: 100%; }
  body {
    background: var(--shell-bg) !important; color: var(--text);
    font-family: 'Tajawal', 'Inter', system-ui, sans-serif;
    min-height: 100vh; overflow-x: hidden;
  }
  .orb { position: fixed; border-radius: 50%; pointer-events: none; z-index: 0; }
  .orb1 { top: -15%; left: -10%; width: 420px; height: 420px;
    background: radial-gradient(circle, rgba(212,180,120,0.16), transparent 65%);
    animation: drift1 16s ease-in-out infinite; }
  .orb2 { bottom: -20%; right: -12%; width: 480px; height: 480px;
    background: radial-gradient(circle, rgba(176,141,79,0.13), transparent 65%);
    animation: drift2 20s ease-in-out infinite; }
  @keyframes drift1 { 50% { transform: translate(-60px, 40px) scale(1.15); } }
  @keyframes drift2 { 50% { transform: translate(50px, -50px) scale(0.9); } }
  @keyframes rise { from { opacity: 0; transform: translateY(10px) scale(.98); } to { opacity: 1; transform: translateY(0) scale(1); } }
  @keyframes pulse { 0%,100% { opacity: .3 } 50% { opacity: 1 } }
  @keyframes shimmer { 0% { background-position: -200% center } 100% { background-position: 200% center } }
  @keyframes sparkle { 0%,100% { transform: scale(1) rotate(0deg); } 50% { transform: scale(1.08) rotate(6deg); } }
  @keyframes togglePop { 0% { transform: scale(.92); } 100% { transform: scale(1); } }
  @keyframes slideInUp { from { opacity: 0; transform: translateY(16px); } to { opacity: 1; transform: translateY(0); } }
  @media (prefers-reduced-motion: reduce) {
    .orb1,.orb2,.msg,.cta,.brand svg,.hero svg,.majlis-toggle.on,.new-chat-btn,.composer-wrap { animation: none !important; transition: none !important; }
  }

  .cta {
    background: linear-gradient(110deg, var(--gold-deep) 25%, #F0D9A8 50%, var(--gold-deep) 75%);
    background-size: 200% auto; animation: shimmer 3s linear infinite;
    color: #241A06; border: none; font-weight: 700; cursor: pointer;
    border-radius: 13px; font-family: inherit; font-size: 15px;
  }
  .cta:disabled { background: rgba(255,255,255,0.06); color: var(--dim); animation: none; cursor: default; }
  .cta.pill { border-radius: 999px; padding: 10px 16px; }
  .cta.pill.full { width: 100%; }
  .cta { transition: transform .15s ease; }
  .cta:hover:not(:disabled) { transform: translateY(-2px); }
  .cta:active:not(:disabled) { transform: translateY(0) scale(.97); }
  button:focus-visible, input:focus, textarea:focus { outline: 2px solid var(--gold); outline-offset: 2px; }
  .brand svg, .hero svg { animation: sparkle 3.5s ease-in-out infinite; transform-origin: center; }

  /* ---------- App shell ---------- */
  .app-shell { display: flex; min-height: 100vh; position: relative; z-index: 1; }

  .brand { display: flex; align-items: center; gap: 10px; }
  .wordmark {
    font-family: 'Marcellus', serif; font-size: 20px; letter-spacing: 0.12em;
    background: linear-gradient(90deg, #EFDDB4, #C9A45E, #B08D4F);
    -webkit-background-clip: text; background-clip: text; color: transparent;
  }
  .tagline { font-size: 10px; color: var(--dim); letter-spacing: 0.2em; }

  .pill {
    padding: 8px 16px; border-radius: 999px; font-size: 13px; font-weight: 700; font-family: inherit;
    border: none; cursor: pointer;
  }
  .pill.pro { background: var(--gold-soft); border: 1px solid var(--gold); color: var(--gold); padding: 4px 10px; }

  /* ---------- Sidebar ---------- */
  .sidebar {
    width: 290px; flex-shrink: 0; background: var(--sidebar-bg);
    border-left: 1px solid var(--border-soft);
    display: flex; flex-direction: column;
    position: fixed; top: 0; bottom: 0; right: -300px; z-index: 60;
    transition: right .3s ease; box-shadow: -10px 0 40px rgba(0,0,0,0.2);
  }
  .sidebar.open { right: 0; }
  .sidebar-top { display: flex; align-items: center; justify-content: space-between; padding: 18px 18px 6px; }

  .sidebar-actions { display: flex; gap: 10px; padding: 14px 18px 8px; }
  .new-chat-btn {
    flex: 1; display: flex; align-items: center; justify-content: center; gap: 8px;
    padding: 12px; border-radius: 999px; border: none;
    background: linear-gradient(115deg, var(--gold-deep), var(--gold)); color: #fff;
    font-weight: 700; font-size: 14px; cursor: pointer; font-family: inherit;
    box-shadow: 0 6px 16px rgba(176,141,79,0.25);
    transition: transform .15s ease, box-shadow .15s ease;
  }
  .new-chat-btn:hover { transform: translateY(-2px); box-shadow: 0 10px 20px rgba(176,141,79,0.32); }
  .new-chat-btn:active { transform: translateY(0) scale(.97); }
  .icon-btn-round {
    width: 44px; height: 44px; border-radius: 999px; border: none; flex-shrink: 0;
    background: var(--gold-soft); color: var(--gold); font-size: 16px; cursor: pointer;
    display: flex; align-items: center; justify-content: center;
    transition: transform .15s ease;
  }
  .icon-btn-round:hover { transform: scale(1.08); }
  .icon-btn-round:active { transform: scale(.94); }
  .icon-btn-round.small { width: 32px; height: 32px; font-size: 14px; background: transparent; border: 1px solid var(--border-soft); color: var(--dim); }
  .icon-btn-round.small:hover { color: var(--danger); border-color: var(--danger); }

  .sidebar-search { padding: 0 18px 6px; }
  .sidebar-search input {
    width: 100%; padding: 9px 12px; border-radius: 10px; border: 1px solid var(--border-soft);
    background: var(--gold-soft); color: var(--text); font-size: 13px; font-family: inherit;
  }

  .sidebar-section-head {
    display: flex; align-items: center; justify-content: space-between;
    padding: 12px 18px 4px; font-size: 12px; color: var(--dim);
  }
  .link-btn.small { background: none; border: none; color: var(--gold); font-size: 13px; font-weight: 700; cursor: pointer; font-family: inherit; }

  .conv-list { flex: 1; overflow-y: auto; padding: 4px 10px 8px; }
  .conv-group-label { font-size: 11px; color: var(--dim); padding: 10px 8px 4px; }
  .conv-item {
    display: flex; align-items: center; gap: 10px; padding: 11px 10px; border-radius: 11px;
    cursor: pointer; font-size: 14px; color: var(--text); transition: background .15s;
  }
  .conv-item::before { content: "💬"; font-size: 13px; flex-shrink: 0; opacity: .7; }
  .conv-item:hover { background: var(--gold-soft); }
  .conv-item.active { background: var(--gold-soft); }
  .conv-item .t { flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
  .conv-del {
    border: none; background: none; color: var(--dim); cursor: pointer; font-size: 13px;
    padding: 2px 6px; border-radius: 6px; flex-shrink: 0;
  }
  .conv-del:hover { color: var(--danger); }
  .conv-empty { text-align: center; color: var(--dim); font-size: 13px; margin-top: 30px; }

  .sidebar-bottom { padding: 10px 14px 16px; border-top: 1px solid var(--border-soft); display: flex; flex-direction: column; gap: 8px; }
  .sidebar-row {
    display: flex; align-items: center; gap: 10px; width: 100%; padding: 9px 10px;
    border-radius: 12px; border: 1px solid var(--border-soft); background: transparent;
    color: var(--text); font-size: 14px; cursor: pointer; font-family: inherit; text-align: right;
    transition: border-color .15s ease, transform .15s ease;
  }
  .sidebar-row:hover { border-color: var(--gold); transform: translateX(-2px); }
  html[dir="ltr"] .sidebar-row:hover { transform: translateX(2px); }
  .sidebar-row:active { transform: scale(.98); }
  .sidebar-row-icon {
    width: 30px; height: 30px; border-radius: 50%; background: var(--gold-soft);
    display: flex; align-items: center; justify-content: center; font-size: 14px; flex-shrink: 0;
  }
  .profile-row { justify-content: flex-start; }
  .profile-name { flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; text-align: right; }
  .avatar.user.small {
    width: 30px; height: 30px; border-radius: 50%; flex-shrink: 0;
    background: linear-gradient(135deg, var(--violet), var(--gold)); color: #fff;
    display: flex; align-items: center; justify-content: center; font-size: 12px; font-weight: 700;
  }
  .meter-wrap { padding: 2px 4px; }
  .meter-label { font-family: 'JetBrains Mono', monospace; font-size: 12px; color: var(--dim); direction: ltr; }
  .meter { width: 100%; height: 4px; margin-top: 4px; background: var(--border-soft); border-radius: 2px; overflow: hidden; }
  .meter-fill {
    width: 100%; height: 100%; background: linear-gradient(90deg, var(--gold-deep), var(--gold));
    transform-origin: right center; transform: scaleX(0); transition: transform .4s;
  }
  .meter-fill.low { background: var(--danger); }

  /* ---------- Chat shell ---------- */
  .chat-shell { flex: 1; min-width: 0; display: flex; flex-direction: column; height: 100vh; }
  .chat-topbar {
    align-items: center; justify-content: space-between; gap: 10px;
    padding: 12px 16px; border-bottom: 1px solid var(--border-soft);
    background: var(--header-bg, rgba(11,18,32,0.85)); backdrop-filter: blur(14px);
    position: sticky; top: 0; z-index: 10;
  }
  .menu-btn {
    width: 38px; height: 38px; border-radius: 10px; border: 1px solid var(--border-soft);
    background: transparent; color: var(--text); font-size: 18px; cursor: pointer;
    display: flex; align-items: center; justify-content: center;
  }
  .menu-btn:hover { border-color: var(--gold); }
  .mobile-only { display: none; }

  .chat-card-wrap { flex: 1; min-height: 0; padding: 20px 24px 8px; display: flex; }
  .chat-card {
    flex: 1; min-height: 0; display: flex; flex-direction: column;
    background: var(--card-bg, var(--surface)); border-radius: 20px;
    box-shadow: 0 20px 56px rgba(0,0,0,0.04); overflow: hidden;
    animation: slideInUp .45s ease both;
  }
  .chat-area { flex: 1; overflow-y: auto; padding: 24px; display: flex; flex-direction: column; gap: 15px; }

  .hero { text-align: center; margin: 6vh auto 0; max-width: 560px; animation: rise .5s ease both; }
  .hero h1 { font-family: 'Marcellus', serif; font-weight: 400; font-size: clamp(24px, 4vw, 32px); margin: 16px 0 8px; }
  .hero .gold-text {
    background: linear-gradient(90deg, #C9A45E, #B08D4F);
    -webkit-background-clip: text; background-clip: text; color: transparent;
  }
  .hero p { color: var(--dim); font-size: 15px; }
  .chips { display: grid; grid-template-columns: repeat(auto-fit, minmax(200px, 1fr)); gap: 10px; max-width: 560px; margin: 24px auto 0; }
  .chip {
    padding: 14px 16px; border-radius: 14px; border: 1px solid var(--border-soft);
    background: var(--gold-soft); color: var(--text);
    font-size: 14px; cursor: pointer; text-align: right; font-family: inherit;
    display: flex; align-items: center; gap: 10px; transition: border-color .2s, transform .2s;
  }
  .chip:hover { border-color: var(--gold); transform: translateY(-2px); }

  .msg { display: flex; gap: 10px; max-width: 88%; animation: rise .35s ease both; }
  .msg.user { align-self: flex-start; flex-direction: row; }
  .msg.ai { align-self: flex-end; flex-direction: row; }
  .avatar {
    width: 30px; height: 30px; border-radius: 50%; flex-shrink: 0; margin-top: 2px;
    display: flex; align-items: center; justify-content: center; font-size: 13px; font-weight: 700;
  }
  .avatar.user { background: linear-gradient(135deg, var(--violet), #7C5C2A); color: #fff; }
  .avatar.ai { background: linear-gradient(135deg, var(--gold-deep), var(--gold)); color: #fff; }
  .bubble {
    padding: 12px 16px; border-radius: 16px; font-size: 15px; line-height: 1.65;
    white-space: pre-wrap; box-shadow: 0 4px 18px rgba(0,0,0,0.06);
    background: var(--surface); border: 1px solid var(--border-soft);
  }
  .msg.user .bubble {
    background: linear-gradient(135deg, rgba(212,180,120,0.14), rgba(176,141,79,0.08));
    border-color: rgba(212,180,120,0.22);
  }
  .msg.ai .bubble { background: var(--gold-soft); }
  .typing { display: flex; gap: 5px; padding: 14px 18px; }
  .typing span { width: 6px; height: 6px; border-radius: 50%; background: var(--gold); animation: pulse 1.2s ease-in-out infinite; }
  .typing span:nth-child(2) { animation-delay: .2s } .typing span:nth-child(3) { animation-delay: .4s }

  .composer-wrap { width: 100%; padding: 8px 24px 16px; z-index: 5; animation: slideInUp .5s ease both; }
  .composer {
    display: flex; align-items: center; gap: 10px; background: var(--surface);
    border: 1px solid var(--border-soft); border-radius: 30px; padding: 10px 10px 10px 16px;
    box-shadow: 0 8px 30px rgba(0,0,0,0.06); transition: border-color .15s, box-shadow .15s;
  }
  .composer:focus-within { border-color: var(--gold); box-shadow: 0 0 0 3px var(--gold-soft); }
  .composer textarea {
    flex: 1; resize: none; border: none; background: transparent; color: var(--text);
    font-size: 15px; font-family: inherit; padding: 9px 8px; min-height: 24px;
  }
  .composer textarea:focus { outline: none; }
  .send-btn {
    width: 48px; height: 48px; border-radius: 50%; flex-shrink: 0; border: none; cursor: pointer;
    background: var(--gold); color: #fff; font-size: 18px;
    display: flex; align-items: center; justify-content: center;
    box-shadow: 0 4px 14px rgba(212,180,120,0.35);
    transition: transform .12s ease;
  }
  .send-btn:hover:not(:disabled) { transform: scale(1.06); }
  .send-btn:active:not(:disabled) { transform: scale(.88); }
  .send-btn span { display: inline-block; transform: scaleX(-1); }
  .send-btn:disabled { background: var(--dim); box-shadow: none; cursor: default; }
  .footnote { text-align: center; font-size: 11px; color: var(--dim); margin-bottom: 8px; }
  .error-line { color: var(--danger); font-size: 13px; text-align: center; margin-bottom: 6px; }

  /* Auth + modal */
  .overlay {
    position: fixed; inset: 0; background: var(--overlay-bg, rgba(4,6,10,0.8)); backdrop-filter: blur(6px);
    display: flex; align-items: center; justify-content: center; padding: 20px; z-index: 80;
  }
  .card {
    background: var(--card-bg, linear-gradient(160deg, #171E2E, #0E1320));
    border: 1px solid var(--border); border-radius: 22px;
    padding: 30px 28px; max-width: 400px; width: 100%; text-align: center;
    box-shadow: 0 0 60px rgba(212,180,120,0.15); animation: rise .35s ease both;
  }
  .card h2 { font-family: 'Marcellus', serif; font-weight: 400; font-size: 23px; margin: 12px 0 6px; letter-spacing: .03em; }
  .card p { color: var(--dim); font-size: 14px; margin-bottom: 18px; }
  .card input {
    width: 100%; padding: 12px 14px; margin-bottom: 10px; border-radius: 12px;
    border: 1px solid var(--border-soft); background: var(--gold-soft);
    color: var(--text); font-size: 14px; font-family: inherit;
  }
  .card .cta { width: 100%; padding: 13px; margin-top: 6px; }
  .link-btn { background: none; border: none; color: var(--gold); font-size: 13px; cursor: pointer; margin-top: 12px; font-family: inherit; }
  .auth-divider { display: flex; align-items: center; gap: 10px; margin: 16px 0 10px; color: var(--dim); font-size: 12px; }
  .auth-divider::before, .auth-divider::after { content: ""; flex: 1; height: 1px; background: var(--border-soft); }
  #googleBtnWrap { display: flex; justify-content: center; margin-bottom: 4px; }
  .ghost-btn {
    width: 100%; padding: 10px; border-radius: 13px; border: 1px solid var(--border-soft);
    background: transparent; color: var(--dim); font-size: 14px; cursor: pointer; margin-top: 10px; font-family: inherit;
  }
  .plans { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; margin-bottom: 18px; font-size: 13px; }
  .plan-box { border: 1px solid var(--border-soft); border-radius: 14px; padding: 14px 12px; color: var(--dim); }
  .plan-box.pro-box { border-color: var(--gold); background: var(--gold-soft); color: var(--text); }
  .mono { font-family: 'JetBrains Mono', monospace; font-size: 12px; direction: ltr; }
  .hidden { display: none !important; }

  /* Code blocks */
  .bubble pre {
    direction: ltr; text-align: left; background: var(--code-bg, #0A0E16);
    border: 1px solid var(--border-soft); border-radius: 12px;
    padding: 36px 14px 14px; margin: 10px 0; overflow-x: auto; position: relative;
  }
  .bubble pre code { font-family: 'JetBrains Mono', monospace; font-size: 13px; line-height: 1.55; background: transparent; padding: 0; color: #E6EAF3; }
  .bubble code { font-family: 'JetBrains Mono', monospace; font-size: 13px; background: var(--gold-soft); padding: 2px 6px; border-radius: 6px; direction: ltr; unicode-bidi: embed; }
  .copy-btn {
    position: absolute; top: 6px; left: 8px; padding: 4px 10px;
    border-radius: 8px; border: 1px solid var(--border-soft); background: rgba(255,255,255,0.08);
    color: #B7C0D8; font-size: 11px; cursor: pointer; font-family: 'Tajawal', sans-serif;
  }
  .copy-btn:hover { color: var(--gold-deep); border-color: var(--gold-deep); }
  .bubble p { margin: 6px 0; } .bubble ul, .bubble ol { margin: 6px 18px; }
  .bubble h1,.bubble h2,.bubble h3 { margin: 10px 0 6px; font-size: 16px; color: var(--gold); }

  /* Majlis */
  .majlis-toggle {
    align-self: center; width: 46px; height: 46px; padding: 0; border-radius: 13px; flex-shrink: 0;
    border: 1px solid var(--border-soft); background: transparent;
    color: var(--dim); font-size: 16px; cursor: pointer; font-family: inherit;
    transition: all .2s; white-space: nowrap;
    display: flex; align-items: center; justify-content: center;
  }
  .majlis-toggle .mt-label { display: none; }
  .majlis-toggle.on {
    border-color: var(--gold); color: var(--gold); background: var(--gold-soft);
    box-shadow: 0 0 14px rgba(212,180,120,0.2);
    animation: togglePop .35s cubic-bezier(0.16, 1, 0.3, 1);
  }
  .majlis-block { align-self: stretch; max-width: 100%; animation: rise .4s ease both; }
  .majlis-title {
    text-align: center; font-family: 'Marcellus', serif; color: var(--gold);
    font-size: 15px; letter-spacing: .08em; margin: 6px 0 10px;
  }
  .majlis-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(220px, 1fr)); gap: 10px; }
  .majlis-card {
    background: var(--surface); border: 1px solid var(--border-soft);
    border-radius: 14px; padding: 14px; font-size: 13.5px; line-height: 1.7;
  }
  .majlis-card .who { font-weight: 700; color: var(--gold); margin-bottom: 6px; font-size: 13px; }
  .majlis-verdict {
    margin-top: 10px; background: var(--gold-soft);
    border: 1px solid var(--gold); border-radius: 14px; padding: 16px;
    font-size: 14.5px; line-height: 1.8;
  }
  .majlis-verdict .who { font-weight: 700; color: var(--gold); margin-bottom: 6px; }

  .sidebar-backdrop { position: fixed; inset: 0; background: rgba(0,0,0,0.35); z-index: 55; }

  /* ---------- LTR (English) overrides ---------- */
  html[dir="ltr"] body { font-family: 'Inter', 'Tajawal', system-ui, sans-serif; }
  html[dir="ltr"] .wordmark, html[dir="ltr"] .hero h1 { font-family: 'Marcellus', 'Inter', serif; }
  html[dir="ltr"] .sidebar { right: auto; left: -300px; border-left: none; border-right: 1px solid var(--border-soft); }
  html[dir="ltr"] .sidebar.open { left: 0; right: auto; }
  html[dir="ltr"] .profile-name, html[dir="ltr"] .sidebar-row, html[dir="ltr"] .chip { text-align: left; }
  html[dir="ltr"] .meter-fill { transform-origin: left center; }
  html[dir="ltr"] .send-btn span { transform: none; }

  /* ---------- Desktop: persistent sidebar ---------- */
  @media (min-width: 900px) {
    .sidebar { position: sticky; right: auto; top: 0; height: 100vh; box-shadow: none; }
    .sidebar-backdrop { display: none !important; }
    .chat-topbar { display: none !important; }
  }
  @media (max-width: 899px) {
    .mobile-only { display: flex; }
    .chat-card-wrap { padding: 14px 12px 6px; }
    .composer-wrap { padding: 8px 12px 16px; }
    .chat-area { padding: 18px 14px; }
  }
  @media (max-width: 480px) {
    .chips { grid-template-columns: 1fr; }
    .composer { gap: 6px; padding: 8px 8px 8px 12px; }
    .majlis-toggle { width: 40px; height: 40px; font-size: 14px; }
    .icon-btn-round.small, #incognitoToggle { flex-shrink: 0; }
    .composer textarea { font-size: 14px; padding: 9px 4px; min-width: 0; }
    .send-btn { width: 42px; height: 42px; font-size: 16px; }
  }
