:root {
  --bg: #0f1115; --panel: #161a21; --panel-2: #1d222b; --line: #2a303b;
  --text: #e7eaf0; --muted: #8e97a8; --accent: #5b9cff; --accent-2: #2f6fe0;
  --danger: #ff6b6b; --ok: #4cc38a; --warn: #f5b84b;
  --radius: 12px; --side-w: 290px;
  color-scheme: dark;
  font-family: "Plex Arabic", "Segoe UI", "Noto Sans Arabic", "Noto Naskh Arabic", Tahoma, system-ui, sans-serif;
}
* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body { background: var(--bg); color: var(--text); font-size: 15.5px; line-height: 1.7; }
[hidden] { display: none !important; }
a { color: var(--accent); }
button, input, select, textarea { font: inherit; color: inherit; }
input, select, textarea {
  background: var(--panel-2); border: 1px solid var(--line); border-radius: 10px; padding: .55em .75em; min-width: 0;
}
input:focus-visible, select:focus-visible, textarea:focus-visible, button:focus-visible {
  outline: 2px solid var(--accent); outline-offset: 1px;
}
.muted { color: var(--muted); }
.error { color: var(--danger); min-height: 1.2em; margin: .3em 0 0; font-size: .9em; }

.btn {
  border: 1px solid var(--line); background: var(--panel-2); border-radius: 10px; padding: .5em 1em;
  cursor: pointer; text-decoration: none; display: inline-flex; align-items: center; justify-content: center; gap: .4em;
}
.btn:hover { border-color: #3a4352; }
.btn.primary { background: var(--accent-2); border-color: var(--accent-2); color: #fff; }
.btn.primary:hover { background: var(--accent); }
.btn.ghost { background: transparent; }
.btn.small { padding: .25em .7em; font-size: .87em; }
.btn.icon { padding: .3em .6em; }
.btn.wide { width: 100%; }
.btn.danger { color: var(--danger); }
.btn:disabled { opacity: .5; cursor: default; }
.card { background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); }

/* login */
.login { min-height: 100%; display: grid; place-items: center; padding: 16px; }
.login-card { width: min(380px, 100%); padding: 28px; display: grid; gap: 14px; }
.login-card h1 { margin: 0 0 6px; font-size: 1.5em; justify-content: center; }
.brand { display: flex; align-items: center; gap: 10px; font-weight: 700; }
.brand img { flex: none; }
.side-brand { font-size: 1.1em; padding: 2px 4px; }
.login-card label { display: grid; gap: 4px; font-size: .92em; color: var(--muted); }

/* layout */
.app { display: flex; height: 100%; height: 100dvh; }
.sidebar {
  width: var(--side-w); flex: none; background: var(--panel); border-inline-end: 1px solid var(--line);
  display: flex; flex-direction: column; min-height: 0;
}
.side-top { padding: 12px; display: grid; gap: 10px; }
.thread-list { flex: 1; overflow-y: auto; padding: 0 8px 8px; }
.thread {
  display: flex; gap: 8px; align-items: baseline; width: 100%; text-align: start; padding: .5em .6em; border-radius: 8px;
  background: none; border: 0; cursor: pointer; color: var(--text);
}
.thread:hover { background: var(--panel-2); }
.thread.active { background: #232a36; }
.thread .t-title { flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.thread .t-cost { font-size: .75em; color: var(--muted); direction: ltr; }
.side-bottom { border-top: 1px solid var(--line); padding: 10px 12px; display: grid; gap: 6px; }
.side-actions { display: flex; flex-wrap: wrap; gap: 6px; }

.main { flex: 1; display: flex; flex-direction: column; min-width: 0; }
.topbar { display: flex; align-items: center; gap: 10px; padding: 10px 16px; border-bottom: 1px solid var(--line); }
.thread-title { flex: 1; margin: 0; font-size: 1.05em; font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.thread-actions { display: flex; gap: 6px; }
#toggle-side { display: none; }

.messages { flex: 1; overflow-y: auto; padding: 20px 16px; scroll-behavior: smooth; }
.messages > * { max-width: 860px; margin-inline: auto; }
.empty { text-align: center; padding-top: 12vh; }
.empty h1 { font-size: 1.9em; margin: 0 0 .2em; }
.examples { display: flex; flex-wrap: wrap; justify-content: center; gap: 8px; margin-top: 18px; }
.chip { background: var(--panel); border: 1px solid var(--line); border-radius: 999px; padding: .4em 1em; cursor: pointer; }
.chip:hover { border-color: var(--accent); }

.msg { margin-bottom: 18px; }
.msg.user { display: flex; justify-content: flex-start; }
.msg.user .bubble { background: #233150; border-radius: 14px; padding: .55em 1em; max-width: 85%; white-space: pre-wrap; overflow-wrap: anywhere; }
.msg.assistant .body { background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); padding: 14px 18px; overflow-wrap: anywhere; }
.badge { display: inline-block; font-size: .75em; border: 1px solid var(--line); border-radius: 999px; padding: 0 .6em; color: var(--muted); margin-bottom: 6px; }
.meta { display: flex; flex-wrap: wrap; gap: 6px 12px; align-items: center; font-size: .8em; color: var(--muted); margin-top: 6px; padding-inline: 4px; }
.meta .cost { direction: ltr; unicode-bidi: isolate; }
.meta a { color: var(--muted); }

/* report (.md) styles: report.css */

/* progress */
.progress { border: 1px dashed var(--line); border-radius: 10px; padding: 10px 12px; margin-bottom: 10px; font-size: .88em; }
.progress .step { display: flex; gap: 8px; align-items: center; }
.progress .step-text { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; direction: auto; }
.spinner { width: 14px; height: 14px; flex: none; border: 2px solid var(--line); border-top-color: var(--accent); border-radius: 50%; animation: spin .8s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }
@media (prefers-reduced-motion: reduce) { .spinner { animation-duration: 3s; } .messages { scroll-behavior: auto; } }
.progress .stats { display: flex; flex-wrap: wrap; gap: 4px 14px; color: var(--muted); margin-top: 4px; }
.progress details { margin-top: 4px; }
.progress summary { cursor: pointer; color: var(--muted); }
.progress ol { margin: 4px 0 0; padding-inline-start: 1.4em; max-height: 220px; overflow-y: auto; direction: ltr; text-align: left; }
.progress li a { color: var(--muted); }

/* composer */
.composer { border-top: 1px solid var(--line); padding: 10px 16px calc(10px + env(safe-area-inset-bottom)); background: var(--bg); }
.composer > * { max-width: 860px; margin-inline: auto; }
.composer-row { display: flex; flex-wrap: wrap; gap: 8px; align-items: center; margin-bottom: 8px; }
.modes { display: inline-flex; background: var(--panel); border: 1px solid var(--line); border-radius: 10px; padding: 3px; }
.mode { background: none; border: 0; border-radius: 8px; padding: .3em .8em; cursor: pointer; color: var(--muted); white-space: nowrap; }
.mode.active { background: var(--accent-2); color: #fff; }
#depth, #lang { padding: .35em .6em; font-size: .9em; }
.suggestion {
  display: block; width: auto; margin-bottom: 8px; background: #1e2a1f; border: 1px solid #2f5a3a; color: #bfe8c9;
  border-radius: 999px; padding: .3em 1em; cursor: pointer; font-size: .88em; text-align: start;
}
.input-row { display: flex; gap: 8px; align-items: flex-end; }
.attach { height: 44px; width: 44px; padding: 0; flex: none; font-size: 1.15em; }
.attachments { display: flex; flex-wrap: wrap; gap: 8px; margin-bottom: 8px; }
.thumb { position: relative; width: 64px; height: 64px; border-radius: 10px; overflow: hidden; border: 1px solid var(--line); background: var(--panel-2); }
.thumb img { width: 100%; height: 100%; object-fit: cover; display: block; }
.thumb.loading { display: grid; place-items: center; }
.thumb .remove { position: absolute; top: 2px; inset-inline-end: 2px; width: 22px; height: 22px; padding: 0; border-radius: 50%; border: 0; background: rgba(0,0,0,.65); color: #fff; cursor: pointer; line-height: 1; }
.composer.dragging { outline: 2px dashed var(--accent); outline-offset: -6px; }
.msg.user { flex-direction: column; align-items: flex-start; gap: 6px; }
.msg-images { display: flex; flex-wrap: wrap; gap: 6px; max-width: 85%; }
.msg-images a { display: block; width: 120px; height: 120px; border-radius: 10px; overflow: hidden; border: 1px solid var(--line); }
.msg-images img { width: 100%; height: 100%; object-fit: cover; display: block; }
.candidates { display: flex; flex-wrap: wrap; gap: 8px; margin-top: 10px; }
@media (max-width: 820px) { .msg-images a { width: 88px; height: 88px; } }
#input { flex: 1; resize: none; max-height: 40vh; line-height: 1.5; padding: .65em .9em; }
.send { height: 44px; width: 48px; font-size: 1.1em; transform: scaleX(-1); }

/* dialogs */
.dialog { color: var(--text); padding: 16px; width: min(640px, calc(100vw - 32px)); }
.dialog::backdrop { background: rgba(0,0,0,.6); }
.dialog-head { display: flex; justify-content: space-between; align-items: center; margin-bottom: 8px; }
.dialog-head h3 { margin: 0; }
.table-wrap { overflow-x: auto; }
.users { width: 100%; border-collapse: collapse; font-size: .9em; }
.users th, .users td { padding: .4em; border-bottom: 1px solid var(--line); text-align: start; white-space: nowrap; }
.users td .btn { margin-inline-end: 4px; }
.row-form { display: flex; flex-wrap: wrap; gap: 8px; margin-top: 12px; }
.row-form input { flex: 1 1 140px; }
.stack { display: grid; gap: 10px; }

/* mobile */
@media (max-width: 820px) {
  #toggle-side { display: inline-flex; }
  .sidebar { position: fixed; inset-block: 0; inset-inline-start: 0; z-index: 20; width: min(86vw, var(--side-w)); transform: translateX(100%); transition: transform .2s; }
  .sidebar.open { transform: none; }
  .scrim { position: fixed; inset: 0; background: rgba(0,0,0,.5); z-index: 10; }
  .messages { padding: 14px 12px; }
  .msg.assistant .body { padding: 12px; }
  .composer { padding-inline: 12px; }
  .mode { padding: .3em .55em; font-size: .92em; }
}
.ok { color: var(--ok); }
.pad { padding: 0 .6em; }
td .error { margin: 0; }

/* permissions */
.budget { font-size: .85em; }
.perms { display: flex; flex-wrap: wrap; gap: 8px 14px; align-items: flex-end; padding: 6px 0; white-space: normal; }
.perms fieldset { border: 1px solid var(--line); border-radius: 8px; padding: 4px 10px 6px; margin: 0; display: flex; flex-wrap: wrap; gap: 4px 12px; }
.perms legend { font-size: .85em; color: var(--muted); padding: 0 4px; }
.perms label { display: inline-flex; align-items: center; gap: 4px; font-size: .9em; }
.perms label:has(input[type=number]) { display: grid; gap: 2px; color: var(--muted); }
.perms input[type=number] { width: 9em; }
.users .perms-row td { white-space: normal; background: var(--panel-2); }
.sub { margin: .6em 0 .2em; font-size: .95em; color: var(--muted); }
