:root {
  --bg: #f6f7f9;
  --surface: #ffffff;
  --surface-2: #f1f3f6;
  --border: #e2e5ea;
  --text: #14171f;
  --muted: #6b7280;
  --accent: #3b5bdb;
  --accent-soft: #eef1fd;
  --green: #12805c;
  --green-soft: #e6f5ef;
  --red: #c0322b;
  --red-soft: #fdeceb;
  --amber: #a1650a;
  --amber-soft: #fdf3e2;
  --radius: 10px;
  --shadow: 0 1px 2px rgba(16, 24, 40, .06), 0 1px 3px rgba(16, 24, 40, .1);
  font-synthesis-weight: none;
}

* { box-sizing: border-box; }
html, body { height: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 14px/1.5 -apple-system, BlinkMacSystemFont, "Segoe UI", Inter, Roboto, sans-serif;
  -webkit-font-smoothing: antialiased;
}

/* ---------- layout ---------- */
.app { display: grid; grid-template-columns: 232px 1fr; min-height: 100vh; }

.sidebar {
  background: #101322; color: #c7ccd8; padding: 18px 12px;
  display: flex; flex-direction: column; gap: 2px; position: sticky; top: 0; height: 100vh;
}
.brand { display: flex; align-items: center; gap: 9px; padding: 4px 10px 18px; color: #fff; font-weight: 650; letter-spacing: -.01em; }
.brand svg { flex: none; }
.nav-item {
  display: flex; align-items: center; justify-content: space-between; gap: 8px;
  padding: 9px 11px; border-radius: 8px; cursor: pointer; color: #a8afbe;
  font-weight: 500; user-select: none; transition: background .12s, color .12s;
}
.nav-item:hover { background: #1b1f31; color: #e8eaf0; }
.nav-item.active { background: var(--accent); color: #fff; }
.nav-badge { background: #e0453c; color: #fff; border-radius: 999px; padding: 1px 7px; font-size: 11px; font-weight: 700; }
.nav-item.active .nav-badge { background: rgba(255,255,255,.25); }
.sidebar-foot { margin-top: auto; padding: 10px; font-size: 12px; color: #6c7488; }
.status-dot { display: inline-block; width: 7px; height: 7px; border-radius: 50%; margin-right: 6px; background: #e0453c; }
.status-dot.on { background: #23c197; }

.main { padding: 26px 30px 60px; max-width: 1180px; }
.page-head { display: flex; align-items: flex-start; justify-content: space-between; gap: 16px; margin-bottom: 20px; }
h1 { font-size: 22px; margin: 0 0 4px; letter-spacing: -.02em; }
h2 { font-size: 15px; margin: 0 0 12px; letter-spacing: -.01em; }
.sub { color: var(--muted); margin: 0; }

/* ---------- primitives ---------- */
.card { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); box-shadow: var(--shadow); padding: 18px; }
.card + .card { margin-top: 16px; }
.grid { display: grid; gap: 14px; }
.grid.cols-4 { grid-template-columns: repeat(4, 1fr); }
.grid.cols-3 { grid-template-columns: repeat(3, 1fr); }
.grid.cols-2 { grid-template-columns: repeat(2, 1fr); }
@media (max-width: 900px) { .grid.cols-4, .grid.cols-3, .grid.cols-2 { grid-template-columns: 1fr 1fr; } }

.stat { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 14px 16px; box-shadow: var(--shadow); }
.stat .label { color: var(--muted); font-size: 12px; text-transform: uppercase; letter-spacing: .04em; font-weight: 600; }
.stat .value { font-size: 26px; font-weight: 650; letter-spacing: -.02em; margin-top: 4px; font-variant-numeric: tabular-nums; }
.stat .hint { color: var(--muted); font-size: 12px; margin-top: 2px; }

button, .btn {
  font: inherit; font-weight: 550; cursor: pointer; border-radius: 8px;
  border: 1px solid var(--border); background: var(--surface); color: var(--text);
  padding: 8px 14px; transition: background .12s, border-color .12s;
}
button:hover { background: var(--surface-2); }
button:disabled { opacity: .5; cursor: not-allowed; }
.btn-primary { background: var(--accent); border-color: var(--accent); color: #fff; }
.btn-primary:hover { background: #3350c9; }
.btn-danger { color: var(--red); border-color: #f0c9c6; background: #fff; }
.btn-danger:hover { background: var(--red-soft); }
.btn-sm { padding: 5px 10px; font-size: 13px; }
.btn-ghost { border-color: transparent; background: transparent; color: var(--muted); }
.btn-ghost:hover { background: var(--surface-2); color: var(--text); }

input, select, textarea {
  font: inherit; width: 100%; padding: 8px 11px; border: 1px solid var(--border);
  border-radius: 8px; background: #fff; color: var(--text);
}
input:focus, select:focus, textarea:focus { outline: 2px solid var(--accent-soft); border-color: var(--accent); }
textarea { resize: vertical; min-height: 96px; line-height: 1.55; }
label { display: block; font-weight: 600; font-size: 13px; margin-bottom: 5px; }
.field { margin-bottom: 14px; }
.help, .field .help { color: var(--muted); font-size: 12px; margin-top: 5px; }
.row { display: flex; gap: 10px; align-items: center; flex-wrap: wrap; }
.row.tight { gap: 6px; }
.spacer { flex: 1; }

table { width: 100%; border-collapse: collapse; font-variant-numeric: tabular-nums; }
th { text-align: left; font-size: 12px; text-transform: uppercase; letter-spacing: .04em; color: var(--muted); padding: 8px 10px; border-bottom: 1px solid var(--border); font-weight: 600; }
td { padding: 10px; border-bottom: 1px solid var(--border); vertical-align: middle; }
tbody tr:hover { background: var(--surface-2); }
.table-wrap { overflow-x: auto; }

.pill { display: inline-flex; align-items: center; gap: 5px; padding: 2px 9px; border-radius: 999px; font-size: 12px; font-weight: 600; background: var(--surface-2); color: var(--muted); white-space: nowrap; }
.pill.green { background: var(--green-soft); color: var(--green); }
.pill.red { background: var(--red-soft); color: var(--red); }
.pill.amber { background: var(--amber-soft); color: var(--amber); }
.pill.blue { background: var(--accent-soft); color: var(--accent); }

.empty { text-align: center; padding: 44px 20px; color: var(--muted); }
.empty h3 { margin: 0 0 6px; color: var(--text); font-size: 15px; }

/* ---------- toast ---------- */
#toasts { position: fixed; right: 20px; bottom: 20px; display: flex; flex-direction: column; gap: 8px; z-index: 200; }
.toast { background: #101322; color: #fff; padding: 11px 16px; border-radius: 8px; box-shadow: 0 8px 24px rgba(0,0,0,.2); max-width: 380px; animation: slide .2s ease; }
.toast.err { background: var(--red); }
.toast.ok { background: var(--green); }
@keyframes slide { from { transform: translateY(8px); opacity: 0; } }

/* ---------- modal ---------- */
.modal-bg { position: fixed; inset: 0; background: rgba(16,24,40,.45); display: flex; align-items: flex-start; justify-content: center; padding: 60px 20px; z-index: 150; overflow-y: auto; }
.modal { background: #fff; border-radius: 12px; width: 100%; max-width: 620px; box-shadow: 0 20px 50px rgba(0,0,0,.25); }
.modal.wide { max-width: 860px; }
.modal-head { padding: 18px 22px; border-bottom: 1px solid var(--border); display: flex; align-items: center; justify-content: space-between; }
.modal-head h2 { margin: 0; font-size: 17px; }
.modal-body { padding: 22px; }
.modal-foot { padding: 14px 22px; border-top: 1px solid var(--border); display: flex; gap: 10px; justify-content: flex-end; background: var(--surface-2); border-radius: 0 0 12px 12px; }

/* ---------- inbox ---------- */
.inbox { display: grid; grid-template-columns: 330px 1fr; gap: 0; height: calc(100vh - 150px); border: 1px solid var(--border); border-radius: var(--radius); overflow: hidden; background: #fff; box-shadow: var(--shadow); }
.thread-list { border-right: 1px solid var(--border); overflow-y: auto; background: var(--surface); }
.thread { padding: 12px 14px; border-bottom: 1px solid var(--border); cursor: pointer; }
.thread:hover { background: var(--surface-2); }
.thread.active { background: var(--accent-soft); box-shadow: inset 3px 0 0 var(--accent); }
.thread .top { display: flex; justify-content: space-between; align-items: baseline; gap: 8px; }
.thread .name { font-weight: 620; }
.thread .time { color: var(--muted); font-size: 11px; white-space: nowrap; }
.thread .snippet { color: var(--muted); font-size: 13px; margin-top: 3px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.thread .unread { background: var(--accent); color: #fff; border-radius: 999px; font-size: 11px; padding: 0 6px; font-weight: 700; }

.thread-pane { display: flex; flex-direction: column; min-width: 0; }
.thread-head { padding: 13px 18px; border-bottom: 1px solid var(--border); display: flex; align-items: center; justify-content: space-between; gap: 10px; }
.messages { flex: 1; overflow-y: auto; padding: 18px; background: var(--bg); display: flex; flex-direction: column; gap: 10px; }
.bubble { max-width: 68%; padding: 9px 13px; border-radius: 14px; box-shadow: var(--shadow); word-wrap: break-word; overflow-wrap: anywhere; }
.bubble .text { white-space: pre-wrap; }
.bubble.in { background: #fff; border: 1px solid var(--border); align-self: flex-start; border-bottom-left-radius: 4px; }
.bubble.out { background: var(--accent); color: #fff; align-self: flex-end; border-bottom-right-radius: 4px; }
.bubble .meta { font-size: 11px; margin-top: 5px; opacity: .72; display: flex; gap: 6px; align-items: center; }
.bubble.out .meta { color: #dfe4fb; }
.composer { border-top: 1px solid var(--border); padding: 12px 14px; display: flex; gap: 10px; align-items: flex-end; background: #fff; }
.composer textarea { min-height: 44px; max-height: 140px; }

.counter { font-size: 12px; color: var(--muted); font-variant-numeric: tabular-nums; }
.counter b { color: var(--text); }
.mono { font-family: ui-monospace, SFMono-Regular, Menlo, monospace; font-size: 12.5px; }
.chip { display: inline-block; background: var(--surface-2); border: 1px solid var(--border); border-radius: 6px; padding: 2px 7px; font-size: 12px; cursor: pointer; margin: 0 4px 4px 0; font-family: ui-monospace, Menlo, monospace; }
.chip:hover { background: var(--accent-soft); border-color: var(--accent); color: var(--accent); }
.bar { height: 7px; border-radius: 999px; background: var(--surface-2); overflow: hidden; display: flex; }
.bar span { display: block; height: 100%; }
.hidden { display: none !important; }
.banner { padding: 11px 15px; border-radius: 8px; background: var(--amber-soft); color: var(--amber); border: 1px solid #f2dfbc; margin-bottom: 16px; font-weight: 500; }
.banner.blue { background: var(--accent-soft); color: var(--accent); border-color: #cdd6f7; }

/* ---------- sign-in / first-run gate ---------- */
.auth-gate {
  position: fixed; inset: 0; z-index: 300;
  background: var(--bg);
  display: flex; align-items: center; justify-content: center; padding: 24px;
}
.auth-card {
  background: var(--surface); border: 1px solid var(--border); border-radius: 14px;
  box-shadow: 0 10px 40px rgba(16,24,40,.10); padding: 30px; width: 100%; max-width: 420px;
}
.auth-brand {
  display: flex; align-items: center; gap: 9px; font-weight: 650;
  color: var(--accent); margin-bottom: 20px;
}
.auth-card h1 { font-size: 21px; margin: 0 0 6px; letter-spacing: -.02em; }
.auth-card .sub { margin: 0 0 22px; }
.auth-card input { padding: 10px 12px; font-size: 15px; }
.auth-card button { padding: 11px 16px; font-size: 15px; }
