:root {
  --bg: #07080f;
  --glass: rgba(255, 255, 255, 0.07);
  --glass-strong: rgba(255, 255, 255, 0.12);
  --border: rgba(255, 255, 255, 0.16);
  --highlight: rgba(255, 255, 255, 0.26);
  --text: rgba(255, 255, 255, 0.93);
  --muted: rgba(255, 255, 255, 0.58);
  --accent: #0a84ff;
  --accent-2: #30d1be;
  --danger: #ff6b6b;
  --radius: 22px;
  --blur: blur(26px) saturate(180%);
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }

html, body {
  margin: 0; height: 100%; color: var(--text); background: var(--bg);
  font-family: -apple-system, BlinkMacSystemFont, "SF Pro Text", "Inter", "Segoe UI", system-ui, sans-serif;
  -webkit-font-smoothing: antialiased; overscroll-behavior: none;
}

body {
  background-image:
    radial-gradient(ellipse 70% 50% at 10% 5%, rgba(10, 132, 255, 0.42), transparent 60%),
    radial-gradient(ellipse 60% 45% at 92% 12%, rgba(191, 90, 242, 0.34), transparent 60%),
    radial-gradient(ellipse 65% 55% at 80% 95%, rgba(48, 209, 190, 0.28), transparent 60%),
    radial-gradient(ellipse 55% 45% at 5% 92%, rgba(255, 99, 130, 0.22), transparent 60%);
  background-attachment: fixed;
}

#app { height: 100dvh; display: flex; flex-direction: column; }
.boot { margin: auto; color: var(--muted); }

button, input, textarea { font: inherit; color: inherit; }
button { cursor: pointer; }
a { color: var(--accent-2); }

/* ---------- glass primitives ---------- */
.glass {
  background: var(--glass);
  -webkit-backdrop-filter: var(--blur); backdrop-filter: var(--blur);
  border: 1px solid var(--border);
  box-shadow: 0 8px 32px rgba(0, 0, 0, 0.35), inset 0 1px 0 var(--highlight);
}

.input, .textarea {
  width: 100%; padding: 12px 16px; font-size: 16px; /* 16px stops iOS zoom */
  background: rgba(255, 255, 255, 0.08); border: 1px solid var(--border);
  border-radius: 14px; outline: none;
}
.input:focus, .textarea:focus { border-color: var(--accent); background: rgba(255, 255, 255, 0.11); }
.input::placeholder, .textarea::placeholder { color: var(--muted); }
label.field { display: block; margin: 12px 0 4px; font-size: 0.8rem; color: var(--muted); letter-spacing: 0.04em; }

