/* ===================== Theme tokens ===================== */
:root {
    color-scheme: dark light;

    --bg:            #0e1621;
    --bg-elevated:   #17212b;
    --bg-input:      #1b2733;
    --border:        #24313f;

    --text:          #eaf0f6;
    --text-muted:    #8a9aa9;

    --accent:        #4c8bf5;
    --accent-hover:  #5f99f7;
    --danger:        #e2626b;

    --bubble-in:     #1e2c3a;
    --bubble-out:    #2b5278;
    --tick-read:     #62b0ff;

    --radius:        12px;
    --radius-sm:     8px;
    --shadow:        0 8px 30px rgba(0, 0, 0, .35);

    --app-height:    100dvh;

    --font: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto,
            "Noto Sans Georgian", "Helvetica Neue", Arial, sans-serif;
}

@media (prefers-color-scheme: light) {
    :root {
        --bg:          #eef2f6;
        --bg-elevated: #ffffff;
        --bg-input:    #f0f3f6;
        --border:      #dfe6ec;
        --text:        #14202b;
        --text-muted:  #6b7c8c;
        --bubble-in:   #ffffff;
        --bubble-out:  #d7ebfd;
        --tick-read:   #2f7fd4;
        --shadow:      0 8px 30px rgba(20, 32, 43, .10);
    }
}

/* Explicit theme choices beat prefers-color-scheme, because :root[data-theme]
   is more specific than :root. "system" matches neither. */
:root[data-theme="dark"] {
    --bg:          #0e1621;
    --bg-elevated: #17212b;
    --bg-input:    #1b2733;
    --border:      #24313f;
    --text:        #eaf0f6;
    --text-muted:  #8a9aa9;
    --bubble-in:   #1e2c3a;
    --bubble-out:  #2b5278;
    --tick-read:   #62b0ff;
    --shadow:      0 8px 30px rgba(0, 0, 0, .35);
    color-scheme: dark;
}

:root[data-theme="light"] {
    --bg:          #eef2f6;
    --bg-elevated: #ffffff;
    --bg-input:    #f0f3f6;
    --border:      #dfe6ec;
    --text:        #14202b;
    --text-muted:  #6b7c8c;
    --bubble-in:   #ffffff;
    --bubble-out:  #d7ebfd;
    --tick-read:   #2f7fd4;
    --shadow:      0 8px 30px rgba(20, 32, 43, .10);
    color-scheme: light;
}

*, *::before, *::after { box-sizing: border-box; }

/* The browser's own [hidden] rule is a user-agent style, so ANY author rule
   with a display value beats it — .viewer { display: flex } would show the
   image viewer permanently. This puts the hidden attribute back in charge. */
[hidden] { display: none !important; }

html, body { height: 100%; margin: 0; }

body {
    font-family: var(--font);
    font-size: 16px;
    line-height: 1.45;
    background: var(--bg);
    color: var(--text);
    -webkit-text-size-adjust: 100%;
    -webkit-font-smoothing: antialiased;
    overscroll-behavior: none;
}

.muted { color: var(--text-muted); }

.sr-only {
    position: absolute;
    width: 1px; height: 1px;
    padding: 0; margin: -1px;
    overflow: hidden;
    clip: rect(0 0 0 0);
    clip-path: inset(50%);
    white-space: nowrap;
    border: 0;
}

/* ===================== Login ===================== */
.page-login {
    display: grid;
    place-items: center;
    padding: 24px;
    padding-top: max(24px, env(safe-area-inset-top));
    padding-bottom: max(24px, env(safe-area-inset-bottom));
    min-height: 100dvh;
}

.login-card {
    width: 100%;
    max-width: 380px;
    background: var(--bg-elevated);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    box-shadow: var(--shadow);
    padding: 32px 28px;
}

.login-logo { margin: 0 0 6px; font-size: 26px; letter-spacing: -.02em; }
.login-sub  { margin: 0 0 24px; color: var(--text-muted); font-size: 14px; }

.field { display: block; margin-bottom: 16px; }
.field-label { display: block; margin-bottom: 6px; font-size: 13px; color: var(--text-muted); }

.field input {
    width: 100%;
    padding: 12px 14px;
    font: inherit;
    color: var(--text);
    background: var(--bg-input);
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    outline: none;
    transition: border-color .15s ease;
}
.field input:focus { border-color: var(--accent); }

