:root {
  --bg: #f3f5f7;
  --surface: #ffffff;
  --line: #e2e7ec;
  --text: #17212b;
  --muted: #687684;
  --brand: #12a37a;
  --brand-dark: #0d7f5f;
  --brand-soft: #e3f6ef;
  --side: #10202f;
  --side-text: #c4d0db;
  --danger: #d64545;
  --warn: #b7791f;
  --bubble-in: #ffffff;
  --bubble-out: #dcf8c6;
  --thread-bg: #efeae2;
  --radius: 12px;
}

* { box-sizing: border-box; }

html, body { height: 100%; }

body {
  margin: 0;
  color: var(--text);
  background: var(--bg);
  font: 15px/1.45 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
}

a { color: var(--brand-dark); }
button, input, select, textarea { font: inherit; color: inherit; }
button { cursor: pointer; }
[hidden] { display: none !important; }
code { font-size: 13px; background: #eef1f4; padding: 1px 5px; border-radius: 5px; }

/* ---------- Shell ---------- */

.app { display: flex; min-height: 100vh; }

.sidebar {
  position: sticky;
  top: 0;
  height: 100vh;
  width: 232px;
  flex-shrink: 0;
  display: flex;
  flex-direction: column;
  padding: 18px 12px;
  background: var(--side);
  color: var(--side-text);
}

.brand { display: flex; align-items: center; gap: 10px; font-weight: 700; font-size: 17px; color: #fff; padding: 0 8px 22px; }
.brand-mark { width: 34px; height: 34px; display: grid; place-items: center; border-radius: 10px; background: var(--brand); color: #fff; font-weight: 800; }

.nav { display: flex; flex-direction: column; gap: 2px; flex: 1; }
.nav a {
  display: flex; align-items: center; gap: 11px;
  padding: 10px 10px; border-radius: 9px;
  color: var(--side-text); text-decoration: none; font-weight: 500;
}
.nav a:hover { background: rgba(255, 255, 255, 0.06); color: #fff; }
.nav a.active { background: rgba(18, 163, 122, 0.18); color: #fff; }
.nav svg, .sidebar-user svg { width: 20px; height: 20px; fill: none; stroke: currentColor; stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round; flex-shrink: 0; }
.nav span { flex: 1; }
.nav-badge { min-width: 22px; padding: 1px 7px; border-radius: 99px; background: var(--brand); color: #fff; font-size: 12px; text-align: center; }

.sidebar-user { display: flex; align-items: center; gap: 10px; padding: 12px 8px 0; border-top: 1px solid rgba(255, 255, 255, 0.08); margin: 0; }
.sidebar-user .who { flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.sidebar-user button { background: none; border: 0; color: var(--side-text); padding: 6px; border-radius: 8px; display: grid; }
.sidebar-user button:hover { background: rgba(255, 255, 255, 0.08); color: #fff; }
.avatar { width: 32px; height: 32px; border-radius: 50%; display: grid; place-items: center; background: #2a4157; color: #fff; font-weight: 700; flex-shrink: 0; }

.content { flex: 1; min-width: 0; padding: 28px 32px 48px; }
.content.full { padding: 0; height: 100vh; }
.page-title { margin: 0 0 20px; font-size: 24px; }

/* ---------- Generic UI ---------- */

.card { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); padding: 20px; }
.card h2 { margin: 0 0 14px; font-size: 17px; }
.card.narrow { max-width: 620px; }
.card-head { display: flex; justify-content: space-between; align-items: center; gap: 12px; margin-bottom: 6px; }
.card-head h2 { margin: 0; }
.stack { display: grid; gap: 18px; max-width: 980px; }
.split { display: grid; grid-template-columns: 280px 1fr; gap: 18px; align-items: start; }

label { display: block; margin: 14px 0 6px; font-size: 13px; font-weight: 600; color: var(--muted); }
input, select, textarea {
  width: 100%; padding: 10px 12px;
  border: 1px solid var(--line); border-radius: 9px; background: #fff;
}
input:focus, select:focus, textarea:focus { outline: 2px solid rgba(18, 163, 122, 0.35); border-color: var(--brand); }
textarea { min-height: 140px; resize: vertical; }
textarea.short { min-height: 70px; }

.button {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  padding: 10px 16px; border-radius: 9px; border: 1px solid var(--line);
  background: #fff; font-weight: 600; text-decoration: none; color: var(--text);
}
.button:hover { background: #f6f8fa; }
.button.primary { background: var(--brand); border-color: var(--brand); color: #fff; }
.button.primary:hover { background: var(--brand-dark); }
.button.danger { color: var(--danger); }
.button.small { padding: 6px 11px; font-size: 13px; }
.button.wide { width: 100%; margin-top: 18px; }
.button:disabled { opacity: 0.6; cursor: default; }
form > .button.primary:not(.wide) { margin-top: 14px; }
.link-button { background: none; border: 0; padding: 0; color: var(--danger); font-size: 13px; }

.hint { color: var(--muted); font-size: 13px; margin: 10px 0 0; }
.muted { color: var(--muted); }
.ok { color: var(--brand-dark); }
.bad { color: var(--danger); }

.notice { background: var(--brand-soft); border: 1px solid #b8e7d6; color: #0b5c45; padding: 11px 14px; border-radius: 9px; margin-bottom: 16px; }
.notice.error { background: #fdeeee; border-color: #f3c4c4; color: #8f2424; }
.content.full > .notice { position: fixed; top: 14px; right: 18px; z-index: 5; }

.table-wrap { overflow-x: auto; margin-top: 8px; }
table { width: 100%; border-collapse: collapse; font-size: 14px; }
th, td { text-align: left; padding: 10px 8px; border-bottom: 1px solid var(--line); vertical-align: top; }
th { font-size: 12px; color: var(--muted); font-weight: 600; text-transform: uppercase; letter-spacing: 0.04em; }
tr:last-child td { border-bottom: 0; }

.pill { display: inline-block; padding: 2px 9px; border-radius: 99px; font-size: 12px; font-weight: 600; background: #eef1f4; color: var(--muted); }
.pill.ok { background: var(--brand-soft); color: var(--brand-dark); }
.pill.bad { background: #fdeeee; color: var(--danger); }
.pill.warn { background: #fdf3e1; color: var(--warn); }

details { margin-top: 14px; }
summary { cursor: pointer; font-weight: 600; color: var(--brand-dark); }
.form-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(200px, 1fr)); gap: 0 14px; align-items: end; }
.form-grid .button { margin-top: 14px; }

.list-menu { display: flex; flex-direction: column; gap: 2px; margin-bottom: 14px; }
.list-menu a { display: flex; justify-content: space-between; padding: 9px 10px; border-radius: 8px; text-decoration: none; color: var(--text); }
.list-menu a:hover { background: #f3f5f7; }
.list-menu a.active { background: var(--brand-soft); color: var(--brand-dark); font-weight: 600; }
.list-menu b { color: var(--muted); font-weight: 500; }
.inline-form { display: flex; gap: 8px; }
.inline-form .button { flex-shrink: 0; margin-top: 0 !important; }

.template-preview { margin-top: 10px; padding: 12px 14px; border-radius: 10px; background: var(--bubble-out); white-space: pre-wrap; font-size: 14px; }

.stats { display: grid; grid-template-columns: repeat(auto-fit, minmax(150px, 1fr)); gap: 12px; margin-bottom: 16px; }
.stats div { background: #f6f8fa; border-radius: 10px; padding: 12px; }
.stats span { display: block; font-size: 12px; color: var(--muted); }
.stats strong { font-size: 15px; word-break: break-word; }
.progress { height: 8px; background: #eef1f4; border-radius: 99px; overflow: hidden; margin-bottom: 14px; }
.progress i { display: block; height: 100%; background: var(--brand); }

/* ---------- Login ---------- */

.login-page { display: grid; place-items: center; padding: 16px; }
.login { width: min(380px, 100%); }
.login-brand { color: var(--text); padding: 0 0 6px; }

/* ---------- Inbox ---------- */

.inbox { display: grid; grid-template-columns: 340px 1fr; height: 100vh; }
.conv-pane { display: flex; flex-direction: column; min-height: 0; background: #fff; border-right: 1px solid var(--line); }
.conv-head { padding: 16px 14px 10px; border-bottom: 1px solid var(--line); }
.conv-head-row { display: flex; justify-content: space-between; align-items: center; margin-bottom: 10px; }
.conv-head h2 { margin: 0; font-size: 19px; }
.new-chat { background: #f6f8fa; padding: 2px 12px 12px; border-radius: 10px; margin-bottom: 10px; }
.row-actions { display: flex; gap: 8px; margin-top: 12px; }
.tabs { display: flex; gap: 4px; margin-top: 10px; }
.tabs button { flex: 1; white-space: nowrap; padding: 6px 4px; border: 0; border-radius: 7px; background: none; font-size: 13px; color: var(--muted); font-weight: 600; }
.tabs button.active { background: var(--brand-soft); color: var(--brand-dark); }

.conv-list { flex: 1; overflow-y: auto; }
.conv-item { display: flex; gap: 11px; width: 100%; padding: 12px 14px; border: 0; border-bottom: 1px solid #f0f2f4; background: none; text-align: left; }
.conv-item:hover { background: #f6f8fa; }
.conv-item.active { background: #eaf6f1; }
.conv-item .avatar { width: 42px; height: 42px; background: #d7e2ea; color: #3e5366; }
.conv-main { flex: 1; min-width: 0; }
.conv-top, .conv-bottom { display: flex; justify-content: space-between; gap: 8px; }
.conv-name { font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.conv-time { font-size: 12px; color: var(--muted); flex-shrink: 0; }
.conv-preview { font-size: 13px; color: var(--muted); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.conv-item.unread .conv-time { color: var(--brand); font-weight: 600; }
.conv-item.unread .conv-preview { color: var(--text); }
.conv-badge { background: var(--brand); color: #fff; border-radius: 99px; font-size: 11px; min-width: 20px; padding: 1px 6px; text-align: center; font-weight: 700; flex-shrink: 0; }
.conv-line { font-size: 11px; color: var(--muted); }
.empty { padding: 30px 18px; color: var(--muted); text-align: center; font-size: 14px; }

.thread-pane { display: flex; flex-direction: column; min-height: 0; min-width: 0; background: var(--thread-bg); }
.thread-empty { flex: 1; display: grid; place-items: center; text-align: center; padding: 24px; color: var(--muted); }
.thread-empty p { max-width: 340px; }
.thread-head { display: flex; align-items: center; gap: 12px; padding: 11px 18px; background: #fff; border-bottom: 1px solid var(--line); }
.thread-head .avatar { background: #d7e2ea; color: #3e5366; }
.thread-head .title { flex: 1; min-width: 0; }
.thread-head strong { display: block; }
.thread-head small { color: var(--muted); }
.back-btn { display: none; background: none; border: 0; font-size: 22px; padding: 0 6px 0 0; }

.messages { flex: 1; overflow-y: auto; padding: 18px 6% 10px; display: flex; flex-direction: column; gap: 4px; }
.day { align-self: center; margin: 10px 0 6px; padding: 4px 12px; background: #fff; border-radius: 8px; font-size: 12px; color: var(--muted); box-shadow: 0 1px 0 rgba(0, 0, 0, 0.05); }
.msg { max-width: min(560px, 78%); padding: 7px 10px 5px; border-radius: 10px; background: var(--bubble-in); box-shadow: 0 1px 0 rgba(0, 0, 0, 0.08); align-self: flex-start; word-wrap: break-word; }
.msg.out { align-self: flex-end; background: var(--bubble-out); }
.msg.failed { background: #fde8e8; }
.msg-text { white-space: pre-wrap; }
.msg-tag { display: block; font-size: 11px; font-weight: 700; color: var(--brand-dark); margin-bottom: 2px; text-transform: uppercase; letter-spacing: 0.03em; }
.msg.button-click { border: 1px solid #9fd9c3; }
.msg-meta { display: flex; justify-content: flex-end; gap: 6px; font-size: 11px; color: var(--muted); margin-top: 2px; }
.tick.read { color: #2b8be6; }
.tick.failed { color: var(--danger); font-weight: 700; }
.msg-error { font-size: 12px; color: var(--danger); margin-top: 4px; }
.msg img, .msg video { display: block; max-width: 100%; max-height: 320px; border-radius: 8px; margin-bottom: 4px; }
.msg audio { display: block; max-width: 100%; }
.file-link { display: inline-block; padding: 6px 0; font-weight: 600; }

.composer { background: #f0f2f5; padding: 10px 14px 12px; border-top: 1px solid var(--line); }
.window-note { font-size: 13px; color: #7a5a14; background: #fff6df; border: 1px solid #f1dfae; padding: 8px 11px; border-radius: 8px; margin-bottom: 8px; }
.composer-row { display: flex; gap: 8px; align-items: flex-end; }
.composer textarea { min-height: 44px; max-height: 160px; resize: none; border-radius: 10px; }
.composer select { flex: 1; }
.composer .button { flex-shrink: 0; height: 44px; }
.composer-tools { display: flex; justify-content: space-between; margin-top: 6px; font-size: 12px; color: var(--muted); }
.composer-tools button { background: none; border: 0; padding: 0; color: var(--brand-dark); font-weight: 600; font-size: 12px; }
.composer .template-preview { margin: 0 0 8px; max-height: 180px; overflow: auto; }

/* ---------- Mobile ---------- */

@media (max-width: 860px) {
  .app { flex-direction: column; }
  .sidebar { position: static; width: auto; height: auto; flex-direction: row; align-items: center; padding: 8px 10px; gap: 8px; }
  .brand { padding: 0; }
  .brand span:last-child { display: none; }
  .nav { flex-direction: row; overflow-x: auto; gap: 2px; }
  .nav a { padding: 8px; }
  .nav a span { display: none; }
  .nav-badge { margin-left: -6px; }
  .sidebar-user { border: 0; padding: 0; }
  .sidebar-user .who, .sidebar-user .avatar { display: none; }
  .content { padding: 18px 16px 40px; }
  .content.full { flex: none; height: calc(100vh - 52px); height: calc(100dvh - 52px); }
  .split { grid-template-columns: 1fr; }
  .inbox { grid-template-columns: 1fr; height: 100%; }
  .inbox.has-active .conv-pane { display: none; }
  .inbox:not(.has-active) .thread-pane { display: none; }
  .back-btn { display: block; }
  .messages { padding: 14px 10px; }
  .msg { max-width: 88%; }
}
