/* Reset and Base Styles */
* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
}

body {
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, sans-serif;
    background: linear-gradient(135deg, #1a1a2e 0%, #16213e 100%);
    color: #e8d5c4;
    line-height: 1.6;
}

.container {
    width: 100%;
    height: 100vh;
    display: flex;
    flex-direction: column;
}

/* Header */
header {
    background: rgba(255, 255, 255, 0.03);
    backdrop-filter: blur(10px);
    padding: 1rem 2rem;
    border-bottom: 1px solid rgba(232, 213, 196, 0.1);
    display: flex;
    justify-content: space-between;
    align-items: center;
}

header h1 {
    font-size: 1.5rem;
    font-weight: 400;
    color: #f8e8d8;
    letter-spacing: 1px;
}

.header-actions {
    display: flex;
    gap: 0.75rem;
    align-items: center;
}

/* Main Content */
main {
    flex: 1;
    overflow: hidden;
    display: flex;
    gap: 0;
}

.panel {
    background: rgba(255, 255, 255, 0.02);
    backdrop-filter: blur(10px);
    border-radius: 0;
    padding: 1.5rem;
    flex: 1;
    display: flex;
    flex-direction: column;
}

.panel h2 {
    font-size: 1.25rem;
    margin-bottom: 1rem;
    color: #ffffff;
}

/* Character Selection */
#char-selection {
    flex: 0 0 240px;
    background: rgba(255, 255, 255, 0.02);
    border-right: 1px solid rgba(232, 213, 196, 0.1);
    padding: 0;
}

.char-selection-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 1rem 1rem 0.5rem 1rem;
}

.char-selection-header h2 {
    margin: 0;
    font-size: 1rem;
    color: #aaa;
}

.char-list {
    display: flex;
    flex-direction: column;
    gap: 0;
    overflow-y: auto;
    padding: 0;
}

.char-card {
    background: transparent;
    border: none;
    border-left: 3px solid transparent;
    cursor: pointer;
    transition: all 0.3s ease;
    overflow: hidden;
    display: flex;
    flex-direction: row;
    align-items: center;
    gap: 0.75rem;
    padding: 0.5rem 0.75rem;
    margin: 0;
}

.char-card:hover {
    background: #252525;
    border-left-color: #666;
}

.char-card.selected {
    background: #2a2a2a;
    border-left-color: #4a9eff;
}

/* 小卡片样式（未选中） */
.char-card-avatar {
    width: 50px;
    height: 50px;
    object-fit: cover;
    border-radius: 4px;
    background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
    flex-shrink: 0;
    transition: all 0.3s ease;
}

/* 大卡片样式（选中） */
.char-card.selected .char-card-avatar {
    width: 100%;
    height: auto;
    max-height: 500px;
    min-height: 350px;
    object-fit: contain;
    border-radius: 8px;
    background: #000;
}

.char-card.selected {
    flex-direction: column;
    align-items: stretch;
    padding: 1rem;
    margin: 0.5rem 0;
    background: #2a2a2a;
    border-radius: 8px;
    border: 2px solid #4a9eff;
}

.char-card-info {
    flex: 1;
    display: flex;
    flex-direction: column;
    gap: 0.25rem;
    min-width: 0;
}

.char-card.selected .char-card-info {
    padding-top: 0.5rem;
}

.char-card-name {
    font-weight: 600;
    font-size: 0.9rem;
    color: #fff;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.char-card.selected .char-card-name {
    font-size: 1.1rem;
    white-space: normal;
}

.char-card-desc {
    font-size: 0.75rem;
    color: #888;
    display: -webkit-box;
    -webkit-line-clamp: 1;
    -webkit-box-orient: vertical;
    overflow: hidden;
    line-height: 1.3;
}

.char-card.selected .char-card-desc {
    -webkit-line-clamp: 2;
    color: #aaa;
}

.empty-state {
    color: #666;
    text-align: center;
    padding: 2rem;
}

/* Chat Interface */
#chat-interface {
    flex: 1 1 auto;
    min-width: 0;
}