.btn-primary {
    width: 100%;
    margin-top: 8px;
    padding: 13px 16px;
    font: inherit;
    font-weight: 600;
    color: #fff;
    background: var(--accent);
    border: 0;
    border-radius: var(--radius-sm);
    cursor: pointer;
    min-height: 48px;
    transition: background .15s ease;
}
.btn-primary:hover:not(:disabled) { background: var(--accent-hover); }
.btn-primary:disabled { opacity: .6; cursor: default; }

.btn-block { display: block; text-align: center; text-decoration: none; }

.form-error {
    margin: 0 0 12px;
    padding: 10px 12px;
    font-size: 14px;
    color: var(--danger);
    background: color-mix(in srgb, var(--danger) 12%, transparent);
    border-radius: var(--radius-sm);
}

/* ===================== Chat shell ===================== */
.page-chat {
    display: flex;
    flex-direction: column;
    height: var(--app-height);
    overflow: hidden;
}

.chat-header {
    flex: 0 0 auto;
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 12px;
    padding: 10px 12px;
    padding-top: max(10px, env(safe-area-inset-top));
    background: var(--bg-elevated);
    border-bottom: 1px solid var(--border);
    z-index: 5;
}

.chat-peer { display: flex; align-items: center; gap: 10px; min-width: 0; }

.avatar {
    flex: 0 0 auto;
    width: 38px; height: 38px;
    display: grid; place-items: center;
    border-radius: 50%;
    background: var(--accent);
    color: #fff;
    font-weight: 600;
    text-transform: uppercase;
}

.chat-peer-text { min-width: 0; }
.chat-peer-text strong { display: block; font-size: 15px; line-height: 1.2; }

.chat-status { font-size: 12px; color: var(--text-muted); }
.chat-status.is-online { color: var(--accent); }
.chat-status.is-offline { color: var(--danger); }

.header-actions { display: flex; align-items: center; gap: 4px; }

.conn-dot {
    font-size: 11px;
    color: var(--danger);
    border: 1px solid var(--danger);
    border-radius: 999px;
    padding: 2px 8px;
}

.icon-btn {
    display: grid; place-items: center;
    width: 40px; height: 40px;
    font-size: 20px;
    color: var(--text-muted);
    background: transparent;
    border: 0;
    border-radius: 50%;
    cursor: pointer;
    text-decoration: none;
}
.icon-btn:hover:not(:disabled) { background: var(--bg-input); color: var(--text); }
.icon-btn:disabled { opacity: .35; cursor: default; }

/* ===================== Message area ===================== */
.chat-scroll {
    flex: 1 1 auto;
    overflow-y: auto;
    overflow-x: hidden;
    padding: 8px 10px 4px;
    scroll-behavior: auto;
    overscroll-behavior: contain;
    -webkit-overflow-scrolling: touch;
}

.message-list { display: flex; flex-direction: column; gap: 2px; max-width: 820px; margin: 0 auto; }

.history-top { display: grid; place-items: center; padding: 6px 0; min-height: 8px; }

.btn-ghost {
    font: inherit;
    font-size: 13px;
    color: var(--accent);
    background: transparent;
    border: 1px solid var(--border);
    border-radius: 999px;
    padding: 6px 14px;
    cursor: pointer;
}

.btn-ghost.danger { color: var(--danger); border-color: var(--danger); text-align: center; text-decoration: none; }

