:root {
  --bg: #f4f1ea;
  --panel: #fffdf8;
  --ink: #1c1915;
  --muted: #6b6458;
  --line: #e6dfd2;
  --brand: #0f6b5c;
  --brand-2: #c45c26;
  --ok: #1f7a4d;
  --warn: #b45309;
  --bad: #b42318;
  --shadow: 0 10px 30px rgba(28, 25, 21, .06);
  --radius: 14px;
  font-family: "Segoe UI", system-ui, sans-serif;
}
* { box-sizing: border-box; }
html, body { margin: 0; background: var(--bg); color: var(--ink); }
a { color: var(--brand); text-decoration: none; }
.shell { display: grid; grid-template-columns: 240px 1fr; min-height: 100vh; }
.side { background: #17211f; color: #efe8dc; padding: 22px 16px; }
.side a { color: #efe8dc; display: block; padding: 9px 12px; border-radius: 10px; }
.side a:hover, .side a.active { background: rgba(255,255,255,.08); }
.side a.bridge-link { margin-top: 14px; border: 1px solid rgba(255,255,255,.16); color: #d8efe8; font-weight: 600; }
.side a.bridge-link:hover { background: rgba(15,107,92,.45); }
.brand { font-weight: 700; letter-spacing: .04em; margin: 0 0 22px; padding: 0 12px; }
.brand small { display: block; font-weight: 400; color: #b7c4bf; margin-top: 4px; }
.brand-city { display: block; margin-top: 8px; font-size: 12px; font-weight: 700; color: #f0d2b8; letter-spacing: .02em; }
.brand-city:empty { display: none; }
.city-switch { margin: 0 0 16px; padding: 0 12px; }
.city-switch label { margin: 0 0 6px; color: #b7c4bf; font-size: 11px; text-transform: uppercase; letter-spacing: .06em; }
.city-switch select {
  width: 100%;
  padding: 8px 10px;
  border-radius: 10px;
  border: 1px solid rgba(255,255,255,.16);
  background: #24312e;
  color: #efe8dc;
  font: inherit;
  font-weight: 600;
  cursor: pointer;
}
.city-switch select:focus { outline: 2px solid rgba(15,107,92,.55); outline-offset: 1px; }
.main { padding: 28px; }
.top { display: flex; justify-content: space-between; gap: 16px; align-items: center; margin-bottom: 22px; }
.card { background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); box-shadow: var(--shadow); padding: 18px; }
.grid { display: grid; gap: 14px; }
.grid.stats { grid-template-columns: repeat(auto-fit, minmax(160px, 1fr)); margin-bottom: 18px; }
.stat b { display: block; font-size: 28px; }
.stat span { color: var(--muted); font-size: 13px; }
table { width: 100%; border-collapse: collapse; table-layout: fixed; }
th, td { text-align: left; padding: 10px 8px; border-bottom: 1px solid var(--line); font-size: 14px; vertical-align: top; }
th.col-check, td.col-check { width: 36px; }
th.col-time, td.col-time { width: 10%; white-space: nowrap; font-variant-numeric: tabular-nums; }
th.col-site, td.col-site { width: 11%; }
th.col-locale, td.col-locale { width: 9%; }
th.col-contact, td.col-contact { width: 18%; overflow-wrap: anywhere; word-break: break-word; }
th.col-status, td.col-status { width: 10%; }
th.col-presence, td.col-presence { width: 10%; }
th.col-rating, td.col-rating { width: 8%; white-space: nowrap; }
th.col-mail, td.col-mail { width: 7%; }
th.col-notes, td.col-notes { width: 12%; overflow-wrap: anywhere; }
th.col-preview, td.col-preview { width: 18%; overflow-wrap: anywhere; word-break: break-word; }
th.col-visitor, td.col-visitor { width: 14%; overflow-wrap: anywhere; }
.table-scroll { overflow-x: auto; }
.inbox-table, .visitors-table { min-width: 1080px; }
.pager-wrap { margin-top: 12px; }
.pager { display: flex; flex-wrap: wrap; gap: 10px; align-items: center; justify-content: space-between; }
.pager-btn.is-disabled { opacity: .45; pointer-events: none; }
.pager-meta { font-size: 13px; }
.bubble-meta { display: flex; flex-wrap: wrap; gap: 6px; align-items: baseline; }
.bubble-meta time { font-weight: 600; color: var(--ink); }
.visitors-table .contact-cell, .visitors-table .notes-cell { overflow-wrap: anywhere; font-size: 13px; }
.btn { display: inline-flex; align-items: center; gap: 6px; border: 0; background: var(--brand); color: #fff; border-radius: 10px; padding: 8px 14px; cursor: pointer; font-weight: 600; }
.btn.secondary { background: #ece7dc; color: var(--ink); }
.btn.danger { background: var(--bad); }
.input, select, textarea { width: 100%; padding: 9px 11px; border: 1px solid var(--line); border-radius: 10px; background: #fff; font: inherit; }
label { display: block; font-size: 13px; color: var(--muted); margin: 12px 0 6px; }
.flash { background: #e8f6ee; color: var(--ok); padding: 10px 12px; border-radius: 10px; margin-bottom: 14px; }
.errors { color: var(--bad); font-size: 13px; }
.badge { display: inline-block; padding: 2px 8px; border-radius: 999px; background: #ece7dc; font-size: 12px; }
.badge.ok { background: #d9f0e3; color: var(--ok); }
.badge.warn { background: #fce9d0; color: var(--warn); }
.muted { color: var(--muted); }
.top-actions { display: flex; align-items: center; gap: 10px; }
.icon-btn { border: 1px solid var(--line); background: #fff; border-radius: 10px; width: 38px; height: 38px; cursor: pointer; font-size: 16px; line-height: 1; }
.icon-btn:hover { background: #f6f1e8; }
.nav-badge { display: inline-flex; max-width: 132px; min-width: 18px; height: 18px; padding: 0 7px; margin-left: 6px; border-radius: 999px; align-items: center; justify-content: center; background: #c45c26; color: #fff; font-size: 10px; font-weight: 700; vertical-align: middle; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.check-grid { display: grid; gap: 8px; margin: 8px 0 14px; }
.check-grid label { display: flex; gap: 8px; align-items: center; margin: 0; color: var(--ink); font-size: 14px; }
.side a.bridge-link { position: relative; }
.crm-toast-host { position: fixed; top: 16px; right: 16px; z-index: 100000; display: flex; flex-direction: column; gap: 8px; pointer-events: none; max-width: min(360px, calc(100vw - 24px)); }
.crm-toast { pointer-events: auto; background: #17211f; color: #fff; padding: 12px 14px; border-radius: 12px; box-shadow: 0 12px 28px rgba(0,0,0,.18); font-size: 13px; line-height: 1.4; opacity: 1; transform: translateY(0); transition: opacity .25s ease, transform .25s ease; }
.crm-toast.is-hide { opacity: 0; transform: translateY(-8px); }
@keyframes crm-shake {
  0%,100% { transform: translateX(0); }
  20% { transform: translateX(-3px) rotate(-1deg); }
  40% { transform: translateX(3px) rotate(1deg); }
  60% { transform: translateX(-2px); }
  80% { transform: translateX(2px); }
}
body.crm-shake-notify .side a[href*="conversations"],
body.crm-shake-once .side a[href*="conversations"],
body.crm-shake-notify .bridge-link.has-badge,
.bridge-link.is-shake { animation: crm-shake .55s ease-in-out infinite; }
.auth { min-height: 100vh; display: grid; place-items: center; padding: 24px; }
.auth .card { width: min(420px, 100%); }
.code { font-family: ui-monospace, Consolas, monospace; font-size: 12px; background: #f6f1e8; padding: 10px; border-radius: 8px; overflow: auto; }
.thread { display: grid; gap: 10px; }
.bubble { max-width: 80%; padding: 10px 12px; border-radius: 12px; }
.bubble.visitor { background: #ece7dc; }
.bubble.bot, .bubble.agent { background: #e4f3ef; margin-left: auto; }
.bubble.system { background: #fff4e5; font-size: 13px; }
.locale-switch { display: flex; gap: 8px; }
.locale-switch a { color: var(--muted); font-size: 13px; }
.inbox-row { cursor: pointer; }
.inbox-row:hover { background: #f6f1e8; }
.bubble.typing { background: #fff4e5; font-style: italic; max-width: 100%; }
.thread-workspace {
  display: grid;
  grid-template-columns: minmax(0, 1fr) 300px;
  gap: 16px;
  align-items: stretch;
}
.thread-stage { display: flex; flex-direction: column; gap: 12px; min-width: 0; min-height: calc(100vh - 140px); }
.thread-toolbar { padding: 12px 16px; }
.thread { flex: 1; min-height: 240px; max-height: none; overflow: auto; }
.thread-aside { display: grid; gap: 12px; align-content: start; }
.aside-title { margin: 0 0 8px; font-size: 14px; }
.aside-dl { margin: 0; display: grid; grid-template-columns: 88px 1fr; gap: 4px 8px; font-size: 13px; }
.aside-dl dt { color: var(--muted); }
.aside-dl dd { margin: 0; overflow-wrap: anywhere; }
.reply-composer { display: flex; flex-direction: column; gap: 8px; padding: 12px 16px; }
.reply-toolbar { display: flex; align-items: center; justify-content: space-between; gap: 8px; }
.reply-composer textarea { min-height: 120px; resize: vertical; font-size: 15px; line-height: 1.45; }
.reply-actions { display: flex; align-items: center; justify-content: space-between; gap: 8px; }
.actions { display: flex; gap: 8px; flex-wrap: wrap; align-items: center; }
.thread-workspace.is-expanded .reply-composer textarea { min-height: 22vh; }
.thread-workspace.is-expanded {
  position: fixed;
  inset: 0;
  z-index: 90000;
  background: var(--bg);
  padding: 16px;
  margin: 0;
  width: 100%;
  height: 100%;
  grid-template-columns: minmax(0, 1fr) 320px;
  grid-template-rows: minmax(0, 1fr);
  overflow: hidden;
}
.thread-workspace.is-expanded .thread-stage {
  min-height: 0;
  height: 100%;
  max-height: 100%;
  overflow: hidden;
}
.thread-workspace.is-expanded .thread { min-height: 0; }
.thread-workspace.is-expanded .thread-aside {
  min-height: 0;
  height: 100%;
  overflow: auto;
}
body.thread-expanded { overflow: hidden; }
@media (max-width: 1100px) {
  .thread-workspace { grid-template-columns: 1fr; }
  .thread-stage { min-height: 0; }
  .thread { max-height: 52vh; }
  .thread-workspace.is-expanded {
    grid-template-columns: 1fr;
    grid-template-rows: auto auto;
    overflow: auto;
  }
  .thread-workspace.is-expanded .thread-stage,
  .thread-workspace.is-expanded .thread-aside {
    height: auto;
    max-height: none;
    overflow: visible;
  }
  .thread-workspace.is-expanded .thread { max-height: 42vh; }
}
@media (max-width: 860px) {
  .shell { grid-template-columns: 1fr; }
  .side { display: flex; gap: 8px; overflow: auto; }
}
