/*
  Sizing is in rem/em so text and controls follow the browser's and the device's text-size settings.
  Colour pairs are chosen for WCAG 2.2 AA: 4.5:1 for text, 3:1 for control borders and focus rings.
*/
:root {
  --bg: #ffffff;
  --bg-soft: #f3f4f6;
  --bg-hover: #e6e8ed;
  --bg-active: #dce6ff;
  --text: #111318;
  --muted: #4a5160;
  --separator: #d5d8de;          /* decorative lines only */
  --control-border: #737a87;     /* input and button outlines, 3:1+ */
  --accent: #0b55e0;             /* filled: outgoing bubbles, primary buttons, badges */
  --accent-text: #ffffff;
  --link: #0b4fd1;               /* accent used as text on light backgrounds */
  --focus: #0b4fd1;
  --bubble-in: #e6e8ed;
  --bubble-in-text: #111318;
  --danger: #b42318;
  --shadow: 0 0.5rem 2rem rgba(0, 0, 0, .14);
  --radius: 1.125rem;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #111317;
    --bg-soft: #181b20;
    --bg-hover: #23272f;
    --bg-active: #1d2c52;
    --text: #eceef2;
    --muted: #b0b6c1;
    --separator: #30343c;
    --control-border: #8b92a0;
    --accent: #2a62f0;
    --link: #93b4ff;
    --focus: #93b4ff;
    --bubble-in: #262a32;
    --bubble-in-text: #eceef2;
    --danger: #ff8a80;
    --shadow: 0 0.5rem 2rem rgba(0, 0, 0, .55);
    color-scheme: dark;
  }
}
@media (prefers-contrast: more) {
  :root { --muted: var(--text); --separator: var(--control-border); }
}

/* iOS Safari: follow the system Dynamic Type size. Other browsers use their own default (16px unless the user changed it). */
@supports (font: -apple-system-body) {
  html { font: -apple-system-body; }
}

* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
/* Behind overscroll and edge-to-edge areas (Android navigation bar, iOS safe areas). */
html { background: var(--bg-soft); }
body {
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  font-size: 1rem;
  line-height: 1.45;
  background: var(--bg);
  color: var(--text);
  -webkit-text-size-adjust: 100%;
  text-size-adjust: 100%;
}
[hidden] { display: none !important; }
button, input, select, textarea { font: inherit; color: inherit; }
svg { width: 1.375em; height: 1.375em; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; flex: none; }
a { color: var(--link); }
h1, h2, h3 { margin: 0; }

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

:focus-visible { outline: 3px solid var(--focus); outline-offset: 2px; }
input:focus-visible, select:focus-visible, textarea:focus-visible { outline-offset: 0; }

.btn {
  border: 1px solid var(--control-border); background: var(--bg); border-radius: 0.625rem;
  padding: 0.5em 0.9em; min-height: 2.75rem; cursor: pointer; font-weight: 600;
}
.btn:hover { background: var(--bg-hover); }
.btn.primary { background: var(--accent); color: var(--accent-text); border-color: var(--accent); }
.btn.primary:hover { filter: brightness(1.12); }
.btn.danger { color: var(--danger); border-color: var(--danger); }
.btn:disabled { opacity: .55; cursor: default; }
.icon-btn {
  display: inline-grid; place-items: center; width: 2.75rem; height: 2.75rem; flex: none; padding: 0;
  border: 0; background: transparent; border-radius: 50%; cursor: pointer; color: var(--muted);
}
.icon-btn:hover { background: var(--bg-hover); color: var(--text); }
.link-btn { border: 0; background: none; padding: 0; color: var(--link); cursor: pointer; text-decoration: underline; font-weight: 600; }
input:not([type=checkbox]):not([type=radio]), select, textarea {
  border: 1px solid var(--control-border); background: var(--bg); border-radius: 0.625rem;
  padding: 0.5em 0.65em; width: 100%; min-height: 2.75rem;
}
::placeholder { color: var(--muted); opacity: 1; }
input[type=checkbox], input[type=radio] { width: 1.25em; height: 1.25em; margin: 0; accent-color: var(--accent); flex: none; }
.form-error { color: var(--danger); margin: 0; font-weight: 600; }
.form-error:empty { display: none; }
.field { display: grid; gap: 0.35rem; font-weight: 600; }
.profile-value { font-weight: 400; padding: 0.5em 0.65em; border-radius: 0.625rem; background: var(--bg-soft); }
.field-hint { font-weight: 400; color: var(--muted); font-size: 0.875rem; }

/* ---------- login ---------- */
.login { min-height: 100%; display: grid; place-items: center; padding: 1rem; background: var(--bg-soft); }
.login-card {
  width: 100%; max-width: 24rem; background: var(--bg); padding: 1.75rem; border-radius: 1rem;
  box-shadow: var(--shadow); display: grid; gap: 1rem;
}
.login-card h1 { font-size: 1.5rem; }

