/* === Telegram-like Dark Theme === */
:root {
    --bg: #0e1621;
    --bg-secondary: #17212b;
    --bg-message: #182533;
    --bg-message-out: #2b5278;
    --text: #f5f5f5;
    --text-secondary: #8b98a5;
    --accent: #2aabee;
    --accent-hover: #229ed9;
    --border: #0e1621;
    --danger: #e53935;
    --success: #4caf50;
    --warning: #ff9800;
    --keyboard-bg: #17212b;
    --keyboard-btn: #2b3a4a;
    --keyboard-btn-hover: #3a4d61;
    --input-bg: #242f3d;
    --scrollbar: #2b3a4a;
}

* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
}

body {
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, sans-serif;
    background: var(--bg);
    color: var(--text);
    height: 100vh;
    overflow: hidden;
    -webkit-font-smoothing: antialiased;
}

/* === Layout === */
.app {
    display: flex;
    flex-direction: column;
    height: 100vh;
    max-width: 800px;
    margin: 0 auto;
    background: var(--bg);
    position: relative;
}

/* === Header === */
.header {
    background: var(--bg-secondary);
    padding: 12px 16px;
    display: flex;
    align-items: center;
    gap: 12px;
    border-bottom: 1px solid #0a1017;
    flex-shrink: 0;
    z-index: 10;
}

.header .avatar {
    width: 42px;
    height: 42px;
    border-radius: 50%;
    background: var(--accent);
    display: flex;
    align-items: center;
    justify-content: center;
    font-weight: 600;
    font-size: 16px;
    color: #fff;
    overflow: hidden;
}

.header .avatar img {
    width: 100%;
    height: 100%;
    object-fit: cover;
}

.header .info {
    flex: 1;
    min-width: 0;
}

