/* ------------------------------------------------------------------ */
/* tokens                                                              */
/* ------------------------------------------------------------------ */

:root {
  color-scheme: light dark;

  --bg: #f0f2f5;
  --panel: #ffffff;
  --panel-2: #f7f8fa;
  --chat-bg: #efeae2;
  --border: #e4e6eb;
  --text: #111b21;
  --text-dim: #667781;
  --accent: #1f8a70;
  --accent-ink: #ffffff;
  --bubble-in: #ffffff;
  --bubble-out: #d9fdd3;
  --danger: #c0392b;
  --shadow: 0 1px 2px rgb(11 20 26 / 8%), 0 2px 12px rgb(11 20 26 / 6%);
  --radius: 10px;
  --rail-w: 84px;
  --list-w: 350px;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0b141a;
    --panel: #111b21;
    --panel-2: #182229;
    --chat-bg: #0b141a;
    --border: #222d34;
    --text: #e9edef;
    --text-dim: #8696a0;
    --accent: #21c08b;
    --accent-ink: #08130f;
    --bubble-in: #202c33;
    --bubble-out: #005c4b;
    --danger: #f2777a;
    --shadow: 0 1px 2px rgb(0 0 0 / 40%), 0 2px 14px rgb(0 0 0 / 30%);
  }
}

* {
  box-sizing: border-box;
}

/*
 * The `hidden` attribute is honoured only by the UA stylesheet, and *any*
 * author `display` rule beats a UA rule regardless of specificity. Every
 * panel here sets a display mode (.app and .login are grid, .modal is grid,
 * .chat-inner is flex), so without this every one of them stays on screen
 * while marked hidden — including the settings modal, whose full-viewport
 * overlay then covers the entire app.
 *
 * `!important` so it holds wherever it sits in the file and against #id rules.
 */
[hidden] {
  display: none !important;
}

html,
body {
  height: 100%;
  margin: 0;
}

body {
  background: var(--bg);
  color: var(--text);
  font: 15px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
}

.muted {
  color: var(--text-dim);
}

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

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

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

.login-card {
  width: min(360px, 100%);
  display: flex;
  flex-direction: column;
  gap: 12px;
  padding: 32px 28px;
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 16px;
  box-shadow: var(--shadow);
  text-align: center;
}

.login-mark {
  font-size: 40px;
  line-height: 1;
}

.login-card h1 {
  margin: 0;
  font-size: 22px;
}

.login-card p {
  margin: 0 0 8px;
  font-size: 13px;
}

.login-card input,
.pair-form input {
  width: 100%;
  padding: 11px 13px;
  background: var(--panel-2);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  outline: none;
}

.login-card input:focus,
.pair-form input:focus,
#search:focus,
#composer-input:focus {
  border-color: var(--accent);
}

.login-card button,
.pair-form button,
.primary {
  padding: 11px 14px;
  background: var(--accent);
  color: var(--accent-ink);
  border: none;
  border-radius: var(--radius);
  font-weight: 600;
  cursor: pointer;
}

.login-card button:disabled {
  opacity: 0.6;
  cursor: progress;
}

.login-error {
  margin: 0;
  color: var(--danger);
  font-size: 13px;
}

/* ------------------------------------------------------------------ */
/* app shell                                                           */
/* ------------------------------------------------------------------ */

.app {
  display: grid;
  grid-template-columns: var(--rail-w) var(--list-w) 1fr;
  /*
   * `minmax(0, 1fr)`, not the default `auto`. An auto row is sized to its
   * content, so a long conversation stretched this row to the height of every
   * message at once — the message list stopped scrolling and the composer was
   * pushed thousands of pixels below the fold, unreachable. Short chats hid the
   * bug entirely. The 0 minimum is what lets the row shrink to the viewport and
   * hand the overflow to `.messages`.
   */
  grid-template-rows: minmax(0, 1fr);
  height: 100dvh;
  overflow: hidden;
}

/* rail ------------------------------------------------------------- */

.rail {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 4px;
  padding: 12px 0;
  background: var(--panel-2);
  border-right: 1px solid var(--border);
  overflow-y: auto;
}

