:root {
  --brand: #0f5c52;
  --brand-hover: #0c4a42;
  --brand-deep: #08332e;
  --on-brand: #ffffff;
  --bg: #f4f6f8;
  --surface: #ffffff;
  --surface-2: #eef1f4;
  --ink: #12202b;
  --muted: #5d6b76;
  --line: #dfe5ea;
  --shadow: 0 10px 30px rgba(16, 24, 40, 0.06);
  --radius: 14px;
  --radius-sm: 10px;
  --danger: #b42318;
  --human: #b54708;
  --ok-bg: #e8f7ef;
  --ok-ink: #087443;
  --warn-bg: #fff4e5;
  --warn-ink: #b54708;
  --sidebar: #ffffff;
  --sidebar-ink: #1b2a34;
  --sidebar-muted: #6b7a86;
  --sidebar-hover: #f3f6f8;
  --login-bg: #0f1720;
}

html[data-theme="dark"] {
  --bg: #0e141b;
  --surface: #161d26;
  --surface-2: #1d2631;
  --ink: #e8eef3;
  --muted: #93a1ae;
  --line: #2a3542;
  --shadow: 0 12px 32px rgba(0, 0, 0, 0.28);
  --sidebar: #121922;
  --sidebar-ink: #e8eef3;
  --sidebar-muted: #8b99a6;
  --sidebar-hover: #1c2530;
  --login-bg: #0b1016;
  --ok-bg: #123526;
  --ok-ink: #6ee7b7;
  --warn-bg: #3a2712;
  --warn-ink: #fbbf24;
}

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body {
  font-family: "Segoe UI", ui-sans-serif, system-ui, sans-serif;
  background: var(--bg);
  color: var(--ink);
  letter-spacing: -0.011em;
}

.hidden { display: none !important; }

.kicker {
  margin: 0;
  font-size: 11px;
  letter-spacing: 0.14em;
  text-transform: uppercase;
  color: var(--brand);
  font-weight: 700;
}

h1, h2 { margin: 0; font-weight: 650; letter-spacing: -0.03em; }

.login {
  min-height: 100%;
  display: grid;
  place-items: center;
  background:
    radial-gradient(900px 420px at 15% -10%, color-mix(in srgb, var(--brand) 45%, transparent), transparent 60%),
    var(--login-bg);
}
.login-card {
  width: min(420px, 92vw);
  background: var(--surface);
  padding: 36px 32px;
  display: grid;
  gap: 12px;
  border-radius: var(--radius);
  border: 1px solid var(--line);
  box-shadow: var(--shadow);
}
.login-card h1 { font-size: 28px; }
.login-card .sub { margin: 0 0 8px; color: var(--muted); }
.login-card label { display: grid; gap: 6px; font-size: 13px; color: var(--muted); }
.login-card input, .search input, .composer textarea, .sim input {
  width: 100%;
  border: 1px solid var(--line);
  background: var(--surface);
  padding: 11px 12px;
  font: inherit;
  color: var(--ink);
  border-radius: var(--radius-sm);
}
.login-card button, .composer > button[type="submit"], .sim button, .actions button, .rail-foot button, .wa-actions button {
  border: 0;
  background: var(--brand);
  color: var(--on-brand);
  font: inherit;
  font-weight: 650;
  padding: 12px 16px;
  cursor: pointer;
  border-radius: var(--radius-sm);
}
.login-card button:hover, .composer > button[type="submit"]:hover, .sim button:hover, .actions button:hover, .wa-actions button:hover {
  background: var(--brand-hover);
}
.actions button.ghost, .rail-foot button, .wa-actions button.ghost {
  background: transparent;
  color: inherit;
  border: 1px solid var(--line);
}
.actions button.ghost:hover, .rail-foot button:hover, .wa-actions button.ghost:hover { background: var(--surface-2); }
.error { color: var(--danger); min-height: 1.2em; margin: 0; font-size: 13px; }

