:root {
  --brand: #165a46; --brand-2: #1e7a5f; --gold: #e0b95b;
  --bg: #f3f1ea; --surface: #ffffff; --surface-2: #f8f6f0; --line: #e4dfd2;
  --text: #1d2722; --muted: #66746c; --soft: #8a958e;
  --mine: #e3f1ea; --bot: #ffffff; --accent: #1e7a5f;
  --q: #b7791f; --w: #2f6fd6; --r: #c0392b; --ok: #2e8b57;
  --shadow: 0 1px 2px rgba(20,40,30,.06), 0 6px 24px rgba(20,40,30,.06);
  --radius: 14px;
  --font: -apple-system, BlinkMacSystemFont, "Segoe UI", "SF Arabic", "Geeza Pro", "Noto Sans Arabic", Tahoma, sans-serif;
}
:root[data-theme="dark"] {
  --brand: #1f7a5e; --brand-2: #28946f; --gold: #e6c36c;
  --bg: #0d1512; --surface: #141f1b; --surface-2: #18251f; --line: #24342d;
  --text: #e6eee9; --muted: #9db0a6; --soft: #7d8f86;
  --mine: #173a2e; --bot: #18251f; --accent: #49c095;
  --q: #e0a33a; --w: #6ea8ff; --r: #ef7b70; --ok: #5ccf94;
  --shadow: 0 1px 2px rgba(0,0,0,.3), 0 8px 28px rgba(0,0,0,.25);
}
* { box-sizing: border-box }
html, body { height: 100% }
body { margin: 0; background: var(--bg); color: var(--text); font: 15.5px/1.6 var(--font); -webkit-font-smoothing: antialiased }
button, input, select, textarea { font: inherit; color: inherit }
a { color: var(--accent) }
.app { display: flex; height: 100vh; height: 100dvh }

