:root {
  --bg: #f6f7f9; --surface: #ffffff; --ink: #14171f; --muted: #667085; --line: #e4e7ec;
  --brand: #0f766e; --brand-ink: #ffffff; --brand-soft: #e6f4f2;
  --good: #067647; --good-bg: #e7f6ec; --warn: #9a6700; --warn-bg: #fff4d6; --bad: #b42318; --bad-bg: #fdecea;
  --info: #175cd3; --bubble-in: #ffffff; --bubble-out: #dcf4ee; --read: #1d8cf8;
  --radius: 12px; --shadow: 0 1px 2px rgba(16,24,40,.06), 0 1px 3px rgba(16,24,40,.08);
  --mono: ui-monospace, "SF Mono", Menlo, Consolas, monospace;
}
* { box-sizing: border-box; }
[hidden] { display: none !important; }
html { font-size: 17px; }
body { margin: 0; background: var(--bg); color: var(--ink); font: 1rem/1.5 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif; }
a { color: var(--brand); text-decoration: none; }
a:hover { text-decoration: underline; }
h1 { font-size: 1.75rem; margin: .1rem 0 .2rem; letter-spacing: -.01em; }
h2 { font-size: 1.15rem; margin: 1.5rem 0 .75rem; }
.card h2 { margin-top: 0; }
.muted { color: var(--muted); }
.small { font-size: .85rem; }
.mono { font-family: var(--mono); font-size: .9em; }
.bad-text { color: var(--bad); }

/* top bar */
.topbar { display: flex; align-items: center; gap: 2rem; padding: .8rem 2rem; background: var(--surface); border-bottom: 1px solid var(--line); position: sticky; top: 0; z-index: 20; }
.brand { font-weight: 700; color: var(--ink); white-space: nowrap; }
.brand:hover { text-decoration: none; }
.brand.big { font-size: 1.5rem; }
.brand-mark { color: var(--brand); }
.tag { font-size: .7rem; font-weight: 600; text-transform: uppercase; letter-spacing: .06em; background: var(--warn-bg); color: var(--warn); padding: .1rem .45rem; border-radius: 6px; vertical-align: middle; }
.topbar nav { display: flex; gap: .3rem; }
.topbar nav a { color: var(--muted); padding: .4rem .8rem; border-radius: 8px; font-weight: 500; }
.topbar nav a.active, .topbar nav a:hover { color: var(--ink); background: var(--bg); text-decoration: none; }
.topbar-right { margin-left: auto; display: flex; gap: .5rem; align-items: center; }
.topbar-right form { margin: 0; }

/* layout */
.page { max-width: 1240px; margin: 0 auto; padding: 1.75rem 2rem 4rem; transition: margin-right .2s; }
body.drawer-open .page { margin-right: max(2rem, calc(560px + 1rem)); }
.page-head { display: flex; justify-content: space-between; align-items: flex-end; gap: 1rem; margin-bottom: 1.25rem; }
.crumb { font-size: .9rem; color: var(--muted); }
.actions { display: flex; gap: .5rem; align-items: center; }
.card { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); box-shadow: var(--shadow); padding: 1.25rem 1.4rem; }
.card-head { display: flex; justify-content: space-between; align-items: center; margin-bottom: .75rem; }
.card-head h2 { margin: 0; }
.grid2 { display: grid; grid-template-columns: 1.6fr 1fr; gap: 1.25rem; align-items: start; }
.grid3 { display: grid; grid-template-columns: 2fr 1fr 1fr; gap: 1rem; }
.empty { padding: 1.5rem; text-align: center; color: var(--muted); }

/* tabs */
.tabs { display: flex; gap: .25rem; border-bottom: 1px solid var(--line); margin-bottom: 1.25rem; }
.tabs a { padding: .6rem 1rem; color: var(--muted); font-weight: 600; border-bottom: 2px solid transparent; margin-bottom: -1px; }
.tabs a.active { color: var(--ink); border-color: var(--brand); }
.tabs a:hover { text-decoration: none; color: var(--ink); }

