* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
}

:root {
    --primary: #667eea;
    --secondary: #764ba2;
    --bg-light: #ffffff;
    --bg-dark: #121212;
    --text-light: #333333;
    --text-dark: #f0f0f0;
    --border-light: #e0e0e0;
    --border-dark: #3a3a3a;
    --user-bubble: #e3f2fd;
    --ai-bubble: #f5f5f5;
    --shadow: 0 4px 12px rgba(0,0,0,0.08);
    --shadow-dark: 0 4px 12px rgba(0,0,0,0.25);
    --success: #4caf50;
    --error: #f44336;
    --warning: #ff9800;
    --info: #2196f3;
}

[data-theme="dark"] {
    --bg: var(--bg-dark);
    --text: var(--text-dark);
    --border: var(--border-dark);
    --shadow: var(--shadow-dark);
    --user-bubble: #1e293b;
    --ai-bubble: #252525;
}

[data-theme="light"] {
    --bg: var(--bg-light);
    --text: var(--text-light);
    --border: var(--border-light);
    --shadow: var(--shadow);
    --user-bubble: var(--user-bubble);
    --ai-bubble: var(--ai-bubble);
}

body {
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, 'Helvetica Neue', sans-serif;
    background: linear-gradient(135deg, var(--primary), var(--secondary));
    color: var(--text);
    min-height: 100vh;
    transition: background 0.3s ease;
    line-height: 1.5;
}

.container {
    display: grid;
    grid-template-columns: 1fr 320px;
    gap: 20px;
    padding: 20px;
    max-width: 100%;
    height: 100vh;
}

header {
    grid-column: 1 / -1;
    background: var(--bg);
    padding: 20px;
    border-radius: 16px;
    box-shadow: var(--shadow);
    margin-bottom: 15px;
}

.logo-section {
    display: flex;
    align-items: center;
    gap: 16px;
    margin-bottom: 18px;
    flex-wrap: wrap;
}

.logo-container {
    width: 200px;
    height: 48px;
}

.logo {
    width: 100%;
    height: 100%;
}

header h1 {
    font-size: 1.8rem;
    font-weight: 700;
    background: linear-gradient(to right, var(--primary), var(--secondary));
    -webkit-background-clip: text;
    background-clip: text;
    color: transparent;
    letter-spacing: -0.5px;
    margin: 0;
}

.api-key-section {
    display: flex;
    gap: 12px;
    margin-bottom: 16px;
    flex-wrap: wrap;
}

.api-key-section input {
    flex: 1;
    min-width: 250px;
    padding: 14px 16px;
    border: 2px solid var(--border);
    border-radius: 12px;
    font-size: 1rem;
    background: var(--bg);
    color: var(--text);
    transition: border-color 0.3s;
    font-family: inherit;
}

.api-key-section input:focus {
    outline: none;
    border-color: var(--primary);
    box-shadow: 0 0 0 3px rgba(102, 126, 234, 0.15);
}

.api-key-section input:disabled {
    background: rgba(230, 230, 230, 0.5);
    cursor: not-allowed;
}

.api-key-section button {
    padding: 14px 24px;
    background: var(--primary);
    color: white;
    border: none;
    border-radius: 12px;
    font-weight: 600;
    cursor: pointer;
    transition: all 0.2s;
    white-space: nowrap;
    font-size: 1rem;
    display: flex;
    align-items: center;
    gap: 6px;
}

.api-key-section button:hover:not(:disabled) {
    background: #5a6fd8;
    transform: translateY(-1px);
    box-shadow: 0 4px 8px rgba(102, 126, 234, 0.3);
}

.api-key-section button:disabled {
    background: #ccc;
    cursor: not-allowed;
    transform: none;
    box-shadow: none;
}

.controls {
    display: flex;
    gap: 16px;
    align-items: center;
    flex-wrap: wrap;
}

.model-selector {
    display: flex;
    align-items: center;
    gap: 8px;
}

.model-selector label {
    font-weight: 600;
    font-size: 0.95rem;
    color: var(--text);
}