.chat-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 1rem;
    padding-bottom: 1rem;
    border-bottom: 1px solid #444;
}

.char-info {
    display: flex;
    flex-direction: column;
    position: relative;
}

.char-name {
    font-size: 1.25rem;
    font-weight: 600;
    cursor: pointer;
    transition: color 0.2s;
    display: inline-block;
}

.char-name:hover {
    color: #4a9eff;
}

.char-info-tooltip {
    position: absolute;
    top: 100%;
    left: 0;
    margin-top: 0.5rem;
    background: #1a1a1a;
    border: 2px solid #4a9eff;
    border-radius: 8px;
    padding: 1rem;
    min-width: 300px;
    max-width: 600px;
    max-height: 70vh;
    overflow-y: auto;
    z-index: 1000;
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.5);
}

.char-info-tooltip.hidden {
    display: none;
}

.char-info-tooltip h3 {
    color: #4a9eff;
    margin-bottom: 0.5rem;
    font-size: 1.1rem;
}

.char-info-tooltip p {
    color: #ccc;
    font-size: 0.9rem;
    line-height: 1.6;
    margin-bottom: 0.75rem;
    white-space: pre-wrap;
    word-wrap: break-word;
    overflow-wrap: break-word;
    max-width: 100%;
}

.char-info-tooltip .char-trait {
    color: #888;
    font-size: 0.85rem;
    margin-bottom: 0.5rem;
}

.char-info-tooltip .char-trait strong {
    color: #aaa;
}

.session-info {
    font-size: 0.875rem;
    color: #888;
}

.chat-messages {
    flex: 1;
    overflow-y: auto;
    padding: 1rem;
    background: rgba(0, 0, 0, 0.2);
    border-radius: 12px;
    margin-bottom: 1rem;
    display: flex;
    flex-direction: column;
    gap: 1rem;
}

.message {
    display: flex;
    flex-direction: column;
    max-width: 80%;
    animation: fadeIn 0.3s;
}

@keyframes fadeIn {
    from { opacity: 0; transform: translateY(10px); }
    to { opacity: 1; transform: translateY(0); }
}

.message.user {
    align-self: flex-end;
}

.message.assistant {
    align-self: flex-start;
}

.message-header {
    font-size: 0.75rem;
    color: #888;
    margin-bottom: 0.25rem;
    font-weight: 600;
    display: flex;
    align-items: center;
    gap: 0.5rem;
}

.message.user .message-header {
    text-align: right;
    justify-content: flex-end;
}

.message-timestamp {
    font-size: 0.7rem;
    color: #666;
    font-weight: 400;
}

.message-content {
    background: rgba(255, 255, 255, 0.05);
    padding: 0.75rem 1rem;
    border-radius: 16px;
    white-space: pre-wrap;
    word-wrap: break-word;
    border: 1px solid rgba(232, 213, 196, 0.1);
}

.message.user .message-content {
    background: rgba(232, 169, 169, 0.2);
    border-bottom-right-radius: 4px;
    border-color: rgba(232, 169, 169, 0.3);
}

.message.assistant .message-content {
    background: rgba(169, 196, 232, 0.15);
    border-bottom-left-radius: 4px;
    border-color: rgba(169, 196, 232, 0.2);
}

.message.system .message-content {
    background: #3a2a2a;
    font-style: italic;
}

.message-content.streaming::after {
    content: '▋';
    animation: blink 1s infinite;
}

@keyframes blink {
    0%, 49% { opacity: 1; }
    50%, 100% { opacity: 0; }
}

/* Chat Input */
.chat-input-container {
    display: flex;
    gap: 0.5rem;
}

#message-input {
    flex: 1;
    background: #333;
    border: 2px solid #444;
    border-radius: 6px;
    padding: 0.75rem;
    color: #e0e0e0;
    font-family: inherit;
    font-size: 1rem;
    resize: vertical;
    min-height: 60px;
}

#message-input:focus {
    outline: none;
    border-color: #4a9eff;
}

