:root {
  --bg: #ffffff; --fg: #111113; --muted: #6b6b73; --line: #e7e7ea;
  --me: #111113; --me-fg: #ffffff; --m: #f2f2f4; --m-fg: #111113;
  --chip: #f4f4f6; --danger: #c0392b; --accent: #111113;
  --radius: 20px;
  font-family: -apple-system, BlinkMacSystemFont, "SF Pro Text", "SF Arabic", "Segoe UI", Roboto,
               "Noto Sans Arabic", "Noto Sans", sans-serif;
  color-scheme: light dark;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0c0c0d; --fg: #f2f2f3; --muted: #9a9aa3; --line: #222226;
    --me: #f2f2f3; --me-fg: #0c0c0d; --m: #1b1b1e; --m-fg: #f2f2f3;
    --chip: #1b1b1e; --accent: #f2f2f3;
  }
}
* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { margin: 0; height: 100%; background: var(--bg); color: var(--fg); }
body { font-size: 16px; line-height: 1.45; overscroll-behavior: none; }
[hidden] { display: none !important; }
button { font: inherit; color: inherit; background: none; border: 0; cursor: pointer; }
svg { width: 22px; height: 22px; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }
.muted { color: var(--muted); }

/* ---------- auth ---------- */
.auth { min-height: 100%; display: grid; place-items: center; padding: 24px 16px; }
.auth-card { width: 100%; max-width: 360px; display: flex; flex-direction: column; gap: 14px; }
.auth-card h1 { margin: 6px 0 0; font-size: 26px; letter-spacing: -0.02em; }
.auth-card p { margin: 0; }
.auth-card label { display: flex; flex-direction: column; gap: 6px; font-size: 14px; color: var(--muted); }
.auth-card input { font: inherit; font-size: 17px; color: var(--fg); background: var(--chip); border: 1px solid var(--line);
  border-radius: 14px; padding: 13px 14px; outline: none; }
.auth-card input:focus { border-color: var(--accent); }
.err { color: var(--danger); min-height: 1.2em; font-size: 14px; }
.primary { background: var(--accent); color: var(--bg); border-radius: 14px; padding: 14px; font-weight: 600; font-size: 17px; }
.primary:disabled { opacity: .5; }
.mark { width: 56px; height: 56px; border-radius: 16px; background: var(--accent); color: var(--bg);
  display: grid; place-items: center; font-weight: 800; font-size: 28px; letter-spacing: -0.04em; }
.mark.big { width: 64px; height: 64px; font-size: 32px; border-radius: 20px; }

/* ---------- app shell ---------- */
.app { height: 100dvh; display: flex; flex-direction: column; }
.top { display: flex; align-items: center; justify-content: space-between; gap: 8px;
  padding: calc(env(safe-area-inset-top) + 6px) 8px 6px; border-bottom: 1px solid var(--line); background: var(--bg); }
.icon { width: 44px; height: 44px; display: grid; place-items: center; border-radius: 12px; }
.icon:active { background: var(--chip); }
.title { display: flex; align-items: center; gap: 8px; min-width: 0; font-weight: 600; }
.title span:last-child { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; max-width: 60vw; }
.dot { width: 26px; height: 26px; flex: none; border-radius: 8px; background: var(--accent); color: var(--bg);
  display: grid; place-items: center; font-size: 14px; font-weight: 800; }

/* ---------- messages ---------- */
.messages { flex: 1; overflow-y: auto; padding: 16px 16px 8px; display: flex; flex-direction: column; gap: 10px;
  -webkit-overflow-scrolling: touch; }
