@font-face {
  font-family: "Bricolage";
  src: url("fonts/bricolage.woff2") format("woff2");
  font-weight: 200 800;
  font-display: swap;
}
@font-face {
  font-family: "Atkinson";
  src: url("fonts/atkinson-hyperlegible-latin-400-normal.woff2") format("woff2");
  font-weight: 400;
  font-display: swap;
}
@font-face {
  font-family: "Atkinson";
  src: url("fonts/atkinson-hyperlegible-latin-700-normal.woff2") format("woff2");
  font-weight: 700;
  font-display: swap;
}

/* Airmail: cool envelope paper, post-office blue, airmail red. */
:root {
  --paper: #EEF2F8;
  --sheet: #FFFFFF;
  --ink: #18233A;
  --muted: #5A6680;
  --line: #D6DDE9;
  --blue: #1D4C9C;
  --blue-soft: #DCE6F7;
  --red: #C8283B;
  --on-blue: #FFFFFF;
  --h0: #1D4C9C; --h1: #C8283B; --h2: #0F7A6C; --h3: #A8660B; --h4: #7A3E9D; --h5: #3F7D1F;
  --shadow: 0 1px 2px rgb(24 35 58 / .08), 0 8px 24px rgb(24 35 58 / .10);
  --display: "Bricolage", ui-sans-serif, system-ui, sans-serif;
  --body: "Atkinson", ui-sans-serif, system-ui, sans-serif;
  --stripe: repeating-linear-gradient(-45deg, var(--red) 0 9px, transparent 9px 14px, var(--blue) 14px 23px, transparent 23px 28px);
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --paper: #0F1A31;
    --sheet: #172642;
    --ink: #E7ECF6;
    --muted: #9AA7C2;
    --line: #273A5E;
    --blue: #5F8FE6;
    --blue-soft: #1F3561;
    --red: #EE5A6C;
    --on-blue: #08142B;
    --h0: #6F9BEA; --h1: #EE6C7C; --h2: #3FC1AE; --h3: #E5A548; --h4: #B98BDB; --h5: #86C25F;
    --shadow: 0 1px 2px rgb(0 0 0 / .3), 0 10px 30px rgb(0 0 0 / .35);
    color-scheme: dark;
  }
}
:root[data-theme="dark"] {
  --paper: #0F1A31; --sheet: #172642; --ink: #E7ECF6; --muted: #9AA7C2; --line: #273A5E;
  --blue: #5F8FE6; --blue-soft: #1F3561; --red: #EE5A6C; --on-blue: #08142B;
  color-scheme: dark;
}

* { box-sizing: border-box; }
html, body { height: 100%; }
body {
  margin: 0;
  background: var(--paper);
  color: var(--ink);
  font: 400 16px/1.45 var(--body);
  -webkit-font-smoothing: antialiased;
  -webkit-tap-highlight-color: transparent;
  overscroll-behavior: none;
  overflow: hidden;
}
button, input, textarea { font: inherit; color: inherit; }
button { cursor: pointer; }
:focus-visible { outline: 3px solid var(--blue); outline-offset: 2px; border-radius: 6px; }
.sr-only { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; }

/* ---------- Shell ---------- */
.app {
  position: relative;
  height: 100dvh;
  display: grid;
  grid-template-columns: minmax(0, 1fr);
  grid-template-rows: minmax(0, 1fr);
  overflow: clip;
}
.app::before {
  content: "";
  position: absolute;
  inset: 0 0 auto 0;
  height: calc(5px + env(safe-area-inset-top));
  background: var(--stripe);
  background-position: 0 100%;
  z-index: 5;
  pointer-events: none;
}
.rail, .thread {
  grid-area: 1 / 1;
  min-height: 0;
  display: flex;
  flex-direction: column;
  padding-top: calc(5px + env(safe-area-inset-top));
}
.thread {
  position: relative;
  background: var(--paper);
  transform: translateX(100%);
  transition: transform .28s cubic-bezier(.2, .8, .2, 1);
  z-index: 2;
}
.app[data-view="list"] .thread { visibility: hidden; transition: transform .28s cubic-bezier(.2, .8, .2, 1), visibility 0s .28s; }
.app[data-view="chat"] .thread { transform: none; }
.thread-empty { display: none; }

@media (min-width: 860px) {
  .app { grid-template-columns: minmax(320px, 380px) minmax(0, 1fr); }
  .rail { grid-area: 1 / 1; border-right: 1px solid var(--line); }
  .thread, .app[data-view="list"] .thread { grid-area: 1 / 2; transform: none; transition: none; visibility: visible; }
  .back { display: none !important; }
  .app[data-view="list"] .thread > :not(.thread-empty) { visibility: hidden; }
  .app[data-view="list"] .thread-empty {
    display: grid;
    place-content: center;
    justify-items: center;
    gap: 12px;
    position: absolute;
    inset: 0;
    color: var(--muted);
  }
  .thread-empty svg { width: 120px; }
  .thread-empty p { margin: 0; }
}