.model-selector select {
    padding: 10px 14px;
    border: 2px solid var(--border);
    border-radius: 10px;
    background: var(--bg);
    color: var(--text);
    font-size: 1rem;
    cursor: pointer;
    min-width: 160px;
    font-family: inherit;
}

.theme-btn {
    padding: 10px 18px;
    background: var(--bg);
    color: var(--text);
    border: 2px solid var(--border);
    border-radius: 10px;
    cursor: pointer;
    font-weight: 600;
    display: flex;
    align-items: center;
    gap: 6px;
    transition: all 0.3s;
    font-size: 0.95rem;
}

.theme-btn:hover {
    border-color: var(--primary);
    background: rgba(102, 126, 234, 0.1);
}

.chat-container {
    background: var(--bg);
    border-radius: 16px;
    box-shadow: var(--shadow);
    display: flex;
    flex-direction: column;
    height: calc(100vh - 220px);
    overflow: hidden;
}

.chat-history {
    flex: 1;
    overflow-y: auto;
    padding: 24px;
    display: flex;
    flex-direction: column;
    gap: 18px;
    scrollbar-width: thin;
    scrollbar-color: var(--border) transparent;
    position: relative;
}

.welcome-message {
    max-width: 600px;
    margin: 40px auto;
    text-align: center;
    padding: 30px;
    background: rgba(102, 126, 234, 0.08);
    border-radius: 20px;
    border: 1px solid var(--border);
}

.welcome-icon {
    font-size: 3.5rem;
    margin-bottom: 16px;
    animation: bounce 1.5s infinite;
}

@keyframes bounce {
    0%, 100% { transform: translateY(0); }
    50% { transform: translateY(-10px); }
}

.welcome-message h2 {
    font-size: 1.8rem;
    margin-bottom: 20px;
    color: var(--primary);
}

.welcome-message p {
    font-size: 1.1rem;
    margin: 12px 0;
    line-height: 1.6;
}

.welcome-message a {
    color: var(--primary);
    text-decoration: underline;
    font-weight: 600;
    transition: opacity 0.2s;
}

.welcome-message a:hover {
    opacity: 0.8;
}

.security-note {
    margin-top: 20px;
    padding: 12px;
    background: rgba(76, 175, 80, 0.1);
    border-left: 3px solid var(--success);
    border-radius: 0 8px 8px 0;
    font-size: 0.95rem;
    color: var(--success);
    font-weight: 500;
}

.message {
    max-width: 85%;
    padding: 16px 20px;
    border-radius: 20px;
    line-height: 1.6;
    animation: fadeIn 0.3s ease-out;
    word-wrap: break-word;
    position: relative;
    font-size: 1.05rem;
}

@keyframes fadeIn {
    from { opacity: 0; transform: translateY(10px); }
    to { opacity: 1; transform: translateY(0); }
}

.message.user {
    background: var(--user-bubble);
    margin-left: auto;
    border-bottom-right-radius: 6px;
    border-top-right-radius: 20px;
    color: #1a365d;
}

.message.assistant {
    background: var(--ai-bubble);
    margin-right: auto;
    border-bottom-left-radius: 6px;
    border-top-left-radius: 20px;
}

.input-area {
    padding: 16px 24px;
    border-top: 1px solid var(--border);
    display: flex;
    gap: 12px;
    background: var(--bg);
}

.input-area textarea {
    flex: 1;
    padding: 16px;
    border: 2px solid var(--border);
    border-radius: 16px;
    resize: none;
    font-family: inherit;
    font-size: 1.05rem;
    background: var(--bg);
    color: var(--text);
    min-height: 60px;
    max-height: 200px;
    transition: border-color 0.3s;
    line-height: 1.5;
    font-family: inherit;
}

.input-area textarea:focus {
    outline: none;
    border-color: var(--primary);
    box-shadow: 0 0 0 3px rgba(102, 126, 234, 0.15);
}

.input-area textarea:disabled {
    background: #f5f5f5;
    color: #aaa;
    cursor: not-allowed;
    border-style: dashed;
}

.input-area button {
    width: 56px;
    background: var(--primary);
    color: white;
    border: none;
    border-radius: 16px;
    font-size: 1.4rem;
    font-weight: bold;
    cursor: pointer;
    transition: all 0.2s;
    display: flex;
    align-items: center;
    justify-content: center;
    flex-shrink: 0;
    font-family: inherit;
}