.header .info .name {
    font-weight: 600;
    font-size: 15px;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.header .info .status {
    font-size: 13px;
    color: var(--text-secondary);
}

.header .actions {
    display: flex;
    gap: 8px;
}

.header .btn-icon {
    width: 36px;
    height: 36px;
    border: none;
    background: transparent;
    color: var(--text-secondary);
    border-radius: 50%;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 18px;
    transition: background 0.15s;
}

.header .btn-icon:hover {
    background: rgba(255,255,255,0.08);
    color: var(--text);
}

/* === Messages Area === */
.messages {
    flex: 1;
    overflow-y: auto;
    padding: 16px 12px;
    display: flex;
    flex-direction: column;
    gap: 6px;
    scroll-behavior: smooth;
}

.messages::-webkit-scrollbar {
    width: 6px;
}
.messages::-webkit-scrollbar-thumb {
    background: var(--scrollbar);
    border-radius: 3px;
}

.msg {
    max-width: 85%;
    padding: 8px 12px;
    border-radius: 12px;
    font-size: 15px;
    line-height: 1.4;
    position: relative;
    word-wrap: break-word;
    animation: fadeIn 0.2s ease;
}

@keyframes fadeIn {
    from { opacity: 0; transform: translateY(8px); }
    to { opacity: 1; transform: translateY(0); }
}

.msg.in {
    background: var(--bg-message);
    align-self: flex-start;
    border-bottom-left-radius: 4px;
}

.msg.out {
    background: var(--bg-message-out);
    align-self: flex-end;
    border-bottom-right-radius: 4px;
}

.msg .time {
    font-size: 11px;
    color: rgba(255,255,255,0.45);
    float: right;
    margin-left: 10px;
    margin-top: 4px;
}

.msg .meta {
    font-size: 12px;
    color: var(--accent);
    margin-bottom: 4px;
    font-weight: 500;
}

.msg b, .msg strong { font-weight: 600; }
.msg code {
    background: rgba(0,0,0,0.25);
    padding: 1px 5px;
    border-radius: 4px;
    font-family: 'SF Mono', Consolas, monospace;
    font-size: 13px;
}
.msg pre {
    background: rgba(0,0,0,0.3);
    padding: 8px;
    border-radius: 6px;
    overflow-x: auto;
    font-size: 13px;
    margin: 6px 0;
}
.msg a { color: var(--accent); }

/* System / service messages */
.msg.system {
    align-self: center;
    background: transparent;
    color: var(--text-secondary);
    font-size: 13px;
    max-width: 90%;
    text-align: center;
    padding: 4px 12px;
}

/* === Inline Keyboard === */
.inline-kb {
    display: flex;
    flex-direction: column;
    gap: 6px;
    margin-top: 8px;
    max-width: 100%;
}

.inline-kb .row {
    display: flex;
    gap: 6px;
}

.inline-kb button {
    flex: 1;
    background: var(--keyboard-btn);
    border: none;
    color: var(--text);
    padding: 10px 12px;
    border-radius: 8px;
    font-size: 14px;
    cursor: pointer;
    transition: background 0.15s;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.inline-kb button:hover {
    background: var(--keyboard-btn-hover);
}

.inline-kb button:active {
    background: var(--accent);
}

/* === Reply Keyboard (bottom) === */
.keyboard {
    background: var(--keyboard-bg);
    padding: 8px;
    border-top: 1px solid #0a1017;
    flex-shrink: 0;
    display: none;
}

.keyboard.active {
    display: block;
}

.keyboard .row {
    display: flex;
    gap: 6px;
    margin-bottom: 6px;
}

.keyboard .row:last-child {
    margin-bottom: 0;
}

.keyboard button {
    flex: 1;
    background: var(--keyboard-btn);
    border: none;
    color: var(--text);
    padding: 12px 8px;
    border-radius: 8px;
    font-size: 14px;
    cursor: pointer;
    transition: background 0.15s;
    min-height: 44px;
}

.keyboard button:hover {
    background: var(--keyboard-btn-hover);
}

.keyboard button:active {
    background: var(--accent);
}

.keyboard button.cancel {
    background: #3d2a2a;
    color: #ff8a80;
}

/* === Input Area === */
.input-area {
    background: var(--bg-secondary);
    padding: 10px 12px;
    display: flex;
    align-items: flex-end;
    gap: 8px;
    border-top: 1px solid #0a1017;
    flex-shrink: 0;
}

.input-area textarea {
    flex: 1;
    background: var(--input-bg);
    border: none;
    border-radius: 20px;
    padding: 10px 16px;
    color: var(--text);
    font-size: 15px;
    font-family: inherit;
    resize: none;
    max-height: 120px;
    outline: none;
    line-height: 1.4;
}

.input-area textarea::placeholder {
    color: var(--text-secondary);
}

.input-area .btn-send {
    width: 42px;
    height: 42px;
    border-radius: 50%;
    background: var(--accent);
    border: none;
    color: #fff;
    font-size: 18px;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    flex-shrink: 0;
    transition: background 0.15s;
}

.input-area .btn-send:hover {
    background: var(--accent-hover);
}

.input-area .btn-send:disabled {
    background: #3a4d61;
    cursor: default;
}

/* === Login Page === */
.login-page {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    height: 100vh;
    background: var(--bg);
    padding: 24px;
    text-align: center;
}

.login-page .logo {
    font-size: 64px;
    margin-bottom: 16px;
}

.login-page h1 {
    font-size: 24px;
    margin-bottom: 8px;
}

.login-page p {
    color: var(--text-secondary);
    margin-bottom: 32px;
    max-width: 320px;
}

/* === Modal / Overlay === */
.overlay {
    position: fixed;
    inset: 0;
    background: rgba(0,0,0,0.6);
    display: none;
    align-items: center;
    justify-content: center;
    z-index: 100;
}

.overlay.active {
    display: flex;
}

.modal {
    background: var(--bg-secondary);
    border-radius: 16px;
    padding: 24px;
    max-width: 400px;
    width: 90%;
    max-height: 80vh;
    overflow-y: auto;
}

.modal h3 {
    margin-bottom: 16px;
}

.modal .btn {
    display: block;
    width: 100%;
    padding: 12px;
    margin-top: 8px;
    border: none;
    border-radius: 8px;
    background: var(--keyboard-btn);
    color: var(--text);
    font-size: 15px;
    cursor: pointer;
}

.modal .btn:hover {
    background: var(--keyboard-btn-hover);
}

.modal .btn.primary {
    background: var(--accent);
}

.modal .btn.danger {
    background: var(--danger);
}

/* === Toast === */
.toast {
    position: fixed;
    bottom: 100px;
    left: 50%;
    transform: translateX(-50%) translateY(20px);
    background: #323232;
    color: #fff;
    padding: 12px 20px;
    border-radius: 8px;
    font-size: 14px;
    opacity: 0;
    transition: all 0.25s;
    z-index: 200;
    pointer-events: none;
    max-width: 90%;
    text-align: center;
}

.toast.show {
    opacity: 1;
    transform: translateX(-50%) translateY(0);
}

/* === Responsive === */
@media (max-width: 600px) {
    .app {
        max-width: 100%;
    }
    .msg {
        max-width: 92%;
    }
}

/* HTML content inside messages */
.msg html, .msg body { all: unset; }
.msg * { max-width: 100%; }

/* === Mobile adaptation === */
@media (max-width: 600px) {
    .app {
        max-width: 100%;
        height: 100dvh;
        height: -webkit-fill-available;
    }

    .header {
        padding: 10px 12px;
        padding-top: max(10px, env(safe-area-inset-top));
    }

    .header .avatar {
        width: 36px;
        height: 36px;
        font-size: 14px;
    }

    .header .info .name {
        font-size: 14px;
    }

    .header .info .status {
        font-size: 12px;
    }

    .messages {
        padding: 10px 8px;
        gap: 4px;
    }

    .msg {
        max-width: 92%;
        font-size: 15px;
        padding: 8px 10px;
        border-radius: 14px;
    }

    .msg .time {
        font-size: 10px;
    }

    .inline-kb button {
        padding: 12px 8px;
        font-size: 13px;
        min-height: 44px;
    }

    .keyboard {
        padding: 6px;
        padding-bottom: max(6px, env(safe-area-inset-bottom));
    }

    .keyboard button {
        padding: 14px 6px;
        font-size: 13px;
        min-height: 48px;
        border-radius: 10px;
    }

    .input-area {
        padding: 8px 10px;
        padding-bottom: max(8px, env(safe-area-inset-bottom));
        gap: 6px;
    }

    .input-area textarea {
        font-size: 16px; /* prevents iOS zoom */
        padding: 10px 14px;
        border-radius: 22px;
    }

    .input-area .btn-send {
        width: 44px;
        height: 44px;
    }

    .login-page {
        padding: 16px;
        padding-top: max(16px, env(safe-area-inset-top));
    }

    .auth-box {
        padding: 20px 16px;
        border-radius: 14px;
    }

    .modal {
        width: 94%;
        max-height: 85vh;
        border-radius: 14px;
        padding: 18px;
    }

    .modal .btn {
        padding: 14px;
        font-size: 15px;
        min-height: 48px;
    }

    .toast {
        bottom: 90px;
        left: 12px;
        right: 12px;
        transform: translateY(20px);
        max-width: none;
        width: auto;
    }

    .toast.show {
        transform: translateY(0);
    }
}

/* Prevent overscroll bounce issues */
html, body {
    overscroll-behavior: none;
    -webkit-tap-highlight-color: transparent;
}

/* Better pre/code on mobile */
.msg pre {
    font-size: 12px;
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
}
