/* =========================================
   ChatGPT-Style Chat Interface
   ========================================= */

/* === Chat Layout === */
.chat-layout {
    display: flex;
    height: calc(100vh - 44px);
    padding-top: 44px;
    background: var(--canvas);
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    z-index: 50;
}

/* === Sidebar === */
.chat-sidebar {
    width: 280px;
    flex-shrink: 0;
    background: var(--canvas-parchment);
    border-right: 1px solid var(--hairline);
    display: flex;
    flex-direction: column;
    transition: transform 0.3s ease, width 0.3s ease;
    z-index: 60;
}

.chat-sidebar-header {
    padding: 12px;
    border-bottom: 1px solid var(--hairline);
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 8px;
}

.chat-sidebar-header h3 {
    font-size: 15px;
    font-weight: 600;
    color: var(--ink);
    margin: 0;
}

.chat-sidebar-search {
    padding: 8px 12px;
}

.chat-sidebar-search input {
    width: 100%;
    height: 36px;
    border: 1px solid var(--hairline);
    border-radius: var(--radius-sm);
    padding: 0 12px;
    font-size: 13px;
    background: var(--canvas);
    color: var(--ink);
    outline: none;
    transition: border-color 0.2s;
}

.chat-sidebar-search input:focus {
    border-color: var(--primary);
}

.chat-sidebar-list {
    flex: 1;
    overflow-y: auto;
    padding: 4px 8px;
}

/* === Conversation Item === */
.conv-item {
    padding: 10px 12px;
    cursor: pointer;
    border-radius: var(--radius-sm);
    margin-bottom: 2px;
    transition: background 0.15s;
    position: relative;
    display: flex;
    align-items: center;
    gap: 10px;
}

.conv-item:hover {
    background: rgba(0,0,0,0.04);
}

@media (prefers-color-scheme: dark) {
    .conv-item:hover {
        background: rgba(255,255,255,0.06);
    }
}

.conv-item.active {
    background: rgba(0, 102, 204, 0.1);
}

.conv-item.pinned {
    border-left: 3px solid var(--primary);
}

.conv-item .conv-icon {
    width: 32px;
    height: 32px;
    border-radius: var(--radius-sm);
    background: var(--primary);
    color: white;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 14px;
    flex-shrink: 0;
}

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

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

.conv-item .conv-meta {
    font-size: 11px;
    color: var(--ink-muted-48);
    margin-top: 2px;
    display: flex;
    align-items: center;
    gap: 6px;
}

.conv-item .conv-actions {
    display: none;
    gap: 2px;
    flex-shrink: 0;
}

.conv-item:hover .conv-actions {
    display: flex;
}

.conv-item .conv-actions button {
    width: 26px;
    height: 26px;
    border: none;
    background: transparent;
    border-radius: 4px;
    cursor: pointer;
    font-size: 14px;
    display: flex;
    align-items: center;
    justify-content: center;
    color: var(--ink-muted-48);
    transition: background 0.15s, color 0.15s;
}

.conv-item .conv-actions button:hover {
    background: rgba(0,0,0,0.08);
    color: var(--ink);
}

@media (prefers-color-scheme: dark) {
    .conv-item .conv-actions button:hover {
        background: rgba(255,255,255,0.1);
    }
}

.conv-item .conv-actions button.delete:hover {
    color: #ff3b30;
}

/* === Sidebar Bottom === */
.chat-sidebar-footer {
    padding: 12px;
    border-top: 1px solid var(--hairline);
    display: flex;
    flex-direction: column;
    gap: 6px;
}

.chat-sidebar-footer .user-info {
    display: flex;
    align-items: center;
    gap: 8px;
    padding: 8px;
    border-radius: var(--radius-sm);
    cursor: pointer;
    transition: background 0.15s;
}

.chat-sidebar-footer .user-info:hover {
    background: rgba(0,0,0,0.04);
}

.chat-sidebar-footer .user-info .user-avatar {
    width: 28px;
    height: 28px;
    border-radius: 50%;
    object-fit: cover;
    background: var(--canvas-parchment);
}

.chat-sidebar-footer .user-info .user-name {
    font-size: 13px;
    font-weight: 500;
    color: var(--ink);
}

/* === Main Chat Area === */
.chat-main {
    flex: 1;
    display: flex;
    flex-direction: column;
    min-width: 0;
    background: var(--canvas);
    position: relative;
}

.chat-main-header {
    padding: 12px 20px;
    border-bottom: 1px solid var(--hairline);
    display: flex;
    align-items: center;
    justify-content: space-between;
    background: var(--canvas);
    min-height: 56px;
}

