:root {
  --bg: #f7f6f2;
  --surface: #ffffff;
  --surface-2: #f0eee8;
  --text: #26251f;
  --muted: #6f6b62;
  --border: #e3dfd5;
  --accent: #c96442;
  --accent-soft: #f4e6df;
  --user-bubble: #ebe8e1;
  --code-bg: #1f1e1c;
  --ok: #2f7a4f;
  --warn: #a5670f;
  --radius: 14px;
  --shadow: 0 10px 30px rgba(30, 25, 20, .12);
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #1b1a18;
    --surface: #232220;
    --surface-2: #2b2a27;
    --text: #f0eee7;
    --muted: #a5a099;
    --border: #37352f;
    --accent: #d97757;
    --accent-soft: #3a2b25;
    --user-bubble: #2f2d29;
    --code-bg: #111110;
    --shadow: 0 10px 30px rgba(0, 0, 0, .5);
  }
}
* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { height: 100%; margin: 0; }
body {
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, "SF Arabic", "Geeza Pro", "Segoe UI", Tahoma, system-ui, sans-serif;
  font-size: 16px;
  line-height: 1.65;
  overscroll-behavior: none;
}
.hidden { display: none !important; }
.muted { color: var(--muted); }
button { font: inherit; color: inherit; cursor: pointer; }
input, textarea { font: inherit; color: inherit; }

