:root {
    --primary: #4361ee;
    --primary-dark: #3a0ca3;
    --accent: #f72585;
    --danger: #dc3545;
    --light: #f8f9fa;
    --dark: #212529;
    --gray: #6c757d;
    --border-radius: 22px;
    --shadow: 0 4px 12px rgba(0, 0, 0, 0.08);
    --transition: all 0.25s ease;
}

* { box-sizing: border-box; margin: 0; padding: 0; }

body {
    font-family: 'Segoe UI', 'Microsoft YaHei', sans-serif;
    background: var(--light);
    color: var(--dark);
    line-height: 1.6;
    height: 100vh;
    display: flex;
    flex-direction: column;
}

header {
    background: linear-gradient(135deg, var(--primary), var(--primary-dark));
    color: white;
    padding: 0.95rem 1rem 1rem;
    box-shadow: var(--shadow);
    position: relative;
    z-index: 10;
}

.header-inner {
    max-width: 900px;
    margin: 0 auto;
}

.header-topbar {
    position: relative;
    min-height: 40px;
    display: flex;
    align-items: center;
    justify-content: center;
}

header h1 {
    font-size: 1.08rem;
    font-weight: 600;
    text-align: center;
    margin: 0;
    min-width: 0;
    width: 100%;
    padding: 0 48px;
}

.header-buttons {
    position: absolute;
    top: 50%;
    right: 0;
    transform: translateY(-50%);
    display: flex;
    align-items: center;
    gap: 10px;
}

.header-menu-wrap,
.composer-menu-wrap {
    position: relative;
}

.header-btn,
.action-button,
#send-button {
    transition: var(--transition);
}

.header-btn {
    background: rgba(255, 255, 255, 0.18);
    color: white;
    border: 1px solid rgba(255, 255, 255, 0.16);
    padding: 0.48rem 0.9rem;
    border-radius: 20px;
    cursor: pointer;
    font-size: 0.86rem;
    text-decoration: none;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 6px;
}

.header-btn.icon-only {
    width: 40px;
    height: 40px;
    padding: 0;
    border-radius: 50%;
    font-size: 1rem;
}

.header-btn:hover { background: rgba(255, 255, 255, 0.28); }
.header-btn:disabled { opacity: 0.65; cursor: not-allowed; }
.header-btn.loaded { background: rgba(40, 167, 69, 0.8); }

.header-menu,
.composer-menu {
    position: absolute;
    right: 0;
    top: calc(100% + 10px);
    min-width: 168px;
    background: rgba(255, 255, 255, 0.98);
    border: 1px solid rgba(255, 255, 255, 0.45);
    border-radius: 18px;
    box-shadow: 0 10px 24px rgba(17, 24, 39, 0.14);
    padding: 0.7rem;
    display: none;
    z-index: 60;
}

.header-menu.open,
.composer-menu.open {
    display: block;
}

.header-menu-item,
.composer-menu-item {
    width: 100%;
    border: none;
    background: transparent;
    color: var(--dark);
    text-decoration: none;
    display: flex;
    align-items: center;
    gap: 10px;
    padding: 0.7rem 0.8rem;
    border-radius: 14px;
    cursor: pointer;
    font-size: 0.92rem;
    transition: var(--transition);
}

.header-menu-item:hover,
.composer-menu-item:hover {
    background: #eef3ff;
    transform: translateY(-1px);
}

.chat-tools {
    display: none;
    margin-top: 0.65rem;
    background: rgba(255, 255, 255, 0.98);
    border-radius: 18px;
    box-shadow: 0 10px 24px rgba(17, 24, 39, 0.12);
    padding: 0.9rem;
    border: 1px solid rgba(255, 255, 255, 0.45);
    color: var(--dark);
    position: relative;
    z-index: 15;
}

.chat-tools.open { display: block; }

