:root {
  --bg: #f4f5f7;
  --surface: #ffffff;
  --surface-2: #eef0f4;
  --text: #171a21;
  --muted: #5d6675;
  --border: #d9dde5;
  --accent: #1f6feb;
  --accent-text: #ffffff;
  --accent-soft: #e3eeff;
  --ok: #156c3a;
  --ok-soft: #e2f4e8;
  --err: #a8261c;
  --err-soft: #fbe6e3;
  --bubble-in: #ffffff;
  --bubble-out: #dcecff;
  --bubble-auto: #ece8fb;
  --radius: 10px;
  --topbar: 56px;
  color-scheme: light dark;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #101216;
    --surface: #181b21;
    --surface-2: #21252d;
    --text: #e8eaee;
    --muted: #9aa3b2;
    --border: #2d323c;
    --accent: #4d94ff;
    --accent-text: #0b1220;
    --accent-soft: #1b2a44;
    --ok: #6fd695;
    --ok-soft: #15301f;
    --err: #ff8f85;
    --err-soft: #3a1b18;
    --bubble-in: #21252d;
    --bubble-out: #1d3557;
    --bubble-auto: #2c2748;
  }
}

* { box-sizing: border-box; }

html, body { height: 100%; }

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

a { color: var(--accent); }

h1 { font-size: 1.5rem; margin: 0 0 12px; }
h2 { font-size: 1.1rem; margin: 0 0 12px; }

.muted { color: var(--muted); }
.lead { max-width: 70ch; margin: 0 0 20px; }

/* Top bar */
.topbar {
  position: sticky;
  top: 0;
  z-index: 5;
  display: flex;
  align-items: center;
  gap: 20px;
  height: var(--topbar);
  padding: 0 20px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
}

.brand {
  display: flex;
  align-items: center;
  gap: 8px;
  font-weight: 650;
  color: var(--text);
  text-decoration: none;
  white-space: nowrap;
}

.brand-mark {
  width: 22px;
  height: 22px;
  border-radius: 7px;
  background: var(--accent);
}

.nav { display: flex; gap: 4px; overflow-x: auto; }

.nav a {
  padding: 6px 12px;
  border-radius: 8px;
  color: var(--muted);
  text-decoration: none;
  white-space: nowrap;
}

.nav a:hover { background: var(--surface-2); color: var(--text); }
.nav a.active { background: var(--accent-soft); color: var(--accent); font-weight: 600; }

.topbar-end { margin-left: auto; display: flex; align-items: center; gap: 14px; }

.lang { display: flex; border: 1px solid var(--border); border-radius: 8px; overflow: hidden; }

.lang button {
  border: 0;
  border-radius: 0;
  padding: 4px 9px;
  background: transparent;
  color: var(--muted);
  font-size: 12px;
  font-weight: 600;
}

.lang button.active { background: var(--accent-soft); color: var(--accent); }

main { max-width: 980px; margin: 0 auto; padding: 24px 20px 48px; }

/* Cards and forms */
.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 20px;
  margin: 0 0 16px;
}

.card.narrow { max-width: 420px; margin: 48px auto; }
.card-off { opacity: 0.65; }

.stack { display: flex; flex-direction: column; gap: 14px; }
.narrow-form { max-width: 420px; }

label { display: flex; flex-direction: column; gap: 5px; font-weight: 550; }
label small, form small { font-weight: 400; color: var(--muted); }
label.short { max-width: 320px; }

label.check { flex-direction: row; align-items: center; gap: 8px; font-weight: 400; }
.checks { display: flex; flex-wrap: wrap; gap: 8px 24px; }

input, select, textarea {
  width: 100%;
  padding: 9px 11px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--surface);
  color: var(--text);
  font: inherit;
}

input[type="checkbox"] { width: 17px; height: 17px; padding: 0; accent-color: var(--accent); }
textarea { resize: vertical; }

input:focus-visible, select:focus-visible, textarea:focus-visible, button:focus-visible, a:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 1px;
}

button {
  padding: 9px 16px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--surface);
  color: var(--text);
  font: inherit;
  font-weight: 550;
  cursor: pointer;
}

button:hover { background: var(--surface-2); }
button:disabled { opacity: 0.6; cursor: default; }

button.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-text); }
button.primary:hover { filter: brightness(1.08); background: var(--accent); }

button.danger { color: var(--err); border-color: var(--err); }
button.danger:hover { background: var(--err-soft); }

.link-button { border: 0; padding: 4px 6px; background: transparent; color: var(--muted); font-weight: 400; }
.link-button:hover { background: transparent; color: var(--text); text-decoration: underline; }

.actions { display: flex; flex-wrap: wrap; gap: 12px 20px; align-items: flex-start; }
.actions form { display: flex; flex-direction: column; gap: 5px; max-width: 320px; }
.actions form button { align-self: flex-start; }

.flash { padding: 10px 14px; border-radius: 8px; margin: 0 0 16px; }
.flash-ok { background: var(--ok-soft); color: var(--ok); }
.flash-err { background: var(--err-soft); color: var(--err); }
.flash a { color: inherit; font-weight: 600; }

