:root {
    color-scheme: only light;
    --bg: #080B12; --surface: rgba(20,26,40,0.75); --surface-solid: #141A28;
    --border: rgba(212,180,120,0.18); --border-soft: rgba(255,255,255,0.07);
    --text: #EFEAE0; --dim: #93A0B5;
    --gold: #D4B478; --gold-deep: #B08D4F; --gold-soft: rgba(212,180,120,0.13);
    --violet: #8B7BD8; --danger: #E07856;
  }
  body.light {
    --bg: #FFFFFF; --surface: #FAFAF8; --surface-solid: #FFFFFF;
    --border: rgba(150,112,50,0.35); --border-soft: rgba(0,0,0,0.12);
    --text: #241E10; --dim: #75694F;
    --gold: #9C742F; --gold-deep: #7E5C22; --gold-soft: rgba(156,116,47,0.1);
    --header-bg: rgba(255,255,255,0.92);
    --code-bg: #29241A;
    --card-bg: #FFFFFF;
    --overlay-bg: rgba(60,50,30,0.45);
  }
  body { transition: background .35s, color .35s; }
  body.light .cta { background: linear-gradient(110deg, #8C6830 25%, #C9A45E 50%, #8C6830 75%); background-size: 200% auto; color: #FFF9EC; }
  body.light .cta:disabled { background: rgba(30,25,12,0.08); color: var(--dim); }
  body.light .orb1 { background: radial-gradient(circle, rgba(156,116,47,0.07), transparent 65%); }
  body.light .orb2 { background: radial-gradient(circle, rgba(120,100,190,0.05), transparent 65%); }
  body.light .wordmark { background: linear-gradient(90deg, #8C6830, #9C742F, #6B4E1D); -webkit-background-clip: text; background-clip: text; }
  body.light .bubble, body.light .majlis-card { box-shadow: 0 3px 14px rgba(90,70,30,0.1); }
  body.light .bubble pre { background: #FFFDF6; border-color: rgba(30,25,12,0.14); }
  body.light .bubble pre code { background: transparent; }
  body.light .copy-btn { background: rgba(30,25,12,0.05); }
  .theme-btn {
    width: 38px; height: 38px; border-radius: 50%; border: 1px solid var(--border-soft);
    background: transparent; color: var(--text); font-size: 17px; cursor: pointer;
    display: flex; align-items: center; justify-content: center; transition: border-color .2s;
  }
  .theme-btn:hover { border-color: var(--gold); }

  * { box-sizing: border-box; margin: 0; }
  body {
    background: var(--bg) !important; color: var(--text);
    font-family: 'Tajawal', 'Inter', system-ui, sans-serif;
    min-height: 100vh; display: flex; flex-direction: column; 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.13), transparent 65%);
    animation: drift1 16s ease-in-out infinite; }
  .orb2 { bottom: -20%; right: -12%; width: 480px; height: 480px;
    background: radial-gradient(circle, rgba(139,123,216,0.10), 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); } to { opacity: 1; } }
  @keyframes pulse { 0%,100% { opacity: .3 } 50% { opacity: 1 } }
  @keyframes shimmer { 0% { background-position: -200% center } 100% { background-position: 200% center } }
  @media (prefers-reduced-motion: reduce) { .orb1,.orb2,.msg,.cta { animation: 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: #191204; 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; }
  button:focus-visible, input:focus, textarea:focus { outline: 2px solid var(--gold); outline-offset: 2px; }

  header {
    display: flex; align-items: center; justify-content: space-between;
    padding: 13px 18px; border-bottom: 1px solid var(--border-soft);
    background: var(--header-bg, rgba(8,11,18,0.8)); backdrop-filter: blur(14px);
    position: sticky; top: 0; z-index: 10;
  }
  .brand { display: flex; align-items: center; gap: 10px; }
  .wordmark {
    font-family: 'Marcellus', serif; font-size: 21px; letter-spacing: 0.14em;
    background: linear-gradient(90deg, #EFDDB4, #D4B478, #B08D4F);
    -webkit-background-clip: text; background-clip: text; color: transparent;
  }
  .tagline { font-size: 10px; color: var(--dim); letter-spacing: 0.2em; }

  .meter-wrap { text-align: left; }
  .meter-label { font-family: 'JetBrains Mono', monospace; font-size: 12px; color: var(--dim); direction: ltr; }
  .meter { width: 90px; height: 3px; margin-top: 4px; background: rgba(255,255,255,0.08); border-radius: 2px; overflow: hidden; }
  .meter-fill { height: 100%; background: linear-gradient(90deg, #B08D4F, #E8CE96); transition: width .4s; }
  .meter-fill.low { background: var(--danger); }

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

  main {
    flex: 1; width: 100%; max-width: 780px; margin: 0 auto; padding: 24px 16px;
    display: flex; flex-direction: column; gap: 15px; overflow-y: auto; z-index: 1;
  }
  .hero { text-align: center; margin-top: 8vh; animation: rise .5s ease both; }
  .hero h1 { font-family: 'Marcellus', serif; font-weight: 400; font-size: clamp(26px, 5vw, 36px); margin: 18px 0 8px; }
  .hero .gold-text {
    background: linear-gradient(90deg, #EFDDB4, #D4B478);
    -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: 26px auto 0; }
  .chip {
    padding: 14px 16px; border-radius: 14px; border: 1px solid var(--border);
    background: var(--surface); backdrop-filter: blur(8px); 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, #8B7BD8, #5F4FB0); }
  .avatar.ai {
    background: linear-gradient(135deg, #D4B478, #9C7A3E);
    color: #FFF9EC; font-family: 'Marcellus', serif; font-size: 15px; font-weight: 700;
    border: none;
  }
  .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.25);
    background: var(--surface); backdrop-filter: blur(8px); border: 1px solid var(--border-soft);
  }
  .msg.user .bubble {
    background: linear-gradient(135deg, rgba(212,180,120,0.18), rgba(212,180,120,0.08));
    border-color: rgba(212,180,120,0.32);
  }
  .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%; max-width: 780px; margin: 0 auto; padding: 10px 16px 18px; z-index: 5; }
  .composer {
    display: flex; gap: 10px; background: var(--surface); backdrop-filter: blur(14px);
    border: 1px solid var(--border); border-radius: 18px; padding: 10px;
    box-shadow: 0 8px 30px rgba(0,0,0,0.35);
  }
  .composer textarea {
    flex: 1; resize: none; border: none; background: transparent; color: var(--text);
    font-size: 15px; font-family: inherit; padding: 9px 8px; min-height: 40px;
  }
  .composer .cta { padding: 11px 20px; align-self: flex-end; }
  .footnote { text-align: center; font-size: 11px; color: var(--dim); margin-top: 8px; }
  .error-line { color: var(--danger); font-size: 13px; text-align: center; }

  /* 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: 50;
  }
  .card {
    background: var(--card-bg, linear-gradient(160deg, #171E2E, #0E1320));
    border: 1px solid rgba(212,180,120,0.4); 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: rgba(255,255,255,0.04);
    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; }
  .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; }
  .bubble code { font-family: 'JetBrains Mono', monospace; font-size: 13px; background: rgba(212,180,120,0.12); 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.05);
    color: var(--dim); font-size: 11px; cursor: pointer; font-family: 'Tajawal', sans-serif;
  }
  .copy-btn:hover { color: var(--gold); border-color: var(--gold); }
  .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: flex-end; padding: 11px 14px; border-radius: 13px;
    border: 1px solid var(--border-soft); background: transparent;
    color: var(--dim); font-size: 14px; cursor: pointer; font-family: inherit;
    transition: all .2s; white-space: nowrap;
  }
  .majlis-toggle.on {
    border-color: var(--gold); color: var(--gold); background: var(--gold-soft);
    box-shadow: 0 0 14px rgba(212,180,120,0.25);
  }
  .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: linear-gradient(160deg, rgba(212,180,120,0.12), rgba(212,180,120,0.04));
    border: 1px solid var(--gold); border-radius: 14px; padding: 16px;
    font-size: 14.5px; line-height: 1.8; box-shadow: 0 0 24px rgba(212,180,120,0.12);
  }
  .majlis-verdict .who { font-weight: 700; color: var(--gold); margin-bottom: 6px; }

  /* History sidebar */
  .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); }
  .sidebar {
    position: fixed; top: 0; bottom: 0; right: -300px; width: 290px; z-index: 60;
    background: var(--surface-solid); border-left: 1px solid var(--border-soft);
    display: flex; flex-direction: column; transition: right .3s ease;
    box-shadow: -10px 0 40px rgba(0,0,0,0.2);
  }
  .sidebar.open { right: 0; }
  .sidebar-head { display: flex; align-items: center; justify-content: space-between; padding: 14px 16px; border-bottom: 1px solid var(--border-soft); }
  .sidebar-title { font-family: 'Marcellus', serif; color: var(--gold); letter-spacing: .06em; font-size: 15px; }
  .new-chat-btn {
    margin: 12px 14px 6px; padding: 11px; border-radius: 12px; border: 1px dashed var(--border);
    background: var(--gold-soft); color: var(--gold); font-weight: 700; font-size: 14px;
    cursor: pointer; font-family: inherit;
  }
  .conv-list { flex: 1; overflow-y: auto; padding: 8px; }
  .conv-item {
    display: flex; align-items: center; gap: 8px; padding: 11px 12px; border-radius: 11px;
    cursor: pointer; font-size: 13.5px; color: var(--text); transition: background .15s;
  }
  .conv-item:hover { background: var(--gold-soft); }
  .conv-item.active { background: var(--gold-soft); border: 1px solid var(--border); }
  .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: 14px;
    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-backdrop { position: fixed; inset: 0; background: rgba(0,0,0,0.35); z-index: 55; }