/* ---------- layout ---------- */
.app { display: grid; grid-template-columns: minmax(18rem, 22rem) 1fr; height: 100dvh; }
.sidebar { border-right: 1px solid var(--separator); display: flex; flex-direction: column; min-height: 0; background: var(--bg-soft); }
.sidebar-head { display: flex; gap: 0.25rem; align-items: center; padding: 0.5rem 0.625rem 0.25rem 1rem; position: relative; }
.sidebar-head h1 { flex: 1; min-width: 0; font-size: 1.25rem; overflow-wrap: break-word; }
.line-filter { padding: 0.25rem 0.625rem; }
.line-filter select { font-weight: 600; background: var(--bg); }
.line-single { margin: 0; padding: 0 0.375rem; color: var(--muted); font-weight: 600; }
.nowrap { white-space: nowrap; }
.search { padding: 0.25rem 0.625rem 0.5rem; }
.search input { border-radius: 999px; padding-inline: 1em; }

.notify-banner { margin: 0 0.625rem 0.5rem; padding: 0.5rem 0.75rem; border-radius: 0.625rem; background: var(--bg-active); font-size: 0.875rem; }

.menu {
  position: absolute; top: 100%; right: 0.625rem; z-index: 20; min-width: 14rem; padding: 0.375rem;
  background: var(--bg); border: 1px solid var(--control-border); border-radius: 0.75rem; box-shadow: var(--shadow);
}
.menu [role=menuitem] {
  display: block; width: 100%; text-align: left; border: 0; background: none; padding: 0.6em 0.75em;
  border-radius: 0.5rem; cursor: pointer; min-height: 2.75rem;
}
.menu [role=menuitem]:hover, .menu [role=menuitem]:focus-visible { background: var(--bg-hover); }
.menu .menu-who { padding: 0.4em 0.75em 0.5em; color: var(--muted); font-size: 0.875rem; border-bottom: 1px solid var(--separator); margin-bottom: 0.25rem; }

