/* === Reset & Base === */
*, *::before, *::after {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
}

:root {
    --bg-primary: #111b21;
    --bg-secondary: #1f2c34;
    --bg-input: #2a3942;
    --bg-bubble-user: #005c4b;
    --bg-bubble-ai: #1f2c34;
    --text-primary: #e9edef;
    --text-secondary: #8696a0;
    --accent: #00a884;
    --accent-hover: #02b896;
    --danger: #ea4335;
    --border: #2a3942;
    --radius: 10px;
    --sidebar-width: 300px;
}

html, body {
    height: 100%;
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Helvetica, Arial, sans-serif;
    background: var(--bg-primary);
    color: var(--text-primary);
    overflow: hidden;
    -webkit-tap-highlight-color: transparent;
}

/* === Screens === */
.screen {
    display: none;
    height: 100%;
    width: 100%;
    position: absolute;
    top: 0;
    left: 0;
}

.screen.active {
    display: flex;
}

/* === Login === */
#screen-login {
    align-items: center;
    justify-content: center;
    padding: 24px;
}

.login-container {
    width: 100%;
    max-width: 360px;
}

.login-logo {
    text-align: center;
    margin-bottom: 40px;
}

.logo-icon {
    width: 80px;
    height: 80px;
    border-radius: 50%;
    background: var(--accent);
    color: #fff;
    font-size: 36px;
    font-weight: 700;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    margin-bottom: 16px;
}

.login-logo h1 {
    font-size: 26px;
    font-weight: 600;
    color: var(--text-primary);
}

.login-subtitle {
    color: var(--text-secondary);
    font-size: 14px;
    margin-top: 4px;
}

.login-form {
    display: flex;
    flex-direction: column;
    gap: 12px;
}

.form-group input {
    width: 100%;
    padding: 14px 16px;
    border: none;
    border-radius: var(--radius);
    background: var(--bg-input);
    color: var(--text-primary);
    font-size: 16px;
    outline: none;
    transition: box-shadow 0.2s;
}

.form-group input::placeholder {
    color: var(--text-secondary);
}

.form-group input:focus {
    box-shadow: 0 0 0 2px var(--accent);
}

.btn-primary {
    padding: 14px;
    border: none;
    border-radius: var(--radius);
    background: var(--accent);
    color: #fff;
    font-size: 16px;
    font-weight: 600;
    cursor: pointer;
    transition: background 0.2s;
}

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

.btn-primary:disabled {
    opacity: 0.6;
    cursor: not-allowed;
}

.btn-secondary {
    padding: 14px;
    border: 1px solid var(--border);
    border-radius: var(--radius);
    background: transparent;
    color: var(--text-secondary);
    font-size: 14px;
    cursor: pointer;
    transition: border-color 0.2s, color 0.2s;
}

.btn-secondary:hover {
    border-color: var(--accent);
    color: var(--text-primary);
}

.error-msg {
    color: var(--danger);
    font-size: 13px;
    text-align: center;
    margin-top: 4px;
}

/* === Chat Screen Layout === */
#screen-chat {
    flex-direction: row;
    position: relative;
}

/* === Sidebar === */
.sidebar {
    width: var(--sidebar-width);
    height: 100%;
    background: var(--bg-secondary);
    border-right: 1px solid var(--border);
    display: flex;
    flex-direction: column;
    flex-shrink: 0;
    z-index: 20;
}

.sidebar-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 14px 16px;
    border-bottom: 1px solid var(--border);
    flex-shrink: 0;
}

.sidebar-title {
    font-size: 16px;
    font-weight: 600;
    color: var(--text-primary);
}

.sidebar-header .btn-icon {
    font-size: 24px;
    color: var(--accent);
    font-weight: 700;
}

.conv-list {
    flex: 1;
    overflow-y: auto;
    padding: 4px 0;
}

.conv-item {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 12px 16px;
    cursor: pointer;
    transition: background 0.15s;
    border-bottom: 1px solid rgba(255,255,255,0.04);
}

.conv-item:hover {
    background: rgba(255,255,255,0.06);
}

.conv-item.active {
    background: rgba(0,168,132,0.15);
}

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

