:root {
    --bg: #0f1419;
    --surface: #1a212b;
    --border: #2a3441;
    --text: #e6edf3;
    --muted: #8b97a6;
    --primary: #2ea6ff;
    --primary-hover: #1f8de0;
    --error: #ff6b6b;
    --radius: 10px;
}

* { box-sizing: border-box; }

body {
    margin: 0;
    font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
    background: var(--bg);
    color: var(--text);
    line-height: 1.5;
}

.topbar {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 0.8rem 1.5rem;
    background: var(--surface);
    border-bottom: 1px solid var(--border);
}

.brand { font-weight: 700; font-size: 1.1rem; color: var(--text); text-decoration: none; }
.topbar nav { display: flex; align-items: center; gap: 1rem; }

.container { max-width: 920px; margin: 2rem auto; padding: 0 1.5rem; }

/* Horizontal-scroll safety net for wide tables on narrow screens */
.table-wrap { width: 100%; overflow-x: auto; -webkit-overflow-scrolling: touch; }

.card {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 1.8rem;
}
.card.narrow { max-width: 380px; margin: 3rem auto; }

h1 { margin-top: 0; font-size: 1.5rem; }

label { display: block; margin-bottom: 1rem; font-size: 0.9rem; color: var(--muted); }

input {
    display: block;
    width: 100%;
    margin-top: 0.35rem;
    padding: 0.6rem 0.75rem;
    background: var(--bg);
    border: 1px solid var(--border);
    border-radius: 8px;
    color: var(--text);
    font-size: 1rem;
}
input:focus { outline: none; border-color: var(--primary); }