/* ---------- Rail ---------- */
.rail-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 14px 16px 10px;
}
.brand { display: flex; align-items: center; gap: 10px; }
.brand-mark { width: 32px; height: 32px; }
.brand h1 {
  margin: 0;
  font: 750 30px/1 var(--display);
  letter-spacing: -0.03em;
}
.me { border: 0; background: none; padding: 4px; border-radius: 50%; }

.avatar {
  --c: var(--h0);
  flex: none;
  width: 44px;
  height: 44px;
  border-radius: 50%;
  display: grid;
  place-items: center;
  background: var(--c);
  color: var(--sheet);
  font: 700 18px/1 var(--display);
  user-select: none;
}
.me .avatar { width: 38px; height: 38px; font-size: 16px; }
.avatar[data-hue="1"] { --c: var(--h1); }
.avatar[data-hue="2"] { --c: var(--h2); }
.avatar[data-hue="3"] { --c: var(--h3); }
.avatar[data-hue="4"] { --c: var(--h4); }
.avatar[data-hue="5"] { --c: var(--h5); }
.avatar.all {
  background: var(--stripe), var(--sheet);
  background-size: 40px 40px;
  box-shadow: inset 0 0 0 1px var(--line);
}
.avatar.all svg { width: 26px; height: 26px; padding: 3px; background: var(--sheet); border-radius: 50%; color: var(--blue); }

.link-status {
  margin: 0 12px 6px;
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 12px 14px;
  border: 1px solid var(--line);
  background: var(--sheet);
  border-radius: 14px;
  text-align: left;
}
.link-text { flex: 1; color: var(--muted); font-size: 15px; }
.link-text b { color: var(--ink); font-weight: 700; }
.link-action { color: var(--blue); font-weight: 700; font-size: 15px; }
.pulse {
  width: 10px; height: 10px; border-radius: 50%;
  background: var(--muted);
  opacity: .5;
  flex: none;
}
.link-status.live .pulse { background: var(--blue); opacity: 1; animation: pulse 2.4s ease-out infinite; }
@keyframes pulse {
  0% { box-shadow: 0 0 0 0 color-mix(in srgb, var(--blue) 45%, transparent); }
  80%, 100% { box-shadow: 0 0 0 9px transparent; }
}

