* {
    -webkit-tap-highlight-color: transparent;
}

:root {
    --sat: env(safe-area-inset-top);
    --sar: env(safe-area-inset-right);
    --sab: env(safe-area-inset-bottom);
    --sal: env(safe-area-inset-left);
}

@media (max-width: 1024px) {
    .sidebar {
        width: 260px;
    }
    
    .messages-wrapper {
        max-width: 100%;
        padding: 0 8px;
    }
    
    .chat-header {
        padding: 0 16px;
    }
    
    .info-panel {
        width: 320px;
    }
}

@media (max-width: 768px) {
    body {
        font-size: 14px;
        overflow-x: hidden;
        position: fixed;
        width: 100%;
        height: 100%;
        top: 0;
        left: 0;
        right: 0;
        bottom: 0;
    }
    
    .app-wrapper {
        position: fixed;
        top: 0;
        left: 0;
        right: 0;
        bottom: 0;
        width: 100%;
        height: 100%;
        overflow: hidden;
        background: var(--bg-primary);
    }
    
    .sidebar {
        position: fixed;
        left: 0;
        top: 0;
        bottom: 0;
        width: 280px;
        transform: translateX(-100%);
        transition: transform 0.3s cubic-bezier(0.4, 0, 0.2, 1);
        z-index: 1060;
        box-shadow: none;
        background: var(--sidebar-bg);
        border-right: 1px solid var(--sidebar-border);
        overflow-y: auto;
        overflow-x: hidden;
        padding-top: env(safe-area-inset-top);
        padding-bottom: env(safe-area-inset-bottom);
    }
    
    .sidebar.show {
        transform: translateX(0);
        box-shadow: 0 0 20px rgba(0, 0, 0, 0.3);
    }
    
    .sidebar-overlay {
        position: fixed;
        top: 0;
        left: 0;
        right: 0;
        bottom: 0;
        background: rgba(0, 0, 0, 0.5);
        backdrop-filter: blur(4px);
        z-index: 1050;
        display: none;
        opacity: 0;
        transition: opacity 0.3s ease;
    }
    
    .sidebar-overlay.show {
        display: block;
        opacity: 1;
    }
    
    .chat-main, .main-content {
        position: relative;
        width: 100%;
        height: 100%;
        display: flex;
        flex-direction: column;
        overflow: hidden;
        padding-top: env(safe-area-inset-top);
        padding-bottom: env(safe-area-inset-bottom);
    }
    
    .chat-header, .content-header {
        height: 56px;
        padding: 0 12px;
        position: relative;
        flex-shrink: 0;
        background: var(--bg-secondary);
        border-bottom: 1px solid var(--border-color);
        z-index: 100;
    }
    
    .mobile-menu-btn {
        display: flex !important;
        width: 36px;
        height: 36px;
        border-radius: 10px;
        background: var(--bg-tertiary);
        border: 1px solid var(--border-color);
    }
    
    .mobile-menu-btn:active {
        background: var(--hover-bg);
        transform: scale(0.96);
    }
    
    .chat-title {
        gap: 8px;
    }
    
    .chat-title h2, .page-title {
        font-size: 14px;
        font-weight: 600;
        max-width: 140px;
        overflow: hidden;
        text-overflow: ellipsis;
        white-space: nowrap;
    }
    
    .model-badge {
        font-size: 9px;
        padding: 2px 6px;
        background: var(--bg-tertiary);
        border-radius: 4px;
    }
    
    .ai-avatar {
        width: 32px;
        height: 32px;
    }
    
    .ai-avatar img {
        width: 100%;
        height: 100%;
        border-radius: 10px;
    }
    
    .header-right {
        gap: 4px;
    }
    
    .btn-icon {
        width: 32px;
        height: 32px;
        font-size: 14px;
    }
    
    .messages-container, .scroll-content {
        flex: 1;
        overflow-y: auto;
        padding: 12px;
        position: relative;
        display: flex;
        flex-direction: column;
        scroll-behavior: smooth;
        background: var(--bg-primary);
        -webkit-overflow-scrolling: touch;
    }
    
    .messages-wrapper {
        max-width: 100%;
        padding: 0;
        margin: 0 auto;
        width: 100%;
        display: flex;
        flex-direction: column;
        flex: 1;
    }
    
    .welcome-screen {
        min-height: auto;
        padding: 20px;
        flex: 1;
        display: flex;
        align-items: center;
        justify-content: center;
    }
    
    .welcome-content {
        padding: 16px;
        width: 100%;
    }
    
    .welcome-logo {
        width: 60px;
        height: 60px;
        margin-bottom: 16px;
    }
    
    .welcome-screen h1 {
        font-size: 22px;
        margin-bottom: 8px;
    }
    
    .welcome-screen p {
        font-size: 13px;
        margin-bottom: 24px;
    }
    
    .message {
        gap: 8px;
        margin-bottom: 16px;
    }
    
    .message-avatar {
        width: 32px;
        height: 32px;
        flex-shrink: 0;
    }
    
    .message-avatar img {
        width: 100%;
        height: 100%;
        border-radius: 10px;
        object-fit: cover;
    }
    
    .message-content {
        flex: 1;
        min-width: 0;
    }
    
    .message-header {
        margin-bottom: 4px;
    }
    
    .message-author {
        font-size: 12px;
        font-weight: 600;
    }
    
    .message-time {
        font-size: 9px;
        color: var(--text-muted);
    }
    
    .message-body {
        font-size: 13px;
        padding: 10px 14px;
        border-radius: 16px;
        line-height: 1.5;
    }
    
    .message.user .message-body {
        border-radius: 16px 16px 4px 16px;
    }
    
    .message.assistant .message-body {
        border-radius: 16px 16px 16px 4px;
    }
    
    .message-actions {
        gap: 6px;
        margin-top: 8px;
    }
    
    .message-action-btn {
        padding: 4px 10px;
        font-size: 10px;
    }
    
    .branch-btn {
        padding: 4px 8px;
        font-size: 10px;
    }
    
    .reasoning-block {
        margin-bottom: 8px;
        border-radius: 10px;
    }
    
    .reasoning-header {
        padding: 8px 12px;
        font-size: 11px;
    }
    
    .reasoning-content {
        padding: 10px 12px;
        font-size: 11px;
    }
    
    .deep-thinking-block {
        margin-bottom: 12px;
    }
    
    .deep-thinking-header {
        padding: 10px 12px;
    }
    
    .deep-thinking-header i:first-child {
        font-size: 14px;
    }
    
    .deep-thinking-header span {
        font-size: 12px;
    }
    
    .deep-thinking-content {
        padding: 12px;
        font-size: 12px;
    }
    
    .typing-indicator {
        padding: 8px 16px;
        margin: 8px auto;
        border-radius: 20px;
    }
    
    .typing-dots span {
        width: 6px;
        height: 6px;
    }
    
    .typing-text {
        font-size: 11px;
    }
    
    .input-container {
        padding: 12px;
        position: relative;
        flex-shrink: 0;
        background: var(--bg-secondary);
        border-top: 1px solid var(--border-color);
    }
    
    .input-wrapper {
        padding: 4px 4px 4px 16px;
        border-radius: 28px;
        gap: 6px;
    }
    
    .input-wrapper textarea {
        font-size: 16px !important;
        padding: 10px 0;
        max-height: 100px;
    }
    
    .btn-send, .btn-stop {
        width: 38px;
        height: 38px;
        border-radius: 19px;
    }
    
    .input-footer {
        flex-wrap: wrap;
        justify-content: center;
        gap: 8px;
        margin-top: 8px;
        font-size: 10px;
    }
    
    .input-footer span {
        display: inline-flex;
        align-items: center;
        gap: 4px;
    }
    
    kbd {
        padding: 2px 4px;
        font-size: 9px;
    }
    
    .info-panel {
        width: 100%;
        max-width: 320px;
        right: -100%;
        transition: right 0.3s ease;
        z-index: 1070;
    }
    
    .info-panel.show {
        right: 0;
    }
    
    .modal-content {
        width: 92%;
        max-width: 92%;
        margin: 16px;
        border-radius: 20px;
    }
    
    .modal-header {
        padding: 16px;
    }
    
    .modal-header h3 {
        font-size: 16px;
    }
    
    .modal-body {
        padding: 16px;
        font-size: 13px;
    }
    
    .modal-footer {
        padding: 12px 16px;
        gap: 8px;
    }
    
    .btn-secondary, .btn-primary, .btn-danger {
        padding: 8px 16px;
        font-size: 13px;
    }
    
    .toast-container {
        bottom: 80px;
        right: 12px;
        left: 12px;
        max-width: calc(100% - 24px);
        z-index: 1080;
    }
    
    .toast {
        width: 100%;
        padding: 10px 12px;
    }
    
    .scroll-bottom-btn {
        bottom: 90px;
        right: 16px;
        width: 42px;
        height: 42px;
        z-index: 100;
    }
    
    .connection-status {
        top: 60px;
        right: 12px;
        font-size: 10px;
        padding: 4px 10px;
        z-index: 90;
    }
    
    .history-list {
        max-height: calc(100vh - 200px);
    }
    
    .history-item {
        padding: 10px 12px;
    }
    
    .history-title {
        font-size: 12px;
    }
    
    .history-meta {
        font-size: 9px;
    }
    
    .user-card {
        padding: 8px 10px;
    }
    
    .user-avatar-img {
        width: 32px;
        height: 32px;
    }
    
    .user-name {
        font-size: 12px;
    }
    
    .user-status {
        font-size: 9px;
    }
    
    .sidebar-search {
        padding: 8px 12px;
        margin: 0 12px 12px;
    }
    
    .search-input {
        font-size: 12px;
    }
    
    .access-invite-modal {
        padding: 24px;
        margin: 16px;
    }
    
    .access-invite-modal .icon {
        font-size: 36px;
    }
    
    .access-invite-modal h2 {
        font-size: 20px;
    }
    
    .access-invite-modal .btn-group .btn-primary,
    .access-invite-modal .btn-group .btn-secondary {
        padding: 10px 20px;
        font-size: 13px;
        width: 100%;
    }
    
    .admin-chat-info {
        margin: 4px 12px;
        font-size: 11px;
    }
    
    .admin-chat-info .admin-detail {
        flex-direction: column;
        gap: 2px;
    }
    
    .chat-title-edit input {
        font-size: 13px;
        min-width: 80px;
        padding: 2px 8px;
    }
    
    .chat-title-edit .save-title-btn,
    .chat-title-edit .cancel-title-btn {
        padding: 2px 8px;
        font-size: 11px;
    }
    
    .chat-invites-panel .invite-input-group {
        flex-direction: column;
    }
    
    .chat-invites-panel .invite-item {
        flex-wrap: wrap;
        gap: 8px;
    }
}

