        :root {
            /* Telegram Dark Theme */
            --bg-color: #0e1621;
            --sidebar-bg: #17212b;
            --chat-bg: #0e1621;
            --panel-border: #2b5278;
            --text-primary: #ffffff;
            --text-secondary: #707579;
            --accent: #3390ec;
            --success: #4caf50;
            --danger: #ef4444;
            --terminal-bg: #06040b;
            
            --chat-bubble-out: #2b5278;
            --chat-bubble-in: #182533;
            --input-bg: #242f3d;
            --header-bg: #17212b;
        }

        * {
            box-sizing: border-box;
            margin: 0;
            padding: 0;
            font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
            -webkit-font-smoothing: antialiased;
        }

        body {
            background: var(--bg-color);
            color: var(--text-primary);
            height: 100vh;
            overflow: hidden;
            display: flex;
        }

        /* Split layout */
        .app-container {
            display: grid;
            grid-template-columns: 350px 1fr;
            grid-template-rows: minmax(0, 1fr);
            width: 100%;
            height: 100%;
            position: relative;
        }

        /* "New device logged in" banner — mirrors Telegram's login alert. */
        .login-alert {
            position: fixed;
            top: 14px;
            left: 50%;
            transform: translateX(-50%);
            z-index: 4000;
            display: flex;
            align-items: center;
            gap: 14px;
            max-width: 560px;
            width: calc(100% - 32px);
            padding: 12px 16px;
            background: var(--sidebar-bg);
            border: 1px solid var(--panel-border);
            border-radius: 14px;
            box-shadow: 0 12px 40px rgba(0, 0, 0, 0.5);
            animation: login-alert-in 0.28s ease;
        }
        @keyframes login-alert-in {
            from { opacity: 0; transform: translate(-50%, -16px); }
            to   { opacity: 1; transform: translate(-50%, 0); }
        }
        .login-alert-icon {
            font-size: 1.6rem;
            flex-shrink: 0;
        }
        .login-alert-body {
            flex-grow: 1;
            min-width: 0;
        }
        .login-alert-title {
            font-size: 0.95rem;
            font-weight: 600;
            color: white;
            margin-bottom: 2px;
        }
        .login-alert-text {
            font-size: 0.82rem;
            color: var(--text-secondary);
            overflow-wrap: anywhere;
        }
        .login-alert-actions {
            display: flex;
            gap: 8px;
            flex-shrink: 0;
        }
        .login-alert-btn {
            border: none;
            cursor: pointer;
            font-size: 0.82rem;
            font-weight: 600;
            padding: 8px 14px;
            border-radius: 10px;
            transition: filter 0.15s ease, background 0.15s ease;
        }
        .login-alert-btn:disabled { opacity: 0.6; cursor: default; }
        .login-alert-btn.accept {
            background: var(--accent);
            color: white;
        }
        .login-alert-btn.accept:hover:not(:disabled) { filter: brightness(1.1); }
        .login-alert-btn.ignore {
            background: transparent;
            color: var(--danger);
            box-shadow: 0 0 0 1px var(--danger) inset;
        }
        .login-alert-btn.ignore:hover:not(:disabled) {
            background: rgba(239, 68, 68, 0.12);
        }
        @media (max-width: 600px) {
            .login-alert { flex-wrap: wrap; }
            .login-alert-actions { width: 100%; justify-content: flex-end; }
        }

        /* Sidebar Chats */
        .chat-sidebar {
            background: var(--sidebar-bg);
            border-right: 1px solid var(--panel-border);
            display: flex;
            flex-direction: column;
            height: 100%;
            min-height: 0;
        }

        .sidebar-header {
            padding: 16px;
            display: flex;
            align-items: center;
            gap: 12px;
            border-bottom: 1px solid var(--panel-border);
            flex-shrink: 0;
        }

        .hamburger-btn {
            background: transparent;
            border: none;
            cursor: pointer;
            color: var(--text-primary);
            font-size: 1.4rem;
            display: flex;
            align-items: center;
            justify-content: center;
            transition: color 0.2s;
            padding: 4px;
        }
        .hamburger-btn:hover {
            color: var(--accent);
        }

        .search-container {
            flex-grow: 1;
            position: relative;
        }
        .search-input {
            width: 100%;
            background: var(--input-bg);
            border: none;
            border-radius: 20px;
            padding: 10px 16px;
            color: var(--text-primary);
            outline: none;
            font-size: 0.95rem;
            transition: all 0.2s;
        }
        .search-input:focus {
            background: #2e3c4a;
        }
        .search-input::placeholder {
            color: var(--text-secondary);
        }

        /* All / Personal switch */
        .chat-filters {
            display: flex;
            align-items: center;
            gap: 6px;
            padding: 8px 14px;
            border-bottom: 1px solid var(--input-bg);
            flex-shrink: 0;
        }
        .filter-chip {
            background: transparent;
            border: none;
            color: var(--text-secondary);
            font-size: 0.82rem;
            padding: 5px 14px;
            border-radius: 14px;
            cursor: pointer;
            transition: background 0.15s ease, color 0.15s ease;
        }
        .filter-chip:hover {
            background: var(--input-bg);
            color: var(--text-primary);
        }
        .filter-chip.active {
            background: var(--accent);
            color: white;
        }
        .filter-count {
            margin-left: auto;
            font-size: 0.7rem;
            color: var(--text-secondary);
        }
        /* Small type marker so groups/channels/bots are distinguishable at a glance */
        .chat-kind {
            font-size: 0.72rem;
            margin-right: 4px;
            opacity: 0.85;
        }

        /* Chat list scroll area */
        .chat-list {
            flex-grow: 1;
            overflow-y: auto;
            min-height: 0;
        }

        .chat-item {
            display: flex;
            align-items: center;
            gap: 12px;
            padding: 12px 16px;
            cursor: pointer;
            transition: all 0.15s ease;
        }
        .chat-item:hover {
            background: rgba(255, 255, 255, 0.03);
        }
        .chat-item.active {
            background: rgba(50, 65, 81);
        }

        /* Avatar generation */
        .avatar {
            flex-basis: 52px;
            width: 52px;
            height: 52px;
            border-radius: 50%;
            display: flex;
            align-items: center;
            justify-content: center;
            font-weight: 500;
            font-size: 1.1rem;
            color: white;
            text-transform: uppercase;
        }

        .chat-details {
            flex-grow: 1;
            min-width: 0;
        }
        .chat-meta {
            display: flex;
            justify-content: space-between;
            align-items: center;
            margin-bottom: 2px;
        }
        .chat-name {
            font-size: 1rem;
            font-weight: 500;
            color: white;
            white-space: nowrap;
            overflow: hidden;
            text-overflow: ellipsis;
        }
        .chat-time {
            font-size: 0.8rem;
            color: var(--text-secondary);
        }
        .chat-last-msg {
            font-size: 0.9rem;
            color: var(--text-secondary);
            white-space: nowrap;
            overflow: hidden;
            text-overflow: ellipsis;
        }
        .unread-badge {
            background: var(--accent);
            color: white;
            font-size: 0.75rem;
            font-weight: 500;
            padding: 2px 8px;
            border-radius: 12px;
            min-width: 20px;
            text-align: center;
        }

        /* Main Conversation Pane */
        .conversation-pane {
            display: flex;
            flex-direction: column;
            height: 100%;
            min-height: 0;
            background: var(--chat-bg);
            background-image: url('data:image/svg+xml;utf8,<svg xmlns="http://www.w3.org/2000/svg" width="100" height="100" viewBox="0 0 100 100"><g fill-opacity="0.03"><path d="M50 0c10 5 5 10 0 10s-10-5-10-10 5-10 10-10z"/><path d="M0 50c10 5 5 10 0 10s-10-5-10-10 5-10 10-10z"/><path d="M100 50c10 5 5 10 0 10s-10-5-10-10 5-10 10-10z"/><path d="M50 100c10 5 5 10 0 10s-10-5-10-10 5-10 10-10z"/></g></svg>');
        }

        .chat-header {
            padding: 12px 20px;
            background: var(--header-bg);
            border-bottom: 1px solid var(--panel-border);
            display: flex;
            justify-content: space-between;
            align-items: center;
            height: auto;
            flex-shrink: 0;
        }
        .active-user-info {
            display: flex;
            align-items: center;
            gap: 12px;
        }
        .active-user-name {
            font-size: 1rem;
            font-weight: 500;
            color: white;
        }
        .active-user-status {
            font-size: 0.85rem;
            color: var(--success);
        }

        .header-actions {
            display: flex;
            gap: 15px;
            align-items: center;
        }
        .sync-state {
            font-size: 0.8rem;
            color: var(--text-secondary);
            white-space: nowrap;
        }
        .sync-state.live {
            color: var(--success);
        }
        /* A message we've sent but Telegram hasn't confirmed yet */
        .bubble-container.pending .bubble {
            opacity: 0.7;
        }
        .bubble-container.failed .bubble {
            opacity: 1;
            box-shadow: 0 0 0 1px var(--danger) inset;
        }
        .header-action-btn {
            background: transparent;
            border: none;
            cursor: pointer;
            color: var(--text-secondary);
            font-size: 1.25rem;
            transition: color 0.2s;
            padding: 4px 8px;
            border-radius: 8px;
        }
        .header-action-btn:hover {
            color: var(--accent);
            background: rgba(255,255,255,0.05);
        }

        /* Message Bubbles Scroll Area */
        .messages-area {
            flex-grow: 1;
            padding: 20px;
            overflow-y: auto;
            min-height: 0;
            display: flex;
            flex-direction: column;
            gap: 8px;
        }

        .bubble-container {
            display: flex;
            width: 100%;
            margin-bottom: 0;
        }
        .bubble-container.sent {
            justify-content: flex-end;
        }
        .bubble-container.received {
            justify-content: flex-start;
        }

        .bubble {
            max-width: 60%;
            padding: 8px 12px;
            font-size: 0.95rem;
            line-height: 1.5;
            position: relative;
            border-radius: 12px;
            /* A flex item won't shrink below its longest word by default, so a pasted
               URL would push the bubble past max-width and out of the pane. */
            min-width: 0;
            overflow-wrap: anywhere;
        }
        /* Text lives in its own span: pre-wrap on .bubble would also render the
           template's own newlines and indentation as blank space. */
        .bubble-text {
            white-space: pre-wrap;     /* keep the sender's line breaks */
            overflow-wrap: anywhere;   /* break unbroken strings (URLs, long words) */
        }
        .bubble.sent {
            background: var(--chat-bubble-out);
            color: white;
            border-bottom-right-radius: 4px;
        }
        .bubble.received {
            background: var(--chat-bubble-in);
            color: var(--text-primary);
            border-bottom-left-radius: 4px;
        }

        .bubble-reply {
            font-size: 0.85rem;
            color: rgba(255,255,255,0.8);
            background: rgba(0,0,0,0.15);
            padding: 6px 10px;
            border-radius: 8px;
            border-left: 3px solid var(--accent);
            margin-bottom: 6px;
            /* Quoted text is a single line; clip it rather than let it grow the bubble. */
            overflow-wrap: anywhere;
            overflow: hidden;
            text-overflow: ellipsis;
            white-space: nowrap;
        }

        .bubble-ticks {
            font-size: 0.75rem;
            margin-left: 6px;
            color: rgba(255,255,255,0.6);
            display: inline-block;
        }

        /* Inline photo / video */
        .bubble-media {
            display: block;
            max-width: 100%;
            max-height: 350px;
            border-radius: 12px;
            margin-bottom: 6px;
            background: rgba(0,0,0,0.2);
        }
        img.bubble-media {
            cursor: zoom-in;
            object-fit: cover;
        }
        video.bubble-media {
            min-width: 200px;
            outline: none;
        }
        .media-link {
            display: block;
            text-decoration: none;
        }

        /* Blurred placeholder, the way Telegram does it: the ~100-byte stripped
           thumbnail is on screen instantly, the real file fades in over it. */
        .media-wrap {
            position: relative;
            display: block;
            overflow: hidden;
            border-radius: 12px;
            margin-bottom: 6px;
            max-width: 320px;
            background: var(--input-bg);
            text-decoration: none;
        }
        .media-thumb {
            position: absolute;
            inset: 0;
            width: 100%;
            height: 100%;
            object-fit: cover;
            /* The thumb is only ~20px wide; blurring hides the upscaling artefacts,
               and scale() pushes the blur's soft edges outside the frame. */
            filter: blur(12px);
            transform: scale(1.15);
        }
        .media-wrap .bubble-media {
            position: relative;
            margin-bottom: 0;
            border-radius: 0;
            width: 100%;
            height: 100%;
            max-height: none;
            background: transparent;
            opacity: 0;
            transition: opacity 0.25s ease;
        }
        .media-wrap .bubble-media.loaded {
            opacity: 1;
        }
        /* Spinner over the blur until the bytes land */
        .media-wrap.loading::after {
            content: '';
            position: absolute;
            top: 50%; left: 50%;
            width: 26px; height: 26px;
            margin: -13px 0 0 -13px;
            border: 2px solid rgba(255,255,255,0.35);
            border-left-color: #fff;
            border-radius: 50%;
            animation: spin 0.8s linear infinite;
            pointer-events: none;
        }
        .media-play {
            position: absolute;
            top: 50%; left: 50%;
            transform: translate(-50%, -50%);
            width: 52px; height: 52px;
            border-radius: 50%;
            background: rgba(0,0,0,0.55);
            border: none;
            color: #fff;
            font-size: 1.15rem;
            cursor: pointer;
            display: flex;
            align-items: center;
            justify-content: center;
            transition: background 0.15s ease;
        }
        .media-play:hover {
            background: rgba(0,0,0,0.78);
        }