.conversations { list-style: none; margin: 0; padding: 0 0.375rem 0.625rem; overflow-y: auto; flex: 1; }
.conv {
  display: grid; grid-template-columns: 2.75rem minmax(0, 1fr) auto; gap: 0.125rem 0.625rem; align-items: center;
  padding: 0.55rem 0.625rem; border-radius: 0.75rem; color: inherit; text-decoration: none;
}
.conv:hover { background: var(--bg-hover); }
.conv[aria-current=page] { background: var(--bg-active); }
.avatar {
  grid-row: span 2; width: 2.75rem; height: 2.75rem; border-radius: 50%; display: grid; place-items: center;
  font-weight: 700; color: #fff; text-transform: uppercase;
}
.conv-name { font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.conv-time { font-size: 0.8125rem; color: var(--muted); white-space: nowrap; }
.conv-preview { color: var(--muted); font-size: 0.9375rem; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.conv-meta { display: flex; gap: 0.375rem; align-items: center; justify-self: end; }
.conv.unread .conv-name, .conv.unread .conv-preview { color: var(--text); font-weight: 700; }
.badge {
  background: var(--accent); color: var(--accent-text); border-radius: 999px; font-size: 0.8125rem; font-weight: 700;
  padding: 0 0.5em; min-width: 1.6em; text-align: center;
}
.line-tag { font-size: 0.75rem; color: var(--muted); border: 1px solid var(--control-border); border-radius: 0.375rem; padding: 0 0.35em; white-space: nowrap; }
.conversations .empty { color: var(--muted); text-align: center; padding: 2rem 0.75rem; }

/* ---------- thread ---------- */
.thread { display: flex; flex-direction: column; min-height: 0; min-width: 0; position: relative; }
.empty-state { flex: 1; display: grid; place-items: center; color: var(--muted); padding: 1rem; text-align: center; }
.thread-head {
  display: grid; grid-template-columns: auto minmax(0, 1fr) auto auto; align-items: center; column-gap: 0.375rem;
  grid-template-areas: "back title rename delete" ". sub sub sub";
  padding: 0.5rem 0.75rem; border-bottom: 1px solid var(--separator);
}
.thread-head .back { display: none; grid-area: back; }
.thread-title { display: contents; }
.thread-title h2 { grid-area: title; font-size: 1.0625rem; overflow-wrap: break-word; }
.thread-title h2:focus { outline: none; }
.thread-title h2:focus-visible { outline: 3px solid var(--focus); }
#thread-rename { grid-area: rename; }
#thread-delete { grid-area: delete; }
/* The details line sits under the name and spans the full width, so phone numbers have room. */
.thread-sub { grid-area: sub; font-size: 0.8125rem; color: var(--muted); }

.messages { flex: 1; overflow-y: auto; padding: 1rem 1rem 0.5rem; display: flex; flex-direction: column; gap: 0.125rem; }
.messages:focus-visible { outline-offset: -3px; }
.load-older { align-self: center; margin-bottom: 0.625rem; }
.day { align-self: center; font-size: 0.8125rem; color: var(--muted); margin: 0.875rem 0 0.5rem; font-weight: 600; }
.msg { display: flex; flex-direction: column; max-width: min(75%, 34rem); }
.msg.in { align-self: flex-start; align-items: flex-start; }
.msg.out { align-self: flex-end; align-items: flex-end; }
.msg.gap { margin-top: 0.5rem; }
.bubble { padding: 0.5em 0.85em; border-radius: var(--radius); white-space: pre-wrap; overflow-wrap: anywhere; }
.msg.in .bubble { background: var(--bubble-in); color: var(--bubble-in-text); border-bottom-left-radius: 0.375rem; }
.msg.out .bubble { background: var(--accent); color: var(--accent-text); border-bottom-right-radius: 0.375rem; }
.msg.out.failed .bubble { background: transparent; color: var(--text); border: 2px solid var(--danger); }
.msg.sending .bubble { opacity: .8; }
.msg.in .bubble a { color: inherit; }
.msg.out .bubble a { color: inherit; }
.media { display: flex; flex-wrap: wrap; gap: 0.25rem; margin-bottom: 0.2rem; }
.msg.out .media { justify-content: flex-end; }
.photo-btn { border: 0; padding: 0; background: none; border-radius: 0.875rem; cursor: zoom-in; display: block; }
.photo-btn img { max-width: min(16rem, 100%); max-height: 18rem; border-radius: 0.875rem; display: block; background: var(--bg-soft); }
.meta { font-size: 0.8125rem; color: var(--muted); margin: 0.2rem 0.25rem 0; }
.meta.err { color: var(--danger); font-weight: 600; }

.composer { border-top: 1px solid var(--separator); padding: 0.5rem 0.75rem calc(0.5rem + env(safe-area-inset-bottom)); }
.composer-row { display: flex; align-items: flex-end; gap: 0.375rem; }
.composer textarea { flex: 1; resize: none; border-radius: 1.375rem; padding: 0.55em 1em; max-height: 40vh; line-height: 1.35; }
.send {
  width: 2.75rem; height: 2.75rem; flex: none; border-radius: 50%; border: 0; display: grid; place-items: center;
  background: var(--accent); color: var(--accent-text); cursor: pointer;
}
.send:disabled { background: var(--bg-hover); color: var(--muted); cursor: default; }
.counter { font-size: 0.8125rem; color: var(--muted); text-align: right; padding: 0.2rem 3.25rem 0 0; min-height: 1.2em; }
.attachments { display: flex; gap: 0.5rem; flex-wrap: wrap; }
.attachments:not(:empty) { padding: 0.25rem 0 0.5rem 3.125rem; }
.thumb { position: relative; }
.thumb img { width: 4.5rem; height: 4.5rem; object-fit: cover; border-radius: 0.625rem; display: block; }
.thumb.loading img { opacity: .45; }
.thumb button {
  position: absolute; top: -0.5rem; right: -0.5rem; width: 1.75rem; height: 1.75rem; border-radius: 50%;
  border: 2px solid var(--bg); background: var(--text); color: var(--bg); cursor: pointer; padding: 0;
  display: grid; place-items: center;
}
.thumb button svg { width: 1rem; height: 1rem; stroke-width: 3; }

/* ---------- dialogs ---------- */
dialog {
  border: 1px solid var(--control-border); border-radius: 1rem; padding: 0; width: min(34rem, calc(100vw - 2rem));
  max-height: calc(100dvh - 2rem); background: var(--bg); color: var(--text); box-shadow: var(--shadow);
}
dialog::backdrop { background: rgba(0, 0, 0, .5); }
#dialog-form { padding: 1.375rem; display: grid; gap: 1rem; }
#dialog-form h2 { font-size: 1.25rem; }
.check { display: flex; align-items: center; gap: 0.5rem; cursor: pointer; }
.dialog-actions { display: flex; justify-content: flex-end; gap: 0.5rem; margin-top: 0.25rem; flex-wrap: wrap; }
.dialog-actions .spacer { flex: 1; }
.rows { display: grid; gap: 0.5rem; }
fieldset.row, .row { border: 1px solid var(--control-border); border-radius: 0.75rem; padding: 0.75rem; display: grid; gap: 0.625rem; margin: 0; min-width: 0; }
.row legend { font-weight: 700; padding: 0 0.25rem; }
.row-actions { display: flex; align-items: center; gap: 0.5rem; flex-wrap: wrap; }
.row-actions .check { margin-right: auto; }
fieldset.chips-group { border: 0; padding: 0; margin: 0; min-width: 0; }
fieldset.chips-group legend { font-weight: 600; padding: 0; margin-bottom: 0.375rem; }
.chips {
  display: grid; grid-template-columns: repeat(auto-fill, minmax(11em, 1fr)); gap: 0.125rem 0.75rem;
  font-variant-numeric: tabular-nums; max-height: 14rem; overflow-y: auto; padding: 0.125rem;
}
.chips .check { min-height: 2.25rem; white-space: nowrap; }
.chip-label { color: var(--muted); overflow: hidden; text-overflow: ellipsis; }
.hint { color: var(--muted); margin: 0; }
.notice { margin: 0; padding: 0.625rem 0.75rem; border-radius: 0.625rem; background: var(--bg-active); }
.section-title { font-size: 1rem; }
.dialog-toolbar { display: flex; align-items: center; gap: 0.75rem; flex-wrap: wrap; }
.dialog-toolbar .hint { flex: 1 1 14rem; }
.back-link { display: inline-flex; align-items: center; gap: 0.25rem; justify-self: start; min-height: 2.75rem; text-decoration: none; }
.back-link svg { width: 1.1em; height: 1.1em; }
.back-link:hover { text-decoration: underline; }
.chips-tools { display: flex; gap: 1rem; margin-bottom: 0.375rem; }
.chips-group .check { min-height: 2.25rem; }

.user-list { list-style: none; margin: 0; padding: 0; border: 1px solid var(--control-border); border-radius: 0.75rem; overflow: hidden; }
.user-list li + li { border-top: 1px solid var(--separator); }
.user-item {
  display: grid; grid-template-columns: 1fr auto; grid-template-rows: auto auto; column-gap: 0.75rem; align-items: center;
  width: 100%; text-align: left; background: var(--bg); border: 0; padding: 0.7rem 0.875rem; cursor: pointer; min-height: 3.25rem;
}
.user-item:hover { background: var(--bg-hover); }
.user-item:focus-visible { outline-offset: -3px; }
.user-item svg { grid-row: 1 / 3; grid-column: 2; color: var(--muted); }
.user-name { font-weight: 700; overflow-wrap: anywhere; }
.user-you { font-weight: 400; color: var(--muted); }
.user-detail { color: var(--muted); font-size: 0.9375rem; }

/* Padding leaves room for borders and focus rings inside the scrolling area. */
.number-list { list-style: none; margin: 0; padding: 0.3rem; display: grid; gap: 0.5rem; max-height: 50dvh; overflow-y: auto; }
.number-row { display: grid; grid-template-columns: 10em minmax(0, 1fr); gap: 0.5rem; align-items: center; }
.number-row input { min-width: 0; }
.number-row .num { font-variant-numeric: tabular-nums; font-weight: 600; }

#lightbox { width: auto; max-width: calc(100vw - 2rem); padding: 0; background: #000; border-color: #000; }
#lightbox img { display: block; max-width: calc(100vw - 2rem); max-height: calc(100dvh - 6rem); }
#lightbox .lightbox-bar { display: flex; justify-content: flex-end; padding: 0.375rem; }
#lightbox .lightbox-bar .btn { background: #fff; color: #111; border-color: #fff; }

.toast {
  position: fixed; left: 50%; bottom: 1.5rem; transform: translateX(-50%); z-index: 60; max-width: calc(100vw - 2rem);
  background: var(--text); color: var(--bg); padding: 0.625rem 1rem; border-radius: 0.625rem; box-shadow: var(--shadow);
}

/* ---------- Windows high contrast and other forced-colour modes ---------- */
@media (forced-colors: active) {
  .bubble, .badge, .avatar, .send, .btn.primary, .conv[aria-current=page], .thumb button { border: 1px solid CanvasText; }
  .conv[aria-current=page] { outline: 2px solid Highlight; }
  .msg.out.failed .bubble { border: 2px dashed CanvasText; }
  .send:disabled { color: GrayText; }
}

/* ---------- one pane at a time ----------
  The "narrow" class is set by app.js when fewer than about 50 characters of the current text size fit across.
  It covers phones, small windows, browser zoom and large system text (which screen-width media queries miss on iOS). */
.narrow .app { grid-template-columns: 1fr; }
.narrow .thread { display: none; }
.narrow .app.show-thread .sidebar { display: none; }
.narrow .app.show-thread .thread { display: flex; }
.narrow .thread-head { grid-template-areas: "back title rename delete" "sub sub sub sub"; }
.narrow .thread-head .back { display: inline-grid; }
.narrow .msg { max-width: 88%; }
.narrow .number-row { grid-template-columns: 1fr; gap: 0.25rem; }
.narrow .counter { padding-right: 0; }
.narrow .chips { max-height: none; }

