/*
 * CellInbox console styles.
 *
 * System font stack only — no @font-face, no remote origin of any kind, so the
 * CSP can stay at default-src 'none' with nothing added for fonts. Dark is the
 * default because this console usually lives on a wall display next to a rack;
 * light follows the OS preference.
 *
 * Layout is three panes on a desktop and one pane on a phone, switched by
 * [data-view] on .app rather than by JS-driven display toggles, so the
 * responsive behaviour lives entirely here.
 */

:root {
  color-scheme: dark light;

  --bg: #0d1117;
  --bg-raised: #151b23;
  --bg-inset: #10151c;
  --bg-hover: #1c232d;
  --border: #262e3a;
  --border-soft: #1d242e;

  --text: #e6edf3;
  --text-dim: #9aa7b4;
  --text-faint: #6b7885;

  --accent: #3b82f6;
  --accent-text: #ffffff;
  --accent-soft: #16233a;

  --in-bg: #1b232e;
  --out-bg: #1d4ed8;
  --out-text: #f4f8ff;

  --ok: #3fb950;
  --warn: #d29922;
  --crit: #f85149;
  --off: #8b949e;

  --radius: 10px;
  --radius-sm: 6px;
  --pane-gap: 1px;

  --font: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial,
    "Noto Sans", sans-serif, "Apple Color Emoji", "Segoe UI Emoji";
  --mono: ui-monospace, SFMono-Regular, "SF Mono", Menlo, Consolas, "Liberation Mono", monospace;
}

@media (prefers-color-scheme: light) {
  :root {
    --bg: #f4f6f8;
    --bg-raised: #ffffff;
    --bg-inset: #eef1f4;
    --bg-hover: #e9edf1;
    --border: #d5dbe1;
    --border-soft: #e3e8ec;

    --text: #101720;
    --text-dim: #55606b;
    --text-faint: #7d8892;

    --accent: #1d63d8;
    --accent-soft: #e4ecfb;

    --in-bg: #e8ecf1;
    --out-bg: #1d63d8;
    --out-text: #ffffff;

    --ok: #1a7f37;
    --warn: #9a6700;
    --crit: #cf222e;
    --off: #6e7781;
  }
}

*,
*::before,
*::after {
  box-sizing: border-box;
}

html,
body {
  height: 100%;
}

body {
  margin: 0;
  font-family: var(--font);
  font-size: 14px;
  line-height: 1.45;
  color: var(--text);
  background: var(--bg);
  -webkit-text-size-adjust: 100%;
}

h1,
h2,
h3 {
  margin: 0;
  font-size: inherit;
  font-weight: 600;
}

p {
  margin: 0;
}

button,
input,
textarea {
  font: inherit;
  color: inherit;
}

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

:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
  border-radius: var(--radius-sm);
}

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

/* ------------------------------------------------------------------ buttons */

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  padding: 8px 14px;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  background: var(--bg-raised);
  cursor: pointer;
  font-weight: 500;
  white-space: nowrap;
}

.btn:hover:not(:disabled) {
  background: var(--bg-hover);
}

.btn:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}

.btn-primary {
  background: var(--accent);
  border-color: transparent;
  color: var(--accent-text);
}

.btn-primary:hover:not(:disabled) {
  filter: brightness(1.1);
  background: var(--accent);
}

.btn-quiet {
  background: transparent;
}

.btn-block {
  width: 100%;
}

.btn-small {
  padding: 5px 10px;
  font-size: 12px;
}

.icon-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 32px;
  height: 32px;
  flex: none;
  border: 0;
  border-radius: var(--radius-sm);
  background: transparent;
  color: var(--text-dim);
  cursor: pointer;
  font-size: 16px;
}

.icon-btn:hover {
  background: var(--bg-hover);
  color: var(--text);
}

/* ------------------------------------------------------------- login screen */

.page-login {
  display: grid;
  place-items: center;
  min-height: 100dvh;
  padding: 24px;
}

.auth {
  width: 100%;
  max-width: 380px;
}

.auth-card {
  display: flex;
  flex-direction: column;
  gap: 14px;
  padding: 26px;
  border: 1px solid var(--border);
  border-radius: 14px;
  background: var(--bg-raised);
}

.auth-brand {
  display: flex;
  align-items: center;
  gap: 12px;
  margin-bottom: 4px;
}

