:root {
  --bg: #ffffff;
  --bg-side: #f6f7f9;
  --bg-hover: #eceef2;
  --bg-active: #e1e8fb;
  --bg-input: #ffffff;
  --border: #dde1e7;
  --text: #16191f;
  --text-muted: #69707d;
  --accent: #2f6fed;
  --accent-text: #ffffff;
  --bubble-in: #eceef2;
  --bubble-in-text: #16191f;
  --bubble-out: #2f6fed;
  --bubble-out-text: #ffffff;
  --danger: #c93030;
  --ok: #1f8a4c;
  --shadow: 0 8px 30px rgba(15, 20, 30, 0.14);
  --radius: 10px;
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #15171c;
    --bg-side: #1b1e24;
    --bg-hover: #252931;
    --bg-active: #22324f;
    --bg-input: #1f232a;
    --border: #2e333c;
    --text: #e8eaee;
    --text-muted: #9198a5;
    --accent: #5b8cf5;
    --accent-text: #ffffff;
    --bubble-in: #2a2e37;
    --bubble-in-text: #e8eaee;
    --bubble-out: #3c74ee;
    --bubble-out-text: #ffffff;
    --danger: #f06565;
    --ok: #4cc27f;
    --shadow: 0 8px 30px rgba(0, 0, 0, 0.5);
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body {
  background: var(--bg);
  color: var(--text);
  font: 15px/1.4 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  -webkit-font-smoothing: antialiased;
}
button, input, select, textarea { font: inherit; color: inherit; }
button { cursor: pointer; }
a { color: var(--accent); }
[hidden] { display: none !important; }

.btn {
  border: 1px solid var(--border);
  background: var(--bg-input);
  border-radius: 8px;
  padding: 7px 14px;
  white-space: nowrap;
}
.btn:hover { background: var(--bg-hover); }
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-text); }
.btn.primary:hover { filter: brightness(1.08); }
.btn.danger { color: var(--danger); }
.btn:disabled { opacity: 0.5; cursor: default; }
.icon-btn {
  border: 0; background: transparent; border-radius: 8px;
  width: 36px; height: 36px; display: inline-grid; place-items: center;
  color: var(--text-muted); flex: none;
}
.icon-btn:hover { background: var(--bg-hover); color: var(--text); }
.icon-btn svg { width: 20px; height: 20px; }

input[type=text], input[type=password], input[type=tel], input[type=search], select, textarea {
  border: 1px solid var(--border);
  background: var(--bg-input);
  border-radius: 8px;
  padding: 8px 10px;
  width: 100%;
  outline: none;
}
input:focus, select:focus, textarea:focus { border-color: var(--accent); }

/* ---------- Auth screen ---------- */
.auth {
  min-height: 100%; display: grid; place-items: center; padding: 24px 16px;
  background: var(--bg-side);
}
.auth form {
  width: 100%; max-width: 360px; background: var(--bg); border: 1px solid var(--border);
  border-radius: 14px; padding: 28px; display: grid; gap: 14px; box-shadow: var(--shadow);
}
.auth h1 { margin: 0; font-size: 22px; display: flex; align-items: center; gap: 10px; }
.auth h1 img { width: 32px; height: 32px; }
.auth p { margin: 0; color: var(--text-muted); }
.auth label { display: grid; gap: 4px; font-size: 13px; color: var(--text-muted); }
.form-error { color: var(--danger); font-size: 14px; min-height: 1em; }

