:root {
  --bg: #e9eef0;
  --panel: #ffffff;
  --panel-2: #f5f7f8;
  --line: #d9e0e3;
  --text: #16212a;
  --muted: #5b6a73;
  --accent: #127a6a;
  --accent-ink: #ffffff;
  --accent-soft: #d8efe9;
  --in: #ffffff;
  --out: #d9f0e9;
  --code-bg: rgba(20, 40, 50, .07);
  --quote: #127a6a;
  --warn-bg: #fff6e0;
  --warn-line: #e8c46a;
  --danger: #b3261e;
  --shadow: 0 1px 2px rgba(10, 30, 40, .08), 0 8px 30px rgba(10, 30, 40, .08);
  --radius: 14px;
  --mono: ui-monospace, "SF Mono", "Cascadia Mono", "Roboto Mono", Menlo, Consolas, monospace;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0d1214;
    --panel: #151c1f;
    --panel-2: #1a2326;
    --line: #26323a;
    --text: #e3eaec;
    --muted: #8fa0a7;
    --accent: #2db39a;
    --accent-ink: #062520;
    --accent-soft: #163a33;
    --in: #1b2528;
    --out: #173b35;
    --code-bg: rgba(255, 255, 255, .08);
    --quote: #2db39a;
    --warn-bg: #2b2414;
    --warn-line: #7a6223;
    --danger: #f2b8b5;
    --shadow: 0 1px 2px rgba(0, 0, 0, .4), 0 8px 30px rgba(0, 0, 0, .35);
  }
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }
html, body { height: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 15px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, "Noto Sans", "DejaVu Sans", "Liberation Sans", sans-serif;
  -webkit-font-smoothing: antialiased;
}
code, pre { font-family: var(--mono); font-size: .92em; }
button { font: inherit; color: inherit; cursor: pointer; }
a { color: var(--accent); }
.vh { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; }
.nojs { padding: 24px; text-align: center; }

/* ---------- login ---------- */
.login { min-height: 100%; display: grid; place-items: center; padding: 24px 16px; }
.card {
  width: 100%; max-width: 720px; background: var(--panel); border-radius: 20px;
  box-shadow: var(--shadow); padding: 28px 28px 22px;
}
.brand { display: flex; align-items: center; gap: 14px; margin-bottom: 22px; }
.brand h1 { margin: 0; font-size: 24px; letter-spacing: -.01em; }
.sub { margin: 2px 0 0; color: var(--muted); }
.logo { width: 44px; height: 44px; display: block; flex: none; }
.login-grid { display: grid; grid-template-columns: 200px 1fr; gap: 28px; align-items: start; }
.qr { margin: 0; text-align: center; }
.qr img {
  width: 184px; height: 184px; border-radius: 12px; background: #fff; padding: 6px;
  border: 1px solid var(--line); display: block; margin: 0 auto;
}
.qr figcaption { margin-top: 10px; font-size: 14px; }
.steps ol { margin: 0 0 18px; padding-left: 20px; }
.steps li { margin-bottom: 8px; }
.steps li::marker { color: var(--accent); font-weight: 600; }
.steps code { background: var(--code-bg); padding: 1px 5px; border-radius: 5px; }
#login-form { display: flex; gap: 10px; }
#code {
  flex: 1; min-width: 0; font: 600 22px/1 var(--mono); letter-spacing: .14em; text-transform: uppercase;
  padding: 12px 14px; border-radius: 12px; border: 1.5px solid var(--line);
  background: var(--panel-2); color: var(--text);
}
#code:focus { outline: none; border-color: var(--accent); background: var(--panel); }
.btn-primary {
  border: 0; border-radius: 12px; padding: 0 22px; background: var(--accent); color: var(--accent-ink);
  font-weight: 600;
}
.btn-primary:disabled { opacity: .6; cursor: default; }
.error { color: var(--danger); margin: 10px 0 0; }
.notice { color: var(--muted); margin: 10px 0 0; }
.warn {
  margin-top: 22px; padding: 12px 14px; border-radius: 12px; background: var(--warn-bg);
  border: 1px solid var(--warn-line); font-size: 14px;
}
.foot { margin-top: 14px; color: var(--muted); font-size: 13px; }

@media (max-width: 600px) {
  .card { padding: 22px 18px 18px; border-radius: 16px; }
  .login-grid { grid-template-columns: 1fr; gap: 18px; }
}