.chats {
  list-style: none;
  margin: 0;
  padding: 4px 0 calc(16px + env(safe-area-inset-bottom));
  overflow-y: auto;
  flex: 1;
}
.chat {
  width: 100%;
  display: grid;
  grid-template-columns: auto 1fr auto;
  align-items: center;
  gap: 12px;
  padding: 10px 16px;
  border: 0;
  background: none;
  text-align: left;
}
.chat:hover { background: color-mix(in srgb, var(--blue-soft) 50%, transparent); }
.chat[aria-current="true"] { background: var(--blue-soft); }
.chat-main { min-width: 0; }
.chat-name {
  display: flex; align-items: center; gap: 6px;
  font: 650 17px/1.25 var(--display);
  letter-spacing: -0.01em;
}
.online-dot { width: 8px; height: 8px; border-radius: 50%; background: var(--h2); }
.chat-last {
  margin-top: 2px;
  color: var(--muted);
  font-size: 15px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.chat-side { display: grid; justify-items: end; gap: 4px; font-size: 13px; color: var(--muted); }
.badge {
  min-width: 22px; height: 22px; padding: 0 7px;
  border-radius: 11px;
  background: var(--red);
  color: #fff;
  font: 700 13px/22px var(--body);
  text-align: center;
}

.empty-rail {
  margin: 28px 24px;
  text-align: center;
  color: var(--muted);
}
.empty-rail svg { width: 140px; margin-bottom: 8px; }
.empty-rail h3 { margin: 0 0 6px; color: var(--ink); font: 650 20px/1.2 var(--display); }
.empty-rail p { margin: 0 auto 18px; max-width: 30ch; }

/* ---------- Thread ---------- */
.thread-head {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 10px 12px;
  border-bottom: 1px solid var(--line);
  background: var(--paper);
}
.back {
  border: 0; background: none;
  width: 40px; height: 40px;
  display: grid; place-items: center;
  border-radius: 50%;
  margin-right: -4px;
}
.back svg { width: 24px; }
.thread-head .avatar { width: 40px; height: 40px; font-size: 17px; }
.thread-title { min-width: 0; }
.thread-title h2 { margin: 0; font: 650 18px/1.2 var(--display); letter-spacing: -0.01em; }
.thread-title p { margin: 0; font-size: 14px; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }

.messages {
  flex: 1;
  overflow-y: auto;
  padding: 16px 12px 8px;
  display: flex;
  flex-direction: column;
  gap: 2px;
  overscroll-behavior: contain;
}
.day {
  align-self: center;
  margin: 14px 0 8px;
  padding: 3px 12px;
  border-radius: 999px;
  background: var(--sheet);
  border: 1px solid var(--line);
  color: var(--muted);
  font-size: 13px;
}
.msg {
  max-width: min(78%, 560px);
  align-self: flex-start;
  margin-top: 6px;
}
.msg.cont { margin-top: 0; }
.msg.mine { align-self: flex-end; }
.who {
  margin: 0 0 3px 12px;
  font: 650 14px/1.2 var(--display);
  color: var(--c, var(--h0));
}
.bubble {
  padding: 8px 12px 6px;
  border-radius: 18px;
  background: var(--sheet);
  border: 1px solid var(--line);
  overflow-wrap: anywhere;
}
.msg:not(.mine) .bubble { border-top-left-radius: 6px; }
.msg.mine .bubble {
  background: var(--blue);
  border-color: var(--blue);
  color: var(--on-blue);
  border-bottom-right-radius: 6px;
}
.msg.cont:not(.mine) .bubble { border-top-left-radius: 18px; }
.text { white-space: pre-wrap; font-size: 16.5px; line-height: 1.4; }
.meta {
  display: flex;
  justify-content: flex-end;
  align-items: center;
  gap: 6px;
  margin-top: 2px;
  font-size: 12px;
  color: var(--muted);
}
.msg.mine .meta { color: color-mix(in srgb, var(--on-blue) 78%, transparent); }
.meta svg { width: 16px; height: 16px; }
.msg.new { animation: arrive .32s cubic-bezier(.2, .8, .2, 1); }
@keyframes arrive { from { transform: translateY(8px) scale(.98); opacity: 0; } }

.thread-intro {
  margin: auto auto 0;
  max-width: 34ch;
  padding: 24px 12px;
  text-align: center;
  color: var(--muted);
}
.thread-intro svg { width: 96px; margin-bottom: 6px; }

.composer {
  display: flex;
  align-items: flex-end;
  gap: 8px;
  padding: 8px 10px calc(10px + env(safe-area-inset-bottom));
  border-top: 1px solid var(--line);
  background: var(--paper);
}
.composer textarea {
  flex: 1;
  resize: none;
  max-height: 140px;
  padding: 11px 16px;
  border-radius: 22px;
  border: 1px solid var(--line);
  background: var(--sheet);
  font-size: 16.5px;
  line-height: 1.35;
}
.composer textarea:focus-visible { outline: none; border-color: var(--blue); box-shadow: 0 0 0 3px var(--blue-soft); }
.send {
  flex: none;
  width: 46px; height: 46px;
  border-radius: 50%;
  border: 0;
  background: var(--blue);
  color: var(--on-blue);
  display: grid; place-items: center;
  transition: transform .12s;
}
.send:active { transform: scale(.92); }
.send:disabled { background: var(--line); color: var(--muted); }
.send svg { width: 22px; margin-left: 2px; }

/* ---------- Sheets ---------- */
.sheet {
  border: 0;
  padding: 0;
  width: 100%;
  max-width: 460px;
  max-height: 92dvh;
  margin: auto auto 0;
  border-radius: 22px 22px 0 0;
  background: var(--sheet);
  color: var(--ink);
  box-shadow: var(--shadow);
  overflow: hidden auto;
}
.sheet[open] { animation: rise .3s cubic-bezier(.2, .8, .2, 1); }
@keyframes rise { from { transform: translateY(40px); opacity: 0; } }
.sheet::backdrop { background: rgb(10 18 36 / .45); backdrop-filter: blur(2px); }
@media (min-width: 600px) {
  .sheet { margin: auto; border-radius: 22px; }
}
.airmail { height: 10px; background: var(--stripe); }
.sheet-body { padding: 22px 22px calc(22px + env(safe-area-inset-bottom)); }
.sheet h2 { margin: 0 0 6px; font: 700 26px/1.15 var(--display); letter-spacing: -0.02em; }
.step { margin: 0 0 4px; color: var(--red); font: 650 14px/1.2 var(--display); }
.hint { margin: 0 0 18px; color: var(--muted); }
.sheet input {
  width: 100%;
  padding: 13px 16px;
  border: 1px solid var(--line);
  border-radius: 14px;
  background: var(--paper);
  font-size: 18px;
}
.sheet input:focus-visible { outline: none; border-color: var(--blue); box-shadow: 0 0 0 3px var(--blue-soft); }
.hues { border: 0; padding: 0; margin: 18px 0 22px; }
.hues legend { padding: 0; margin-bottom: 10px; color: var(--muted); font-size: 15px; }
.hues div { display: flex; gap: 10px; flex-wrap: wrap; }
.hues label { position: relative; }
.hues input { position: absolute; opacity: 0; width: 1px; height: 1px; }
.hues .avatar { width: 40px; height: 40px; cursor: pointer; transition: transform .15s; }
.hues input:checked + .avatar { box-shadow: 0 0 0 3px var(--sheet), 0 0 0 5px var(--c); }
.hues input:focus-visible + .avatar { outline: 3px solid var(--blue); outline-offset: 4px; }

.primary, .secondary {
  width: 100%;
  padding: 14px 18px;
  border-radius: 14px;
  font: 650 17px/1.2 var(--display);
}
.primary { border: 0; background: var(--blue); color: var(--on-blue); }
.secondary { border: 1px solid var(--line); background: none; color: var(--ink); }
.row-actions { display: grid; gap: 10px; margin-top: 18px; }
.textlink {
  border: 0; background: none; padding: 6px 0;
  color: var(--blue); font-weight: 700; text-decoration: underline; text-underline-offset: 3px;
}

.options { display: grid; gap: 10px; margin: 4px 0 18px; }
.option {
  display: grid;
  grid-template-columns: 44px 1fr;
  align-items: center;
  gap: 14px;
  padding: 14px;
  border: 1px solid var(--line);
  border-radius: 16px;
  background: var(--paper);
  text-align: left;
}
.option:hover:not(:disabled) { border-color: var(--blue); }
.option:disabled { opacity: .6; cursor: not-allowed; }
.option svg { width: 44px; height: 44px; padding: 10px; border-radius: 12px; background: var(--blue-soft); color: var(--blue); }
.option b { display: block; font: 650 17px/1.25 var(--display); }
.option span span { display: block; margin-top: 2px; color: var(--muted); font-size: 14.5px; line-height: 1.35; }

/* The QR rides inside an airmail envelope border. */
.qr-frame {
  width: min(100%, 300px);
  margin: 6px auto 12px;
  padding: 12px;
  border-radius: 18px;
  background: var(--stripe), #fff;
  background-size: auto;
}
.qr-frame .qr { background: #fff; border-radius: 8px; padding: 4px; }
.qr-frame svg { display: block; width: 100%; height: auto; }
.waiting {
  display: flex; align-items: center; justify-content: center; gap: 10px;
  color: var(--muted);
  margin: 6px 0 0;
}
.dots { display: inline-flex; gap: 4px; }
.dots i { width: 6px; height: 6px; border-radius: 50%; background: var(--blue); animation: dot 1.2s infinite ease-in-out; }
.dots i:nth-child(2) { animation-delay: .15s; }
.dots i:nth-child(3) { animation-delay: .3s; }
@keyframes dot { 0%, 80%, 100% { opacity: .25; } 40% { opacity: 1; } }

.scanner {
  position: relative;
  aspect-ratio: 1;
  width: min(100%, 320px);
  margin: 6px auto 12px;
  border-radius: 18px;
  overflow: hidden;
  background: #0A1328;
}
.scanner video { width: 100%; height: 100%; object-fit: cover; display: block; }
.scanner::after {
  content: "";
  position: absolute;
  inset: 16%;
  border-radius: 14px;
  box-shadow: 0 0 0 999px rgb(10 19 40 / .35);
  outline: 3px solid #fff;
}
.scanner-msg { position: absolute; inset: auto 0 10px; text-align: center; color: #fff; font-size: 14px; z-index: 1; }
.paste textarea {
  width: 100%;
  min-height: 84px;
  padding: 12px;
  border-radius: 12px;
  border: 1px solid var(--line);
  background: var(--paper);
  font: 13px/1.4 ui-monospace, monospace;
  resize: vertical;
}
.error {
  margin: 12px 0 0;
  padding: 12px 14px;
  border-radius: 12px;
  background: color-mix(in srgb, var(--red) 12%, var(--sheet));
  color: var(--ink);
  border: 1px solid color-mix(in srgb, var(--red) 40%, transparent);
  font-size: 15px;
}
.center { text-align: center; }

.toast {
  position: fixed;
  left: 50%;
  bottom: calc(90px + env(safe-area-inset-bottom));
  transform: translate(-50%, 20px);
  padding: 10px 16px;
  border-radius: 999px;
  background: var(--ink);
  color: var(--paper);
  font-size: 15px;
  opacity: 0;
  pointer-events: none;
  transition: opacity .2s, transform .2s;
  z-index: 50;
  max-width: calc(100% - 32px);
}
.toast.show { opacity: 1; transform: translate(-50%, 0); }

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation: none !important; transition: none !important; }
}
