/* EN: Telegram-inspired compact archive interface.
   RU: Компактный интерфейс архива в стиле Telegram. */
:root {
    --bg: var(--tg-theme-bg-color, #17212b);
    --surface: var(--tg-theme-secondary-bg-color, #202b36);
    --surface-2: color-mix(in srgb, var(--surface) 88%, white 12%);
    --surface-3: color-mix(in srgb, var(--surface) 76%, white 24%);
    --text: var(--tg-theme-text-color, #f2f5f7);
    --muted: var(--tg-theme-hint-color, #8d9aa5);
    --accent: var(--tg-theme-button-color, #36c5b5);
    --accent-text: var(--tg-theme-button-text-color, #ffffff);
    --incoming: #202f3d;
    --outgoing: #2b6f91;
    --line: color-mix(in srgb, var(--muted) 22%, transparent);
    --shadow: 0 10px 28px rgb(0 0 0 / 18%);
    color-scheme: dark;
}

* { box-sizing: border-box; }
html, body { width: 100%; max-width: 100%; min-height: 100%; overflow-x: hidden; }
body {
    margin: 0;
    background: var(--bg);
    color: var(--text);
    font: 14px -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
    letter-spacing: .01em;
}
button, input { font: inherit; }
button {
    min-height: 36px;
    width: 100%;
    border: 0;
    border-radius: 10px;
    padding: 8px 12px;
    background: var(--accent);
    color: var(--accent-text);
    font-weight: 650;
    cursor: pointer;
    transition: transform .14s ease, filter .14s ease, background .14s ease;
}
button:not(:disabled):hover { filter: brightness(1.08); }
button:not(:disabled):active { transform: scale(.97); }
button:disabled { opacity: .45; cursor: default; }
button.secondary {
    border: 1px solid var(--line);
    background: transparent;
    color: var(--text);
    box-shadow: none;
}
.icon-button {
    width: 36px;
    min-width: 36px;
    padding: 0;
    font-size: 20px;
    line-height: 1;
}

#login {
    display: grid;
    align-content: center;
    gap: 10px;
    min-height: 100dvh;
    max-width: 430px;
    margin: 0 auto;
    padding: 28px 20px;
}
#login h1 { margin: 0 0 8px; font-size: 26px; letter-spacing: -.035em; }
#login input, #login button { min-height: 46px; }

#archive {
    display: flex;
    flex-direction: column;
    width: 100%;
    height: 100dvh;
    min-height: 0;
    overflow: hidden;
    background: var(--bg);
}
#archive[hidden], #login[hidden] { display: none !important; }
#archive > header {
    z-index: 4;
    flex: 0 0 auto;
    padding: 12px 14px 0;
    background: color-mix(in srgb, var(--bg) 94%, black 6%);
    border-bottom: 1px solid var(--line);
}
.appbar { display: flex; align-items: center; gap: 10px; min-width: 0; }
.appmark {
    display: grid;
    flex: 0 0 34px;
    width: 34px;
    height: 34px;
    place-items: center;
    border-radius: 50%;
    background: var(--accent);
    color: var(--accent-text);
    font-size: 18px;
    box-shadow: 0 4px 12px rgb(54 197 181 / 22%);
}
.app-title { min-width: 0; flex: 1; }
.app-title h1 { margin: 0; font-size: 19px; line-height: 1.1; letter-spacing: -.025em; }
.chosen {
    min-height: 17px;
    overflow: hidden;
    color: var(--muted);
    font-size: 12px;
    text-overflow: ellipsis;
    white-space: nowrap;
}
.archive-change-indicator {
    margin-top: 2px;
    color: #f2c46d;
    font-size: 11px;
    font-weight: 650;
    line-height: 1.2;
}
#archiveLogout { margin: 0; }

.section-tabs {
    display: flex;
    gap: 4px;
    margin-top: 12px;
    overflow-x: auto;
    scrollbar-width: none;
}
.section-tabs::-webkit-scrollbar { display: none; }
.tab {
    width: auto;
    min-height: 34px;
    padding: 7px 12px;
    border-radius: 9px 9px 0 0;
    background: transparent;
    color: var(--muted);
    font-size: 13px;
    box-shadow: none;
}
.tab.active { background: var(--surface-2); color: var(--text); }

#controls { padding: 10px 0 11px; }
.toolbar { display: grid; gap: 8px; }
.pair { display: flex; align-items: center; gap: 7px; }
.pair .icon-button { flex: 0 0 36px; }
.search-field {
    display: flex;
    align-items: center;
    gap: 7px;
    min-height: 38px;
    padding: 0 11px;
    border: 1px solid transparent;
    border-radius: 11px;
    background: var(--surface);
    color: var(--muted);
}
.search-field:focus-within { border-color: color-mix(in srgb, var(--accent) 65%, transparent); }
.search-field > span { font-size: 22px; line-height: 1; transform: rotate(-15deg); }
.search-field input {
    min-width: 0;
    width: 100%;
    min-height: 36px;
    padding: 0;
    border: 0;
    outline: 0;
    background: transparent;
    color: var(--text);
}
input::placeholder { color: var(--muted); }

#navigator {
    flex: 0 0 auto;
    display: grid;
    gap: 7px;
    max-height: min(44dvh, 420px);
    min-height: 0;
    overflow-y: auto;
    padding: 10px 12px 14px;
    overscroll-behavior: contain;
    -webkit-overflow-scrolling: touch;
    scrollbar-color: var(--surface-3) transparent;
}
.navItem {
    position: relative;
    min-height: 54px;
    padding: 10px 12px 9px 48px;
    border: 1px solid var(--line);
    border-radius: 13px;
    background: var(--surface);
    color: var(--text);
    text-align: left;
    box-shadow: none;
}
.navItem::before {
    position: absolute;
    top: 50%;
    left: 12px;
    display: grid;
    width: 26px;
    height: 26px;
    place-items: center;
    border-radius: 50%;
    background: color-mix(in srgb, var(--accent) 22%, var(--surface-3));
    color: var(--accent);
    content: "◌";
    font-size: 15px;
    transform: translateY(-50%);
}
.navItem:hover { background: var(--surface-2); }
.navTitle { overflow: hidden; font-size: 14px; font-weight: 650; text-overflow: ellipsis; white-space: nowrap; }
.navHint { margin-top: 3px; overflow: hidden; color: var(--muted); font-size: 11px; text-overflow: ellipsis; white-space: nowrap; }
.navChangeBadge { color: #f2c46d; font-weight: 650; }
.pager { display: flex; gap: 7px; }
.pager button { flex: 1; }
#navigator > .secondary { min-height: 36px; }
.empty { margin: 0; padding: 28px 12px; color: var(--muted); text-align: center; }

#messages {
    display: grid;
    flex: 1 1 auto;
    gap: 7px;
    min-height: 0;
    overflow-x: hidden;
    overflow-y: auto;
    padding: 12px 12px 26px;
    overscroll-behavior: contain;
    -webkit-overflow-scrolling: touch;
    scrollbar-color: var(--surface-3) transparent;
}
#messages[hidden] { display: none !important; }
.message {
    justify-self: start;
    width: fit-content;
    max-width: min(86%, 620px);
    min-width: 0;
    padding: 8px 10px 7px;
    overflow-wrap: anywhere;
    border: 1px solid rgb(255 255 255 / 5%);
    border-radius: 6px 15px 15px 15px;
    background: var(--incoming);
    box-shadow: 0 2px 8px rgb(0 0 0 / 12%);
}
.message.outgoing { justify-self: end; background: var(--outgoing); border-radius: 15px 6px 15px 15px; }
.meta { margin-bottom: 4px; color: var(--muted); font-size: 11px; line-height: 1.3; }
.outgoing .meta { color: #d5ecf5; }
.body { max-width: 100%; overflow-wrap: anywhere; white-space: pre-wrap; line-height: 1.4; }
.media { display: block; width: 100%; max-width: 100%; max-height: min(48vh, 420px); margin: 5px 0; border-radius: 10px; background: #071017; object-fit: contain; cursor: zoom-in; }
.audio { width: 100%; max-width: 100%; margin: 5px 0; }
.actions { display: flex; gap: 6px; margin-top: 7px; }
.actions button { width: auto; min-height: 30px; padding: 5px 9px; border-radius: 8px; font-size: 12px; }
.status { margin-top: 5px; color: var(--muted); font-size: 11px; line-height: 1.35; }
.deleted { opacity: .7; border-left: 3px solid #e66b72; }

.media-filters { display: flex; flex-wrap: wrap; gap: 6px; width: 100%; padding: 3px 0 2px; }
.media-filter { width: auto; min-height: 30px; padding: 5px 9px; border: 1px solid var(--line); border-radius: 999px; background: transparent; color: var(--muted); font-size: 11px; white-space: nowrap; }
.media-filter.active { border-color: var(--accent); background: var(--accent); color: var(--accent-text); }
.archive-pager { display: grid; gap: 7px; width: 100%; }
.pager-info { display: block; width: 100%; text-align: center; }
.pager-actions {
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 8px;
    width: 100%;
}
.pager-actions input {
    flex: 0 0 52px;
    width: 52px !important;
    min-height: 36px;
    padding: 6px;
    border: 1px solid var(--line);
    outline: 0;
    text-align: center;
}

.reply-preview { display: block; width: 100%; margin: 0 0 6px; padding: 6px 8px; border: 0; border-left: 3px solid var(--accent); border-radius: 5px; background: rgb(0 0 0 / 14%); color: var(--text); text-align: left; font-size: 11px; }
.reply-preview strong { display: block; overflow: hidden; color: var(--accent); text-overflow: ellipsis; white-space: nowrap; }
.reply-preview span { display: block; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.message.reply-highlight { outline: 2px solid var(--accent); outline-offset: 2px; }

#archiveBackButton { flex: 0 0 auto; width: calc(100% - 24px); margin: 9px 12px 0; }
#mtprotoPanel { border-top: 1px solid var(--line) !important; padding-top: 12px !important; }
#viewer { position: fixed; inset: 0; z-index: 10; display: grid; place-items: center; padding: 0; background: rgb(0 0 0 / 92%); }
#viewer[hidden] { display: none; }
.viewer-media { width: 100vw; max-width: 100vw; height: 100vh; max-height: 100vh; object-fit: contain; }
.viewer-audio { width: min(90vw, 520px); }
.close { position: absolute; top: 14px; right: 14px; z-index: 1; width: auto; min-height: 34px; background: rgb(0 0 0 / 55%); }

@media (min-width: 700px) {
    #archive { max-width: 860px; margin: 0 auto; box-shadow: var(--shadow); }
    #archive > header { padding-left: 20px; padding-right: 20px; }
    #navigator, #messages { padding-left: 20px; padding-right: 20px; }
    #navigator { max-height: 38dvh; }
}
@media (max-width: 420px) {
    #archive > header { padding-left: 11px; padding-right: 11px; }
    #navigator, #messages { padding-left: 9px; padding-right: 9px; }
    .message { max-width: 91%; }
    .tab { padding-left: 10px; padding-right: 10px; }
}

/* EN: Telegram-like conversation list instead of oversized cards.
   RU: Список диалогов в стиле Telegram вместо громоздких карточек. */
.section-tabs[hidden] { display: none !important; }
#navigator { gap: 0; padding-top: 4px; }
.navItem {
    min-height: 62px;
    padding: 11px 30px 10px 52px;
    border: 0;
    border-bottom: 1px solid var(--line);
    border-radius: 0;
    background: transparent;
    box-shadow: none;
}
.navItem::after {
    position: absolute;
    top: 50%;
    right: 9px;
    color: var(--muted);
    content: "›";
    font-size: 22px;
    transform: translateY(-53%);
}
.navItem:hover { background: color-mix(in srgb, var(--surface) 65%, transparent); }
.navItem::before { left: 10px; width: 34px; height: 34px; }
.navTitle { font-size: 14px; }
.navHint { font-size: 11px; }
#navigator > .secondary {
    width: auto;
    min-height: 34px;
    margin: 7px 0;
    padding: 7px 12px;
    border-radius: 9px;
}
#archiveBackButton {
    width: auto;
    min-height: 34px;
    margin: 8px 12px 0;
    padding: 7px 12px;
    border-radius: 9px;
}
#controls { background: transparent; }

/* EN: Final screen-specific layout for the three archive views.
   RU: Финальная разметка для трёх экранов архива. */
#controls { padding-top: 8px; }
#controls[hidden], #dialogFilters[hidden], #mediaFilters[hidden], .pair[hidden] { display: none !important; }
.dialog-filters {
    display: flex;
    gap: 7px;
    width: 100%;
    overflow-x: auto;
    padding: 1px 0 3px;
    scrollbar-width: none;
}
.dialog-filters::-webkit-scrollbar { display: none; }
.dialog-filter {
    flex: 0 0 auto;
    width: auto;
    min-height: 31px;
    padding: 5px 11px;
    border: 0;
    border-radius: 999px;
    background: var(--surface);
    color: var(--muted);
    font-size: 12px;
    font-weight: 600;
}
.dialog-filter.active { background: color-mix(in srgb, var(--accent) 24%, var(--surface)); color: var(--text); }

#navigator {
    gap: 0;
    padding: 2px 0 14px;
}
#navigator .navItem {
    min-height: 66px;
    padding: 11px 38px 10px 66px;
    border: 0;
    border-bottom: 1px solid var(--line);
    border-radius: 0;
    background: transparent;
}
#navigator .navItem::before {
    left: 14px;
    width: 42px;
    height: 42px;
    background: var(--surface-3);
    color: var(--muted);
    content: "";
}
#navigator .navItem::after { right: 14px; content: ""; }
#navigator .navItem:hover { background: color-mix(in srgb, var(--surface) 72%, transparent); }
#navigator .navTitle { font-size: 15px; font-weight: 650; }
#navigator .navHint { margin-top: 4px; font-size: 12px; }
#navigator > .secondary {
    width: calc(100% - 24px);
    min-height: 36px;
    margin: 8px 12px;
    padding: 7px 12px;
    border-radius: 10px;
}
.pager { gap: 8px; padding: 10px 12px 0; }
.pager button { min-height: 36px; border-radius: 10px; }