.chat-main-header .chat-model-info {
    display: flex;
    align-items: center;
    gap: 8px;
    font-size: 14px;
    color: var(--ink-muted-48);
}

.chat-main-header .chat-model-info strong {
    color: var(--ink);
}

.chat-main-header .chat-header-actions {
    display: flex;
    gap: 6px;
}

/* === Messages Area === */
.chat-messages {
    flex: 1;
    overflow-y: auto;
    padding: 0;
    scroll-behavior: smooth;
}

.chat-messages-inner {
    max-width: 768px;
    margin: 0 auto;
    padding: 24px 24px 0;
}

/* === Message Groups === */
.message-group {
    margin-bottom: 24px;
}

.message-row {
    display: flex;
    gap: 16px;
    padding: 8px 0;
    animation: msgFadeIn 0.3s ease;
}

@keyframes msgFadeIn {
    from { opacity: 0; transform: translateY(8px); }
    to { opacity: 1; transform: translateY(0); }
}

.message-row.user {
    flex-direction: row-reverse;
}

.message-avatar {
    width: 32px;
    height: 32px;
    border-radius: 50%;
    flex-shrink: 0;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 14px;
    margin-top: 4px;
}

.message-avatar.user-avatar {
    background: var(--primary);
    color: white;
}

.message-avatar.assistant-avatar {
    background: rgba(0, 0, 0, 0.05);
    color: var(--ink);
    border: 1px solid var(--hairline);
}

@media (prefers-color-scheme: dark) {
    .message-avatar.assistant-avatar {
        background: rgba(255, 255, 255, 0.08);
    }
}

.message-content {
    flex: 1;
    min-width: 0;
    max-width: 85%;
}

.message-content.user-content {
    display: flex;
    flex-direction: column;
    align-items: flex-end;
}

.message-bubble {
    padding: 10px 16px;
    border-radius: 18px;
    line-height: 1.6;
    font-size: 15px;
    word-wrap: break-word;
    overflow-wrap: break-word;
}

.message-bubble.user-bubble {
    background: var(--primary);
    color: white;
    border-bottom-right-radius: 4px;
}

.message-bubble.assistant-bubble {
    background: var(--canvas-parchment);
    color: var(--ink);
    border-bottom-left-radius: 4px;
}

.message-bubble.assistant-bubble p {
    margin: 0 0 8px;
}

.message-bubble.assistant-bubble p:last-child {
    margin-bottom: 0;
}

.message-bubble.assistant-bubble ul,
.message-bubble.assistant-bubble ol {
    padding-left: 20px;
    margin: 8px 0;
}

.message-bubble.assistant-bubble li {
    margin-bottom: 4px;
}

.message-bubble.assistant-bubble h1,
.message-bubble.assistant-bubble h2,
.message-bubble.assistant-bubble h3,
.message-bubble.assistant-bubble h4 {
    margin: 12px 0 6px;
    font-weight: 600;
}

.message-bubble.assistant-bubble h1 { font-size: 20px; }
.message-bubble.assistant-bubble h2 { font-size: 18px; }
.message-bubble.assistant-bubble h3 { font-size: 16px; }

.message-bubble.assistant-bubble blockquote {
    border-left: 3px solid var(--hairline);
    padding-left: 12px;
    margin: 8px 0;
    color: var(--ink-muted-48);
}

/* === Code Blocks === */
.message-bubble.assistant-bubble pre {
    background: #1e1e1e;
    border-radius: 10px;
    padding: 14px 16px;
    overflow-x: auto;
    margin: 8px 0;
    position: relative;
}

.message-bubble.assistant-bubble code {
    font-family: 'SF Mono', 'Fira Code', 'Cascadia Code', 'JetBrains Mono', monospace;
    font-size: 13px;
    line-height: 1.5;
}

.message-bubble.assistant-bubble :not(pre) > code {
    background: rgba(0, 0, 0, 0.06);
    padding: 2px 6px;
    border-radius: 4px;
    font-size: 13px;
    color: #e83e8c;
}

@media (prefers-color-scheme: dark) {
    .message-bubble.assistant-bubble :not(pre) > code {
        background: rgba(255, 255, 255, 0.08);
        color: #ff7b9c;
    }
}

.message-bubble.assistant-bubble pre code {
    color: #d4d4d4;
    background: none;
    padding: 0;
}

/* Copy button for code blocks */
.code-block-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 6px 16px;
    background: #2d2d2d;
    border-radius: 10px 10px 0 0;
    margin: 8px 0 -8px;
    font-size: 12px;
    color: #999;
}

.code-block-header .code-lang {
    text-transform: uppercase;
    font-size: 11px;
    letter-spacing: 0.5px;
}