#message-input::placeholder {
    color: #666;
}

/* Buttons */
.btn-primary, .btn-secondary {
    padding: 0.5rem 1rem;
    border: 1px solid rgba(232, 213, 196, 0.2);
    border-radius: 12px;
    font-size: 0.9rem;
    font-weight: 300;
    cursor: pointer;
    transition: all 0.3s ease;
    backdrop-filter: blur(10px);
}

.btn-primary {
    background: rgba(232, 169, 169, 0.15);
    color: #f8d4d4;
    border-color: rgba(232, 169, 169, 0.3);
}

.btn-primary:hover:not(:disabled) {
    background: rgba(232, 169, 169, 0.25);
    border-color: rgba(232, 169, 169, 0.5);
    transform: translateY(-1px);
    box-shadow: 0 4px 12px rgba(232, 169, 169, 0.2);
}

.btn-primary:disabled {
    background: rgba(100, 100, 100, 0.1);
    color: rgba(232, 213, 196, 0.3);
    cursor: not-allowed;
    opacity: 0.4;
}

.btn-secondary {
    background: rgba(255, 255, 255, 0.05);
    color: #e8d5c4;
}

.btn-secondary:hover:not(:disabled) {
    background: rgba(255, 255, 255, 0.1);
    transform: translateY(-1px);
}

.btn-secondary:disabled {
    background: rgba(100, 100, 100, 0.05);
    color: rgba(232, 213, 196, 0.3);
    cursor: not-allowed;
    opacity: 0.4;
}

.btn-danger {
    background: rgba(200, 150, 150, 0.15);
    color: #f0c8c8;
    border-color: rgba(200, 150, 150, 0.3);
}

.btn-danger:hover:not(:disabled) {
    background: rgba(200, 150, 150, 0.25);
    border-color: rgba(200, 150, 150, 0.5);
    transform: translateY(-1px);
}

.btn-danger:disabled {
    background: rgba(100, 100, 100, 0.1);
    color: rgba(232, 213, 196, 0.3);
    cursor: not-allowed;
    opacity: 0.4;
}

.btn-icon {
    padding: 0.5rem;
    min-width: 40px;
    min-height: 40px;
    font-size: 1.2rem;
    display: flex;
    align-items: center;
    justify-content: center;
}

#send-btn {
    position: relative;
}

#send-btn.sending {
    pointer-events: none;
}

#send-btn.sending::after {
    content: '⋯';
    animation: dots 1.5s infinite;
    margin-left: 0.25rem;
}

@keyframes dots {
    0%, 20% { content: '⋯'; }
    40% { content: '⋯⋯'; }
    60%, 100% { content: '⋯⋯⋯'; }
}

/* Footer */
footer {
    background: #2a2a2a;
    padding: 0.75rem 2rem;
    border-top: 2px solid #3a3a3a;
    display: flex;
    justify-content: space-between;
    font-size: 0.875rem;
    color: #888;
}

/* Loading Overlay */
#loading-overlay {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background: rgba(0, 0, 0, 0.8);
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    z-index: 1000;
}

.spinner {
    width: 50px;
    height: 50px;
    border: 4px solid #333;
    border-top-color: #4a9eff;
    border-radius: 50%;
    animation: spin 1s linear infinite;
}

@keyframes spin {
    to { transform: rotate(360deg); }
}

#loading-overlay p {
    margin-top: 1rem;
    font-size: 1.125rem;
}

/* Utility Classes */
.hidden {
    display: none !important;
}

/* Scrollbar Styling */
::-webkit-scrollbar {
    width: 8px;
    height: 8px;
}

::-webkit-scrollbar-track {
    background: #1a1a1a;
}

::-webkit-scrollbar-thumb {
    background: #444;
    border-radius: 4px;
}

::-webkit-scrollbar-thumb:hover {
    background: #555;
}

/* Text Formatting Styles - SillyTavern Style */
/* Default assistant message text */
.message.assistant .message-content {
    font-style: normal;
    color: #b8a89a;
}