@media (max-width: 480px) {
    body {
        font-size: 13px;
    }
    
    .chat-header, .content-header {
        padding: 0 10px;
        height: 52px;
    }
    
    .chat-title h2, .page-title {
        font-size: 13px;
        max-width: 100px;
    }
    
    .btn-icon {
        width: 30px;
        height: 30px;
        font-size: 12px;
    }
    
    .message {
        gap: 6px;
        margin-bottom: 12px;
    }
    
    .message-avatar {
        width: 28px;
        height: 28px;
    }
    
    .message-body {
        font-size: 12px;
        padding: 8px 12px;
        border-radius: 14px;
    }
    
    .message.user .message-body {
        border-radius: 14px 14px 4px 14px;
    }
    
    .message.assistant .message-body {
        border-radius: 14px 14px 14px 4px;
    }
    
    .message-author {
        font-size: 11px;
    }
    
    .message-time {
        font-size: 8px;
    }
    
    .message-action-btn {
        padding: 3px 8px;
        font-size: 9px;
    }
    
    .branch-btn {
        padding: 3px 6px;
        font-size: 9px;
    }
    
    .welcome-content h1 {
        font-size: 20px;
    }
    
    .welcome-content p {
        font-size: 12px;
    }
    
    .input-container {
        padding: 10px;
    }
    
    .input-wrapper {
        padding: 3px 3px 3px 12px;
    }
    
    .input-wrapper textarea {
        font-size: 15px !important;
        padding: 8px 0;
    }
    
    .btn-send, .btn-stop {
        width: 34px;
        height: 34px;
    }
    
    .input-footer {
        font-size: 9px;
    }
    
    .modal-content {
        width: 95%;
        border-radius: 16px;
    }
    
    .modal-header {
        padding: 12px 14px;
    }
    
    .modal-body {
        padding: 14px;
    }
    
    .modal-footer {
        padding: 10px 14px;
    }
    
    .btn-secondary, .btn-primary, .btn-danger {
        padding: 6px 12px;
        font-size: 12px;
    }
    
    .toast {
        padding: 8px 10px;
        font-size: 12px;
    }
    
    .scroll-bottom-btn {
        width: 38px;
        height: 38px;
        bottom: 85px;
        right: 12px;
    }
    
    .sidebar {
        width: 260px;
    }
    
    .info-panel {
        max-width: 280px;
    }
    
    .info-header {
        padding: 14px 16px;
    }
    
    .info-content {
        padding: 14px 16px;
    }
    
    .stat-item {
        font-size: 12px;
        padding: 8px 0;
    }
    
    .btn-action {
        font-size: 12px;
        padding: 8px;
    }
    
    .access-invite-modal {
        padding: 20px;
    }
    
    .access-invite-modal .icon {
        font-size: 32px;
    }
    
    .access-invite-modal h2 {
        font-size: 18px;
    }
    
    .access-invite-modal p {
        font-size: 12px;
    }
}

