:root {
  --bg: #0d1117;
  --surface: #151b23;
  --surface-2: #1c232d;
  --border: #262f3b;
  --text: #e6edf3;
  --muted: #8b98a8;
  --accent: #3ddc97;
  --accent-ink: #06231a;
  --accent-soft: rgba(61, 220, 151, .14);
  --bubble-me: #1d3b31;
  --danger: #ff6b6b;
  --warn: #ffb454;
  --radius: 14px;
  --shadow: 0 8px 30px rgba(0, 0, 0, .35);
  color-scheme: dark;
}
:root[data-theme="light"] {
  --bg: #f4f6f9;
  --surface: #ffffff;
  --surface-2: #eef1f5;
  --border: #dde3ea;
  --text: #17202a;
  --muted: #667385;
  --accent: #14a874;
  --accent-ink: #ffffff;
  --accent-soft: rgba(20, 168, 116, .12);
  --bubble-me: #dcf5ea;
  --shadow: 0 8px 30px rgba(20, 30, 50, .10);
  color-scheme: light;
}

* { box-sizing: border-box; }
html, body { height: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 15px/1.5 system-ui, -apple-system, "Segoe UI", Roboto, "Noto Sans", sans-serif;
  -webkit-font-smoothing: antialiased;
}
button, input, textarea, select { font: inherit; color: inherit; }
button { cursor: pointer; }
[hidden] { display: none !important; }

/* ---- shell ---- */
.shell { display: flex; flex-direction: column; height: 100dvh; }
.topbar {
  display: flex; align-items: center; gap: 12px;
  padding: 10px 16px; border-bottom: 1px solid var(--border); background: var(--surface);
}
.brand { display: flex; align-items: center; gap: 10px; font-weight: 700; letter-spacing: .2px; }
.brand img { width: 28px; height: 28px; border-radius: 8px; }
.spacer { flex: 1; }
.me { display: flex; align-items: center; gap: 8px; }
.me .name { font-weight: 600; }
.avatar { width: 32px; height: 32px; border-radius: 50%; background: var(--surface-2); object-fit: cover; flex: none; }
.avatar.sm { width: 28px; height: 28px; }
.avatar.lg { width: 40px; height: 40px; }

.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  padding: 8px 14px; border-radius: 10px; border: 1px solid var(--border);
  background: var(--surface-2); text-decoration: none; transition: filter .15s, transform .05s;
}
.btn:hover { filter: brightness(1.12); }
.btn:active { transform: translateY(1px); }
.btn:disabled { opacity: .5; cursor: not-allowed; }
.btn.primary { background: var(--accent); border-color: transparent; color: var(--accent-ink); font-weight: 650; }
.btn.ghost { background: transparent; }
.btn.danger { color: var(--danger); }
.btn.sm { padding: 5px 10px; font-size: 13px; border-radius: 8px; }
.icon-btn { width: 36px; height: 36px; padding: 0; border-radius: 10px; }

/* ---- landing ---- */
.landing { flex: 1; display: grid; place-items: center; padding: 24px 16px; overflow: auto; }
.hero { max-width: 520px; text-align: center; }
.hero .logo { width: 72px; height: 72px; border-radius: 20px; box-shadow: var(--shadow); }
.hero h1 { font-size: 30px; margin: 20px 0 8px; }
.hero p { color: var(--muted); margin: 0 0 24px; }
.hero .btn { padding: 12px 22px; font-size: 16px; border-radius: 12px; }
.notice { margin-top: 16px; color: var(--danger); }

