:root {
  --bg: #f6f7f9;
  --surface: #fff;
  --surface-2: #f0f2f5;
  --line: #e3e7ed;
  --line-2: #d4dae3;
  --text: #121820;
  --muted: #5d6877;
  --blue: #1f6feb;
  --blue-2: #1a5fd0;
  --blue-soft: #ebf2fe;
  --warn: #b45309;
  --warn-soft: #fff4e5;
  --ok: #15803d;
  --ok-soft: #e9f7ee;
  --side: #0b0e13;
  --side-text: #c9d1dc;
  --r: 10px;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #0b0e13; --surface: #11161e; --surface-2: #171d27; --line: #222a36; --line-2: #2c3644;
    --text: #e8ecf2; --muted: #8e99aa; --blue: #3b82f6; --blue-2: #2f6fe0; --blue-soft: #15233b;
    --warn: #f59e0b; --warn-soft: #2a2010; --ok: #22c55e; --ok-soft: #10261a; --side: #07090d;
  }
}
:root[data-theme="dark"] {
  --bg: #0b0e13; --surface: #11161e; --surface-2: #171d27; --line: #222a36; --line-2: #2c3644;
  --text: #e8ecf2; --muted: #8e99aa; --blue: #3b82f6; --blue-2: #2f6fe0; --blue-soft: #15233b;
  --warn: #f59e0b; --warn-soft: #2a2010; --ok: #22c55e; --ok-soft: #10261a; --side: #07090d;
}

*, *::before, *::after { box-sizing: border-box; }
html, body { height: 100%; }
body { margin: 0; background: var(--bg); color: var(--text); font: 400 15px/1.5 "Inter", system-ui, -apple-system, "Segoe UI", sans-serif; -webkit-font-smoothing: antialiased; }
h1, h2, h3, p { margin: 0; }
h1 { font-size: 22px; letter-spacing: -.02em; }
h2 { font-size: 17px; }
a { color: var(--blue); text-decoration: none; }
button, input, select, textarea { font: inherit; color: inherit; }
.muted { color: var(--muted); }
.small { font-size: 13px; }