/* buttons + forms */
.btn { display: inline-flex; align-items: center; gap: .4rem; font: inherit; font-weight: 600; padding: .55rem 1.05rem; border-radius: 9px; border: 1px solid var(--line); background: var(--surface); color: var(--ink); cursor: pointer; white-space: nowrap; }
.btn:hover { background: var(--bg); text-decoration: none; }
.btn.primary { background: var(--brand); border-color: var(--brand); color: var(--brand-ink); }
.btn.primary:hover { filter: brightness(1.08); }
.btn.ghost { border-color: transparent; background: transparent; }
.btn.small { font-size: .85rem; padding: .35rem .75rem; }
.btn.wide { width: 100%; justify-content: center; }
.btn:disabled { opacity: .6; cursor: progress; }
.btn.locked:disabled { cursor: not-allowed; }
.btn.busy::after { content: ""; width: .8em; height: .8em; border: 2px solid currentColor; border-right-color: transparent; border-radius: 50%; animation: spin .7s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }
label { display: flex; flex-direction: column; gap: .35rem; font-weight: 600; font-size: .92rem; margin-bottom: 1rem; }
input, select, textarea { font: inherit; font-weight: 400; padding: .6rem .75rem; border: 1px solid #d0d5dd; border-radius: 9px; background: #fff; color: var(--ink); }
input:focus, select:focus, textarea:focus { outline: 2px solid var(--brand-soft); border-color: var(--brand); }
textarea { resize: vertical; }
.hint { font-weight: 400; font-size: .82rem; color: var(--muted); }
.form.narrow { max-width: 620px; }

/* pills + alerts */
.pill { display: inline-block; font-size: .78rem; font-weight: 600; padding: .12rem .55rem; border-radius: 999px; background: var(--bg); color: var(--muted); border: 1px solid var(--line); white-space: nowrap; }
.pill.tiny { font-size: .7rem; padding: 0 .4rem; }
.pill.good, .pill.status-approved { background: var(--good-bg); color: var(--good); border-color: transparent; }
.pill.warn, .pill.status-pending, .pill.status-in_appeal, .pill.status-paused { background: var(--warn-bg); color: var(--warn); border-color: transparent; }
.pill.bad, .pill.status-rejected, .pill.status-disabled, .pill.status-deleted { background: var(--bad-bg); color: var(--bad); border-color: transparent; }
.alert { border-radius: 10px; padding: .8rem 1rem; margin-bottom: 1rem; }
.alert.error { background: var(--bad-bg); color: var(--bad); }
.alert.warn { background: var(--warn-bg); color: var(--warn); }
.alert.good { background: var(--good-bg); color: var(--ink); }

/* tables */
.table { width: 100%; border-collapse: collapse; }
.table th { text-align: left; font-size: .78rem; text-transform: uppercase; letter-spacing: .05em; color: var(--muted); font-weight: 600; padding: .5rem .6rem; border-bottom: 1px solid var(--line); }
.table td { padding: .75rem .6rem; border-bottom: 1px solid var(--line); vertical-align: top; }
.table tr:last-child td { border-bottom: 0; }
.link-row { cursor: pointer; }
.link-row:hover td { background: #fafbfc; }
.facts { display: grid; grid-template-columns: max-content 1fr; gap: .45rem 1rem; margin: 1rem 0 0; }
.facts dt { color: var(--muted); font-size: .88rem; }
.facts dd { margin: 0; word-break: break-all; }
pre.json { margin: 0; background: #0f172a; color: #e2e8f0; padding: 1rem; border-radius: 10px; overflow: auto; max-height: 480px; font: .82rem/1.45 var(--mono); }
.flash { animation: flash 1.6s ease-out; }
@keyframes flash { from { background: #fff7d1; } to { background: transparent; } }

/* feed */
.feed { list-style: none; margin: 0; padding: 0; }
.feed li { display: flex; gap: .8rem; align-items: center; padding: .55rem .2rem; border-bottom: 1px solid var(--line); }
.feed li:last-child { border-bottom: 0; }
.feed .time { color: var(--muted); font-size: .85rem; min-width: 9.5rem; }

/* login */
.login-wrap { min-height: 100vh; display: grid; place-items: center; padding: 1rem; }
.login-card { width: 100%; max-width: 400px; padding: 2rem; }
.login-card p { margin-top: .2rem; margin-bottom: 1.5rem; }

/* inbox */
.inbox { display: grid; grid-template-columns: 300px 1fr; gap: 1rem; height: calc(100vh - 350px); min-height: 480px; }
.contacts { padding: 0; display: flex; flex-direction: column; overflow: hidden; }
.contacts-head { display: flex; justify-content: space-between; align-items: center; gap: .5rem; padding: .9rem 1rem; border-bottom: 1px solid var(--line); }
.contacts-head select { flex: 1; }
.new-chat { display: flex; gap: .4rem; padding: .6rem 1rem; border-bottom: 1px solid var(--line); }
.new-chat input { flex: 1; min-width: 0; padding: .4rem .6rem; }
#contact-list { list-style: none; margin: 0; padding: 0; overflow-y: auto; }
#contact-list li[data-id] { display: flex; gap: .75rem; align-items: center; padding: .8rem 1rem; cursor: pointer; border-bottom: 1px solid var(--line); }
#contact-list li[data-id]:hover { background: #fafbfc; }
#contact-list li.active { background: var(--brand-soft); }
.avatar { width: 40px; height: 40px; border-radius: 50%; background: #cbd5e1; color: #fff; display: grid; place-items: center; font-weight: 700; flex: none; }
.c-name { font-weight: 600; }
.thread { padding: 0; display: flex; flex-direction: column; overflow: hidden; }
.thread-head { padding: .9rem 1.25rem; border-bottom: 1px solid var(--line); display: flex; gap: .75rem; align-items: baseline; }
.bubbles { flex: 1; overflow-y: auto; padding: 1.25rem; background: #efeae2; display: flex; flex-direction: column; gap: .5rem; }
.bubble { max-width: 70%; padding: .55rem .8rem .35rem; border-radius: 12px; background: var(--bubble-in); box-shadow: 0 1px 1px rgba(0,0,0,.08); align-self: flex-start; }
.bubble.out { background: var(--bubble-out); align-self: flex-end; }
.bubble-body { white-space: pre-wrap; word-wrap: break-word; }
.bubble-meta { font-size: .75rem; color: var(--muted); text-align: right; margin-top: .15rem; }
.bubble-tag { font-size: .72rem; font-weight: 600; color: var(--brand); text-transform: uppercase; letter-spacing: .04em; }
.bubble-error { font-size: .8rem; color: var(--bad); margin-top: .25rem; }
.ticks { font-weight: 700; letter-spacing: -.2em; margin-left: .25rem; padding-right: .2em; }
.ticks.read { color: var(--read); }
.ticks.failed { color: var(--bad); letter-spacing: 0; }
.ticks.pending { letter-spacing: 0; }
.composer { border-top: 1px solid var(--line); padding: .9rem 1.25rem; background: var(--surface); }
.window-note { margin-bottom: .6rem; font-size: .9rem; }
.text-form { display: flex; gap: .6rem; align-items: flex-end; }
.text-form textarea { flex: 1; }
.tpl-row { display: flex; gap: .6rem; }
.tpl-row select { flex: 1; }
.tpl-params { display: flex; flex-wrap: wrap; gap: .6rem; margin-top: .6rem; }
.tpl-params label { flex: 1 1 180px; margin: 0; }
.tpl-preview { margin-top: .5rem; font-size: .9rem; white-space: pre-wrap; }
.send-error { margin: .6rem 0 0; }

/* templates */
.templates-layout { display: flex; flex-direction: column; gap: 1.25rem; }
.examples { display: flex; flex-wrap: wrap; gap: .75rem; align-items: flex-end; }
.examples > div { flex-basis: 100%; }
.examples label { flex: 1 1 200px; margin-bottom: .5rem; }
.preview-wrap { background: #efeae2; padding: 1rem; border-radius: 10px; margin: .5rem 0 1rem; }
.preview { max-width: 520px; margin-top: .4rem; white-space: pre-wrap; }
.result { margin-top: 1rem; }

/* API activity drawer */
.drawer { position: fixed; top: 58px; right: 0; bottom: 0; width: 560px; max-width: 100vw; background: var(--surface); border-left: 1px solid var(--line); box-shadow: -8px 0 24px rgba(16,24,40,.08); transform: translateX(100%); transition: transform .2s; z-index: 15; display: flex; flex-direction: column; }
.drawer.open { transform: none; }
.drawer-head { display: grid; grid-template-columns: 1fr auto; gap: .1rem 1rem; align-items: center; padding: .9rem 1rem; border-bottom: 1px solid var(--line); }
.drawer-head .muted { grid-column: 1; grid-row: 2; }
.drawer-head .btn { grid-column: 2; grid-row: 1 / span 2; }
.drawer .activity-wrap { overflow-y: auto; flex: 1; }
.activity { width: 100%; border-collapse: collapse; font-size: .82rem; }
.activity th { text-align: left; color: var(--muted); font-weight: 600; padding: .45rem .5rem; position: sticky; top: 0; background: var(--surface); }
.activity td { padding: .45rem .5rem; border-top: 1px solid var(--line); vertical-align: top; }
.activity .path { word-break: break-all; }
.dot { width: 8px; height: 8px; border-radius: 50%; background: var(--line); display: inline-block; transition: background .3s; }
.dot.on { background: var(--good); }

.toast { position: fixed; left: 50%; bottom: 1.5rem; transform: translate(-50%, 2rem); opacity: 0; background: var(--ink); color: #fff; padding: .75rem 1.1rem; border-radius: 10px; max-width: 640px; transition: all .2s; pointer-events: none; z-index: 30; }
.toast.show { opacity: 1; transform: translate(-50%, 0); }
.toast.error { background: var(--bad); }

/* connect */
.checklist { list-style: none; padding: 0; margin: 1rem 0; }
.checklist li { display: flex; gap: .8rem; align-items: flex-start; padding: .7rem 0; border-bottom: 1px solid var(--line); font-size: 1.05rem; }
.checklist .mark { width: 1.6rem; height: 1.6rem; border-radius: 50%; border: 2px solid var(--line); display: grid; place-items: center; flex: none; font-size: .9rem; font-weight: 700; }
.checklist li.ok .mark { background: var(--good); border-color: var(--good); color: #fff; }
.checklist li.error .mark { background: var(--bad); border-color: var(--bad); color: #fff; }
.checklist li.running .mark { border-color: var(--brand); border-right-color: transparent; animation: spin .8s linear infinite; }
.checklist li.skipped { color: var(--muted); }
.checklist .detail { font-size: .85rem; color: var(--muted); }
.checklist .detail.bad-text { color: var(--bad); }
.pin-box { font: 700 1.6rem var(--mono); letter-spacing: .25em; }

@media (max-width: 960px) {
  .grid2, .grid3, .inbox { grid-template-columns: 1fr; }
  .inbox { height: auto; }
  body.drawer-open .page { margin-right: auto; }
  .topbar { flex-wrap: wrap; gap: .75rem; padding: .75rem 1rem; }
  .page { padding: 1.25rem 1rem 3rem; }
}

/* Setup page */
.setup-status { display: flex; flex-wrap: wrap; gap: 1rem 2.5rem; margin-bottom: 1.25rem; }
.status-item { display: flex; flex-direction: column; gap: .3rem; align-items: flex-start; }
.step { display: grid; grid-template-columns: 2.25rem 1fr; gap: 1rem; margin-bottom: 1.25rem; }
.step.is-off { opacity: .7; }
.step-num { width: 2rem; height: 2rem; border-radius: 50%; background: var(--brand-soft); color: var(--brand); font-weight: 700; display: grid; place-items: center; }
.step.is-off .step-num { background: var(--bg); color: var(--muted); }
.step-body { min-width: 0; }
.step-where { margin: -.25rem 0 1rem; }
.setup-row { display: grid; grid-template-columns: 11rem 1fr; gap: 1rem; align-items: center; padding: .7rem 0; border-top: 1px solid var(--line); }
.setup-label { color: var(--muted); font-size: .88rem; font-weight: 600; }
.copy-field { display: flex; align-items: center; gap: .5rem; min-width: 0; background: var(--bg); border: 1px solid var(--line); border-radius: 9px; padding: .3rem .3rem .3rem .75rem; }
.copy-field code { flex: 1; min-width: 0; font: .9rem var(--mono); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.chips { display: flex; flex-wrap: wrap; gap: .4rem; }
.chip { font: .82rem var(--mono); padding: .2rem .6rem; border-radius: 7px; background: var(--brand-soft); color: var(--brand); }
.chip.optional { background: var(--bg); color: var(--muted); border: 1px dashed var(--line); }
.toast.good { background: var(--good); }
@media (max-width: 720px) { .setup-row { grid-template-columns: 1fr; gap: .4rem; } }
.token-form { margin-top: 1rem; padding-top: 1rem; border-top: 1px solid var(--line); }
.token-form input { width: 100%; box-sizing: border-box; }