/* ---------- sidebar ---------- */
.side { width: 320px; flex-shrink: 0; background: var(--surface); border-inline-end: 1px solid var(--line); display: flex; flex-direction: column; min-height: 0 }
.brand { display: flex; align-items: center; gap: 12px; padding: 18px 18px 12px }
.logo svg { width: 40px; height: 40px; display: block }
.brand-text { display: flex; flex-direction: column; line-height: 1.25; flex: 1; min-width: 0 }
.brand-text b { font-size: 17px }
.brand-text small { color: var(--muted); font-size: 12.5px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap }
.new-btn { margin: 6px 16px 10px; padding: 12px 16px; border: 0; border-radius: 12px; background: linear-gradient(135deg, var(--brand), var(--brand-2)); color: #fff; font-weight: 600; cursor: pointer; box-shadow: var(--shadow); display: flex; gap: 8px; align-items: center; justify-content: center; transition: transform .12s }
.new-btn:hover { transform: translateY(-1px) }
.new-btn span:first-child { font-size: 18px; line-height: 1 }
.nav { padding: 0 10px }
.nav-item { display: flex; gap: 10px; align-items: center; padding: 9px 10px; border-radius: 10px; color: var(--text); text-decoration: none }
.nav-item:hover, .nav-item.on { background: var(--surface-2) }
.search { padding: 8px 16px }
.search input { width: 100%; padding: 9px 12px; border-radius: 10px; border: 1px solid var(--line); background: var(--surface-2); outline: none }
.search input:focus { border-color: var(--accent) }
.tabs { display: flex; gap: 4px; padding: 4px 12px 8px }
.tabs button { flex: 1; border: 0; background: none; padding: 7px 4px; border-radius: 9px; cursor: pointer; color: var(--muted); font-size: 13.5px; display: flex; gap: 6px; justify-content: center; align-items: center }
.tabs button.on { background: var(--surface-2); color: var(--text); font-weight: 600 }
.tabs i { font-style: normal; font-size: 11.5px; min-width: 20px; padding: 0 6px; border-radius: 9px; background: var(--line); color: var(--text) }
.tabs i:empty { display: none }
.threads { flex: 1; overflow-y: auto; padding: 0 8px 12px; min-height: 0 }
.th { display: block; padding: 10px 12px; border-radius: 12px; margin: 2px 0; cursor: pointer; text-decoration: none; color: var(--text); position: relative; border: 1px solid transparent }
.th:hover { background: var(--surface-2) }
.th.on { background: var(--mine); border-color: color-mix(in srgb, var(--accent) 25%, transparent) }
.th-top { display: flex; gap: 8px; align-items: center }
.th-title { flex: 1; font-weight: 600; font-size: 14.5px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap }
.th-time { color: var(--soft); font-size: 12px; flex-shrink: 0 }
.th-sub { display: flex; gap: 6px; align-items: center; margin-top: 3px; color: var(--muted); font-size: 12.5px; overflow: hidden; white-space: nowrap }
.th-sub .site { overflow: hidden; text-overflow: ellipsis }
.unread::after { content: ""; position: absolute; inset-inline-end: 10px; bottom: 12px; width: 9px; height: 9px; border-radius: 50%; background: var(--gold); box-shadow: 0 0 0 3px var(--surface) }
.empty-list { color: var(--soft); text-align: center; padding: 28px 12px; font-size: 14px }
.side-foot { display: flex; gap: 8px; padding: 12px 16px; border-top: 1px solid var(--line) }
.side-foot select { flex: 1; padding: 7px 10px; border-radius: 9px; border: 1px solid var(--line); background: var(--surface-2) }
.icon-btn { border: 1px solid var(--line); background: var(--surface-2); border-radius: 9px; width: 38px; height: 38px; cursor: pointer; display: grid; place-items: center; flex-shrink: 0 }
.close-side, .menu-btn, .scrim { display: none }

/* ---------- status pills ---------- */
.pill { display: inline-flex; align-items: center; gap: 6px; font-size: 12px; font-weight: 600; padding: 2px 9px; border-radius: 999px; white-space: nowrap; flex-shrink: 0 }
.pill::before { content: ""; width: 7px; height: 7px; border-radius: 50% ; background: currentColor }
.pill.queued { color: var(--q); background: color-mix(in srgb, var(--q) 13%, transparent) }
.pill.working, .pill.stopping { color: var(--w); background: color-mix(in srgb, var(--w) 13%, transparent) }
.pill.working::before, .pill.stopping::before { animation: pulse 1.2s infinite }
.pill.replied { color: var(--accent); background: color-mix(in srgb, var(--accent) 13%, transparent) }
.pill.done { color: var(--soft); background: color-mix(in srgb, var(--soft) 13%, transparent) }
@keyframes pulse { 50% { opacity: .25 } }

/* ---------- main ---------- */
.main { flex: 1; display: flex; flex-direction: column; min-width: 0; min-height: 0 }
.topbar { display: none }
.view { flex: 1; overflow-y: auto; min-height: 0; display: flex; flex-direction: column }
.page { width: 100%; max-width: 1100px; margin: 0 auto; padding: 32px 32px 48px }
.hello { font-size: 26px; font-weight: 700; margin: 0 0 4px }
.lead { color: var(--muted); margin: 0 0 24px }
.stats { display: grid; grid-template-columns: repeat(auto-fit, minmax(170px, 1fr)); gap: 14px; margin-bottom: 30px }
.stat { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); padding: 16px 18px; box-shadow: var(--shadow); cursor: pointer; text-decoration: none; color: inherit }
.stat b { display: block; font-size: 28px; line-height: 1.2 }
.stat span { color: var(--muted); font-size: 13.5px }
.section-h { display: flex; align-items: center; justify-content: space-between; gap: 12px; margin: 8px 0 14px }
.section-h h2 { font-size: 18px; margin: 0 }
.section-h input { max-width: 260px; padding: 8px 12px; border-radius: 10px; border: 1px solid var(--line); background: var(--surface); outline: none }
.sites { display: grid; grid-template-columns: repeat(auto-fill, minmax(240px, 1fr)); gap: 14px }
.site-card { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); padding: 16px; display: flex; flex-direction: column; gap: 10px; box-shadow: var(--shadow); transition: transform .12s, border-color .12s }
.site-card:hover { transform: translateY(-2px); border-color: color-mix(in srgb, var(--accent) 40%, var(--line)) }
.site-head { display: flex; gap: 12px; align-items: center }
.site-ico { width: 40px; height: 40px; border-radius: 11px; display: grid; place-items: center; font-weight: 700; color: #fff; flex-shrink: 0; font-size: 17px }
.site-name { font-weight: 650; line-height: 1.3 }
.site-url { color: var(--muted); font-size: 12.5px; direction: ltr; unicode-bidi: plaintext; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; display: block; text-decoration: none }
.site-url:hover { color: var(--accent) }
.site-meta { color: var(--soft); font-size: 12.5px; min-height: 1.4em }
.site-actions { display: flex; gap: 8px; margin-top: auto }
.btn { border: 1px solid var(--line); background: var(--surface-2); padding: 8px 14px; border-radius: 10px; cursor: pointer; text-decoration: none; color: var(--text); font-size: 14px; display: inline-flex; align-items: center; gap: 6px; justify-content: center }
.btn:hover { border-color: var(--accent) }
.btn.primary { background: var(--accent); border-color: var(--accent); color: #fff }
.btn.primary:hover { filter: brightness(1.08) }
.btn.ghost { background: none }
.btn.danger { color: var(--r) }
.btn[disabled] { opacity: .5; pointer-events: none }
.site-actions .btn { flex: 1; padding: 7px 10px; font-size: 13.5px }

/* ---------- new task ---------- */
.form-card { background: var(--surface); border: 1px solid var(--line); border-radius: 18px; padding: 24px; box-shadow: var(--shadow); max-width: 760px; margin: 0 auto; width: 100% }
.form-card h1 { margin: 0 0 4px; font-size: 22px }
.field { display: flex; flex-direction: column; gap: 6px; margin-top: 16px }
.field label { font-weight: 600; font-size: 14px }
.field .hint { color: var(--soft); font-size: 12.5px }
.inp { width: 100%; padding: 11px 13px; border-radius: 11px; border: 1px solid var(--line); background: var(--surface-2); outline: none }
.inp:focus { border-color: var(--accent); box-shadow: 0 0 0 3px color-mix(in srgb, var(--accent) 15%, transparent) }
textarea.inp { min-height: 150px; resize: vertical; line-height: 1.7 }
.examples { display: flex; flex-wrap: wrap; gap: 8px; margin-top: 8px }
.chip { border: 1px dashed var(--line); background: none; border-radius: 999px; padding: 5px 12px; font-size: 13px; cursor: pointer; color: var(--muted) }
.chip:hover { border-color: var(--accent); color: var(--accent) }
.drop { border: 2px dashed var(--line); border-radius: 14px; padding: 18px; text-align: center; color: var(--muted); cursor: pointer; transition: .15s }
.drop.over, .drop:hover { border-color: var(--accent); color: var(--accent); background: color-mix(in srgb, var(--accent) 5%, transparent) }
.files-chosen { display: flex; flex-wrap: wrap; gap: 8px; margin-top: 10px }
.fchip { display: inline-flex; gap: 8px; align-items: center; padding: 5px 6px 5px 10px; border-radius: 10px; background: var(--surface-2); border: 1px solid var(--line); font-size: 13px; max-width: 100% }
.fchip img { width: 28px; height: 28px; object-fit: cover; border-radius: 6px }
.fchip span { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; max-width: 220px }
.fchip button { border: 0; background: none; cursor: pointer; color: var(--soft); padding: 0 4px }
.form-actions { display: flex; justify-content: flex-end; gap: 10px; margin-top: 22px; align-items: center }
.kbd-hint { color: var(--soft); font-size: 12.5px; margin-inline-end: auto }
.err { color: var(--r); font-size: 13.5px }

/* ---------- thread ---------- */
.thead { position: sticky; top: 0; z-index: 2; background: color-mix(in srgb, var(--bg) 88%, transparent); backdrop-filter: blur(10px); border-bottom: 1px solid var(--line); padding: 14px 28px }
.thead-row { display: flex; gap: 12px; align-items: center; flex-wrap: wrap; max-width: 900px; margin: 0 auto }
.thead h1 { font-size: 19px; margin: 0; flex: 1; min-width: 200px; cursor: text; border-radius: 8px; padding: 2px 6px; margin-inline-start: -6px }
.thead h1:hover { background: var(--surface-2) }
.thead h1 input { width: 100%; font-size: 19px; font-weight: 700; border: 1px solid var(--accent); border-radius: 8px; padding: 2px 6px; background: var(--surface) }
.thead-meta { display: flex; gap: 10px; align-items: center; color: var(--muted); font-size: 13px; flex-wrap: wrap; max-width: 900px; margin: 6px auto 0 }
.thead-meta select { border: 1px solid var(--line); background: var(--surface); border-radius: 8px; padding: 3px 8px; font-size: 13px; max-width: 240px }
.thead-actions { display: flex; gap: 8px }
.activity { max-width: 900px; margin: 12px auto 0; background: var(--surface); border: 1px solid color-mix(in srgb, var(--w) 30%, var(--line)); border-radius: 12px; padding: 10px 14px; display: flex; gap: 12px; align-items: flex-start }
.activity.queued { border-color: color-mix(in srgb, var(--q) 35%, var(--line)) }
.spinner { width: 18px; height: 18px; border-radius: 50%; border: 2.5px solid color-mix(in srgb, var(--w) 25%, transparent); border-top-color: var(--w); animation: spin .8s linear infinite; flex-shrink: 0; margin-top: 3px }
.activity.queued .spinner { border-color: color-mix(in srgb, var(--q) 25%, transparent); border-top-color: var(--q); animation-duration: 2s }
@keyframes spin { to { transform: rotate(360deg) } }
.act-body { flex: 1; min-width: 0 }
.act-now { font-weight: 600; font-size: 14px }
.act-sub { color: var(--muted); font-size: 12.5px }
.act-steps { margin: 6px 0 0; padding: 0; list-style: none; font-size: 12.5px; color: var(--muted); max-height: 0; overflow: hidden; transition: max-height .25s }
.act-steps.open { max-height: 300px; overflow-y: auto }
.act-steps li { padding: 2px 0; display: flex; gap: 8px }
.act-steps li span:last-child { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; direction: ltr; unicode-bidi: plaintext }
.act-toggle { border: 0; background: none; color: var(--accent); cursor: pointer; font-size: 12.5px; padding: 0 }
.posts { flex: 1; width: 100%; max-width: 900px; margin: 0 auto; padding: 22px 28px 12px; display: flex; flex-direction: column; gap: 16px }
.post { display: flex; gap: 12px; align-items: flex-start; max-width: 100% }
.avatar { width: 36px; height: 36px; border-radius: 11px; display: grid; place-items: center; color: #fff; font-weight: 700; flex-shrink: 0; font-size: 15px }
.avatar.bot { background: linear-gradient(135deg, #c9773b, #e0b95b); font-size: 17px }
.bubble { flex: 1; min-width: 0; background: var(--surface); border: 1px solid var(--line); border-radius: 4px 16px 16px 16px; padding: 10px 14px 12px; box-shadow: var(--shadow) }
[dir="rtl"] .bubble { border-radius: 16px 4px 16px 16px }
.post.me .bubble { background: var(--mine); border-color: color-mix(in srgb, var(--accent) 22%, var(--line)) }
.post.err .bubble { border-color: color-mix(in srgb, var(--r) 45%, var(--line)) }
.b-head { display: flex; gap: 8px; align-items: baseline; margin-bottom: 4px }
.b-head b { font-size: 14px }
.b-head time { color: var(--soft); font-size: 12px }
.b-body { word-wrap: break-word; overflow-wrap: anywhere; line-height: 1.75 }
.b-body p { margin: 0 0 .6em } .b-body p:last-child { margin-bottom: 0 }
.b-body ul, .b-body ol { margin: .3em 0 .6em; padding-inline-start: 1.4em }
.b-body code { background: var(--surface-2); border: 1px solid var(--line); border-radius: 5px; padding: 0 4px; font-size: .92em; direction: ltr; unicode-bidi: isolate }
.b-body pre { background: var(--surface-2); border: 1px solid var(--line); border-radius: 9px; padding: 10px; overflow-x: auto; direction: ltr; text-align: left }
.b-body pre code { border: 0; padding: 0; background: none }
.tbl { overflow-x: auto; margin: .4em 0 .7em }
.b-body table { border-collapse: collapse; font-size: 14px; min-width: 60% }
.b-body th, .b-body td { border: 1px solid var(--line); padding: 6px 10px; text-align: start; vertical-align: top }
.b-body th { background: var(--surface-2); font-weight: 600 }
.b-body h3 { font-size: 15.5px; margin: .6em 0 .3em }
.b-body a { word-break: break-all }
.b-files { display: flex; flex-wrap: wrap; gap: 8px; margin-top: 10px }
.b-files img { max-width: min(320px, 100%); max-height: 260px; border-radius: 10px; display: block; border: 1px solid var(--line) }
.b-files audio { width: min(360px, 100%) }
.b-files video { max-width: min(420px, 100%); border-radius: 10px }
.file-link { display: inline-flex; gap: 8px; align-items: center; padding: 7px 11px; border-radius: 10px; background: var(--surface-2); border: 1px solid var(--line); text-decoration: none; color: var(--text); font-size: 13.5px; max-width: 100% }
.file-link span { overflow: hidden; text-overflow: ellipsis; white-space: nowrap }
.file-link small { color: var(--soft) }
.composer { position: sticky; bottom: 0; background: linear-gradient(to top, var(--bg) 70%, transparent); padding: 10px 28px 18px }
.composer-box { max-width: 900px; margin: 0 auto; background: var(--surface); border: 1px solid var(--line); border-radius: 16px; box-shadow: var(--shadow); padding: 8px; transition: border-color .15s }
.composer-box:focus-within { border-color: var(--accent) }
.composer-box.over { border-color: var(--accent); background: color-mix(in srgb, var(--accent) 5%, var(--surface)) }
.composer textarea { width: 100%; border: 0; outline: none; background: none; resize: none; padding: 8px 10px; min-height: 44px; max-height: 40vh; line-height: 1.6 }
.composer-row { display: flex; align-items: center; gap: 8px }
.composer-row .kbd-hint { margin-inline-end: auto }
.done-note { max-width: 900px; margin: 0 auto 8px; text-align: center; color: var(--muted); font-size: 13.5px }

.toast { position: fixed; bottom: 24px; left: 50%; transform: translateX(-50%) translateY(20px); background: var(--text); color: var(--bg); padding: 10px 18px; border-radius: 12px; opacity: 0; transition: .25s; pointer-events: none; z-index: 50; font-size: 14px }
.toast.show { opacity: 1; transform: translateX(-50%) }
.loading { margin: auto; color: var(--soft) }

/* ---------- mobile ---------- */
@media (max-width: 860px) {
  .side { position: fixed; inset-block: 0; inset-inline-start: 0; z-index: 30; width: min(320px, 86vw); transform: translateX(-105%); transition: transform .22s; box-shadow: var(--shadow) }
  [dir="rtl"] .side { transform: translateX(105%) }
  .app.side-open .side { transform: none }
  .app.side-open .scrim { display: block; position: fixed; inset: 0; background: rgba(0,0,0,.35); z-index: 20 }
  .close-side, .menu-btn { display: grid }
  .topbar { display: flex; gap: 10px; align-items: center; padding: 10px 14px; border-bottom: 1px solid var(--line); background: var(--surface) }
  .top-title { font-weight: 650; overflow: hidden; text-overflow: ellipsis; white-space: nowrap }
  .page { padding: 20px 16px 40px }
  .thead { padding: 12px 16px } .posts { padding: 16px 14px 8px } .composer { padding: 8px 10px 12px }
  .form-card { padding: 18px; border-radius: 14px }
  .hello { font-size: 22px }
}
