/*
 * Styled to sit comfortably next to iOS's own Messages and Phone apps: system font,
 * large titles, inset separators, bubble tails, grouped settings, safe-area padding for the
 * notch and home indicator, and both colour schemes — a Home Screen web app follows the
 * device theme and can't ask which one you wanted.
 */

:root {
  --bg: #ffffff;
  --bg-grouped: #f2f2f7;
  --bg-elevated: #ffffff;
  --fill: rgba(118, 118, 128, 0.12);
  --fill-strong: rgba(118, 118, 128, 0.2);
  --text: #000000;
  --text-dim: #8a8a8e;
  --separator: rgba(60, 60, 67, 0.18);
  --accent: #007aff;
  --bubble-in: #e9e9eb;
  --bubble-in-text: #000000;
  --bubble-out: #007aff;
  --bubble-out-text: #ffffff;
  --danger: #ff3b30;
  --ok: #34c759;
  --nav-blur: rgba(249, 249, 249, 0.86);
  --avatar-l: 62%;
  --radius-bubble: 18px;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #000000;
    --bg-grouped: #000000;
    --bg-elevated: #1c1c1e;
    --fill: rgba(118, 118, 128, 0.24);
    --fill-strong: rgba(118, 118, 128, 0.36);
    --text: #ffffff;
    --text-dim: #8d8d93;
    --separator: rgba(84, 84, 88, 0.6);
    --accent: #0a84ff;
    --bubble-in: #262629;
    --bubble-in-text: #ffffff;
    --bubble-out: #0a84ff;
    --nav-blur: rgba(22, 22, 24, 0.84);
    --avatar-l: 48%;
  }
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }

html, body {
  margin: 0;
  height: 100%;
  background: var(--bg);
  color: var(--text);
  font: 17px/1.35 -apple-system, BlinkMacSystemFont, "SF Pro Text", system-ui, sans-serif;
  -webkit-font-smoothing: antialiased;
  -webkit-text-size-adjust: 100%;
  overscroll-behavior-y: none;
}

body { display: flex; flex-direction: column; }

button { font: inherit; color: inherit; }

.view {
  display: flex;
  flex-direction: column;
  flex: 1;
  min-height: 0;
  width: 100%;
  /* No effect on a phone; stops a stretched full-width sheet when debugging on a laptop. */
  max-width: 34rem;
  margin: 0 auto;
}
.view[hidden] { display: none; }

/* Pushing into a conversation or settings slides in from the right, like UINavigationController. */
.view.slide-in { animation: slide-in 0.28s cubic-bezier(0.2, 0.8, 0.2, 1); }
.view.fade-in { animation: fade-in 0.18s ease-out; }
@keyframes slide-in { from { transform: translateX(28%); opacity: 0; } to { transform: none; opacity: 1; } }
@keyframes fade-in { from { opacity: 0; } to { opacity: 1; } }
@media (prefers-reduced-motion: reduce) {
  .view.slide-in, .view.fade-in { animation: none; }
}

/* ---- icons --------------------------------------------------------------- */

[data-icon] svg { display: block; width: 100%; height: 100%; }

/* ---- navigation bars ------------------------------------------------------ */

.nav {
  position: sticky;
  top: 0;
  z-index: 3;
  padding: env(safe-area-inset-top) 16px 8px;
  background: var(--nav-blur);
  -webkit-backdrop-filter: saturate(180%) blur(20px);
  backdrop-filter: saturate(180%) blur(20px);
  border-bottom: 0.5px solid var(--separator);
}

.nav-row {
  display: flex;
  align-items: center;
  gap: 10px;
  height: 44px;
}
.nav-spacer { flex: 1; }

.large-title {
  font-size: 34px;
  font-weight: 700;
  letter-spacing: 0.01em;
  margin: 0 0 8px;
}

/* A conversation's bar: back button, centred person, action. */
.nav-compact {
  display: grid;
  grid-template-columns: 1fr auto 1fr;
  align-items: center;
  padding-bottom: 6px;
  min-height: calc(env(safe-area-inset-top) + 52px);
}
.thread-head {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 2px;
  font-size: 12px;
  font-weight: 500;
  max-width: 52vw;
  text-align: center;
}
.thread-head > span:last-child {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  max-width: 100%;
}
.thread-head > span:only-child { font-size: 17px; font-weight: 600; }
.nav-end { justify-self: end; }