/* EN: Media opens as a compact three-column gallery, like Telegram.
   RU: Медиа открывается компактной галереей из трёх колонок, как в Telegram. */
#messages.media-grid {
    display: grid;
    grid-template-columns: repeat(3, minmax(0, 1fr));
    align-content: start;
    gap: 2px;
    padding: 2px;
}
#messages.media-grid .message {
    display: grid;
    width: 100%;
    max-width: none;
    min-height: 0;
    padding: 0;
    border: 0;
    border-radius: 0;
    background: #0c151d;
    box-shadow: none;
}
#messages.media-grid .media {
    width: 100%;
    height: auto;
    aspect-ratio: 1;
    max-height: none;
    margin: 0;
    border-radius: 0;
    object-fit: cover;
}
#messages.media-grid .meta,
#messages.media-grid .body,
#messages.media-grid .actions,
#messages.media-grid .status,
#messages.media-grid .reply-preview { display: none; }
#messages.media-grid .archive-pager { grid-column: 1 / -1; }
#messages.media-grid .media-tile {
    position: relative;
    overflow: hidden;
}
#messages.media-grid .media-tile .media {
    position: relative;
    z-index: 1;
}
#messages.media-grid .media-play {
    position: absolute;
    z-index: 2;
    top: 50%;
    left: 50%;
    display: grid;
    width: 34px;
    height: 34px;
    place-items: center;
    border-radius: 50%;
    background: rgb(0 0 0 / 55%);
    color: #fff;
    font-size: 14px;
    pointer-events: none;
    transform: translate(-50%, -50%);
}
#messages.media-grid .media-placeholder {
    display: grid;
    width: 100%;
    height: 100%;
    min-height: 100px;
    place-items: center;
    border: 0;
    border-radius: 0;
    background: var(--surface-3);
    color: var(--text);
    font-size: 30px;
}

