/* حكيم/Codex — نظام التصميم كله هنا. خصائص منطقية حصرًا (RTL أولًا). */
:root {
  color-scheme: light;
  --bg: #f7f8f9;
  --surface: #ffffff;
  --surface-2: #f1f3f5;
  --border: #e3e6ea;
  --text: #16191d;
  --text-dim: #61686f;
  --text-faint: #8b939b;
  --brand: #0f7a5a;
  --brand-soft: #e6f4ef;
  --danger: #c0392b;
  --danger-soft: #fdecea;
  --warn: #b7791f;
  --warn-soft: #fdf5e6;
  --ok: #1f7a3f;
  --mono: ui-monospace, SFMono-Regular, "SF Mono", Menlo, Consolas, monospace;
  --r: 12px;
  --shadow: 0 1px 2px rgba(16,25,32,.06), 0 8px 24px rgba(16,25,32,.08);
  --safe-b: env(safe-area-inset-bottom, 0px);
}
[data-theme="dark"] {
  color-scheme: dark;
  --bg: #0e1113;
  --surface: #171b1e;
  --surface-2: #1f2427;
  --border: #2b3236;
  --text: #e8ecef;
  --text-dim: #9aa4ac;
  --text-faint: #6e7981;
  --brand: #3fbd92;
  --brand-soft: #122d25;
  --danger: #f0705f;
  --danger-soft: #331916;
  --warn: #e0a94a;
  --warn-soft: #302512;
  --ok: #4cc073;
  --shadow: 0 1px 2px rgba(0,0,0,.4), 0 8px 24px rgba(0,0,0,.35);
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { height: 100%; margin: 0; overscroll-behavior-y: none; }
body {
  background: var(--bg);
  color: var(--text);
  font-family: "SF Arabic", -apple-system, BlinkMacSystemFont, "Segoe UI", system-ui, sans-serif;
  font-size: 16px;
  line-height: 1.6;
  -webkit-text-size-adjust: 100%;
}
button, input, textarea, select { font: inherit; color: inherit; }
button { cursor: pointer; border: none; background: none; }

/* ── الهيكل ───────────────────────────────────────────────────────────── */
#app { display: flex; flex-direction: column; height: 100dvh; }

.topbar {
  display: flex; align-items: center; gap: 8px;
  padding: 10px 12px; padding-top: calc(10px + env(safe-area-inset-top, 0px));
  background: var(--surface); border-block-end: 1px solid var(--border);
  position: sticky; inset-block-start: 0; z-index: 30;
}
.topbar h1 { font-size: 16px; font-weight: 650; margin: 0; flex: 1; }
.topbar .sub { font-size: 12px; color: var(--text-faint); font-weight: 400; }

.icon-btn {
  width: 38px; height: 38px; border-radius: 10px; display: grid; place-items: center;
  color: var(--text-dim); flex: none;
}
.icon-btn:active { background: var(--surface-2); }
.icon-btn svg { width: 21px; height: 21px; }

.dot { width: 8px; height: 8px; border-radius: 50%; background: var(--text-faint); flex: none; }
.dot.on { background: var(--ok); }
.dot.off { background: var(--danger); }

/* ── الرسائل ──────────────────────────────────────────────────────────── */
.stream { flex: 1; overflow-y: auto; overflow-x: hidden; padding: 14px 12px 8px; }
.msg { margin-block-end: 14px; max-width: 100%; }
.msg-user {
  background: var(--brand-soft); border: 1px solid transparent;
  border-radius: var(--r) var(--r) 4px var(--r);
  padding: 10px 13px; margin-inline-start: auto; max-width: 88%; width: fit-content;
  white-space: pre-wrap; overflow-wrap: anywhere;
}
.msg-agent { white-space: pre-wrap; overflow-wrap: anywhere; padding-inline: 2px; }
.msg-agent pre, .msg-user pre {
  background: var(--surface-2); border: 1px solid var(--border); border-radius: 8px;
  padding: 10px; overflow-x: auto; font-family: var(--mono); font-size: 13px;
  direction: ltr; text-align: left; margin: 8px 0; white-space: pre;
}
.msg-agent code { font-family: var(--mono); font-size: .89em; direction: ltr; unicode-bidi: embed; }

.role { font-size: 12px; color: var(--text-faint); margin-block-end: 4px; }

/* بطاقة أمر/أداة */
.card {
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--r);
  margin-block-end: 12px; overflow: hidden;
}
.card-head {
  display: flex; align-items: center; gap: 8px; padding: 9px 12px;
  background: var(--surface-2); font-size: 13px; color: var(--text-dim);
}
.card-head .grow { flex: 1; min-width: 0; }
.card-body { padding: 0; }
.card pre {
  margin: 0; padding: 11px 12px; background: transparent; border: none; border-radius: 0;
  font-family: var(--mono); font-size: 12.5px; line-height: 1.55;
  direction: ltr; text-align: left; overflow-x: auto; max-height: 340px; white-space: pre;
}
.cmd { font-family: var(--mono); font-size: 12.5px; direction: ltr; text-align: left;
       overflow-x: auto; white-space: pre; padding: 11px 12px; }