/* ---------- chat ---------- */
.chat {
  height: 100vh; height: 100dvh; max-width: 880px; margin: 0 auto;
  display: grid; grid-template-rows: auto 1fr auto; background: var(--bg);
}
@media (min-width: 900px) {
  .chat { box-shadow: var(--shadow); }
}
.bar {
  display: flex; align-items: center; justify-content: space-between; gap: 12px;
  padding: 10px 14px; background: var(--panel); border-bottom: 1px solid var(--line);
}
.peer { display: flex; align-items: center; gap: 10px; min-width: 0; }
.peer-text { min-width: 0; }
.avatar { width: 38px; height: 38px; display: block; flex: none; }
.peer-name { font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.peer-status { color: var(--muted); font-size: 13px; }
.bar-right { display: flex; align-items: center; gap: 10px; flex: none; }
.timer {
  font: 600 14px/1 var(--mono); color: var(--muted); padding: 6px 8px; border-radius: 8px;
  background: var(--panel-2); min-width: 54px; text-align: center;
}
.timer.low { color: var(--danger); }
.btn-ghost {
  border: 1px solid var(--line); background: transparent; border-radius: 10px; padding: 6px 12px;
}
.btn-ghost:hover { background: var(--panel-2); }

.feed {
  overflow-y: auto; padding: 16px 14px 8px; display: flex; flex-direction: column; gap: 6px;
  overscroll-behavior: contain;
}
.empty { margin: auto; color: var(--muted); text-align: center; max-width: 340px; font-size: 14px; }
.msg {
  max-width: min(78%, 560px); padding: 7px 11px 6px; border-radius: var(--radius);
  box-shadow: 0 1px 1px rgba(0,0,0,.06); word-wrap: break-word; overflow-wrap: anywhere;
}
.msg.in { align-self: flex-start; background: var(--in); border-bottom-left-radius: 5px; }
.msg.out { align-self: flex-end; background: var(--out); border-bottom-right-radius: 5px; }
.msg .text { white-space: pre-wrap; }
.msg .meta { float: right; margin: 6px 0 -2px 10px; font-size: 11.5px; color: var(--muted); line-height: 1; }
.msg::after { content: ""; display: block; clear: both; }
.msg code { background: var(--code-bg); padding: 1px 4px; border-radius: 4px; }
.msg pre {
  margin: 4px 0; padding: 8px 10px; background: var(--code-bg); border-radius: 8px;
  overflow-x: auto; white-space: pre;
}
.msg pre code { background: none; padding: 0; }
.msg blockquote {
  margin: 4px 0; padding: 2px 0 2px 10px; border-left: 3px solid var(--quote);
}
.msg a { word-break: break-all; }
.msg .tag { color: var(--accent); }
.spoiler {
  background: var(--muted); color: transparent; border-radius: 3px; cursor: pointer;
  transition: background .15s, color .15s; user-select: none;
}
.spoiler a, .spoiler code { color: transparent; }
.spoiler.shown { background: var(--code-bg); color: inherit; user-select: auto; cursor: auto; }
.spoiler.shown a { color: var(--accent); }
.spoiler.shown code { color: inherit; }
.att {
  display: flex; gap: 10px; align-items: center; padding: 8px 10px; border-radius: 10px;
  background: var(--code-bg); margin-bottom: 4px;
}
.att-icon {
  width: 34px; height: 34px; border-radius: 50%; background: var(--accent); color: var(--accent-ink);
  display: grid; place-items: center; font-weight: 700; font-size: 12px; flex: none;
}
.att-name { font-weight: 600; }
.att-note { font-size: 12.5px; color: var(--muted); }

.composer { background: var(--panel); border-top: 1px solid var(--line); padding: 6px 12px 8px; }
.toolbar { display: flex; gap: 2px; flex-wrap: wrap; margin-bottom: 4px; }
.toolbar button {
  border: 0; background: transparent; border-radius: 8px; min-width: 32px; height: 30px; padding: 0 8px;
  color: var(--muted);
}
.toolbar button:hover { background: var(--panel-2); color: var(--text); }
.tb-spoiler { background: var(--muted); color: var(--panel); border-radius: 3px; padding: 0 3px; font-size: 12px; }
.tb-link { text-decoration: underline; font-size: 13px; }
.send-row { display: flex; align-items: flex-end; gap: 8px; }
#input {
  flex: 1; resize: none; max-height: 40vh; min-height: 42px; padding: 10px 12px; border-radius: 12px;
  border: 1.5px solid var(--line); background: var(--panel-2); color: var(--text); font: inherit;
}
#input:focus { outline: none; border-color: var(--accent); background: var(--panel); }
.btn-send {
  width: 42px; height: 42px; border: 0; border-radius: 50%; background: var(--accent);
  color: var(--accent-ink); display: grid; place-items: center; flex: none;
}
.btn-send:disabled { opacity: .5; cursor: default; }
.send-error { color: var(--danger); margin: 6px 2px 0; font-size: 13px; }
.hint { margin: 6px 2px 0; color: var(--muted); font-size: 12px; }
.hint code { background: var(--code-bg); padding: 0 4px; border-radius: 4px; }
@media (max-width: 600px) {
  .hint { display: none; }
  .msg { max-width: 88%; }
}