.spinner {
    width: 18px; height: 18px;
    border: 2px solid var(--border);
    border-top-color: var(--accent);
    border-radius: 50%;
    animation: spin .8s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

.day-sep { display: grid; place-items: center; margin: 14px 0 10px; }
.day-sep span {
    font-size: 12px;
    color: var(--text-muted);
    background: var(--bg-elevated);
    border-radius: 999px;
    padding: 3px 12px;
}

.unread-sep { display: grid; place-items: center; margin: 14px 0 8px; position: relative; }
.unread-sep::before {
    content: "";
    position: absolute;
    left: 0; right: 0; top: 50%;
    height: 1px;
    background: var(--accent);
    opacity: .35;
}
.unread-sep span {
    position: relative;
    font-size: 12px;
    color: var(--accent);
    background: var(--bg);
    border-radius: 999px;
    padding: 2px 12px;
}

.row { display: flex; }
.row-in  { justify-content: flex-start; }
.row-out { justify-content: flex-end; }

.row.is-grouped { margin-top: 0; }
.row:not(.is-grouped) { margin-top: 8px; }

.bubble {
    position: relative;
    max-width: min(78%, 560px);
    padding: 7px 10px 5px;
    border-radius: 14px;
    background: var(--bubble-in);
    animation: pop .12s ease-out;
}
.row-out .bubble { background: var(--bubble-out); }

/* Only the last bubble in a run gets the tail. */
.row-in.is-last  .bubble { border-bottom-left-radius: 5px; }
.row-out.is-last .bubble { border-bottom-right-radius: 5px; }

@keyframes pop { from { opacity: 0; transform: translateY(4px); } to { opacity: 1; transform: none; } }

.bubble.is-pending { opacity: .72; }
.bubble.is-failed  { background: color-mix(in srgb, var(--danger) 22%, var(--bubble-out)); cursor: pointer; }

.bubble-text {
    white-space: pre-wrap;      /* keeps line breaks without any HTML */
    overflow-wrap: anywhere;
    font-size: 15px;
}

.bubble-meta {
    display: flex;
    align-items: center;
    justify-content: flex-end;
    gap: 4px;
    margin-top: 2px;
    font-size: 11px;
    color: var(--text-muted);
    line-height: 1;
}

.tick { font-size: 11px; }
.tick.is-read { color: var(--tick-read); }

.empty-state { display: grid; place-content: center; gap: 4px; height: 60%; text-align: center; color: var(--text-muted); }
.empty-state p { margin: 0; }

/* ===================== Scroll-to-bottom ===================== */
.scroll-down {
    position: fixed;
    right: 14px;
    bottom: calc(76px + env(safe-area-inset-bottom));
    width: 44px; height: 44px;
    display: grid; place-items: center;
    background: var(--bg-elevated);
    color: var(--text);
    border: 1px solid var(--border);
    border-radius: 50%;
    box-shadow: var(--shadow);
    cursor: pointer;
    z-index: 6;
}
.scroll-down-arrow { font-size: 18px; line-height: 1; }

.badge {
    position: absolute;
    top: -6px; right: -4px;
    min-width: 20px;
    padding: 1px 5px;
    font-size: 11px;
    font-weight: 600;
    color: #fff;
    background: var(--accent);
    border-radius: 999px;
}

/* ===================== Composer ===================== */
.composer {
    flex: 0 0 auto;
    display: flex;
    align-items: flex-end;
    gap: 4px;
    padding: 8px 8px;
    padding-bottom: max(8px, env(safe-area-inset-bottom));
    background: var(--bg-elevated);
    border-top: 1px solid var(--border);
}

.composer-input {
    flex: 1 1 auto;
    min-height: 42px;
    max-height: 160px;
    padding: 10px 12px;
    font: inherit;
    font-size: 15px;
    color: var(--text);
    background: var(--bg-input);
    border: 1px solid var(--border);
    border-radius: 20px;
    outline: none;
    resize: none;
    overflow-y: auto;
}
.composer-input:focus { border-color: var(--accent); }

.send-btn {
    flex: 0 0 auto;
    width: 42px; height: 42px;
    display: grid; place-items: center;
    font-size: 17px;
    color: #fff;
    background: var(--accent);
    border: 0;
    border-radius: 50%;
    cursor: pointer;
    transition: opacity .15s ease;
}
.send-btn:disabled { opacity: .4; cursor: default; }

/* ===================== Emoji panel ===================== */
.emoji-panel {
    flex: 0 0 auto;
    max-height: 46vh;
    display: flex;
    flex-direction: column;
    background: var(--bg-elevated);
    border-top: 1px solid var(--border);
    padding-bottom: env(safe-area-inset-bottom);
}

.emoji-tabs { display: flex; gap: 2px; padding: 6px 8px; border-bottom: 1px solid var(--border); }

.emoji-tab {
    font-size: 18px;
    background: transparent;
    border: 0;
    border-radius: var(--radius-sm);
    padding: 4px 8px;
    cursor: pointer;
    opacity: .6;
}
.emoji-tab.is-active { opacity: 1; background: var(--bg-input); }

.emoji-grid {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(42px, 1fr));
    gap: 2px;
    padding: 6px;
    overflow-y: auto;
}

.emoji-cell {
    font-size: 24px;
    line-height: 1;
    padding: 6px 0;
    background: transparent;
    border: 0;
    border-radius: var(--radius-sm);
    cursor: pointer;
}
.emoji-cell:hover { background: var(--bg-input); }

