:root {
  --bg: #f3f5f4;
  --card: #ffffff;
  --text: #1c2421;
  --muted: #66736e;
  --line: #e1e6e4;
  --brand: #0f766e;
  --brand-dark: #0b5d57;
  --in: #ffffff;
  --out: #d9f5ec;
  --error: #b42318;
  --ok: #067647;
}

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body { font: 15px/1.45 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Arial, sans-serif; background: var(--bg); color: var(--text); }
[hidden] { display: none !important; }
h1 { font-size: 1.5rem; margin: 8px 0 4px; }
h2 { font-size: 1.05rem; margin: 0 0 12px; }
.muted { color: var(--muted); }
.small { font-size: .85rem; }
.error { color: var(--error); min-height: 1em; margin: 8px 0 0; }
.ok { color: var(--ok); margin: 8px 0 0; }

input, select, textarea { font: inherit; padding: 9px 10px; border: 1px solid var(--line); border-radius: 8px; background: #fff; color: var(--text); width: 100%; }
input:focus, select:focus, textarea:focus { outline: 2px solid var(--brand); outline-offset: -1px; }
textarea { resize: vertical; }
label { display: block; font-size: .85rem; color: var(--muted); }
label > input, label > select, label > textarea { margin-top: 4px; color: var(--text); }
button { font: inherit; cursor: pointer; border-radius: 8px; border: 1px solid var(--line); background: #fff; padding: 9px 14px; }
button.primary { background: var(--brand); border-color: var(--brand); color: #fff; font-weight: 600; }
button.primary:hover { background: var(--brand-dark); }
button.primary:disabled { opacity: .6; cursor: wait; }
button.link { border: 0; background: none; color: var(--brand); padding: 4px; }

.card { background: var(--card); border: 1px solid var(--line); border-radius: 12px; padding: 16px; }

.login { min-height: 100%; display: grid; place-items: center; padding: 16px; }
.login-card { width: 100%; max-width: 380px; display: grid; gap: 12px; }
.login-logo { width: 56px; height: 56px; border-radius: 12px; }

.shell { min-height: 100%; display: flex; flex-direction: column; }
.topbar { display: flex; align-items: center; gap: 16px; padding: 10px 16px; background: var(--brand); color: #fff; flex-wrap: wrap; }
.brand { display: flex; align-items: center; gap: 8px; font-weight: 700; }
.brand img { width: 28px; height: 28px; border-radius: 6px; }
.topbar nav { display: flex; gap: 4px; flex: 1; flex-wrap: wrap; }
.topbar nav a { color: #e6f4f1; text-decoration: none; padding: 6px 10px; border-radius: 6px; }
.topbar nav a.active, .topbar nav a:hover { background: rgba(255, 255, 255, .16); color: #fff; }
.who { font-size: .9rem; }
.who .link { color: #fff; text-decoration: underline; }

main { flex: 1; padding: 16px; width: 100%; max-width: 1200px; margin: 0 auto; }
.view { display: grid; gap: 16px; }

.chats { grid-template-columns: 320px 1fr; height: calc(100vh - 90px); min-height: 480px; }
.chat-list { overflow: auto; padding: 12px; }
.chat-list-head { display: flex; align-items: center; justify-content: space-between; gap: 12px; margin-bottom: 8px; }
.chat-list-head h2 { margin: 0; }
.chat-list-head input { max-width: 240px; }
.chat-list ul { list-style: none; margin: 0; padding: 0; }
.chat-list li { padding: 10px; border-radius: 8px; cursor: pointer; border-bottom: 1px solid var(--line); }
.chat-list li:hover, .chat-list li.active { background: #edf7f5; }
.chat-list .name { font-weight: 600; display: flex; justify-content: space-between; gap: 8px; }
.chat-list .preview { color: var(--muted); font-size: .88rem; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }

.thread { display: flex; flex-direction: column; padding: 0; overflow: hidden; }
.thread-head { display: flex; flex-wrap: wrap; align-items: center; gap: 8px 12px; padding: 12px 16px; border-bottom: 1px solid var(--line); }
.thread-head .pill { margin-left: auto; }
.back { display: none; }
.messages { flex: 1; overflow: auto; padding: 16px; background: #efeae2; display: flex; flex-direction: column; gap: 6px; }
.bubble { max-width: 75%; padding: 8px 10px; border-radius: 10px; background: var(--in); box-shadow: 0 1px 1px rgba(0, 0, 0, .06); white-space: pre-wrap; word-wrap: break-word; }
.bubble.out { align-self: flex-end; background: var(--out); }
.bubble time { display: block; font-size: .72rem; color: var(--muted); text-align: right; margin-top: 2px; }
.composer { display: flex; gap: 8px; padding: 12px; border-top: 1px solid var(--line); align-items: flex-end; }
.template-composer { flex-wrap: wrap; }
.template-composer select { flex: 1 1 200px; width: auto; }
.params { display: flex; gap: 8px; flex: 1 1 100%; flex-wrap: wrap; }
.params input { flex: 1 1 140px; width: auto; }
.thread .error { padding: 0 12px 8px; }

.pill { font-size: .75rem; padding: 3px 8px; border-radius: 999px; background: #eef2f1; color: var(--muted); white-space: nowrap; }
.pill.green { background: #dcfae6; color: var(--ok); }
.pill.amber { background: #fef0c7; color: #93370d; }
.pill.red { background: #fee4e2; color: var(--error); }

.row-form { display: flex; gap: 8px; flex-wrap: wrap; align-items: flex-end; }
.row-form > * { flex: 1 1 180px; }
.row-form > button { flex: 0 0 auto; }
.stack-form { display: grid; gap: 12px; }
.stack-form > button { justify-self: start; }

.table { width: 100%; border-collapse: collapse; }
.table th, .table td { text-align: left; padding: 8px; border-bottom: 1px solid var(--line); vertical-align: top; }
.table th { font-size: .8rem; color: var(--muted); font-weight: 600; }
.table td.body { white-space: pre-wrap; max-width: 420px; }

.account-card { max-width: 440px; }

.stats { display: grid; grid-template-columns: repeat(auto-fit, minmax(160px, 1fr)); gap: 12px; }
.stat { border: 1px solid var(--line); border-radius: 10px; padding: 12px; }
.stat .label { color: var(--muted); font-size: .85rem; }
.stat .value { font-size: 1.4rem; font-weight: 700; }

@media (max-width: 760px) {
  main { padding: 12px 16px; }
  .chats { grid-template-columns: 1fr; height: calc(100vh - 130px); }
  .chats.open .chat-list { display: none; }
  .chats:not(.open) .thread { display: none; }
  .back { display: inline; }
  .table thead { display: none; }
  .table tr { display: block; border-bottom: 1px solid var(--line); padding: 6px 0; }
  .table td { display: block; border: 0; padding: 2px 0; }
}
