:root {
  --green: #00a884;
  --green-dark: #008069;
  --panel: #f0f2f5;
  --rail: #0b141a;
  --bubble-out: #d9fdd3;
  --bubble-in: #ffffff;
  --chat-bg: #efeae2;
  --tick: #53bdeb;
  --text: #111b21;
  --muted: #667781;
  --line: #e9edef;
  --amber-bg: #fef3c7;
  --amber: #92400e;
  --red: #dc2626;
  --violet: #6d28d9;
  --violet-bg: #ede9fe;
  --radius: 10px;
  --shadow: 0 1px 0.5px rgba(11, 20, 26, 0.13);
}

* { box-sizing: border-box; }

html, body { height: 100%; margin: 0; }

body {
  background: var(--panel);
  color: var(--text);
  font: 14px/1.4 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  overflow: hidden;
}

button { font: inherit; color: inherit; cursor: pointer; }
svg { display: block; }

/* ---------- App shell ---------- */

.app { display: flex; height: 100%; }

.rail {
  width: 68px;
  flex-shrink: 0;
  background: var(--rail);
  color: #fff;
  display: flex;
  flex-direction: column;
  align-items: center;
  padding: 12px 0;
  gap: 18px;
}
.rail-logo {
  width: 40px; height: 40px;
  border-radius: 12px;
  display: grid; place-items: center;
  background: var(--green);
  color: #fff;
}
.rail-logo svg { width: 24px; height: 24px; }
.rail-items { display: flex; flex-direction: column; gap: 6px; flex: 1; }
.rail-btn {
  width: 56px;
  padding: 8px 0 6px;
  border: 0;
  border-radius: 12px;
  background: transparent;
  color: rgba(255, 255, 255, 0.65);
  display: flex; flex-direction: column; align-items: center; gap: 3px;
  font-size: 10px;
  transition: background 0.15s, color 0.15s;
}
.rail-btn svg { width: 22px; height: 22px; fill: none; stroke: currentColor; stroke-width: 1.7; stroke-linecap: round; stroke-linejoin: round; }
.rail-btn:hover { background: rgba(255, 255, 255, 0.08); color: #fff; }
.rail-btn.is-active { background: rgba(0, 168, 132, 0.22); color: #fff; }
.rail-avatar {
  width: 34px; height: 34px; border-radius: 50%;
  display: grid; place-items: center;
  background: #2a3942; font-weight: 600;
}

.view { flex: 1; min-width: 0; display: flex; overflow: hidden; }

.demo-label {
  position: fixed;
  right: 10px; bottom: 10px;
  z-index: 50;
  padding: 4px 10px;
  border-radius: 999px;
  background: rgba(11, 20, 26, 0.78);
  color: #fff;
  font-size: 11px;
  pointer-events: none;
}

/* ---------- Conversation list ---------- */

.list {
  width: 330px;
  flex-shrink: 0;
  background: #fff;
  border-right: 1px solid var(--line);
  display: flex; flex-direction: column;
  min-height: 0;
}
.list-head { padding: 14px 14px 8px; }
.list-head h1 { margin: 0 0 2px; font-size: 19px; }
.list-head p { margin: 0 0 10px; color: var(--muted); font-size: 12px; }
.search {
  display: flex; align-items: center; gap: 8px;
  background: var(--panel);
  border-radius: 8px;
  padding: 7px 10px;
  color: var(--muted);
  font-size: 13px;
}
.search svg { width: 16px; height: 16px; fill: none; stroke: currentColor; stroke-width: 2; }
.chips { display: flex; gap: 6px; padding: 10px 14px; overflow-x: auto; scrollbar-width: none; }
.chips::-webkit-scrollbar { display: none; }
.chip {
  flex-shrink: 0;
  border: 0;
  border-radius: 999px;
  padding: 5px 11px;
  background: var(--panel);
  color: #54656f;
  font-size: 12.5px;
}
.chip.is-active { background: #d9fdd3; color: var(--green-dark); font-weight: 600; }
.chip .n { opacity: 0.65; margin-left: 3px; }

.convos { flex: 1; overflow-y: auto; }
.convo {
  display: flex; gap: 11px; width: 100%;
  padding: 10px 14px 0;
  border: 0; background: #fff; text-align: left;
  transition: background 0.15s;
}
.convo:hover { background: #f5f6f6; }
.convo.is-active { background: #f0f2f5; }
.convo.flash { animation: flash 1.2s ease; }
@keyframes flash { 0% { background: #d9fdd3; } 100% { background: #fff; } }
.convo-body { flex: 1; min-width: 0; padding-bottom: 10px; border-bottom: 1px solid var(--line); }
.convo-top, .convo-bottom { display: flex; align-items: center; gap: 6px; }
.convo-name { flex: 1; min-width: 0; font-size: 15px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.convo-time { font-size: 11.5px; color: var(--muted); }
.convo-time.unread { color: var(--green); font-weight: 600; }
.convo-last { flex: 1; min-width: 0; color: var(--muted); font-size: 13px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; display: flex; align-items: center; gap: 3px; }
.badge-n { min-width: 19px; height: 19px; padding: 0 5px; border-radius: 999px; background: var(--green); color: #fff; font-size: 11px; font-weight: 700; display: grid; place-items: center; }

.tag { display: inline-flex; align-items: center; gap: 4px; padding: 1px 7px; border-radius: 999px; font-size: 10.5px; font-weight: 600; white-space: nowrap; }
.tag-ai { background: var(--violet-bg); color: var(--violet); }
.tag-human { background: #fee2e2; color: #b91c1c; }
.tag-closed { background: var(--amber-bg); color: var(--amber); }
.tag-done { background: #e7f6ef; color: var(--green-dark); }
.convo-tags { margin-top: 5px; display: flex; gap: 5px; }

.avatar {
  width: 46px; height: 46px; flex-shrink: 0;
  border-radius: 50%;
  display: grid; place-items: center;
  color: #fff; font-weight: 600; font-size: 16px;
}
.avatar.sm { width: 38px; height: 38px; font-size: 14px; }
.avatar.unknown { background: #dfe5e7 !important; color: #8696a0; }
.avatar.unknown svg { width: 24px; height: 24px; fill: currentColor; }

/* ---------- Chat ---------- */

.chat { flex: 1; min-width: 0; display: flex; flex-direction: column; background: var(--chat-bg); }
.chat-head {
  display: flex; align-items: center; gap: 10px;
  padding: 8px 14px;
  background: var(--panel);
  border-bottom: 1px solid #d1d7db;
  min-height: 60px;
}
.back { display: none; border: 0; background: none; padding: 4px; margin-left: -6px; }
.back svg { width: 22px; height: 22px; fill: none; stroke: #54656f; stroke-width: 2; }
.chat-who { flex: 1; min-width: 0; }
.chat-name { font-size: 15.5px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.chat-sub { display: flex; align-items: center; gap: 6px; flex-wrap: wrap; color: var(--muted); font-size: 12px; margin-top: 2px; }
.window { padding: 1px 8px; border-radius: 999px; font-size: 11px; font-weight: 600; }
.window.open { background: #d1fae5; color: #047857; }
.window.closed { background: var(--amber-bg); color: var(--amber); }
.head-actions { display: flex; align-items: center; gap: 8px; }
.btn {
  border: 1px solid #d1d7db; background: #fff;
  border-radius: 8px; padding: 6px 11px; font-size: 12.5px; font-weight: 600;
  white-space: nowrap;
  transition: background 0.15s, border-color 0.15s;
}
.btn:hover { background: #f5f6f6; }
.btn.primary { background: var(--green); border-color: var(--green); color: #fff; }
.btn.primary:hover { background: var(--green-dark); }
.btn.ghost-ai { color: var(--violet); border-color: #ddd6fe; }
.btn.panel-toggle { display: none; }

.toggle { display: inline-flex; align-items: center; gap: 7px; font-size: 12px; color: #54656f; border: 0; background: none; padding: 0; }
.switch { width: 32px; height: 18px; border-radius: 999px; background: #c4ccd0; position: relative; transition: background 0.2s; }
.switch::after { content: ""; position: absolute; top: 2px; left: 2px; width: 14px; height: 14px; border-radius: 50%; background: #fff; transition: transform 0.2s; box-shadow: 0 1px 2px rgba(0,0,0,.2); }
.toggle.on .switch { background: var(--violet); }
.toggle.on .switch::after { transform: translateX(14px); }

.messages {
  flex: 1; overflow-y: auto;
  padding: 14px 6% 10px;
  background-color: var(--chat-bg);
  background-image: radial-gradient(rgba(0, 0, 0, 0.045) 1px, transparent 1px);
  background-size: 18px 18px;
  display: flex; flex-direction: column; gap: 3px;
}
.day { align-self: center; margin: 6px 0 8px; padding: 4px 11px; border-radius: 7px; background: #fff; color: var(--muted); font-size: 11.5px; box-shadow: var(--shadow); }
.sys { align-self: center; margin: 8px 0; padding: 5px 12px; border-radius: 7px; background: #fdf4c5; color: #54656f; font-size: 12px; text-align: center; max-width: 90%; box-shadow: var(--shadow); }
.sys.violet { background: var(--violet-bg); color: var(--violet); }

.msg { max-width: min(78%, 440px); display: flex; flex-direction: column; animation: pop 0.28s cubic-bezier(0.22, 1, 0.36, 1) both; }
.msg.in { align-self: flex-start; }
.msg.out { align-self: flex-end; }
.msg + .msg.in, .msg + .msg.out { margin-top: 4px; }
@keyframes pop { from { opacity: 0; transform: translateY(6px) scale(0.98); } to { opacity: 1; transform: none; } }
.msg.still { animation: none; }
.card code { font-size: 12.5px; background: var(--panel); padding: 1px 6px; border-radius: 5px; }
.bubble { padding: 6px 8px 7px 9px; border-radius: var(--radius); box-shadow: var(--shadow); white-space: pre-line; word-wrap: break-word; }
.in .bubble { background: var(--bubble-in); border-top-left-radius: 3px; }
.out .bubble { background: var(--bubble-out); border-top-right-radius: 3px; }
.by { font-size: 11.5px; font-weight: 600; margin-bottom: 2px; display: flex; align-items: center; gap: 5px; }
.by.ai { color: var(--violet); }
.by.agent { color: var(--green-dark); }
.by.template { color: #54656f; }
.reply-tag { font-size: 11px; color: var(--muted); display: flex; align-items: center; gap: 4px; margin-bottom: 2px; }
.reply-tag svg { width: 12px; height: 12px; fill: none; stroke: currentColor; stroke-width: 2; }
.meta { float: right; margin: 6px 0 -4px 10px; font-size: 11px; color: var(--muted); display: inline-flex; align-items: center; gap: 3px; }
.ticks svg { width: 16px; height: 11px; }
.ticks.read { color: var(--tick); }
.ticks.pending svg { width: 12px; height: 12px; }
.wa-buttons { display: flex; flex-direction: column; gap: 2px; margin-top: 2px; }
.wa-buttons .wb {
  background: #fff; border-radius: 8px; box-shadow: var(--shadow);
  text-align: center; padding: 7px; color: #027eb5; font-size: 13.5px; font-weight: 500;
}
.wa-buttons .wb.picked { background: #e7f3fb; }
.wa-buttons .wb svg { display: inline-block; vertical-align: -2px; width: 14px; height: 14px; fill: none; stroke: currentColor; stroke-width: 2; margin-right: 4px; }
.tpl-head { font-weight: 700; margin-bottom: 3px; }
.tpl-foot { color: var(--muted); font-size: 12px; margin-top: 4px; }
.var { background: rgba(0, 168, 132, 0.14); border-radius: 4px; padding: 0 3px; }

.typing { align-self: flex-end; display: flex; align-items: center; gap: 8px; margin-top: 4px; font-size: 11.5px; color: var(--violet); animation: pop 0.2s both; }
.typing.in { align-self: flex-start; color: var(--muted); }
.dots { display: inline-flex; gap: 3px; background: var(--bubble-out); padding: 9px 11px; border-radius: var(--radius); box-shadow: var(--shadow); }
.typing.in .dots { background: #fff; }
.dots i { width: 6px; height: 6px; border-radius: 50%; background: #8696a0; animation: blink 1.1s infinite; }
.dots i:nth-child(2) { animation-delay: 0.15s; }
.dots i:nth-child(3) { animation-delay: 0.3s; }
@keyframes blink { 0%, 60%, 100% { opacity: 0.3; transform: none; } 30% { opacity: 1; transform: translateY(-2px); } }

.composer { display: flex; align-items: center; gap: 8px; padding: 8px 12px; background: var(--panel); }
.composer .field {
  flex: 1; min-width: 0;
  background: #fff; border-radius: 8px; padding: 10px 12px;
  color: var(--muted); font-size: 14px;
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.composer .send { width: 42px; height: 42px; border-radius: 50%; border: 0; background: var(--green); color: #fff; display: grid; place-items: center; flex-shrink: 0; }
.composer .send svg { width: 20px; height: 20px; fill: currentColor; }
.composer.locked .field { background: #fff8e6; color: var(--amber); }
.composer.ai .field { color: var(--violet); }

.empty-chat { flex: 1; display: grid; place-items: center; text-align: center; color: var(--muted); padding: 24px; }

/* ---------- AI side panel ---------- */

.panel {
  width: 300px; flex-shrink: 0;
  background: #fff;
  border-left: 1px solid var(--line);
  overflow-y: auto;
  padding: 16px;
}
.panel h2 { margin: 0 0 14px; font-size: 15px; display: flex; align-items: center; gap: 8px; }
.panel h2 .x { margin-left: auto; display: none; border: 0; background: none; font-size: 20px; line-height: 1; color: var(--muted); }
.sec { margin-bottom: 18px; }
.sec-title { font-size: 11px; font-weight: 700; letter-spacing: 0.06em; text-transform: uppercase; color: var(--muted); margin-bottom: 7px; }
.intent { display: flex; justify-content: space-between; align-items: baseline; font-weight: 600; margin-bottom: 6px; }
.intent span { font-size: 12px; color: var(--muted); font-weight: 500; }
.bar { height: 6px; background: var(--panel); border-radius: 999px; overflow: hidden; }
.bar i { display: block; height: 100%; width: 0; border-radius: inherit; background: var(--violet); transition: width 0.9s cubic-bezier(0.22, 1, 0.36, 1); }
.bar.low i { background: #f59e0b; }
.src { display: flex; flex-wrap: wrap; gap: 5px; }
.src span { padding: 3px 8px; border-radius: 6px; background: var(--panel); font-size: 12px; }
.steps { margin: 0; padding: 0; list-style: none; }
.steps li { position: relative; padding: 0 0 9px 18px; font-size: 13px; }
.steps li::before { content: ""; position: absolute; left: 3px; top: 5px; width: 7px; height: 7px; border-radius: 50%; background: var(--green); }
.steps li.warn::before { background: #f59e0b; }
.steps li small { display: block; color: var(--muted); font-size: 11.5px; }
.note { padding: 10px; border-radius: 8px; background: var(--panel); font-size: 12.5px; color: #3b4a54; }
.kv { display: grid; grid-template-columns: auto 1fr; gap: 5px 10px; font-size: 12.5px; }
.kv dt { color: var(--muted); }
.kv dd { margin: 0; text-align: right; }

/* ---------- Pages (agent, templates, analytics) ---------- */

.page { flex: 1; overflow-y: auto; padding: 24px clamp(14px, 3vw, 32px) 60px; }
.page-head { display: flex; align-items: flex-end; justify-content: space-between; gap: 12px; flex-wrap: wrap; margin-bottom: 18px; }
.page-head h1 { margin: 0; font-size: 22px; }
.page-head p { margin: 4px 0 0; color: var(--muted); }
.grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(260px, 1fr)); gap: 14px; }
.card { background: #fff; border-radius: 12px; padding: 16px; box-shadow: 0 1px 2px rgba(11, 20, 26, 0.06); animation: pop 0.35s both; }
.card h3 { margin: 0 0 4px; font-size: 14.5px; }
.card > p { margin: 0 0 12px; color: var(--muted); font-size: 12.5px; }
.row { display: flex; align-items: center; justify-content: space-between; gap: 10px; padding: 8px 0; border-top: 1px solid var(--line); font-size: 13px; }
.row:first-of-type { border-top: 0; }
.row small { display: block; color: var(--muted); font-size: 11.5px; }
.dot { width: 8px; height: 8px; border-radius: 50%; background: var(--green); display: inline-block; margin-right: 6px; }
.dot.amber { background: #f59e0b; }

.tester { display: flex; flex-direction: column; gap: 10px; }
.tester .messages { flex: none; height: 230px; border-radius: 10px; padding: 12px; }
.suggest { display: flex; flex-wrap: wrap; gap: 6px; }
.suggest button { border: 1px solid #d1d7db; background: #fff; border-radius: 999px; padding: 5px 11px; font-size: 12.5px; }
.suggest button:hover { border-color: var(--green); color: var(--green-dark); }
.ask { display: flex; gap: 8px; }
.ask input { flex: 1; min-width: 0; border: 1px solid #d1d7db; border-radius: 8px; padding: 8px 10px; font: inherit; outline: none; }
.ask input:focus { border-color: var(--green); }

.table { width: 100%; border-collapse: collapse; background: #fff; border-radius: 12px; overflow: hidden; box-shadow: 0 1px 2px rgba(11, 20, 26, 0.06); }
.table th, .table td { text-align: left; padding: 11px 14px; border-top: 1px solid var(--line); font-size: 13px; }
.table th { border-top: 0; color: var(--muted); font-size: 11px; text-transform: uppercase; letter-spacing: 0.05em; background: #fafbfb; }
.table tr.pick { cursor: pointer; transition: background 0.15s; }
.table tr.pick:hover td { background: #f7f8f8; }
.table tr.is-active td { background: #effaf6; }
.table code { font-size: 12.5px; }
.status { font-weight: 600; font-size: 12px; }
.status.approved { color: var(--green-dark); }
.status.review { color: #b45309; }
.status.rejected { color: var(--red); }
.cat { padding: 2px 8px; border-radius: 999px; font-size: 11px; font-weight: 600; background: var(--panel); }
.cat.marketing { background: #fce7f3; color: #9d174d; }
.cat.utility { background: #dbeafe; color: #1e40af; }
.cat.authentication { background: #e0e7ff; color: #3730a3; }
.tpl-layout { display: grid; grid-template-columns: minmax(0, 1fr) 300px; gap: 16px; align-items: start; }
.phone { background: var(--chat-bg); border-radius: 16px; padding: 14px; min-height: 260px; display: flex; flex-direction: column; box-shadow: 0 1px 2px rgba(11, 20, 26, 0.06); }
.phone .msg { max-width: 100%; align-self: stretch; }

.kpis { display: grid; grid-template-columns: repeat(auto-fill, minmax(170px, 1fr)); gap: 12px; margin-bottom: 14px; }
.kpi { background: #fff; border-radius: 12px; padding: 14px 16px; box-shadow: 0 1px 2px rgba(11, 20, 26, 0.06); animation: pop 0.35s both; }
.kpi b { display: block; font-size: 24px; margin: 2px 0; }
.kpi span { color: var(--muted); font-size: 12px; }
.kpi em { font-style: normal; color: var(--green-dark); font-size: 12px; font-weight: 600; }
.chart { display: flex; align-items: flex-end; gap: 10px; height: 170px; padding-top: 10px; }
.chart .col { flex: 1; display: flex; flex-direction: column; align-items: center; gap: 6px; height: 100%; justify-content: flex-end; }
.chart .stack { width: 100%; max-width: 34px; display: flex; flex-direction: column-reverse; border-radius: 6px 6px 2px 2px; overflow: hidden; height: 0; transition: height 0.9s cubic-bezier(0.22, 1, 0.36, 1); }
.chart .stack i { display: block; }
.chart .stack .a { background: var(--violet); }
.chart .stack .h { background: #c4b5fd; }
.chart .col span { font-size: 11px; color: var(--muted); }
.legend { display: flex; gap: 14px; font-size: 12px; color: var(--muted); margin-top: 10px; }
.legend i { display: inline-block; width: 10px; height: 10px; border-radius: 3px; margin-right: 5px; vertical-align: -1px; }
.funnel .row b { font-weight: 600; }
.funnel .bar { margin-top: 5px; }
.funnel .bar i { background: var(--green); }

/* ---------- Responsive ---------- */

@media (max-width: 1180px) {
  .panel {
    position: fixed; top: 0; right: 0; bottom: 0; z-index: 30;
    width: min(320px, 100%);
    box-shadow: -8px 0 24px rgba(11, 20, 26, 0.15);
    transform: translateX(105%);
    transition: transform 0.25s ease;
  }
  .app.panel-open .panel { transform: none; }
  .panel h2 .x { display: block; }
  .btn.panel-toggle { display: inline-block; }
}

@media (max-width: 900px) {
  .list { width: 290px; }
  .tpl-layout { grid-template-columns: 1fr; }
}

@media (max-width: 760px) {
  .app { flex-direction: column-reverse; }
  .rail { width: 100%; height: 58px; flex-direction: row; padding: 0 8px; gap: 0; }
  .rail-logo, .rail-avatar { display: none; }
  .rail-items { flex-direction: row; justify-content: space-around; width: 100%; gap: 0; }
  .rail-btn { width: auto; flex: 1; padding: 6px 0; }
  .view { min-height: 0; }
  .list { width: 100%; border-right: 0; }
  .app[data-pane="chat"] .list { display: none; }
  .app[data-pane="list"] .chat { display: none; }
  .back { display: block; }
  .messages { padding: 12px 10px; }
  .msg { max-width: 86%; }
  .head-actions .toggle .lbl { display: none; }
  .demo-label { bottom: 66px; font-size: 10px; padding: 3px 8px; white-space: nowrap; }
}

@media (max-width: 420px) {
  .head-actions .btn.take { display: none; }
  .chat-head { padding: 8px 10px; }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation-duration: 0.01ms !important; transition-duration: 0.01ms !important; }
}
