:root {
  --bg: #eef0f4;
  --panel: #ffffff;
  --ink: #1b1f2a;
  --muted: #646b7a;
  --line: #dde1e8;
  --bar: #1f2f4f;
  --bar-ink: #f2f4f8;
  --mine: #2f4b7c;
  --mine-ink: #ffffff;
  --theirs: #ffffff;
  --theirs-ink: #1b1f2a;
  --accent: #e0a526;
  --accent-ink: #2a1f00;
  --danger: #b3261e;
  --read: #8fd0ff;
  --chat-bg: #e6e9ef;
  --radius: 14px;
  --font: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #14171e;
    --panel: #1b1f28;
    --ink: #e6e8ee;
    --muted: #9198a8;
    --line: #2a303c;
    --bar: #1b2539;
    --bar-ink: #e6e8ee;
    --mine: #36578f;
    --theirs: #232834;
    --theirs-ink: #e6e8ee;
    --chat-bg: #11141a;
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body {
  background: var(--bg);
  color: var(--ink);
  font: 16px/1.4 var(--font);
  -webkit-tap-highlight-color: transparent;
  overscroll-behavior: none;
}
button, input, textarea { font: inherit; color: inherit; }
button { cursor: pointer; }
:focus-visible { outline: 3px solid var(--accent); outline-offset: 2px; }
a { color: inherit; }
[hidden] { display: none !important; }

#app { height: 100dvh; display: flex; }

/* ---------- layout: one pane on phones, two on wide screens ---------- */
.pane-list, .pane-chat { display: flex; flex-direction: column; min-width: 0; height: 100%; }
.pane-list { width: 100%; background: var(--panel); }
.pane-chat { flex: 1; background: var(--chat-bg); }
.layout-list .pane-chat { display: none; }
.layout-chat .pane-list { display: none; }
@media (min-width: 820px) {
  .pane-list { width: 360px; flex: none; border-right: 1px solid var(--line); }
  .layout-list .pane-chat, .layout-chat .pane-list { display: flex; }
  .back-btn { display: none !important; }
}

.bar {
  display: flex; align-items: center; gap: 8px;
  min-height: 56px; padding: 8px 8px 8px 16px;
  padding-top: max(8px, env(safe-area-inset-top));
  background: var(--bar); color: var(--bar-ink);
}
.bar h1 { font-size: 1.15rem; font-weight: 650; margin: 0; flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.bar .sub { display: block; font-size: .78rem; font-weight: 400; opacity: .75; }
.icon-btn {
  display: inline-grid; place-items: center; width: 40px; height: 40px; flex: none;
  border: 0; border-radius: 50%; background: transparent; color: inherit;
}
.icon-btn:hover { background: color-mix(in srgb, currentColor 12%, transparent); }
.icon-btn svg { width: 22px; height: 22px; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }

.banner { padding: 8px 16px; font-size: .88rem; background: var(--accent); color: var(--accent-ink); }
.banner button { border: 0; background: transparent; text-decoration: underline; padding: 0; font-weight: 600; }

.search { padding: 8px 12px; border-bottom: 1px solid var(--line); }
.search input {
  width: 100%; padding: 9px 14px; border-radius: 20px; border: 1px solid var(--line);
  background: var(--bg);
}
.tabs { display: flex; gap: 6px; padding: 8px 12px 4px; }
.tabs button { border: 1px solid var(--line); background: transparent; border-radius: 16px; padding: 4px 12px; font-size: .85rem; color: var(--muted); }
.tabs button[aria-pressed="true"] { background: var(--mine); border-color: var(--mine); color: var(--mine-ink); }

.scroll { flex: 1; overflow-y: auto; -webkit-overflow-scrolling: touch; }

/* ---------- chat list ---------- */
.chat-row {
  display: flex; gap: 12px; align-items: center; width: 100%;
  padding: 10px 14px; border: 0; background: transparent; text-align: left;
}
.chat-row:hover, .chat-row[aria-current="true"] { background: color-mix(in srgb, var(--mine) 9%, transparent); }
.chat-row .meta { flex: 1; min-width: 0; border-bottom: 1px solid var(--line); padding-bottom: 10px; }
.chat-row .top, .chat-row .bottom { display: flex; gap: 8px; align-items: baseline; }
.chat-row .name { flex: 1; font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.chat-row .time { font-size: .75rem; color: var(--muted); flex: none; }
.chat-row .preview { flex: 1; color: var(--muted); font-size: .9rem; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.chat-row.unread .time { color: var(--accent); font-weight: 600; }
.badge { flex: none; min-width: 22px; height: 22px; padding: 0 6px; border-radius: 11px; background: var(--accent); color: var(--accent-ink); font-size: .75rem; font-weight: 700; display: grid; place-items: center; }
.muted-icon { opacity: .6; font-size: .8rem; }

.avatar {
  width: 48px; height: 48px; border-radius: 50%; flex: none; overflow: hidden;
  display: grid; place-items: center; font-weight: 600; color: #fff; font-size: 1.05rem;
  background: var(--mine);
}
.avatar img { width: 100%; height: 100%; object-fit: cover; display: block; }
.avatar.small { width: 38px; height: 38px; font-size: .9rem; }

.empty { padding: 40px 24px; text-align: center; color: var(--muted); }
.empty strong { display: block; color: var(--ink); font-size: 1.05rem; margin-bottom: 6px; }

/* ---------- messages ---------- */
.messages { padding: 12px 10px 6px; display: flex; flex-direction: column; gap: 2px; }
.day { align-self: center; margin: 12px 0 8px; padding: 3px 12px; border-radius: 10px; font-size: .78rem; background: var(--panel); color: var(--muted); }
.system { align-self: center; max-width: 85%; margin: 6px 0; padding: 4px 12px; border-radius: 10px; font-size: .8rem; text-align: center; background: color-mix(in srgb, var(--panel) 70%, transparent); color: var(--muted); }

.msg { display: flex; flex-direction: column; max-width: min(80%, 560px); margin-top: 6px; }
.msg.cont { margin-top: 1px; }
.msg.mine { align-self: flex-end; align-items: flex-end; }
.msg.theirs { align-self: flex-start; }
.bubble {
  position: relative; padding: 6px 10px 6px; border-radius: var(--radius);
  background: var(--theirs); color: var(--theirs-ink);
  overflow-wrap: anywhere; white-space: pre-wrap;
  box-shadow: 0 1px 0 color-mix(in srgb, var(--ink) 8%, transparent);
  border: 0; text-align: left; font: inherit; max-width: 100%;
}
.mine .bubble { background: var(--mine); color: var(--mine-ink); }
.theirs:not(.cont) .bubble { border-top-left-radius: 4px; }
.mine:not(.cont) .bubble { border-top-right-radius: 4px; }
.bubble a { color: inherit; text-decoration: underline; }
.sender { font-size: .8rem; font-weight: 650; margin-bottom: 2px; color: var(--mine); }
@media (prefers-color-scheme: dark) { .sender { color: var(--read); } }
.stamp { float: right; margin: 6px 0 -4px 10px; font-size: .7rem; opacity: .7; white-space: nowrap; }
.stamp .ticks.read { color: var(--read); opacity: 1; }
.deleted { font-style: italic; opacity: .7; }
.quote {
  display: block; margin: 0 0 4px; padding: 4px 8px; border-left: 3px solid var(--accent);
  border-radius: 6px; background: color-mix(in srgb, currentColor 10%, transparent); font-size: .85rem;
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis; max-width: 100%;
}
.quote b { display: block; font-size: .78rem; }
.reactions { margin-top: -6px; z-index: 1; padding: 1px 6px; border-radius: 12px; background: var(--panel); border: 1px solid var(--line); font-size: .85rem; }
.highlight .bubble { outline: 3px solid var(--accent); }

.media { display: block; margin: -2px -6px 4px; border-radius: 10px; overflow: hidden; background: color-mix(in srgb, currentColor 10%, transparent); }
.media img, .media video { display: block; max-width: 100%; max-height: 360px; min-width: 120px; min-height: 80px; object-fit: cover; }
.media.sticker { background: transparent; }
.media.sticker img { width: 140px; height: 140px; min-width: 0; object-fit: contain; }
.media audio { display: block; width: 260px; max-width: 100%; margin: 4px; }
.media-btn { display: block; position: relative; padding: 0; border: 0; background: none; }
.media-btn .play { position: absolute; inset: 0; display: grid; place-items: center; font-size: 2.4rem; color: #fff; text-shadow: 0 1px 6px #0008; }
.doc { text-decoration: none !important; display: flex; gap: 10px; align-items: center; padding: 8px; text-decoration: none; }
.doc .ext { width: 38px; height: 46px; border-radius: 6px; display: grid; place-items: center; background: var(--accent); color: var(--accent-ink); font-size: .7rem; font-weight: 700; }
.doc small { display: block; opacity: .7; }
.media-missing { padding: 10px 12px; font-size: .85rem; opacity: .85; }
.media-missing button { margin-left: 6px; border: 0; background: none; text-decoration: underline; padding: 0; }

/* ---------- composer ---------- */
.composer { background: var(--panel); border-top: 1px solid var(--line); padding: 6px 8px; padding-bottom: max(6px, env(safe-area-inset-bottom)); }
.composer .row { display: flex; align-items: flex-end; gap: 4px; }
.composer textarea {
  flex: 1; resize: none; max-height: 140px; padding: 9px 14px; border-radius: 20px;
  border: 1px solid var(--line); background: var(--bg); line-height: 1.35;
}
.send-btn { background: var(--mine); color: var(--mine-ink); }
.send-btn:hover { background: var(--mine); filter: brightness(1.1); }
.reply-to { display: flex; align-items: center; gap: 8px; margin: 0 4px 6px; }
.reply-to .quote { flex: 1; margin: 0; }
.recording { display: flex; align-items: center; gap: 10px; flex: 1; padding: 0 10px; color: var(--danger); font-weight: 600; }
.recording .dot { width: 10px; height: 10px; border-radius: 50%; background: var(--danger); animation: pulse 1s infinite alternate; }
@keyframes pulse { to { opacity: .3; } }
@media (prefers-reduced-motion: reduce) { .recording .dot { animation: none; } }

/* ---------- sheets, forms ---------- */
.sheet-backdrop { position: fixed; inset: 0; background: #0006; display: flex; align-items: flex-end; justify-content: center; z-index: 10; }
.sheet { width: 100%; max-width: 480px; background: var(--panel); border-radius: 18px 18px 0 0; padding: 12px 0 max(12px, env(safe-area-inset-bottom)); }
@media (min-width: 820px) { .sheet-backdrop { align-items: center; } .sheet { border-radius: 18px; } }
.sheet .emojis { display: flex; justify-content: space-around; padding: 4px 12px 10px; border-bottom: 1px solid var(--line); }
.sheet .emojis button { border: 0; background: none; font-size: 1.7rem; padding: 4px; border-radius: 50%; }
.sheet .emojis button[aria-pressed="true"] { background: color-mix(in srgb, var(--accent) 35%, transparent); }
.sheet .item { display: block; width: 100%; text-align: left; padding: 13px 22px; border: 0; background: none; }
.sheet .item:hover { background: var(--bg); }
.sheet .item.danger { color: var(--danger); }

.page { padding: 20px 18px 40px; max-width: 560px; width: 100%; margin: 0 auto; }
.page h2 { font-size: 1.05rem; margin: 26px 0 8px; }
.page p { margin: 6px 0; color: var(--muted); }
.card { background: var(--panel); border: 1px solid var(--line); border-radius: 12px; padding: 14px 16px; }
.kv { display: grid; grid-template-columns: auto 1fr; gap: 6px 14px; font-size: .92rem; }
.kv dt { color: var(--muted); }
.kv dd { margin: 0; overflow-wrap: anywhere; }
.btn { display: inline-block; padding: 9px 16px; border-radius: 10px; border: 1px solid var(--line); background: var(--panel); font-weight: 600; text-decoration: none; }
.btn.primary { background: var(--mine); border-color: var(--mine); color: var(--mine-ink); }
.btn.danger { color: var(--danger); }
.btn-row { display: flex; flex-wrap: wrap; gap: 8px; margin-top: 12px; }
.field { display: block; margin: 12px 0; }
.field span { display: block; font-size: .85rem; color: var(--muted); margin-bottom: 4px; }
.field input { width: 100%; padding: 10px 12px; border-radius: 10px; border: 1px solid var(--line); background: var(--bg); }
.error { color: var(--danger); font-size: .9rem; min-height: 1.3em; }

.login { margin: auto; width: 100%; max-width: 360px; padding: 24px; }
.login h1 { font-size: 1.6rem; margin: 0 0 4px; }
.login p { color: var(--muted); margin: 0 0 18px; }

.qr { display: block; width: 260px; max-width: 100%; margin: 12px auto; border-radius: 12px; background: #fff; padding: 8px; }
.code { font-size: 2rem; letter-spacing: .2em; font-weight: 700; text-align: center; margin: 12px 0; }
ol.steps { padding-left: 20px; color: var(--muted); }
ol.steps li { margin: 4px 0; }

#toast {
  position: fixed; left: 50%; bottom: calc(80px + env(safe-area-inset-bottom)); transform: translateX(-50%);
  background: var(--ink); color: var(--bg); padding: 10px 16px; border-radius: 10px; font-size: .9rem; z-index: 20;
  max-width: calc(100% - 32px);
}

.lightbox { position: fixed; inset: 0; background: #000e; z-index: 15; display: grid; place-items: center; }
.lightbox img, .lightbox video { max-width: 100vw; max-height: 100dvh; }
.lightbox .icon-btn { position: absolute; top: max(10px, env(safe-area-inset-top)); right: 10px; color: #fff; }