.btn {
    display: inline-block;
    padding: 0.6rem 1.1rem;
    border: none;
    border-radius: 8px;
    font-size: 0.95rem;
    cursor: pointer;
    text-decoration: none;
}
.btn.primary { background: var(--primary); color: #fff; }
.btn.primary:hover { background: var(--primary-hover); }
.btn.disabled, .btn[aria-disabled="true"] { opacity: 0.5; pointer-events: none; }

button:disabled { opacity: 0.5; cursor: not-allowed; }

.link {
    background: none; border: none; color: var(--primary);
    cursor: pointer; font-size: 0.9rem; padding: 0;
}
.inline { display: inline; margin: 0; }

.alert { padding: 0.7rem 0.9rem; border-radius: 8px; margin-bottom: 1rem; font-size: 0.9rem; }
.alert.error { background: rgba(255, 107, 107, 0.12); color: var(--error); border: 1px solid rgba(255, 107, 107, 0.3); }
.alert.ok { background: rgba(63, 185, 80, 0.12); color: #3fb950; border: 1px solid rgba(63, 185, 80, 0.3); }
.alert.info { background: rgba(46, 166, 255, 0.1); color: var(--primary); border: 1px solid rgba(46, 166, 255, 0.3); }
.alert.warn { background: rgba(240, 173, 78, 0.12); color: #f0ad4e; border: 1px solid rgba(240, 173, 78, 0.35); }

/* Sync page: breathing room between account line, button, status, link */
#sync-box { margin: 1.3rem 0; }
#sync-box .alert { margin-bottom: 0; }

/* Sync chat-type filters: checkbox rows (override the full-width text-input styles) */
.filters { margin: 1.4rem 0 0.2rem; padding-top: 1.2rem; border-top: 1px solid var(--border); }
.filters .check {
    display: flex; align-items: center; gap: 0.55rem;
    margin-bottom: 0.7rem; color: var(--text); font-size: 0.95rem; cursor: pointer;
}
.filters .check input[type="checkbox"] {
    width: auto; margin: 0; padding: 0; flex: none; accent-color: var(--primary);
}
.filters button { margin-top: 0.4rem; }
.filters > p:last-child { margin-bottom: 0; }
.filters .tz-select {
    width: 100%; margin: 0 0 0.7rem; padding: 0.55rem 0.6rem;
    background: var(--bg); border: 1px solid var(--border); border-radius: 8px;
    color: var(--text); font-size: 0.95rem;
}
.filters .tz-select:focus { outline: none; border-color: var(--primary); }

.muted { color: var(--muted); }
.small { font-size: 0.82rem; }
a { color: var(--primary); }

.chat-cell { display: flex; align-items: center; gap: 0.6rem; }
.chat-cell h1 { margin: 0; }
.avatar { display: inline-flex; align-items: center; justify-content: center; border-radius: 50%;
    object-fit: cover; flex: none; overflow: hidden; background: var(--border); color: var(--text);
    font-weight: 600; text-transform: uppercase; }
.avatar.sm { width: 28px; height: 28px; font-size: 0.8rem; }
.avatar.lg { width: 44px; height: 44px; font-size: 1.1rem; }
.avatar.placeholder { background: linear-gradient(135deg, #2ea6ff, #1f6dd0); color: #fff; }

.search { display: flex; flex-wrap: nowrap; gap: 0.5rem; align-items: center; margin: 1rem 0 1.4rem; }
.search input { flex: 1 1 auto; min-width: 0; max-width: 360px; margin: 0; }
.search button { flex: none; }

/* Compact icon search bar: submit (magnifier) + date filter as square icon buttons */
.icon-btn {
    flex: none; width: 2.6rem; height: 2.6rem; padding: 0;
    display: inline-flex; align-items: center; justify-content: center;
}
.icon-btn svg { width: 1.15rem; height: 1.15rem; }
.search input.date-filter {
    flex: none; width: 2.6rem; max-width: 2.6rem; height: 2.6rem; margin: 0; padding: 0;
    border: 1px solid var(--border); border-radius: 8px; cursor: pointer;
    color-scheme: dark; color: transparent; position: relative; overflow: hidden;
    background: var(--bg) url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='none' stroke='%23aeb8c4' stroke-width='2' stroke-linecap='round' stroke-linejoin='round'%3E%3Crect x='3' y='4' width='18' height='18' rx='2'/%3E%3Cline x1='16' y1='2' x2='16' y2='6'/%3E%3Cline x1='8' y1='2' x2='8' y2='6'/%3E%3Cline x1='3' y1='10' x2='21' y2='10'/%3E%3C/svg%3E") center / 1.2rem no-repeat;
}
.search input.date-filter.has-day { border-color: var(--primary); }
.date-filter::-webkit-datetime-edit,
.date-filter::-webkit-inner-spin-button,
.date-filter::-webkit-clear-button { display: none; }
.date-filter::-webkit-calendar-picker-indicator {
    position: absolute; inset: 0; width: 100%; height: 100%;
    margin: 0; padding: 0; opacity: 0; cursor: pointer;
}
.reset-link { flex: none; text-decoration: none; white-space: nowrap; }
.day-note { margin: -0.9rem 0 1.1rem; }

/* Chat detail: collapsible chat info panel */
.meta { margin: 0.2rem 0 1rem; }
.meta > summary { cursor: pointer; color: var(--muted); font-size: 0.85rem; display: inline-block; }
.meta > summary:hover { color: var(--primary); }
.meta[open] > summary { margin-bottom: 0.8rem; }
.meta-list { display: grid; grid-template-columns: max-content 1fr; gap: 0.28rem 0.9rem; margin: 0; font-size: 0.9rem; }
.meta-list dt { color: var(--muted); }
.meta-list dd { margin: 0; word-break: break-word; }
.acc-meta { margin: 0.6rem 0 0.2rem; }

/* Chat list: title + last-message preview stacked next to the avatar */
.chat-lines { display: flex; flex-direction: column; min-width: 0; }
.chat-preview { color: var(--muted); font-size: 0.82rem; max-width: 340px;
    white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.nowrap { white-space: nowrap; }
/* Corner date shown only on mobile (desktop uses the «Последнее» column) */
.chat-date-sm { display: none; }

.messages { list-style: none; padding: 0; margin: 0; display: flex; flex-direction: column; gap: 0.6rem; }
.msg { background: var(--bg); border: 1px solid var(--border); border-radius: 10px; padding: 0.6rem 0.8rem; }
.msg-head { display: flex; justify-content: space-between; align-items: baseline; gap: 1rem; margin-bottom: 0.2rem; }
.msg-sender { font-weight: 600; font-size: 0.9rem; }
.msg-text { white-space: pre-wrap; word-break: break-word; }
.reply-quote { border-left: 2px solid var(--primary); padding: 0.1rem 0.5rem; margin: 0.2rem 0 0.35rem; display: flex; flex-direction: column; gap: 0.05rem; }
.reply-sender { color: var(--primary); font-size: 0.78rem; font-weight: 600; }
.reply-snippet { color: var(--muted); font-size: 0.82rem; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }

.pager { display: flex; justify-content: space-between; align-items: center; margin-top: 1.4rem; }

/* Search highlighting + jump-to-message target */
mark { background: rgba(46, 166, 255, 0.28); color: inherit; border-radius: 3px; padding: 0 1px; }
.msg-target { border-color: var(--primary); box-shadow: 0 0 0 1px var(--primary); }
.msg-target.flash { animation: msg-flash 2s ease-out; }
@keyframes msg-flash {
    0% { background: rgba(46, 166, 255, 0.22); }
    100% { background: var(--bg); }
}

/* Global message-search results */
.results-head { font-size: 1rem; margin: 1.6rem 0 0.6rem; }
.hits { list-style: none; padding: 0; margin: 0.4rem 0 0; display: flex; flex-direction: column; gap: 0.3rem; }
.hit-link { display: flex; align-items: center; gap: 0.6rem; padding: 0.5rem; border-radius: 10px;
    text-decoration: none; color: inherit; }
.hit-link:hover { background: var(--bg); }
.hit-lines { display: flex; flex-direction: column; min-width: 0; flex: 1 1 auto; }
.hit-top { display: flex; justify-content: space-between; align-items: baseline; gap: 1rem; }
.hit-chat { font-weight: 600; }
.hit-sender { color: var(--muted); }
.hit-snippet { color: var(--muted); font-size: 0.88rem; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.hit-snippet mark { color: var(--text); }

.msg-media { display: inline-block; margin-top: 0.4rem; cursor: zoom-in; }
.msg-media img { max-width: 280px; max-height: 280px; border-radius: 8px; display: block; }
.msg-file { display: inline-block; margin-top: 0.3rem; font-size: 0.9rem; }

/* Media lightbox overlay */
body.media-open { overflow: hidden; }
#media-overlay {
    position: fixed; inset: 0; z-index: 100; display: none;
    align-items: center; justify-content: center;
    background: rgba(0, 0, 0, 0.85); padding: 2rem;
}
#media-overlay.open { display: flex; }
.media-body { max-width: 100%; max-height: 100%; display: flex; align-items: center; justify-content: center; }
.media-body img, .media-body video { max-width: 90vw; max-height: 88vh; border-radius: 8px; display: block; }
.media-body iframe { width: 90vw; height: 88vh; border: none; background: #fff; border-radius: 8px; }
.media-body audio { width: min(90vw, 420px); }
.media-close {
    position: fixed; top: 0.8rem; right: 1.1rem; background: none; border: none;
    color: #fff; font-size: 2.4rem; line-height: 1; cursor: pointer; padding: 0.2rem 0.5rem;
}
.media-close:hover { color: var(--primary); }
.media-file-card {
    background: var(--surface); border: 1px solid var(--border); border-radius: 12px;
    padding: 1.6rem; display: flex; flex-direction: column; gap: 1rem; align-items: center;
    max-width: 90vw;
}
.media-file-name { color: var(--text); word-break: break-word; text-align: center; }

code { background: var(--bg); padding: 0.1rem 0.35rem; border-radius: 4px; font-size: 0.9em; }

.row { display: flex; align-items: center; gap: 1rem; }
.row.spread { justify-content: space-between; }

table.list { width: 100%; border-collapse: collapse; margin-top: 1rem; }
table.list th, table.list td { padding: 0.6rem 0.5rem; border-bottom: 1px solid var(--border); text-align: left; }
table.list th { color: var(--muted); font-weight: 600; font-size: 0.85rem; }
.right { text-align: right; }

.badge { display: inline-block; padding: 0.15rem 0.55rem; border-radius: 999px; font-size: 0.8rem; }
.badge.ok { background: rgba(63, 185, 80, 0.15); color: #3fb950; }
.badge.pending { background: rgba(46, 166, 255, 0.15); color: var(--primary); }
.badge.err { background: rgba(255, 107, 107, 0.15); color: var(--error); }
.badge.muted { background: rgba(139, 151, 166, 0.15); color: var(--muted); }

.link.danger { color: var(--error); }

.actions { white-space: nowrap; }
.actions > * { margin-left: 0.75rem; }
.actions > :first-child { margin-left: 0; }

.center { text-align: center; }
.btn.block { display: block; width: 100%; text-align: center; }
.btn.big { padding: 0.95rem 1.2rem; font-size: 1.05rem; font-weight: 600; }

/* Secondary button (non-primary .btn) */
.btn:not(.primary):not(.link) {
    background: transparent;
    color: var(--text);
    border: 1px solid var(--border);
}
.btn:not(.primary):not(.link):hover { border-color: var(--primary); }

/* Destructive confirm button (later rule + equal specificity wins over secondary) */
.btn.danger:not(.link) { background: var(--error); color: #fff; border: none; }
.btn.danger:not(.link):hover { background: #e05555; border: none; }

/* Irreversible-action zone: separated from the primary action above it */
.danger-zone { margin-top: 1.4rem; padding-top: 1.2rem; border-top: 1px solid var(--border); }
.danger-zone > p:first-child { margin-top: 0; }
.danger-zone > p:last-child { margin-bottom: 0; }

/* Phone-login fallback hidden behind a text link */
.fallback { margin-top: 2.4rem; }
.fallback > summary {
    cursor: pointer;
    color: var(--muted);
    font-size: 0.78rem;
    list-style: none;
    display: inline-block;
}
.fallback > summary:hover { color: var(--primary); }
.fallback > summary::-webkit-details-marker { display: none; }
.fallback > summary::marker { content: ""; }
.fallback[open] > summary { margin-bottom: 1.2rem; }
.fallback form { text-align: left; }

.divider { display: flex; align-items: center; gap: 0.75rem; margin: 1.4rem 0; color: var(--muted); font-size: 0.82rem; }
.divider::before, .divider::after { content: ""; flex: 1; height: 1px; background: var(--border); }

.qr-box { margin: 1.2rem 0; min-height: 220px; display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 0.8rem; }
.qr { background: #fff; padding: 16px; border-radius: 12px; line-height: 0; display: inline-block; margin: 0 auto; }
.qr svg { width: 240px; height: 240px; display: block; }

form a { margin-left: 0.75rem; font-size: 0.9rem; }

/* ------------------------------------------------------------------ */
/* Адаптив: телефоны и узкие экраны                                    */
/* ------------------------------------------------------------------ */
@media (max-width: 640px) {
    .topbar {
        flex-wrap: wrap;
        gap: 0.4rem 0.9rem;
        padding: 0.7rem 1rem;
    }
    .topbar nav { gap: 0.9rem; font-size: 0.92rem; }
    /* email занимает место и не критичен на телефоне */
    .topbar nav .muted { display: none; }

    .container { margin: 1.2rem auto; padding: 0 1rem; }
    .card { padding: 1.2rem; }
    .card.narrow { margin: 1.5rem auto; }

    h1 { font-size: 1.3rem; }

    /* Заголовок + действия переносятся вместо сжатия в одну строку */
    .row.spread { flex-wrap: wrap; gap: 0.6rem 1rem; }

    /* Второстепенные колонки таблиц скрываем — данные остаются на detail-странице */
    .hide-sm { display: none; }

    /* Поджимаем ячейки и действия, чтобы таблица влезала без горизонтального скролла */
    table.list th, table.list td { padding: 0.55rem 0.35rem; }
    .actions > * { margin-left: 0.55rem; }

    /* «Сообщений» скрыт — дату последнего сообщения показываем в правом углу строки */
    .chat-date-sm { display: inline-block; margin-left: auto; align-self: flex-start; padding-left: 0.5rem; text-align: right; line-height: 1.25; }
    .chat-preview { max-width: 47vw; }

    /* Поиск и картинки во всю ширину */
    .search input { max-width: 100%; }
    .msg-media img { max-width: 100%; }

    /* Действия в сообщении не режем по одной строке */
    .msg-head { gap: 0.5rem; }
}

/* Совсем узкие экраны: уменьшаем QR, чтобы не вылезал за карточку */
@media (max-width: 400px) {
    .qr svg { width: 200px; height: 200px; }
    .qr-box { min-height: 200px; }
}