.input-area button:hover:not(:disabled) {
    background: #5a6fd8;
    transform: scale(1.05);
}

.input-area button:disabled {
    background: #ccc;
    cursor: not-allowed;
    transform: none;
    opacity: 0.7;
}

.input-area button:active:not(:disabled) {
    transform: scale(0.95);
}

.history-panel {
    background: var(--bg);
    border-radius: 16px;
    box-shadow: var(--shadow);
    padding: 20px;
    display: flex;
    flex-direction: column;
    height: calc(100vh - 220px);
}

.history-panel h3 {
    font-size: 1.4rem;
    margin-bottom: 16px;
    color: var(--primary);
    display: flex;
    align-items: center;
    gap: 8px;
}

.history-list {
    flex: 1;
    overflow-y: auto;
    display: flex;
    flex-direction: column;
    gap: 12px;
    padding-right: 8px;
    scrollbar-width: thin;
    scrollbar-color: var(--border) transparent;
}

.empty-history {
    text-align: center;
    color: #aaa;
    padding: 30px 10px;
    font-size: 1.1rem;
}

.history-item {
    padding: 14px 16px;
    border: 1px solid var(--border);
    border-radius: 12px;
    cursor: pointer;
    transition: all 0.2s;
    background: var(--bg);
    color: var(--text);
    text-align: left;
}

.history-item:hover {
    border-color: var(--primary);
    transform: translateX(4px);
    background: rgba(102, 126, 234, 0.08);
}

.history-item.active {
    background: rgba(102, 126, 234, 0.15);
    border-color: var(--primary);
    font-weight: 600;
    box-shadow: 0 2px 6px rgba(102, 126, 234, 0.2);
}

