:root {
  color-scheme: light;
  --bg: #edf1f4;
  --surface: #ffffff;
  --surface-soft: #f5f7f9;
  --surface-active: #e8f2f8;
  --text: #18202a;
  --muted: #66717f;
  --border: #d5dce3;
  --accent: #1769aa;
  --accent-hover: #0f578e;
  --brand: #175c46;
  --incoming: #ffffff;
  --outgoing: #dceef9;
  --success: #18794e;
  --success-bg: #e7f5ee;
  --warning: #8a5a00;
  --warning-bg: #fff4d6;
  --danger: #b42318;
  --danger-bg: #feeceb;
  --focus: #6bb6ff;
  font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
}

* {
  box-sizing: border-box;
}

[hidden] {
  display: none !important;
}

html,
body {
  min-height: 100%;
}

body {
  margin: 0;
  overflow: hidden;
  background: var(--bg);
  color: var(--text);
  font-size: 15px;
  line-height: 1.5;
}

button,
input,
textarea {
  font: inherit;
  letter-spacing: 0;
}

button,
.button-link {
  min-height: 40px;
  border: 1px solid transparent;
  border-radius: 6px;
  padding: 8px 15px;
  cursor: pointer;
  font-weight: 650;
  text-decoration: none;
  transition: background-color 120ms ease, border-color 120ms ease, color 120ms ease;
}

button:disabled {
  cursor: wait;
  opacity: 0.62;
}

button.primary,
.button-link.primary {
  background: var(--accent);
  color: #ffffff;
}

button.primary:hover,
.button-link.primary:hover {
  background: var(--accent-hover);
}

button.secondary {
  border-color: var(--border);
  background: var(--surface);
  color: var(--text);
}

button.secondary:hover,
button.quiet:hover {
  background: var(--surface-soft);
}

button.quiet {
  background: transparent;
  color: var(--muted);
}

button.danger {
  color: var(--danger);
}

input,
textarea {
  width: 100%;
  border: 1px solid #b8c2cc;
  border-radius: 6px;
  background: #ffffff;
  color: var(--text);
  outline: none;
}

input {
  min-height: 44px;
  padding: 10px 12px;
}

textarea {
  padding: 10px 12px;
  resize: none;
}

input:focus,
textarea:focus,
button:focus-visible,
a:focus-visible {
  border-color: var(--accent);
  box-shadow: 0 0 0 3px color-mix(in srgb, var(--focus) 45%, transparent);
  outline: none;
}

label {
  display: block;
  margin: 16px 0 6px;
  font-weight: 650;
}

h1,
h2,
p {
  margin-top: 0;
}

h1 {
  margin-bottom: 0;
  font-size: 19px;
  line-height: 1.25;
}

h2 {
  margin-bottom: 2px;
  font-size: 17px;
  line-height: 1.35;
}

.muted {
  margin-bottom: 0;
  color: var(--muted);
}

.sr-only {
  position: absolute;
  width: 1px;
  height: 1px;
  overflow: hidden;
  margin: -1px;
  clip: rect(0, 0, 0, 0);
  white-space: nowrap;
}

.brand-mark {
  display: grid;
  width: 58px;
  height: 58px;
  place-items: center;
  border-radius: 8px;
  background: var(--brand);
  color: #ffffff;
  font-size: 16px;
  font-weight: 800;
}

.brand-mark.small {
  width: 42px;
  height: 42px;
  font-size: 12px;
}

.login-page {
  display: grid;
  min-height: 100vh;
  overflow: auto;
  place-items: center;
  padding: 24px;
  background: var(--bg);
}

.login-shell {
  width: min(100%, 390px);
  border-top: 4px solid var(--brand);
  background: var(--surface);
  padding: 34px;
  box-shadow: 0 12px 32px rgb(33 43 54 / 12%);
}

.login-shell .brand-mark {
  margin-bottom: 22px;
}

.login-shell h1 {
  margin-bottom: 3px;
  font-size: 24px;
}

.login-form button {
  width: 100%;
  margin-top: 22px;
}

.app-header {
  display: flex;
  min-height: 68px;
  align-items: center;
  justify-content: space-between;
  gap: 20px;
  border-bottom: 1px solid var(--border);
  background: var(--surface);
  padding: 12px 20px;
}

.brand-row,
.header-actions,
.row-actions,
.dialog-actions,
.copy-row,
.composer-actions,
.contact-identity {
  display: flex;
  align-items: center;
}

.brand-row {
  gap: 12px;
}

.brand-row p {
  margin-bottom: 0;
  color: var(--muted);
  font-size: 13px;
}

.header-actions,
.row-actions {
  gap: 7px;
}

.header-actions form {
  margin: 0;
}

.status-badge {
  display: inline-flex;
  min-height: 30px;
  align-items: center;
  border-radius: 999px;
  padding: 4px 10px;
  font-size: 13px;
  font-weight: 700;
  white-space: nowrap;
}

.status-ok {
  background: var(--success-bg);
  color: var(--success);
}

.status-warn {
  background: var(--warning-bg);
  color: var(--warning);
}