.code-block-header .code-copy-btn {
    background: transparent;
    border: 1px solid rgba(255,255,255,0.1);
    color: #ccc;
    padding: 2px 10px;
    border-radius: 4px;
    font-size: 11px;
    cursor: pointer;
    transition: background 0.2s;
}

.code-block-header .code-copy-btn:hover {
    background: rgba(255,255,255,0.1);
}

/* === Tables in Messages === */
.message-bubble.assistant-bubble table {
    border-collapse: collapse;
    width: 100%;
    margin: 8px 0;
    font-size: 14px;
}

.message-bubble.assistant-bubble th,
.message-bubble.assistant-bubble td {
    border: 1px solid var(--hairline);
    padding: 8px 12px;
    text-align: left;
}

.message-bubble.assistant-bubble th {
    background: var(--canvas-parchment);
    font-weight: 600;
}

/* === Message Actions === */
.message-actions {
    display: flex;
    gap: 4px;
    padding: 4px 0 0;
    opacity: 0;
    transition: opacity 0.2s;
}

.message-row:hover .message-actions {
    opacity: 1;
}

.message-actions button {
    width: 28px;
    height: 28px;
    border: none;
    background: transparent;
    border-radius: 6px;
    cursor: pointer;
    font-size: 13px;
    display: flex;
    align-items: center;
    justify-content: center;
    color: var(--ink-muted-48);
    transition: background 0.15s, color 0.15s;
}

.message-actions button:hover {
    background: var(--canvas-parchment);
    color: var(--ink);
}

.message-actions button.copied {
    color: #34c759;
}

/* === Streaming Indicator === */
.streaming-cursor {
    display: inline-block;
    width: 8px;
    height: 16px;
    background: var(--primary);
    animation: blink 1s step-end infinite;
    margin-left: 2px;
    vertical-align: middle;
    border-radius: 1px;
}

@keyframes blink {
    50% { opacity: 0; }
}

/* === Typing Indicator === */
.typing-indicator {
    display: flex;
    align-items: center;
    gap: 4px;
    padding: 12px 16px;
    background: var(--canvas-parchment);
    border-radius: 18px;
    border-bottom-left-radius: 4px;
}

.typing-dot {
    width: 8px;
    height: 8px;
    border-radius: 50%;
    background: var(--ink-muted-48);
    animation: typingBounce 1.4s infinite ease-in-out;
}

.typing-dot:nth-child(1) { animation-delay: 0s; }
.typing-dot:nth-child(2) { animation-delay: 0.2s; }
.typing-dot:nth-child(3) { animation-delay: 0.4s; }

@keyframes typingBounce {
    0%, 60%, 100% { transform: translateY(0); opacity: 0.3; }
    30% { transform: translateY(-6px); opacity: 1; }
}

/* === Input Area === */
.chat-input-area {
    padding: 16px 24px;
    border-top: 1px solid var(--hairline);
    background: var(--canvas);
    position: relative;
}

.chat-input-container {
    max-width: 768px;
    margin: 0 auto;
    display: flex;
    gap: 8px;
    align-items: flex-end;
    background: var(--canvas-parchment);
    border: 1px solid var(--hairline);
    border-radius: 16px;
    padding: 8px 8px 8px 16px;
    transition: border-color 0.2s, box-shadow 0.2s;
}

.chat-input-container:focus-within {
    border-color: var(--primary);
    box-shadow: 0 0 0 3px rgba(0, 102, 204, 0.1);
}

.chat-input-container textarea {
    flex: 1;
    border: none;
    background: transparent;
    font-family: var(--font-text);
    font-size: 15px;
    line-height: 1.5;
    color: var(--ink);
    resize: none;
    outline: none;
    max-height: 200px;
    padding: 6px 0;
    min-height: 24px;
}

.chat-input-container textarea::placeholder {
    color: var(--ink-muted-48);
}

.chat-input-container .chat-send-btn {
    width: 36px;
    height: 36px;
    border-radius: 10px;
    border: none;
    background: var(--primary);
    color: white;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 16px;
    flex-shrink: 0;
    transition: opacity 0.2s, transform 0.1s;
}

.chat-input-container .chat-send-btn:active {
    transform: scale(0.92);
}

.chat-input-container .chat-send-btn:disabled {
    opacity: 0.4;
    cursor: not-allowed;
}

.chat-input-container .chat-send-btn:hover:not(:disabled) {
    opacity: 0.9;
}

/* === Model Selector Dropdown === */
.chat-model-selector {
    position: relative;
    padding: 0 24px;
}