@media (max-width: 360px) {
    .chat-title h2, .page-title {
        max-width: 80px;
        font-size: 12px;
    }
    
    .btn-icon {
        width: 28px;
        height: 28px;
        font-size: 11px;
    }
    
    .message-body {
        font-size: 11px;
        padding: 6px 10px;
    }
    
    .message-avatar {
        width: 24px;
        height: 24px;
    }
    
    .welcome-logo {
        width: 50px;
        height: 50px;
    }
    
    .welcome-screen h1 {
        font-size: 18px;
    }
    
    .welcome-screen p {
        font-size: 11px;
    }
    
    .input-footer {
        font-size: 8px;
    }
    
    .chat-title-edit input {
        font-size: 12px;
        min-width: 60px;
        padding: 2px 6px;
    }
}

@media (max-width: 768px) and (orientation: landscape) {
    .chat-header, .content-header {
        height: 48px;
    }
    
    .messages-container, .scroll-content {
        padding: 8px;
    }
    
    .message {
        margin-bottom: 10px;
    }
    
    .message-avatar {
        width: 28px;
        height: 28px;
    }
    
    .message-body {
        padding: 6px 10px;
        font-size: 12px;
    }
    
    .input-container {
        padding: 8px;
    }
    
    .input-wrapper textarea {
        max-height: 60px;
    }
    
    .welcome-screen {
        min-height: 200px;
    }
    
    .welcome-logo {
        width: 40px;
        height: 40px;
        margin-bottom: 8px;
    }
    
    .welcome-screen h1 {
        font-size: 18px;
    }
    
    .welcome-screen p {
        font-size: 11px;
    }
    
    .mode-buttons {
        flex-direction: row;
        flex-wrap: wrap;
        gap: 8px;
    }
    
    .mode-btn {
        width: auto;
        padding: 6px 12px;
        font-size: 12px;
    }
    
    .advanced-options {
        flex-direction: row;
        justify-content: center;
    }
}