/* ===================== Action sheet ===================== */
.sheet-backdrop {
    position: fixed; inset: 0;
    background: rgba(0, 0, 0, .45);
    z-index: 20;
}

.action-sheet {
    position: fixed;
    left: 50%; bottom: max(16px, env(safe-area-inset-bottom));
    transform: translateX(-50%);
    width: min(360px, calc(100% - 24px));
    display: flex; flex-direction: column; gap: 1px;
    background: var(--border);
    border-radius: var(--radius);
    overflow: hidden;
    box-shadow: var(--shadow);
    z-index: 21;
}

.sheet-item {
    font: inherit;
    padding: 15px;
    color: var(--text);
    background: var(--bg-elevated);
    border: 0;
    cursor: pointer;
}
.sheet-item.danger { color: var(--danger); }
.sheet-item:hover { background: var(--bg-input); }

/* ===================== Toast ===================== */
.toast {
    position: fixed;
    left: 50%;
    bottom: calc(84px + env(safe-area-inset-bottom));
    transform: translateX(-50%);
    max-width: calc(100% - 32px);
    padding: 10px 16px;
    font-size: 14px;
    color: var(--text);
    background: var(--bg-elevated);
    border: 1px solid var(--border);
    border-radius: 999px;
    box-shadow: var(--shadow);
    z-index: 30;
}

/* ===================== Media ===================== */
.bubble.has-media { padding: 4px 4px 4px; max-width: min(72%, 420px); }
.bubble.has-media .bubble-text { padding: 4px 6px 0; }
.bubble.has-media .bubble-meta { padding: 0 6px 2px; }

.media { position: relative; overflow: hidden; border-radius: 10px; background: rgba(0,0,0,.18); }

.media-image {
    display: block;
    width: 100%;
    height: auto;
    max-height: 60vh;
    object-fit: cover;
    cursor: zoom-in;
    background: rgba(0,0,0,.15);
}

.media-video {
    display: block;
    width: 100%;
    max-height: 60vh;
    background: #000;
    border-radius: 10px;
}

.media-download {
    display: block;
    padding: 6px 8px;
    font-size: 12px;
    color: var(--accent);
    text-decoration: none;
}

.media-error { padding: 8px; font-size: 13px; color: var(--danger); }

.attach-preview {
    flex: 0 0 auto;
    display: flex;
    align-items: center;
    gap: 10px;
    padding: 8px 10px;
    background: var(--bg-elevated);
    border-top: 1px solid var(--border);
}

.attach-thumb {
    width: 48px; height: 48px;
    display: grid; place-items: center;
    font-size: 22px;
    background: var(--bg-input);
    border-radius: var(--radius-sm);
    overflow: hidden;
    flex: 0 0 auto;
}
.attach-thumb img { width: 100%; height: 100%; object-fit: cover; }

.attach-info { flex: 1 1 auto; min-width: 0; display: flex; flex-direction: column; gap: 2px; }

