*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }
    :root {
      --bg: #f7f6f3; --surface: #ffffff; --surface2: #f0efe9;
      --border: #e8e6de; --border2: #d8d5cb;
      --ink: #18170f; --ink-2: #6a6860; --ink-3: #b0ada4;
      --accent: #2d6a4f; --accent-lt: #eaf2ed; --accent-md: #a8d5b5; --accent-dk: #1b4332;
      --danger: #c0392b; --sidebar-w: 270px;
      --t: 0.18s cubic-bezier(0.4,0,0.2,1); --radius: 14px;
      --shadow-sm: 0 1px 4px rgba(0,0,0,0.05);
      --shadow-md: 0 4px 16px rgba(0,0,0,0.12);
    }
    html, body { height: 100%; font-family: "Heebo", sans-serif; background: var(--bg); color: var(--ink); overflow: auto; font-size: 15px; line-height: 1.65; -webkit-font-smoothing: antialiased; }
    .shell { display: flex; height: 100vh; }

    /* Sidebar */
    .sidebar { will-change: transform; width: var(--sidebar-w); min-width: var(--sidebar-w); background: var(--surface); border-left: 1px solid var(--border); display: flex; flex-direction: column; padding: 18px 12px; gap: 10px; transition: transform var(--t); overflow: hidden; flex-shrink: 0; }
    .sidebar.hidden { transform: translateX(100%); }
    .sidebar-top { display: flex; align-items: center; gap: 8px; padding-bottom: 14px; border-bottom: 1px solid var(--border); }
    .logo-mark { width: 28px; height: 28px; background: var(--accent); border-radius: 8px; display: flex; align-items: center; justify-content: center; flex-shrink: 0; }
    .brand-name { font-family: "Fraunces", serif; font-size: 1.1rem; color: var(--ink); flex: 1; }
    .icon-btn { background: none; border: none; color: var(--ink-3); cursor: pointer; padding: 6px; border-radius: 8px; display: flex; align-items: center; justify-content: center; transition: all var(--t); flex-shrink: 0; }
    .icon-btn:hover { background: var(--surface2); color: var(--ink); }
    .new-btn { display: flex; align-items: center; justify-content: center; gap: 7px; background: var(--accent); color: #fff; border: none; font-family: "Heebo", sans-serif; font-size: 0.86rem; font-weight: 600; padding: 10px 16px; border-radius: var(--radius); cursor: pointer; width: 100%; transition: all var(--t); }
    .new-btn:hover { background: var(--accent-dk); transform: translateY(-1px); box-shadow: 0 4px 16px rgba(45,106,79,0.22); }
    .sidebar-label { font-size: 0.68rem; font-weight: 700; text-transform: uppercase; letter-spacing: 0.13em; color: var(--ink-3); padding: 0 4px; }
    .hist-list { scrollbar-width: thin; scrollbar-color: var(--border2) transparent; flex: 1; overflow-y: auto; display: flex; flex-direction: column; gap: 2px; }
    .hist-list::-webkit-scrollbar { width: 3px; }
    .hist-list::-webkit-scrollbar-thumb { background: var(--border2); border-radius: 2px; }
    .hist-item { display: flex; align-items: center; gap: 6px; padding: 8px 10px; border-radius: 10px; font-size: 0.83rem; color: var(--ink-2); cursor: pointer; transition: all var(--t); }
    .hist-item:hover { background: var(--surface2); color: var(--ink); }
    .hist-item.active { background: var(--accent-lt); color: var(--accent); font-weight: 600; }
    .hist-title { flex: 1; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
    .hist-del { opacity: 0; background: none; border: none; color: var(--ink-3); cursor: pointer; padding: 2px 5px; border-radius: 4px; font-size: 0.7rem; transition: all var(--t); }
    .hist-item:hover .hist-del { opacity: 1; }
    .hist-del:hover { color: var(--danger); }
    .hist-empty { font-size: 0.82rem; color: var(--ink-3); text-align: center; padding: 28px 0; }
    .sidebar-foot { padding-top: 12px; border-top: 1px solid var(--border); display: flex; flex-direction: column; gap: 8px; }
    .model-badge { display: flex; align-items: center; gap: 7px; font-size: 0.74rem; color: var(--ink-3); }
    .model-dot { width: 6px; height: 6px; border-radius: 50%; background: var(--accent); flex-shrink: 0; animation: pulse 2.5s ease-in-out infinite; }
    @keyframes pulse { 0%,100%{opacity:1} 50%{opacity:0.4} }
    .clear-btn { display: flex; align-items: center; gap: 6px; background: none; border: 1px solid var(--border); color: var(--ink-3); font-family: "Heebo", sans-serif; font-size: 0.76rem; padding: 6px 10px; border-radius: 8px; cursor: pointer; transition: all var(--t); width: 100%; justify-content: center; }
    .clear-btn:hover { border-color: var(--danger); color: var(--danger); background: #fdf0ef; }
    .overlay { display: none; position: fixed; inset: 0; background: rgba(24,23,15,0.28); z-index: 50; }
    .overlay.show { display: block; }

    /* Main */
    .main { flex: 1; display: flex; flex-direction: column; overflow: hidden; min-width: 0; padding-bottom: 88px; }
    .topbar { display: flex; align-items: center; gap: 10px; padding: 12px 18px; background: rgba(247,246,243,0.9); backdrop-filter: blur(6px); border-bottom: 1px solid var(--border); flex-shrink: 0; }
    .topbar-brand { flex: 1; display: flex; align-items: center; gap: 8px; }
    .topbar-name { font-family: "Fraunces", serif; font-size: 1.05rem; color: var(--ink); }
    .topbar-dot { width: 5px; height: 5px; border-radius: 50%; background: var(--accent); animation: pulse 2.5s ease-in-out infinite; }

    /* Chat */
    .chat-win { flex: 1; overflow-y: auto; padding: 32px 20px; display: flex; flex-direction: column; }
    .chat-win { scrollbar-width: thin; scrollbar-color: var(--border2) transparent; }
    .chat-win::-webkit-scrollbar { width: 4px; }
    .chat-win::-webkit-scrollbar-thumb { background: var(--border2); border-radius: 2px; }
    .welcome { flex: 1; display: flex; align-items: center; justify-content: center; }
    .welcome-inner { will-change: transform, opacity; display: flex; flex-direction: column; align-items: center; text-align: center; max-width: 460px; animation: rise 0.5s cubic-bezier(0.4,0,0.2,1) both; }
    @keyframes rise { from{opacity:0;transform:translateY(18px)} to{opacity:1;transform:translateY(0)} }
    .welcome-icon { width: 56px; height: 56px; background: var(--accent); border-radius: 18px; display: flex; align-items: center; justify-content: center; margin-bottom: 20px; animation: breathe 4s ease-in-out infinite; }
    @keyframes breathe { 0%,100%{transform:scale(1)} 50%{transform:scale(1.06)} }
    .welcome-title { font-family: "Fraunces", serif; font-size: 2.2rem; color: var(--ink); margin-bottom: 10px; line-height: 1.15; }
    .welcome-title em { color: var(--accent); font-style: italic; }
    .welcome-sub { font-size: 0.94rem; color: var(--ink-2); margin-bottom: 30px; font-weight: 300; line-height: 1.75; }
    .chips { display: flex; flex-wrap: wrap; gap: 8px; justify-content: center; }
    .chip { background: var(--surface); border: 1px solid var(--border); color: var(--ink-2); font-family: "Heebo", sans-serif; font-size: 0.83rem; padding: 8px 16px; border-radius: 100px; cursor: pointer; transition: all var(--t); }
    .chip:hover { border-color: var(--accent); color: var(--accent); background: var(--accent-lt); transform: translateY(-1px); box-shadow: 0 3px 10px rgba(45,106,79,0.12); }

    /* Messages */
    .messages { display: flex; flex-direction: column; gap: 20px; }
    .msg { will-change: transform, opacity; display: flex; gap: 10px; animation: msg-rise 0.28s cubic-bezier(0.4,0,0.2,1) both; }
    @keyframes msg-rise { from{opacity:0;transform:translateY(8px)} to{opacity:1;transform:translateY(0)} }
    .msg.user { flex-direction: row-reverse; }
    .avatar { width: 32px; height: 32px; border-radius: 50%; flex-shrink: 0; display: flex; align-items: center; justify-content: center; }
    .msg.user .avatar { background: var(--ink); }
    .msg.bot .avatar { background: var(--accent); }
    .msg-body { max-width: clamp(260px, 74%, 600px); display: flex; flex-direction: column; gap: 5px; }
    .bubble { padding: 12px 16px; border-radius: 18px; font-size: 0.92rem; line-height: 1.75; word-break: break-word; }
    .msg.user .bubble { background: var(--ink); color: var(--bg); border-bottom-right-radius: 4px; }
    .msg.bot .bubble { background: var(--surface); border: 1px solid var(--border); color: var(--ink); border-bottom-left-radius: 4px; box-shadow: 0 1px 4px rgba(24,23,15,0.05); }
    .bubble strong { font-weight: 700; }
    .bubble em { font-style: italic; }
    .bubble code { background: var(--surface2); padding: 1px 5px; border-radius: 4px; font-family: "Courier New", monospace; font-size: 0.85em; }
    .bubble pre { background: #1a1a12; color: #e8e6d8; padding: 12px 14px; border-radius: 10px; margin: 8px 0; overflow-x: auto; font-family: "Courier New", monospace; font-size: 0.82em; line-height: 1.6; direction: ltr; text-align: left; }
    .bubble ul { padding-right: 18px; margin: 6px 0; }
    .bubble li { margin-bottom: 3px; }
    .bubble h3 { font-family: "Fraunces", serif; margin: 10px 0 4px; }
    .msg-image { border-radius: 12px; max-width: 280px; max-height: 220px; object-fit: cover; border: 1px solid var(--border); display: block; }
    .msg-actions { display: flex; align-items: center; gap: 6px; opacity: 0; transition: opacity var(--t); flex-wrap: wrap; margin-top: 2px; }
    .msg:hover .msg-actions { opacity: 1; }
    .action-btn { background: none; border: 1px solid var(--border); color: var(--ink-3); font-family: "Heebo", sans-serif; font-size: 0.72rem; padding: 3px 9px; border-radius: 6px; cursor: pointer; display: flex; align-items: center; gap: 4px; transition: all var(--t); white-space: nowrap; }
    .action-btn:hover { background: var(--surface2); color: var(--ink); }
    .action-btn.active { background: var(--accent-lt); color: var(--accent); border-color: var(--accent-md); }
    .action-btn.copied { color: var(--accent); border-color: var(--accent-md); }
    .msg-time { font-size: 0.68rem; color: var(--ink-3); }

    /* Interim speech banner */
    .interim-banner { position: fixed; bottom: 108px; left: 50%; transform: translateX(-50%); background: rgba(24,23,15,0.88); color: #fff; padding: 9px 22px; border-radius: 100px; font-size: 0.86rem; font-family: "Heebo", sans-serif; direction: rtl; max-width: 80vw; text-align: center; z-index: 200; backdrop-filter: blur(8px); opacity: 0; transition: opacity 0.2s ease; pointer-events: none; }
    .interim-banner.show { opacity: 1; }

    /* Typing */
    .typing-row { display: flex; gap: 10px; }
    .typing-dots { display: flex; align-items: center; gap: 5px; padding: 14px 18px; background: var(--surface); border: 1px solid var(--border); border-radius: 18px; border-bottom-left-radius: 4px; box-shadow: 0 1px 4px rgba(24,23,15,0.05); }
    .typing-dots span { width: 7px; height: 7px; border-radius: 50%; background: var(--ink-3); animation: dot-b 1.2s ease-in-out infinite; }
    .typing-dots span:nth-child(2) { animation-delay: .18s; }
    .typing-dots span:nth-child(3) { animation-delay: .36s; }
    @keyframes dot-b { 0%,60%,100%{transform:translateY(0);opacity:.3} 30%{transform:translateY(-6px);opacity:1} }

    /* Input */
    .input-area { padding: 10px 18px 16px; border-top: 1px solid var(--border); background: rgba(247,246,243,0.97); backdrop-filter: blur(6px); flex-shrink: 0; position: fixed; bottom: 0; left: 0; right: 0; z-index: 40; }
    .img-preview { display: flex; align-items: center; gap: 10px; background: var(--surface); border: 1px solid var(--border); border-radius: 10px; padding: 7px 12px; margin-bottom: 8px; }
    #imgThumb { width: 42px; height: 42px; object-fit: cover; border-radius: 6px; border: 1px solid var(--border); }
    #imgName { flex: 1; font-size: 0.8rem; color: var(--ink-2); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
    .rm-img { background: none; border: none; color: var(--ink-3); cursor: pointer; padding: 4px; border-radius: 4px; display: flex; transition: color var(--t); }
    .rm-img:hover { color: var(--danger); }
    .input-row { display: flex; align-items: flex-end; gap: 8px; background: var(--surface); border: 1.5px solid var(--border); border-radius: 22px; padding: 5px 6px 5px 10px; transition: border-color var(--t), box-shadow var(--t); }
    .input-row:focus-within { border-color: var(--accent); box-shadow: 0 0 0 3px rgba(45,106,79,0.1); }
    #userInput { flex: 1; background: none; border: none; outline: none; resize: none; font-family: "Heebo", sans-serif; font-size: 0.92rem; color: var(--ink); line-height: 1.6; max-height: 140px; overflow-y: auto; padding: 7px 2px; direction: rtl; caret-color: var(--accent); }
    #userInput::placeholder { color: var(--ink-3); }
    .input-btns { display: flex; align-items: center; gap: 3px; }
    .mic-btn { width: 36px; height: 36px; border-radius: 11px; background: none; border: 1px solid var(--border); color: var(--ink-3); cursor: pointer; display: flex; align-items: center; justify-content: center; transition: all var(--t); position: relative; }
    .mic-btn:hover { background: var(--surface2); color: var(--accent); border-color: var(--accent-md); }
    .mic-btn.listening { background: var(--danger); color: white; border-color: var(--danger); }
    .mic-btn.listening::after { content:""; position:absolute; inset:-3px; border-radius:14px; border:2px solid var(--danger); opacity:0.6; animation: mic-ring 1.2s ease-in-out infinite; }
    @keyframes mic-ring { 0%{opacity:0.6;transform:scale(1)} 100%{opacity:0;transform:scale(1.5)} }
    .stop-tts-btn { width: 36px; height: 36px; border-radius: 11px; background: var(--danger); border: none; color: white; cursor: pointer; align-items: center; justify-content: center; transition: all var(--t); display: none; }
    .stop-tts-btn.show { display: flex; }
    .stop-tts-btn:hover { background: #a93226; }
    .attach-btn:hover { color: var(--accent); background: var(--accent-lt); }
    .send-btn { width: 36px; height: 36px; border-radius: 11px; background: var(--accent); border: none; color: white; cursor: pointer; display: flex; align-items: center; justify-content: center; flex-shrink: 0; transition: all var(--t); }
    .send-btn:hover { background: var(--accent-dk); transform: translateY(-1px); box-shadow: 0 4px 12px rgba(45,106,79,0.28); }
    .send-btn:active { transform: scale(0.94); }
    .send-btn:disabled { opacity: .35; cursor: not-allowed; transform: none; box-shadow: none; }
    .input-foot { display: flex; align-items: center; justify-content: space-between; margin-top: 7px; padding: 0 4px; }
    .input-hint { font-size: 0.69rem; color: var(--ink-3); }
    .voice-select { background: none; border: none; font-family: "Heebo", sans-serif; font-size: 0.69rem; color: var(--ink-3); cursor: pointer; outline: none; direction: rtl; max-width: 150px; }
    .voice-select:focus { color: var(--accent); }
    .toast { position: fixed; bottom: 24px; left: 50%; transform: translateX(-50%) translateY(20px); background: var(--ink); color: #fff; font-size: 0.83rem; padding: 9px 18px; border-radius: 100px; opacity: 0; transition: all 0.3s ease; pointer-events: none; z-index: 999; }
    .toast.show { opacity: 1; transform: translateX(-50%) translateY(0); }



/* ═══════════════════════════════════════
   DARK MODE
   ═══════════════════════════════════════ */
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #141410;
    --surface: #1c1c18;
    --surface2: #242420;
    --border: #2e2e28;
    --border2: #3a3a34;
    --ink: #f0efe8;
    --ink-2: #a0a098;
    --ink-3: #606058;
  }
  .topbar, .input-area { background: rgba(20,20,16,0.96); }
}

/* ═══════════════════════════════════════
   MOBILE FIX
   ═══════════════════════════════════════ */
@media (max-width: 640px) {
  .sidebar { position: fixed; right: 0; top: 0; bottom: 0; z-index: 60;
             transform: translateX(100%); width: var(--sidebar-w) !important;
             min-width: var(--sidebar-w); padding: 18px 12px !important;
             border-left: 1px solid var(--border) !important; }
  .sidebar.hidden { transform: translateX(100%); }
  .sidebar.open   { transform: translateX(0); }
  .input-area { padding-bottom: max(16px, env(safe-area-inset-bottom)); }
  .topbar, .input-area { backdrop-filter: none; -webkit-backdrop-filter: none; }
  .welcome-title { font-size: 1.7rem; }
  .main { padding-bottom: 96px; }
}