/* EN: Dialog lists fill the Web App; an opened dialog hides the empty navigator.
   RU: Список диалогов заполняет Web App; в открытом диалоге пустая навигация скрыта. */
#archive[data-screen="profiles"] #navigator,
#archive[data-screen="dialogs"] #navigator,
#archive[data-screen="navigation"] #navigator {
    flex: 1 1 0;
    height: 0;
    max-height: none;
    min-height: 0;
}
#archive[data-screen="chat"] #navigator { display: none; }
#archive[data-screen="bots"] #archiveBackButton { display: none !important; }
#archive[data-screen="profiles"] #archiveBackButton,
#archive[data-screen="dialogs"] #archiveBackButton,
#archive[data-screen="chat"] #archiveBackButton {
    display: inline-grid !important;
}
#archive[data-screen="chat"] #messages { padding-top: 8px; }

/* EN: Use the full remaining column and scroll only when content overflows.
   RU: Использовать весь оставшийся столбец и прокручивать только при переполнении. */
#archive #navigator {
    flex: 1 1 0;
    height: 0;
    max-height: none;
    min-height: 0;
    overflow-y: auto;
}
#archive[data-screen="chat"] #navigator { display: none; }

/* EN: Compact icon controls instead of wide text buttons.
   RU: Компактные иконки вместо широких текстовых кнопок. */