/* Actions in *asterisks* - italic and dimmer */
.message.assistant .message-content em {
    font-style: italic;
    color: #9d8d7f;
    font-weight: 300;
}

/* Quoted text (dialogue) - bright, normal font, stands out */
.message.assistant .message-content q {
    font-style: normal;
    color: #ffd4a3;
    font-weight: 400;
}

/* User messages - pure white, clear and bright */
.message.user .message-content {
    font-style: normal;
    color: #ffffff;
    font-weight: 400;
}

.message-content br {
    display: block;
    content: "";
    margin: 0.5em 0;
}

/* ============================================================================
   Mobile Responsive Design
   ============================================================================ */

/* Mobile Navigation Toggle Button */
.mobile-char-toggle {
    display: none;
    position: fixed;
    bottom: 20px;
    left: 20px;
    z-index: 999;
    width: 56px;
    height: 56px;
    border-radius: 50%;
    background: linear-gradient(135deg, rgba(232, 169, 169, 0.3), rgba(232, 169, 169, 0.2));
    border: 2px solid rgba(232, 169, 169, 0.5);
    backdrop-filter: blur(10px);
    color: #f8d4d4;
    font-size: 24px;
    cursor: pointer;
    box-shadow: 0 4px 20px rgba(0, 0, 0, 0.3);
    transition: all 0.3s ease;
}

.mobile-char-toggle:active {
    transform: scale(0.95);
}

.mobile-char-toggle.active {
    background: linear-gradient(135deg, rgba(232, 169, 169, 0.5), rgba(232, 169, 169, 0.4));
}

/* Tablet Size (768px - 1024px) */
@media (max-width: 1024px) {
    header {
        padding: 0.75rem 1rem;
    }

    header h1 {
        font-size: 1.25rem;
    }

    #char-selection {
        flex: 0 0 200px;
    }

    .char-card-avatar {
        width: 40px;
        height: 40px;
    }

    .char-card.selected .char-card-avatar {
        max-height: 350px;
        min-height: 250px;
    }
}

/* Mobile Landscape and Small Tablets (max-width: 900px) */
@media (max-width: 900px) {
    main {
        position: relative;
        overflow: visible; /* Allow scrolling on mobile */
    }

    #char-selection {
        position: fixed;
        left: 0;
        top: 0;
        bottom: 0;
        z-index: 998;
        width: 280px;
        max-width: 80vw;
        transform: translateX(-100%);
        transition: transform 0.3s ease;
        border-right: 2px solid rgba(232, 213, 196, 0.2);
        box-shadow: 4px 0 20px rgba(0, 0, 0, 0.3);
        overflow-y: auto; /* Allow scrolling in char selection */
    }

    #char-selection.mobile-visible {
        transform: translateX(0);
    }

    .mobile-char-toggle {
        display: flex;
        align-items: center;
        justify-content: center;
    }

    #chat-interface {
        width: 100%;
        margin-left: 0;
        display: flex;
        flex-direction: column;
        min-height: 0; /* Important for flex child scrolling */
    }

    /* Mobile Overlay for char selection */
    .mobile-overlay {
        display: none;
        position: fixed;
        top: 0;
        left: 0;
        right: 0;
        bottom: 0;
        background: rgba(0, 0, 0, 0.6);
        z-index: 997;
        backdrop-filter: blur(4px);
    }

    .mobile-overlay.visible {
        display: block;
    }
}