.auth-brand h1 {
  font-size: 19px;
  letter-spacing: -0.01em;
}

.auth-sub {
  color: var(--text-dim);
  font-size: 13px;
}

.auth-foot {
  margin-top: 2px;
  color: var(--text-faint);
  font-size: 12px;
  line-height: 1.4;
}

.field {
  display: flex;
  flex-direction: column;
  gap: 5px;
}

.field-label {
  display: flex;
  align-items: baseline;
  gap: 6px;
  font-size: 12px;
  font-weight: 600;
  color: var(--text-dim);
}

.field-hint {
  font-weight: 400;
  color: var(--text-faint);
}

.field-input {
  padding: 9px 11px;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  background: var(--bg-inset);
  color: var(--text);
}

.field-input:focus {
  border-color: var(--accent);
  outline: none;
  box-shadow: 0 0 0 3px var(--accent-soft);
}

.field-code {
  font-family: var(--mono);
  letter-spacing: 0.16em;
}

.form-error,
.form-notice {
  padding: 9px 11px;
  border-radius: var(--radius-sm);
  font-size: 13px;
  border: 1px solid transparent;
}

.form-error {
  border-color: var(--crit);
  background: color-mix(in srgb, var(--crit) 12%, transparent);
  color: var(--crit);
}

.form-notice {
  border-color: var(--border);
  background: var(--bg-inset);
  color: var(--text-dim);
}

/* --------------------------------------------------------------- app layout */

.page-app {
  overflow: hidden;
}

.app {
  display: grid;
  grid-template-rows: auto minmax(0, 1fr);
  height: 100dvh;
}

.topbar {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 0 12px;
  height: 48px;
  border-bottom: 1px solid var(--border);
  background: var(--bg-raised);
}

.topbar-title {
  font-size: 14px;
  letter-spacing: -0.01em;
}

.topbar-logo {
  display: block;
}

.nav-back {
  display: none;
  font-size: 22px;
  line-height: 1;
}

.topbar-status {
  display: flex;
  align-items: center;
  gap: 6px;
  margin-left: auto;
  padding: 3px 9px;
  border-radius: 999px;
  background: var(--bg-inset);
  font-size: 12px;
  color: var(--text-dim);
}

.topbar-status .dot {
  width: 7px;
  height: 7px;
  border-radius: 50%;
  background: var(--off);
}

.topbar-status[data-state="live"] .dot {
  background: var(--ok);
}

.topbar-status[data-state="connecting"] .dot,
.topbar-status[data-state="reconnecting"] .dot {
  background: var(--warn);
  animation: pulse 1.4s ease-in-out infinite;
}

.topbar-status[data-state="offline"] .dot {
  background: var(--crit);
}

@keyframes pulse {
  50% {
    opacity: 0.25;
  }
}

/* A long-lived tab must not burn a core on a pulsing dot for a user who asked
   the OS to stop animating things. */
@media (prefers-reduced-motion: reduce) {
  * {
    animation-duration: 0.001ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: 0.001ms !important;
  }
}

.menu {
  position: relative;
  flex: none;
}

.menu-summary {
  list-style: none;
  cursor: pointer;
}

.menu-summary::-webkit-details-marker {
  display: none;
}

.avatar {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 28px;
  height: 28px;
  flex: none;
  border-radius: 50%;
  background: var(--accent-soft);
  color: var(--accent);
  font-size: 12px;
  font-weight: 600;
}

.menu-body {
  position: absolute;
  top: calc(100% + 8px);
  right: 0;
  z-index: 20;
  width: 200px;
  padding: 10px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--bg-raised);
  box-shadow: 0 12px 30px rgb(0 0 0 / 0.32);
}

.menu-user {
  display: flex;
  flex-direction: column;
  gap: 2px;
  margin-bottom: 8px;
  font-weight: 600;
  word-break: break-word;
}

.menu-role {
  font-weight: 400;
  font-size: 12px;
  color: var(--text-dim);
}

.panes {
  display: grid;
  grid-template-columns: minmax(240px, 320px) minmax(0, 1fr) minmax(240px, 300px);
  gap: var(--pane-gap);
  background: var(--border-soft);
  min-height: 0;
}

.pane {
  display: flex;
  flex-direction: column;
  min-height: 0;
  min-width: 0;
  background: var(--bg);
}

.pane-head {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 10px 12px;
  border-bottom: 1px solid var(--border-soft);
  flex: none;
}

