:root {
  --paper: #E9EEF2;
  --surface: #FFFFFF;
  --ink: #17232E;
  --ink-soft: #5A6A78;
  --line: #CFD8E0;
  --mine: #1D4F7A;
  --mine-ink: #FFFFFF;
  --theirs: #FFFFFF;
  --theirs-ink: #17232E;
  --signal: #F4C542;
  --signal-ink: #2A2100;
  --danger: #B3261E;
  --focus: #1D4F7A;

  --r-bubble: 18px;
  --r-control: 22px;
  --col: 720px;

  --t-body: 1.0625rem;   /* 17px */
  --t-small: 0.8125rem;  /* 13px */
  --t-title: 1.375rem;   /* 22px */

  color-scheme: light dark;
}

@media (prefers-color-scheme: dark) {
  :root {
    --paper: #141C23;
    --surface: #1E2A34;
    --ink: #E6ECF1;
    --ink-soft: #93A4B3;
    --line: #2E3D4A;
    --mine: #3E7CB8;
    --mine-ink: #FFFFFF;
    --theirs: #233240;
    --theirs-ink: #E6ECF1;
    --focus: #8DB8E2;
  }
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }

html, body {
  margin: 0;
  height: 100%;
  background: var(--paper);
  color: var(--ink);
  font-family: "Atkinson Hyperlegible", system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  font-size: var(--t-body);
  line-height: 1.45;
  -webkit-text-size-adjust: 100%;
  -webkit-tap-highlight-color: transparent;
}

button, input, textarea { font: inherit; color: inherit; }
button { cursor: pointer; }
:focus-visible { outline: 3px solid var(--focus); outline-offset: 2px; }

.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;
}

.app { height: 100%; }
.view { height: 100dvh; }
.view-center {
  display: grid; place-items: center;
  padding: max(24px, env(safe-area-inset-top)) 16px max(24px, env(safe-area-inset-bottom));
}
.muted { color: var(--ink-soft); }

/* ---------- Sign in ---------- */
.login {
  width: 100%; max-width: 360px;
  display: flex; flex-direction: column; gap: 6px;
}
.login h1 { font-size: 2rem; line-height: 1.1; margin: 0 0 20px; letter-spacing: -0.01em; }
.login label { font-weight: 700; font-size: var(--t-small); margin-top: 10px; }
.login input {
  padding: 12px 14px; border-radius: 12px;
  border: 1.5px solid var(--line); background: var(--surface);
}
.login input:focus-visible { outline: none; border-color: var(--focus); }
.login p { margin: 0 0 12px; }
.btn-primary {
  margin-top: 20px; padding: 13px 16px; border: 0; border-radius: 12px;
  background: var(--mine); color: var(--mine-ink); font-weight: 700;
}
.btn-primary:disabled { opacity: 0.6; }
.error { color: var(--danger); font-size: var(--t-small); margin: 8px 0 0; }
.notice {
  margin: 0 0 8px; padding: 10px 12px; border-radius: 10px;
  background: var(--surface); border: 1px solid var(--line); font-size: 0.9375rem;
}
.hint { font-size: var(--t-small); color: var(--ink-soft); margin: 4px 0 0; }
.login label.check {
  display: flex; align-items: center; gap: 10px;
  margin-top: 14px; font-size: var(--t-body); font-weight: 400;
}
.check input { width: 20px; height: 20px; margin: 0; accent-color: var(--mine); }
.login .muted { margin: -8px 0 8px; }
.login-alt { align-self: center; margin-top: 10px; }

/* ---------- Chat layout ---------- */
.view-chat { display: flex; flex-direction: column; }

.top {
  padding: calc(env(safe-area-inset-top) + 10px) 12px 10px 16px;
  border-bottom: 1px solid var(--line);
  background: var(--paper);
}
.top-inner {
  position: relative;
  max-width: var(--col); margin: 0 auto;
  display: flex; align-items: center; gap: 12px;
}
.who { flex: 1; min-width: 0; }
.top h1 { margin: 0; font-size: var(--t-title); line-height: 1.2; }
.status { margin: 0; font-size: var(--t-small); color: var(--ink-soft); min-height: 1.2em; }

.icon-btn {
  flex: none; width: 44px; height: 44px; border: 0; border-radius: 50%;
  display: grid; place-items: center; background: transparent; color: var(--ink-soft);
}
.icon-btn svg { width: 24px; height: 24px; }
.top .icon-btn svg circle { fill: currentColor; }
.icon-btn:active { background: var(--line); }

.menu {
  position: absolute; right: 0; top: calc(100% + 4px); z-index: 10;
  min-width: 240px; padding: 6px;
  background: var(--surface); border: 1px solid var(--line); border-radius: 14px;
  box-shadow: 0 8px 24px rgb(23 35 46 / 0.18);
  display: flex; flex-direction: column;
}
.menu button {
  text-align: left; padding: 12px 14px; border: 0; border-radius: 10px; background: transparent;
}
.menu button:active, .menu button:hover { background: var(--paper); }