.status-error {
  background: var(--danger-bg);
  color: var(--danger);
}

.chat-workspace {
  display: grid;
  height: calc(100vh - 69px);
  grid-template-columns: 340px minmax(0, 1fr);
}

.conversation-pane {
  display: flex;
  min-width: 0;
  flex-direction: column;
  border-right: 1px solid var(--border);
  background: var(--surface);
  padding: 20px 14px 14px;
}

.pane-heading,
.chat-heading,
.dialog-heading {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 16px;
}

.pane-heading {
  min-height: 48px;
  padding: 0 4px;
}

.pane-heading .primary {
  min-height: 36px;
  padding: 6px 12px;
}

.search-label {
  margin: 14px 0 6px;
  color: var(--muted);
  font-size: 12px;
}

.conversation-list {
  min-height: 0;
  overflow-y: auto;
  margin-top: 10px;
}

.conversation-item {
  display: grid;
  width: 100%;
  min-height: 72px;
  grid-template-columns: 44px minmax(0, 1fr) auto;
  align-items: center;
  gap: 10px;
  border: 0;
  border-radius: 6px;
  background: transparent;
  padding: 9px 10px;
  color: var(--text);
  text-align: left;
}

.conversation-item:hover {
  background: var(--surface-soft);
}

.conversation-item.active {
  background: var(--surface-active);
}

.conversation-avatar,
.contact-avatar {
  display: grid;
  place-items: center;
  border-radius: 50%;
  background: #e5ece9;
  color: var(--brand);
  font-weight: 800;
}

.conversation-avatar {
  width: 44px;
  height: 44px;
}

.contact-avatar {
  width: 42px;
  height: 42px;
}

.conversation-copy {
  min-width: 0;
}

.conversation-phone,
.conversation-preview {
  overflow: hidden;
  margin: 0;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.conversation-phone {
  font-weight: 720;
}

.conversation-preview {
  color: var(--muted);
  font-size: 13px;
}

.conversation-meta {
  display: flex;
  min-width: 42px;
  align-items: flex-end;
  align-self: stretch;
  flex-direction: column;
  justify-content: center;
  gap: 5px;
  color: var(--muted);
  font-size: 11px;
}

.unread-badge {
  display: grid;
  min-width: 22px;
  height: 22px;
  place-items: center;
  border-radius: 11px;
  background: var(--accent);
  color: #ffffff;
  font-size: 11px;
  font-weight: 800;
}

.conversation-empty {
  display: grid;
  min-height: 180px;
  place-items: center;
  color: var(--muted);
}

.chat-pane {
  position: relative;
  min-width: 0;
  background: #e9eef1;
}

.chat-empty {
  display: grid;
  height: 100%;
  place-content: center;
  justify-items: center;
  padding: 24px;
  text-align: center;
}

.chat-empty .empty-symbol {
  display: grid;
  width: 74px;
  height: 74px;
  place-items: center;
  margin-bottom: 18px;
  border-radius: 50%;
  background: #dce7e3;
  color: var(--brand);
  font-weight: 800;
}

.chat-empty p {
  margin-bottom: 20px;
  color: var(--muted);
}

.chat-active {
  display: grid;
  height: 100%;
  grid-template-rows: 68px minmax(0, 1fr) auto;
}

.chat-heading {
  align-items: center;
  border-bottom: 1px solid var(--border);
  background: var(--surface);
  padding: 10px 18px;
}

.contact-identity {
  min-width: 0;
  gap: 11px;
}

.contact-identity h2 {
  overflow: hidden;
  max-width: min(52vw, 520px);
  text-overflow: ellipsis;
  white-space: nowrap;
}

.contact-identity p {
  font-size: 12px;
}

.mobile-back {
  display: none;
}

.message-list {
  min-height: 0;
  overflow-y: auto;
  padding: 24px max(20px, calc((100% - 820px) / 2));
  scroll-behavior: smooth;
}

.message-row {
  display: flex;
  margin-bottom: 10px;
}

.message-row.outgoing {
  justify-content: flex-end;
}

.message-bubble {
  max-width: min(72%, 620px);
  border: 1px solid #d1d9df;
  border-radius: 8px;
  background: var(--incoming);
  padding: 9px 11px 7px;
  box-shadow: 0 1px 2px rgb(23 35 48 / 6%);
}

.message-row.outgoing .message-bubble {
  border-color: #bcd7e8;
  background: var(--outgoing);
}

.message-text {
  overflow-wrap: anywhere;
  margin: 0;
  white-space: pre-wrap;
}

.message-text.placeholder {
  color: var(--muted);
  font-style: italic;
}

.message-meta {
  display: flex;
  align-items: center;
  justify-content: flex-end;
  gap: 7px;
  margin-top: 4px;
  color: var(--muted);
  font-size: 11px;
}

.message-status.failed,
.message-status.cancelled {
  color: var(--danger);
}

.message-status.delivered {
  color: var(--success);
}

.message-empty {
  position: absolute;
  inset: 68px 0 108px;
  display: grid;
  place-items: center;
  color: var(--muted);
  pointer-events: none;
}

.composer {
  border-top: 1px solid var(--border);
  background: var(--surface);
  padding: 12px max(18px, calc((100% - 820px) / 2));
}

.composer textarea {
  min-height: 44px;
  max-height: 140px;
}

.composer-actions {
  min-height: 44px;
  justify-content: space-between;
  gap: 16px;
  padding-top: 7px;
}

.composer-state {
  color: var(--muted);
  font-size: 12px;
  font-variant-numeric: tabular-nums;
}

.composer-actions button {
  min-width: 92px;
}

.inline-error {
  display: block;
  min-height: 21px;
  margin-top: 6px;
  color: var(--danger);
}

.alert {
  margin-top: 16px;
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 10px 12px;
}

.alert-error {
  border-color: #efaaa5;
  background: var(--danger-bg);
  color: var(--danger);
}

.alert-success {
  border-color: #9ad2b8;
  background: var(--success-bg);
  color: var(--success);
}

dialog {
  width: min(92vw, 570px);
  max-height: 90vh;
  overflow: auto;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--surface);
  padding: 24px;
  box-shadow: 0 24px 64px rgb(18 29 40 / 28%);
}