/* ---------- files ---------- */
.chat { position: relative; }
.att-body { min-width: 0; flex: 1; }
.att-name { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.att-icon.emoji { background: transparent; font-size: 26px; }
.att-btn {
  flex: none; border: 0; border-radius: 9px; padding: 6px 12px; background: var(--accent);
  color: var(--accent-ink); font-size: 13px; font-weight: 600;
}
.att-btn.ghost { background: transparent; color: var(--muted); border: 1px solid var(--line); }
.att-btn:disabled { opacity: .6; cursor: default; }
.att-err { font-size: 12.5px; color: var(--danger); }
.bar-track { height: 4px; border-radius: 2px; background: var(--line); margin-top: 5px; overflow: hidden; }
.bar-track i { display: block; height: 100%; width: 0; background: var(--accent); transition: width .2s; }
.msg.pending .meta { display: none; }
.msg.failed .att-note { color: var(--danger); }
.msg.failed .bar-track { display: none; }
.msg .text.muted { color: var(--muted); }

.tray { display: flex; flex-wrap: wrap; gap: 6px; margin: 2px 0 6px; }
.chip {
  display: flex; align-items: center; gap: 6px; max-width: 100%; padding: 4px 4px 4px 10px;
  border-radius: 999px; background: var(--accent-soft); font-size: 13px;
}
.chip.bad { background: var(--warn-bg); outline: 1px solid var(--warn-line); }
.chip-name { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; max-width: 260px; font-weight: 600; }
.chip-size { color: var(--muted); white-space: nowrap; }
.chip-x {
  border: 0; background: transparent; width: 22px; height: 22px; border-radius: 50%; line-height: 1;
  color: var(--muted); font-size: 16px;
}
.chip-x:hover { background: var(--panel-2); color: var(--text); }

.btn-attach {
  width: 42px; height: 42px; border: 0; border-radius: 50%; background: transparent;
  color: var(--muted); display: grid; place-items: center; flex: none;
}
.btn-attach:hover { background: var(--panel-2); color: var(--text); }

.drop {
  position: absolute; inset: 0; z-index: 10; padding: 16px;
  background: color-mix(in srgb, var(--bg) 70%, transparent); pointer-events: none;
}
.drop-box {
  height: 100%; border: 2.5px dashed var(--accent); border-radius: 18px; display: grid; place-items: center;
  font-size: 18px; font-weight: 600; color: var(--accent); background: var(--accent-soft);
}
@media (max-width: 600px) {
  .chip-name { max-width: 150px; }
}

/* ---------- photos and videos in place ---------- */
.msg.has-media { padding: 4px 4px 6px; }
.msg.has-media .text { padding: 4px 7px 0; }
.msg.has-media .meta { margin-right: 6px; }
.media-msg { display: flex; flex-direction: column; gap: 4px; }
.media {
  position: relative; display: block; max-width: 100%; padding: 0; border: 0; border-radius: 11px;
  overflow: hidden; background: var(--code-bg); cursor: zoom-in;
}
.media:disabled { cursor: default; }
.media img, .media video { display: block; width: 100%; height: 100%; object-fit: cover; }
.media.round { border-radius: 50%; }
.media.local { margin-bottom: 4px; aspect-ratio: auto; }
.media.local img, .media.local video { height: auto; max-height: 360px; object-fit: contain; }
.media-play {
  position: absolute; left: 50%; top: 50%; width: 52px; height: 52px; margin: -26px 0 0 -26px;
  border-radius: 50%; background: rgba(0, 0, 0, .55); pointer-events: none;
}
.media-play::after {
  content: ""; position: absolute; left: 20px; top: 15px;
  border-style: solid; border-width: 11px 0 11px 18px; border-color: transparent transparent transparent #fff;
}
.media-dur {
  position: absolute; left: 8px; top: 8px; padding: 1px 7px; border-radius: 9px;
  background: rgba(0, 0, 0, .55); color: #fff; font-size: 12px; pointer-events: none;
}
.media.round .media-dur { left: 50%; top: auto; bottom: 10px; transform: translateX(-50%); }
.media-foot { margin: 0; padding: 4px 6px; background: transparent; }
.media-foot .att-name { display: none; }

.viewer {
  position: fixed; inset: 0; z-index: 50; display: flex; flex-direction: column;
  background: rgba(8, 12, 14, .98); color: #fff;
}
.viewer-bar { display: flex; align-items: center; gap: 10px; padding: 10px 14px; }
.viewer-name { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; opacity: .85; }
.viewer-x {
  width: 38px; height: 38px; border: 0; border-radius: 50%; background: rgba(255, 255, 255, .12);
  color: #fff; font-size: 24px; line-height: 1;
}
.viewer-x:hover { background: rgba(255, 255, 255, .22); }
.viewer-stage { flex: 1; min-height: 0; display: grid; place-items: center; padding: 0 12px 12px; }
.viewer-stage img { max-width: 100%; max-height: calc(100vh - 72px); max-height: calc(100dvh - 72px); object-fit: contain; border-radius: 6px; }
/* video fills the window (letterboxed), like Telegram's player */
.viewer-stage video { width: 100%; height: calc(100vh - 72px); height: calc(100dvh - 72px); object-fit: contain; }