.icon-only {
    display: inline-grid !important;
    width: 38px !important;
    min-width: 38px !important;
    min-height: 38px !important;
    padding: 0 !important;
    place-items: center;
    font-size: 0 !important;
}
.icon-only[hidden] { display: none !important; }
.icon-only svg { width: 18px; height: 18px; }
#archiveBackButton.icon-only { margin: 8px 12px 0; }
#archive .appbar #archiveBackButton.icon-only {
    flex: 0 0 36px;
    width: 36px !important;
    min-width: 36px !important;
    margin: 0;
}

/* EN: Put the conversation back arrow into the Telegram-like app bar.
   RU: Перенести стрелку назад в верхнюю панель в стиле Telegram. */
#archive[data-screen="chat"] .appmark { display: none; }
#archive[data-screen="chat"] .appbar #archiveBackButton.icon-only {
    flex: 0 0 36px;
    width: 36px !important;
    min-width: 36px !important;
    margin: 0;
}
#archive[data-screen="chat"] .app-title h1 { font-size: 17px; }
#archive[data-screen="chat"] .app-title .chosen { font-size: 11px; }
#messages .archive-pager .pager-actions button.icon-only { width: 38px; }
#messages .archive-pager .pager-actions button.icon-only {
    flex: 0 0 38px;
    min-width: 38px;
    display: inline-grid !important;
}

/* EN: Media categories are a single thin horizontal strip, shown only in Media.
   RU: Категории медиа — одна тонкая горизонтальная полоса только во вкладке «Медиа». */
.media-filters {
    min-width: 0;
    flex-wrap: nowrap;
    overflow-x: scroll;
    padding: 2px 0 4px;
    scrollbar-width: none;
    overscroll-behavior-x: contain;
    touch-action: pan-x;
    -webkit-overflow-scrolling: touch;
}
.media-filters::-webkit-scrollbar { display: none; }
.media-filter { flex: 0 0 auto; min-height: 28px; padding: 4px 10px; font-size: 11px; }

@media (min-width: 700px) {
    #archive { max-width: 520px; }
}
.media-filters { padding-top: 2px; }
.media-filter { min-height: 29px; }
.archive-pager { padding-top: 2px; }
.archive-pager .pager-actions button { min-height: 35px; }