/* Mobile Portrait (max-width: 768px) */
@media (max-width: 768px) {
    /* Header Optimization */
    header {
        padding: 0.5rem 1rem;
        flex-wrap: wrap;
        gap: 0.5rem;
    }

    header h1 {
        font-size: 1.1rem;
        flex: 1;
    }

    .header-user-info {
        gap: 8px;
        font-size: 12px;
    }

    .user-avatar {
        width: 32px;
        height: 32px;
    }

    .user-name {
        padding: 4px 10px;
        font-size: 0.9rem;
    }

    .user-credits {
        padding: 4px 8px;
        font-size: 0.85rem;
    }

    .btn-logout {
        padding: 4px 10px;
        font-size: 11px;
    }

    /* Main Content */
    main {
        flex-direction: column;
    }

    /* Character Selection Mobile Optimizations */
    #char-selection {
        width: 260px;
    }

    .char-selection-header {
        padding: 0.75rem 0.75rem 0.5rem;
    }

    .char-card {
        padding: 0.6rem;
    }

    .char-card-avatar {
        width: 45px;
        height: 45px;
    }

    .char-card.selected .char-card-avatar {
        max-height: 300px;
        min-height: 200px;
    }

    .char-card-name {
        font-size: 0.85rem;
    }

    .char-card-desc {
        font-size: 0.7rem;
    }

    /* Chat Interface Mobile Optimizations */
    #chat-interface {
        padding: 1rem;
    }

    .chat-header {
        flex-direction: column;
        align-items: flex-start;
        gap: 0.5rem;
        padding-bottom: 0.75rem;
    }

    .char-name {
        font-size: 1.1rem;
    }

    .session-info {
        font-size: 0.8rem;
    }

    #reset-chat-btn {
        align-self: flex-end;
        position: absolute;
        top: 0;
        right: 0;
    }

    /* Messages Mobile Optimization */
    .chat-messages {
        padding: 0.75rem;
        margin-bottom: 0.75rem;
        gap: 0.75rem;
        flex: 1 1 auto;
        overflow-y: auto;
        -webkit-overflow-scrolling: touch; /* Smooth scrolling on iOS */
        max-height: calc(100vh - 300px); /* Prevent overflow */
    }

    .message {
        max-width: 85%;
    }

    .message-header {
        font-size: 0.7rem;
        margin-bottom: 0.2rem;
    }

    .message-timestamp {
        font-size: 0.65rem;
    }

    .message-content {
        padding: 0.6rem 0.85rem;
        font-size: 0.95rem;
        border-radius: 14px;
    }

    /* Character Info Tooltip Mobile */
    .char-info-tooltip {
        position: fixed;
        top: auto;
        bottom: 10px;
        left: 10px;
        right: 10px;
        max-width: none;
        max-height: 50vh;
        min-width: auto;
    }

    /* Chat Input Mobile Optimization */
    .chat-input-container {
        gap: 0.4rem;
        flex-wrap: nowrap;
        position: relative;
        z-index: 10; /* Ensure input is above other elements */
        background: rgba(26, 26, 26, 0.95);
        padding: 0.5rem;
        margin: -0.5rem;
        border-radius: 8px;
    }

    #message-input {
        font-size: 16px; /* Prevents zoom on iOS */
        padding: 0.6rem;
        min-height: 50px;
        max-height: 120px;
        touch-action: manipulation; /* Improve touch responsiveness */
    }

    #send-btn {
        min-width: 50px;
        min-height: 50px;
        font-size: 1.5rem;
    }

    /* Footer Mobile */
    footer {
        padding: 0.5rem 1rem;
        flex-direction: column;
        gap: 0.25rem;
        font-size: 0.75rem;
        text-align: center;
    }

    /* Buttons - Larger Touch Targets */
    .btn-icon {
        min-width: 44px;
        min-height: 44px;
    }

    #import-char-btn {
        min-width: 40px;
        min-height: 40px;
    }
}

/* Small Mobile (max-width: 480px) */
@media (max-width: 480px) {
    /* Ultra-compact header */
    header h1 {
        font-size: 1rem;
    }

    .user-name {
        display: none; /* Hide username on very small screens */
    }

    /* Character selection even more compact */
    #char-selection {
        width: 240px;
    }

    /* Messages take more width */
    .message {
        max-width: 92%;
    }

    /* Compact footer */
    footer {
        font-size: 0.7rem;
        padding: 0.4rem 0.75rem;
    }

    #model-info {
        display: none; /* Hide model info on very small screens */
    }

    /* Mobile toggle button positioning */
    .mobile-char-toggle {
        bottom: 15px;
        left: 15px;
        width: 52px;
        height: 52px;
        font-size: 22px;
    }
}

