/* ===== Musaid — Arabic RTL assistant UI ===== */
:root {
  --bg: #f3f5f9;
  --surface: #ffffff;
  --surface-2: #eef1f6;
  --surface-3: #e4e8f0;
  --text: #0f172a;
  --muted: #64748b;
  --border: #dfe4ec;
  --accent: #0f766e;
  --accent-strong: #115e59;
  --accent-soft: #d6f1ec;
  --on-accent: #ffffff;
  --user: #0f766e;
  --danger: #dc2626;
  --danger-soft: #fee2e2;
  --warn-soft: #fef3c7;
  --code-bg: #0f172a;
  --code-text: #e2e8f0;
  --shadow: 0 10px 30px rgba(15, 23, 42, .12);
  --radius: 16px;
  --font: 'IBM Plex Sans Arabic', system-ui, -apple-system, 'Segoe UI', Tahoma, 'Noto Sans Arabic', sans-serif;
  --mono: ui-monospace, SFMono-Regular, Menlo, Consolas, 'Liberation Mono', monospace;
  color-scheme: light;
}
:root[data-theme="dark"], :root:not([data-theme="light"]).sys-dark {
  --bg: #0b1220;
  --surface: #121a2c;
  --surface-2: #182238;
  --surface-3: #1f2b45;
  --text: #e6eaf2;
  --muted: #94a3b8;
  --border: #243150;
  --accent: #2dd4bf;
  --accent-strong: #5eead4;
  --accent-soft: #13332f;
  --on-accent: #06201d;
  --user: #135e56;
  --danger: #f87171;
  --danger-soft: #3b1515;
  --warn-soft: #3b2f0b;
  --code-bg: #0a0f1c;
  --code-text: #dbe2ee;
  --shadow: 0 10px 30px rgba(0, 0, 0, .45);
  color-scheme: dark;
}

* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body {
  font-family: var(--font);
  font-size: 15px;
  line-height: 1.7;
  color: var(--text);
  background: var(--bg);
  -webkit-font-smoothing: antialiased;
  overscroll-behavior-y: none;
}
button, input, select, textarea { font: inherit; color: inherit; }
a { color: var(--accent); }
.hidden { display: none !important; }
.muted { color: var(--muted); }
.small { font-size: 12.5px; }
.center { text-align: center; }
.grow { flex: 1; }
code { font-family: var(--mono); font-size: .92em; background: var(--surface-2); padding: 1px 5px; border-radius: 6px; }

.view { min-height: 100dvh; }

/* ===== brand ===== */
.brand { font-weight: 700; font-size: 20px; display: flex; align-items: center; gap: 8px; }
.brand.big { font-size: 30px; justify-content: center; margin-bottom: 6px; }
.spark { color: var(--accent); }

/* ===== buttons / inputs ===== */
.btn {
  border: 1px solid var(--border); background: var(--surface); color: var(--text);
  border-radius: 12px; padding: 10px 16px; cursor: pointer; font-weight: 600; font-size: 14px;
  display: inline-flex; align-items: center; justify-content: center; gap: 6px; text-decoration: none;
  transition: background .15s, transform .05s;
}
.btn:hover { background: var(--surface-2); }
.btn:active { transform: translateY(1px); }
.btn:disabled { opacity: .55; cursor: not-allowed; }
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--on-accent); }
.btn.primary:hover { background: var(--accent-strong); border-color: var(--accent-strong); }
.btn.ghost { background: transparent; }
.btn.danger { color: var(--danger); border-color: transparent; }
.btn.danger:hover { background: var(--danger-soft); }
.btn.wide { width: 100%; }
.icon-btn {
  border: none; background: transparent; color: var(--text); width: 40px; height: 40px; border-radius: 12px;
  cursor: pointer; font-size: 18px; display: inline-flex; align-items: center; justify-content: center; flex: none;
}
.icon-btn:hover { background: var(--surface-2); }
input[type="text"], input[type="password"], input[type="search"], select, textarea {
  width: 100%; border: 1px solid var(--border); background: var(--surface); border-radius: 12px; padding: 10px 12px; outline: none;
}
input:focus, select:focus, textarea:focus { border-color: var(--accent); box-shadow: 0 0 0 3px color-mix(in srgb, var(--accent) 22%, transparent); }
textarea { resize: vertical; }
.field { display: flex; flex-direction: column; gap: 6px; margin-bottom: 14px; }
.field > span { font-size: 13px; font-weight: 600; color: var(--muted); }
.row2 { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }
.row-btns { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; margin-top: 8px; }
.error-text { color: var(--danger); font-size: 13px; min-height: 1.2em; margin-top: 8px; white-space: pre-wrap; }