.btn {
  padding: 11px 18px; border-radius: 999px; border: 1px solid var(--border);
  background: var(--glass-strong); font-weight: 600; font-size: 0.95rem;
}
.btn:hover { background: rgba(255, 255, 255, 0.18); }
.btn.primary { background: linear-gradient(135deg, #0a84ff, #5e5ce6); border-color: transparent; }
.btn.danger { color: var(--danger); }
.btn.small { padding: 6px 12px; font-size: 0.82rem; }
.btn:disabled { opacity: 0.5; cursor: default; }
.icon-btn {
  width: 40px; height: 40px; border-radius: 50%; border: 1px solid var(--border);
  background: var(--glass-strong); display: grid; place-items: center; font-size: 1.1rem; flex: none;
}

.error { color: var(--danger); font-size: 0.9rem; min-height: 1.2em; margin: 8px 0 0; }
.hint { color: var(--muted); font-size: 0.85rem; line-height: 1.45; }

/* ---------- auth screen ---------- */
.auth { margin: auto; width: min(420px, 92vw); padding: 28px 24px; border-radius: var(--radius); }
.auth h1 { margin: 0 0 4px; font-size: 1.7rem; }
.auth .sub { color: var(--muted); margin: 0 0 18px; }
.auth .btn { width: 100%; margin-top: 16px; }

/* ---------- shell ---------- */
.shell { flex: 1; display: flex; min-height: 0; padding: var(--safe-top) 0 0; }
.pane { display: flex; flex-direction: column; min-width: 0; min-height: 0; }
.pane-list { width: 100%; }
.pane-chat { display: none; width: 100%; }
.shell.show-chat .pane-list { display: none; }
.shell.show-chat .pane-chat { display: flex; }

@media (min-width: 860px) {
  .shell { padding: 16px; gap: 16px; }
  .pane-list { width: 340px; flex: none; display: flex !important; }
  .pane-chat { display: flex !important; flex: 1; }
  .pane { border-radius: var(--radius); overflow: hidden; }
}

.topbar {
  display: flex; align-items: center; gap: 10px; padding: 12px 14px;
  border-bottom: 1px solid var(--border); flex: none;
}
.topbar h2 { margin: 0; font-size: 1.15rem; flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.topbar .sub { display: block; font-size: 0.75rem; font-weight: 400; color: var(--muted); }
.back { display: inline-grid; }
@media (min-width: 860px) { .back { display: none; } }

/* ---------- conversation list ---------- */
.list { flex: 1; overflow-y: auto; padding: 8px; }
.item {
  display: flex; gap: 12px; align-items: center; width: 100%; text-align: left;
  padding: 11px 12px; border-radius: 16px; border: 0; background: transparent;
}
.item:hover, .item.active { background: rgba(255, 255, 255, 0.10); }
.avatar {
  width: 46px; height: 46px; border-radius: 50%; flex: none; display: grid; place-items: center;
  font-weight: 700; background: linear-gradient(135deg, #0a84ff, #bf5af2);
}
.avatar.group { background: linear-gradient(135deg, #30d1be, #0a84ff); }
.item .meta { flex: 1; min-width: 0; }
.item .row { display: flex; justify-content: space-between; gap: 8px; }
.item .name { font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.item .time { font-size: 0.75rem; color: var(--muted); flex: none; }
.item .preview { color: var(--muted); font-size: 0.88rem; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.badge {
  background: var(--accent); color: #fff; font-size: 0.72rem; font-weight: 700;
  min-width: 20px; height: 20px; padding: 0 6px; border-radius: 10px; display: grid; place-items: center; flex: none;
}
.empty { padding: 36px 20px; text-align: center; color: var(--muted); }

/* ---------- messages ---------- */
.messages { flex: 1; overflow-y: auto; padding: 14px 12px; display: flex; flex-direction: column; gap: 4px; }
.day { align-self: center; margin: 10px 0 4px; padding: 3px 12px; border-radius: 999px; font-size: 0.74rem; color: var(--muted); background: var(--glass); }
.msg { max-width: 82%; display: flex; flex-direction: column; }
.msg.mine { align-self: flex-end; align-items: flex-end; }
.msg .who { font-size: 0.74rem; color: var(--accent-2); margin: 6px 10px 2px; }
.bubble {
  padding: 9px 14px; border-radius: 20px; line-height: 1.38; white-space: pre-wrap; overflow-wrap: anywhere;
  background: rgba(255, 255, 255, 0.11); border: 1px solid var(--border);
}
.msg.mine .bubble { background: linear-gradient(135deg, #0a84ff, #5e5ce6); border-color: transparent; }
.bubble.deleted { font-style: italic; color: var(--muted); background: transparent; }
.msg .stamp { font-size: 0.68rem; color: var(--muted); margin: 2px 8px 0; }
.msg .actions { display: none; gap: 6px; margin-top: 4px; }
.msg.open .actions { display: flex; }
.loadmore { align-self: center; margin: 4px 0 8px; }

.composer {
  display: flex; align-items: flex-end; gap: 10px; padding: 10px 12px calc(10px + var(--safe-bottom));
  border-top: 1px solid var(--border); flex: none;
}
.composer .textarea { resize: none; max-height: 140px; min-height: 44px; border-radius: 22px; padding: 11px 16px; }
.send { width: 44px; height: 44px; border-radius: 50%; border: 0; flex: none; background: linear-gradient(135deg, #0a84ff, #5e5ce6); font-size: 1.15rem; }
.send:disabled { opacity: 0.4; }
.counter { font-size: 0.72rem; color: var(--danger); padding: 0 16px 6px; text-align: right; }

.placeholder { margin: auto; color: var(--muted); text-align: center; padding: 20px; }
@media (max-width: 859px) { .pane-chat .placeholder { display: none; } }

/* ---------- sheets (dialogs) ---------- */
.overlay { position: fixed; inset: 0; z-index: 20; background: rgba(0, 0, 0, 0.5); display: flex; align-items: flex-end; justify-content: center; }
.sheet {
  width: min(520px, 100%); max-height: 88dvh; overflow-y: auto; padding: 18px 18px calc(18px + var(--safe-bottom));
  border-radius: 26px 26px 0 0; background: rgba(24, 26, 40, 0.82);
}
@media (min-width: 860px) { .overlay { align-items: center; } .sheet { border-radius: 26px; } }
.sheet h3 { margin: 0 0 6px; font-size: 1.2rem; }
.sheet .head { display: flex; align-items: center; justify-content: space-between; margin-bottom: 10px; }
.section { margin: 18px 0 6px; font-size: 0.78rem; color: var(--muted); letter-spacing: 0.08em; text-transform: uppercase; }
.row-line { display: flex; align-items: center; justify-content: space-between; gap: 10px; padding: 9px 0; border-bottom: 1px solid var(--border); }
.row-line:last-child { border-bottom: 0; }
.row-line .t { min-width: 0; }
.row-line .t b { display: block; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.row-line .t span { font-size: 0.8rem; color: var(--muted); }
.chips { display: flex; flex-wrap: wrap; gap: 8px; margin: 8px 0; }
.chip { padding: 7px 13px; border-radius: 999px; border: 1px solid var(--border); background: var(--glass); font-size: 0.9rem; }
.chip.on { background: var(--accent); border-color: transparent; }
.code { word-break: break-all; padding: 10px 12px; border-radius: 12px; background: rgba(0, 0, 0, 0.3); font-family: ui-monospace, Menlo, monospace; font-size: 0.82rem; }
.seg { display: flex; gap: 6px; margin-bottom: 8px; }
.seg .btn { flex: 1; }
.seg .btn.on { background: var(--accent); border-color: transparent; }
.toast { position: fixed; left: 50%; bottom: calc(24px + var(--safe-bottom)); transform: translateX(-50%); z-index: 40; padding: 10px 18px; border-radius: 999px; font-size: 0.9rem; }
.dot { display: inline-block; width: 9px; height: 9px; border-radius: 50%; background: #5fd68a; margin-right: 6px; }
.dot.off { background: rgba(255, 255, 255, 0.3); }
.conn { font-size: 0.75rem; color: var(--muted); padding: 2px 14px 6px; }
