/* لوحة وكلاء المهندس — كل الأصول محلية (بدون CDN)، RTL، والتكبير معطّل على الموبايل */
@font-face { font-family: 'latif'; src: url('/fonts/Amiri-Bold.woff2') format('woff2'); font-weight: 400 900; font-display: swap; }

:root {
  --bg: #0C1418; --panel: #121d23; --line: #223038; --text: #eef3f1; --muted: #8fa0a2;
  --green: #006847; --green2: #0a8a60; --lime: #AECB1F; --orange: #F5A12A; --red: #e5484d;
  --bot: #17252c; --me: #0b5a41; --vh: 100dvh;
  color-scheme: dark;
}
@media (prefers-color-scheme: light) {
  :root { --bg: #f3f5f2; --panel: #ffffff; --line: #dfe5e0; --text: #0C1418; --muted: #5d6b6b; --bot: #ffffff; --me: #d9f2e6; color-scheme: light; }
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { margin: 0; height: 100%; background: var(--bg); color: var(--text); overscroll-behavior: none; }
body { font-family: 'latif', 'Amiri', system-ui, sans-serif; font-size: 17px; line-height: 1.7; touch-action: manipulation; -webkit-text-size-adjust: 100%; text-size-adjust: 100%; }
[hidden] { display: none !important; }
button, input, textarea { font: inherit; color: inherit; }
/* 16px أو أكثر حتى الآيفون ما يكبّر الشاشة عند الكتابة */
input, textarea { font-size: 17px; }
button { cursor: pointer; border: 0; background: none; padding: 0; touch-action: manipulation; }
a { color: var(--lime); }
@media (prefers-color-scheme: light) { a { color: var(--green); } }
.muted { color: var(--muted); } .small { font-size: 14px; margin: 0 0 8px; } .err { color: var(--red); min-height: 1.6em; margin: 4px 0 0; }

/* ---------- الدخول */
#auth { min-height: var(--vh); display: grid; place-items: center; padding: 24px; }
.card { width: min(380px, 100%); background: var(--panel); border: 1px solid var(--line); border-radius: 20px; padding: 28px 22px; text-align: center; display: grid; gap: 12px; }
.card .logo { width: 84px; height: 84px; border-radius: 22px; margin: 0 auto; }
.card h1 { margin: 0; font-size: 26px; } .card p { margin: 0; }
input[type=password], input[type=text] { width: 100%; padding: 12px 14px; border-radius: 12px; border: 1px solid var(--line); background: var(--bg); outline: none; }
input:focus, textarea:focus { border-color: var(--green2); }
.primary { background: var(--green); color: #fff; border-radius: 12px; padding: 12px; width: 100%; }
.primary:active { background: var(--green2); } .primary:disabled { opacity: .6; }

/* ---------- الهيكل */
#app { position: fixed; inset: 0 0 auto 0; height: var(--vh); display: flex; flex-direction: column; }
header { display: flex; align-items: center; gap: 10px; padding: calc(8px + env(safe-area-inset-top)) 12px 8px; background: var(--panel); border-bottom: 1px solid var(--line); }
header .logo { width: 40px; height: 40px; border-radius: 12px; }
.title { flex: 1; min-width: 0; display: grid; line-height: 1.35; }
.title strong { font-size: 18px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
#status { font-size: 13px; color: var(--muted); display: flex; align-items: center; gap: 6px; }
.dot { width: 9px; height: 9px; border-radius: 50%; background: var(--orange); display: inline-block; }
.dot.ok { background: #2ecc71; } .dot.bad { background: var(--red); }
.icon { width: 42px; height: 42px; border-radius: 12px; font-size: 21px; display: grid; place-items: center; color: var(--muted); flex: none; }
.icon:active { background: var(--line); }
.chip { border: 1px solid var(--line); border-radius: 999px; padding: 4px 12px; font-size: 15px; display: flex; gap: 6px; align-items: center; flex: none; }
.chip b { background: var(--line); border-radius: 999px; min-width: 24px; padding: 0 6px; font-size: 13px; }
.chip.on { background: var(--orange); color: #111; border-color: var(--orange); } .chip.on b { background: rgba(0,0,0,.2); }
.chip.has b { background: var(--orange); color: #111; }

main { flex: 1; overflow-y: auto; overflow-x: hidden; padding: 10px 10px 16px; -webkit-overflow-scrolling: touch; overscroll-behavior: contain; display: flex; flex-direction: column; gap: 8px; }
#older, #empty { text-align: center; font-size: 14px; padding: 8px; }
#empty { margin: auto; }
.day { align-self: center; font-size: 13px; color: var(--muted); background: var(--panel); border: 1px solid var(--line); border-radius: 999px; padding: 0 12px; margin: 6px 0; }

/* ---------- الرسائل */
.msg { display: flex; flex-direction: column; max-width: 94%; min-width: 0; }
.msg.out { align-self: flex-start; align-items: stretch; width: 94%; }
.msg.in { align-self: flex-end; align-items: flex-end; max-width: 84%; }
.bubble { background: var(--bot); border: 1px solid var(--line); border-radius: 16px; padding: 8px 12px 4px; min-width: 0; overflow: hidden; }
.msg.out .bubble { border-start-start-radius: 6px; }
.msg.in .bubble { background: var(--me); border-color: transparent; border-end-end-radius: 6px; }
.text { white-space: pre-wrap; overflow-wrap: anywhere; unicode-bidi: plaintext; }
.text code, .text pre { font-family: ui-monospace, Menlo, monospace; font-size: 14px; background: rgba(127,127,127,.18); border-radius: 6px; padding: 1px 5px; direction: ltr; unicode-bidi: embed; }
.text pre { display: block; padding: 8px; overflow-x: auto; white-space: pre; }
.text blockquote { margin: 4px 0; padding-inline-start: 10px; border-inline-start: 3px solid var(--green2); color: var(--muted); }
.meta { display: flex; justify-content: flex-end; align-items: center; gap: 10px; font-size: 12px; color: var(--muted); line-height: 1.6; direction: ltr; }
.meta button { font-size: 15px; color: var(--muted); padding: 0 4px; }
.quote { border-inline-start: 3px solid var(--lime); padding: 0 8px; margin-bottom: 4px; font-size: 14px; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.gone { color: var(--muted); font-size: 14px; }

.grid { display: grid; gap: 4px; margin: 0 -6px 6px; border-radius: 10px; overflow: hidden; }
.grid.n1 { grid-template-columns: 1fr; } .grid.n2, .grid.n3, .grid.n4 { grid-template-columns: 1fr 1fr; } .grid.nx { grid-template-columns: 1fr 1fr 1fr; }
.tile { position: relative; background: #05090b; aspect-ratio: 1; overflow: hidden; display: block; width: 100%; }
.grid.n1 .tile { aspect-ratio: var(--ar, 1); max-height: 70vh; }
.tile img, .tile video { width: 100%; height: 100%; object-fit: contain; display: block; background: #05090b; }
.tile .num { position: absolute; top: 4px; inset-inline-start: 4px; background: rgba(0,0,0,.6); color: #fff; font-size: 12px; border-radius: 999px; padding: 0 7px; line-height: 1.6; font-family: system-ui, sans-serif; }

.doc { display: flex; align-items: center; gap: 10px; padding: 6px 0; border-bottom: 1px dashed var(--line); }
.doc:last-of-type { border-bottom: 0; }
.doc .thumb { width: 52px; height: 52px; border-radius: 10px; background: #05090b; object-fit: cover; flex: none; display: grid; place-items: center; font-size: 24px; }
.doc .info { flex: 1; min-width: 0; line-height: 1.4; }
.doc .info b { display: block; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; direction: ltr; text-align: right; font-size: 14px; font-family: system-ui, sans-serif; font-weight: 600; }
.doc .info small { color: var(--muted); font-size: 12px; font-family: system-ui, sans-serif; }
.doc .act { width: 40px; height: 40px; border-radius: 10px; background: var(--line); display: grid; place-items: center; font-size: 18px; text-decoration: none; color: var(--text); flex: none; }
audio { width: 100%; margin: 4px 0; }

.kb { display: grid; gap: 6px; margin-top: 6px; }
.kb .r { display: flex; gap: 6px; }
.kb button, .kb a { flex: 1; min-width: 0; background: var(--panel); border: 1px solid var(--green2); color: var(--text); border-radius: 12px; padding: 9px 8px; font-size: 16px; text-align: center; text-decoration: none; line-height: 1.4; overflow-wrap: anywhere; }
.kb button:active { background: var(--green); color: #fff; }
.kb button.busy { opacity: .55; pointer-events: none; }
.kb button.busy::after { content: ' …'; }

#list.only-kb .msg:not(.has-kb), #list.only-kb .day { display: none; }

/* ---------- أسفل الشاشة */
footer { background: var(--panel); border-top: 1px solid var(--line); padding: 6px 8px calc(6px + env(safe-area-inset-bottom)); }
.composer { display: flex; align-items: flex-end; gap: 4px; }
textarea { flex: 1; min-width: 0; resize: none; border: 1px solid var(--line); background: var(--bg); border-radius: 18px; padding: 8px 14px; max-height: 150px; outline: none; line-height: 1.6; }
.send { width: 44px; height: 44px; border-radius: 50%; background: var(--green); color: #fff; font-size: 18px; flex: none; transform: scaleX(-1); }
.send:active { background: var(--green2); } .send:disabled { opacity: .5; }
.bar { display: flex; align-items: center; gap: 8px; padding: 2px 6px 6px; font-size: 14px; position: relative; }
.bar span { flex: 1; min-width: 0; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; border-inline-start: 3px solid var(--lime); padding-inline-start: 8px; }
.bar .icon { width: 32px; height: 32px; font-size: 15px; }
#stageProg { position: absolute; inset-inline-start: 0; bottom: 0; height: 3px; width: 0; background: var(--lime); border-radius: 3px; transition: width .15s; }

.fab { position: absolute; inset-inline-end: 14px; bottom: calc(86px + env(safe-area-inset-bottom)); width: 44px; height: 44px; border-radius: 50%; background: var(--panel); border: 1px solid var(--line); font-size: 20px; box-shadow: 0 4px 14px rgba(0,0,0,.35); z-index: 3; color: var(--text); }
.fab b { position: absolute; top: -8px; inset-inline-start: -6px; background: var(--orange); color: #111; border-radius: 999px; font-size: 12px; min-width: 22px; padding: 0 5px; line-height: 1.7; font-family: system-ui, sans-serif; }
#typing { position: absolute; inset-inline-start: 14px; bottom: calc(70px + env(safe-area-inset-bottom)); font-size: 13px; color: var(--muted); background: var(--panel); border: 1px solid var(--line); border-radius: 999px; padding: 0 12px; z-index: 2; }
#typing span { display: inline-block; width: 5px; height: 5px; border-radius: 50%; background: var(--lime); margin-inline-end: 2px; animation: blink 1.2s infinite; }
#typing span:nth-child(2) { animation-delay: .2s; } #typing span:nth-child(3) { animation-delay: .4s; }
@keyframes blink { 0%, 80%, 100% { opacity: .25; } 40% { opacity: 1; } }

/* ---------- القوائم السفلية */
#backdrop { position: fixed; inset: 0; background: rgba(0,0,0,.5); z-index: 10; }
.sheet { position: fixed; inset: auto 0 0 0; z-index: 11; background: var(--panel); border-radius: 20px 20px 0 0; padding: 14px 14px calc(18px + env(safe-area-inset-bottom)); max-height: 82vh; overflow-y: auto; border-top: 1px solid var(--line); }
.sheet h2 { margin: 0 0 4px; font-size: 20px; }
.row { display: flex; align-items: center; gap: 12px; width: 100%; text-align: start; padding: 10px 6px; border-bottom: 1px solid var(--line); }
.row:active { background: var(--line); }
.row > span { font-size: 22px; width: 34px; text-align: center; flex: none; }
.row > div { flex: 1; min-width: 0; line-height: 1.45; } .row b { display: block; font-weight: inherit; } .row small { color: var(--muted); font-size: 13px; display: block; }
.row.danger b { color: var(--red); }
.row .edit { width: 40px; height: 40px; border-radius: 10px; background: var(--line); display: grid; place-items: center; font-size: 17px; flex: none; }
.row code { direction: ltr; unicode-bidi: embed; font-family: ui-monospace, Menlo, monospace; font-size: 14px; color: var(--lime); }
@media (prefers-color-scheme: light) { .row code { color: var(--green); } }
#pwForm { display: grid; gap: 8px; padding: 10px 0; }

/* ---------- عارض الصور */
#lb { position: fixed; inset: 0; z-index: 20; background: #000; color: #fff; touch-action: none; }
.lbTop { position: absolute; inset: 0 0 auto 0; display: flex; justify-content: space-between; align-items: center; padding: calc(6px + env(safe-area-inset-top)) 10px 6px; z-index: 2; background: linear-gradient(#000a, #0000); font-family: system-ui, sans-serif; font-size: 14px; }
.lbTop .icon { color: #fff; }
#lbStage { position: absolute; inset: 0; display: grid; place-items: center; overflow: hidden; }
#lbImg { max-width: 100%; max-height: 100%; transform-origin: center; will-change: transform; user-select: none; -webkit-user-select: none; -webkit-touch-callout: default; }
.lbNav { position: absolute; top: 50%; width: 44px; height: 64px; margin-top: -32px; font-size: 34px; color: #fff; background: rgba(0,0,0,.35); z-index: 2; border-radius: 12px; }
#lbPrev { inset-inline-start: 6px; } #lbNext { inset-inline-end: 6px; }

#toast { position: fixed; inset-inline: 16px; bottom: calc(96px + env(safe-area-inset-bottom)); z-index: 30; margin: auto; width: fit-content; max-width: calc(100% - 32px); background: #000d; color: #fff; padding: 8px 16px; border-radius: 14px; font-size: 15px; text-align: center; white-space: pre-wrap; }

@media (min-width: 820px) {
  #app { max-width: 820px; margin: 0 auto; inset-inline: 0; border-inline: 1px solid var(--line); }
  .sheet { max-width: 520px; margin: 0 auto; }
  .msg.out { width: auto; max-width: 78%; }
}