.back {
  justify-self: start;
  display: inline-flex;
  align-items: center;
  gap: 2px;
  background: none;
  border: 0;
  color: var(--accent);
  font-size: 17px;
  padding: 8px 8px 8px 0;
  margin-left: -8px;
  cursor: pointer;
}
.back [data-icon] { width: 26px; height: 26px; }

.icon-button {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 36px;
  height: 36px;
  padding: 6px;
  border: 0;
  border-radius: 50%;
  background: none;
  color: var(--accent);
  cursor: pointer;
  text-decoration: none;
}
.icon-button:active { background: var(--fill); }
.icon-button[hidden] { display: none; }

/* Relay status: quiet unless something's wrong. */
.dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: var(--text-dim);
  flex: none;
  transition: background 0.25s;
}
.dot.live { background: var(--ok); }
.dot.down { background: var(--danger); }

/* ---- segmented control ---------------------------------------------------- */

.tabs {
  display: flex;
  padding: 2px;
  border-radius: 9px;
  background: var(--fill);
  margin-bottom: 8px;
}
.tab {
  flex: 1;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  border: 0;
  border-radius: 7px;
  padding: 6px 0;
  background: none;
  color: var(--text);
  font-size: 13px;
  font-weight: 600;
  cursor: pointer;
  transition: background 0.2s;
}
.tab.active { background: var(--bg-elevated); box-shadow: 0 3px 8px rgba(0, 0, 0, 0.12), 0 0 1px rgba(0, 0, 0, 0.04); }
@media (prefers-color-scheme: dark) { .tab.active { background: #636366; } }
.tab-badge {
  min-width: 18px;
  padding: 0 5px;
  border-radius: 9px;
  background: var(--danger);
  color: #fff;
  font-size: 12px;
  line-height: 18px;
  text-align: center;
}
.tab-badge[hidden] { display: none; }

/* ---- search --------------------------------------------------------------- */

.search-wrap {
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 0 8px;
  height: 36px;
  border-radius: 10px;
  background: var(--fill);
  color: var(--text-dim);
}
.search-icon { width: 16px; height: 16px; flex: none; }
#search {
  flex: 1;
  min-width: 0;
  height: 100%;
  border: 0;
  background: none;
  color: var(--text);
  font-size: 17px;
  outline: none;
  -webkit-appearance: none;
}
#search::placeholder { color: var(--text-dim); }

/* ---- lists ---------------------------------------------------------------- */

.list {
  list-style: none;
  margin: 0;
  padding: 0 0 calc(env(safe-area-inset-bottom) + 12px);
  overflow-y: auto;
  flex: 1;
  -webkit-overflow-scrolling: touch;
}

.row {
  position: relative;
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 10px 16px 10px 20px;
  cursor: pointer;
  transition: background 0.15s;
}
.row:active { background: var(--fill); }
/* Separators start after the avatar, as in iOS lists. */
.row::after {
  content: "";
  position: absolute;
  left: 80px;
  right: 0;
  bottom: 0;
  border-bottom: 0.5px solid var(--separator);
}
.row:last-child::after { display: none; }

.row-body { flex: 1; min-width: 0; display: flex; flex-direction: column; gap: 2px; }
.row-top { display: flex; align-items: baseline; gap: 8px; }
.row-name {
  flex: 1;
  font-weight: 600;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.row-time {
  flex: none;
  display: inline-flex;
  align-items: center;
  gap: 4px;
  color: var(--text-dim);
  font-size: 15px;
}
.row-time [data-icon] { width: 12px; height: 12px; opacity: 0.6; }
.row-preview {
  color: var(--text-dim);
  font-size: 15px;
  line-height: 1.3;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
  overflow-wrap: anywhere;
}

/* Unread: a blue dot in the left margin, like Messages. */
.unread-dot {
  position: absolute;
  left: 5px;
  top: 50%;
  width: 10px;
  height: 10px;
  margin-top: -5px;
  border-radius: 50%;
  background: var(--accent);
}
.row.unread .row-preview { color: var(--text); }

/* ---- avatars -------------------------------------------------------------- */

.avatar {
  --hue: 210;
  flex: none;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 48px;
  height: 48px;
  border-radius: 50%;
  background: linear-gradient(180deg,
    hsl(var(--hue) 55% calc(var(--avatar-l) + 12%)),
    hsl(var(--hue) 50% var(--avatar-l)));
  color: #fff;
  font-size: 19px;
  font-weight: 600;
  letter-spacing: 0.02em;
  user-select: none;
}
.avatar.neutral { background: linear-gradient(180deg, #a5abb8, #858994); }
.avatar [data-icon] { width: 60%; height: 60%; opacity: 0.95; }
.avatar.avatar-sm { width: 30px; height: 30px; font-size: 12px; }

/* ---- empty states ---------------------------------------------------------- */

.empty {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 6px;
  margin: 18vh 32px 0;
  color: var(--text-dim);
  text-align: center;
  font-size: 15px;
}
.empty[hidden] { display: none; }
.empty b { color: var(--text); font-size: 20px; }
.empty-icon { width: 44px; height: 44px; margin-bottom: 6px; opacity: 0.5; }

/* ---- conversation ----------------------------------------------------------- */

.bubbles {
  flex: 1;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  padding: 8px 12px calc(env(safe-area-inset-bottom) + 20px);
  display: flex;
  flex-direction: column;
}

.stamp {
  align-self: center;
  margin: 16px 0 8px;
  color: var(--text-dim);
  font-size: 12px;
}
.stamp b { font-weight: 600; }

.bubble {
  position: relative;
  max-width: 75%;
  margin-top: 2px;
  padding: 7px 12px 8px;
  border-radius: var(--radius-bubble);
  white-space: pre-wrap;
  overflow-wrap: anywhere;
  align-self: flex-start;
  background: var(--bubble-in);
  color: var(--bubble-in-text);
  line-height: 1.3;
  cursor: pointer;
  -webkit-user-select: text;
  user-select: text;
  transition: filter 0.15s, transform 0.15s;
}
.bubble.out { align-self: flex-end; background: var(--bubble-out); color: var(--bubble-out-text); }
.bubble.group-start { margin-top: 10px; }

/* The tail: only on the last bubble of a run, the way Messages draws it. */
.bubble.tail::before,
.bubble.tail::after {
  content: "";
  position: absolute;
  bottom: 0;
  height: 18px;
}
.bubble.tail::before { width: 18px; background: inherit; }
.bubble.tail::after { width: 10px; background: var(--bg); }
.bubble.tail:not(.out)::before { left: -6px; border-bottom-right-radius: 14px; }
.bubble.tail:not(.out)::after { left: -10px; border-bottom-right-radius: 10px; }
.bubble.tail.out::before { right: -6px; border-bottom-left-radius: 14px; }
.bubble.tail.out::after { right: -10px; border-bottom-left-radius: 10px; }

/* Tapping a message copies it; a brief press state confirms the tap. */
.bubble:active { filter: brightness(0.92); }
.bubble.copied { transform: scale(0.97); }

.bubble a {
  color: inherit;
  text-decoration: underline;
  text-underline-offset: 2px;
  text-decoration-thickness: 1px;
}
.bubble:not(.out) a { color: var(--accent); }

/* One-time codes: a tappable chip that copies just the code. */
.code-chip {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  margin: 0 1px;
  padding: 0 8px;
  border: 0;
  border-radius: 8px;
  background: var(--fill-strong);
  color: inherit;
  font: 600 1em/1.45 ui-monospace, "SF Mono", Menlo, monospace;
  letter-spacing: 0.06em;
  cursor: pointer;
  vertical-align: baseline;
}
.bubble.out .code-chip { background: rgba(255, 255, 255, 0.22); }
.code-chip [data-icon] { width: 13px; height: 13px; opacity: 0.7; }

/* Notification-sourced text may be cut short by the system; say so. */
.bubble .truncated {
  display: block;
  margin-top: 4px;
  font-size: 12px;
  opacity: 0.65;
  font-style: italic;
}

/* ---- calls ------------------------------------------------------------------ */

.call-row { padding-left: 16px; }
.call-row::after { left: 60px; }
.call-icon {
  flex: none;
  width: 30px;
  height: 30px;
  padding: 7px;
  border-radius: 50%;
  background: var(--fill);
  color: var(--text-dim);
}
.call-icon.missed { color: var(--danger); background: rgba(255, 59, 48, 0.12); }
.call-icon.live { color: #fff; background: var(--ok); animation: pulse 1.2s ease-in-out infinite; }
@keyframes pulse { 50% { box-shadow: 0 0 0 6px rgba(52, 199, 89, 0.25); } }
@media (prefers-reduced-motion: reduce) { .call-icon.live { animation: none; } }
.call-row .row-name.missed { color: var(--danger); }
.call-row .row-preview { -webkit-line-clamp: 1; font-size: 14px; }
.call-row .row-preview.live { color: var(--ok); font-weight: 600; }
.call-back {
  flex: none;
  width: 34px;
  height: 34px;
  padding: 8px;
  border-radius: 50%;
  background: var(--fill);
  color: var(--accent);
}
.call-back:active { background: var(--fill-strong); }

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

.view.centered {
  justify-content: center;
  align-items: center;
  padding: calc(env(safe-area-inset-top) + 24px) 24px calc(env(safe-area-inset-bottom) + 24px);
  background: var(--bg-grouped);
  max-width: none;
}
.card-screen { width: 100%; max-width: 22rem; text-align: center; }
.app-icon { border-radius: 16px; box-shadow: 0 6px 18px rgba(0, 0, 0, 0.18); margin-bottom: 18px; }
.card-screen h1 { font-size: 28px; margin: 0 0 10px; }
.card-screen p { color: var(--text-dim); margin: 0 0 22px; font-size: 16px; }
.card-screen b { color: var(--text); }

#pair-form { display: flex; flex-direction: column; gap: 12px; }

#pair-code {
  font: 600 26px/1.2 ui-monospace, "SF Mono", Menlo, monospace;
  letter-spacing: 0.12em;
  text-align: center;
  padding: 14px 0 14px 0.12em;
  text-transform: uppercase;
  border-radius: 14px;
  border: 0;
  background: var(--bg-elevated);
  color: var(--text);
  box-shadow: inset 0 0 0 0.5px var(--separator);
}
#pair-code:focus { outline: 2px solid var(--accent); outline-offset: 1px; }

button.primary,
button.secondary {
  width: 100%;
  padding: 14px;
  border: 0;
  border-radius: 14px;
  font-size: 17px;
  font-weight: 600;
  cursor: pointer;
  transition: opacity 0.15s;
}
button.primary { background: var(--accent); color: #fff; }
button.secondary { background: var(--fill); color: var(--accent); }
button.danger { color: var(--danger); }
button:active { opacity: 0.8; }
button:disabled { opacity: 0.45; }

.stack { display: flex; flex-direction: column; gap: 10px; margin-top: 8px; }
.small { color: var(--text-dim); font-size: 14px; }
.error { color: var(--danger) !important; margin-top: 16px; }

/* The pairing fingerprint: large, monospaced, easy to compare digit by digit. */
.fingerprint {
  font: 600 26px/1.3 ui-monospace, "SF Mono", Menlo, monospace;
  letter-spacing: 0.04em;
  color: var(--text) !important;
  margin: 8px 0 20px !important;
  word-spacing: 0.3em;
  white-space: pre-line;
}
.small-fingerprint { font-size: 15px; margin: 0 !important; text-align: right; line-height: 1.35; }

/* ---- settings (grouped) ------------------------------------------------------- */

.view.grouped-bg { background: var(--bg-grouped); max-width: none; }
.view.grouped-bg > * { width: 100%; max-width: 34rem; margin-left: auto; margin-right: auto; }
.view.grouped-bg .nav { background: var(--nav-blur); }

.settings {
  overflow-y: auto;
  padding: 8px 16px calc(env(safe-area-inset-bottom) + 24px);
}
.group-label {
  margin: 24px 16px 6px;
  color: var(--text-dim);
  font-size: 13px;
  font-weight: 400;
  text-transform: uppercase;
  letter-spacing: 0.02em;
}
.group {
  overflow: hidden;
  border-radius: 12px;
  background: var(--bg-elevated);
}
.group-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  width: 100%;
  min-height: 46px;
  padding: 11px 16px;
  border: 0;
  background: none;
  text-align: left;
  font-size: 17px;
}
.group-row + .group-row { box-shadow: inset 16px 0.5px 0 0 var(--bg-elevated), inset 0 0.5px 0 0 var(--separator); }
.group-row[hidden] { display: none; }
.group-value { color: var(--text-dim); text-align: right; min-width: 0; overflow-wrap: anywhere; }
.group-row > span:first-child { flex: none; }
.group-action { color: var(--accent); cursor: pointer; }
.group-action:active { background: var(--fill); }
.group-action.danger { color: var(--danger); }
.group-note { margin: 6px 16px 0; color: var(--text-dim); font-size: 13px; line-height: 1.4; }

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

.toast {
  position: fixed;
  left: 50%;
  bottom: calc(env(safe-area-inset-bottom) + 28px);
  transform: translateX(-50%);
  z-index: 10;
  max-width: 88vw;
  padding: 10px 18px;
  border-radius: 999px;
  background: rgba(30, 30, 32, 0.92);
  -webkit-backdrop-filter: blur(16px);
  backdrop-filter: blur(16px);
  color: #fff;
  font-size: 15px;
  font-weight: 500;
  text-align: center;
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.2);
  animation: toast-in 0.22s ease-out;
}
@keyframes toast-in { from { opacity: 0; transform: translate(-50%, 8px); } }
.toast[hidden] { display: none; }

/* ---- incoming call ------------------------------------------------------------- */

/* Full screen, over everything, like the Phone app's incoming call — while it rings. */
.incoming {
  position: fixed;
  inset: 0;
  z-index: 20;
  display: flex;
  flex-direction: column;
  justify-content: space-between;
  padding: calc(env(safe-area-inset-top) + 56px) 32px calc(env(safe-area-inset-bottom) + 36px);
  background:
    radial-gradient(120% 70% at 50% 0%, rgba(80, 110, 160, 0.55), transparent 70%),
    linear-gradient(180deg, #1b2230, #0b0d12);
  color: #fff;
  text-align: center;
  animation: fade-in 0.25s ease-out;
}
.incoming[hidden] { display: none; }
.incoming-top { display: flex; flex-direction: column; align-items: center; gap: 8px; }
.incoming-kicker { font-size: 15px; color: rgba(255, 255, 255, 0.7); margin-bottom: 18px; }
.avatar.avatar-xl { width: 112px; height: 112px; font-size: 44px; margin-bottom: 10px; box-shadow: 0 0 0 0 rgba(255, 255, 255, 0.35); animation: ring 1.6s ease-out infinite; }
@keyframes ring { 70% { box-shadow: 0 0 0 26px rgba(255, 255, 255, 0); } 100% { box-shadow: 0 0 0 0 rgba(255, 255, 255, 0); } }
.incoming-name { font-size: 32px; font-weight: 600; line-height: 1.15; overflow-wrap: anywhere; }
.incoming-number { font-size: 17px; color: rgba(255, 255, 255, 0.75); }
.incoming-timer { font-size: 15px; color: rgba(255, 255, 255, 0.6); font-variant-numeric: tabular-nums; margin-top: 4px; }
.incoming-actions { display: flex; justify-content: center; gap: 72px; }
.incoming-button {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 8px;
  border: 0;
  background: none;
  color: #fff;
  font-size: 13px;
  cursor: pointer;
}
.incoming-button[hidden] { display: none; }
.incoming-circle {
  width: 68px;
  height: 68px;
  padding: 21px;
  border-radius: 50%;
  background: rgba(255, 255, 255, 0.18);
  -webkit-backdrop-filter: blur(10px);
  backdrop-filter: blur(10px);
}
.incoming-circle.dismiss { background: rgba(255, 59, 48, 0.9); }
.incoming-note { margin: 0; font-size: 13px; color: rgba(255, 255, 255, 0.55); }
@media (prefers-reduced-motion: reduce) { .avatar.avatar-xl { animation: none; } }