.history-item .title {
    font-weight: 600;
    margin-bottom: 4px;
    display: block;
    font-size: 0.95rem;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.history-item .preview {
    font-size: 0.85rem;
    color: #aaa;
    margin-top: 2px;
    display: block;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.clear-btn {
    padding: 12px;
    background: var(--error);
    color: white;
    border: none;
    border-radius: 10px;
    font-weight: 600;
    cursor: pointer;
    margin-top: 12px;
    transition: background 0.3s;
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 8px;
    font-size: 1rem;
}

.clear-btn:hover:not(:disabled) {
    background: #d32f2f;
}

.clear-btn:disabled {
    background: #ccc;
    cursor: not-allowed;
    opacity: 0.7;
}

.loading {
    display: inline-block;
    width: 24px;
    height: 24px;
    border: 3px solid var(--primary);
    border-radius: 50%;
    border-top-color: transparent;
    animation: spin 1s linear infinite;
    margin: 8px auto;
}

@keyframes spin {
    to { transform: rotate(360deg); }
}

.error-banner {
    background: rgba(244, 67, 54, 0.15);
    color: #c62828;
    padding: 14px 20px;
    border-radius: 12px;
    margin: 12px 0;
    border-left: 4px solid var(--error);
    font-weight: 500;
    animation: shake 0.5s;
}

.input-error {
    color: #ff5252;
    background: rgba(255, 82, 82, 0.1);
    border-left: 3px solid #ff5252;
    padding: 10px 15px;
    border-radius: 0 8px 8px 0;
    margin-top: 8px;
    font-size: 0.95rem;
    animation: shake 0.5s;
}

.status-banner {
    position: fixed;
    top: 16px;
    left: 50%;
    transform: translateX(-50%);
    padding: 14px 28px;
    border-radius: 14px;
    color: white;
    font-weight: 600;
    box-shadow: 0 6px 20px rgba(0,0,0,0.25);
    z-index: 100000;
    max-width: 90%;
    text-align: center;
    font-family: inherit;
}

.status-banner.success {
    background: linear-gradient(90deg, #43e97b, #38f9d7);
}

.status-banner.error {
    background: linear-gradient(90deg, #ff416c, #ff4b2b);
}

.status-banner.info {
    background: linear-gradient(90deg, #4facfe, #00f2fe);
}

@keyframes slideDown {
    from { opacity: 0; transform: translate(-50%, -30px); }
    to { opacity: 1; transform: translate(-50%, 0); }
}

@keyframes fadeOut {
    to { opacity: 0; transform: translate(-50%, -20px); }
}

@keyframes pulse {
    0% { opacity: 1; }
    50% { opacity: 0.7; }
    100% { opacity: 1; }
}

@keyframes shake {
    0%, 100% { transform: translateX(0); }
    25% { transform: translateX(-6px); }
    75% { transform: translateX(6px); }
}

::-webkit-scrollbar {
    width: 8px;
    height: 8px;
}

::-webkit-scrollbar-track {
    background: transparent;
    border-radius: 4px;
}

::-webkit-scrollbar-thumb {
    background: var(--border);
    border-radius: 4px;
    border: 2px solid transparent;
    background-clip: content-box;
}

::-webkit-scrollbar-thumb:hover {
    background: #888;
}

[data-theme="dark"] ::-webkit-scrollbar-thumb {
    background: #555;
}

[data-theme="dark"] ::-webkit-scrollbar-thumb:hover {
    background: #777;
}

@media (max-width: 1024px) {
    .container {
        grid-template-columns: 1fr;
        grid-template-rows: auto 1fr auto;
        padding: 15px;
        gap: 15px;
    }
    
    .chat-container {
        height: 45vh;
        order: 2;
    }
    
    .history-panel {
        height: auto;
        max-height: 45vh;
        order: 3;
    }
    
    header {
        padding: 16px;
        margin-bottom: 10px;
    }
    
    .logo-container {
        width: 160px;
        height: 40px;
    }
    
    header h1 {
        font-size: 1.5rem;
    }
}

@media (max-width: 768px) {
    .container {
        padding: 10px;
        gap: 10px;
    }
    
    header {
        padding: 14px;
        margin-bottom: 8px;
    }
    
    .logo-container {
        width: 140px;
        height: 36px;
    }
    
    header h1 {
        font-size: 1.35rem;
    }
    
    .api-key-section {
        flex-direction: column;
        gap: 10px;
    }
    
    .api-key-section input {
        min-width: auto;
        width: 100%;
    }
    
    .controls {
        flex-direction: column;
        align-items: stretch;
        gap: 10px;
    }
    
    .model-selector {
        flex-direction: column;
        align-items: flex-start;
    }
    
    .chat-container, .history-panel {
        height: auto;
        max-height: 40vh;
        border-radius: 14px;
    }
    
    .chat-history, .input-area {
        padding: 16px;
    }
    
    .input-area textarea {
        padding: 14px;
        min-height: 50px;
        font-size: 1rem;
    }
    
    .input-area button {
        width: 50px;
        font-size: 1.3rem;
    }
    
    .history-panel h3 {
        font-size: 1.25rem;
        margin-bottom: 12px;
    }
    
    .history-item {
        padding: 12px 14px;
    }
    
    .clear-btn {
        padding: 10px;
        font-size: 0.95rem;
    }
}

@media (max-width: 480px) {
    .logo-section {
        gap: 12px;
        flex-direction: column;
        align-items: flex-start;
    }
    
    .logo-container {
        width: 120px;
        height: 32px;
    }
    
    header h1 {
        font-size: 1.2rem;
    }
    
    .api-key-section button {
        padding: 12px 18px;
        font-size: 0.9rem;
    }
    
    .theme-btn {
        padding: 8px 14px;
        font-size: 0.9rem;
    }
    
    .message {
        max-width: 92%;
        padding: 14px 16px;
        font-size: 0.95rem;
    }
    
    .input-area {
        padding: 12px;
        gap: 8px;
    }
    
    .input-area textarea {
        padding: 12px;
        min-height: 48px;
    }
    
    .input-area button {
        width: 48px;
        font-size: 1.25rem;
    }
    
    .welcome-message {
        margin: 20px auto;
        padding: 20px;
    }
    
    .welcome-icon {
        font-size: 2.8rem;
    }
    
    .welcome-message h2 {
        font-size: 1.5rem;
    }
    
    .welcome-message p {
        font-size: 1rem;
    }
}