:root {
  --bg: #f5f1e8;
  --panel: #ffffff;
  --ink: #1d2a27;
  --muted: #66736f;
  --line: #e4ddd0;
  --brand: #0f5c4d;
  --brand-ink: #ffffff;
  --brand-soft: #e3efe9;
  --accent: #c8643f;
  --danger: #b3261e;
  --warn-bg: #fff4dc;
  --guest: #ffffff;
  --bot: #e3efe9;
  --owner: #fde8dc;
  --radius: 12px;
  --shadow: 0 1px 2px rgba(0,0,0,.05), 0 4px 16px rgba(0,0,0,.05);
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #111816; --panel: #1a2320; --ink: #e8efec; --muted: #9aa8a3; --line: #2c3834;
    --brand: #3fa58c; --brand-ink: #0b1311; --brand-soft: #1f332d; --accent: #e38a66;
    --danger: #f2948d; --warn-bg: #3a3020; --guest: #212c29; --bot: #1f332d; --owner: #3b2a22;
    --shadow: none; color-scheme: dark;
  }
}
* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body {
  background: var(--bg); color: var(--ink);
  font: 15px/1.5 system-ui, -apple-system, "Segoe UI", Roboto, "Noto Sans", sans-serif;
}
button, input, textarea, select { font: inherit; color: inherit; }
a { color: var(--brand); }

/* ---------- auth screens ---------- */
.auth-wrap { min-height: 100%; display: grid; place-items: center; padding: 16px; }
.auth-card {
  width: 100%; max-width: 400px; background: var(--panel); border: 1px solid var(--line);
  border-radius: 16px; padding: 28px; box-shadow: var(--shadow);
}
.brand-mark { display: flex; align-items: center; gap: 10px; margin-bottom: 18px; }
.brand-mark .dot { width: 34px; height: 34px; border-radius: 10px; background: var(--brand); display: grid; place-items: center; color: var(--brand-ink); font-weight: 700; }
.brand-mark h1 { font-size: 18px; margin: 0; }
.brand-mark small { display: block; color: var(--muted); font-size: 12px; }
.auth-card h2 { font-size: 20px; margin: 0 0 6px; }
.auth-card p { color: var(--muted); margin: 0 0 16px; }
.qr { display: block; width: 200px; height: 200px; margin: 8px auto 12px; background: #fff; border-radius: 8px; padding: 8px; }
.secret { font-family: ui-monospace, Menlo, Consolas, monospace; font-size: 12px; word-break: break-all; background: var(--bg); padding: 8px; border-radius: 8px; }

/* ---------- form controls ---------- */
label { display: block; font-size: 13px; color: var(--muted); margin: 12px 0 4px; }
input[type=text], input[type=email], input[type=password], input[type=number], textarea, select {
  width: 100%; padding: 10px 12px; border: 1px solid var(--line); border-radius: 10px; background: var(--panel);
}
textarea { resize: vertical; min-height: 70px; }
input:focus, textarea:focus, select:focus, button:focus-visible { outline: 2px solid var(--brand); outline-offset: 1px; }
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  padding: 9px 16px; border-radius: 10px; border: 1px solid var(--line); background: var(--panel); cursor: pointer;
}
.btn:hover { border-color: var(--brand); }
.btn.primary { background: var(--brand); border-color: var(--brand); color: var(--brand-ink); font-weight: 600; }
.btn.danger { color: var(--danger); }
.btn.small { padding: 5px 10px; font-size: 13px; }
.btn[disabled] { opacity: .55; cursor: wait; }
.btn.block { width: 100%; margin-top: 16px; }
.row { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }
.spacer { flex: 1; }
.muted { color: var(--muted); }
.error { color: var(--danger); font-size: 14px; min-height: 20px; margin-top: 8px; }

/* ---------- layout ---------- */
.shell { display: grid; grid-template-columns: 220px 1fr; min-height: 100vh; }
.side { position: sticky; top: 0; height: 100vh; }
.side { background: var(--panel); border-right: 1px solid var(--line); padding: 18px 12px; display: flex; flex-direction: column; }
.side nav { display: flex; flex-direction: column; gap: 2px; margin-top: 8px; }
.side nav button {
  text-align: left; border: 0; background: none; padding: 10px 12px; border-radius: 10px; cursor: pointer; display: flex; justify-content: space-between;
}
.side nav button[aria-current=page] { background: var(--brand-soft); color: var(--brand); font-weight: 600; }
.side nav button:hover { background: var(--brand-soft); }
.side .who { margin-top: auto; font-size: 12px; color: var(--muted); padding: 8px 12px; word-break: break-all; }
.badge { background: var(--accent); color: #fff; border-radius: 999px; font-size: 11px; padding: 1px 7px; font-weight: 600; }
.main { padding: 22px; min-width: 0; }
.main h2 { margin: 0 0 4px; font-size: 22px; }
.main > header { margin-bottom: 16px; }
.card { background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); box-shadow: var(--shadow); }
.pad { padding: 16px; }