.search-bar {
    display: flex;
    gap: 10px;
    align-items: center;
    flex-wrap: wrap;
    background: linear-gradient(180deg, #f8fbff 0%, #f3f7fd 100%);
    border: 1px solid #e7eef8;
    border-radius: 16px;
    padding: 0.75rem;
}

.search-input-wrap {
    flex: 1;
    min-width: 220px;
    position: relative;
}

#search-input {
    width: 100%;
    border: 1px solid #d8dee4;
    border-radius: 14px;
    padding: 0.75rem 4.8rem 0.75rem 0.9rem;
    font-size: 0.94rem;
}

#search-input:focus,
#nickname-input:focus,
#message-input:focus {
    outline: none;
    border-color: var(--primary);
    box-shadow: 0 0 0 2px rgba(67, 97, 238, 0.15);
}

.search-inline-icon {
    position: absolute;
    right: 0.9rem;
    top: 50%;
    transform: translateY(-50%);
    color: var(--gray);
    pointer-events: none;
}

.search-clear-btn {
    position: absolute;
    right: 2.6rem;
    top: 50%;
    transform: translateY(-50%);
    width: 22px;
    height: 22px;
    border: none;
    border-radius: 50%;
    background: #e9eef5;
    color: #6f7b8a;
    font-size: 0.72rem;
    cursor: pointer;
    display: none;
    align-items: center;
    justify-content: center;
    padding: 0;
}

.search-clear-btn.show {
    display: inline-flex;
}

.search-clear-btn:hover {
    background: #dce7f5;
    color: var(--primary-dark);
}

.search-btn,
.secondary-btn {
    border: none;
    border-radius: 14px;
    padding: 0.72rem 1rem;
    cursor: pointer;
    font-size: 0.9rem;
    white-space: nowrap;
    transition: var(--transition);
}

.search-btn { background: var(--primary); color: white; }
.search-btn:hover { opacity: 0.92; }
.secondary-btn { background: #eef2f7; color: var(--dark); }
.secondary-btn:hover { background: #e3e8ef; }

.search-results {
    display: none;
    border-top: 1px solid #edf0f2;
    padding-top: 0.75rem;
    margin-top: 0.15rem;
}

.search-results.show { display: block; }

.search-results-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    gap: 10px;
    margin-bottom: 0.6rem;
    flex-wrap: wrap;
}

.search-results-title {
    font-size: 0.9rem;
    color: var(--gray);
}

.search-result-list {
    display: flex;
    flex-direction: column;
    gap: 10px;
    max-height: 280px;
    overflow-y: auto;
}

.search-result-item {
    border: 1px solid #e9ecef;
    border-radius: 14px;
    padding: 0.8rem 0.9rem;
    background: #fafbfc;
    cursor: pointer;
    transition: var(--transition);
}

.search-result-item:hover {
    border-color: rgba(67, 97, 238, 0.4);
    background: #f4f7ff;
    transform: translateY(-1px);
}

.search-result-meta {
    display: flex;
    justify-content: space-between;
    gap: 10px;
    flex-wrap: wrap;
    font-size: 0.78rem;
    color: var(--gray);
    margin-bottom: 0.45rem;
}

.search-result-content {
    font-size: 0.9rem;
    color: var(--dark);
    word-break: break-word;
}

.search-result-content mark,
.search-result-meta mark {
    background: #ffe58f;
    padding: 0 2px;
    border-radius: 4px;
}

.search-empty {
    color: var(--gray);
    text-align: center;
    padding: 1rem 0;
    font-size: 0.9rem;
}

#chat-container {
    flex: 1;
    display: flex;
    flex-direction: column;
    width: 100%;
    max-width: 900px;
    margin: 0 auto;
    padding: 1rem;
    overflow: hidden;
}

#load-more-container {
    text-align: center;
    padding: 10px;
    display: none;
}

#load-more-btn {
    background: #e9ecef;
    border: none;
    padding: 8px 20px;
    border-radius: 20px;
    cursor: pointer;
    font-size: 0.85rem;
    color: var(--gray);
}

#load-more-btn:hover { background: #dee2e6; }
#load-more-btn:disabled { opacity: 0.5; cursor: not-allowed; }

#chat-messages {
    flex: 1;
    overflow-y: auto;
    padding: 0.5rem;
    display: flex;
    flex-direction: column;
    gap: 0.75rem;
}

