/* ===========================================================
   Messenger — Telegram-подобная дизайн-система
   =========================================================== */
:root {
    --accent: #3390ec;
    --accent-hover: #2b7fd6;
    --bg: #ffffff;
    --sidebar-bg: #ffffff;
    --panel-header: #ffffff;
    --chat-bg: #d5dde3;
    --text: #0f0f0f;
    --text-secondary: #707579;
    --in-bubble: #ffffff;
    --out-bubble: #eeffde;
    --out-bubble-meta: #5aa159;
    --border: #e8e8ea;
    --hover: #f1f2f4;
    --active-chat: #3390ec;
    --active-chat-text: #ffffff;
    --badge: #3390ec;
    --shadow: 0 1px 2px rgba(0,0,0,.08);
    --bubble-shadow: 0 1px 1px rgba(0,0,0,.12);
    --scroll-thumb: rgba(0,0,0,.2);
}

:root[data-theme="dark"] {
    --accent: #3390ec;
    --accent-hover: #4aa0f0;
    --bg: #17212b;
    --sidebar-bg: #17212b;
    --panel-header: #17212b;
    --chat-bg: #0e1621;
    --text: #ffffff;
    --text-secondary: #7d8e9a;
    --in-bubble: #182533;
    --out-bubble: #2b5278;
    --out-bubble-meta: #a5c8e8;
    --border: #0d1520;
    --hover: #202b36;
    --active-chat: #2b5278;
    --active-chat-text: #ffffff;
    --badge: #3390ec;
    --shadow: 0 1px 2px rgba(0,0,0,.3);
    --bubble-shadow: 0 1px 1px rgba(0,0,0,.25);
    --scroll-thumb: rgba(255,255,255,.15);
}

* { box-sizing: border-box; }
html, body { margin: 0; padding: 0; height: 100%; }
body {
    font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
    background: var(--bg);
    color: var(--text);
    -webkit-font-smoothing: antialiased;
    overflow: hidden;
}
#app { height: var(--app-height, 100dvh); }
a { color: var(--accent); text-decoration: none; }
button { font-family: inherit; cursor: pointer; }

::-webkit-scrollbar { width: 8px; height: 8px; }
::-webkit-scrollbar-thumb { background: var(--scroll-thumb); border-radius: 4px; }
::-webkit-scrollbar-track { background: transparent; }

/* ---------- splash ---------- */
.app-splash { height: 100dvh; display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 18px; background: var(--bg); }
.app-splash__logo { font-size: 64px; }
.app-splash__spinner, .spinner { width: 34px; height: 34px; border: 3px solid var(--border); border-top-color: var(--accent); border-radius: 50%; animation: spin .8s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }

/* ---------- shell (two-pane) ---------- */
.tg-shell { display: grid; grid-template-columns: minmax(260px, 380px) 1fr; height: var(--app-height, 100dvh); overflow: hidden; }
.tg-sidebar { background: var(--sidebar-bg); border-right: 1px solid var(--border); display: flex; flex-direction: column; min-width: 0; }
.tg-main { background: var(--chat-bg); display: flex; flex-direction: column; min-width: 0; position: relative; }

/* фон-паттерн чата */
.tg-main::before {
    content: ""; position: absolute; inset: 0; pointer-events: none; opacity: .5;
    background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='120' height='120' viewBox='0 0 120 120'%3E%3Cg fill='%23000' fill-opacity='0.03'%3E%3Ccircle cx='20' cy='20' r='3'/%3E%3Ccircle cx='80' cy='60' r='3'/%3E%3Ccircle cx='40' cy='100' r='3'/%3E%3C/g%3E%3C/svg%3E");
}
:root[data-theme="dark"] .tg-main::before {
    background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='120' height='120' viewBox='0 0 120 120'%3E%3Cg fill='%23fff' fill-opacity='0.03'%3E%3Ccircle cx='20' cy='20' r='3'/%3E%3Ccircle cx='80' cy='60' r='3'/%3E%3Ccircle cx='40' cy='100' r='3'/%3E%3C/g%3E%3C/svg%3E");
}

@media (max-width: 900px) {
    .tg-shell { grid-template-columns: 1fr; }
    .tg-sidebar, .tg-main { grid-column: 1; grid-row: 1; }
    .tg-main { display: none; }
    .tg-shell.chat-open .tg-sidebar { display: none; }
    .tg-shell.chat-open .tg-main { display: flex; }
}

/* ---------- generic ---------- */
.icon-btn { background: transparent; border: none; color: var(--text-secondary); width: 40px; height: 40px; border-radius: 50%; display: inline-flex; align-items: center; justify-content: center; transition: background .15s; }
.icon-btn:hover { background: var(--hover); color: var(--text); }
.icon-btn svg { width: 22px; height: 22px; }

