:root {
  --bg: #e9edf5;
  --panel: #ffffff;
  --text: #17191f;
  --muted: #6b7280;
  --accent: #5a6cf2;
  --in: #ffffff;
  --out: #dfe5ff;
  --out-meta: #5a6cf2;
  --line: #dde1ea;
  --warn: #c2410c;
  --on: #16a34a;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0e1220;
    --panel: #171c2e;
    --text: #e8eaf2;
    --muted: #8b93a7;
    --accent: #8191ff;
    --in: #1d2337;
    --out: #3a479e;
    --out-meta: #c3cbff;
    --line: #262d44;
    --warn: #fb923c;
    --on: #4ade80;
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body {
  font: 16px/1.4 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  background: var(--bg);
  color: var(--text);
  -webkit-text-size-adjust: 100%;
  -webkit-tap-highlight-color: transparent;
}
[hidden] { display: none !important; }
button { font: inherit; color: inherit; background: none; border: 0; cursor: pointer; }

.icon { display: grid; place-items: center; width: 40px; height: 40px; border-radius: 50%; flex: none; color: var(--muted); }
.icon svg { width: 24px; height: 24px; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }
.icon:active { background: var(--line); }

/* ---------- login ---------- */
#login { min-height: 100%; display: grid; place-items: center; padding: 16px; }
#lf { width: 100%; max-width: 320px; display: grid; gap: 12px; text-align: center; }
#lf img { margin: 0 auto; border-radius: 18px; }
#lf h1 { margin: 4px 0 12px; font-size: 24px; font-weight: 600; }
#lf input {
  font: inherit; padding: 12px 14px; border-radius: 12px;
  border: 1px solid var(--line); background: var(--panel); color: var(--text);
}
#lf input:focus { outline: 2px solid var(--accent); outline-offset: -1px; }
#lf button { padding: 12px; border-radius: 12px; background: var(--accent); color: #fff; font-weight: 600; }
#lf button:disabled { opacity: .6; }
#lerr { margin: 0; min-height: 1.4em; color: var(--warn); font-size: 14px; }

/* ---------- chat ---------- */
#chat { height: 100%; height: 100dvh; display: flex; flex-direction: column; max-width: 820px; margin: 0 auto; position: relative; }

header {
  display: flex; align-items: center; gap: 12px;
  padding: 8px 8px 8px 16px; padding-top: max(8px, env(safe-area-inset-top));
  background: var(--panel); border-bottom: 1px solid var(--line);
}
.av {
  width: 40px; height: 40px; border-radius: 50%; flex: none; display: grid; place-items: center;
  background: var(--accent); color: #fff; font-weight: 600;
}
.who { flex: 1; min-width: 0; display: flex; flex-direction: column; }
.who b { font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.who small { color: var(--muted); font-size: 13px; }
.who small.on { color: var(--accent); }
.who small.warn { color: var(--warn); }

#list {
  flex: 1; overflow-y: auto; overscroll-behavior: contain;
  padding: 8px 12px; display: flex; flex-direction: column; gap: 3px;
}
.day {
  align-self: center; margin: 10px 0 6px; padding: 2px 10px; border-radius: 10px;
  font-size: 13px; color: var(--muted); background: var(--panel);
}
.msg {
  max-width: min(78%, 520px); padding: 6px 10px 6px; border-radius: 16px;
  background: var(--in); align-self: flex-start; position: relative;
  box-shadow: 0 1px 1px rgb(0 0 0 / .06); overflow-wrap: anywhere;
}
.msg.out { background: var(--out); align-self: flex-end; }
.msg.flash { animation: flash 1s; }
@keyframes flash { 50% { filter: brightness(.85); } }
.tx { white-space: pre-wrap; }
.tx a { color: var(--accent); }
.msg.out .tx a { color: inherit; }
.q {
  border-left: 3px solid var(--accent); padding: 1px 8px; margin: 2px 0 4px; border-radius: 4px;
  font-size: 14px; color: var(--muted); background: rgb(127 127 127 / .08); cursor: pointer;
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis; max-width: 100%;
}
.ph { display: block; max-width: 100%; height: auto; border-radius: 12px; margin: 0 -6px 2px; max-height: 60vh; object-fit: cover; cursor: zoom-in; background: var(--line); }
.msg:has(.ph) { padding-top: 4px; }
.meta { float: right; margin: 6px 0 -4px 10px; font-size: 11px; color: var(--muted); white-space: nowrap; user-select: none; }
.msg.out .meta { color: var(--out-meta); }
.tick { margin-left: 3px; letter-spacing: -3px; }
.tick.fail { color: var(--warn); letter-spacing: 0; cursor: pointer; font-weight: 700; }