.pane-title {
  font-size: 13px;
  color: var(--text-dim);
  text-transform: uppercase;
  letter-spacing: 0.06em;
}

.empty {
  padding: 22px 16px;
  color: var(--text-faint);
  font-size: 13px;
  text-align: center;
}

/* ---------------------------------------------------------- the thread list */

.search {
  width: 100%;
  padding: 7px 10px;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  background: var(--bg-inset);
}

.search:focus {
  border-color: var(--accent);
  outline: none;
}

.thread-list {
  margin: 0;
  padding: 0;
  list-style: none;
  overflow-y: auto;
  flex: 1;
  overscroll-behavior: contain;
}

.thread-row {
  display: flex;
  gap: 10px;
  padding: 10px 12px;
  border-bottom: 1px solid var(--border-soft);
  cursor: pointer;
}

.thread-row:hover {
  background: var(--bg-hover);
}

.thread-row.is-selected {
  background: var(--accent-soft);
  box-shadow: inset 2px 0 0 var(--accent);
}

.thread-row-body {
  flex: 1;
  min-width: 0;
}

.thread-row-top,
.thread-row-bottom {
  display: flex;
  align-items: baseline;
  gap: 8px;
}

.thread-row-name {
  flex: 1;
  min-width: 0;
  font-weight: 500;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.thread-row.is-unread .thread-row-name {
  font-weight: 700;
}

.thread-row-time {
  flex: none;
  font-size: 11px;
  color: var(--text-faint);
  font-variant-numeric: tabular-nums;
}

.thread-row-preview {
  flex: 1;
  min-width: 0;
  font-size: 13px;
  color: var(--text-dim);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.thread-row-preview.is-call {
  font-style: italic;
}

.badge {
  flex: none;
  min-width: 18px;
  padding: 0 5px;
  border-radius: 999px;
  background: var(--accent);
  color: var(--accent-text);
  font-size: 11px;
  font-weight: 700;
  text-align: center;
  line-height: 18px;
}

/* ---------------------------------------------------------- the thread view */

.thread-head {
  justify-content: space-between;
  border-bottom: 1px solid var(--border);
}

.thread-title {
  font-size: 15px;
}

.thread-sub {
  font-size: 12px;
  color: var(--text-dim);
}

.messages {
  flex: 1;
  min-height: 0;
  overflow-y: auto;
  overscroll-behavior: contain;
  padding: 14px 16px 6px;
  display: flex;
  flex-direction: column;
  gap: 3px;
}

.day-sep {
  display: flex;
  justify-content: center;
  margin: 14px 0 8px;
}

.day-sep span {
  padding: 2px 10px;
  border-radius: 999px;
  background: var(--bg-inset);
  color: var(--text-faint);
  font-size: 11px;
  font-weight: 600;
}

.msg {
  display: flex;
  flex-direction: column;
  max-width: min(72ch, 76%);
  margin-bottom: 6px;
}

.msg.is-in {
  align-self: flex-start;
  align-items: flex-start;
}

.msg.is-out {
  align-self: flex-end;
  align-items: flex-end;
}

.msg.is-pending {
  opacity: 0.62;
}

.bubble {
  padding: 7px 11px;
  border-radius: 14px;
  background: var(--in-bg);
  /* Message bodies contain pasted logs, long URLs and no whitespace at all;
     both properties are needed or a single long token blows out the column. */
  overflow-wrap: anywhere;
  word-break: break-word;
}

.msg.is-in .bubble {
  border-bottom-left-radius: 4px;
}

.msg.is-out .bubble {
  border-bottom-right-radius: 4px;
  background: var(--out-bg);
  color: var(--out-text);
}

.bubble-text {
  /* pre-wrap keeps the sender's own line breaks, which carry meaning in an
     address or a list, without letting their leading spaces collapse. */
  white-space: pre-wrap;
}

.bubble-text.is-empty {
  color: var(--text-faint);
  font-style: italic;
}

.bubble-meta {
  display: flex;
  align-items: center;
  gap: 6px;
  margin-top: 2px;
  padding: 0 3px;
  font-size: 11px;
  color: var(--text-faint);
  font-variant-numeric: tabular-nums;
}

.chip {
  padding: 0 5px;
  border-radius: 4px;
  background: var(--bg-inset);
  font-size: 10px;
  font-weight: 600;
  letter-spacing: 0.04em;
}

.delivery.is-failed {
  color: var(--crit);
  font-weight: 600;
}

.delivery.is-delivered {
  color: var(--ok);
}

.bubble-error {
  margin-top: 3px;
  padding: 0 3px;
  font-size: 11px;
  color: var(--crit);
  overflow-wrap: anywhere;
}

/* ------------------------------------------------------------- attachments */

.attachments {
  display: flex;
  flex-wrap: wrap;
  gap: 5px;
  margin-bottom: 4px;
}

.attachment {
  display: block;
  border-radius: 8px;
  overflow: hidden;
  text-decoration: none;
  color: inherit;
}

.attachment-image {
  display: block;
  max-width: 260px;
  max-height: 320px;
  width: auto;
  height: auto;
  background: var(--bg-inset);
}

.attachment-file {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 7px 9px;
  border: 1px solid var(--border);
  background: var(--bg-inset);
  max-width: 260px;
}

.attachment-icon {
  flex: none;
  font-size: 15px;
  color: var(--text-dim);
}

.attachment-body {
  min-width: 0;
  display: flex;
  flex-direction: column;
}

.attachment-name {
  font-size: 12px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.attachment-meta {
  font-size: 11px;
  color: var(--text-faint);
}

/* -------------------------------------------------------------- call events */

.call-row {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  align-self: center;
  max-width: 100%;
  margin: 5px 0;
  padding: 5px 12px;
  border: 1px solid var(--border-soft);
  border-radius: 999px;
  background: var(--bg-inset);
  font-size: 12px;
  color: var(--text-dim);
}

.call-row.is-missed {
  border-color: color-mix(in srgb, var(--crit) 45%, transparent);
  color: var(--crit);
}

.call-label {
  font-weight: 600;
}

.call-detail {
  color: var(--text-faint);
  font-variant-numeric: tabular-nums;
}

.call-row.is-missed .call-detail {
  color: inherit;
  opacity: 0.85;
}

/* ---------------------------------------------------------------- composer */

.composer {
  flex: none;
  padding: 9px 12px calc(9px + env(safe-area-inset-bottom));
  border-top: 1px solid var(--border);
  background: var(--bg-raised);
}

.composer-row {
  display: flex;
  align-items: flex-end;
  gap: 8px;
}

.composer-input {
  flex: 1;
  min-width: 0;
  padding: 8px 11px;
  border: 1px solid var(--border);
  border-radius: 18px;
  background: var(--bg-inset);
  resize: none;
  /* 16px stops iOS Safari zooming the viewport when the field takes focus. */
  font-size: 16px;
  line-height: 1.35;
  max-height: 160px;
}

@media (pointer: fine) {
  .composer-input {
    font-size: 14px;
  }
}

.composer-input:focus {
  border-color: var(--accent);
  outline: none;
}

.attach-btn {
  position: relative;
  overflow: hidden;
  font-size: 19px;
}

.attach-btn input[type="file"] {
  position: absolute;
  inset: 0;
  opacity: 0;
  cursor: pointer;
}

.send-btn {
  flex: none;
  border-radius: 18px;
}

.composer-attachments {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  margin-bottom: 8px;
}

.staged {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 4px 6px 4px 9px;
  border: 1px solid var(--border);
  border-radius: 999px;
  background: var(--bg-inset);
  font-size: 12px;
  max-width: 220px;
}

.staged-name {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.staged-size {
  flex: none;
  color: var(--text-faint);
}

.staged-remove {
  flex: none;
  width: 18px;
  height: 18px;
  border: 0;
  border-radius: 50%;
  background: var(--bg-hover);
  color: var(--text-dim);
  cursor: pointer;
  line-height: 1;
}

.composer-error {
  margin-top: 7px;
  font-size: 12px;
  color: var(--crit);
}

/* ------------------------------------------------------------ fleet panel */

.pane-fleet .pane-head {
  justify-content: space-between;
}

.fleet-verdict {
  padding: 2px 9px;
  border-radius: 999px;
  font-size: 11px;
  font-weight: 700;
  background: var(--bg-inset);
  color: var(--text-dim);
}

.fleet-verdict[data-status="ok"] {
  background: color-mix(in srgb, var(--ok) 16%, transparent);
  color: var(--ok);
}

.fleet-verdict[data-status="degraded"] {
  background: color-mix(in srgb, var(--warn) 16%, transparent);
  color: var(--warn);
}

.fleet-verdict[data-status="critical"],
.fleet-verdict[data-status="offline"] {
  background: color-mix(in srgb, var(--crit) 16%, transparent);
  color: var(--crit);
}

.fleet-list {
  flex: 1;
  min-height: 0;
  overflow-y: auto;
  padding: 10px;
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.device {
  padding: 11px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--bg-raised);
}

.device-head {
  display: flex;
  align-items: center;
  gap: 8px;
}

.device-dot {
  width: 8px;
  height: 8px;
  flex: none;
  border-radius: 50%;
  background: var(--off);
}

.device[data-status="ok"] .device-dot {
  background: var(--ok);
}

.device[data-status="degraded"] .device-dot {
  background: var(--warn);
}

.device[data-status="critical"] .device-dot,
.device[data-status="offline"] .device-dot {
  background: var(--crit);
}

.device-ident {
  flex: 1;
  min-width: 0;
}

.device-id {
  font-family: var(--mono);
  font-size: 12px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.device-number {
  font-size: 12px;
  color: var(--text-dim);
}

.device-status {
  flex: none;
  font-size: 11px;
  font-weight: 600;
  color: var(--text-dim);
}

.device[data-status="offline"] .device-status,
.device[data-status="critical"] .device-status {
  color: var(--crit);
}

.device-stats {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 7px 10px;
  margin: 10px 0 0;
}

.stat dt {
  font-size: 10px;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: var(--text-faint);
}

.stat dd {
  display: flex;
  align-items: center;
  gap: 5px;
  margin: 1px 0 0;
  font-size: 13px;
  font-variant-numeric: tabular-nums;
}

.stat-note {
  font-size: 11px;
  color: var(--text-faint);
}

.signal .bar {
  fill: var(--text-faint);
  opacity: 0.28;
}

.signal .bar.is-on {
  fill: var(--ok);
  opacity: 1;
}

.device-warn {
  margin-top: 9px;
  font-size: 12px;
  color: var(--warn);
}

.device-reasons {
  margin: 8px 0 0;
  padding-left: 16px;
  font-size: 12px;
  color: var(--text-dim);
}

.device-actions {
  margin-top: 10px;
}

/* -------------------------------------------------------------------- toasts */

.toasts {
  position: fixed;
  right: 14px;
  bottom: calc(14px + env(safe-area-inset-bottom));
  z-index: 40;
  display: flex;
  flex-direction: column;
  gap: 7px;
  max-width: min(340px, calc(100vw - 28px));
  pointer-events: none;
}

.toast {
  padding: 9px 12px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--bg-raised);
  box-shadow: 0 8px 22px rgb(0 0 0 / 0.3);
  font-size: 13px;
}

.toast.is-error {
  border-color: var(--crit);
  color: var(--crit);
}

.noscript {
  padding: 16px;
  text-align: center;
  color: var(--text-dim);
}

/* ---------------------------------------------------------------- responsive
 *
 * Below the breakpoint the three panes stack into one and [data-view] on .app
 * decides which is on screen. Doing it with CSS rather than by toggling
 * `hidden` in JS means a resize from phone to desktop width restores all three
 * panes without any script running — relevant here because the tab is expected
 * to survive rotations and window changes for days.
 */

@media (max-width: 860px) {
  .panes {
    grid-template-columns: 1fr;
  }

  .pane {
    display: none;
  }

  .app[data-view="threads"] .pane-threads,
  .app[data-view="thread"] .pane-thread,
  .app[data-view="fleet"] .pane-fleet {
    display: flex;
  }

  .app[data-view="thread"] .nav-back {
    display: inline-flex;
  }

  .msg {
    max-width: 88%;
  }

  .attachment-image {
    max-width: min(70vw, 260px);
  }

  .topbar-status .conn-label {
    /* The dot carries the state on a narrow screen; the word costs the title
       its room. Kept in the DOM so it is still announced to a screen reader. */
    position: absolute;
    width: 1px;
    height: 1px;
    overflow: hidden;
    clip: rect(0 0 0 0);
    white-space: nowrap;
  }
}

@media (min-width: 861px) {
  /* The fleet toggle only makes sense in the stacked layout. */
  [data-toggle-fleet],
  .nav-back {
    display: none;
  }
}