/* Buttons and fields */
.btn { display: inline-flex; align-items: center; justify-content: center; gap: 6px; height: 38px; padding: 0 14px; border-radius: 8px; border: 1px solid var(--line-2); background: var(--surface); cursor: pointer; font-weight: 600; font-size: 14px; white-space: nowrap; }
.btn:hover { border-color: var(--muted); }
.btn--primary { background: var(--blue); border-color: var(--blue); color: #fff; }
.btn--primary:hover { background: var(--blue-2); border-color: var(--blue-2); }
.btn--sm { height: 32px; padding: 0 10px; font-size: 13px; }
.btn--danger { color: #dc2626; }
.btn:disabled { opacity: .55; cursor: default; }
.field { display: grid; gap: 6px; margin-bottom: 14px; }
.field > span { font-size: 13px; font-weight: 600; color: var(--muted); }
.input { width: 100%; padding: 9px 12px; border: 1px solid var(--line-2); border-radius: 8px; background: var(--surface); outline: none; }
.input:focus { border-color: var(--blue); box-shadow: 0 0 0 3px var(--blue-soft); }
textarea.input { resize: vertical; min-height: 90px; }
.card { background: var(--surface); border: 1px solid var(--line); border-radius: var(--r); padding: 18px; }
.chip { display: inline-flex; align-items: center; gap: 4px; padding: 1px 8px; border-radius: 99px; font-size: 12px; font-weight: 600; background: var(--surface-2); color: var(--muted); }
.chip--ai { background: var(--blue-soft); color: var(--blue); }
.chip--warn { background: var(--warn-soft); color: var(--warn); }
.chip--ok { background: var(--ok-soft); color: var(--ok); }

/* Auth */
.auth { min-height: 100%; display: grid; place-items: center; padding: 24px 16px; }
.auth__box { width: 100%; max-width: 400px; }
.auth__logo { display: flex; align-items: center; gap: 10px; margin-bottom: 22px; font-weight: 700; font-size: 22px; }
.auth__logo img { width: 34px; }
.auth .card { padding: 24px; }
.auth__switch { margin-top: 14px; text-align: center; font-size: 14px; }
.error { color: #dc2626; font-size: 14px; min-height: 20px; margin-bottom: 8px; }

/* Shell */
.shell { display: grid; grid-template-columns: 220px 1fr; height: 100%; }
.side { background: var(--side); color: var(--side-text); display: flex; flex-direction: column; padding: 16px 12px; gap: 4px; }
.side__brand { display: flex; align-items: center; gap: 10px; color: #fff; font-weight: 700; font-size: 18px; padding: 4px 8px 16px; }
.side__brand img { width: 28px; }
.side__company { font-size: 12px; color: #8e99aa; padding: 0 8px 12px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.nav { display: flex; align-items: center; gap: 10px; padding: 9px 10px; border-radius: 8px; color: var(--side-text); font-weight: 500; }
.nav:hover { background: rgba(255,255,255,.06); }
.nav.is-active { background: rgba(59,130,246,.18); color: #fff; }
.nav svg { width: 18px; height: 18px; fill: none; stroke: currentColor; stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round; flex: none; }
.nav__badge { margin-left: auto; background: #f59e0b; color: #111; border-radius: 99px; font-size: 11px; font-weight: 700; padding: 0 7px; }
.side__foot { margin-top: auto; font-size: 13px; padding: 8px; color: #8e99aa; }
.side__foot button { background: none; border: 0; color: #8e99aa; cursor: pointer; padding: 0; text-decoration: underline; }
.main { overflow: auto; min-width: 0; }
.page { padding: 24px; max-width: 1200px; }
.page__head { display: flex; align-items: center; justify-content: space-between; gap: 12px; margin-bottom: 18px; flex-wrap: wrap; }
.banner { background: var(--warn-soft); color: var(--warn); border-radius: 8px; padding: 10px 14px; margin-bottom: 16px; font-size: 14px; }

/* Dashboard */
.stats { display: grid; grid-template-columns: repeat(4, minmax(0, 1fr)); gap: 12px; margin-bottom: 18px; }
.stat__n { font-size: 28px; font-weight: 700; letter-spacing: -.02em; }
.stat__l { color: var(--muted); font-size: 13px; }

/* Inbox */
.inbox { display: grid; grid-template-columns: 330px 1fr; height: 100%; }
.inbox__list { border-right: 1px solid var(--line); background: var(--surface); overflow: auto; }
.inbox__filters { display: flex; gap: 6px; padding: 12px; border-bottom: 1px solid var(--line); position: sticky; top: 0; background: var(--surface); flex-wrap: wrap; }
.inbox__filters button { border: 1px solid var(--line-2); background: var(--surface); border-radius: 99px; padding: 3px 10px; font-size: 13px; cursor: pointer; }
.inbox__filters button.is-on { background: var(--text); color: var(--surface); border-color: var(--text); }
.conv { display: block; padding: 12px 14px; border-bottom: 1px solid var(--line); color: var(--text); }
.conv:hover { background: var(--surface-2); }
.conv.is-active { background: var(--blue-soft); }
.conv__top { display: flex; align-items: center; gap: 8px; }
.conv__name { font-weight: 600; flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.conv__time { font-size: 12px; color: var(--muted); }
.conv__prev { font-size: 13px; color: var(--muted); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; margin-top: 2px; }
.conv__unread { background: var(--blue); color: #fff; border-radius: 99px; font-size: 11px; font-weight: 700; padding: 0 6px; }
.thread { display: grid; grid-template-rows: auto 1fr auto; height: 100%; min-width: 0; }
.thread__head { display: flex; align-items: center; gap: 10px; padding: 12px 16px; border-bottom: 1px solid var(--line); background: var(--surface); flex-wrap: wrap; }
.thread__who { flex: 1; min-width: 0; }
.thread__msgs { overflow: auto; padding: 16px; display: flex; flex-direction: column; gap: 8px; }
.msg { max-width: min(560px, 85%); padding: 8px 12px; border-radius: 12px; white-space: pre-wrap; word-wrap: break-word; }
.msg--client { background: var(--surface); border: 1px solid var(--line); align-self: flex-start; border-bottom-left-radius: 4px; }
.msg--ai, .msg--agent { background: var(--blue); color: #fff; align-self: flex-end; border-bottom-right-radius: 4px; }
.msg--agent { background: #0f766e; }
.msg--system { align-self: center; background: var(--surface-2); color: var(--muted); font-size: 13px; border-radius: 8px; max-width: 90%; text-align: center; }
.msg__meta { font-size: 11px; opacity: .75; margin-top: 2px; }
.thread__form { display: flex; gap: 8px; padding: 12px 16px; border-top: 1px solid var(--line); background: var(--surface); }
.thread__form textarea { min-height: 42px; height: 42px; max-height: 160px; }
.empty { display: grid; place-items: center; height: 100%; color: var(--muted); padding: 24px; text-align: center; }
.handoff { background: var(--warn-soft); color: var(--warn); padding: 8px 16px; font-size: 14px; }

/* CRM board */
.board { display: grid; grid-template-columns: repeat(4, minmax(220px, 1fr)); gap: 12px; overflow-x: auto; }
.col { background: var(--surface-2); border-radius: var(--r); padding: 10px; min-height: 200px; }
.col__head { display: flex; justify-content: space-between; font-weight: 600; font-size: 14px; padding: 4px 4px 10px; }
.deal { background: var(--surface); border: 1px solid var(--line); border-radius: 8px; padding: 10px; margin-bottom: 8px; }
.deal__name { font-weight: 600; }
.deal__row { display: flex; gap: 6px; align-items: center; margin-top: 8px; }
.deal__row select { padding: 3px 6px; border-radius: 6px; border: 1px solid var(--line-2); background: var(--surface); font-size: 13px; }

/* Tables */
.table { width: 100%; border-collapse: collapse; background: var(--surface); border: 1px solid var(--line); border-radius: var(--r); overflow: hidden; }
.table th, .table td { text-align: left; padding: 10px 12px; border-bottom: 1px solid var(--line); font-size: 14px; vertical-align: middle; }
.table th { font-size: 12px; color: var(--muted); font-weight: 600; text-transform: uppercase; letter-spacing: .04em; background: var(--surface-2); }
.table-wrap { overflow-x: auto; }

/* Settings */
.grid2 { display: grid; grid-template-columns: 1fr 1fr; gap: 16px; align-items: start; }
.stack { display: grid; gap: 16px; }
.row { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }
.code { font-family: ui-monospace, SFMono-Regular, Menlo, monospace; background: var(--surface-2); padding: 1px 6px; border-radius: 5px; font-size: 13px; }

.toast { position: fixed; left: 50%; bottom: 20px; transform: translateX(-50%) translateY(20px); background: #121820; color: #fff; padding: 10px 16px; border-radius: 8px; opacity: 0; pointer-events: none; transition: .2s; font-size: 14px; z-index: 50; max-width: calc(100% - 32px); }
.toast.is-on { opacity: 1; transform: translateX(-50%); }

/* Mobile */
.topbar { display: none; }
@media (max-width: 860px) {
  .shell { grid-template-columns: 1fr; grid-template-rows: auto 1fr; }
  .side { display: none; position: fixed; inset: 0 30% 0 0; z-index: 40; }
  .shell.menu-open .side { display: flex; }
  .topbar { display: flex; align-items: center; gap: 10px; padding: 10px 16px; background: var(--side); color: #fff; font-weight: 700; }
  .topbar button { background: none; border: 0; color: #fff; font-size: 22px; cursor: pointer; padding: 0 4px; }
  .page { padding: 16px; }
  .stats { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .inbox { grid-template-columns: 1fr; }
  .inbox.has-thread .inbox__list { display: none; }
  .inbox:not(.has-thread) .thread { display: none; }
  .grid2 { grid-template-columns: 1fr; }
  .board { grid-template-columns: repeat(4, 80%); }
}