.rail-item {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 5px;
  width: 100%;
  padding: 9px 4px;
  background: none;
  border: none;
  border-left: 3px solid transparent;
  cursor: pointer;
  position: relative;
}

.rail-item.is-active {
  border-left-color: var(--acc, var(--accent));
  background: color-mix(in srgb, var(--acc, var(--accent)) 12%, transparent);
}

.rail-avatar {
  display: grid;
  place-items: center;
  width: 42px;
  height: 42px;
  border-radius: 50%;
  background: var(--acc, var(--accent));
  color: #fff;
  font-size: 15px;
  font-weight: 700;
  box-shadow: 0 0 0 2px var(--panel-2), 0 0 0 4px color-mix(in srgb, var(--acc, var(--accent)) 45%, transparent);
}

.rail-avatar-all {
  background: linear-gradient(135deg, hsl(150 60% 45%), hsl(240 60% 55%), hsl(330 60% 55%));
  font-size: 12px;
}

.rail-avatar-gear {
  background: var(--border);
  color: var(--text-dim);
  box-shadow: none;
}

.rail-label {
  max-width: 100%;
  font-size: 10.5px;
  color: var(--text-dim);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.rail-item.is-active .rail-label {
  color: var(--text);
  font-weight: 600;
}

/* Connection state, as a dot on the avatar. */
.rail-dot {
  position: absolute;
  top: 9px;
  right: 16px;
  width: 12px;
  height: 12px;
  border-radius: 50%;
  border: 2px solid var(--panel-2);
  background: var(--text-dim);
}

.rail-dot[data-state="open"] {
  background: #22c55e;
}
.rail-dot[data-state="connecting"],
.rail-dot[data-state="starting"] {
  background: #eab308;
  animation: pulse 1.4s ease-in-out infinite;
}
.rail-dot[data-state="qr"],
.rail-dot[data-state="pairing"] {
  background: #3b82f6;
  animation: pulse 1.4s ease-in-out infinite;
}
.rail-dot[data-state="logged-out"],
.rail-dot[data-state="offline"] {
  background: #ef4444;
}

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

@media (prefers-reduced-motion: reduce) {
  .rail-dot {
    animation: none !important;
  }
}

.rail-badge {
  position: absolute;
  top: 4px;
  left: 46px;
  min-width: 18px;
  padding: 1px 5px;
  border-radius: 9px;
  background: var(--accent);
  color: var(--accent-ink);
  font-size: 11px;
  font-weight: 700;
  text-align: center;
}

.rail-settings {
  margin-top: auto;
}

/* list ------------------------------------------------------------- */

.list {
  display: flex;
  flex-direction: column;
  background: var(--panel);
  border-right: 1px solid var(--border);
  min-height: 0;
}

.list-head {
  padding: 14px 14px 10px;
  border-bottom: 1px solid var(--border);
}

.list-title {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-bottom: 10px;
}

.list-title h2 {
  margin: 0;
  font-size: 17px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.chip {
  padding: 1px 8px;
  border-radius: 999px;
  background: var(--panel-2);
  border: 1px solid var(--border);
  color: var(--text-dim);
  font-size: 11px;
  white-space: nowrap;
}

#search {
  width: 100%;
  padding: 8px 12px;
  background: var(--panel-2);
  border: 1px solid var(--border);
  border-radius: 999px;
  outline: none;
}

.list-scroll {
  flex: 1;
  overflow-y: auto;
  overscroll-behavior: contain;
}

.row {
  display: grid;
  grid-template-columns: 4px 44px 1fr auto;
  gap: 10px;
  align-items: center;
  width: 100%;
  padding: 10px 12px 10px 0;
  background: none;
  border: none;
  border-bottom: 1px solid var(--border);
  cursor: pointer;
  text-align: left;
}

.row:hover {
  background: var(--panel-2);
}

.row.is-active {
  background: color-mix(in srgb, var(--acc, var(--accent)) 14%, transparent);
}

/* The account stripe — the fastest way to see which number a chat is on. */
.row-stripe {
  align-self: stretch;
  background: var(--acc, var(--accent));
}

.row-avatar,
.head-avatar {
  position: relative;
  display: grid;
  place-items: center;
  width: 44px;
  height: 44px;
  border-radius: 50%;
  background: var(--panel-2);
  border: 1px solid var(--border);
  font-size: 17px;
  overflow: hidden;
  flex: none;
}

.head-avatar {
  width: 38px;
  height: 38px;
  font-size: 15px;
}

/* Layered over the glyph rather than replacing it, so a picture that fails to
   load leaves the placeholder in place with no reflow. */
.avatar-img {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  object-fit: cover;
  opacity: 0;
  transition: opacity 0.15s ease-in;
}

.has-photo .avatar-img {
  opacity: 1;
}

@media (prefers-reduced-motion: reduce) {
  .avatar-img {
    transition: none;
  }
}

.row-main {
  min-width: 0;
}

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

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

.row-time {
  font-size: 11.5px;
  color: var(--text-dim);
  white-space: nowrap;
}

.row-preview {
  display: flex;
  align-items: center;
  gap: 5px;
  font-size: 13px;
  color: var(--text-dim);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.row-tag {
  padding: 0 6px;
  border-radius: 999px;
  background: color-mix(in srgb, var(--acc, var(--accent)) 20%, transparent);
  color: var(--acc, var(--accent));
  font-size: 10.5px;
  font-weight: 700;
  white-space: nowrap;
}

.row-unread {
  min-width: 20px;
  padding: 1px 6px;
  border-radius: 10px;
  background: var(--accent);
  color: var(--accent-ink);
  font-size: 11.5px;
  font-weight: 700;
  text-align: center;
}

.list-empty {
  padding: 32px 20px;
  color: var(--text-dim);
  text-align: center;
  font-size: 14px;
}

/* chat ------------------------------------------------------------- */

.chat {
  display: flex;
  flex-direction: column;
  min-width: 0;
  /* Same reason as the grid row: without it this refuses to shrink below its
     content and the scroll never lands on `.messages`. */
  min-height: 0;
  background: var(--chat-bg);
}

.empty {
  margin: auto;
  padding: 40px;
  max-width: 460px;
  text-align: center;
}

.empty-mark {
  font-size: 54px;
}

.empty h2 {
  margin: 12px 0 8px;
}

.chat-inner {
  display: flex;
  flex-direction: column;
  min-height: 0;
  height: 100%;
}

.chat-head {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 10px 14px;
  background: var(--panel);
  border-bottom: 1px solid var(--border);
}

.chat-account-dot {
  width: 10px;
  height: 10px;
  border-radius: 50%;
  background: var(--acc, var(--accent));
  flex: none;
}

.chat-head-text {
  flex: 1;
  min-width: 0;
}

.chat-head-text h2 {
  margin: 0;
  font-size: 16px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.chat-head-text p {
  margin: 0;
  font-size: 12px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.icon-btn {
  display: grid;
  place-items: center;
  width: 36px;
  height: 36px;
  background: none;
  border: none;
  border-radius: 50%;
  color: var(--text-dim);
  font-size: 18px;
  cursor: pointer;
  flex: none;
}

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

.back-btn {
  display: none;
  font-size: 26px;
}

/* messages --------------------------------------------------------- */

.messages {
  flex: 1;
  min-height: 0;
  overflow-y: auto;
  overscroll-behavior: contain;
  padding: 16px clamp(12px, 6vw, 64px);
  display: flex;
  flex-direction: column;
  gap: 3px;
}

.day {
  align-self: center;
  margin: 12px 0 6px;
  padding: 4px 11px;
  border-radius: 999px;
  background: var(--panel);
  border: 1px solid var(--border);
  color: var(--text-dim);
  font-size: 11.5px;
}

.msg {
  max-width: min(72ch, 78%);
  padding: 6px 9px 5px;
  border-radius: 8px;
  background: var(--bubble-in);
  box-shadow: 0 1px 0.5px rgb(11 20 26 / 13%);
  align-self: flex-start;
  overflow-wrap: anywhere;
}

.msg.out {
  align-self: flex-end;
  background: var(--bubble-out);
}

/* First bubble of a run gets the tail corner. */
.msg.run-start {
  border-top-left-radius: 2px;
}

.msg.out.run-start {
  border-top-left-radius: 8px;
  border-top-right-radius: 2px;
}

.msg-sender {
  margin-bottom: 2px;
  font-size: 12.5px;
  font-weight: 700;
  color: var(--acc, var(--accent));
}

.msg-quote {
  margin-bottom: 4px;
  padding: 4px 8px;
  border-left: 3px solid var(--acc, var(--accent));
  border-radius: 4px;
  background: rgb(127 127 127 / 12%);
  font-size: 13px;
  color: var(--text-dim);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.msg-text {
  white-space: pre-wrap;
}

.msg-text.deleted {
  font-style: italic;
  color: var(--text-dim);
}

.msg-meta {
  display: flex;
  align-items: center;
  justify-content: flex-end;
  gap: 4px;
  margin-top: 2px;
  font-size: 11px;
  color: var(--text-dim);
  line-height: 1;
}

.tick {
  font-size: 12px;
}

.tick.read {
  color: #53bdeb;
}

.msg-media img,
.msg-media video {
  display: block;
  max-width: 100%;
  max-height: 360px;
  border-radius: 6px;
  background: rgb(127 127 127 / 12%);
}

.msg-media audio {
  width: min(280px, 60vw);
}

.msg-file {
  display: flex;
  align-items: center;
  gap: 9px;
  padding: 8px 10px;
  border-radius: 6px;
  background: rgb(127 127 127 / 12%);
  color: inherit;
  text-decoration: none;
}

.msg-file-icon {
  font-size: 22px;
}

.msg-file-name {
  font-size: 13.5px;
  font-weight: 600;
  overflow-wrap: anywhere;
}

.msg-file-size {
  font-size: 11.5px;
  color: var(--text-dim);
}

.media-dead {
  gap: 10px;
  align-items: center;
  border: 1px dashed color-mix(in srgb, var(--text-dim) 45%, transparent);
  background: none;
}

.media-dead .ghost {
  margin-left: auto;
  padding: 4px 10px;
  font-size: 12px;
}

.reactions {
  display: inline-flex;
  gap: 3px;
  margin-top: 3px;
  padding: 1px 6px;
  border-radius: 999px;
  background: rgb(127 127 127 / 14%);
  font-size: 12px;
}

.typing {
  align-self: flex-start;
  padding: 7px 12px;
  border-radius: 8px;
  background: var(--bubble-in);
  color: var(--text-dim);
  font-size: 13px;
  font-style: italic;
}

.load-more {
  align-self: center;
  margin: 4px 0 10px;
  padding: 5px 14px;
  border-radius: 999px;
  border: 1px solid var(--border);
  background: var(--panel);
  color: var(--text-dim);
  font-size: 12.5px;
  cursor: pointer;
}

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

.composer {
  display: flex;
  align-items: flex-end;
  gap: 8px;
  padding: 10px 14px;
  background: var(--panel);
  border-top: 1px solid var(--border);
}

.attach {
  cursor: pointer;
  font-size: 17px;
}

#composer-input {
  flex: 1;
  max-height: 140px;
  padding: 9px 13px;
  background: var(--panel-2);
  border: 1px solid var(--border);
  border-radius: 20px;
  outline: none;
  resize: none;
}

.send-btn {
  display: grid;
  place-items: center;
  width: 40px;
  height: 40px;
  background: var(--accent);
  color: var(--accent-ink);
  border: none;
  border-radius: 50%;
  font-size: 16px;
  cursor: pointer;
  flex: none;
}

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

.composer-note {
  padding: 7px 16px;
  background: color-mix(in srgb, var(--danger) 14%, var(--panel));
  color: var(--danger);
  font-size: 12.5px;
  border-top: 1px solid var(--border);
}

/* ------------------------------------------------------------------ */
/* settings / pairing                                                  */
/* ------------------------------------------------------------------ */

.modal {
  position: fixed;
  inset: 0;
  display: grid;
  place-items: center;
  padding: 20px;
  background: rgb(0 0 0 / 55%);
  z-index: 40;
}

.modal-card {
  display: flex;
  flex-direction: column;
  width: min(760px, 100%);
  max-height: min(88dvh, 900px);
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 14px;
  box-shadow: var(--shadow);
  overflow: hidden;
}

.modal-head,
.modal-foot {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 14px 18px;
  border-bottom: 1px solid var(--border);
}

.modal-foot {
  border-bottom: none;
  border-top: 1px solid var(--border);
}

.modal-head h2 {
  margin: 0;
  font-size: 18px;
}

.modal-body {
  flex: 1;
  min-height: 0;
  overflow-y: auto;
  padding: 16px 18px;
  display: grid;
  gap: 14px;
}

.acct {
  display: grid;
  grid-template-columns: auto 1fr auto;
  gap: 12px;
  align-items: start;
  padding: 14px;
  border: 1px solid var(--border);
  border-left: 4px solid var(--acc, var(--accent));
  border-radius: var(--radius);
  background: var(--panel-2);
}

.acct-name {
  margin: 0 0 2px;
  font-size: 15px;
  font-weight: 700;
}

.acct-meta {
  margin: 0;
  font-size: 12.5px;
  color: var(--text-dim);
}

.acct-meta code {
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
  font-size: 12px;
}

.state-pill {
  padding: 2px 9px;
  border-radius: 999px;
  font-size: 11.5px;
  font-weight: 700;
  white-space: nowrap;
  background: rgb(127 127 127 / 18%);
}

.state-pill[data-state="open"] {
  background: #22c55e22;
  color: #16a34a;
}
.state-pill[data-state="qr"],
.state-pill[data-state="pairing"] {
  background: #3b82f622;
  color: #3b82f6;
}
.state-pill[data-state="logged-out"],
.state-pill[data-state="offline"] {
  background: #ef444422;
  color: #ef4444;
}

.acct-actions {
  grid-column: 1 / -1;
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  align-items: center;
}

.ghost {
  padding: 7px 12px;
  background: none;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  color: var(--text);
  cursor: pointer;
  font-size: 13px;
}

.ghost:hover:not(:disabled) {
  border-color: var(--accent);
}

/*
 * Pairing controls are disabled whenever the worker is offline — there is
 * nothing to send the command to. Without this they still look clickable, so
 * the button appears to do nothing rather than to be unavailable.
 */
.ghost:disabled,
.pair-form button:disabled,
.pair-form input:disabled {
  opacity: 0.4;
  cursor: not-allowed;
}

.ghost.danger {
  color: var(--danger);
  border-color: color-mix(in srgb, var(--danger) 40%, var(--border));
}

.pair-form {
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
  align-items: center;
}

.pair-form input {
  width: 200px;
}

.pair-form button {
  padding: 7px 12px;
  font-size: 13px;
}

.qr-box {
  grid-column: 1 / -1;
  display: flex;
  gap: 16px;
  align-items: center;
  flex-wrap: wrap;
  padding: 14px;
  border-radius: var(--radius);
  background: var(--panel);
  border: 1px solid var(--border);
}

.qr-box img {
  width: 200px;
  height: 200px;
  border-radius: 8px;
  background: #fff;
  padding: 6px;
}

.pair-code {
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
  font-size: 30px;
  font-weight: 700;
  letter-spacing: 5px;
}

.acct-error {
  grid-column: 1 / -1;
  margin: 0;
  font-size: 12.5px;
  color: var(--danger);
  overflow-wrap: anywhere;
}

/* ------------------------------------------------------------------ */
/* toast                                                               */
/* ------------------------------------------------------------------ */

.toast {
  position: fixed;
  left: 50%;
  bottom: 26px;
  transform: translateX(-50%);
  max-width: min(520px, calc(100vw - 32px));
  padding: 10px 16px;
  background: var(--panel);
  border: 1px solid var(--border);
  border-left: 4px solid var(--danger);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  font-size: 13.5px;
  z-index: 60;
}

/* ------------------------------------------------------------------ */
/* narrow screens                                                      */
/* ------------------------------------------------------------------ */

@media (max-width: 900px) {
  :root {
    --rail-w: 68px;
    --list-w: 1fr;
  }

  .app {
    grid-template-columns: var(--rail-w) 1fr;
  }

  .chat {
    display: none;
  }

  /* One pane at a time: opening a chat swaps the list out for it. */
  .app.chat-open .list {
    display: none;
  }

  .app.chat-open .chat {
    display: flex;
  }

  .back-btn {
    display: grid;
  }
}