/* ---------- Main layout ---------- */
.layout { display: grid; grid-template-columns: 340px minmax(0, 1fr); height: 100dvh; }
.sidebar {
  background: var(--bg-side); border-right: 1px solid var(--border);
  display: flex; flex-direction: column; min-width: 0; min-height: 0;
}
.side-head { padding: 12px 12px 8px; display: grid; gap: 8px; }
.side-top { display: flex; align-items: center; gap: 6px; }
.side-top select { flex: 1; min-width: 0; font-weight: 600; }
.side-search { display: flex; gap: 6px; }
.side-search input { flex: 1; min-width: 0; }
.side-search .btn { flex: none; display: grid; place-items: center; color: var(--text-muted); }
.side-search .btn.primary { color: var(--accent-text); }
.side-search svg { width: 18px; height: 18px; }
.mock-banner {
  margin: 0 12px 8px; padding: 6px 10px; border-radius: 8px; font-size: 12px;
  background: #fff4d6; color: #6b4e00;
}
@media (prefers-color-scheme: dark) { .mock-banner { background: #3a3013; color: #f3d98a; } }

.conv-list { flex: 1; overflow-y: auto; padding: 0 6px 12px; }
.conv {
  display: grid; grid-template-columns: 42px 1fr auto; gap: 10px; align-items: center;
  padding: 9px 8px; border-radius: 10px; cursor: pointer; text-decoration: none; color: inherit;
}
.conv:hover { background: var(--bg-hover); }
.conv.active { background: var(--bg-active); }
.conv .main { min-width: 0; }
.conv .name { font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.conv .preview {
  color: var(--text-muted); font-size: 13.5px;
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.conv.unread .name, .conv.unread .preview { color: var(--text); font-weight: 700; }
.conv .meta { display: grid; justify-items: end; gap: 4px; font-size: 12px; color: var(--text-muted); }
.badge {
  background: var(--accent); color: var(--accent-text); border-radius: 999px;
  min-width: 20px; height: 20px; padding: 0 6px; font-size: 12px; font-weight: 700;
  display: inline-grid; place-items: center;
}
.via { font-size: 11px; color: var(--text-muted); }
.empty { color: var(--text-muted); text-align: center; padding: 40px 20px; }

.avatar {
  width: 42px; height: 42px; border-radius: 50%; display: grid; place-items: center;
  font-weight: 700; font-size: 15px; color: #fff; flex: none;
}

/* ---------- Thread ---------- */
.thread { display: flex; flex-direction: column; min-width: 0; min-height: 0; }
.thread-head {
  display: flex; align-items: center; gap: 10px; padding: 10px 14px;
  border-bottom: 1px solid var(--border); min-height: 61px;
}
.thread-head .title { flex: 1; min-width: 0; }
.thread-head .title .name { font-weight: 700; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.thread-head .title .sub { color: var(--text-muted); font-size: 13px; }
.back-btn { display: none; }

.messages { flex: 1; overflow-y: auto; padding: 16px 16px 8px; display: flex; flex-direction: column; gap: 2px; }
.day { text-align: center; color: var(--text-muted); font-size: 12px; margin: 14px 0 8px; }
.msg { display: flex; flex-direction: column; max-width: min(72%, 560px); }
.msg.in { align-self: flex-start; align-items: flex-start; }
.msg.out { align-self: flex-end; align-items: flex-end; }
.msg + .msg { margin-top: 2px; }
.msg.gap { margin-top: 10px; }
.bubble {
  padding: 8px 12px; border-radius: 18px; white-space: pre-wrap; overflow-wrap: anywhere;
}
.msg.in .bubble { background: var(--bubble-in); color: var(--bubble-in-text); border-bottom-left-radius: 6px; }
.msg.out .bubble { background: var(--bubble-out); color: var(--bubble-out-text); border-bottom-right-radius: 6px; }
.msg.out .bubble a { color: inherit; }
.msg.failed .bubble { opacity: 0.6; }
.msg .media { display: grid; gap: 4px; margin-bottom: 4px; }
.msg .media img, .msg .media video {
  max-width: 260px; max-height: 320px; border-radius: 14px; display: block; background: var(--bubble-in);
}
.msg .media a.file {
  display: inline-block; padding: 8px 12px; border-radius: 14px; background: var(--bubble-in);
  color: var(--bubble-in-text); text-decoration: none;
}
.msg .info { font-size: 11.5px; color: var(--text-muted); margin: 3px 6px 0; }
.msg .info .err { color: var(--danger); }
.msg .sender { font-size: 11.5px; color: var(--text-muted); margin: 0 6px 2px; }

.composer { border-top: 1px solid var(--border); padding: 10px 12px calc(10px + env(safe-area-inset-bottom)); }
.attachments { display: flex; gap: 8px; flex-wrap: wrap; margin-bottom: 8px; }
.attachment {
  position: relative; width: 64px; height: 64px; border-radius: 10px; overflow: hidden;
  background: var(--bg-hover); display: grid; place-items: center; font-size: 11px; color: var(--text-muted);
}
.attachment img { width: 100%; height: 100%; object-fit: cover; }
.attachment button {
  position: absolute; top: 2px; right: 2px; width: 20px; height: 20px; border-radius: 50%;
  border: 0; background: rgba(0,0,0,.6); color: #fff; font-size: 12px; line-height: 1;
}
.composer-row { display: flex; align-items: flex-end; gap: 6px; }
.composer textarea {
  resize: none; max-height: 160px; min-height: 40px; border-radius: 20px; padding: 9px 14px;
}
.send-btn {
  width: 40px; height: 40px; border-radius: 50%; border: 0; flex: none;
  background: var(--accent); color: var(--accent-text); display: grid; place-items: center;
}
.send-btn:disabled { opacity: 0.4; cursor: default; }
.send-btn svg { width: 18px; height: 18px; }
.counter { font-size: 11px; color: var(--text-muted); text-align: right; margin-top: 4px; min-height: 1em; }

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

/* ---------- New message ---------- */
.new-msg { padding: 16px; display: grid; gap: 12px; border-bottom: 1px solid var(--border); }
.new-msg label { display: grid; gap: 4px; font-size: 13px; color: var(--text-muted); }
.chips { display: flex; flex-wrap: wrap; gap: 6px; align-items: center; border: 1px solid var(--border); border-radius: 8px; padding: 4px 6px; background: var(--bg-input); }
.chips input { border: 0 !important; flex: 1; min-width: 140px; padding: 4px; }
.chip { background: var(--bg-active); border-radius: 999px; padding: 2px 4px 2px 10px; display: inline-flex; align-items: center; gap: 4px; font-size: 14px; }
.chip button { border: 0; background: transparent; color: var(--text-muted); padding: 0 4px; }
.suggest { border: 1px solid var(--border); border-radius: 8px; overflow: hidden; }
.suggest button { display: block; width: 100%; text-align: left; border: 0; background: var(--bg); padding: 8px 10px; }
.suggest button:hover { background: var(--bg-hover); }

/* ---------- Menus and dialogs ---------- */
.menu {
  position: fixed; z-index: 30; background: var(--bg); border: 1px solid var(--border);
  border-radius: 10px; box-shadow: var(--shadow); padding: 4px; min-width: 200px;
}
.menu button { display: block; width: 100%; text-align: left; border: 0; background: transparent; padding: 8px 12px; border-radius: 6px; }
.menu button:hover { background: var(--bg-hover); }
.menu .who { padding: 8px 12px; font-size: 13px; color: var(--text-muted); border-bottom: 1px solid var(--border); margin-bottom: 4px; }

dialog {
  border: 1px solid var(--border); border-radius: 14px; padding: 0; background: var(--bg); color: var(--text);
  box-shadow: var(--shadow); width: min(440px, calc(100vw - 32px));
}
dialog::backdrop { background: rgba(10, 12, 16, 0.45); }
dialog form { padding: 20px; display: grid; gap: 12px; }
dialog h2 { margin: 0; font-size: 18px; }
dialog label { display: grid; gap: 4px; font-size: 13px; color: var(--text-muted); }
dialog .actions { display: flex; justify-content: flex-end; gap: 8px; margin-top: 4px; }

/* ---------- Admin ---------- */
.admin { overflow-y: auto; padding: 20px; display: grid; grid-template-columns: minmax(0, 1fr); gap: 24px; align-content: start; }
.admin section { display: grid; grid-template-columns: minmax(0, 1fr); gap: 10px; }
.admin h2 { margin: 0; font-size: 17px; }
.admin .hint { color: var(--text-muted); font-size: 13.5px; margin: 0; }
.admin table { width: 100%; border-collapse: collapse; font-size: 14px; }
.admin th, .admin td { text-align: left; padding: 8px; border-bottom: 1px solid var(--border); vertical-align: top; }
.admin th { color: var(--text-muted); font-weight: 600; font-size: 12.5px; }
.admin .row-actions { display: flex; gap: 6px; justify-content: flex-end; flex-wrap: wrap; }
.admin td input { min-width: 100px; }
.admin .add-row { display: flex; gap: 8px; flex-wrap: wrap; }
.admin .add-row input { flex: 1; min-width: 140px; }
.checks { display: grid; gap: 6px; max-height: 180px; overflow-y: auto; }
.checks label { display: flex; gap: 8px; align-items: center; color: var(--text); font-size: 14px; }
code, pre { font-family: ui-monospace, SFMono-Regular, Menlo, monospace; font-size: 12.5px; }
.copy-line { display: flex; gap: 8px; align-items: center; }
.copy-line code { flex: 1; min-width: 0; background: var(--bg-side); border: 1px solid var(--border); padding: 8px 10px; border-radius: 8px; overflow-x: auto; white-space: nowrap; }
.log-entry { border: 1px solid var(--border); border-radius: 8px; padding: 8px 10px; display: grid; gap: 4px; }
.log-entry pre { margin: 0; white-space: pre-wrap; word-break: break-all; }
.log-entry .meta { font-size: 12px; color: var(--text-muted); display: flex; gap: 10px; }
.ok { color: var(--ok); } .bad { color: var(--danger); }
.table-wrap { overflow-x: auto; }

#toast {
  position: fixed; left: 50%; bottom: 24px; transform: translateX(-50%) translateY(20px);
  background: var(--text); color: var(--bg); padding: 10px 16px; border-radius: 10px;
  opacity: 0; pointer-events: none; transition: .2s; z-index: 50; max-width: calc(100vw - 32px);
}
#toast.show { opacity: 1; transform: translateX(-50%) translateY(0); }

/* ---------- Mobile: one pane at a time ---------- */
@media (max-width: 760px) {
  .layout { grid-template-columns: 1fr; }
  .layout.has-thread .sidebar { display: none; }
  .layout:not(.has-thread) .thread { display: none; }
  .back-btn { display: inline-grid; }
  .msg { max-width: 85%; }
  .admin { padding: 16px; }
}