/* ---------- chats ---------- */
.chats { display: grid; grid-template-columns: 300px 1fr; height: calc(100vh - 110px); min-height: 420px; overflow: hidden; }
.convo-list { border-right: 1px solid var(--line); overflow-y: auto; }
.convo { width: 100%; text-align: left; border: 0; border-bottom: 1px solid var(--line); background: none; padding: 12px 14px; cursor: pointer; display: block; }
.convo:hover, .convo[aria-selected=true] { background: var(--brand-soft); }
.convo .top { display: flex; gap: 8px; align-items: center; }
.convo .name { font-weight: 600; flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.convo .time { font-size: 11px; color: var(--muted); }
.convo .preview { font-size: 13px; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.thread { display: flex; flex-direction: column; min-width: 0; }
.thread-head { padding: 12px 16px; border-bottom: 1px solid var(--line); display: flex; gap: 10px; align-items: center; flex-wrap: wrap; }
.thread-head .title { font-weight: 600; }
.msgs { flex: 1; overflow-y: auto; padding: 16px; display: flex; flex-direction: column; gap: 8px; background: var(--bg); }
.bubble { max-width: 75%; padding: 8px 12px; border-radius: 12px; white-space: pre-wrap; word-wrap: break-word; border: 1px solid var(--line); }
.bubble.guest { background: var(--guest); align-self: flex-start; }
.bubble.bot { background: var(--bot); align-self: flex-end; }
.bubble.owner { background: var(--owner); align-self: flex-end; }
.bubble.system { align-self: center; font-size: 12px; color: var(--muted); background: none; border: 0; }
.bubble .meta { font-size: 11px; color: var(--muted); margin-top: 3px; }
.composer { display: flex; gap: 8px; padding: 12px; border-top: 1px solid var(--line); }
.composer textarea { min-height: 44px; height: 44px; }
.pill { font-size: 12px; padding: 3px 10px; border-radius: 999px; background: var(--brand-soft); color: var(--brand); }
.pill.off { background: var(--warn-bg); color: var(--accent); }
.empty { padding: 40px; text-align: center; color: var(--muted); }

/* ---------- tables ---------- */
.table { width: 100%; border-collapse: collapse; }
.table th, .table td { text-align: left; padding: 10px 12px; border-bottom: 1px solid var(--line); vertical-align: top; }
.table th { font-size: 12px; color: var(--muted); font-weight: 600; text-transform: uppercase; letter-spacing: .03em; }
.table-wrap { overflow-x: auto; }
.kb-row { display: grid; grid-template-columns: 140px 220px 1fr auto; gap: 8px; padding: 10px 12px; border-bottom: 1px solid var(--line); align-items: start; }
.kb-row textarea { min-height: 42px; }
.kb-head { font-size: 12px; color: var(--muted); font-weight: 600; text-transform: uppercase; }

/* ---------- photos ---------- */
.folder { margin-bottom: 18px; }
.folder-head { display: flex; align-items: center; gap: 10px; padding: 12px 16px; border-bottom: 1px solid var(--line); }
.folder-head h3 { margin: 0; font-size: 16px; }
.grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(140px, 1fr)); gap: 10px; padding: 14px 16px; }
.thumb { position: relative; border-radius: 10px; overflow: hidden; border: 1px solid var(--line); aspect-ratio: 4 / 3; background: var(--bg); }
.thumb img { width: 100%; height: 100%; object-fit: cover; display: block; }
.thumb button { position: absolute; top: 6px; right: 6px; }
.order-tag { position: absolute; left: 6px; top: 6px; background: var(--brand); color: var(--brand-ink); font-size: 11px; border-radius: 6px; padding: 1px 6px; }
input[type=file] { display: none; }

/* ---------- toast ---------- */
#toast {
  position: fixed; left: 50%; bottom: 20px; transform: translateX(-50%) translateY(20px); opacity: 0;
  background: var(--ink); color: var(--bg); padding: 10px 16px; border-radius: 10px; transition: .2s; pointer-events: none; max-width: calc(100% - 32px);
}
#toast.show { opacity: 1; transform: translateX(-50%) translateY(0); }
#toast.bad { background: var(--danger); color: #fff; }

/* ---------- mobile ---------- */
@media (max-width: 820px) {
  .shell { grid-template-columns: 1fr; }
  .side { flex-direction: row; align-items: center; padding: 8px; border-right: 0; border-bottom: 1px solid var(--line); overflow-x: auto; height: auto; position: static; }
  .side .brand-mark, .side .who { display: none; }
  .side nav { flex-direction: row; margin: 0; }
  .side nav button { white-space: nowrap; }
  .main { padding: 16px; }
  .chats { grid-template-columns: 1fr; height: auto; }
  .chats.has-thread .convo-list { display: none; }
  .chats:not(.has-thread) .thread { display: none; }
  .msgs { min-height: 50vh; }
  .kb-row { grid-template-columns: 1fr; }
  .bubble { max-width: 90%; }
}
@media (min-width: 821px) { .back-btn { display: none; } }