/* Landscape Mobile Specific (max-height: 600px in landscape) */
@media (max-height: 600px) and (orientation: landscape) {
    header {
        padding: 0.4rem 1rem;
    }

    header h1 {
        font-size: 1rem;
    }

    .user-avatar {
        width: 28px;
        height: 28px;
    }

    #chat-interface {
        padding: 0.75rem;
    }

    .chat-messages {
        margin-bottom: 0.5rem;
        max-height: calc(100vh - 280px); /* More space for smaller screens */
        overflow-y: auto;
        -webkit-overflow-scrolling: touch;
    }

    #message-input {
        min-height: 40px;
        rows: 2;
        font-size: 16px; /* Maintain size to prevent zoom */
    }

    footer {
        padding: 0.3rem 1rem;
        font-size: 0.7rem;
    }

    .char-card.selected .char-card-avatar {
        max-height: 200px;
        min-height: 150px;
    }
}

/* Touch-friendly improvements for all mobile */
@media (hover: none) and (pointer: coarse) {
    /* Larger touch targets */
    .char-card {
        min-height: 56px;
    }

    button, .btn-primary, .btn-secondary {
        min-height: 44px;
    }

    /* Better tap feedback */
    .char-card:active {
        background: #333;
        transform: scale(0.98);
    }

    .btn-primary:active:not(:disabled),
    .btn-secondary:active:not(:disabled) {
        transform: scale(0.96);
    }

    /* Prevent text selection on buttons */
    button, .btn-primary, .btn-secondary, .char-card {
        -webkit-user-select: none;
        user-select: none;
        -webkit-tap-highlight-color: transparent;
    }

    /* Optimize scrolling */
    .chat-messages, .char-list {
        -webkit-overflow-scrolling: touch;
    }
}

/* ============================================================================
   User Authentication Display
   ============================================================================ */

.header-user-info {
    display: flex;
    align-items: center;
    gap: 12px;
    font-size: 13px;
}

.user-avatar {
    width: 40px;
    height: 40px;
    border-radius: 50%;
    overflow: hidden;
    background: rgba(74, 158, 255, 0.15);
    border: 2px solid rgba(74, 158, 255, 0.35);
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    transition: all 0.3s ease;
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.2);
}

.user-avatar:hover {
    border-color: rgba(74, 158, 255, 0.7);
    transform: scale(1.1);
    box-shadow: 0 4px 12px rgba(74, 158, 255, 0.3);
}

.user-avatar:active {
    transform: scale(1.05);
}

.user-avatar img {
    width: 100%;
    height: 100%;
    object-fit: cover;
}

.user-avatar span {
    font-size: 18px;
}

.user-name {
    font-weight: 600;
    padding: 6px 14px;
    background: rgba(255, 255, 255, 0.1);
    border-radius: 20px;
    color: #f8e8d8;
}

.user-credits {
    color: #90ee90;
    font-weight: 600;
    padding: 6px 12px;
    background: rgba(255, 255, 255, 0.05);
    border-radius: 12px;
}

.btn-profile {
    padding: 6px 14px;
    background: rgba(74, 158, 255, 0.2);
    color: #a5d5ff;
    border: 1px solid rgba(74, 158, 255, 0.3);
    border-radius: 6px;
    cursor: pointer;
    transition: all 0.2s ease;
    font-size: 12px;
    font-weight: 500;
}

.btn-profile:hover {
    background: rgba(74, 158, 255, 0.3);
    border-color: rgba(74, 158, 255, 0.5);
}

.btn-logout {
    padding: 6px 14px;
    background: rgba(255, 82, 82, 0.2);
    color: #ffa5a5;
    border: 1px solid rgba(255, 82, 82, 0.3);
    border-radius: 6px;
    cursor: pointer;
    transition: all 0.2s ease;
    font-size: 12px;
    font-weight: 500;
}

.btn-logout:hover {
    background: rgba(255, 82, 82, 0.3);
    border-color: rgba(255, 82, 82, 0.5);
}