.message {
    max-width: 85%;
    padding: 0.9rem 1rem 0.8rem;
    border-radius: var(--border-radius);
    position: relative;
    word-break: break-word;
    animation: fadeIn 0.25s ease;
    box-shadow: var(--shadow);
}

.message.other {
    align-self: flex-start;
    background: white;
    border-bottom-left-radius: 8px;
}

@keyframes fadeIn {
    from { opacity: 0; transform: translateY(10px); }
    to { opacity: 1; transform: translateY(0); }
}

.message .user {
    font-weight: 600;
    margin-bottom: 0.25rem;
    font-size: 0.85rem;
    padding-right: 64px;
}

.message .content { line-height: 1.5; }

.message .timestamp {
    font-size: 0.7rem;
    opacity: 0.7;
    margin-top: 0.5rem;
    text-align: right;
}

.message-actions {
    position: absolute;
    top: 8px;
    right: 8px;
    display: flex;
    gap: 6px;
}

.copy-btn,
.delete-btn {
    width: 28px;
    height: 28px;
    border-radius: 50%;
    border: none;
    font-size: 12px;
    cursor: pointer;
    opacity: 0.85;
    transition: var(--transition);
    display: flex;
    align-items: center;
    justify-content: center;
}

.copy-btn { background: rgba(0, 0, 0, 0.08); color: var(--gray); }
.delete-btn { background: rgba(220, 53, 69, 0.12); color: var(--danger); }
.copy-btn:hover,
.delete-btn:hover { opacity: 1; transform: scale(1.08); }

.message-highlight { animation: messageHighlight 2.3s ease; }

@keyframes messageHighlight {
    0% { background: #fff9db; }
    60% { background: #fff3bf; }
    100% { background: white; }
}

.media-container {
    position: relative;
    margin-top: 0.5rem;
    border-radius: 12px;
    overflow: hidden;
}

.media-placeholder {
    background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
    padding: 2rem;
    text-align: center;
    color: white;
    cursor: pointer;
    border-radius: 12px;
    transition: var(--transition);
}

.media-placeholder:hover { opacity: 0.92; transform: scale(1.02); }
.media-placeholder.video-placeholder { background: linear-gradient(135deg, #11998e 0%, #38ef7d 100%); }
.media-placeholder i { font-size: 2rem; margin-bottom: 0.5rem; display: block; }
.media-placeholder span { font-size: 0.85rem; }

.chat-image,
.chat-video {
    max-width: 100%;
    max-height: 300px;
    border-radius: 12px;
    display: block;
    cursor: pointer;
}

.chat-video { background: #000; }
.media-wrapper { position: relative; display: inline-block; }

.download-btn {
    position: absolute;
    top: 8px;
    right: 8px;
    background: rgba(0, 0, 0, 0.6);
    color: white;
    border: none;
    width: 32px;
    height: 32px;
    border-radius: 50%;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    opacity: 0;
    transition: var(--transition);
}

.media-wrapper:hover .download-btn { opacity: 1; }

.file-attachment {
    display: flex;
    align-items: center;
    gap: 10px;
    padding: 12px;
    background: rgba(0,0,0,0.05);
    border-radius: 12px;
    margin-top: 0.5rem;
}

.file-icon { font-size: 1.5rem; opacity: 0.8; }
.file-info { flex: 1; min-width: 0; }

.file-name {
    font-size: 0.9rem;
    font-weight: 500;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.file-download-btn {
    background: var(--primary);
    color: white;
    border: none;
    width: 32px;
    height: 32px;
    border-radius: 50%;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    flex-shrink: 0;
}

#message-form-container {
    background: white;
    padding: 0.75rem;
    border-radius: var(--border-radius);
    box-shadow: var(--shadow);
    max-width: 900px;
    margin: 0 auto;
    width: 100%;
}

#nickname-container {
    display: flex;
    align-items: center;
    margin-bottom: 8px;
    padding: 0 8px;
    flex-wrap: wrap;
    gap: 6px;
}

#nickname-icon { color: var(--gray); font-size: 0.9rem; }

#nickname-input {
    border: none;
    border-bottom: 1px dashed #ddd;
    padding: 4px 0;
    font-size: 0.9rem;
    color: var(--primary);
    width: 160px;
    background: transparent;
}

.nickname-saved {
    font-size: 0.75rem;
    color: var(--gray);
    margin-left: 6px;
}

.nickname-saved.show { color: var(--primary); }

#message-form { position: relative; }
#input-wrapper { position: relative; }

#message-input {
    display: block;
    padding: 0.75rem 7rem 0.75rem 1rem;
    border: 1px solid #ddd;
    border-radius: var(--border-radius);
    font-size: 1rem;
    min-height: 48px;
    max-height: 120px;
    resize: none;
    overflow-y: auto;
    width: 100%;
}

.action-buttons {
    position: absolute;
    right: 0.75rem;
    top: 50%;
    transform: translateY(-50%);
    display: flex;
    gap: 0.4rem;
    align-items: center;
}

.composer-menu {
    bottom: calc(100% + 10px);
    top: auto;
}

.action-button,
#send-button {
    width: 32px;
    height: 32px;
    border-radius: 50%;
    border: none;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: var(--transition);
}

.action-button {
    background: rgba(255, 255, 255, 0.9);
    color: var(--gray);
    font-size: 1.05rem;
}

.action-button:hover { background: rgba(0, 0, 0, 0.05); color: var(--primary); }

#send-button {
    background: var(--primary);
    color: white;
}