.app {
  height: 100%;
  display: grid;
  grid-template-columns: 340px 1fr;
}

.rail {
  background: var(--sidebar);
  color: var(--sidebar-ink);
  display: flex;
  flex-direction: column;
  min-height: 0;
  border-right: 1px solid var(--line);
}
.rail-head {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  padding: 22px 18px 12px;
  gap: 12px;
}
.rail-head h2 { font-size: 18px; color: var(--sidebar-ink); }
.biz-logo { height: 32px; max-width: 140px; object-fit: contain; display: block; margin-bottom: 8px; }
.login-logo {
  height: 44px;
  max-width: 260px;
  object-fit: contain;
  display: block;
  margin-bottom: 8px;
  background: #07090d;
  border-radius: 10px;
  padding: 8px 12px;
}
.user-chip { display: flex; align-items: center; gap: 8px; min-width: 0; cursor: pointer; }
.avatar {
  width: 28px;
  height: 28px;
  border-radius: 50%;
  object-fit: cover;
  background: var(--brand);
  flex-shrink: 0;
  border: 1px solid var(--line);
}
.plugin-nav { display: flex; gap: 6px; padding: 0 14px 10px; flex-wrap: wrap; }
.plugin-nav button {
  background: var(--surface-2);
  color: var(--brand);
  border: 0;
  padding: 6px 10px;
  font: inherit;
  font-size: 12px;
  font-weight: 650;
  cursor: pointer;
  border-radius: 999px;
}
.search { padding: 0 14px 10px; }
.search input { background: var(--surface-2); }
.filters {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 4px;
  padding: 0 14px 10px;
}
.filters button {
  background: transparent;
  color: var(--sidebar-muted);
  border: 0;
  padding: 8px 0;
  font: inherit;
  font-size: 12px;
  cursor: pointer;
  border-radius: 8px;
}
.filters button.on {
  color: var(--brand);
  background: color-mix(in srgb, var(--brand) 14%, transparent);
  font-weight: 650;
}

