/* Color emoji for every device (Windows 10's font lacks emoji newer than 2019).
   Twemoji graphics (c) Twitter, Inc. and contributors, CC-BY 4.0; font: SIL OFL 1.1 (fonts/LICENSE-twemoji.txt). */
@font-face {
  font-family: "Twemoji";
  src: url("fonts/twemoji.woff2") format("woff2");
  font-display: swap;
  unicode-range: U+200D, U+203C, U+2049, U+20E3, U+2139, U+2194-2199, U+21A9-21AA, U+231A-231B,
    U+2328, U+23CF, U+23E9-23F3, U+23F8-23FA, U+24C2, U+25AA-25AB, U+25B6, U+25C0, U+25FB-25FE,
    U+2600-27BF, U+2934-2935, U+2B05-2B07, U+2B1B-2B1C, U+2B50, U+2B55, U+3030, U+303D, U+3297,
    U+3299, U+FE0F, U+1F000-1FAFF, U+E0020-E007F;
}

:root {
  --font: "Twemoji", -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, "Noto Color Emoji", "Segoe UI Emoji", sans-serif;
  --bar: #ffffff;
  --bar-text: #000000;
  --bar-sub: #707579;
  --bar-border: #dadce0;
  --accent: #3390ec;
  --wall: linear-gradient(160deg, #d3e2b6 0%, #a9cba0 35%, #c9d89a 70%, #93bd8f 100%);
  --doodle-img: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='160' height='160' viewBox='0 0 160 160'%3E%3Cg fill='none' stroke='%23304a20' stroke-opacity='0.16' stroke-width='2' stroke-linecap='round' stroke-linejoin='round'%3E%3Cpath d='M20 30c6-10 18-10 24 0M14 34h36'/%3E%3Ccircle cx='110' cy='26' r='9'/%3E%3Cpath d='M100 90l8-14 8 14zM30 110l6 6 12-14'/%3E%3Cpath d='M126 118c0-8 12-8 12 0s-12 14-12 14-12-6-12-14 12-8 12 0'/%3E%3Cpath d='M64 62h22v14H64zM70 62v-6h10v6'/%3E%3Cpath d='M78 138c4-6 12-6 16 0M140 66l6 6m0-6l-6 6M50 148l4-8 4 8'/%3E%3C/g%3E%3C/svg%3E");
  --in-bg: #ffffff;
  --out-bg: #e3fdd2;
  --text: #000000;
  --in-meta: #8f9aa3;
  --out-meta: #4fae4e;
  --in-accent: #3390ec;
  --out-accent: #4fae4e;
  --reply-in-bg: rgba(51, 144, 236, 0.08);
  --reply-out-bg: rgba(79, 174, 78, 0.12);
  --link: #1a7bd6;
  --shadow: 0 1px 2px rgba(16, 35, 47, 0.15);
  --pill-bg: rgba(48, 74, 40, 0.42);
  --pill-text: #ffffff;
  --menu-bg: #ffffff;
  --muted: #707579;
  --flash: rgba(51, 144, 236, 0.35);
  --hover: rgba(0, 0, 0, 0.05);
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bar: #17212b;
    --bar-text: #f5f5f5;
    --bar-sub: #6d7f8f;
    --bar-border: #0e1621;
    --accent: #6ab3f3;
    --wall: linear-gradient(160deg, #0e1621 0%, #12202d 50%, #0f1923 100%);
    --doodle-img: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='160' height='160' viewBox='0 0 160 160'%3E%3Cg fill='none' stroke='%23ffffff' stroke-opacity='0.045' stroke-width='2' stroke-linecap='round' stroke-linejoin='round'%3E%3Cpath d='M20 30c6-10 18-10 24 0M14 34h36'/%3E%3Ccircle cx='110' cy='26' r='9'/%3E%3Cpath d='M100 90l8-14 8 14zM30 110l6 6 12-14'/%3E%3Cpath d='M126 118c0-8 12-8 12 0s-12 14-12 14-12-6-12-14 12-8 12 0'/%3E%3Cpath d='M64 62h22v14H64zM70 62v-6h10v6'/%3E%3Cpath d='M78 138c4-6 12-6 16 0M140 66l6 6m0-6l-6 6M50 148l4-8 4 8'/%3E%3C/g%3E%3C/svg%3E");
    --in-bg: #182533;
    --out-bg: #2b5278;
    --text: #f5f5f5;
    --in-meta: #6d7f8f;
    --out-meta: #7da8d3;
    --in-accent: #6ab3f3;
    --out-accent: #a3d2ff;
    --reply-in-bg: rgba(106, 179, 243, 0.10);
    --reply-out-bg: rgba(255, 255, 255, 0.08);
    --link: #6ab3f3;
    --shadow: 0 1px 2px rgba(0, 0, 0, 0.3);
    --pill-bg: rgba(30, 44, 58, 0.75);
    --menu-bg: #1e2c3a;
    --muted: #8696a4;
    --flash: rgba(106, 179, 243, 0.35);
    --hover: rgba(255, 255, 255, 0.05);
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body {
  font-family: var(--font);
  font-size: 15px;
  line-height: 1.35;
  color: var(--text);
  background: var(--bar);
  -webkit-font-smoothing: antialiased;
}
button { font: inherit; color: inherit; }
[hidden] { display: none !important; }
svg { fill: currentColor; }

/* ---- layout ------------------------------------------------------------- */
.app {
  height: 100dvh;
  display: flex;
  flex-direction: column;
  position: relative;
  background-color: #a9cba0;
  background-image: var(--doodle-img), var(--wall);
  background-size: 160px 160px, cover;
}
@media (prefers-color-scheme: dark) {
  .app { background-color: #0e1621; }
}

.topbar {
  flex: none;
  height: 56px;
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 0 8px 0 16px;
  background: var(--bar);
  color: var(--bar-text);
  border-bottom: 1px solid var(--bar-border);
  position: relative;
  z-index: 5;
}
.avatar {
  width: 40px; height: 40px; border-radius: 50%;
  flex: none;
  display: grid; place-items: center;
  color: #fff; font-weight: 600; font-size: 16px;
  background: linear-gradient(180deg, #ff885e, #ff516a);
}
.peer { flex: 1; min-width: 0; }
.peer-name { font-weight: 600; font-size: 16px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.peer-status { font-size: 13.5px; color: var(--bar-sub); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.peer-status.live { color: var(--accent); }
.icon-btn {
  width: 40px; height: 40px; border: 0; border-radius: 50%;
  background: transparent; color: var(--bar-sub);
  display: grid; place-items: center; cursor: pointer; flex: none;
}
.icon-btn:hover { background: var(--hover); }
.icon-btn svg { width: 24px; height: 24px; }

.menu {
  position: absolute; right: 8px; top: 52px;
  min-width: 220px;
  background: var(--menu-bg);
  border-radius: 10px;
  box-shadow: 0 4px 24px rgba(0, 0, 0, 0.22);
  padding: 6px 0;
}
.menu-info { padding: 8px 16px; font-size: 13px; color: var(--muted); white-space: pre-line; }
.menu button {
  width: 100%; text-align: left; border: 0; background: none;
  padding: 10px 16px; cursor: pointer; color: #e53935;
}
.menu button:hover { background: var(--hover); }
.menu-credit { padding: 6px 16px 4px; font-size: 11px; color: var(--muted); border-top: 1px solid var(--bar-border); margin-top: 4px; }

/* ---- search ------------------------------------------------------------- */
.searchbar {
  flex: none;
  display: flex; align-items: center; gap: 8px;
  padding: 8px 8px 8px 16px;
  background: var(--bar);
  border-bottom: 1px solid var(--bar-border);
  position: relative; z-index: 4;
}
.searchbar input {
  flex: 1; min-width: 0;
  height: 38px; border-radius: 19px;
  border: 1px solid var(--bar-border);
  background: var(--hover);
  color: var(--bar-text);
  padding: 0 16px; font: inherit; outline: none;
}
.searchbar input:focus { border-color: var(--accent); }
.search-results {
  position: absolute; left: 0; right: 0; top: 100%;
  max-height: min(60vh, 480px); overflow-y: auto;
  background: var(--bar);
  box-shadow: 0 6px 16px rgba(0, 0, 0, 0.18);
}
.search-results:empty { display: none; }
.sr-item {
  display: block; width: 100%; text-align: left; border: 0; background: none; cursor: pointer;
  padding: 10px 16px; border-bottom: 1px solid var(--bar-border); color: var(--bar-text);
}
.sr-item:hover { background: var(--hover); }
.sr-head { display: flex; justify-content: space-between; gap: 8px; font-size: 13px; color: var(--muted); }
.sr-head b { color: var(--bar-text); font-weight: 600; }
.sr-text { font-size: 14px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.sr-text mark { background: rgba(255, 200, 0, 0.45); color: inherit; border-radius: 2px; }
.sr-empty { padding: 14px 16px; color: var(--muted); font-size: 14px; }

/* ---- chat --------------------------------------------------------------- */
.chat {
  flex: 1;
  overflow-y: auto;
  overflow-x: hidden;
  position: relative;
  overscroll-behavior: contain;
  overflow-anchor: none;
  -webkit-overflow-scrolling: touch;
}
.messages {
  max-width: 760px;
  margin: 0 auto;
  padding: 8px 16px 12px;
}
.sticky-date {
  position: sticky; top: 8px; height: 0; z-index: 3;
  display: flex; justify-content: center;
  pointer-events: none;
  transition: opacity .3s;
}
.sticky-date.fade { opacity: 0; }

.day-pill { display: none; justify-content: center; margin: 10px 0 8px; }
.row.day-start .day-pill { display: flex; }
.day-pill span, .sticky-date span, .loader span {
  background: var(--pill-bg); color: var(--pill-text);
  font-size: 13.5px; font-weight: 500;
  padding: 2px 10px; border-radius: 12px;
  -webkit-backdrop-filter: blur(6px); backdrop-filter: blur(6px);
}
.loader { display: flex; justify-content: center; padding: 10px; }
.loader span::after { content: "Loading…"; }

.row { display: flex; flex-direction: column; margin-top: 2px; }
.row.grp-start { margin-top: 8px; }
.row.day-start { margin-top: 0; }
.row.in .bubble { align-self: flex-start; }
.row.out .bubble { align-self: flex-end; }

.bubble {
  position: relative;
  max-width: min(480px, 85%);
  min-width: 56px;
  padding: 6px 9px 7px 10px;
  border-radius: 14px;
  background: var(--in-bg);
  box-shadow: var(--shadow);
  word-wrap: break-word;
  overflow-wrap: anywhere;
}
.row.out .bubble { background: var(--out-bg); }
.row.in.grp-end .bubble { border-bottom-left-radius: 4px; }
.row.out.grp-end .bubble { border-bottom-right-radius: 4px; }
.row.in:not(.grp-start) .bubble { border-top-left-radius: 6px; }
.row.out:not(.grp-start) .bubble { border-top-right-radius: 6px; }
.row.in:not(.grp-end) .bubble { border-bottom-left-radius: 6px; }
.row.out:not(.grp-end) .bubble { border-bottom-right-radius: 6px; }

/* tail */
.row.grp-end .bubble::after {
  content: ""; position: absolute; bottom: 0;
  width: 11px; height: 18px;
  -webkit-mask: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 11 18'%3E%3Cpath d='M0 0v18h11C6 17 1 12 0 0z'/%3E%3C/svg%3E") no-repeat;
  mask: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 11 18'%3E%3Cpath d='M0 0v18h11C6 17 1 12 0 0z'/%3E%3C/svg%3E") no-repeat;
}
.row.out.grp-end .bubble::after { right: -10px; background: var(--out-bg); }
.row.in.grp-end .bubble::after { left: -10px; background: var(--in-bg); transform: scaleX(-1); }
.row.grp-end .bubble.bare::after { display: none; }

.text { white-space: pre-wrap; }
.text a { color: var(--link); text-decoration: none; }
.text a:hover { text-decoration: underline; }

.meta {
  float: right;
  position: relative;
  top: 6px;
  margin: -4px -3px 0 12px;
  display: inline-flex; align-items: center; gap: 3px;
  font-size: 12px; line-height: 1;
  color: var(--in-meta);
  white-space: nowrap;
  user-select: none;
  height: 18px;
}
.row.out .meta { color: var(--out-meta); }
.meta .edited { font-style: italic; margin-right: 2px; }
.meta svg { width: 18px; height: 18px; }

/* reply quote */
.reply {
  display: block; width: 100%; text-align: left;
  border: 0; cursor: pointer;
  margin: 1px 0 5px;
  padding: 3px 8px 4px 9px;
  border-left: 3px solid var(--in-accent);
  border-radius: 4px 6px 6px 4px;
  background: var(--reply-in-bg);
  font-size: 14px; line-height: 1.3;
  min-width: 0;
}
.row.out .reply { border-left-color: var(--out-accent); background: var(--reply-out-bg); }
.reply b { display: block; color: var(--in-accent); font-weight: 600; font-size: 13.5px; }
.row.out .reply b { color: var(--out-accent); }
.reply span { display: block; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; opacity: .85; }

/* media */
.bubble.has-media { padding: 0; overflow: hidden; }
.bubble.has-media::after { display: none; }
.bubble.has-media .reply { margin: 6px 8px 6px; width: auto; }
.bubble.has-media .text { padding: 6px 9px 7px 10px; }
.photo {
  display: block; position: relative;
  width: min(360px, 70vw);
  max-width: 100%;
  background: rgba(0, 0, 0, 0.08);
  cursor: zoom-in;
}
.photo img, .photo video { display: block; width: 100%; height: 100%; object-fit: cover; }
.photo video { cursor: auto; background: #000; }
.media-meta {
  position: absolute; right: 6px; bottom: 6px;
  background: rgba(0, 0, 0, 0.45); color: #fff;
  border-radius: 10px; padding: 3px 7px;
  font-size: 12px; line-height: 1; display: inline-flex; align-items: center; gap: 3px;
}
.media-meta svg { width: 16px; height: 16px; }
.media-missing {
  display: grid; place-items: center;
  aspect-ratio: 4 / 3;
  color: var(--muted); font-size: 13px;
}

.file {
  display: flex; align-items: center; gap: 12px;
  padding: 2px 0 4px;
  color: inherit; text-decoration: none;
  min-width: 220px;
}
.file-icon {
  width: 48px; height: 48px; border-radius: 50%; flex: none;
  display: grid; place-items: center;
  background: var(--in-accent); color: #fff;
}
.row.out .file-icon { background: var(--out-meta); }
.file-icon svg { width: 24px; height: 24px; }
.file-info { min-width: 0; }
.file-name { font-weight: 500; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.file-size { font-size: 13px; color: var(--in-meta); }
.row.out .file-size { color: var(--out-meta); }
.audio { padding: 2px 0; }
.audio .file-name { font-size: 14px; margin-bottom: 4px; }
.audio audio { width: min(280px, 64vw); height: 36px; display: block; }

/* emoji-only messages: big, no bubble */
.bubble.bare { background: transparent !important; box-shadow: none; padding: 0 2px; }
.bubble.bare .text { font-size: 40px; line-height: 1.15; }
.bubble.bare .meta {
  float: none; position: static; margin: 2px 0 0;
  background: var(--pill-bg); color: var(--pill-text) !important;
  padding: 0 6px; border-radius: 10px;
}
.row.out .bubble.bare .meta { margin-left: auto; display: flex; width: fit-content; }

.row.flash { animation: flash 1.8s ease-out; border-radius: 10px; }
@keyframes flash { 0%, 35% { background: var(--flash); } 100% { background: transparent; } }

.row.new { animation: pop .22s ease-out; }
@keyframes pop { from { opacity: 0; transform: translateY(8px); } to { opacity: 1; transform: none; } }

/* ---- bottom ------------------------------------------------------------- */
.to-bottom {
  position: absolute; right: 18px; bottom: 58px; z-index: 4;
  width: 48px; height: 48px; border-radius: 50%; border: 0; cursor: pointer;
  background: var(--bar); color: var(--bar-sub);
  box-shadow: 0 1px 4px rgba(0, 0, 0, 0.25);
  display: grid; place-items: center;
}
.to-bottom svg { width: 28px; height: 28px; }
.badge {
  position: absolute; top: -6px; right: -2px;
  min-width: 22px; height: 22px; padding: 0 6px; border-radius: 11px;
  background: var(--accent); color: #fff; font-size: 12.5px; font-weight: 600;
  display: grid; place-items: center;
}
.readonly {
  flex: none;
  text-align: center;
  padding: 12px 16px calc(12px + env(safe-area-inset-bottom));
  background: var(--bar);
  color: var(--muted);
  font-size: 14px;
  border-top: 1px solid var(--bar-border);
}

/* ---- lightbox ----------------------------------------------------------- */
.lightbox {
  position: fixed; inset: 0; z-index: 20;
  background: rgba(0, 0, 0, 0.9);
  display: grid; place-items: center; cursor: zoom-out;
}
.lightbox img { max-width: 96vw; max-height: 94vh; object-fit: contain; }

@media (max-width: 600px) {
  .messages { padding: 6px 10px 10px; }
  .bubble { max-width: 88%; }
  .topbar { padding-left: 12px; }
}

/* ---- login -------------------------------------------------------------- */
.login-body {
  display: grid; place-items: center; min-height: 100dvh; padding: 16px;
  background: var(--wall);
}
.login-card {
  width: 100%; max-width: 360px;
  background: var(--bar); color: var(--bar-text);
  border-radius: 16px; padding: 32px 24px 24px;
  box-shadow: 0 8px 32px rgba(0, 0, 0, 0.18);
  display: flex; flex-direction: column; gap: 12px; text-align: center;
}
.login-logo {
  width: 72px; height: 72px; border-radius: 50%; margin: 0 auto 4px;
  background: var(--accent); color: #fff; display: grid; place-items: center;
}
.login-logo svg { width: 34px; height: 34px; }
.login-card h1 { font-size: 20px; margin: 0; }
.login-sub { margin: 0 0 8px; color: var(--muted); font-size: 14px; }
.login-card input {
  height: 48px; border-radius: 10px; border: 1px solid var(--bar-border);
  background: transparent; color: inherit; padding: 0 14px; font: inherit; font-size: 16px; outline: none;
}
.login-card input:focus { border-color: var(--accent); box-shadow: 0 0 0 1px var(--accent); }
.login-card button {
  height: 48px; border: 0; border-radius: 10px; cursor: pointer;
  background: var(--accent); color: #fff; font-weight: 600; font-size: 15px;
}
.login-card button:hover { filter: brightness(1.05); }
.login-error { margin: 0; color: #e53935; font-size: 14px; }