/* ── الموافقات ────────────────────────────────────────────────────────── */
.approval { border-color: var(--warn); box-shadow: var(--shadow); }
.approval .card-head { background: var(--warn-soft); color: var(--warn); font-weight: 650; }
.approval-actions { display: flex; gap: 8px; padding: 11px 12px; border-block-start: 1px solid var(--border); }
.btn {
  flex: 1; padding: 11px 14px; border-radius: 10px; font-weight: 600; font-size: 15px;
  background: var(--surface-2); color: var(--text); min-height: 44px;
}
.btn-ok { background: var(--brand); color: #fff; }
.btn-no { background: var(--danger-soft); color: var(--danger); }
.btn:active { transform: scale(.985); }
.btn:disabled { opacity: .5; }

/* ── الفروق (diff) ────────────────────────────────────────────────────── */
.diff pre { max-height: 60vh; }
.diff .add { color: var(--ok); }
.diff .del { color: var(--danger); }
.diff .hdr { color: var(--text-faint); }

/* ── المُدخِل ─────────────────────────────────────────────────────────── */
.composer {
  border-block-start: 1px solid var(--border); background: var(--surface);
  padding: 10px 12px; padding-block-end: calc(10px + var(--safe-b));
}
.chips { display: flex; gap: 6px; flex-wrap: wrap; margin-block-end: 8px; }
.chip {
  display: inline-flex; align-items: center; gap: 6px; font-size: 12.5px;
  background: var(--surface-2); border: 1px solid var(--border);
  border-radius: 999px; padding: 4px 10px; color: var(--text-dim); max-width: 100%;
}
.chip b { font-weight: 500; color: var(--text); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.chip button { color: var(--text-faint); font-size: 16px; line-height: 1; padding: 0 2px; }

.input-row { display: flex; gap: 8px; align-items: flex-end; }
textarea#prompt {
  flex: 1; resize: none; border: 1px solid var(--border); background: var(--bg);
  border-radius: 12px; padding: 11px 13px; max-height: 40vh; min-height: 46px;
  outline: none; line-height: 1.5;
}
textarea#prompt:focus { border-color: var(--brand); }
.send {
  width: 46px; height: 46px; border-radius: 12px; background: var(--brand); color: #fff;
  display: grid; place-items: center; flex: none;
}
.send:disabled { background: var(--surface-2); color: var(--text-faint); }
.send svg { width: 20px; height: 20px; }

.running-bar {
  display: flex; align-items: center; gap: 10px; padding: 8px 12px;
  background: var(--surface-2); border-block-start: 1px solid var(--border); font-size: 13px;
}
.spinner {
  width: 14px; height: 14px; border: 2px solid var(--border); border-top-color: var(--brand);
  border-radius: 50%; animation: spin .8s linear infinite; flex: none;
}
@keyframes spin { to { transform: rotate(360deg); } }
.stop-btn { color: var(--danger); font-weight: 600; padding: 4px 10px; border-radius: 8px; }

/* ── الدرج الجانبي ────────────────────────────────────────────────────── */
.scrim {
  position: fixed; inset: 0; background: rgba(0,0,0,.45); z-index: 40;
  opacity: 0; pointer-events: none; transition: opacity .2s;
}
.scrim.open { opacity: 1; pointer-events: auto; }
.drawer {
  position: fixed; inset-block: 0; inset-inline-end: 0; width: min(86vw, 340px);
  background: var(--surface); z-index: 50; display: flex; flex-direction: column;
  transform: translateX(100%); transition: transform .22s cubic-bezier(.32,.72,0,1);
  padding-block-start: env(safe-area-inset-top, 0px);
}
html[dir="rtl"] .drawer { transform: translateX(-100%); }
.drawer.open { transform: translateX(0) !important; }
.drawer-head { padding: 14px; border-block-end: 1px solid var(--border); display: flex; align-items: center; gap: 8px; }
.drawer-head h2 { font-size: 15px; margin: 0; flex: 1; }
.thread-list { flex: 1; overflow-y: auto; padding: 8px; }
.thread {
  display: flex; align-items: center; gap: 8px; padding: 11px 12px; border-radius: 10px;
  margin-block-end: 4px; cursor: pointer;
}
.thread:active, .thread.active { background: var(--surface-2); }
.thread .t { flex: 1; min-width: 0; }
.thread .t b { display: block; font-weight: 550; font-size: 14.5px; overflow: hidden;
               text-overflow: ellipsis; white-space: nowrap; }
.thread .t span { font-size: 12px; color: var(--text-faint); }
.drawer-foot { padding: 12px; border-block-start: 1px solid var(--border);
               padding-block-end: calc(12px + var(--safe-b)); display: grid; gap: 8px; }

/* ── الدخول ───────────────────────────────────────────────────────────── */
.login { display: grid; place-items: center; height: 100dvh; padding: 24px; }
.login form { width: 100%; max-width: 340px; display: grid; gap: 12px; }
.login h1 { font-size: 21px; margin: 0 0 4px; text-align: center; }
.login p { margin: 0 0 12px; text-align: center; color: var(--text-faint); font-size: 13.5px; }
.login input {
  padding: 13px 14px; border-radius: 12px; border: 1px solid var(--border);
  background: var(--surface); outline: none; min-height: 48px;
}
.login input:focus { border-color: var(--brand); }
.err { color: var(--danger); font-size: 13.5px; text-align: center; min-height: 20px; }

.empty { text-align: center; color: var(--text-faint); padding: 48px 20px; font-size: 14px; }
.hidden { display: none !important; }

select.policy {
  border: 1px solid var(--border); background: var(--surface); border-radius: 10px;
  padding: 9px 11px; width: 100%; min-height: 42px;
}
label.fld { font-size: 12.5px; color: var(--text-dim); display: block; margin-block-end: 4px; }

.toast {
  position: fixed; inset-block-end: calc(76px + var(--safe-b)); inset-inline: 16px;
  background: var(--text); color: var(--bg); padding: 11px 14px; border-radius: 10px;
  font-size: 14px; text-align: center; z-index: 60; box-shadow: var(--shadow);
}