@supports (padding-top: env(safe-area-inset-top)) {
    body {
        padding-top: env(safe-area-inset-top);
        padding-bottom: env(safe-area-inset-bottom);
        padding-left: env(safe-area-inset-left);
        padding-right: env(safe-area-inset-right);
    }
    
    .chat-header, .content-header {
        padding-top: max(12px, env(safe-area-inset-top));
    }
    
    .input-container {
        padding-bottom: max(12px, env(safe-area-inset-bottom));
    }
    
    .sidebar {
        padding-top: env(safe-area-inset-top);
        padding-bottom: env(safe-area-inset-bottom);
    }
    
    .modal-content {
        margin-top: env(safe-area-inset-top);
        margin-bottom: env(safe-area-inset-bottom);
    }
    
    .connection-status {
        top: calc(60px + env(safe-area-inset-top));
    }
    
    .scroll-bottom-btn {
        bottom: calc(90px + env(safe-area-inset-bottom));
    }
    
    .toast-container {
        bottom: calc(80px + env(safe-area-inset-bottom));
    }
    
    .access-invite-modal {
        margin-top: env(safe-area-inset-top);
        margin-bottom: env(safe-area-inset-bottom);
    }
}

@media (hover: none) and (pointer: coarse) {
    .btn-icon,
    .model-selector-btn,
    .history-item,
    .quick-btn,
    .btn-send,
    .btn-stop,
    .message-action-btn,
    .modal-close,
    .toast-close,
    .user-card,
    .sidebar-header button,
    .btn-new-chat,
    .nav-item,
    .mode-btn,
    .pin-btn,
    .branch-btn {
        cursor: pointer;
        -webkit-tap-highlight-color: transparent;
    }
    
    .btn-icon:active,
    .model-selector-btn:active,
    .history-item:active,
    .quick-btn:active,
    .btn-new-chat:active,
    .user-card:active,
    .nav-item:active,
    .mode-btn:active,
    .pin-btn:active,
    .branch-btn:active {
        transform: scale(0.96);
        transition: transform 0.1s ease;
    }
    
    .message-actions {
        opacity: 1;
    }
    
    .history-delete {
        opacity: 0.6;
    }
    
    .message-edit-btn {
        opacity: 0.6;
    }
    
    .pin-btn {
        opacity: 0.6;
    }
}

