:root {
  --bg: #0a0a0b;
  --panel: #131315;
  --panel-2: #1b1b1e;
  --border: #29292d;
  --gold: #f5c400;
  --gold-dim: #a98900;
  --text: #f2f2f0;
  --muted: #9a9a9e;
  --danger: #e0473c;
  --ok: #57a648;
  --bubble-in: #1c1c1f;
  --bubble-out: #f5c400;
  --radius: 14px;
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
}

* { box-sizing: border-box; }
html, body { height: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 15px/1.45 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  overscroll-behavior: none;
}
img { max-width: 100%; display: block; }
button { font: inherit; }
a { color: var(--gold); }
::-webkit-scrollbar { width: 8px; height: 8px; }
::-webkit-scrollbar-thumb { background: #333; border-radius: 8px; }

#app { height: 100vh; height: 100dvh; display: flex; flex-direction: column; }

/* ---------- buttons, inputs, generic ---------- */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 8px;
  border: none; border-radius: 999px; padding: 11px 20px; font-weight: 600; cursor: pointer;
  background: var(--panel-2); color: var(--text); transition: transform .06s ease, opacity .15s ease;
}
.btn:active { transform: scale(.97); }
.btn:disabled { opacity: .5; cursor: not-allowed; }
.btn-gold { background: var(--gold); color: #1a1400; }
.btn-ghost { background: transparent; border: 1px solid var(--border); }
.btn-danger { background: var(--danger); color: #fff; }
.btn-icon { width: 40px; height: 40px; padding: 0; border-radius: 50%; background: var(--panel-2); color: var(--text); font-size: 18px; }
.btn-block { width: 100%; }
.btn-sm { padding: 7px 14px; font-size: 13px; }

label { display: block; font-size: 13px; color: var(--muted); margin: 14px 0 6px; }
input[type=text], input[type=email], input[type=password], input[type=search], textarea, select {
  width: 100%; padding: 11px 13px; border-radius: 10px; border: 1px solid var(--border);
  background: var(--panel-2); color: var(--text); font: inherit;
}
textarea { resize: vertical; min-height: 60px; }
input:focus, textarea:focus, select:focus { outline: none; border-color: var(--gold); }
.form-msg { margin-top: 10px; font-size: 13px; }
.form-msg.ok { color: var(--ok); }
.form-msg.err { color: var(--danger); }
.muted { color: var(--muted); }
.small { font-size: 13px; }
.center { text-align: center; }
.row { display: flex; align-items: center; }
.row-gap { display: flex; align-items: center; gap: 10px; }
.spread { display: flex; align-items: center; justify-content: space-between; }
.hidden { display: none !important; }

.avatar {
  width: 44px; height: 44px; border-radius: 50%; flex: none; object-fit: cover;
  display: flex; align-items: center; justify-content: center; font-weight: 700; color: #1a1400;
  font-size: 16px; background: var(--gold);
}
.avatar.sm { width: 32px; height: 32px; font-size: 13px; }
.avatar.lg { width: 84px; height: 84px; font-size: 28px; }

/* ---------- auth screen ---------- */
.auth-screen {
  flex: 1; display: flex; align-items: center; justify-content: center; padding: 24px;
  background: radial-gradient(circle at 50% 0%, #1c1a08, var(--bg) 60%);
}
.auth-card { width: 100%; max-width: 380px; }
.auth-logo { width: 72px; height: 72px; border-radius: 20px; margin: 0 auto 14px; display: block; }
.auth-title { text-align: center; font-size: 22px; font-weight: 800; margin: 0 0 4px; }
.auth-sub { text-align: center; color: var(--muted); margin: 0 0 22px; font-size: 14px; }
.auth-tabs { display: flex; gap: 6px; background: var(--panel-2); border-radius: 999px; padding: 4px; margin-bottom: 6px; }
.auth-tab { flex: 1; text-align: center; padding: 8px; border-radius: 999px; cursor: pointer; font-weight: 600; color: var(--muted); }
.auth-tab.active { background: var(--gold); color: #1a1400; }

/* ---------- app shell ---------- */
.shell { flex: 1; display: grid; grid-template-columns: 1fr; overflow: hidden; min-height: 0; }
.pane { display: flex; flex-direction: column; min-height: 0; min-width: 0; }
.pane-list { background: var(--panel); border-right: 1px solid var(--border); }
.pane-thread { background: var(--bg); }
body[data-pane="thread"] .pane-list { display: none; }
body[data-pane="list"] .pane-thread { display: none; }
@media (min-width: 860px) {
  .shell { grid-template-columns: 360px 1fr; }
  body[data-pane] .pane-list, body[data-pane] .pane-thread { display: flex; }
  .back-btn { display: none; }
}

.topbar {
  display: flex; align-items: center; gap: 10px; padding: calc(10px + var(--safe-top)) 14px 10px;
  border-bottom: 1px solid var(--border); flex: none;
}
.topbar .brand { display: flex; align-items: center; gap: 9px; font-weight: 800; font-size: 17px; }
.topbar .brand img { width: 30px; height: 30px; border-radius: 8px; }
.topbar .spacer { flex: 1; }
.searchbar { padding: 8px 14px; flex: none; }
.searchbar input { border-radius: 999px; }

.list { flex: 1; overflow-y: auto; }
.list-item {
  display: flex; align-items: center; gap: 12px; padding: 11px 14px; cursor: pointer;
  border-bottom: 1px solid #1a1a1c;
}
.list-item:hover, .list-item.active { background: var(--panel-2); }
.list-item .meta { flex: 1; min-width: 0; }
.list-item .top-row { display: flex; justify-content: space-between; gap: 8px; }
.list-item .name { font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.list-item .time { font-size: 11px; color: var(--muted); flex: none; }
.list-item .preview { font-size: 13px; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.badge { background: var(--gold); color: #1a1400; font-size: 11px; font-weight: 700; min-width: 19px; height: 19px; border-radius: 999px; display: flex; align-items: center; justify-content: center; padding: 0 5px; }
.dot-online { width: 9px; height: 9px; border-radius: 50%; background: var(--ok); border: 2px solid var(--panel); position: absolute; margin-left: 26px; margin-top: 26px; }
.empty-state { flex: 1; display: flex; flex-direction: column; align-items: center; justify-content: center; color: var(--muted); text-align: center; padding: 24px; gap: 10px; }
.empty-state img { width: 90px; opacity: .35; }

/* ---------- thread ---------- */
.thread-head {
  display: flex; align-items: center; gap: 12px; padding: calc(9px + var(--safe-top)) 14px 9px;
  border-bottom: 1px solid var(--border); flex: none; background: var(--panel);
}
.thread-head .info { flex: 1; min-width: 0; cursor: pointer; }
.thread-head .name { font-weight: 700; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.thread-head .status { font-size: 12px; color: var(--muted); }
.back-btn { background: none; border: none; color: var(--text); font-size: 20px; cursor: pointer; padding: 4px 8px; }
.thread-head .avatar { box-shadow: 0 0 0 2px var(--bg), 0 0 0 3px rgba(245,196,0,.7), 0 0 10px rgba(245,196,0,.45); }
.thread-head .btn-icon { box-shadow: 0 0 0 1.5px rgba(245,196,0,.7), 0 0 10px rgba(245,196,0,.3); background: rgba(245,196,0,.08); }

.messages { flex: 1; overflow-y: auto; padding: 16px 4% 8px; position: relative; display: flex; flex-direction: column; gap: 3px; }
.messages::before {
  content: ""; position: absolute; inset: 0;
  background-image: url('../icons/logo.webp'); background-repeat: no-repeat; background-position: center;
  background-size: min(60%, 380px) auto; opacity: .08; pointer-events: none;
}
.day-sep { text-align: center; margin: 14px 0 8px; position: relative; z-index: 1; }
.day-sep span { background: rgba(27,27,30,.75); color: var(--muted); font-size: 11px; padding: 4px 12px; border-radius: 999px; border: 1px solid rgba(245,196,0,.2); backdrop-filter: blur(4px); }

.msg-row { display: flex; margin: 3px 0; position: relative; z-index: 1; max-width: 78%; touch-action: pan-y; border-radius: 17px; }
.msg-row.jump-flash { animation: jump-flash 900ms ease; }
@keyframes jump-flash { 0%, 100% { background: transparent; } 30% { background: rgba(245,196,0,.18); } }
.msg-row.out { align-self: flex-end; flex-direction: row-reverse; }
.msg-row.in { align-self: flex-start; }
.msg-row .bubble-wrap { position: relative; transition: transform .12s ease; }
.msg-row .swipe-reply-icon {
  position: absolute; top: 50%; left: -34px; transform: translateY(-50%) scale(.5); width: 26px; height: 26px;
  border-radius: 50%; background: rgba(245,196,0,.15); border: 1px solid rgba(245,196,0,.5); color: var(--gold);
  display: flex; align-items: center; justify-content: center; font-size: 13px; opacity: 0; transition: opacity .12s, transform .12s;
}
.msg-row.in .swipe-reply-icon { left: auto; right: -34px; }
.msg-row.swiping .swipe-reply-icon { opacity: 1; transform: translateY(-50%) scale(1); }

.bubble {
  padding: 8px 12px; border-radius: 17px; position: relative; word-wrap: break-word; white-space: pre-wrap;
  color: #fff; backdrop-filter: blur(8px); -webkit-backdrop-filter: blur(8px);
}
.msg-row.out .bubble {
  background: rgba(245,196,0,.16); border: 1.5px solid rgba(245,196,0,.65);
  box-shadow: 0 0 14px rgba(245,196,0,.35), inset 0 0 10px rgba(245,196,0,.06);
  border-bottom-right-radius: 5px;
}
.msg-row.in .bubble {
  background: rgba(35,26,8,.6); border: 1.5px solid rgba(245,196,0,.3);
  box-shadow: 0 0 12px rgba(245,196,0,.12), inset 0 0 10px rgba(0,0,0,.15);
  border-bottom-left-radius: 5px;
}
.bubble::after {
  content: ""; position: absolute; bottom: -1px; width: 14px; height: 14px; background: inherit; border: inherit; z-index: -1;
}
.msg-row.out .bubble::after { right: -6px; clip-path: polygon(0 0, 100% 100%, 100% 0%); }
.msg-row.in .bubble::after { left: -6px; clip-path: polygon(100% 0, 0 100%, 0 0); }
.bubble .meta { display: flex; align-items: center; gap: 3px; font-size: 10px; margin-top: 4px; opacity: .75; justify-content: flex-end; }
.msg-tick { display: inline-flex; }
.msg-tick.read { color: #7fd8ff; }
.ai-badge { display: inline-block; font-size: 10px; font-weight: 700; padding: 1px 7px; border-radius: 999px; background: rgba(0,0,0,.25); margin-right: 6px; }
.msg-row.in .ai-badge { background: rgba(245,196,0,.18); color: var(--gold); }
.msg-emoji-only .bubble { background: transparent !important; border: none !important; box-shadow: none !important; font-size: 42px; padding: 0; }
.msg-emoji-only .bubble::after { display: none; }
.msg-img { max-width: 260px; border-radius: 12px; margin-bottom: 4px; cursor: pointer; }
.msg-file { display: flex; align-items: center; gap: 8px; background: rgba(0,0,0,.2); border-radius: 10px; padding: 8px 10px; margin-bottom: 4px; }

.msg-reply-quote {
  display: block; border-left: 3px solid var(--gold); background: rgba(0,0,0,.2); border-radius: 8px;
  padding: 5px 8px; margin-bottom: 5px; font-size: 12.5px; opacity: .85; cursor: pointer;
}
.msg-reply-quote .rq-name { display: block; font-weight: 700; color: var(--gold); font-size: 11px; }
.msg-reply-quote .rq-body { display: block; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; max-width: 220px; }

.msg-reactions { display: flex; gap: 3px; flex-wrap: wrap; margin-top: 3px; }
.msg-reactions .reaction-chip {
  background: rgba(27,27,30,.85); border: 1px solid rgba(245,196,0,.4); border-radius: 999px; padding: 1px 7px;
  font-size: 12px; display: flex; align-items: center; gap: 3px; cursor: pointer; backdrop-filter: blur(4px);
}
.msg-reactions .reaction-chip.mine { border-color: var(--gold); background: rgba(245,196,0,.15); }
.msg-reactions .reaction-chip .n { font-size: 10px; color: var(--muted); }

.react-bar {
  position: absolute; top: -38px; display: flex; gap: 2px; background: rgba(20,20,22,.92); border: 1px solid rgba(245,196,0,.4);
  border-radius: 999px; padding: 4px 6px; box-shadow: 0 4px 16px rgba(0,0,0,.4); z-index: 6; backdrop-filter: blur(6px);
}
.msg-row.out .react-bar { right: 0; }
.msg-row.in .react-bar { left: 0; }
.react-bar button { background: none; border: none; font-size: 18px; cursor: pointer; padding: 3px 5px; border-radius: 50%; }
.react-bar button:hover { background: rgba(245,196,0,.15); }

.voice-msg { display: flex; align-items: center; gap: 8px; min-width: 180px; }
.voice-msg .vplay { width: 34px; height: 34px; border-radius: 50%; border: none; background: rgba(0,0,0,.2); color: inherit; cursor: pointer; flex: none; display: flex; align-items: center; justify-content: center; }
.voice-msg .vbars { flex: 1; height: 26px; display: flex; align-items: center; gap: 2px; }
.voice-msg .vbars i { width: 2px; background: currentColor; opacity: .55; border-radius: 2px; }
.voice-msg .vtime { font-size: 11px; opacity: .75; flex: none; }

.reply-preview {
  display: none; align-items: center; gap: 10px; padding: 8px 12px; margin: 8px 12px 0;
  background: rgba(245,196,0,.08); border: 1px solid rgba(245,196,0,.35); border-left: 3px solid var(--gold);
  border-radius: 10px; backdrop-filter: blur(6px);
}
.reply-preview.show { display: flex; }
.reply-preview .rp-body { flex: 1; min-width: 0; }
.reply-preview .rp-name { display: block; font-size: 11px; font-weight: 700; color: var(--gold); }
.reply-preview .rp-text { display: block; font-size: 12.5px; color: var(--muted); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.reply-preview .rp-close { background: none; border: none; color: var(--muted); font-size: 18px; cursor: pointer; flex: none; }

.composer {
  display: flex; align-items: flex-end; gap: 8px; padding: 10px 12px calc(10px + var(--safe-bottom));
  border-top: 1px solid var(--border); background: var(--panel); flex: none; position: relative;
}
.composer .btn-icon {
  flex: none; background: rgba(245,196,0,.06); box-shadow: 0 0 0 1.5px rgba(245,196,0,.55), 0 0 8px rgba(245,196,0,.2);
}
.composer-input-wrap { flex: 1; position: relative; display: flex; align-items: flex-end; }
.composer-input-wrap textarea {
  flex: 1; max-height: 120px; border-radius: 22px; padding: 10px 40px 10px 16px;
  background: rgba(27,27,30,.7); border: 1.5px solid rgba(245,196,0,.4); box-shadow: 0 0 10px rgba(245,196,0,.12);
}
.ai-compose-btn {
  position: absolute; right: 6px; bottom: 6px; width: 28px; height: 28px; border-radius: 50%; border: none;
  background: rgba(245,196,0,.18); color: var(--gold); cursor: pointer; display: flex; align-items: center; justify-content: center;
  font-size: 14px; box-shadow: 0 0 8px rgba(245,196,0,.35);
}
.ai-compose-btn:disabled { opacity: .4; cursor: default; }
.composer .send-btn {
  background: var(--gold); color: #1a1400; box-shadow: 0 0 14px rgba(245,196,0,.55);
}
.record-indicator { position: absolute; inset: 6px 12px auto; background: var(--panel-2); border-radius: 20px; padding: 10px 16px; display: flex; align-items: center; gap: 10px; }
.rec-dot { width: 9px; height: 9px; border-radius: 50%; background: var(--danger); animation: pulse 1s infinite; }
@keyframes pulse { 0%,100% { opacity: 1 } 50% { opacity: .3 } }

.emoji-panel { position: absolute; bottom: 64px; left: 8px; right: 8px; max-height: 260px; overflow-y: auto; background: var(--panel-2); border: 1px solid var(--border); border-radius: 14px; padding: 8px; z-index: 5; }
.emoji-panel .grp-label { font-size: 11px; color: var(--muted); margin: 6px 4px 2px; }
.emoji-grid { display: grid; grid-template-columns: repeat(8, 1fr); }
.emoji-grid button { background: none; border: none; font-size: 22px; padding: 5px; cursor: pointer; border-radius: 8px; }
.emoji-grid button:hover { background: rgba(255,255,255,.08); }

/* ---------- modal ---------- */
.modal-backdrop { position: fixed; inset: 0; background: rgba(0,0,0,.6); display: flex; align-items: center; justify-content: center; z-index: 50; padding: 20px; }
.modal { background: var(--panel); border: 1px solid var(--border); border-radius: 18px; width: 100%; max-width: 420px; padding: 20px; max-height: 88vh; overflow-y: auto; }
.modal h3 { margin: 0 0 4px; }
.modal-close { position: absolute; top: 14px; right: 16px; background: none; border: none; color: var(--muted); font-size: 20px; cursor: pointer; }

/* ---------- settings ---------- */
.settings-body { flex: 1; overflow-y: auto; padding: 20px; max-width: 480px; }
.settings-section { margin-bottom: 26px; }
.settings-section h4 { margin: 0 0 10px; font-size: 13px; text-transform: uppercase; letter-spacing: .04em; color: var(--muted); }
.toggle-row { display: flex; align-items: center; justify-content: space-between; padding: 12px 0; border-bottom: 1px solid #1a1a1c; gap: 16px; }
.toggle-row .label { font-weight: 600; }
.toggle-row .hint { font-size: 12px; color: var(--muted); margin-top: 3px; }
.switch { position: relative; width: 46px; height: 26px; flex: none; }
.switch input { opacity: 0; width: 100%; height: 100%; margin: 0; position: absolute; cursor: pointer; z-index: 1; }
.switch .track { position: absolute; inset: 0; background: var(--panel-2); border: 1px solid var(--border); border-radius: 999px; transition: background .15s; }
.switch .thumb { position: absolute; top: 2px; left: 2px; width: 20px; height: 20px; border-radius: 50%; background: var(--muted); transition: transform .15s, background .15s; }
.switch input:checked ~ .track { background: rgba(245,196,0,.25); border-color: var(--gold); }
.switch input:checked ~ .track .thumb, .switch input:checked ~ .thumb { transform: translateX(20px); background: var(--gold); }

.qr-box { display: flex; flex-direction: column; align-items: center; gap: 14px; text-align: center; padding: 20px; background: var(--panel-2); border-radius: 16px; }
.qr-box img { width: 190px; height: 190px; border-radius: 10px; background: #fff; padding: 8px; }

/* ---------- call overlay ---------- */
.call-overlay { position: fixed; inset: 0; background: #000; z-index: 100; display: flex; flex-direction: column; }
.call-video-wrap { flex: 1; position: relative; overflow: hidden; }
.call-video-wrap video { width: 100%; height: 100%; object-fit: cover; background: #000; }
.call-video-wrap video.remote { position: absolute; inset: 0; }
.call-video-wrap video.local { position: absolute; width: 110px; height: 150px; right: 14px; bottom: 100px; border-radius: 12px; object-fit: cover; box-shadow: 0 4px 14px rgba(0,0,0,.5); border: 2px solid var(--panel); }
.call-status { position: absolute; top: calc(16px + var(--safe-top)); left: 0; right: 0; text-align: center; color: #fff; font-weight: 600; text-shadow: 0 2px 8px rgba(0,0,0,.6); }
.call-status .sub { display: block; font-size: 13px; font-weight: 400; opacity: .8; margin-top: 3px; }
.call-controls { display: flex; justify-content: center; gap: 14px; padding: 18px 12px calc(18px + var(--safe-bottom)); background: linear-gradient(0deg, rgba(0,0,0,.6), transparent); }
.call-controls .btn-icon { width: 54px; height: 54px; font-size: 22px; background: rgba(255,255,255,.14); color: #fff; }
.call-controls .btn-icon.off { background: #fff; color: #111; }
.call-controls .btn-icon.end { background: var(--danger); color: #fff; }
.grid-call { display: grid; gap: 6px; padding: 6px; flex: 1; }
.grid-call .tile { position: relative; border-radius: 10px; overflow: hidden; background: #151517; }
.grid-call .tile video { width: 100%; height: 100%; object-fit: cover; }
.grid-call .tile .tag { position: absolute; left: 8px; bottom: 6px; font-size: 12px; background: rgba(0,0,0,.5); padding: 2px 8px; border-radius: 999px; color: #fff; }

/* ---------- toast ---------- */
#toast-root { position: fixed; top: calc(12px + var(--safe-top)); left: 0; right: 0; display: flex; flex-direction: column; align-items: center; gap: 8px; z-index: 200; pointer-events: none; }
.toast { background: var(--panel-2); border: 1px solid var(--border); padding: 10px 16px; border-radius: 999px; font-size: 13px; box-shadow: 0 6px 20px rgba(0,0,0,.4); }

/* ---------- misc ---------- */
.fab { position: absolute; right: 18px; bottom: calc(18px + var(--safe-bottom)); width: 54px; height: 54px; border-radius: 50%; background: var(--gold); color: #1a1400; border: none; font-size: 24px; cursor: pointer; box-shadow: 0 6px 18px rgba(0,0,0,.4); }
.pane-list { position: relative; }
.member-row { display: flex; align-items: center; gap: 10px; padding: 9px 0; }
.member-row .meta { flex: 1; }
.member-row .name { font-weight: 600; font-size: 14px; }
.member-row .status { font-size: 12px; color: var(--muted); }
.chip { display: inline-flex; align-items: center; gap: 6px; background: var(--panel-2); border-radius: 999px; padding: 4px 10px; font-size: 12px; color: var(--muted); }
.install-banner { display: flex; align-items: center; gap: 10px; background: var(--panel-2); border-radius: 12px; padding: 10px 12px; margin: 10px 14px; }
.install-banner img { width: 30px; height: 30px; border-radius: 8px; }
.install-banner .txt { flex: 1; font-size: 13px; }