/* ===== login ===== */
#view-login { display: flex; align-items: center; justify-content: center; padding: 24px 16px; }
.login-card {
  width: 100%; max-width: 380px; background: var(--surface); border: 1px solid var(--border); border-radius: 22px;
  padding: 32px 26px; box-shadow: var(--shadow); display: flex; flex-direction: column; gap: 14px; text-align: center;
}
.login-card p { margin: 0; }

/* ===== layout ===== */
#view-app { display: flex; height: 100dvh; overflow: hidden; }
#sidebar {
  width: 300px; flex: none; background: var(--surface); border-inline-start: 1px solid var(--border);
  display: flex; flex-direction: column; padding: 14px 14px calc(14px + env(safe-area-inset-bottom, 0px)); gap: 12px;
  padding-top: calc(14px + env(safe-area-inset-top, 0px));
}
.sb-head { display: flex; align-items: center; justify-content: space-between; }
#btn-close-sb { display: none; }
.session-list { flex: 1; overflow-y: auto; margin: 0 -6px; padding: 0 6px; }
.session-list .group { font-size: 12px; color: var(--muted); font-weight: 600; margin: 12px 6px 4px; }
.session {
  display: block; padding: 9px 12px; border-radius: 12px; text-decoration: none; color: var(--text); margin-bottom: 2px;
}
.session:hover { background: var(--surface-2); }
.session.active { background: var(--accent-soft); }
.s-title { font-weight: 600; font-size: 14px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.s-meta { font-size: 12px; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.s-meta .live-dot { color: var(--accent); }
.remote-block { border-top: 1px solid var(--border); padding-top: 8px; }
.remote-block > summary { cursor: pointer; font-size: 13px; font-weight: 600; color: var(--muted); list-style: none; display: flex; align-items: center; gap: 6px; }
.remote-block > summary::-webkit-details-marker { display: none; }
.remote-block > summary::before { content: '▸'; font-size: 11px; transition: transform .15s; }
.remote-block[open] > summary::before { transform: rotate(90deg); }
.rc-count { font-size: 11px; background: var(--accent-soft); color: var(--accent-strong); border-radius: 999px; padding: 0 7px; }
.rc-count:empty { display: none; }
.remote-list { max-height: 30vh; overflow-y: auto; margin-top: 6px; display: flex; flex-direction: column; gap: 4px; }
.rc-item { border: 1px solid var(--border); border-radius: 10px; padding: 6px 9px; font-size: 12.5px; }
.rc-item .rc-name { font-weight: 600; display: flex; align-items: center; gap: 6px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.rc-item .rc-dot { width: 8px; height: 8px; border-radius: 50%; background: var(--muted); flex: none; }
.rc-item .rc-dot.busy { background: var(--accent); animation: blink 1.2s infinite; }
.rc-item .rc-meta { color: var(--muted); font-size: 11.5px; direction: ltr; text-align: left; unicode-bidi: isolate; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.rc-item .rc-tags { display: flex; gap: 4px; flex-wrap: wrap; margin-top: 3px; }
.rc-item .rc-tag { font-size: 10.5px; padding: 0 6px; border-radius: 999px; background: var(--surface-2); color: var(--muted); }
.rc-item .rc-tag.rc { background: var(--accent-soft); color: var(--accent-strong); }
.rc-note { margin: 6px 0 0; }
.sb-foot { display: flex; gap: 6px; align-items: center; border-top: 1px solid var(--border); padding-top: 10px; }
.sb-foot .btn { flex: 1; }

#main { flex: 1; min-width: 0; display: flex; flex-direction: column; position: relative; }
#chat-head {
  display: flex; align-items: center; gap: 8px; padding: 10px 12px; padding-top: calc(10px + env(safe-area-inset-top, 0px));
  border-bottom: 1px solid var(--border); background: color-mix(in srgb, var(--bg) 85%, transparent); backdrop-filter: blur(10px);
  position: sticky; top: 0; z-index: 5;
}
.head-title { flex: 1; min-width: 0; }
#chat-title { font-weight: 700; font-size: 16px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.chips { display: flex; gap: 6px; flex-wrap: nowrap; overflow-x: auto; scrollbar-width: none; }
.chips::-webkit-scrollbar { display: none; }
.chip {
  font-size: 11.5px; padding: 2px 9px; border-radius: 999px; background: var(--surface-2); color: var(--muted); white-space: nowrap;
  border: 1px solid var(--border); cursor: pointer; flex: none;
}
.chip.accent { background: var(--accent-soft); color: var(--accent-strong); border-color: transparent; }
.chip.warn { background: var(--warn-soft); border-color: transparent; }
#btn-menu { display: none; }

/* ===== messages ===== */
.messages { flex: 1; overflow-y: auto; padding: 18px 14px 24px; scroll-behavior: smooth; }
.messages > * { max-width: 860px; margin-inline: auto; }
.empty { text-align: center; color: var(--muted); padding: 60px 10px 20px; }
.empty .brand { justify-content: center; font-size: 28px; margin-bottom: 8px; }
.empty .hints { display: flex; flex-wrap: wrap; gap: 8px; justify-content: center; margin-top: 18px; }
.empty .hint { border: 1px solid var(--border); background: var(--surface); border-radius: 999px; padding: 6px 14px; cursor: pointer; font-size: 13.5px; }
.empty .hint:hover { background: var(--surface-2); }

.msg { margin-bottom: 18px; display: flex; flex-direction: column; }
.msg.user { align-items: flex-start; }
.msg.user .bubble {
  background: var(--user); color: #fff; border-radius: 18px 4px 18px 18px; padding: 10px 14px; max-width: 85%;
  white-space: pre-wrap; word-break: break-word; line-height: 1.65;
}
.msg.assistant .a-body { display: flex; flex-direction: column; gap: 10px; }
.md { word-break: break-word; }
.md > :first-child { margin-top: 0; }
.md > :last-child { margin-bottom: 0; }
.md p { margin: 0 0 10px; }
.md ul, .md ol { padding-inline-start: 22px; margin: 6px 0 10px; }
.md li { margin: 2px 0; }
.md h1, .md h2, .md h3, .md h4 { margin: 14px 0 8px; line-height: 1.35; }
.md h1 { font-size: 1.35em; } .md h2 { font-size: 1.22em; } .md h3 { font-size: 1.1em; }
.md blockquote { margin: 8px 0; padding: 4px 12px; border-inline-start: 3px solid var(--accent); color: var(--muted); }
.md table { border-collapse: collapse; display: block; overflow-x: auto; max-width: 100%; font-size: 13.5px; margin: 8px 0; }
.md th, .md td { border: 1px solid var(--border); padding: 6px 10px; }
.md th { background: var(--surface-2); }
.md img { max-width: 100%; border-radius: 10px; }
.md hr { border: none; border-top: 1px solid var(--border); margin: 14px 0; }
.md pre {
  position: relative; background: var(--code-bg); color: var(--code-text); padding: 12px 14px; border-radius: 12px;
  overflow-x: auto; direction: ltr; text-align: left; font-size: 13px; line-height: 1.55; margin: 8px 0;
}
.md pre code { background: none; padding: 0; font-size: inherit; color: inherit; }
.copy-btn {
  position: absolute; top: 6px; right: 6px; border: none; background: rgba(255,255,255,.12); color: #fff; border-radius: 8px;
  padding: 2px 8px; font-size: 11px; cursor: pointer;
}
.copy-btn:hover { background: rgba(255,255,255,.22); }

details.think, details.tool {
  border: 1px solid var(--border); background: var(--surface); border-radius: 12px; padding: 0; overflow: hidden;
}
details.think > summary, details.tool > summary {
  cursor: pointer; list-style: none; padding: 8px 12px; font-size: 13px; color: var(--muted); display: flex; align-items: center; gap: 8px;
}
details.think > summary::-webkit-details-marker, details.tool > summary::-webkit-details-marker { display: none; }
details.think > summary::before, details.tool > summary::before { content: '▸'; font-size: 11px; transition: transform .15s; }
details[open].think > summary::before, details[open].tool > summary::before { transform: rotate(90deg); }
.think-body { padding: 4px 14px 12px; color: var(--muted); font-size: 13.5px; white-space: pre-wrap; max-height: 320px; overflow-y: auto; }
details.tool .t-name { font-weight: 600; color: var(--text); }
details.tool .t-short { font-family: var(--mono); font-size: 12px; direction: ltr; unicode-bidi: isolate; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; flex: 1; min-width: 0; color: var(--muted); }
details.tool .t-state { font-size: 11px; padding: 1px 7px; border-radius: 999px; background: var(--surface-2); flex: none; }
details.tool .t-state.err { background: var(--danger-soft); color: var(--danger); }
details.tool .t-state.ok { background: var(--accent-soft); color: var(--accent-strong); }
.t-body { padding: 0 12px 12px; display: flex; flex-direction: column; gap: 8px; }
.t-body pre {
  margin: 0; background: var(--surface-2); border-radius: 10px; padding: 10px; direction: ltr; text-align: left;
  font-family: var(--mono); font-size: 12px; max-height: 300px; overflow: auto; white-space: pre-wrap; word-break: break-word;
}
.t-body .lbl { font-size: 12px; color: var(--muted); }
.t-body pre.err { outline: 1px solid var(--danger); }

.a-error { background: var(--danger-soft); color: var(--danger); border-radius: 12px; padding: 10px 14px; white-space: pre-wrap; font-size: 13.5px; margin-top: 8px; }
.a-note { color: var(--muted); font-size: 12.5px; margin-top: 6px; }
.a-meta { display: flex; flex-wrap: wrap; gap: 10px; color: var(--muted); font-size: 11.5px; margin-top: 8px; }
.typing { display: inline-flex; gap: 4px; padding: 10px 2px 0; }
.typing i { width: 7px; height: 7px; border-radius: 50%; background: var(--accent); opacity: .35; animation: blink 1.2s infinite; }
.typing i:nth-child(2) { animation-delay: .2s; } .typing i:nth-child(3) { animation-delay: .4s; }
@keyframes blink { 0%, 80%, 100% { opacity: .25; } 40% { opacity: 1; } }
.status-bar { font-size: 12.5px; color: var(--muted); padding: 2px 16px; max-width: 860px; margin-inline: auto; width: 100%; }

/* ===== composer ===== */
#composer { padding: 8px 12px calc(10px + env(safe-area-inset-bottom, 0px)); background: var(--bg); border-top: 1px solid var(--border); }
.composer-inner {
  max-width: 860px; margin-inline: auto; display: flex; align-items: flex-end; gap: 8px; background: var(--surface);
  border: 1px solid var(--border); border-radius: 20px; padding: 6px 6px 6px 12px;
}
.composer-inner:focus-within { border-color: var(--accent); box-shadow: 0 0 0 3px color-mix(in srgb, var(--accent) 18%, transparent); }
#input { border: none; background: transparent; padding: 8px 0; max-height: 40vh; resize: none; box-shadow: none; line-height: 1.6; }
#input:focus { box-shadow: none; }
.send {
  width: 40px; height: 40px; border-radius: 50%; border: none; background: var(--accent); color: var(--on-accent); font-size: 20px; font-weight: 700;
  cursor: pointer; flex: none; display: inline-flex; align-items: center; justify-content: center;
}
.send:disabled { opacity: .4; cursor: not-allowed; }
.send.stop { background: var(--danger); color: #fff; font-size: 15px; }
.composer-hint { max-width: 860px; margin: 4px auto 0; font-size: 11.5px; text-align: center; min-height: 1em; }

/* ===== sheet (bottom) ===== */
.sheet { position: fixed; inset: 0; z-index: 50; background: rgba(2, 6, 23, .5); display: flex; align-items: flex-end; justify-content: center; }
.sheet-card {
  width: 100%; max-width: 560px; background: var(--surface); border-radius: 22px 22px 0 0; max-height: 92dvh; display: flex; flex-direction: column;
  box-shadow: var(--shadow); animation: up .18s ease-out;
}
@keyframes up { from { transform: translateY(30px); opacity: .6; } to { transform: none; opacity: 1; } }
.sheet-handle { width: 42px; height: 5px; background: var(--border); border-radius: 999px; margin: 10px auto 0; }
.sheet-head, .modal-head { display: flex; align-items: center; justify-content: space-between; padding: 8px 18px 4px; }
.sheet-head h3, .modal-head h3 { margin: 0; font-size: 17px; }
.sheet-body { padding: 8px 18px; overflow-y: auto; }
.sheet-foot { display: flex; gap: 8px; padding: 10px 18px calc(14px + env(safe-area-inset-bottom, 0px)); border-top: 1px solid var(--border); }
.modes { display: grid; gap: 8px; }
.mode {
  display: flex; gap: 10px; align-items: flex-start; border: 1px solid var(--border); border-radius: 12px; padding: 9px 12px; cursor: pointer; background: var(--surface);
}
.mode input { margin-top: 5px; accent-color: var(--accent); }
.mode.on { border-color: var(--accent); background: var(--accent-soft); }
.mode .m-label { font-weight: 600; font-size: 14px; }
.mode .m-desc { font-size: 12.5px; color: var(--muted); }
.adv summary { cursor: pointer; color: var(--muted); font-size: 13.5px; margin-bottom: 10px; }

/* ===== modal ===== */
.modal { position: fixed; inset: 0; z-index: 60; background: rgba(2, 6, 23, .5); display: flex; align-items: center; justify-content: center; padding: 0; }
.modal-card { width: 100%; max-width: 720px; height: 100dvh; background: var(--surface); display: flex; flex-direction: column; }
.modal-head { padding-top: calc(10px + env(safe-area-inset-top, 0px)); }
.modal-body { flex: 1; overflow-y: auto; padding: 14px 18px calc(18px + env(safe-area-inset-bottom, 0px)); }
.tabs { display: flex; gap: 4px; padding: 0 12px; border-bottom: 1px solid var(--border); overflow-x: auto; }
.tab { border: none; background: transparent; padding: 10px 12px; cursor: pointer; color: var(--muted); font-weight: 600; border-bottom: 2px solid transparent; white-space: nowrap; }
.tab.active { color: var(--accent-strong); border-bottom-color: var(--accent); }
.cards { display: flex; flex-direction: column; gap: 10px; margin-bottom: 14px; }
.card { border: 1px solid var(--border); border-radius: 14px; padding: 14px; background: var(--surface); margin-bottom: 12px; }
.card h4 { margin: 0 0 8px; font-size: 15px; }
.acc-card .acc-top { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; }
.acc-card .acc-name { font-weight: 700; }
.acc-card .badge { font-size: 11px; padding: 1px 8px; border-radius: 999px; background: var(--surface-2); color: var(--muted); }
.acc-card .badge.def { background: var(--accent-soft); color: var(--accent-strong); }
.acc-card .acc-hint { font-family: var(--mono); font-size: 12px; direction: ltr; text-align: left; color: var(--muted); margin: 4px 0; }
.acc-card .acc-status { font-size: 12.5px; margin: 4px 0; white-space: pre-wrap; }
.acc-card .acc-status.ok { color: var(--accent-strong); }
.acc-card .acc-status.err { color: var(--danger); }
.acc-card .acc-actions { display: flex; gap: 6px; flex-wrap: wrap; margin-top: 8px; }
.acc-card .acc-actions .btn { padding: 6px 12px; font-size: 13px; }
.steps { padding-inline-start: 20px; margin: 8px 0 12px; }
.steps li { margin-bottom: 8px; }
.models-list { display: flex; flex-direction: column; gap: 8px; margin-bottom: 10px; }
.model-row { display: grid; grid-template-columns: 1fr 1fr auto; gap: 8px; align-items: center; }
.model-row input { padding: 8px 10px; }
.model-row .del { color: var(--danger); }

/* ===== toast ===== */
.toast {
  position: fixed; bottom: calc(90px + env(safe-area-inset-bottom, 0px)); left: 50%; transform: translateX(-50%); z-index: 100;
  background: var(--text); color: var(--bg); padding: 10px 16px; border-radius: 12px; font-size: 13.5px; box-shadow: var(--shadow); max-width: 90vw; text-align: center;
}
.toast.err { background: var(--danger); color: #fff; }
.toast.ok { background: var(--accent); color: var(--on-accent); }

/* ===== responsive ===== */
@media (max-width: 959px) {
  #sidebar {
    position: fixed; inset-block: 0; inset-inline-start: 0; z-index: 40; transform: translateX(100%); transition: transform .2s ease;
    box-shadow: none; width: min(320px, 86vw);
  }
  #sidebar.open { transform: none; box-shadow: var(--shadow); }
  #backdrop { position: fixed; inset: 0; background: rgba(2, 6, 23, .45); z-index: 30; }
  #btn-menu, #btn-close-sb { display: inline-flex; }
}
@media (min-width: 960px) {
  .modal { padding: 24px; }
  .modal-card { height: auto; max-height: 90vh; border-radius: 22px; box-shadow: var(--shadow); }
  .sheet { align-items: center; }
  .sheet-card { border-radius: 22px; max-height: 88vh; }
  .sheet-handle { display: none; }
  .messages { padding: 24px 24px 28px; }
}
@media (prefers-reduced-motion: reduce) { * { animation: none !important; transition: none !important; } }