.chat-model-selector select {
    height: 32px;
    border: 1px solid var(--hairline);
    border-radius: var(--radius-sm);
    padding: 0 28px 0 10px;
    font-size: 13px;
    background: var(--canvas-parchment);
    color: var(--ink-muted-48);
    cursor: pointer;
    outline: none;
    appearance: none;
    background-image: url("data:image/svg+xml,%3Csvg width='10' height='6' viewBox='0 0 10 6' fill='none' xmlns='http://www.w3.org/2000/svg'%3E%3Cpath d='M1 1L5 5L9 1' stroke='%237a7a7a' stroke-width='1.5' stroke-linecap='round' stroke-linejoin='round'/%3E%3C/svg%3E");
    background-repeat: no-repeat;
    background-position: right 10px center;
    transition: border-color 0.2s;
}

.chat-model-selector select:focus {
    border-color: var(--primary);
}

/* === Empty State === */
.chat-empty-state {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    height: 100%;
    text-align: center;
    padding: 40px 24px;
}

.chat-empty-state .empty-icon {
    width: 72px;
    height: 72px;
    border-radius: 50%;
    background: linear-gradient(135deg, var(--primary), #4a9eff);
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 32px;
    margin-bottom: 20px;
    box-shadow: 0 8px 32px rgba(0, 102, 204, 0.2);
}

.chat-empty-state h2 {
    font-size: 24px;
    font-weight: 600;
    margin-bottom: 8px;
    color: var(--ink);
}

.chat-empty-state p {
    font-size: 14px;
    color: var(--ink-muted-48);
    max-width: 360px;
    margin-bottom: 24px;
}

.chat-empty-state .empty-suggestions {
    display: flex;
    flex-wrap: wrap;
    gap: 8px;
    justify-content: center;
    max-width: 400px;
}

.chat-empty-state .suggestion-chip {
    padding: 8px 16px;
    border: 1px solid var(--hairline);
    border-radius: var(--radius-pill);
    font-size: 13px;
    color: var(--ink-muted-48);
    cursor: pointer;
    transition: border-color 0.2s, color 0.2s;
}

.chat-empty-state .suggestion-chip:hover {
    border-color: var(--primary);
    color: var(--primary);
}

/* === Scrollbar === */
.chat-sidebar-list::-webkit-scrollbar,
.chat-messages::-webkit-scrollbar {
    width: 4px;
}

.chat-sidebar-list::-webkit-scrollbar-thumb,
.chat-messages::-webkit-scrollbar-thumb {
    background: var(--hairline);
    border-radius: 2px;
}

/* === Mobile Responsive === */
@media (max-width: 768px) {
    .chat-layout {
        height: calc(100vh - 44px);
    }

    .chat-sidebar {
        position: fixed;
        top: 44px;
        left: 0;
        bottom: 0;
        width: 280px;
        transform: translateX(-100%);
        box-shadow: 0 0 40px rgba(0,0,0,0.2);
    }

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

    .chat-sidebar-overlay {
        display: none;
        position: fixed;
        top: 44px;
        left: 0;
        right: 0;
        bottom: 0;
        background: rgba(0,0,0,0.3);
        z-index: 55;
    }

    .chat-sidebar-overlay.open {
        display: block;
    }

    .chat-messages-inner {
        padding: 16px 12px;
    }

    .chat-input-area {
        padding: 12px;
    }

    .message-bubble {
        font-size: 15px;
        padding: 10px 14px;
    }

    .message-content {
        max-width: 90%;
    }

    .chat-main-header {
        padding: 10px 14px;
    }

    .chat-model-selector {
        padding: 0 12px;
    }
}

/* === Stop Generation Button === */
.stop-generation-btn {
    position: absolute;
    bottom: 100%;
    left: 50%;
    transform: translateX(-50%);
    margin-bottom: 8px;
    background: var(--canvas);
    border: 1px solid var(--hairline);
    border-radius: var(--radius-pill);
    padding: 6px 16px;
    font-size: 13px;
    color: var(--ink);
    cursor: pointer;
    display: none;
    align-items: center;
    gap: 6px;
    box-shadow: 0 2px 8px rgba(0,0,0,0.08);
    transition: background 0.2s;
    z-index: 10;
}

.stop-generation-btn:hover {
    background: var(--canvas-parchment);
}

.stop-generation-btn.visible {
    display: flex;
}

/* === Regenerate / Error Retry === */
.retry-btn {
    display: inline-flex;
    align-items: center;
    gap: 4px;
    padding: 4px 12px;
    border: 1px solid var(--hairline);
    border-radius: var(--radius-pill);
    font-size: 12px;
    color: var(--ink-muted-48);
    cursor: pointer;
    transition: all 0.15s;
    margin-top: 8px;
}

.retry-btn:hover {
    border-color: var(--primary);
    color: var(--primary);
}

/* === Settings Pane === */
.chat-settings-pane {
    display: none;
    position: absolute;
    top: 56px;
    right: 12px;
    width: 300px;
    background: var(--canvas);
    border: 1px solid var(--hairline);
    border-radius: var(--radius-lg);
    box-shadow: 0 8px 32px rgba(0,0,0,0.12);
    padding: 16px;
    z-index: 70;
}

.chat-settings-pane.open {
    display: block;
}

.chat-settings-pane h4 {
    font-size: 14px;
    font-weight: 600;
    margin-bottom: 12px;
    color: var(--ink);
}

.chat-settings-pane .setting-item {
    margin-bottom: 12px;
}

.chat-settings-pane .setting-item label {
    display: block;
    font-size: 12px;
    font-weight: 500;
    color: var(--ink-muted-48);
    margin-bottom: 4px;
}

.chat-settings-pane .setting-item textarea {
    width: 100%;
    min-height: 60px;
    border: 1px solid var(--hairline);
    border-radius: var(--radius-sm);
    padding: 8px 12px;
    font-size: 13px;
    font-family: var(--font-text);
    color: var(--ink);
    background: var(--canvas);
    resize: vertical;
    outline: none;
}

.chat-settings-pane .setting-item textarea:focus {
    border-color: var(--primary);
}

/* === Sidebar Toggle Button (Mobile) === */
.chat-menu-btn {
    display: none;
    background: transparent;
    border: none;
    color: var(--ink);
    font-size: 20px;
    cursor: pointer;
    padding: 4px;
    border-radius: 6px;
}

@media (max-width: 768px) {
    .chat-menu-btn {
        display: flex;
    }
}

.chat-menu-btn:hover {
    background: var(--canvas-parchment);
}

/* === New Chat Button (in sidebar) === */
.new-chat-btn {
    display: flex;
    align-items: center;
    gap: 6px;
    padding: 8px 12px;
    border: 1px dashed var(--hairline);
    border-radius: var(--radius-sm);
    font-size: 13px;
    color: var(--ink-muted-48);
    cursor: pointer;
    transition: all 0.2s;
    margin-bottom: 4px;
}

.new-chat-btn:hover {
    border-color: var(--primary);
    color: var(--primary);
    background: rgba(0, 102, 204, 0.04);
}

/* === Scroll-to-bottom button === */
.scroll-bottom-btn {
    position: absolute;
    bottom: 80px;
    right: 24px;
    width: 36px;
    height: 36px;
    border-radius: 50%;
    background: var(--canvas);
    border: 1px solid var(--hairline);
    box-shadow: 0 2px 8px rgba(0,0,0,0.1);
    cursor: pointer;
    display: none;
    align-items: center;
    justify-content: center;
    font-size: 16px;
    color: var(--ink-muted-48);
    transition: all 0.2s;
    z-index: 5;
}

.scroll-bottom-btn.visible {
    display: flex;
}

.scroll-bottom-btn:hover {
    background: var(--canvas-parchment);
    color: var(--ink);
}

/* === Welcome message === */
.welcome-message {
    text-align: center;
    padding: 60px 20px;
}

.welcome-message .welcome-icon {
    font-size: 48px;
    margin-bottom: 16px;
}

.welcome-message h1 {
    font-size: 28px;
    font-weight: 600;
    margin-bottom: 8px;
    color: var(--ink);
}

.welcome-message p {
    font-size: 15px;
    color: var(--ink-muted-48);
    max-width: 400px;
    margin: 0 auto;
}

/* === Model badge in messages === */
.message-bubble .model-badge {
    display: inline-block;
    font-size: 11px;
    color: var(--ink-muted-48);
    margin-top: 6px;
    padding: 2px 8px;
    background: rgba(0,0,0,0.03);
    border-radius: 4px;
}

@media (prefers-color-scheme: dark) {
    .message-bubble .model-badge {
        background: rgba(255,255,255,0.05);
    }
}

/* === Conversation settings modal (inline) === */
.settings-btn {
    background: transparent;
    border: none;
    color: var(--ink-muted-48);
    cursor: pointer;
    font-size: 16px;
    padding: 6px;
    border-radius: 6px;
    transition: background 0.15s;
}

.settings-btn:hover {
    background: var(--canvas-parchment);
}

/* === Error message styling === */
.message-error {
    color: #ff3b30;
    font-size: 14px;
    padding: 8px 12px;
    background: rgba(255, 59, 48, 0.06);
    border-radius: 8px;
    margin: 8px 0;
}