.row { display: flex; flex-direction: column; max-width: 100%; }
.row.user { align-items: flex-end; }
.row.m { align-items: flex-start; }
.bubble { max-width: min(86%, 680px); padding: 10px 14px; border-radius: var(--radius); overflow-wrap: anywhere; }
.user .bubble { background: var(--me); color: var(--me-fg); border-bottom-right-radius: 6px; white-space: pre-wrap; }
.m .bubble { background: var(--m); color: var(--m-fg); border-bottom-left-radius: 6px; }
.m.error .bubble { border: 1px solid var(--danger); }
.bubble p { margin: 0 0 8px; } .bubble p:last-child { margin-bottom: 0; }
.bubble ul, .bubble ol { margin: 4px 0 8px; padding-inline-start: 22px; }
.bubble li { margin: 2px 0; }
.bubble a { color: inherit; text-decoration: underline; text-underline-offset: 2px; }
.bubble code { font-family: ui-monospace, SFMono-Regular, Menlo, monospace; font-size: 14px;
  background: rgba(127,127,127,.15); padding: 1px 5px; border-radius: 6px; }
.bubble pre { margin: 6px 0; padding: 10px 12px; overflow-x: auto; border-radius: 12px; background: rgba(127,127,127,.15); }
.bubble pre code { background: none; padding: 0; }
.meta { display: flex; gap: 10px; align-items: center; font-size: 12px; color: var(--muted); padding: 3px 6px 0; }
.copy { font-size: 12px; color: var(--muted); padding: 2px 0; }
.typing .bubble { display: flex; align-items: center; gap: 10px; color: var(--muted); }
.dots { display: inline-flex; gap: 4px; }
.dots i { width: 6px; height: 6px; border-radius: 50%; background: currentColor; opacity: .35; animation: blink 1.2s infinite; }
.dots i:nth-child(2) { animation-delay: .2s; } .dots i:nth-child(3) { animation-delay: .4s; }
@keyframes blink { 0%, 80%, 100% { opacity: .25; } 40% { opacity: .9; } }

/* ---------- empty state ---------- */
.empty { flex: 1; display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 14px;
  padding: 24px 16px; text-align: center; }
.empty h2 { margin: 4px 0 6px; font-size: 21px; font-weight: 600; letter-spacing: -0.01em; }
.chips { display: flex; flex-wrap: wrap; justify-content: center; gap: 8px; max-width: 520px; }
.chip { background: var(--chip); border: 1px solid var(--line); border-radius: 999px; padding: 9px 14px; font-size: 15px; }
.chip:active { opacity: .7; }

/* ---------- composer ---------- */
.composer { display: flex; align-items: flex-end; gap: 8px; padding: 8px 12px calc(env(safe-area-inset-bottom) + 10px);
  border-top: 1px solid var(--line); background: var(--bg); }
.composer textarea { flex: 1; resize: none; font: inherit; font-size: 17px; color: var(--fg); background: var(--chip);
  border: 1px solid var(--line); border-radius: 22px; padding: 10px 16px; max-height: 40vh; outline: none; line-height: 1.35; }
.composer textarea:focus { border-color: var(--accent); }
.send { width: 44px; height: 44px; flex: none; border-radius: 50%; background: var(--accent); color: var(--bg);
  display: grid; place-items: center; }
.send:disabled { opacity: .35; }
.send .i-stop { display: none; fill: currentColor; stroke: none; }
.send.stop .i-send { display: none; } .send.stop .i-stop { display: block; }

/* ---------- drawer ---------- */
.scrim { position: fixed; inset: 0; background: rgba(0,0,0,.35); z-index: 10; }
.drawer { position: fixed; top: 0; bottom: 0; left: 0; width: min(84vw, 340px); background: var(--bg); z-index: 11;
  transform: translateX(-102%); transition: transform .22s ease; display: flex; flex-direction: column;
  padding: calc(env(safe-area-inset-top) + 12px) 0 calc(env(safe-area-inset-bottom) + 12px); border-right: 1px solid var(--line); }
