:root {
  --bg: #FAF8F4;
  --panel: #FFFFFF;
  --line: #ECE6DC;
  --text: #1F1C18;
  --muted: #7B746A;
  --accent: #B08D57;
  --accent-ink: #7E6236;
  --accent-soft: #F4ECDD;
  --ai: #5B4FC9;
  --ai-soft: #EEECFB;
  --human: #1E8358;
  --human-soft: #E3F4EB;
  --alert: #D64545;
  --alert-soft: #FCE9E9;
  --note: #FFF5D1;
  --radius: 12px;
  --font: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, "Noto Sans Arabic", sans-serif;
  color-scheme: light;
}
* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body { font-family: var(--font); font-size: 15.5px; line-height: 1.45; color: var(--text); background: var(--bg); -webkit-font-smoothing: antialiased; }
button, input, textarea, select { font: inherit; color: inherit; }
a { color: var(--accent-ink); }
[hidden] { display: none !important; }
h1, h2, h3 { text-transform: uppercase; letter-spacing: .06em; font-weight: 700; margin: 0; }
h1 { font-size: 20px; }
h2 { font-size: 15px; }
h3 { font-size: 13.5px; color: var(--muted); }

/* ---------- buttons & inputs ---------- */
.btn { border: 1px solid var(--line); background: var(--panel); border-radius: 10px; padding: 9px 14px; cursor: pointer; font-weight: 600; font-size: 13.5px; letter-spacing: .04em; white-space: nowrap; }
.btn:hover { border-color: var(--accent); }
.btn.primary { background: var(--accent); border-color: var(--accent); color: #fff; }
.btn.primary:hover { background: var(--accent-ink); }
.btn.ghost { background: transparent; text-align: left; }
.btn.ai { background: var(--ai-soft); border-color: #D9D5F6; color: var(--ai); }
.btn.human { background: var(--human-soft); border-color: #C5E8D6; color: var(--human); }
.btn.danger { color: var(--alert); }
.btn.small { padding: 6px 10px; font-size: 12.5px; }
.btn.wide { width: 100%; }
.btn:disabled { opacity: .5; cursor: default; }
.icon-btn { border: 0; background: transparent; font-size: 20px; width: 40px; height: 40px; border-radius: 10px; cursor: pointer; display: inline-flex; align-items: center; justify-content: center; flex: none; }
.icon-btn:hover, .icon-btn.on { background: var(--accent-soft); }
input[type=text], input[type=email], input[type=password], input[type=search], input[type=number], input:not([type]), textarea, select {
  width: 100%; border: 1px solid var(--line); background: #fff; border-radius: 10px; padding: 10px 12px; outline: none;
}
input:focus, textarea:focus, select:focus { border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-soft); }
label { display: block; font-size: 13.5px; font-weight: 600; color: var(--muted); margin: 12px 0 0; }
label > input, label > textarea, label > select { margin-top: 6px; color: var(--text); font-weight: 400; }
.alert { background: var(--alert-soft); color: var(--alert); padding: 10px 12px; border-radius: 10px; margin: 10px 0; }
.ok-box { background: var(--human-soft); color: var(--human); padding: 10px 12px; border-radius: 10px; margin: 10px 0; }

/* ---------- login / install ---------- */
.auth-page { display: flex; align-items: center; justify-content: center; padding: 16px; min-height: 100%; }
.auth-card { width: 100%; max-width: 400px; background: var(--panel); border: 1px solid var(--line); border-radius: 18px; padding: 28px; }
.auth-card h1 { text-align: center; margin-bottom: 8px; }
.auth-card .btn { margin-top: 20px; padding: 12px; }
.auth-logo { display: block; width: 56px; height: 56px; margin: 0 auto 12px; }
.checks { list-style: none; padding: 0; margin: 12px 0; font-size: 14px; }
.checks .ok { color: var(--human); } .checks .bad { color: var(--alert); font-weight: 700; }

/* ---------- layout ---------- */
#app { display: grid; grid-template-columns: 236px 360px 1fr; height: 100vh; height: 100dvh; }
.sidebar { background: var(--panel); border-right: 1px solid var(--line); display: flex; flex-direction: column; padding: 14px 12px; overflow-y: auto; }
.brand { display: flex; align-items: center; gap: 10px; font-weight: 800; letter-spacing: .08em; padding: 4px 6px 14px; }
.brand img { width: 30px; height: 30px; }
.filters a { display: flex; justify-content: space-between; align-items: center; padding: 10px 12px; border-radius: 10px; text-decoration: none; color: var(--text); font-weight: 600; font-size: 14px; letter-spacing: .03em; text-transform: uppercase; }
.filters a:hover { background: var(--bg); }
.filters a.active { background: var(--accent-soft); color: var(--accent-ink); }
.badge { min-width: 22px; height: 22px; padding: 0 7px; border-radius: 11px; background: var(--line); color: var(--text); font-size: 12px; display: inline-flex; align-items: center; justify-content: center; font-weight: 700; }
.badge.red { background: var(--alert); color: #fff; }
.side-section { margin-top: 18px; }
.side-title { font-size: 12px; font-weight: 700; color: var(--muted); letter-spacing: .08em; padding: 0 12px 6px; }
.num-filter { display: flex; align-items: center; gap: 8px; width: 100%; border: 0; background: transparent; padding: 8px 12px; border-radius: 10px; cursor: pointer; text-align: left; font-size: 14px; }
.num-filter:hover { background: var(--bg); }
.num-filter.active { background: var(--accent-soft); font-weight: 600; }
.dot { width: 10px; height: 10px; border-radius: 50%; flex: none; display: inline-block; }
.side-bottom { margin-top: auto; padding-top: 16px; display: grid; gap: 6px; }
.me-line { font-size: 13px; color: var(--muted); padding: 8px 12px 0; }

.list-pane { border-right: 1px solid var(--line); display: flex; flex-direction: column; min-width: 0; background: var(--panel); }
.list-head { display: flex; align-items: center; gap: 8px; padding: 16px 16px 8px; min-height: 56px; }
.search { padding: 4px 14px 10px; }
.conv-list { overflow-y: auto; flex: 1; }
.conv { display: grid; grid-template-columns: 44px 1fr auto; gap: 10px; padding: 12px 14px; border-bottom: 1px solid var(--line); cursor: pointer; align-items: center; }
.conv:hover { background: var(--bg); }
.conv.active { background: var(--accent-soft); }
.avatar { width: 44px; height: 44px; border-radius: 50%; background: var(--bg); border: 2px solid var(--accent); display: flex; align-items: center; justify-content: center; font-weight: 700; color: var(--accent-ink); font-size: 16px; text-transform: uppercase; }
.conv-main { min-width: 0; }
.conv-name { font-weight: 650; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.conv-preview { color: var(--muted); font-size: 14px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; margin-top: 1px; }
.conv.unread .conv-name, .conv.unread .conv-preview { color: var(--text); font-weight: 700; }
.conv-meta { display: flex; flex-direction: column; align-items: flex-end; gap: 5px; font-size: 12px; color: var(--muted); }
.conv-tags { display: flex; gap: 5px; margin-top: 4px; flex-wrap: wrap; }
.tag { font-size: 11px; font-weight: 700; letter-spacing: .04em; padding: 2px 7px; border-radius: 6px; text-transform: uppercase; background: var(--bg); color: var(--muted); }
.tag.ai { background: var(--ai-soft); color: var(--ai); }
.tag.human { background: var(--human-soft); color: var(--human); }
.tag.closed { background: var(--line); }
.tag.attention { background: var(--alert); color: #fff; }
.tag.num { color: #fff; }
.list-empty { padding: 40px 20px; text-align: center; color: var(--muted); }
.load-more { display: block; margin: 12px auto; }

/* ---------- chat ---------- */
.chat-pane { display: flex; flex-direction: column; min-width: 0; background: var(--bg); }
.empty-chat { margin: auto; text-align: center; color: var(--muted); }
.empty-chat img { width: 64px; opacity: .5; }
.chat { display: flex; flex-direction: column; height: 100%; min-height: 0; }
.chat-head { display: flex; align-items: center; gap: 10px; padding: 10px 16px; background: var(--panel); border-bottom: 1px solid var(--line); min-height: 64px; flex-wrap: wrap; }
.chat-who { flex: 1; min-width: 160px; cursor: pointer; }
.chat-who .name { font-weight: 700; font-size: 16px; }
.chat-who .sub { color: var(--muted); font-size: 13px; }
.chat-actions { display: flex; gap: 6px; align-items: center; flex-wrap: wrap; }
.chat-actions select { width: auto; padding: 8px 10px; font-size: 13.5px; }
.handover-bar { background: var(--alert-soft); color: var(--alert); padding: 9px 16px; font-size: 14px; font-weight: 600; }
.handover-bar.info { background: var(--ai-soft); color: var(--ai); }
.messages { flex: 1; overflow-y: auto; padding: 18px 6%; display: flex; flex-direction: column; gap: 6px; }
.day { align-self: center; font-size: 12px; color: var(--muted); background: var(--panel); border: 1px solid var(--line); padding: 3px 10px; border-radius: 8px; margin: 10px 0 4px; }
.msg { max-width: min(560px, 82%); padding: 8px 12px 6px; border-radius: 14px; background: var(--panel); border: 1px solid var(--line); word-wrap: break-word; overflow-wrap: anywhere; position: relative; }
.msg .body, .msg.system { white-space: pre-wrap; }
.msg.in { align-self: flex-start; border-top-left-radius: 4px; }
.msg.out { align-self: flex-end; background: var(--accent-soft); border-color: #E9DCC4; border-top-right-radius: 4px; }
.msg.out.ai { background: var(--ai-soft); border-color: #DDD9F7; }
.msg.note { align-self: center; background: var(--note); border-color: #F1E3A6; max-width: 90%; font-size: 14px; }
.msg.system { align-self: center; background: transparent; border: 0; color: var(--muted); font-size: 13px; text-align: center; padding: 2px 8px; }
.msg .who { font-size: 11.5px; font-weight: 700; letter-spacing: .04em; text-transform: uppercase; color: var(--muted); margin-bottom: 2px; }
.msg.out.ai .who { color: var(--ai); }
.msg .meta { font-size: 11.5px; color: var(--muted); text-align: right; margin-top: 2px; }
.msg .meta .ticks.read { color: #2F8FE0; }
.msg .err { color: var(--alert); font-size: 12.5px; margin-top: 4px; white-space: normal; }
.msg img.media { display: block; max-width: 100%; max-height: 320px; border-radius: 10px; margin-bottom: 4px; cursor: zoom-in; }
.msg audio, .msg video { max-width: 100%; display: block; margin-bottom: 4px; }
.msg .file { display: inline-flex; gap: 8px; align-items: center; padding: 8px 10px; background: var(--bg); border-radius: 8px; text-decoration: none; margin-bottom: 4px; }
.window-closed { background: var(--note); padding: 10px 16px; font-size: 14px; display: flex; gap: 12px; align-items: center; justify-content: space-between; border-top: 1px solid #F1E3A6; }
.composer { display: flex; gap: 6px; align-items: flex-end; padding: 10px 12px calc(10px + env(safe-area-inset-bottom)); background: var(--panel); border-top: 1px solid var(--line); }
.composer textarea { resize: none; max-height: 160px; min-height: 42px; }
.composer.note-mode textarea { background: var(--note); border-color: #F1E3A6; }
.composer .btn { height: 42px; }

/* ---------- drawer / modal ---------- */
.drawer { position: fixed; inset: 0; background: rgba(31,28,24,.35); display: flex; justify-content: flex-end; z-index: 30; }
.drawer.center { justify-content: center; align-items: center; padding: 16px; }
.drawer-panel { width: min(420px, 100%); height: 100%; background: var(--panel); padding: 20px; overflow-y: auto; }
.drawer.center .drawer-panel { height: auto; max-height: 90vh; border-radius: 16px; width: min(520px, 100%); }
.drawer-head { display: flex; justify-content: space-between; align-items: center; margin-bottom: 8px; }
.kv { margin: 12px 0; }
.kv .k { font-size: 12.5px; color: var(--muted); font-weight: 700; letter-spacing: .05em; text-transform: uppercase; }
.row { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }
.row > * { flex: 1; }
.row > .btn { flex: none; }

/* ---------- settings page ---------- */
.page { position: fixed; inset: 0; background: var(--bg); overflow-y: auto; z-index: 20; }
.page-inner { max-width: 880px; margin: 0 auto; padding: 20px 16px 80px; }
.page-head { display: flex; align-items: center; gap: 10px; margin-bottom: 16px; position: sticky; top: 0; background: var(--bg); padding: 10px 0; z-index: 2; }
.page-head h1 { flex: 1; }
.card { background: var(--panel); border: 1px solid var(--line); border-radius: 16px; padding: 20px; margin-bottom: 16px; }
.card h2 { margin-bottom: 6px; }
.card .hint { color: var(--muted); font-size: 14px; margin: 4px 0 0; }
.copy-field { display: flex; gap: 6px; margin-top: 6px; }
.copy-field input { font-family: ui-monospace, Menlo, monospace; font-size: 13.5px; }
.status-line { font-size: 14px; margin-top: 8px; }
.status-line.good { color: var(--human); } .status-line.bad { color: var(--alert); }
.table { width: 100%; border-collapse: collapse; font-size: 14px; }
.table td, .table th { padding: 10px 8px; border-bottom: 1px solid var(--line); text-align: left; vertical-align: top; }
.table th { font-size: 12px; text-transform: uppercase; letter-spacing: .06em; color: var(--muted); }
.knowledge { min-height: 280px; font-size: 14.5px; line-height: 1.5; }
.switch { display: flex; align-items: center; gap: 10px; font-weight: 600; color: var(--text); cursor: pointer; }
.switch input { width: 20px; height: 20px; accent-color: var(--accent); margin: 0; }
.logs { font-family: ui-monospace, Menlo, monospace; font-size: 12.5px; max-height: 360px; overflow: auto; background: var(--bg); border-radius: 10px; padding: 10px; white-space: pre-wrap; }

.toast { position: fixed; left: 50%; bottom: 24px; transform: translateX(-50%); background: var(--text); color: #fff; padding: 10px 16px; border-radius: 10px; z-index: 50; max-width: 90vw; font-size: 14px; cursor: pointer; }
.lightbox { position: fixed; inset: 0; background: rgba(0,0,0,.85); display: flex; align-items: center; justify-content: center; z-index: 60; cursor: zoom-out; }
.lightbox img { max-width: 94vw; max-height: 94vh; }

.mobile-only { display: none; }

/* ---------- tablet ---------- */
@media (max-width: 1100px) {
  #app { grid-template-columns: 300px 1fr; }
  .sidebar { position: fixed; inset: 0 auto 0 0; width: 270px; z-index: 25; transform: translateX(-100%); transition: transform .2s; }
  body.menu-open .sidebar { transform: none; box-shadow: 0 0 30px rgba(0,0,0,.12); }
  .mobile-only { display: inline-flex; }
  #btnBack { display: none; }
}
/* ---------- phone ---------- */
@media (max-width: 760px) {
  body { font-size: 16px; }
  #app { grid-template-columns: 1fr; }
  .chat-pane { display: none; }
  body.chat-open .list-pane { display: none; }
  body.chat-open .chat-pane { display: flex; }
  #btnBack { display: inline-flex; }
  .messages { padding: 12px 10px; }
  .msg { max-width: 88%; }
  .chat-head { padding: 8px 10px; }
  .chat-actions { width: 100%; }
  .chat-actions select { flex: 1; }
  .chat-actions [data-act=info] { display: none; }
}