/* ---- two-pane layout ---- */
.main { flex: 1; min-height: 0; display: grid; grid-template-columns: 360px 1fr; }
.sidebar { display: flex; flex-direction: column; min-height: 0; border-right: 1px solid var(--border); background: var(--surface); }
.side-head { padding: 12px; display: grid; gap: 10px; border-bottom: 1px solid var(--border); }
.search {
  width: 100%; padding: 9px 12px; border-radius: 10px; border: 1px solid var(--border);
  background: var(--bg); outline: none;
}
.search:focus, .field:focus { border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-soft); }
.tabs { display: flex; gap: 4px; background: var(--bg); border-radius: 10px; padding: 3px; }
.tab { flex: 1; border: 0; background: transparent; padding: 6px 8px; border-radius: 8px; color: var(--muted); }
.tab.active { background: var(--surface-2); color: var(--text); font-weight: 600; }
.list { flex: 1; overflow-y: auto; }
.item {
  display: flex; gap: 10px; padding: 12px; border-bottom: 1px solid var(--border);
  cursor: pointer; text-decoration: none; color: inherit;
}
.item:hover { background: var(--surface-2); }
.item.active { background: var(--accent-soft); box-shadow: inset 3px 0 0 var(--accent); }
.item .body { min-width: 0; flex: 1; }
.item .row { display: flex; align-items: baseline; gap: 8px; }
.item .subject { font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; flex: 1; }
.item .time { color: var(--muted); font-size: 12px; flex: none; }
.item .preview { color: var(--muted); font-size: 13px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.item .meta { display: flex; gap: 6px; margin-top: 6px; flex-wrap: wrap; }
.empty { padding: 32px 16px; text-align: center; color: var(--muted); }

.chip {
  display: inline-flex; align-items: center; gap: 4px; padding: 1px 8px; border-radius: 999px;
  font-size: 12px; background: var(--surface-2); color: var(--muted); border: 1px solid var(--border);
}
.chip.open { color: var(--accent); background: var(--accent-soft); border-color: transparent; }
.chip.closed { color: var(--muted); }
.chip.wait { color: var(--warn); border-color: transparent; background: rgba(255, 180, 84, .13); }

/* ---- conversation ---- */
.pane { display: flex; flex-direction: column; min-width: 0; min-height: 0; }
.pane-empty { flex: 1; display: grid; place-items: center; color: var(--muted); text-align: center; padding: 24px; }
.conv-head { display: flex; align-items: center; gap: 12px; padding: 12px 16px; border-bottom: 1px solid var(--border); background: var(--surface); }
.conv-head .titles { min-width: 0; flex: 1; }
.conv-head h2 { margin: 0; font-size: 17px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.conv-head .sub { color: var(--muted); font-size: 13px; display: flex; gap: 8px; flex-wrap: wrap; align-items: center; }
.actions { display: flex; gap: 8px; flex-wrap: wrap; justify-content: flex-end; }
.back { display: none; }

.messages { flex: 1; overflow-y: auto; padding: 16px; display: flex; flex-direction: column; gap: 4px; }
.msg { display: flex; gap: 10px; max-width: min(720px, 92%); }
.msg.mine { align-self: flex-end; flex-direction: row-reverse; }
.msg.gap { margin-top: 12px; }
.msg .avatar { visibility: hidden; }
.msg.first .avatar { visibility: visible; }
.bubble { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 8px 12px; min-width: 0; }
.msg.mine .bubble { background: var(--bubble-me); border-color: transparent; }
.msg.staff .bubble { border-color: var(--accent); }
.msg.ai .bubble { border-style: dashed; }
.who { font-size: 12px; color: var(--muted); margin-bottom: 2px; display: flex; gap: 6px; align-items: center; }
.who b { color: var(--text); font-weight: 600; }
.text { white-space: pre-wrap; overflow-wrap: anywhere; }
.stamp { font-size: 11px; color: var(--muted); text-align: right; margin-top: 2px; }
.system { align-self: center; color: var(--muted); font-size: 12px; padding: 6px 0; }

.composer { border-top: 1px solid var(--border); background: var(--surface); padding: 12px 16px; }
.composer .row { display: flex; gap: 10px; align-items: flex-end; }
.field {
  width: 100%; padding: 10px 12px; border-radius: 12px; border: 1px solid var(--border);
  background: var(--bg); outline: none; resize: none;
}
textarea.field { max-height: 200px; min-height: 42px; }
.closed-bar { display: flex; align-items: center; justify-content: space-between; gap: 12px; color: var(--muted); }
.draft {
  display: flex; gap: 10px; align-items: flex-start; margin-bottom: 10px; padding: 10px 12px;
  border-radius: 12px; background: var(--accent-soft); border: 1px dashed var(--accent);
}
.draft .text { flex: 1; font-size: 14px; }
.draft b { display: block; font-size: 12px; color: var(--accent); margin-bottom: 2px; }

/* ---- new ticket form ---- */
.form { padding: 20px 16px; overflow-y: auto; }
.form-card { max-width: 640px; margin: 0 auto; background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 20px; display: grid; gap: 14px; }
.form-card h2 { margin: 0; }
.form-card label { display: grid; gap: 6px; font-weight: 600; font-size: 14px; }
.form-card .hint { font-weight: 400; color: var(--muted); font-size: 12px; text-align: right; }

/* ---- toast ---- */
.toasts { position: fixed; bottom: 16px; left: 50%; transform: translateX(-50%); display: grid; gap: 8px; z-index: 10; width: min(92vw, 420px); }
.toast { background: var(--surface-2); border: 1px solid var(--border); border-left: 3px solid var(--danger); padding: 10px 14px; border-radius: 10px; box-shadow: var(--shadow); }
.toast.ok { border-left-color: var(--accent); }
.dot { width: 8px; height: 8px; border-radius: 50%; background: var(--accent); display: inline-block; }
.dot.off { background: var(--danger); }

/* ---- mobile: one pane at a time ---- */
@media (max-width: 800px) {
  .main { grid-template-columns: 1fr; }
  .main.has-active .sidebar, .main.has-form .sidebar { display: none; }
  .main:not(.has-active):not(.has-form) .pane { display: none; }
  .back { display: inline-flex; }
  .me .name { display: none; }
  .msg { max-width: 100%; }
}

/* ---- attachments ---- */
.chips { display: flex; flex-wrap: wrap; gap: 8px; }
.chips:not(:empty) { margin-bottom: 10px; }
.att-chip {
  display: flex; align-items: center; gap: 8px; max-width: 260px; padding: 4px 6px 4px 8px;
  border-radius: 10px; background: var(--surface-2); border: 1px solid var(--border); font-size: 13px;
}
.att-chip.loading { opacity: .6; }
.att-chip img { width: 28px; height: 28px; border-radius: 6px; object-fit: cover; }
.att-chip .n, .att-file .n { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; min-width: 0; }
.att-chip .sz, .att-file .sz { color: var(--muted); font-size: 12px; flex: none; }
.att-chip .x { border: 0; background: transparent; color: var(--muted); padding: 2px 6px; border-radius: 6px; }
.att-chip .x:hover { color: var(--danger); }
.atts { display: flex; flex-direction: column; align-items: flex-start; gap: 6px; margin-top: 6px; }
.att-img { display: block; max-width: 100%; max-height: 280px; border-radius: 10px; border: 1px solid var(--border); background: var(--surface-2); }
.att-file {
  display: inline-flex; align-items: center; gap: 8px; max-width: 100%; padding: 6px 10px; border-radius: 10px;
  background: var(--surface-2); border: 1px solid var(--border); color: inherit; text-decoration: none;
}
.att-file:hover { border-color: var(--accent); }
.attach-row { display: flex; align-items: center; gap: 10px; flex-wrap: wrap; }
.attach-row .hint { flex: 1; text-align: left; min-width: 180px; }
.attach-row .chips { flex-basis: 100%; }
.pane { position: relative; }
.pane.dragging::after {
  content: "Отпустите, чтобы прикрепить файлы"; position: absolute; inset: 8px; display: grid; place-items: center;
  border: 2px dashed var(--accent); border-radius: var(--radius); background: var(--accent-soft);
  color: var(--accent); font-weight: 650; z-index: 5; pointer-events: none;
}

.hint-box { padding: 10px 12px; border-radius: 10px; background: var(--accent-soft); color: var(--text); font-size: 13px; }