/* ---------- avatar ---------- */
.avatar { border-radius: 50%; color: #fff; display: flex; align-items: center; justify-content: center; font-weight: 600; flex-shrink: 0; overflow: hidden; background-size: cover; background-position: center; }
.avatar img { width: 100%; height: 100%; object-fit: cover; }

/* ---------- sidebar header ---------- */
.side-header { height: 56px; display: flex; align-items: center; gap: 8px; padding: 0 12px; border-bottom: 1px solid var(--border); background: var(--panel-header); }
.side-header .title { font-weight: 600; font-size: 20px; flex: 1; }
.search-box { padding: 8px 12px; }
.search-box input { width: 100%; border: none; background: var(--hover); border-radius: 20px; padding: 9px 14px; color: var(--text); font-size: 14px; outline: none; }
.search-box input::placeholder { color: var(--text-secondary); }

/* ---------- chat list ---------- */
.chat-list { flex: 1; overflow-y: auto; }
.chat-item { display: flex; align-items: center; gap: 12px; padding: 9px 12px; cursor: pointer; transition: background .12s; }
.chat-item:hover { background: var(--hover); }
.chat-item.active { background: var(--active-chat); }
.chat-item.active, .chat-item.active .ci-time, .chat-item.active .ci-preview, .chat-item.active .ci-name { color: var(--active-chat-text); }
.chat-item .avatar { width: 52px; height: 52px; font-size: 20px; }
.ci-body { flex: 1; min-width: 0; }
.ci-row { display: flex; align-items: center; justify-content: space-between; gap: 8px; }
.ci-name { font-weight: 600; font-size: 15px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.ci-time { font-size: 12px; color: var(--text-secondary); flex-shrink: 0; }
.ci-preview { font-size: 14px; color: var(--text-secondary); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.ci-badge { background: var(--badge); color: #fff; border-radius: 12px; min-width: 20px; height: 20px; padding: 0 6px; font-size: 12px; font-weight: 600; display: inline-flex; align-items: center; justify-content: center; flex-shrink: 0; }
.chat-item.active .ci-badge { background: #fff; color: var(--accent); }

.empty-state { flex: 1; display: flex; flex-direction: column; align-items: center; justify-content: center; color: var(--text-secondary); gap: 10px; text-align: center; padding: 20px; }
.empty-state .big { font-size: 56px; }
.placeholder-pill { background: rgba(0,0,0,.15); color: #fff; padding: 8px 16px; border-radius: 18px; font-size: 14px; }
:root[data-theme="dark"] .placeholder-pill { background: rgba(255,255,255,.1); }

/* ---------- chat view ---------- */
.chat-header { height: 56px; display: flex; align-items: center; gap: 12px; padding: 0 12px; background: var(--panel-header); border-bottom: 1px solid var(--border); z-index: 2; }
.chat-header .avatar { width: 40px; height: 40px; font-size: 16px; }
.ch-info { flex: 1; min-width: 0; }
.ch-title { font-weight: 600; font-size: 16px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.ch-status { font-size: 13px; color: var(--text-secondary); }
.ch-status.online { color: var(--accent); }
.back-btn { display: none; }
@media (max-width: 900px) { .back-btn { display: inline-flex; } }

.messages { flex: 1; overflow-y: auto; padding: 12px 6%; display: flex; flex-direction: column; gap: 2px; z-index: 1; }
@media (max-width: 900px) { .messages { padding: 12px 10px; } }

.day-sep { align-self: center; background: rgba(0,0,0,.15); color: #fff; font-size: 12px; padding: 4px 12px; border-radius: 14px; margin: 12px 0; }
:root[data-theme="dark"] .day-sep { background: rgba(255,255,255,.12); }

.msg-row { display: flex; margin-top: 2px; }
.msg-row.out { justify-content: flex-end; }
.msg-row.grouped { margin-top: 1px; }

.bubble { position: relative; max-width: min(70%, 480px); padding: 6px 10px 8px; border-radius: 12px; background: var(--in-bubble); box-shadow: var(--bubble-shadow); font-size: 15px; line-height: 1.35; word-wrap: break-word; overflow-wrap: anywhere; }
.msg-row.out .bubble { background: var(--out-bubble); }
.msg-row.in .bubble { border-bottom-left-radius: 4px; }
.msg-row.out .bubble { border-bottom-right-radius: 4px; }
.bubble .sender { font-size: 13px; font-weight: 600; color: var(--accent); margin-bottom: 2px; }
.bubble .text { white-space: pre-wrap; }
.bubble .text.deleted { font-style: italic; color: var(--text-secondary); }
.bubble .meta { float: right; margin: 6px 0 -2px 8px; font-size: 11px; color: var(--text-secondary); display: inline-flex; align-items: center; gap: 3px; user-select: none; }
.msg-row.out .bubble .meta { color: var(--out-bubble-meta); }
.bubble .edited { font-size: 11px; opacity: .8; }
.ticks { display: inline-flex; align-items: center; }
.ticks svg { width: 16px; height: 16px; }
.ticks.read { color: #4fc3f7; }

.reply-quote { border-left: 3px solid var(--accent); padding: 2px 8px; margin-bottom: 4px; background: rgba(0,0,0,.04); border-radius: 4px; font-size: 13px; }
:root[data-theme="dark"] .reply-quote { background: rgba(255,255,255,.05); }
.reply-quote .rq-name { color: var(--accent); font-weight: 600; }
.reply-quote .rq-text { color: var(--text-secondary); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }

.attach-img { max-width: 100%; border-radius: 10px; margin-bottom: 4px; cursor: pointer; display: block; }
.attach-file { display: flex; align-items: center; gap: 10px; padding: 8px; background: rgba(0,0,0,.05); border-radius: 8px; margin-bottom: 4px; }
:root[data-theme="dark"] .attach-file { background: rgba(255,255,255,.06); }
.attach-file .fi { width: 40px; height: 40px; border-radius: 50%; background: var(--accent); color: #fff; display: flex; align-items: center; justify-content: center; flex-shrink: 0; }

.reactions { display: flex; flex-wrap: wrap; gap: 4px; margin-top: 4px; }
.reaction { background: rgba(51,144,236,.12); color: var(--accent); border: none; border-radius: 12px; padding: 2px 8px; font-size: 13px; display: inline-flex; gap: 4px; align-items: center; }
.reaction.mine { background: var(--accent); color: #fff; }

.msg-actions { position: absolute; top: 0; opacity: 0; transition: opacity .12s; display: flex; gap: 2px; }
.msg-row.in .msg-actions { right: -68px; }
.msg-row.out .msg-actions { left: -68px; }
.msg-row:hover .msg-actions { opacity: 1; }
.msg-actions .icon-btn { width: 30px; height: 30px; background: var(--panel-header); box-shadow: var(--shadow); }
.msg-actions .icon-btn svg { width: 16px; height: 16px; }

/* ---------- typing ---------- */
.typing-dots { display: inline-flex; gap: 3px; align-items: center; }
.typing-dots span { width: 5px; height: 5px; border-radius: 50%; background: currentColor; animation: td 1.2s infinite; }
.typing-dots span:nth-child(2) { animation-delay: .2s; }
.typing-dots span:nth-child(3) { animation-delay: .4s; }
@keyframes td { 0%,60%,100% { opacity: .3; } 30% { opacity: 1; } }

/* ---------- input ---------- */
.composer { padding: 8px 6%; display: flex; align-items: flex-end; gap: 8px; z-index: 2; }
@media (max-width: 900px) { .composer { padding: 8px 10px; } }
.composer-box { flex: 1; background: var(--panel-header); border-radius: 22px; box-shadow: var(--shadow); display: flex; align-items: flex-end; padding: 4px 6px 4px 12px; }
.composer-box textarea { flex: 1; border: none; outline: none; resize: none; background: transparent; color: var(--text); font-size: 15px; line-height: 1.4; padding: 9px 0; max-height: 140px; font-family: inherit; }
.composer-box textarea::placeholder { color: var(--text-secondary); }
.send-btn { width: 48px; height: 48px; border-radius: 50%; border: none; background: var(--accent); color: #fff; display: flex; align-items: center; justify-content: center; flex-shrink: 0; transition: transform .1s, background .15s; }
.send-btn:hover { background: var(--accent-hover); }
.send-btn:active { transform: scale(.92); }
.send-btn svg { width: 24px; height: 24px; }

.reply-bar { display: flex; align-items: center; gap: 10px; padding: 6px 6% 0; }
@media (max-width: 900px) { .reply-bar { padding: 6px 10px 0; } }

/* ---------- emoji picker ---------- */
.emoji-panel { position: absolute; bottom: 64px; left: 6%; background: var(--panel-header); border: 1px solid var(--border); border-radius: 12px; box-shadow: var(--shadow); padding: 8px; display: grid; grid-template-columns: repeat(8, 1fr); gap: 2px; max-height: 220px; overflow-y: auto; z-index: 5; }
.emoji-panel span { font-size: 24px; text-align: center; cursor: pointer; padding: 4px; border-radius: 8px; }
.emoji-panel span:hover { background: var(--hover); }

/* ---------- context menu ---------- */
.ctx-menu { position: fixed; background: var(--panel-header); border: 1px solid var(--border); border-radius: 10px; box-shadow: 0 4px 20px rgba(0,0,0,.25); padding: 6px; z-index: 100; min-width: 160px; }
.ctx-menu button { display: flex; align-items: center; gap: 10px; width: 100%; border: none; background: transparent; color: var(--text); padding: 9px 12px; border-radius: 6px; font-size: 14px; text-align: left; }
.ctx-menu button:hover { background: var(--hover); }
.ctx-menu button.danger { color: #e0533d; }
.ctx-menu .emoji-row { display: flex; gap: 2px; padding: 4px; border-bottom: 1px solid var(--border); margin-bottom: 4px; }
.ctx-menu .emoji-row span { font-size: 22px; cursor: pointer; padding: 3px; border-radius: 6px; }
.ctx-menu .emoji-row span:hover { background: var(--hover); }

/* ---------- fullscreen media ---------- */
.lightbox { position: fixed; inset: 0; background: rgba(0,0,0,.9); z-index: 200; display: flex; align-items: center; justify-content: center; }
.lightbox img { max-width: 95vw; max-height: 95vh; object-fit: contain; }

/* ---------- auth ---------- */
.auth-wrap { min-height: var(--app-height, 100dvh); display: flex; align-items: center; justify-content: center; background: var(--chat-bg); padding: 16px; }
.auth-card { background: var(--panel-header); border-radius: 16px; box-shadow: 0 4px 24px rgba(0,0,0,.12); width: 100%; max-width: 380px; padding: 32px 28px; text-align: center; }
.auth-card .logo { font-size: 56px; margin-bottom: 8px; }
.auth-card h1 { font-size: 22px; margin: 0 0 4px; }
.auth-card .sub { color: var(--text-secondary); margin: 0 0 22px; font-size: 14px; }
.field { margin-bottom: 14px; text-align: left; }
.field input { width: 100%; border: 1px solid var(--border); background: var(--bg); border-radius: 10px; padding: 12px 14px; font-size: 15px; color: var(--text); outline: none; transition: border-color .15s; }
.field input:focus { border-color: var(--accent); }
.btn-primary { width: 100%; border: none; background: var(--accent); color: #fff; border-radius: 10px; padding: 13px; font-size: 15px; font-weight: 600; transition: background .15s; }
.btn-primary:hover { background: var(--accent-hover); }
.btn-primary:disabled { opacity: .6; }
.auth-error { background: rgba(224,83,61,.12); color: #e0533d; border-radius: 8px; padding: 10px; font-size: 14px; margin-bottom: 12px; }
.link-btn { background: none; border: none; color: var(--accent); font-size: 14px; margin-top: 16px; }
.turnstile-box { margin: 0 0 12px; display: flex; justify-content: center; min-height: 65px; }

/* ---------- search / group pages ---------- */
.list-page { display: flex; flex-direction: column; height: var(--app-height, 100dvh); background: var(--bg); }
.list-page .top { height: 56px; display: flex; align-items: center; gap: 10px; padding: 0 12px; border-bottom: 1px solid var(--border); }
.list-page .top h2 { font-size: 18px; margin: 0; }
.user-row { display: flex; align-items: center; gap: 12px; padding: 10px 14px; border-bottom: 1px solid var(--border); }
.user-row .avatar { width: 46px; height: 46px; font-size: 18px; }
.user-row .u-name { font-weight: 600; }
.user-row .u-login { font-size: 13px; color: var(--text-secondary); }
.chip { background: var(--accent); color: #fff; border-radius: 16px; padding: 5px 10px; font-size: 13px; display: inline-flex; gap: 6px; align-items: center; }
.chip .x { cursor: pointer; }

/* ---------- доработки: панель действий под баблом ---------- */
.bubble-wrap { display: flex; flex-direction: column; max-width: min(78%, 520px); }
.msg-row.out .bubble-wrap { align-items: flex-end; }
.msg-row.in .bubble-wrap { align-items: flex-start; }
.msg-row .bubble { max-width: 100%; }
.msg-actions-below { margin-top: 4px; background: var(--panel-header); border: 1px solid var(--border); border-radius: 12px; box-shadow: var(--shadow); padding: 6px; }
.msg-actions-below .react-row { display: flex; gap: 6px; padding: 2px 4px 6px; border-bottom: 1px solid var(--border); }
.msg-actions-below .react-row span { font-size: 20px; cursor: pointer; padding: 2px 4px; border-radius: 6px; }
.msg-actions-below .react-row span:hover { background: var(--hover); }
.msg-actions-below .act-row { display: flex; flex-wrap: wrap; gap: 2px; padding-top: 4px; }
.msg-actions-below .act-row button { display: flex; align-items: center; gap: 6px; border: none; background: transparent; color: var(--text); padding: 7px 10px; border-radius: 8px; font-size: 13px; }
.msg-actions-below .act-row button:hover { background: var(--hover); }
.msg-actions-below .act-row button.danger { color: #e0533d; }
.msg-actions-below svg { width: 16px; height: 16px; }

/* ---------- выделение сообщений ---------- */
.msg-row.selectable { cursor: pointer; }
.msg-row.selected { background: rgba(51,144,236,.08); border-radius: 8px; }
.sel-check { width: 22px; height: 22px; border-radius: 50%; border: 2px solid var(--accent); color: #fff; display: flex; align-items: center; justify-content: center; font-size: 13px; flex-shrink: 0; align-self: center; margin: 0 6px; }
.msg-row.selected .sel-check { background: var(--accent); }
.chat-header.selection { gap: 4px; }

/* ---------- модалки / action sheet ---------- */
.modal-overlay { position: fixed; inset: 0; background: rgba(0,0,0,.45); z-index: 300; display: flex; align-items: center; justify-content: center; padding: 16px; }
.modal, .action-sheet { background: var(--panel-header); border-radius: 14px; box-shadow: 0 8px 40px rgba(0,0,0,.3); width: 100%; max-width: 360px; padding: 18px; }
.modal-title { font-weight: 600; font-size: 17px; margin-bottom: 8px; }
.modal-text { color: var(--text-secondary); font-size: 14px; }
.modal-actions { display: flex; justify-content: flex-end; gap: 8px; margin-top: 16px; }
.btn-secondary { border: none; background: var(--hover); color: var(--text); border-radius: 8px; padding: 9px 16px; }
.btn-danger { border: none; background: #e0533d; color: #fff; border-radius: 8px; padding: 9px 16px; }
.action-sheet { max-width: 320px; display: flex; flex-direction: column; gap: 4px; }
.action-sheet button { border: none; background: transparent; color: var(--text); padding: 13px; border-radius: 8px; font-size: 15px; }
.action-sheet button:hover { background: var(--hover); }
.action-sheet button.danger { color: #e0533d; }

/* ---------- настройки чата: фон ---------- */
.setting-label { font-size: 13px; color: var(--text-secondary); margin: 14px 0 6px; }
.bg-grid { display: grid; grid-template-columns: repeat(4, 1fr); gap: 8px; }
.bg-swatch { height: 56px; border-radius: 10px; border: 2px solid transparent; cursor: pointer; display: flex; align-items: center; justify-content: center; color: var(--text-secondary); font-size: 12px; }
.bg-swatch.active { border-color: var(--accent); }
.bg-swatch.upload { background: var(--hover); font-size: 22px; }
.form-select { width: 100%; padding: 10px; border-radius: 10px; border: 1px solid var(--border); background: var(--bg); color: var(--text); }
.fwd-list { max-height: 50vh; overflow-y: auto; }
.fwd-list .user-row { cursor: pointer; }

/* ---------- прочее ---------- */
.auth-info { background: rgba(51,144,236,.1); color: var(--accent); border-radius: 8px; padding: 10px; font-size: 13px; margin-bottom: 12px; text-align: left; }
.avatar-edit { position: relative; cursor: pointer; display: inline-block; }
.avatar-edit__badge { position: absolute; right: -2px; bottom: -2px; width: 22px; height: 22px; border-radius: 50%; background: var(--accent); color: #fff; display: flex; align-items: center; justify-content: center; border: 2px solid var(--panel-header); }
.avatar-edit__badge svg { width: 12px; height: 12px; }

/* ---------- iOS safe-area (кнопка отправки не за краем) ---------- */
.composer {
    padding-left: calc(6% + env(safe-area-inset-left));
    padding-right: calc(6% + env(safe-area-inset-right));
    padding-bottom: calc(8px + env(safe-area-inset-bottom));
}
@media (max-width: 900px) {
    .composer {
        padding-left: calc(10px + env(safe-area-inset-left));
        padding-right: calc(10px + env(safe-area-inset-right));
    }
}

#blazor-error-ui { position: fixed; bottom: 0; left: 0; right: 0; background: #ffdddd; color: #a00; padding: 10px 16px; display: none; z-index: 1000; }
#blazor-error-ui.show, #blazor-error-ui[style*="block"] { display: block; }