.conv-item-title {
    font-size: 14px;
    color: var(--text-primary);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.conv-item-date {
    font-size: 11px;
    color: var(--text-secondary);
    margin-top: 2px;
}

.conv-item-delete {
    background: none;
    border: none;
    color: var(--text-secondary);
    font-size: 16px;
    cursor: pointer;
    padding: 4px 6px;
    border-radius: 4px;
    opacity: 0;
    transition: opacity 0.15s, color 0.15s;
    flex-shrink: 0;
    margin-left: 8px;
}

.conv-item:hover .conv-item-delete {
    opacity: 1;
}

.conv-item-delete:hover {
    color: var(--danger);
}

/* Sidebar overlay (mobile) */
.sidebar-overlay {
    display: none;
    position: fixed;
    inset: 0;
    background: rgba(0,0,0,0.5);
    z-index: 15;
}

.sidebar-overlay.active {
    display: block;
}

/* === Chat Main === */
.chat-main {
    flex: 1;
    display: flex;
    flex-direction: column;
    min-width: 0;
    height: 100%;
}

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

.chat-title {
    font-size: 17px;
    font-weight: 600;
    flex: 1;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.btn-icon {
    background: none;
    border: none;
    color: var(--text-secondary);
    font-size: 20px;
    cursor: pointer;
    padding: 4px 8px;
    border-radius: 6px;
    transition: background 0.15s;
    flex-shrink: 0;
}

.btn-icon:hover {
    background: var(--bg-input);
    color: var(--text-primary);
}

.btn-hamburger {
    display: none;
}

/* === Chat Messages === */
.chat-messages {
    flex: 1;
    overflow-y: auto;
    padding: 16px;
    display: flex;
    flex-direction: column;
    gap: 4px;
}

.welcome-msg {
    text-align: center;
    color: var(--text-secondary);
    font-size: 14px;
    margin: auto 0;
    padding: 40px 20px;
}

.welcome-icon {
    width: 64px;
    height: 64px;
    border-radius: 50%;
    background: var(--accent);
    color: #fff;
    font-size: 28px;
    font-weight: 700;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    margin-bottom: 16px;
    opacity: 0.7;
}

/* === Message Bubbles === */
.msg-bubble {
    max-width: 85%;
    padding: 8px 12px;
    border-radius: 8px;
    font-size: 14px;
    line-height: 1.45;
    word-wrap: break-word;
    position: relative;
    white-space: pre-wrap;
}

.msg-bubble.user {
    background: var(--bg-bubble-user);
    align-self: flex-end;
    border-bottom-right-radius: 2px;
}

.msg-bubble.assistant {
    background: var(--bg-bubble-ai);
    align-self: flex-start;
    border-bottom-left-radius: 2px;
}

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

.msg-bubble.assistant .msg-time {
    text-align: left;
}

/* === Typing Indicator === */
.typing-indicator {
    padding: 0 16px 8px;
    flex-shrink: 0;
}

.typing-dots {
    display: inline-flex;
    align-items: center;
    gap: 4px;
    background: var(--bg-bubble-ai);
    padding: 10px 14px;
    border-radius: 8px;
    border-bottom-left-radius: 2px;
}

.typing-dots span {
    width: 7px;
    height: 7px;
    border-radius: 50%;
    background: var(--text-secondary);
    animation: typing-bounce 1.4s infinite ease-in-out;
}

.typing-dots span:nth-child(2) {
    animation-delay: 0.2s;
}

.typing-dots span:nth-child(3) {
    animation-delay: 0.4s;
}

@keyframes typing-bounce {
    0%, 60%, 100% { transform: translateY(0); opacity: 0.4; }
    30% { transform: translateY(-4px); opacity: 1; }
}

/* === Input Bar === */
.input-bar {
    display: flex;
    flex-direction: column;
    background: var(--bg-secondary);
    border-top: 1px solid var(--border);
    flex-shrink: 0;
}

.input-bar-row {
    display: flex;
    align-items: flex-end;
    padding: 8px 12px;
    gap: 8px;
}

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

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

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

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

.btn-send:active {
    transform: scale(0.95);
}

.btn-send:disabled {
    opacity: 0.5;
    cursor: not-allowed;
}

/* === Attach Button === */
.btn-attach {
    width: 42px;
    height: 42px;
    border-radius: 50%;
    border: none;
    background: transparent;
    color: var(--text-secondary);
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    flex-shrink: 0;
    transition: color 0.15s;
}

.btn-attach:hover {
    color: var(--text-primary);
}

.btn-attach:disabled {
    opacity: 0.4;
    cursor: not-allowed;
}

/* === Search Toggle Button === */
.btn-search-toggle {
    width: 42px;
    height: 42px;
    border-radius: 50%;
    border: none;
    background: transparent;
    color: var(--text-secondary);
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    flex-shrink: 0;
    transition: background 0.2s, color 0.2s;
}

.btn-search-toggle:hover {
    color: var(--text-primary);
}

.btn-search-toggle.active {
    background: var(--accent);
    color: #fff;
}

.btn-search-toggle.active:hover {
    background: var(--accent-hover);
}

/* === Document Toggle Button === */
.btn-doc-toggle {
    width: 42px;
    height: 42px;
    border-radius: 50%;
    border: none;
    background: transparent;
    color: var(--text-secondary);
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    flex-shrink: 0;
    transition: background 0.2s, color 0.2s;
}

.btn-doc-toggle:hover {
    color: var(--text-primary);
}

.btn-doc-toggle.active {
    background: #6c5ce7;
    color: #fff;
}

.btn-doc-toggle.active:hover {
    background: #7d6ff0;
}

/* === Generated Document Card === */
.msg-generated-doc {
    display: flex;
    align-items: center;
    gap: 10px;
    padding: 10px 12px;
    background: rgba(108, 92, 231, 0.15);
    border: 1px solid rgba(108, 92, 231, 0.3);
    border-radius: 8px;
    margin-top: 8px;
    cursor: pointer;
    text-decoration: none;
    color: inherit;
    transition: background 0.15s;
}

.msg-generated-doc:hover {
    background: rgba(108, 92, 231, 0.25);
}

.msg-generated-doc .doc-icon {
    width: 40px;
    height: 40px;
    border-radius: 8px;
    background: #6c5ce7;
    color: #fff;
    display: flex;
    align-items: center;
    justify-content: center;
    flex-shrink: 0;
}

.msg-generated-doc .doc-icon svg {
    width: 22px;
    height: 22px;
}

.msg-generated-doc .doc-info {
    min-width: 0;
    flex: 1;
}

.msg-generated-doc .doc-name {
    font-size: 13px;
    color: var(--text-primary);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.msg-generated-doc .doc-meta {
    font-size: 11px;
    color: var(--text-secondary);
    margin-top: 1px;
}

.msg-generated-doc .doc-download {
    color: var(--text-secondary);
    flex-shrink: 0;
}

.msg-generated-doc .doc-download svg {
    width: 20px;
    height: 20px;
}

/* === Attachment Preview === */
.attachment-preview {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 8px 16px;
    background: var(--bg-input);
    border-bottom: 1px solid var(--border);
    gap: 8px;
}

.attachment-preview-info {
    display: flex;
    align-items: center;
    gap: 8px;
    min-width: 0;
    flex: 1;
}

.attachment-preview-name {
    font-size: 13px;
    color: var(--text-primary);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.attachment-preview-size {
    font-size: 11px;
    color: var(--text-secondary);
    flex-shrink: 0;
}

.btn-remove-attachment {
    background: none;
    border: none;
    color: var(--text-secondary);
    font-size: 20px;
    cursor: pointer;
    padding: 2px 6px;
    border-radius: 4px;
    line-height: 1;
    flex-shrink: 0;
}

.btn-remove-attachment:hover {
    color: var(--danger);
}

/* === Message File Attachments === */
.msg-file {
    display: flex;
    align-items: center;
    gap: 10px;
    padding: 8px 10px;
    background: rgba(0,0,0,0.15);
    border-radius: 8px;
    margin-bottom: 4px;
    cursor: pointer;
    text-decoration: none;
    color: inherit;
    transition: background 0.15s;
}

.msg-file:hover {
    background: rgba(0,0,0,0.25);
}

.file-icon {
    width: 36px;
    height: 36px;
    border-radius: 6px;
    background: var(--accent);
    color: #fff;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 11px;
    font-weight: 700;
    text-transform: uppercase;
    flex-shrink: 0;
}

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

.msg-file-name {
    font-size: 13px;
    color: var(--text-primary);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.msg-file-size {
    font-size: 11px;
    color: rgba(255,255,255,0.5);
}

.msg-file-image {
    max-width: 260px;
    max-height: 260px;
    border-radius: 8px;
    margin-bottom: 4px;
    cursor: pointer;
    display: block;
}

/* === Scrollbar === */
::-webkit-scrollbar {
    width: 6px;
}

::-webkit-scrollbar-track {
    background: transparent;
}

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

/* === Telegram Button === */
.btn-telegram {
    color: #0088cc;
}

.btn-telegram:hover {
    color: #0098e0;
    background: rgba(0, 136, 204, 0.1);
}

/* === Telegram Modal === */
.telegram-overlay {
    display: none;
    position: fixed;
    inset: 0;
    background: rgba(0,0,0,0.6);
    z-index: 50;
}

.telegram-overlay.active {
    display: block;
}

.telegram-modal {
    display: none;
    position: fixed;
    bottom: 0;
    left: 0;
    right: 0;
    max-height: 70vh;
    background: var(--bg-secondary);
    border-radius: 16px 16px 0 0;
    z-index: 51;
    flex-direction: column;
    overflow: hidden;
}

.telegram-modal.active {
    display: flex;
}

.telegram-modal-header {
    display: flex;
    align-items: center;
    padding: 14px 16px;
    border-bottom: 1px solid var(--border);
    gap: 10px;
    flex-shrink: 0;
}

.telegram-modal-title {
    font-size: 17px;
    font-weight: 600;
    color: #0088cc;
    flex: 1;
}

.telegram-bot-status {
    font-size: 11px;
    color: var(--text-secondary);
    padding: 2px 8px;
    border-radius: 10px;
    background: var(--bg-input);
}

.telegram-bot-status.online {
    color: var(--accent);
    background: rgba(0, 168, 132, 0.15);
}

.telegram-contacts-list {
    flex: 1;
    overflow-y: auto;
    padding: 4px 0;
}

.telegram-contact-item {
    display: flex;
    align-items: center;
    padding: 12px 16px;
    gap: 12px;
    border-bottom: 1px solid rgba(255,255,255,0.04);
}

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

.telegram-contact-name {
    font-size: 14px;
    color: var(--text-primary);
}

.telegram-contact-chatid {
    font-size: 11px;
    color: var(--text-secondary);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    max-width: 160px;
}

.telegram-contact-delete {
    background: none;
    border: none;
    color: var(--text-secondary);
    font-size: 18px;
    cursor: pointer;
    padding: 4px 8px;
    border-radius: 4px;
    flex-shrink: 0;
}

.telegram-contact-delete:hover {
    color: var(--danger);
}

.telegram-add-form {
    display: flex;
    padding: 10px 16px;
    gap: 8px;
    border-top: 1px solid var(--border);
    flex-shrink: 0;
}

.telegram-add-form input {
    flex: 1;
    padding: 10px 12px;
    border: none;
    border-radius: var(--radius);
    background: var(--bg-input);
    color: var(--text-primary);
    font-size: 14px;
    outline: none;
    min-width: 0;
}

.telegram-add-form input::placeholder {
    color: var(--text-secondary);
}

.telegram-add-form input:focus {
    box-shadow: 0 0 0 2px #0088cc;
}

.btn-tg-add {
    width: 40px;
    height: 40px;
    border-radius: 50%;
    border: none;
    background: #0088cc;
    color: #fff;
    font-size: 22px;
    font-weight: 700;
    cursor: pointer;
    flex-shrink: 0;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: background 0.15s;
}

.btn-tg-add:hover {
    background: #0098e0;
}

/* === Forward Button on Bubbles === */
.btn-forward {
    position: absolute;
    top: 4px;
    right: 4px;
    width: 28px;
    height: 28px;
    border: none;
    border-radius: 50%;
    background: rgba(0, 136, 204, 0.15);
    color: #0088cc;
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    opacity: 0;
    transition: opacity 0.15s, background 0.15s;
}

.msg-bubble.assistant:hover .btn-forward {
    opacity: 1;
}

.btn-forward:hover {
    background: rgba(0, 136, 204, 0.3);
}

/* === Forward Dropdown Menu === */
.forward-menu {
    position: absolute;
    top: 36px;
    right: 4px;
    background: var(--bg-secondary);
    border: 1px solid var(--border);
    border-radius: 8px;
    min-width: 180px;
    max-height: 200px;
    overflow-y: auto;
    z-index: 40;
    box-shadow: 0 4px 16px rgba(0,0,0,0.3);
}

.forward-menu-item {
    padding: 10px 14px;
    font-size: 13px;
    color: var(--text-primary);
    cursor: pointer;
    border-bottom: 1px solid rgba(255,255,255,0.04);
    transition: background 0.1s;
}

.forward-menu-item:last-child {
    border-bottom: none;
}

.forward-menu-item:hover {
    background: rgba(0, 136, 204, 0.15);
}

.forward-menu-empty {
    padding: 12px 14px;
    font-size: 12px;
    color: var(--text-secondary);
    text-align: center;
}

/* === Forward Status Feedback === */
.forward-status {
    position: absolute;
    top: 4px;
    right: 4px;
    padding: 4px 10px;
    border-radius: 12px;
    font-size: 11px;
    font-weight: 600;
    z-index: 5;
    animation: forward-fade 2s ease forwards;
}

.forward-status.sending {
    background: rgba(0, 136, 204, 0.2);
    color: #0088cc;
}

.forward-status.sent {
    background: rgba(0, 168, 132, 0.2);
    color: var(--accent);
}

.forward-status.error {
    background: rgba(234, 67, 53, 0.2);
    color: var(--danger);
}

@keyframes forward-fade {
    0%, 70% { opacity: 1; }
    100% { opacity: 0; pointer-events: none; }
}

/* === Mobile (< 768px) === */
@media (max-width: 768px) {
    .sidebar {
        position: fixed;
        left: 0;
        top: 0;
        height: 100%;
        transform: translateX(-100%);
        transition: transform 0.25s ease;
        z-index: 20;
    }

    .sidebar.open {
        transform: translateX(0);
    }

    .btn-hamburger {
        display: flex;
    }

    .msg-bubble {
        max-width: 90%;
    }
}