dialog::backdrop {
  background: rgb(16 24 32 / 55%);
}

.compact-dialog {
  width: min(92vw, 450px);
}

.events-dialog {
  width: min(94vw, 920px);
}

.dialog-actions {
  justify-content: flex-end;
  gap: 10px;
  margin-top: 22px;
}

.copy-row {
  gap: 8px;
}

.copy-row input {
  min-width: 0;
  font-family: ui-monospace, SFMono-Regular, Consolas, monospace;
  font-size: 12px;
}

.event-list {
  max-height: 62vh;
  overflow-y: auto;
  margin-top: 18px;
  border: 1px solid var(--border);
  border-radius: 6px;
}

.event-item {
  display: grid;
  grid-template-columns: 150px 125px minmax(0, 1fr);
  gap: 12px;
  border-bottom: 1px solid var(--border);
  padding: 12px;
}

.event-item:last-child {
  border-bottom: 0;
}

.event-time,
.event-type {
  color: var(--muted);
  font-size: 12px;
}

.event-type {
  font-family: ui-monospace, SFMono-Regular, Consolas, monospace;
  font-weight: 700;
}

.event-copy p {
  margin-bottom: 4px;
  white-space: pre-wrap;
}

details summary {
  color: var(--accent);
  cursor: pointer;
  font-size: 12px;
}

pre {
  max-height: 260px;
  overflow: auto;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: var(--surface-soft);
  padding: 10px;
  color: #35404c;
  font-family: ui-monospace, SFMono-Regular, Consolas, monospace;
  font-size: 12px;
  white-space: pre-wrap;
}

.dialog-empty {
  display: grid;
  min-height: 180px;
  place-items: center;
  color: var(--muted);
}

.toast {
  position: fixed;
  right: 22px;
  bottom: 22px;
  max-width: min(420px, calc(100vw - 44px));
  border: 1px solid #9ad2b8;
  border-radius: 6px;
  background: #123e30;
  color: #ffffff;
  padding: 12px 16px;
  box-shadow: 0 12px 28px rgb(18 29 40 / 22%);
  z-index: 10;
}

@media (max-width: 760px) {
  body {
    overflow: hidden;
  }

  .app-header {
    min-height: 62px;
    padding: 10px 12px;
  }

  .brand-mark.small {
    width: 38px;
    height: 38px;
  }

  .brand-row p,
  .status-badge {
    display: none;
  }

  .app-header h1 {
    font-size: 16px;
  }

  .header-actions {
    gap: 1px;
  }

  .header-actions button {
    padding-right: 9px;
    padding-left: 9px;
  }

  .chat-workspace {
    display: block;
    height: calc(100vh - 63px);
  }

  .conversation-pane {
    height: 100%;
    border-right: 0;
  }

  .chat-pane {
    display: none;
    height: 100%;
  }

  body.chat-open .conversation-pane {
    display: none;
  }

  body.chat-open .chat-pane {
    display: block;
  }

  .mobile-back {
    display: inline-flex;
    padding-right: 9px;
    padding-left: 9px;
  }

  .chat-heading {
    gap: 8px;
    padding: 8px 10px;
  }

  .contact-avatar {
    display: none;
  }

  .contact-identity {
    flex: 1;
  }

  .contact-identity h2 {
    max-width: 45vw;
    font-size: 15px;
  }

  .message-list {
    padding: 16px 12px;
  }

  .message-bubble {
    max-width: 84%;
  }

  .composer {
    padding: 10px 12px;
  }

  .events-dialog {
    width: 96vw;
    padding: 16px;
  }

  .event-item {
    grid-template-columns: 1fr;
    gap: 4px;
  }
}

@media (max-width: 440px) {
  .login-shell {
    padding: 26px 22px;
  }

  .dialog-actions {
    align-items: stretch;
    flex-direction: column-reverse;
  }

  .dialog-actions button {
    width: 100%;
  }

  .row-actions {
    flex-wrap: wrap;
    justify-content: flex-end;
  }
}