@media (pointer: coarse) {
  .msg { -webkit-user-select: none; user-select: none; -webkit-touch-callout: none; }
}

#down {
  position: absolute; right: 16px; bottom: 76px; background: var(--panel);
  box-shadow: 0 2px 8px rgb(0 0 0 / .18); width: 44px; height: 44px;
}

#reply {
  display: flex; align-items: center; gap: 8px; padding: 6px 8px 6px 16px;
  background: var(--panel); border-top: 1px solid var(--line);
}
#reply div { flex: 1; min-width: 0; border-left: 3px solid var(--accent); padding-left: 8px; display: flex; flex-direction: column; }
#reply b { color: var(--accent); font-size: 13px; }
#reply span { font-size: 14px; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }

#cf {
  display: flex; align-items: flex-end; gap: 4px; padding: 6px;
  padding-bottom: max(6px, env(safe-area-inset-bottom));
  background: var(--panel); border-top: 1px solid var(--line);
}
#reply:not([hidden]) + #cf { border-top: 0; }
#ta {
  flex: 1; resize: none; border: 0; outline: 0; background: transparent; color: var(--text);
  font: inherit; padding: 9px 6px; max-height: 160px; line-height: 1.4;
}
#send { color: var(--accent); }

#ctx {
  position: fixed; z-index: 10; background: var(--panel); border-radius: 12px; padding: 4px;
  box-shadow: 0 4px 20px rgb(0 0 0 / .22); display: flex; flex-direction: column; min-width: 140px;
}
#ctx button { text-align: left; padding: 10px 14px; border-radius: 8px; }
#ctx button:hover, #ctx button:active { background: var(--line); }

#lb { position: fixed; inset: 0; z-index: 20; background: rgb(0 0 0 / .92); display: grid; place-items: center; cursor: zoom-out; }
#lb img { max-width: 100%; max-height: 100%; object-fit: contain; }

/* ---------- calls ---------- */
header .icon { color: var(--accent); }
header #out { color: var(--muted); }
#call {
  position: fixed; inset: 0; z-index: 30; background: #0b0e17; color: #fff;
  display: flex; flex-direction: column; align-items: center; overflow: hidden;
}
#rv { position: absolute; inset: 0; width: 100%; height: 100%; object-fit: cover; background: #000; display: none; }
#call.has-video #rv { display: block; }
#lv {
  position: absolute; z-index: 2; top: max(16px, env(safe-area-inset-top)); right: 16px;
  width: min(28vw, 150px); aspect-ratio: 3 / 4; object-fit: cover; border-radius: 14px;
  background: #222; transform: scaleX(-1); box-shadow: 0 4px 16px rgb(0 0 0 / .4); display: none;
}
#lv.back { transform: none; }
#call.video #lv:not(.off) { display: block; }
.cinfo {
  position: relative; z-index: 1; margin-top: 18vh; display: flex; flex-direction: column;
  align-items: center; gap: 6px; text-align: center; text-shadow: 0 1px 4px rgb(0 0 0 / .6);
}
.cinfo .av { width: 104px; height: 104px; font-size: 42px; margin-bottom: 10px; }
.cinfo b { font-size: 26px; font-weight: 600; }
.cinfo span { font-size: 16px; opacity: .8; font-variant-numeric: tabular-nums; }
#call.has-video .cinfo { margin-top: max(20px, env(safe-area-inset-top)); align-self: flex-start; align-items: flex-start; padding-left: 20px; }
#call.has-video .cinfo .av { display: none; }
#call.has-video .cinfo b { font-size: 18px; }
.cbar {
  position: absolute; z-index: 2; left: 0; right: 0; bottom: max(32px, env(safe-area-inset-bottom));
  display: flex; justify-content: center; gap: 22px;
}
#cin { gap: 90px; }
.cb {
  width: 62px; height: 62px; border-radius: 50%; display: grid; place-items: center;
  background: rgb(255 255 255 / .2); color: #fff; backdrop-filter: blur(8px); -webkit-backdrop-filter: blur(8px);
}
.cb svg { width: 28px; height: 28px; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }
.cb.off { background: #fff; color: #111; }
.cb.hang { background: #e5484d; }
.cb.acc { background: #2fbf71; animation: pulse 1.4s infinite; }
.cb.hang .rot { transform: rotate(135deg); transform-origin: 12px 12px; }
#call:not(.video) #ccam, #call:not(.video) #cflip { display: none; }
@keyframes pulse { 50% { transform: scale(1.08); } }