.drawer.open { transform: none; }
.drawer-head { display: flex; align-items: center; gap: 10px; padding: 4px 16px 12px; }
.drawer-head strong { flex: 1; font-size: 18px; }
.pill { background: var(--accent); color: var(--bg); border-radius: 999px; padding: 7px 14px; font-size: 14px; font-weight: 600; }
.chats { flex: 1; overflow-y: auto; padding: 0 8px; }
.chat-item { display: flex; align-items: center; gap: 6px; border-radius: 12px; }
.chat-item.active { background: var(--chip); }
.chat-item .open { flex: 1; text-align: start; padding: 11px 10px; min-width: 0; }
.chat-item .t { display: block; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.chat-item .s { display: block; font-size: 12px; color: var(--muted); }
.chat-item .more { width: 36px; height: 36px; display: grid; place-items: center; color: var(--muted); }
.link { color: var(--muted); padding: 12px 18px; text-align: start; font-size: 14px; }

@media (min-width: 900px) {
  .app { display: grid; grid-template-columns: 300px 1fr; grid-template-rows: auto 1fr auto; }
  .drawer { position: static; transform: none; width: auto; grid-row: 1 / 4; grid-column: 1; }
  .scrim, #menu { display: none !important; }
  .top, .messages, .empty, .composer { grid-column: 2; }
  .messages, .empty { grid-row: 2; }
}

/* ---------- admin: steps, permission cards, mode ---------- */
.steps { max-width: min(86%, 680px); margin-top: 4px; font-size: 13px; color: var(--muted); }
.steps summary { cursor: pointer; padding: 2px 6px; list-style: none; }
.steps summary::before { content: "▸ "; }
.steps[open] summary::before { content: "▾ "; }
.steps ol { margin: 4px 0 0; padding-inline-start: 26px; display: flex; flex-direction: column; gap: 4px; }
.steps li span { display: block; color: var(--fg); }
.steps code { display: block; font-family: ui-monospace, SFMono-Regular, Menlo, monospace; font-size: 12px;
  white-space: pre-wrap; overflow-wrap: anywhere; color: var(--muted); }
.typing { align-items: flex-start; }
.perm { align-self: stretch; max-width: 680px; border: 1.5px solid var(--accent); border-radius: 18px; padding: 14px;
  display: flex; flex-direction: column; gap: 8px; background: var(--bg); }
.perm-h { font-size: 13px; color: var(--muted); }
.perm-t { font-weight: 600; }
.perm-d { font-family: ui-monospace, SFMono-Regular, Menlo, monospace; font-size: 13px; background: var(--chip);
  border-radius: 10px; padding: 8px 10px; white-space: pre-wrap; overflow-wrap: anywhere; max-height: 30vh; overflow: auto; }
.perm-b { display: flex; flex-wrap: wrap; gap: 8px; }
.perm-b button { border-radius: 999px; padding: 9px 16px; font-size: 15px; font-weight: 600; background: var(--chip); border: 1px solid var(--line); }
.perm-b .pa { background: var(--accent); color: var(--bg); border-color: var(--accent); }
.perm-b .pd { color: var(--danger); }
.perm-b button:disabled { opacity: .5; }
.mode { flex: none; height: 44px; padding: 0 12px; border-radius: 22px; font-size: 13px; font-weight: 600;
  background: var(--chip); border: 1px solid var(--line); color: var(--muted); }
.mode[data-mode="ask"] { color: var(--fg); border-color: var(--accent); }
.mode[data-mode="plan"] { color: var(--fg); border-style: dashed; border-color: var(--accent); }
.toast { position: fixed; left: 50%; bottom: calc(env(safe-area-inset-bottom) + 84px); transform: translate(-50%, 12px);
  max-width: min(92vw, 520px); background: var(--fg); color: var(--bg); padding: 10px 14px; border-radius: 14px;
  font-size: 14px; opacity: 0; pointer-events: none; transition: opacity .2s, transform .2s; z-index: 20; text-align: center; }
.toast.on { opacity: 1; transform: translate(-50%, 0); }