.attach-name { font-size: 14px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.attach-size { font-size: 12px; }

.progress { height: 4px; background: var(--bg-input); border-radius: 999px; overflow: hidden; }
.progress-bar { height: 100%; width: 0; background: var(--accent); transition: width .15s linear; }

.drop-hint {
    position: fixed; inset: 0;
    display: grid; place-items: center;
    background: rgba(0, 0, 0, .5);
    z-index: 25;
    pointer-events: none;
}
.drop-hint span {
    padding: 14px 22px;
    font-size: 16px;
    color: #fff;
    border: 2px dashed rgba(255, 255, 255, .7);
    border-radius: var(--radius);
}

.viewer {
    position: fixed; inset: 0;
    display: flex; flex-direction: column;
    background: rgba(0, 0, 0, .94);
    z-index: 40;
}

.viewer-bar {
    flex: 0 0 auto;
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 10px 14px;
    padding-top: max(10px, env(safe-area-inset-top));
}

.viewer-btn {
    font: inherit;
    font-size: 14px;
    color: #fff;
    background: rgba(255, 255, 255, .12);
    border: 0;
    border-radius: 999px;
    padding: 8px 14px;
    cursor: pointer;
    text-decoration: none;
}
.viewer-btn:hover { background: rgba(255, 255, 255, .2); }

.viewer-stage {
    flex: 1 1 auto;
    display: grid;
    place-items: center;
    padding: 8px;
    padding-bottom: max(8px, env(safe-area-inset-bottom));
    overflow: auto;
}
.viewer-stage img { max-width: 100%; max-height: 100%; object-fit: contain; }

/* ===================== PWA, settings, devices ===================== */
.install-banner {
    flex: 0 0 auto;
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 10px;
    padding: 10px 12px;
    font-size: 14px;
    background: var(--bg-elevated);
    border-bottom: 1px solid var(--border);
}

.install-banner-actions { display: flex; align-items: center; gap: 6px; flex: 0 0 auto; }

.settings-sheet {
    position: fixed;
    left: 50%;
    bottom: 0;
    transform: translateX(-50%);
    width: min(420px, 100%);
    max-height: 88dvh;
    overflow-y: auto;
    display: flex;
    flex-direction: column;
    gap: 18px;
    padding: 16px 18px;
    padding-bottom: max(20px, env(safe-area-inset-bottom));
    background: var(--bg-elevated);
    border: 1px solid var(--border);
    border-bottom: 0;
    border-radius: var(--radius) var(--radius) 0 0;
    box-shadow: var(--shadow);
    z-index: 21;
}

.settings-head { display: flex; align-items: center; justify-content: space-between; font-size: 16px; }

.settings-group { display: flex; flex-direction: column; gap: 8px; }
.settings-label { font-size: 13px; color: var(--text-muted); }
.settings-note { margin: 0; font-size: 13px; color: var(--text-muted); }

.segmented {
    display: grid;
    grid-template-columns: repeat(3, 1fr);
    gap: 2px;
    padding: 3px;
    background: var(--bg-input);
    border-radius: 10px;
}

.segmented label { position: relative; }

.segmented input {
    position: absolute;
    opacity: 0;
    width: 100%;
    height: 100%;
    margin: 0;
    cursor: pointer;
}

.segmented span {
    display: block;
    padding: 8px 4px;
    text-align: center;
    font-size: 14px;
    color: var(--text-muted);
    border-radius: 8px;
    transition: background .12s ease, color .12s ease;
}

.segmented input:checked + span {
    background: var(--bg-elevated);
    color: var(--text);
    box-shadow: 0 1px 3px rgba(0, 0, 0, .2);
}

.switch { display: flex; align-items: center; gap: 10px; cursor: pointer; font-size: 14px; }

.switch input { position: absolute; opacity: 0; width: 0; height: 0; }

.switch-track {
    position: relative;
    flex: 0 0 auto;
    width: 44px;
    height: 26px;
    background: var(--bg-input);
    border: 1px solid var(--border);
    border-radius: 999px;
    transition: background .15s ease, border-color .15s ease;
}

.switch-track::after {
    content: "";
    position: absolute;
    top: 2px;
    left: 2px;
    width: 20px;
    height: 20px;
    background: var(--text-muted);
    border-radius: 50%;
    transition: transform .15s ease, background .15s ease;
}

.switch input:checked + .switch-track { background: var(--accent); border-color: var(--accent); }
.switch input:checked + .switch-track::after { transform: translateX(18px); background: #fff; }
.switch input:disabled + .switch-track { opacity: .5; }
.switch input:focus-visible + .switch-track { outline: 2px solid var(--accent); outline-offset: 2px; }
.switch-text { color: var(--text); }

.device-list { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 6px; }

.device-item { display: flex; align-items: center; justify-content: space-between; gap: 10px; font-size: 14px; }
.device-text { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

.device-revoke {
    flex: 0 0 auto;
    font: inherit;
    font-size: 13px;
    color: var(--danger);
    background: transparent;
    border: 1px solid var(--border);
    border-radius: 999px;
    padding: 3px 10px;
    cursor: pointer;
}
.device-revoke:disabled { opacity: .5; cursor: default; }

/* ===================== Accessibility and desktop ===================== */
:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; border-radius: 4px; }

@media (display-mode: standalone) {
    .chat-header { padding-top: max(14px, env(safe-area-inset-top)); }
}

@media (min-width: 900px) {
    .chat-header, .composer { padding-left: 20px; padding-right: 20px; }
    .composer { justify-content: center; }
    .composer-input { max-width: 760px; }
}

@media (prefers-reduced-motion: reduce) {
    *, *::before, *::after {
        animation-duration: .001ms !important;
        animation-iteration-count: 1 !important;
        transition-duration: .001ms !important;
        scroll-behavior: auto !important;
    }
}
