:root {
  --bg: #f3f5f8;
  --card: #ffffff;
  --text: #1c2430;
  --muted: #6b7684;
  --line: #e3e7ed;
  --primary: #0e7490;
  --primary-dark: #0b5d73;
  --primary-soft: #e0f2f7;
  --warn: #b45309;
  --warn-soft: #fef3c7;
  --good: #15803d;
  --good-soft: #dcfce7;
  --bad: #b91c1c;
  --bad-soft: #fee2e2;
  --accent: #6d28d9;
  --accent-soft: #ede9fe;
  --radius: 10px;
  --shadow: 0 1px 2px rgba(16, 24, 40, .06), 0 1px 3px rgba(16, 24, 40, .08);
}

* { box-sizing: border-box; }
html, body { margin: 0; }
body {
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, Arial, sans-serif;
  font-size: 15px; line-height: 1.45; color: var(--text); background: var(--bg);
}
a { color: var(--primary); text-decoration: none; }
a:hover { text-decoration: underline; }
h1, h2, h3 { margin: 0 0 .5rem; line-height: 1.25; }
h1 { font-size: 1.35rem; }
h2 { font-size: 1.15rem; }
h3 { font-size: .95rem; text-transform: uppercase; letter-spacing: .03em; color: var(--muted); }
code { background: #eef1f5; padding: .1rem .35rem; border-radius: 5px; font-size: .9em; }
code.big { font-size: 1rem; padding: .4rem .6rem; display: inline-block; word-break: break-all; }
.muted { color: var(--muted); }
.hint { color: var(--muted); font-size: .85rem; margin: .5rem 0 0; }

/* üst panel */
.topbar {
  display: flex; align-items: center; gap: 1.5rem; padding: .7rem 1.25rem;
  background: #0f2a36; color: #fff; position: sticky; top: 0; z-index: 10; flex-wrap: wrap;
}
.brand { color: #fff; font-weight: 700; font-size: 1.05rem; }
.brand span { color: #67e8f9; font-weight: 600; }
.brand:hover { text-decoration: none; }
.topbar nav { display: flex; gap: .25rem; flex-wrap: wrap; }
.topbar nav a { color: #cbd5e1; padding: .35rem .7rem; border-radius: 7px; }
.topbar nav a:hover { background: rgba(255,255,255,.08); text-decoration: none; }
.topbar nav a.active { background: rgba(103, 232, 249, .16); color: #fff; }
.me { margin-left: auto; display: flex; align-items: center; gap: .6rem; color: #cbd5e1; }
.me form { margin: 0; }
button.link { background: none; border: 0; color: #67e8f9; cursor: pointer; font: inherit; padding: 0; }

main.page { max-width: 1180px; margin: 0 auto; padding: 1.25rem; }
main.page.wide { max-width: 1400px; }
.page-title { margin-bottom: 1rem; }

/* düymələr və formalar */
.btn {
  background: var(--primary); color: #fff; border: 0; border-radius: 8px; padding: .55rem 1rem;
  font: inherit; font-weight: 600; cursor: pointer; white-space: nowrap;
}
.btn:hover { background: var(--primary-dark); }
.btn.small { padding: .35rem .7rem; font-size: .85rem; }
.btn.ghost { background: transparent; color: var(--primary); border: 1px solid var(--primary); }
.btn.ghost:hover { background: var(--primary-soft); }
.btn.wide { width: 100%; }
input, select, textarea {
  font: inherit; color: inherit; border: 1px solid var(--line); border-radius: 8px;
  padding: .5rem .65rem; background: #fff; width: 100%;
}
input:focus, select:focus, textarea:focus { outline: 2px solid var(--primary-soft); border-color: var(--primary); }
label { display: block; font-size: .85rem; color: var(--muted); margin-bottom: .6rem; }
label input, label textarea { margin-top: .2rem; color: var(--text); }

.flash { padding: .65rem .9rem; border-radius: 8px; margin-bottom: 1rem; background: var(--primary-soft); }
.flash.error { background: var(--bad-soft); color: var(--bad); }
.flash.ok { background: var(--good-soft); color: var(--good); }
.flash.warn { background: var(--warn-soft); color: var(--warn); }

.card { background: var(--card); border-radius: var(--radius); box-shadow: var(--shadow); padding: 1rem; margin-bottom: 1rem; }

/* giriş */
main.login-page { min-height: 100vh; display: grid; place-items: center; padding: 1rem; }
.login-card { background: #fff; padding: 2rem; border-radius: 14px; box-shadow: var(--shadow); width: 100%; max-width: 380px; }
.login-card h1 { font-size: 1.5rem; }

/* statistika */
.stats { display: grid; grid-template-columns: repeat(5, 1fr); gap: .75rem; margin-bottom: 1rem; }
.stat { background: #fff; border-radius: var(--radius); box-shadow: var(--shadow); padding: .8rem 1rem; color: var(--text); border-top: 3px solid var(--primary); }
.stat:hover { text-decoration: none; transform: translateY(-1px); }
.stat b { display: block; font-size: 1.6rem; line-height: 1.1; }
.stat span { color: var(--muted); font-size: .85rem; }
.stat.warn { border-top-color: #f59e0b; }
.stat.accent { border-top-color: var(--accent); }
.stat.good { border-top-color: var(--good); }

/* filtr */
.filters { display: flex; gap: .5rem; margin-bottom: 1rem; flex-wrap: wrap; }
.filters input { flex: 2 1 220px; }
.filters select { flex: 1 1 150px; width: auto; }

/* cədvəl */
.table-wrap { background: #fff; border-radius: var(--radius); box-shadow: var(--shadow); overflow-x: auto; }
table.leads { width: 100%; border-collapse: collapse; }
table.leads th { text-align: left; font-size: .78rem; text-transform: uppercase; color: var(--muted); padding: .7rem .9rem; border-bottom: 1px solid var(--line); }
table.leads td { padding: .7rem .9rem; border-bottom: 1px solid var(--line); vertical-align: top; }
table.leads tr[data-href] { cursor: pointer; }
table.leads tr[data-href]:hover td { background: #f8fafc; }
table.leads tr.unread td { background: #f0f9ff; }
table.leads tr.unread .name { font-weight: 700; }
table.leads tr.unread .name::before { content: "● "; color: var(--primary); }
.leads .name { color: var(--text); font-weight: 600; }
.sub { color: var(--muted); font-size: .82rem; margin-top: .15rem; }
.last { max-width: 320px; }
.preview { color: #344054; font-size: .9rem; overflow: hidden; text-overflow: ellipsis; }
.empty { text-align: center; color: var(--muted); padding: 2rem !important; }
.pager { display: flex; gap: 1rem; justify-content: center; align-items: center; margin: 1rem 0; color: var(--muted); }

/* nişanlar */
.ch { display: inline-block; font-size: .72rem; font-weight: 600; padding: .05rem .45rem; border-radius: 99px; background: #eef1f5; color: #475467; }
.ch.whatsapp { background: #dcfce7; color: #166534; }
.ch.instagram { background: #fce7f3; color: #9d174d; }
.ch.messenger { background: #dbeafe; color: #1e40af; }
.tag { display: inline-block; font-size: .78rem; padding: .05rem .5rem; border-radius: 6px; background: #eef1f5; }
.status { display: inline-block; font-size: .8rem; font-weight: 600; padding: .1rem .55rem; border-radius: 99px; }
.status.new { background: var(--primary-soft); color: var(--primary-dark); }
.status.handed_off { background: var(--warn-soft); color: var(--warn); }
.status.offer_sent { background: var(--accent-soft); color: var(--accent); }
.status.won { background: var(--good-soft); color: var(--good); }
.status.lost { background: #eef1f5; color: var(--muted); }
.badge { background: #f59e0b; color: #fff; border-radius: 99px; font-size: .72rem; padding: 0 .45rem; }

/* müştəri səhifəsi */
.lead-layout { display: grid; grid-template-columns: minmax(0, 1fr) 340px; gap: 1rem; align-items: start; }
.chat-panel { background: #fff; border-radius: var(--radius); box-shadow: var(--shadow); display: flex; flex-direction: column; height: calc(100vh - 110px); min-height: 480px; }
.chat-head { display: flex; align-items: center; gap: .8rem; padding: .8rem 1rem; border-bottom: 1px solid var(--line); flex-wrap: wrap; }
.chat-head .who { flex: 1; min-width: 160px; }
.chat-head h1 { margin: 0; font-size: 1.15rem; }
.chat-head form { margin: 0; }
.back { font-size: 1.3rem; color: var(--muted); }
.mode-form { display: flex; align-items: center; gap: .5rem; }
.mode { font-size: .82rem; font-weight: 600; padding: .2rem .6rem; border-radius: 99px; }
.mode.bot { background: var(--primary-soft); color: var(--primary-dark); }
.mode.human { background: var(--warn-soft); color: var(--warn); }

.messages { flex: 1; overflow-y: auto; padding: 1rem; background: #f7f9fb; display: flex; flex-direction: column; gap: .45rem; }
.msg { display: flex; flex-direction: column; max-width: 78%; }
.msg.theirs { align-self: flex-start; }
.msg.mine { align-self: flex-end; align-items: flex-end; }
.bubble { padding: .55rem .8rem; border-radius: 14px; white-space: pre-wrap; word-wrap: break-word; overflow-wrap: anywhere; }
.msg.theirs .bubble { background: #fff; border: 1px solid var(--line); border-bottom-left-radius: 4px; }
.msg.mine .bubble { background: var(--primary); color: #fff; border-bottom-right-radius: 4px; }
.msg.mine.from-bot .bubble { background: #155e75; }
.msg.mine.from-agent .bubble { background: #0f766e; }
.test-chat .msg.mine .bubble { background: #2563eb; }
.msg .meta { font-size: .72rem; color: var(--muted); margin-top: .15rem; padding: 0 .3rem; }
.msg .failed { color: var(--bad); font-weight: 600; }
.ticks.read { color: #0ea5e9; }
.note { align-self: center; font-size: .78rem; color: var(--muted); background: #eef1f5; padding: .2rem .7rem; border-radius: 99px; text-align: center; max-width: 90%; }
.note.intro { border-radius: 10px; padding: .6rem .9rem; font-size: .85rem; }

.typing { padding: .3rem 1rem; color: var(--muted); font-size: .85rem; background: #f7f9fb; }
.typing span { animation: blink 1.2s infinite; }
.typing span:nth-child(2) { animation-delay: .2s; }
.typing span:nth-child(3) { animation-delay: .4s; }
@keyframes blink { 0%, 100% { opacity: .2; } 50% { opacity: 1; } }

.composer { border-top: 1px solid var(--line); padding: .75rem 1rem; margin: 0; }
.composer textarea { resize: vertical; min-height: 44px; }
.composer-row { display: flex; justify-content: space-between; align-items: center; gap: .75rem; margin-top: .5rem; }
.composer-row .hint { margin: 0; }
.window-closed { background: var(--warn-soft); color: var(--warn); padding: .7rem .9rem; border-radius: 8px; font-size: .9rem; }

.side .card h3 { margin-bottom: .7rem; }
.row-form { margin-bottom: .5rem; }
.card.ai { background: #f0fdfa; border: 1px solid #ccfbf1; }
.card.ai p { margin: .3rem 0; }
.next { font-weight: 600; }
.card.meta { font-size: .82rem; color: var(--muted); }
.info-form label { margin-bottom: .45rem; }

.facts { display: grid; grid-template-columns: auto 1fr; gap: .35rem .8rem; margin: 0 0 .8rem; font-size: .9rem; }
.facts dt { color: var(--muted); }
.facts dd { margin: 0; }
.ai-summary { background: #f0fdfa; border: 1px solid #ccfbf1; padding: .6rem .8rem; border-radius: 8px; font-size: .9rem; }
.ai-summary p { margin: .3rem 0 0; }

/* bildirişlər */
.notif { border: 1px solid var(--line); border-left: 4px solid var(--good); border-radius: 8px; padding: .5rem .7rem; margin-bottom: .5rem; font-size: .85rem; }
.notif.not_configured { border-left-color: #94a3b8; background: #f8fafc; }
.notif.failed { border-left-color: var(--bad); background: var(--bad-soft); }
.notif-head { color: var(--muted); }
.notif-title { font-weight: 600; margin: .15rem 0; }
.notif-status { color: var(--muted); font-size: .8rem; }
.notif details { margin-top: .3rem; }
.notif summary { cursor: pointer; color: var(--primary); font-size: .8rem; }
.notif pre { white-space: pre-wrap; font: inherit; font-size: .8rem; margin: .3rem 0 0; background: #fff; padding: .4rem; border-radius: 6px; }
a.btn { display: inline-block; text-align: center; }
a.btn:hover { text-decoration: none; color: #fff; }

/* bilik bazası */
.kb-layout { display: grid; grid-template-columns: 260px minmax(0, 1fr); gap: 1rem; align-items: start; }
.kb-files a { display: block; padding: .45rem .6rem; border-radius: 7px; color: var(--text); }
.kb-files a.active { background: var(--primary-soft); color: var(--primary-dark); font-weight: 600; }
.kb-files a:hover { text-decoration: none; background: #f1f5f9; }
.kb-new { display: flex; gap: .4rem; margin-top: .8rem; }
.kb-head { display: flex; justify-content: space-between; align-items: center; margin-bottom: .6rem; gap: 1rem; }
.kb-editor textarea { min-height: 62vh; font-family: ui-monospace, "Cascadia Mono", Consolas, monospace; font-size: .88rem; line-height: 1.5; }

table.checks { border-collapse: collapse; width: 100%; }
table.checks td { padding: .45rem .5rem; border-bottom: 1px solid var(--line); }
ul.plain { margin: 0; padding-left: 1.1rem; }

@media (max-width: 980px) {
  .stats { grid-template-columns: repeat(2, 1fr); }
  .lead-layout, .kb-layout { grid-template-columns: 1fr; }
  .chat-panel { height: 75vh; }
}
@media (max-width: 600px) {
  main.page { padding: .75rem; }
  .topbar { gap: .6rem; padding: .6rem .8rem; }
  .me { margin-left: 0; }
  .msg { max-width: 92%; }
  table.leads th:nth-child(3), table.leads td:nth-child(3) { display: none; }
}

.ad-pick { margin-bottom: .5rem; }
.ad-pick select { margin-top: .2rem; }