.sidebar,
.sidebar-overlay,
.info-panel {
    will-change: transform;
}

body.sidebar-open {
    overflow: hidden;
    position: fixed;
    width: 100%;
    height: 100%;
}

@media (max-width: 768px) {
    ::-webkit-scrollbar {
        width: 3px;
        height: 3px;
    }
    
    ::-webkit-scrollbar-thumb {
        background: var(--primary);
        border-radius: 3px;
    }
}

@media (max-width: 768px) {
    .modal {
        position: fixed;
        top: 0;
        left: 0;
        right: 0;
        bottom: 0;
        z-index: 2000;
    }
    
    .modal-content {
        max-height: 85vh;
        overflow-y: auto;
    }
}

.sidebar.collapsed .logo-badge,
.sidebar.collapsed .btn-new-chat span,
.sidebar.collapsed .sidebar-search,
.sidebar.collapsed .section-header span:not(:first-child),
.sidebar.collapsed .history-info,
.sidebar.collapsed .user-info,
.sidebar.collapsed .history-delete,
.sidebar.collapsed .user-menu-btn,
.sidebar.collapsed .load-more-btn span,
.sidebar.collapsed .pin-btn {
    display: none;
}

.sidebar.collapsed .btn-new-chat {
    justify-content: center;
    padding: 12px;
    margin: 12px auto;
    width: 48px;
    border-radius: 24px;
}

.sidebar.collapsed .btn-new-chat i {
    margin: 0;
}

.sidebar.collapsed .section-header {
    justify-content: center;
    padding: 12px 0;
}

.sidebar.collapsed .section-header i {
    margin: 0;
}

.sidebar.collapsed .history-item {
    justify-content: center;
    padding: 12px;
}

.sidebar.collapsed .history-item i {
    margin: 0;
}

.sidebar.collapsed .user-card {
    justify-content: center;
    padding: 8px;
}

.sidebar.collapsed .user-avatar-img {
    width: 36px;
    height: 36px;
}

@media (max-width: 768px) {
    #sidebarCollapseBtn {
        display: none !important;
    }
    
    .mobile-menu-btn {
        display: flex !important;
    }
    
    .btn-icon,
    .send-btn,
    .btn-stop,
    .mode-btn,
    .history-item,
    .user-card,
    .btn-new-chat {
        min-height: 44px;
        min-width: 44px;
    }
    
    .message-action-btn,
    .branch-btn {
        padding: 6px 12px;
        min-height: 36px;
    }
    
    .pin-btn {
        min-height: 32px;
        min-width: 32px;
    }
}

.touch-active {
    transform: scale(0.95);
    transition: transform 0.1s ease;
}

.touched {
    animation: touchFeedback 0.2s ease;
}

@keyframes touchFeedback {
    0% { transform: scale(1); }
    50% { transform: scale(0.92); }
    100% { transform: scale(1); }
}

.sidebar.collapsed .branches-section .section-header span,
.sidebar.collapsed .branches-section .history-item .history-info {
    display: none;
}

.sidebar.collapsed .branches-section .history-item {
    justify-content: center;
    padding: 8px;
}

.sidebar.collapsed .branches-section .history-item i {
    margin: 0;
}

.sidebar.collapsed .available-chats-section .section-header span,
.sidebar.collapsed .available-chats-section .history-item .history-info {
    display: none;
}

.sidebar.collapsed .available-chats-section .history-item {
    justify-content: center;
    padding: 8px;
}

.sidebar.collapsed .available-chats-section .history-item i {
    margin: 0;
}