.list {
  list-style: none;
  margin: 0;
  padding: 0;
  overflow: auto;
  flex: 1;
}
.list li {
  padding: 12px 16px;
  border-top: 1px solid var(--line);
  cursor: pointer;
  display: grid;
  gap: 4px;
}
.list li:hover, .list li.active { background: var(--sidebar-hover); }
.list li.active { box-shadow: inset 3px 0 0 var(--brand); }
.list .row { display: flex; justify-content: space-between; gap: 8px; align-items: center; }
.list .name { font-weight: 650; }
.list .time { font-size: 11px; color: var(--sidebar-muted); }
.list .preview { font-size: 13px; color: var(--sidebar-muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; flex: 1; min-width: 0; }
.list .meta { display: flex; gap: 6px; align-items: center; flex-shrink: 0; }
.list .chip {
  font-size: 10px;
  font-weight: 700;
  letter-spacing: 0.04em;
  text-transform: uppercase;
  padding: 2px 6px;
  border-radius: 999px;
}
.list .chip.bot { background: var(--ok-bg); color: var(--ok-ink); }
.list .chip.human { background: var(--warn-bg); color: var(--human); }
.list .assignee { font-size: 11px; color: var(--sidebar-muted); max-width: 90px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.badge {
  min-width: 18px;
  height: 18px;
  border-radius: 9px;
  background: var(--brand);
  color: var(--on-brand);
  font-size: 11px;
  font-weight: 700;
  display: inline-grid;
  place-items: center;
  padding: 0 5px;
}

.rail-foot {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 12px 16px;
  border-top: 1px solid var(--line);
  font-size: 13px;
  color: var(--sidebar-muted);
}
.rail-foot a { color: var(--brand); text-decoration: none; font-weight: 650; }

.head-pills {
  display: flex;
  flex-direction: column;
  gap: 4px;
  align-items: stretch;
}
.head-pills .pill { white-space: nowrap; }
.pill {
  font-size: 11px;
  font-weight: 700;
  letter-spacing: 0.06em;
  text-transform: uppercase;
  padding: 5px 8px;
  border-radius: 999px;
  background: var(--surface-2);
  color: var(--muted);
  border: 0;
  cursor: pointer;
  font-family: inherit;
}
.pill.on { background: var(--ok-bg); color: var(--ok-ink); }
.pill.off { background: var(--warn-bg); color: var(--warn-ink); }
.pill.bot { background: var(--ok-bg); color: var(--ok-ink); }
.pill.human { background: var(--warn-bg); color: var(--human); }

.stage { min-width: 0; min-height: 0; display: flex; flex-direction: column; }
.empty { margin: auto; max-width: 420px; padding: 24px; }
.empty h1 { font-size: 28px; margin: 8px 0 10px; }
.empty p { color: var(--muted); }
.sim { display: grid; gap: 8px; margin-top: 28px; padding-top: 18px; border-top: 1px solid var(--line); }

.thread-wrap { height: 100%; display: flex; flex-direction: column; min-height: 0; }
.thread-head {
  display: flex;
  justify-content: space-between;
  gap: 16px;
  padding: 16px 22px;
  border-bottom: 1px solid var(--line);
  background: color-mix(in srgb, var(--surface) 88%, transparent);
  backdrop-filter: blur(10px);
  align-items: center;
}
.thread-title { display: flex; align-items: center; gap: 8px; min-width: 0; }
.thread-back { display: none; flex-shrink: 0; width: 36px; height: 36px; }
.thread-head h1 { font-size: 20px; }
.thread-head p { margin: 4px 0 0; color: var(--muted); font-size: 13px; }
.actions { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }

.thread {
  flex: 1;
  overflow: auto;
  padding: 22px;
  display: flex;
  flex-direction: column;
  gap: 10px;
  background: var(--bg);
}

.bubble {
  max-width: 68%;
  padding: 10px 12px;
  background: var(--surface);
  border: 1px solid var(--line);
  display: grid;
  gap: 4px;
  border-radius: 14px;
  box-shadow: 0 1px 2px rgba(16, 24, 40, 0.04);
}
.bubble.in { align-self: flex-start; }
.bubble.out {
  align-self: flex-end;
  background: color-mix(in srgb, var(--brand) 14%, var(--surface));
  border-color: color-mix(in srgb, var(--brand) 25%, var(--line));
}
.bubble.system {
  align-self: center;
  background: transparent;
  border: 0;
  color: var(--muted);
  font-size: 12px;
  text-align: center;
  box-shadow: none;
}
.bubble .who { font-size: 11px; font-weight: 700; color: var(--brand); }
.bubble.out .who { color: var(--human); }
.bubble .when { font-size: 11px; color: var(--muted); }

.composer {
  display: grid;
  grid-template-columns: minmax(90px, 150px) auto 1fr auto;
  gap: 8px;
  padding: 14px 18px 18px;
  background: var(--surface);
  border-top: 1px solid var(--line);
  align-items: end;
}
.composer select {
  max-width: 160px;
  border: 1px solid var(--line);
  background: var(--surface);
  color: var(--ink);
  font: inherit;
  font-size: 12px;
  padding: 10px 8px;
  border-radius: var(--radius-sm);
}
.composer textarea { resize: none; min-height: 44px; max-height: 140px; }
.composer .error { grid-column: 1 / -1; margin: 0; }
.composer-preview {
  grid-column: 1 / -1;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  padding: 8px 12px;
  border: 1px dashed var(--line);
  border-radius: 10px;
  background: var(--surface-2);
  font-size: 13px;
}
.composer-preview.hidden { display: none; }
.composer-preview button {
  width: 28px;
  height: 28px;
  padding: 0;
  background: transparent;
  color: var(--muted);
  border: 0;
}
.composer-tools { display: flex; gap: 6px; }
.icon-btn.rec {
  background: #c0392b;
  color: #fff;
  border-color: #c0392b;
}
.bubble .media-img {
  max-width: min(260px, 70vw);
  border-radius: 10px;
  display: block;
  margin: 6px 0;
}
.bubble audio {
  width: min(240px, 70vw);
  margin: 6px 0;
  display: block;
}
.bubble .file-chip {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  margin: 6px 0;
  padding: 8px 10px;
  border-radius: 8px;
  border: 1px solid var(--line);
  color: inherit;
  text-decoration: none;
  font-size: 13px;
}
.icon-btn {
  width: 44px;
  height: 44px;
  padding: 0;
  display: grid;
  place-items: center;
  background: var(--surface);
  color: var(--ink);
  border: 1px solid var(--line);
  border-radius: 10px;
}
.icon-btn svg { width: 18px; height: 18px; }
.icon-btn:hover { background: var(--surface-2); }
.qc-wrap { position: relative; }
.qc-btn { color: var(--on-brand); background: var(--brand); border-color: var(--brand); }
.qc-btn:hover { background: var(--brand-hover); }
.qc-btn.is-open { background: var(--brand-deep); }
.qc-menu {
  position: absolute;
  right: 0;
  top: calc(100% + 8px);
  z-index: 55;
  min-width: 230px;
  padding: 6px;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
}
.qc-item {
  width: 100%;
  display: flex;
  align-items: center;
  gap: 10px;
  background: transparent;
  color: var(--ink);
  border: 0;
  padding: 9px 10px;
  border-radius: var(--radius-sm);
  font: inherit;
  font-weight: 600;
  text-align: left;
  cursor: pointer;
}
.qc-item:hover { background: var(--surface-2); }
.qc-item svg { width: 16px; height: 16px; flex-shrink: 0; color: var(--brand); }
.wa-lines { list-style: none; padding: 0; margin: 10px 0 14px; display: grid; gap: 6px; }
.wa-line {
  width: 100%;
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px 10px;
  border: 1px solid var(--line);
  background: var(--surface);
  color: inherit;
  font: inherit;
  border-radius: 8px;
  cursor: pointer;
}
.wa-line.on { border-color: var(--brand); background: color-mix(in srgb, var(--brand) 10%, var(--surface)); }
.wa-line .muted { margin-left: auto; font-size: 12px; }

#wa-modal:empty { display: none; }
.wa-back {
  position: fixed;
  inset: 0;
  background: rgba(10, 16, 22, 0.48);
  display: grid;
  place-items: center;
  z-index: 80;
  padding: 18px;
}
.wa-card {
  width: min(420px, 100%);
  background: var(--surface);
  color: var(--ink);
  border-radius: var(--radius);
  padding: 24px;
  box-shadow: var(--shadow);
  border: 1px solid var(--line);
}
.wa-card h2 { font-size: 20px; margin-bottom: 6px; }
.wa-card .sub { color: var(--muted); font-size: 13px; margin: 0 0 14px; }
.wa-card img.qr {
  width: 240px;
  height: 240px;
  display: block;
  margin: 12px auto;
  background: #fff;
  border-radius: 10px;
  padding: 8px;
}
.wa-card .pair {
  font-family: ui-monospace, Consolas, monospace;
  font-size: 22px;
  letter-spacing: 0.18em;
  text-align: center;
  font-weight: 700;
  margin: 8px 0 14px;
}
.wa-actions { display: flex; gap: 8px; flex-wrap: wrap; margin-top: 16px; }
.wa-actions button { flex: 1; min-width: 120px; }
.wa-dot {
  display: inline-block;
  width: 8px;
  height: 8px;
  border-radius: 50%;
  margin-right: 6px;
  background: var(--warn-ink);
}
.wa-dot.on { background: var(--ok-ink); }

@media (max-width: 860px) {
  .app { grid-template-columns: 1fr; }
  .stage { display: none; }
  .app[data-thread="1"] .rail { display: none; }
  .app[data-thread="1"] .stage { display: flex; min-height: 0; }
  .thread-back { display: grid; }
}