/* ---------- gate ---------- */
.gate { position: fixed; inset: 0; display: grid; place-items: center; padding: 24px; background: var(--bg); z-index: 50; }
.gate-card { width: 100%; max-width: 360px; background: var(--surface); border: 1px solid var(--border); border-radius: 20px; padding: 26px 22px; box-shadow: var(--shadow); text-align: center; }
.gate-card h1 { font-size: 20px; margin: 8px 0 4px; }
.gate-card input { width: 100%; margin: 16px 0 12px; padding: 14px; border-radius: 12px; border: 1px solid var(--border); background: var(--bg); font-size: 17px; text-align: center; letter-spacing: .2em; }
.logo-big { font-size: 34px; color: var(--accent); line-height: 1; }
.err { color: #c0392b; font-size: 14px; margin: 10px 0 0; }

/* ---------- layout ---------- */
.app { display: flex; height: 100dvh; overflow: hidden; }
.sidebar {
  width: 268px; flex: 0 0 268px; background: var(--surface-2); border-inline-start: 1px solid var(--border);
  display: flex; flex-direction: column; height: 100dvh;
}
.side-top { padding: 14px 14px 8px; }
.brand { display: flex; align-items: center; gap: 8px; font-weight: 700; padding: 2px 4px 12px; }
.logo { color: var(--accent); font-size: 20px; }
.new-chat { width: 100%; display: flex; align-items: center; gap: 8px; padding: 11px 14px; border-radius: 12px; border: 1px solid var(--border); background: var(--surface); font-weight: 600; }
.new-chat .plus { font-size: 18px; color: var(--accent); }
.side-scroll { flex: 1; overflow-y: auto; padding: 6px 10px 10px; -webkit-overflow-scrolling: touch; }
.side-section { margin-bottom: 18px; }
.side-head { display: flex; align-items: center; justify-content: space-between; font-size: 12px; color: var(--muted); padding: 6px 6px; letter-spacing: .02em; }
.mini { border: none; background: transparent; color: var(--muted); font-size: 16px; padding: 0 4px; }
.side-item { display: flex; align-items: center; gap: 8px; padding: 9px 10px; border-radius: 10px; font-size: 14px; color: var(--text); text-decoration: none; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.side-item:hover { background: var(--surface); }
.side-item.active { background: var(--surface); font-weight: 600; }
.side-item .ico { color: var(--muted); }
.side-item .txt { overflow: hidden; text-overflow: ellipsis; }
.side-item .del { margin-inline-start: auto; border: none; background: none; color: var(--muted); opacity: 0; font-size: 13px; }
.side-item:hover .del { opacity: 1; }
.side-bottom { border-top: 1px solid var(--border); padding: 10px 14px 14px; }
.side-link { border: none; background: none; padding: 6px 0; font-size: 14px; }
.quota-line { font-size: 11.5px; color: var(--muted); margin-top: 4px; line-height: 1.5; }

.main { flex: 1; display: flex; flex-direction: column; height: 100dvh; min-width: 0; position: relative; }
.topbar { display: flex; align-items: center; gap: 10px; padding: 10px 12px; border-bottom: 1px solid var(--border); background: color-mix(in srgb, var(--bg) 88%, transparent); backdrop-filter: blur(8px); position: sticky; top: 0; z-index: 5; }
.icon-btn { border: none; background: none; font-size: 18px; padding: 6px 8px; border-radius: 8px; color: var(--text); }
.title-wrap { flex: 1; min-width: 0; }
.thread-title { font-size: 15px; font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.model-chip { border: 1px solid var(--border); background: var(--surface); border-radius: 999px; padding: 6px 12px; font-size: 12.5px; max-width: 42vw; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }

/* ---------- messages ---------- */
.messages { flex: 1; overflow-y: auto; padding: 18px 14px 120px; -webkit-overflow-scrolling: touch; scroll-behavior: smooth; }
.msg { max-width: 760px; margin: 0 auto 20px; }
.msg.user { display: flex; justify-content: flex-start; }
.msg.user .bubble { background: var(--user-bubble); padding: 11px 14px; border-radius: 16px; border-start-start-radius: 6px; max-width: 88%; white-space: pre-wrap; word-break: break-word; }
.msg.assistant .body { padding: 2px 0; }
.who { display: flex; align-items: center; gap: 6px; font-size: 12px; color: var(--muted); margin-bottom: 6px; }
.who .dot { color: var(--accent); font-size: 13px; }
.prose { word-break: break-word; }
.prose p { margin: 0 0 12px; }
.prose h1, .prose h2, .prose h3 { margin: 18px 0 8px; line-height: 1.35; }
.prose h1 { font-size: 21px; } .prose h2 { font-size: 18px; } .prose h3 { font-size: 16px; }
.prose ul, .prose ol { margin: 0 0 12px; padding-inline-start: 22px; }
.prose li { margin: 3px 0; }
.prose blockquote { margin: 0 0 12px; padding: 6px 12px; border-inline-start: 3px solid var(--border); color: var(--muted); }
.prose a { color: var(--accent); }
.prose table { border-collapse: collapse; width: 100%; margin: 0 0 14px; font-size: 14.5px; display: block; overflow-x: auto; }
.prose th, .prose td { border: 1px solid var(--border); padding: 7px 10px; text-align: start; }
.prose th { background: var(--surface-2); }
.prose code.inline { background: var(--surface-2); border-radius: 5px; padding: 1px 5px; font-size: .92em; font-family: ui-monospace, SFMono-Regular, Menlo, monospace; direction: ltr; unicode-bidi: embed; }
.code-wrap { background: var(--code-bg); border-radius: 12px; margin: 0 0 14px; overflow: hidden; direction: ltr; }
.code-head { display: flex; align-items: center; justify-content: space-between; padding: 7px 12px; font-size: 11.5px; color: #b9b3a8; background: rgba(255,255,255,.05); font-family: ui-monospace, monospace; }
.code-head button { border: none; background: none; color: #d6d0c4; font-size: 11.5px; }
.code-wrap pre { margin: 0; padding: 12px; overflow-x: auto; }
.code-wrap code { font-family: ui-monospace, SFMono-Regular, Menlo, monospace; font-size: 13px; color: #f2efe9; line-height: 1.55; white-space: pre; }
.status-line { display: inline-flex; align-items: center; gap: 6px; font-size: 12.5px; color: var(--muted); background: var(--surface-2); border-radius: 999px; padding: 4px 10px; margin: 0 0 8px; }
.status-line .spin { width: 9px; height: 9px; border-radius: 50%; background: var(--accent); animation: pulse 1s infinite ease-in-out; }
@keyframes pulse { 0%,100% { opacity: .25; transform: scale(.8); } 50% { opacity: 1; transform: scale(1.15); } }
.msg-err { background: var(--accent-soft); border: 1px solid color-mix(in srgb, var(--accent) 35%, transparent); border-radius: 12px; padding: 12px 14px; font-size: 14.5px; }
.msg-err .detail { font-size: 12px; color: var(--muted); margin-top: 6px; word-break: break-word; }
.msg-err .retry { margin-top: 10px; border: 1px solid var(--border); background: var(--surface); border-radius: 10px; padding: 7px 12px; font-size: 13.5px; }
.msg-tools { display: flex; gap: 8px; margin-top: 6px; }
.msg-tools button { border: none; background: none; color: var(--muted); font-size: 12px; }

/* ---------- empty ---------- */
.empty { position: absolute; inset: 60px 0 150px; display: flex; flex-direction: column; align-items: center; justify-content: center; text-align: center; padding: 0 24px; gap: 6px; pointer-events: none; }
.empty h1 { font-size: 24px; margin: 10px 0 0; }
.empty p { margin: 0; max-width: 330px; font-size: 14.5px; }
.empty .chips { display: flex; flex-wrap: wrap; gap: 8px; justify-content: center; margin-top: 18px; pointer-events: auto; }
.chip { border: 1px solid var(--border); background: var(--surface); border-radius: 999px; padding: 8px 13px; font-size: 13.5px; }

/* ---------- composer ---------- */
.composer-wrap { position: absolute; inset-inline: 0; bottom: 0; padding: 10px 12px calc(10px + env(safe-area-inset-bottom)); background: linear-gradient(to top, var(--bg) 72%, transparent); }
.composer { max-width: 760px; margin: 0 auto; background: var(--surface); border: 1px solid var(--border); border-radius: 20px; padding: 8px 10px 6px; box-shadow: 0 2px 12px rgba(40,30,20,.05); }
.composer textarea { width: 100%; border: none; outline: none; background: transparent; resize: none; font-size: 16px; line-height: 1.5; max-height: 40vh; padding: 8px 6px 4px; }
.composer-bar { display: flex; align-items: center; gap: 6px; }
.attach { font-size: 17px; opacity: .75; }
.files { display: flex; gap: 6px; overflow-x: auto; flex: 1; }
.file-pill { display: inline-flex; align-items: center; gap: 6px; background: var(--surface-2); border-radius: 999px; padding: 4px 9px; font-size: 12px; white-space: nowrap; }
.file-pill button { border: none; background: none; color: var(--muted); font-size: 12px; }
.send { margin-inline-start: auto; width: 36px; height: 36px; border-radius: 50%; border: none; background: var(--accent); color: #fff; font-size: 18px; display: grid; place-items: center; }
.send:disabled { opacity: .45; }
.composer-note { text-align: center; font-size: 11px; color: var(--muted); margin-top: 6px; }

/* ---------- sheets ---------- */
.scrim { position: fixed; inset: 0; background: rgba(0,0,0,.35); z-index: 18; display: none; }
.scrim.on { display: block; }
.sheet { position: fixed; inset: 0; z-index: 30; display: flex; align-items: flex-end; justify-content: center; background: rgba(0,0,0,.32); }
.sheet-card { width: 100%; max-width: 560px; max-height: 86dvh; background: var(--surface); border-radius: 20px 20px 0 0; display: flex; flex-direction: column; padding: 14px 14px calc(14px + env(safe-area-inset-bottom)); box-shadow: var(--shadow); }
.sheet-head { display: flex; align-items: center; justify-content: space-between; }
.sheet-head h2 { font-size: 17px; margin: 4px 0; }
.search { width: 100%; margin: 10px 0; padding: 11px 13px; border-radius: 12px; border: 1px solid var(--border); background: var(--bg); font-size: 15px; }
.ta { width: 100%; padding: 11px 13px; border-radius: 12px; border: 1px solid var(--border); background: var(--bg); font-size: 15px; margin-bottom: 12px; resize: vertical; }
.model-list { overflow-y: auto; -webkit-overflow-scrolling: touch; }
.model-row { display: flex; align-items: center; gap: 10px; padding: 11px 8px; border-bottom: 1px solid var(--border); }
.model-row .mname { font-size: 14.5px; font-weight: 500; }
.model-row .mmeta { font-size: 11.5px; color: var(--muted); }
.model-row .badge { margin-inline-start: auto; font-size: 11px; padding: 3px 8px; border-radius: 999px; background: var(--surface-2); color: var(--muted); }
.model-row .badge.ready { background: var(--accent-soft); color: var(--accent); }
.model-row.sel { background: var(--surface-2); border-radius: 10px; }
.plat-head { font-size: 12px; color: var(--muted); padding: 14px 8px 6px; position: sticky; top: 0; background: var(--surface); }
.settings-body { overflow-y: auto; padding-top: 6px; }
.settings-body h3 { font-size: 14px; margin: 18px 0 8px; }
.key-row { display: flex; align-items: center; gap: 8px; padding: 10px 0; border-bottom: 1px solid var(--border); font-size: 14px; }
.key-row .nm { flex: 1; min-width: 0; }
.key-row .nm small { display: block; color: var(--muted); font-size: 11.5px; }
.key-row .st { font-size: 11.5px; padding: 3px 8px; border-radius: 999px; background: var(--surface-2); color: var(--muted); }
.key-row .st.on { background: var(--accent-soft); color: var(--accent); }
.usage-row { display: flex; justify-content: space-between; font-size: 13.5px; padding: 6px 0; border-bottom: 1px dashed var(--border); }
.btn { border: 1px solid var(--border); background: var(--surface); border-radius: 12px; padding: 11px 14px; font-size: 15px; }
.btn.primary { background: var(--accent); border-color: var(--accent); color: #fff; font-weight: 600; }
.btn.wide { width: 100%; margin-top: 8px; }
.key-input { width: 100%; padding: 10px 12px; border-radius: 10px; border: 1px solid var(--border); background: var(--bg); font-size: 14px; margin: 6px 0; direction: ltr; text-align: left; }
.toast { position: fixed; bottom: 96px; inset-inline: 0; margin: auto; width: fit-content; max-width: 88vw; background: #26251f; color: #fff; padding: 9px 14px; border-radius: 999px; font-size: 13px; z-index: 60; box-shadow: var(--shadow); }

/* ---------- mobile ---------- */
@media (max-width: 860px) {
  .sidebar { position: fixed; top: 0; bottom: 0; inset-inline-start: 0; z-index: 20; transform: translateX(110%); transition: transform .22s ease; box-shadow: var(--shadow); width: 84vw; max-width: 320px; }
  [dir="rtl"] .sidebar { transform: translateX(110%); }
  .sidebar.open { transform: translateX(0); }
  .messages { padding: 14px 12px 130px; }
  .msg.user .bubble { max-width: 92%; }
  .composer textarea { font-size: 16px; }
  .model-chip { max-width: 38vw; font-size: 12px; }
}
@media (min-width: 861px) {
  .scrim { display: none !important; }
  #menu { display: none; }
}

/* ---------- studio ---------- */
.studio-card { width: 100%; max-width: 860px; height: 92dvh; background: var(--surface); border-radius: 20px 20px 0 0; display: flex; flex-direction: column; padding: 12px 12px calc(10px + env(safe-area-inset-bottom)); box-shadow: var(--shadow); }
.studio-tabs { display: flex; gap: 6px; overflow-x: auto; padding: 8px 2px; border-bottom: 1px solid var(--border); }
.stab { border: 1px solid var(--border); background: var(--bg); border-radius: 999px; padding: 7px 13px; font-size: 13px; white-space: nowrap; }
.stab.on { background: var(--accent); border-color: var(--accent); color: #fff; font-weight: 600; }
.studio-body { flex: 1; overflow-y: auto; padding: 10px 2px; -webkit-overflow-scrolling: touch; }
.row-sel { display: flex; gap: 8px; align-items: center; margin-bottom: 8px; flex-wrap: wrap; }
.sel, .num, .txt { border: 1px solid var(--border); background: var(--bg); border-radius: 10px; padding: 8px 10px; font-size: 14px; }
.num { width: 78px; }
.txt { flex: 1; min-width: 120px; }
.btn.sm { padding: 8px 12px; font-size: 13.5px; border-radius: 10px; }
.ed { width: 100%; min-height: 190px; border: 1px solid var(--border); background: var(--code-bg); color: #f2efe9; border-radius: 12px; padding: 12px; font-family: ui-monospace, SFMono-Regular, Menlo, monospace; font-size: 13px; line-height: 1.55; resize: vertical; white-space: pre; overflow-wrap: normal; overflow-x: auto; }
.run-head { display: flex; justify-content: space-between; font-size: 12px; color: var(--muted); margin: 12px 0 6px; }
.console { background: var(--surface-2); border: 1px solid var(--border); border-radius: 12px; padding: 10px; min-height: 70px; max-height: 240px; overflow: auto; font-family: ui-monospace, monospace; font-size: 12.5px; direction: ltr; text-align: left; }
.cline { white-space: pre-wrap; word-break: break-word; padding: 1px 0; }
.cline.err { color: #c0392b; }
.cline.ok { color: var(--ok); }
.cline.dim { color: var(--muted); }
.frame-wrap { border: 1px solid var(--border); border-radius: 12px; overflow: hidden; margin-top: 10px; background: #fff; }
.frame-wrap iframe { width: 100%; height: 300px; border: none; display: block; background: #fff; }
.frame-wrap.tall iframe { height: 380px; background: #0f0f0f; }
.tools-list { display: flex; flex-direction: column; }
.tool-row { display: flex; align-items: center; gap: 10px; padding: 10px 4px; border-bottom: 1px solid var(--border); }
.tool-main { flex: 1; min-width: 0; }
.tool-name { font-size: 14px; font-weight: 600; }
.tool-note { font-size: 11.5px; color: var(--muted); line-height: 1.45; }
.tool-cat { font-size: 11px; color: var(--muted); white-space: nowrap; }
.st { font-size: 11px; padding: 3px 8px; border-radius: 999px; white-space: nowrap; }
.st.on { background: #e3f2e8; color: #2f7a4f; }
.st.off { background: #f6e3e0; color: #a33; }
.st.ref { background: var(--surface-2); color: var(--muted); }
@media (prefers-color-scheme: dark) { .st.on { background: #1f3a29; color: #8fd6a9; } .st.off { background: #3a2320; color: #f0a49a; } }
.vid-result { display: flex; flex-direction: column; gap: 10px; margin-top: 10px; }
.vid-thumb { max-width: 100%; border-radius: 12px; }
.fileinp { width: 100%; margin: 8px 0; font-size: 13px; }
.small { font-size: 12.5px; line-height: 1.6; }
.tool-link { text-decoration: none; color: var(--accent); font-size: 15px; padding: 0 4px; }