#send-button:hover { background: var(--primary-dark); transform: scale(1.05); }
#send-button:disabled { opacity: 0.5; cursor: not-allowed; }

.upload-progress {
    position: fixed;
    bottom: 100px;
    left: 50%;
    transform: translateX(-50%);
    background: white;
    padding: 15px 25px;
    border-radius: 12px;
    box-shadow: 0 4px 20px rgba(0,0,0,0.15);
    display: none;
    z-index: 1000;
    min-width: 280px;
}

.upload-progress.show { display: block; }
.progress-bar { height: 6px; background: #e9ecef; border-radius: 3px; overflow: hidden; margin-top: 8px; }
.progress-fill { height: 100%; background: linear-gradient(90deg, var(--primary), var(--accent)); width: 0%; transition: width 0.3s ease; }

.progress-text {
    font-size: 0.85rem;
    color: var(--gray);
    display: flex;
    justify-content: space-between;
}

.media-lightbox,
.qr-modal,
.confirm-modal {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    display: none;
    justify-content: center;
    align-items: center;
    z-index: 3000;
}

.media-lightbox { background: rgba(0, 0, 0, 0.95); z-index: 2000; }
.qr-modal { background: rgba(0, 0, 0, 0.7); }
.confirm-modal { background: rgba(0, 0, 0, 0.5); z-index: 3500; }
.lightbox-content { max-width: 90%; max-height: 90%; }

.close-lightbox {
    position: absolute;
    top: 20px;
    right: 20px;
    color: white;
    font-size: 2rem;
    cursor: pointer;
    z-index: 2001;
    opacity: 0.8;
}

.image-nav {
    position: absolute;
    top: 50%;
    width: 100%;
    display: flex;
    justify-content: space-between;
    padding: 0 20px;
    transform: translateY(-50%);
}

.nav-button {
    background: rgba(255, 255, 255, 0.1);
    color: white;
    border: none;
    width: 50px;
    height: 50px;
    border-radius: 50%;
    cursor: pointer;
    font-size: 1.2rem;
}

.nav-button:disabled { opacity: 0.3; cursor: not-allowed; }
.nav-button:hover:not(:disabled) { background: rgba(255, 255, 255, 0.2); }

.hidden-input { display: none; }

.copy-toast {
    position: fixed;
    bottom: 20px;
    left: 50%;
    transform: translateX(-50%);
    background: rgba(0, 0, 0, 0.82);
    color: white;
    padding: 10px 20px;
    border-radius: 8px;
    opacity: 0;
    transition: opacity 0.3s;
    z-index: 4000;
    max-width: calc(100vw - 40px);
    text-align: center;
}

.copy-toast.show { opacity: 1; }

.link-container { display: inline; }

.link-actions {
    display: inline-flex;
    gap: 2px;
    margin-left: 4px;
    vertical-align: middle;
}

.link-action-btn {
    background: none;
    border: none;
    cursor: pointer;
    opacity: 0.55;
    padding: 2px 4px;
    font-size: 0.85em;
    color: inherit;
}

.link-action-btn:hover { opacity: 1; }

.qr-content,
.confirm-content {
    background: white;
    border-radius: 14px;
    text-align: center;
    margin: 20px;
}

.qr-content { padding: 20px; }
.qr-image { max-width: 200px; }

.confirm-content {
    padding: 24px;
    max-width: 420px;
    width: calc(100% - 32px);
}

.confirm-content h3 { margin-bottom: 12px; color: var(--danger); }
.confirm-content p { margin-bottom: 18px; color: var(--gray); }

.confirm-actions {
    display: flex;
    gap: 10px;
    justify-content: center;
    flex-wrap: wrap;
}

.confirm-actions button {
    padding: 10px 24px;
    border: none;
    border-radius: 10px;
    cursor: pointer;
    font-size: 0.9rem;
}

.cancel-btn { background: #e9ecef; color: var(--dark); }
.danger-action-btn { background: var(--danger); color: white; }

.footer {
    text-align: center;
    padding: 8px;
    color: #888;
    font-size: 13px;
    border-top: 1px solid #eee;
}

.footer a { color: #888; text-decoration: none; }
.footer a:hover { color: var(--primary); }

@media (max-width: 768px) {
    #chat-container,
    #message-form-container { max-width: 100%; }

    #chat-container { padding: 0.6rem; }
    .chat-tools { padding: 0.72rem; border-radius: 16px; }
    .message { max-width: 94%; }
    .search-bar,
    .header-buttons { justify-content: center; }
    header { padding: 0.85rem 0.75rem; }
    .header-topbar { min-height: 36px; }
    header h1 { padding: 0 44px; }
    .header-buttons { gap: 8px; }
    .header-btn.icon-only { width: 38px; height: 38px; }
}

@media (max-width: 480px) {
    header { padding: 0.72rem 0.6rem 0.6rem; }
    header h1 { font-size: 1rem; line-height: 1.25; text-align: center; padding: 0 42px; }
    #chat-container { padding: 0.5rem; }
    .chat-tools { padding: 0.58rem; border-radius: 14px; box-shadow: 0 3px 10px rgba(0,0,0,0.08); }
    #message-input { padding-right: 5.2rem; }
    .action-button, #send-button { width: 28px; height: 28px; font-size: 0.95rem; }
    .header-buttons { right: 0; gap: 6px; }
    .action-buttons {
        right: 0.5rem;
        top: 50%;
    }
    .header-btn.icon-only { width: 34px; height: 34px; font-size: 0.9rem; }
    .header-menu,
    .composer-menu {
        min-width: 152px;
        border-radius: 14px;
        padding: 0.45rem;
    }
    .header-menu-item,
    .composer-menu-item {
        padding: 0.62rem 0.7rem;
        font-size: 0.86rem;
        border-radius: 12px;
    }
    .search-bar {
        display: grid;
        grid-template-columns: 1fr 1fr;
        gap: 8px;
        padding: 0.55rem;
        border-radius: 14px;
        background: #f7faff;
    }
    .search-input-wrap { min-width: 100%; grid-column: 1 / -1; }
    #search-input { padding: 0.68rem 4.2rem 0.68rem 0.8rem; font-size: 0.88rem; border-radius: 12px; }
    .search-btn, .secondary-btn { width: 100%; padding: 0.68rem 0.5rem; font-size: 0.84rem; border-radius: 12px; }
    .search-result-item { padding: 0.7rem 0.72rem; border-radius: 12px; }
    .search-result-meta { font-size: 0.72rem; gap: 4px; margin-bottom: 0.35rem; }
    .search-result-content { font-size: 0.84rem; line-height: 1.45; }
    .message .user { padding-right: 70px; }
}