.banner {
  max-width: var(--col); width: calc(100% - 32px); margin: 12px auto 0;
  padding: 12px 14px; border-radius: 14px;
  background: var(--surface); border: 1px solid var(--line);
}
.banner p { margin: 0 0 8px; font-size: 0.9375rem; }
.banner-actions { display: flex; gap: 12px; align-items: center; }
.btn-small {
  padding: 8px 14px; border: 0; border-radius: 10px;
  background: var(--mine); color: var(--mine-ink); font-weight: 700;
}
.btn-link { padding: 8px 4px; border: 0; background: none; color: var(--ink-soft); }

.scroller {
  flex: 1; overflow-y: auto; overscroll-behavior: contain;
  -webkit-overflow-scrolling: touch;
}
.messages {
  max-width: var(--col); margin: 0 auto;
  padding: 12px 16px 8px;
  display: flex; flex-direction: column;
}
.empty { text-align: center; color: var(--ink-soft); margin: 30vh 16px 0; }

/* ---------- Messages ---------- */
.day {
  align-self: center; margin: 16px 0 8px;
  font-size: var(--t-small); font-weight: 700; color: var(--ink-soft);
}

.msg { display: flex; flex-direction: column; max-width: min(78%, 520px); margin-top: 6px; }
.msg.mine { align-self: flex-end; align-items: flex-end; }
.msg.theirs { align-self: flex-start; align-items: flex-start; }

.bubble {
  padding: 9px 14px; border-radius: var(--r-bubble);
  white-space: pre-wrap; overflow-wrap: anywhere;
}
.mine .bubble { background: var(--mine); color: var(--mine-ink); border-bottom-right-radius: 6px; }
.theirs .bubble { background: var(--theirs); color: var(--theirs-ink); border-bottom-left-radius: 6px; }
@media (prefers-color-scheme: light) {
  .theirs .bubble { box-shadow: 0 0 0 1px var(--line); }
}
.bubble a { color: inherit; text-decoration-thickness: 1px; text-underline-offset: 2px; }

.kind-image .bubble { padding: 4px; }
.photo {
  display: block; width: 100%; max-width: 320px; min-height: 120px; max-height: 420px;
  object-fit: cover; border-radius: 14px; background: var(--line); cursor: zoom-in;
}

.voice { display: flex; flex-direction: column; gap: 6px; min-width: 240px; }
.voice-label { font-size: var(--t-small); font-weight: 700; }
.voice audio { width: 100%; height: 40px; }

.media-error { font-size: var(--t-small); }

.meta { font-size: 0.75rem; color: var(--ink-soft); margin: 3px 6px 0; }
.seen { align-self: flex-end; font-size: 0.75rem; font-weight: 700; color: var(--ink-soft); margin: 2px 6px 0; }

/* ---------- Composer ---------- */
.composer {
  border-top: 1px solid var(--line);
  background: var(--paper);
  padding: 8px 12px calc(env(safe-area-inset-bottom) + 8px);
}
.compose-row, .recorder, .send-error { max-width: var(--col); margin: 0 auto; }
.compose-row { display: flex; align-items: flex-end; gap: 6px; }

#text-input {
  flex: 1; min-width: 0; resize: none;
  max-height: 140px; padding: 10px 16px; line-height: 1.35;
  border: 1.5px solid var(--line); border-radius: var(--r-control);
  background: var(--surface);
}
#text-input:focus-visible { outline: none; border-color: var(--focus); }

.send-btn {
  flex: none; width: 44px; height: 44px; border: 0; border-radius: 50%;
  display: grid; place-items: center;
  background: var(--mine); color: var(--mine-ink);
}
.send-btn svg { width: 22px; height: 22px; }
.send-btn:disabled { opacity: 0.5; }

/* The one loud moment: recording takes over the composer. */
.recorder {
  display: flex; align-items: center; gap: 12px;
  min-height: 48px; padding: 4px 6px 4px 18px;
  border-radius: var(--r-control);
  background: var(--signal); color: var(--signal-ink);
}
.rec-dot {
  width: 12px; height: 12px; border-radius: 50%; background: var(--danger);
  animation: pulse 1.2s ease-in-out infinite;
}
.rec-time { font-weight: 700; font-size: 1.25rem; font-variant-numeric: tabular-nums; min-width: 3.2em; }
.rec-label { flex: 1; font-weight: 700; }
.rec-btn {
  padding: 10px 16px; border: 0; border-radius: 18px;
  background: rgb(0 0 0 / 0.08); color: var(--signal-ink); font-weight: 700;
}
.rec-send { background: var(--signal-ink); color: var(--signal); }
@keyframes pulse { 50% { opacity: 0.35; } }
@media (prefers-reduced-motion: reduce) { .rec-dot { animation: none; } }

.send-error { color: var(--danger); font-size: var(--t-small); margin-top: 6px; }
.send-error.info { color: var(--ink-soft); }

/* ---------- Photo viewer ---------- */
.lightbox {
  position: fixed; inset: 0; z-index: 50;
  display: grid; place-items: center;
  background: rgb(8 12 16 / 0.92);
  padding: max(16px, env(safe-area-inset-top)) 16px max(16px, env(safe-area-inset-bottom));
}
.lightbox img { max-width: 100%; max-height: 100%; object-fit: contain; }
.lightbox-close {
  position: absolute; top: max(12px, env(safe-area-inset-top)); right: 12px;
  padding: 10px 16px; border: 0; border-radius: 12px;
  background: rgb(255 255 255 / 0.15); color: #fff; font-weight: 700;
}