.facts { display: grid; grid-template-columns: max-content 1fr; gap: 8px 20px; margin: 0 0 20px; }
.facts dt { color: var(--muted); }
.facts dd { margin: 0; overflow-wrap: anywhere; }

.status { font-weight: 550; }
.status-ok { color: var(--ok); }
.status-err { color: var(--err); }

.steps { margin: 0 0 20px; padding-left: 22px; }
.steps li { margin-bottom: 6px; }

.rule-grid { display: grid; grid-template-columns: 1fr 1.3fr 1.3fr 90px; gap: 14px; align-items: start; }

.test-result { padding: 12px 14px; border-radius: 8px; background: var(--surface-2); white-space: pre-wrap; overflow-wrap: anywhere; }
.test-result.is-error { background: var(--err-soft); color: var(--err); }

/* Inbox */
.page-inbox main { max-width: none; padding: 0; display: flex; flex-direction: column; height: calc(100dvh - var(--topbar)); }
.page-inbox .flash { margin: 12px 16px 0; }

.inbox { flex: 1; min-height: 0; display: grid; grid-template-columns: 320px 1fr; }

.chat-list { overflow-y: auto; background: var(--surface); border-right: 1px solid var(--border); }

.chat-item {
  display: block;
  width: 100%;
  padding: 12px 16px;
  border: 0;
  border-bottom: 1px solid var(--border);
  border-radius: 0;
  background: transparent;
  text-align: left;
  font-weight: 400;
}

.chat-item:hover { background: var(--surface-2); }
.chat-item.active { background: var(--accent-soft); }

.chat-row { display: flex; justify-content: space-between; align-items: baseline; gap: 10px; }
.chat-name { font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.chat-time { flex: none; font-size: 12px; color: var(--muted); }
.chat-preview { flex: 1; min-width: 0; font-size: 13px; color: var(--muted); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

.badge {
  flex: none;
  min-width: 20px;
  padding: 0 6px;
  border-radius: 10px;
  background: var(--accent);
  color: var(--accent-text);
  font-size: 12px;
  font-weight: 650;
  text-align: center;
}

.chat-empty { padding: 24px 16px; color: var(--muted); }

.thread { min-width: 0; min-height: 0; display: flex; flex-direction: column; }
.thread-empty { margin: auto; color: var(--muted); padding: 24px; text-align: center; }
.thread-open { flex: 1; min-height: 0; display: flex; flex-direction: column; }
.thread-open[hidden] { display: none; }

.thread-head {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 10px 16px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
}

.thread-who { flex: 1; min-width: 0; display: flex; flex-direction: column; }
.thread-who strong, .thread-who span { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.thread-who span { font-size: 13px; }
.back { display: none; }

.pill { flex: none; padding: 5px 12px; border-radius: 999px; font-size: 13px; }
.pill.is-on { background: var(--ok-soft); border-color: transparent; color: var(--ok); }
.pill.is-paused { background: var(--surface-2); color: var(--muted); }

.messages { flex: 1; min-height: 0; overflow-y: auto; padding: 16px; display: flex; flex-direction: column; gap: 8px; }

.day { align-self: center; margin: 8px 0; padding: 2px 10px; border-radius: 999px; background: var(--surface-2); color: var(--muted); font-size: 12px; }

.msg { max-width: min(640px, 82%); display: flex; flex-direction: column; gap: 2px; }
.msg-in { align-self: flex-start; }
.msg-out { align-self: flex-end; align-items: flex-end; }

.bubble { padding: 8px 12px; border-radius: 14px; border: 1px solid var(--border); background: var(--bubble-in); white-space: pre-wrap; overflow-wrap: anywhere; }
.msg-out .bubble { background: var(--bubble-out); border-color: transparent; }
.msg-out.msg-auto .bubble { background: var(--bubble-auto); }

.msg-meta { font-size: 12px; color: var(--muted); padding: 0 4px; }

.note { align-self: center; max-width: 80%; padding: 6px 12px; border-radius: 8px; background: var(--err-soft); color: var(--err); font-size: 13px; text-align: center; overflow-wrap: anywhere; }

.reply { display: grid; grid-template-columns: 1fr auto; gap: 10px; align-items: end; padding: 12px 16px; background: var(--surface); border-top: 1px solid var(--border); }
.reply-error { grid-column: 1 / -1; color: var(--err); font-size: 13px; }

@media (max-width: 760px) {
  .topbar { gap: 10px; padding: 0 12px; }
  .brand { font-size: 0; gap: 0; }
  main { padding: 16px 12px 40px; }
  .rule-grid { grid-template-columns: 1fr; }
  .facts { grid-template-columns: 1fr; gap: 2px; }
  .facts dd { margin-bottom: 10px; }

  .inbox { grid-template-columns: 1fr; }
  .inbox .thread { display: none; }
  .inbox.has-open .chat-list { display: none; }
  .inbox.has-open .thread { display: flex; }
  .back { display: inline-block; }
}
