/* === Reset & Base === */
*, *::before, *::after {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
}

:root {
    --sidebar-width: 260px;
    --bg-page: #fcfaf8;
    --bg-sidebar: #fcfaf8;
    --bg-white: #ffffff;
    --bg-hover: #f3f1eb;
    --bg-active: #ebe8e1;
    --bg-user-msg: #f3f1eb;
    --text-primary: #2d2d2d;
    --text-secondary: #5d5d5d;
    --text-muted: #888888;
    --border: #e6e4df;
    --border-light: #f0eee9;
    --accent: #d97706;
    --danger: #dc3545;
    --radius: 8px;
    --radius-lg: 16px;
    --radius-xl: 20px;
    --shadow-sm: 0 1px 2px rgba(0, 0, 0, 0.04);
    --shadow-md: 0 4px 12px rgba(0, 0, 0, 0.05);
    --shadow-lg: 0 4px 16px rgba(0, 0, 0, 0.1);
    --font-sans: -apple-system, BlinkMacSystemFont, "Segoe UI", "Noto Sans", Helvetica, Arial, sans-serif;
    --font-serif: ui-serif, Georgia, Cambria, "Times New Roman", Times, serif;
}

html, body {
    height: 100%;
    font-family: var(--font-sans);
    font-size: 15px;
    background: var(--bg-page);
    color: var(--text-primary);
    -webkit-font-smoothing: antialiased;
}

/* === Login Page === */
.login-body {
    display: flex;
    align-items: center;
    justify-content: center;
    min-height: 100vh;
    background: var(--bg-page);
}

.login-container {
    background: var(--bg-white);
    padding: 2.5rem;
    border-radius: var(--radius-lg);
    width: 100%;
    max-width: 380px;
    box-shadow: var(--shadow-lg);
}

.login-container h1 {
    text-align: center;
    margin-bottom: 1.5rem;
    font-size: 1.5rem;
    font-weight: 600;
    color: var(--text-primary);
}

.login-error {
    background: #fef2f2;
    color: var(--danger);
    padding: 0.6rem 1rem;
    border-radius: var(--radius);
    margin-bottom: 1rem;
    font-size: 0.9rem;
}

.form-group {
    margin-bottom: 1rem;
}

.form-group label {
    display: block;
    margin-bottom: 0.4rem;
    font-size: 0.9rem;
    font-weight: 500;
    color: var(--text-secondary);
}

.form-group input {
    width: 100%;
    padding: 0.6rem 0.8rem;
    background: var(--bg-white);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    color: var(--text-primary);
    font-size: 0.95rem;
    outline: none;
    transition: border-color 0.15s;
}

.form-group input:focus {
    border-color: var(--accent);
    box-shadow: 0 0 0 2px rgba(199, 90, 0, 0.1);
}

.login-btn {
    width: 100%;
    padding: 0.7rem;
    background: var(--text-primary);
    color: var(--bg-white);
    border: none;
    border-radius: var(--radius);
    font-size: 1rem;
    font-weight: 500;
    cursor: pointer;
    margin-top: 0.5rem;
    transition: opacity 0.15s;
}

.login-btn:hover {
    opacity: 0.85;
}

/* === App Layout === */
.app-container {
    display: flex;
    height: 100vh;
    overflow: hidden;
}

/* === Sidebar === */
.sidebar {
    width: var(--sidebar-width);
    min-width: var(--sidebar-width);
    background: var(--bg-sidebar);
    display: flex;
    flex-direction: column;
    transition: margin-left 0.25s ease;
    overflow: hidden;
}

.sidebar.collapsed {
    margin-left: calc(-1 * var(--sidebar-width));
}

.sidebar-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 0.9rem 1rem;
}

.sidebar-brand {
    font-size: 1.05rem;
    font-weight: 700;
    color: var(--text-primary);
    letter-spacing: -0.01em;
}

.sidebar-collapse-btn {
    background: none;
    border: none;
    color: var(--text-muted);
    cursor: pointer;
    padding: 4px;
    border-radius: var(--radius);
    display: flex;
    align-items: center;
    justify-content: center;
}

.sidebar-collapse-btn:hover {
    background: var(--bg-hover);
    color: var(--text-secondary);
}

/* Sidebar nav items */
.sidebar-nav {
    padding: 0 0.5rem;
    display: flex;
    flex-direction: column;
    gap: 1px;
}

.nav-item {
    display: flex;
    align-items: center;
    gap: 0.6rem;
    padding: 0.45rem 0.7rem;
    border: none;
    background: none;
    color: var(--text-secondary);
    font-size: 0.88rem;
    font-family: inherit;
    cursor: pointer;
    border-radius: var(--radius);
    text-align: left;
    width: 100%;
}

.nav-item:hover {
    background: var(--bg-hover);
    color: var(--text-primary);
}

.nav-item svg {
    flex-shrink: 0;
}

/* Sidebar history */
.sidebar-history {
    flex: 1;
    overflow-y: auto;
    padding: 0.5rem;
    margin-top: 0.3rem;
}

.history-label {
    font-size: 0.72rem;
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: 0.06em;
    color: var(--text-muted);
    padding: 0.5rem 0.6rem 0.3rem;
}

#history-items {
    list-style: none;
}

#history-items li {
    display: flex;
    align-items: center;
    padding: 0.4rem 0.6rem;
    border-radius: var(--radius);
    cursor: pointer;
    font-size: 0.87rem;
    color: var(--text-secondary);
    margin-bottom: 1px;
    transition: background 0.1s;
}

#history-items li:hover {
    background: var(--bg-hover);
}

#history-items li.active {
    background: var(--bg-active);
    color: var(--text-primary);
}

#history-items li .session-title {
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
    flex: 1;
    line-height: 1.4;
}

#history-items li .delete-session-btn {
    background: none;
    border: none;
    color: var(--text-muted);
    cursor: pointer;
    font-size: 1rem;
    padding: 0 0.25rem;
    opacity: 0;
    transition: opacity 0.12s;
    line-height: 1;
}

#history-items li:hover .delete-session-btn {
    opacity: 1;
}

#history-items li .delete-session-btn:hover {
    color: var(--danger);
}

/* Sidebar footer */
.sidebar-footer {
    padding: 0.7rem 1rem;
    border-top: 1px solid var(--border-light);
    display: flex;
    align-items: center;
    justify-content: space-between;
}

.user-info {
    display: flex;
    align-items: center;
    gap: 0.5rem;
}

.user-avatar {
    width: 28px;
    height: 28px;
    background: var(--accent);
    color: #fff;
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 0.8rem;
    font-weight: 600;
}

.user-name {
    font-size: 0.88rem;
    font-weight: 500;
    color: var(--text-primary);
}

.logout-form {
    display: flex;
    align-items: center;
}

.logout-btn {
    color: var(--text-muted);
    display: flex;
    align-items: center;
    padding: 4px;
    border: none;
    background: none;
    cursor: pointer;
    font: inherit;
    border-radius: var(--radius);
    transition: color 0.15s;
}

.logout-btn:hover {
    color: var(--danger);
    background: var(--bg-hover);
}

/* === Sidebar Toggle (when collapsed) === */
.sidebar-toggle {
    position: absolute;
    top: 0.7rem;
    left: 0.7rem;
    z-index: 10;
    background: var(--bg-white);
    border: 1px solid var(--border);
    color: var(--text-muted);
    width: 2rem;
    height: 2rem;
    border-radius: var(--radius);
    cursor: pointer;
    display: none;
    align-items: center;
    justify-content: center;
    box-shadow: var(--shadow-sm);
}

.sidebar-toggle:hover {
    color: var(--text-primary);
    border-color: var(--text-muted);
}

.sidebar.collapsed ~ .chat-area .sidebar-toggle {
    display: flex;
}

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

/* === Welcome Screen === */
.welcome-screen {
    flex: 1;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    padding: 2rem;
    margin-top: -10vh;
}

.welcome-screen.hidden {
    display: none;
}

.welcome-content {
    margin-bottom: 2rem;
}

.greeting-title {
    font-family: var(--font-serif);
    font-size: 2.2rem;
    font-weight: 400;
    color: var(--text-primary);
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 8px;
}

.greeting-icon {
    margin-bottom: 4px;
}

/* Welcome input (centered on welcome screen) */
.welcome-input-wrapper {
    width: 100%;
    max-width: 680px;
}

.welcome-input-box {
    background: var(--bg-white);
    border: 1px solid var(--border);
    border-radius: var(--radius-xl);
    padding: 12px 14px;
    box-shadow: var(--shadow-md);
    transition: box-shadow 0.2s, border-color 0.2s;
    display: flex;
    flex-direction: column;
}

.welcome-input-box:focus-within {
    border-color: #d2d0cb;
    box-shadow: 0 6px 16px rgba(0, 0, 0, 0.08);
}

.welcome-textarea {
    width: 100%;
    border: none;
    outline: none;
    resize: none;
    font-family: inherit;
    font-size: 1rem;
    color: var(--text-primary);
    background: transparent;
    padding: 8px 4px 16px 4px;
    line-height: 1.5;
    min-height: 50px;
    max-height: 200px;
    overflow-y: auto;
}

.welcome-textarea::placeholder {
    color: #a3a3a3;
}

.welcome-input-actions {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding-top: 4px;
}

.actions-right {
    display: flex;
    align-items: center;
    gap: 6px;
}

.welcome-action-btn {
    background: none;
    border: none;
    color: var(--text-muted);
    cursor: pointer;
    padding: 6px;
    border-radius: var(--radius);
    display: flex;
    align-items: center;
    justify-content: center;
    transition: background 0.15s, color 0.15s;
}

.welcome-action-btn:hover {
    background: var(--bg-hover);
    color: var(--text-primary);
}

.welcome-send-btn {
    background: none;
    border: none;
    color: var(--text-muted);
    cursor: pointer;
    padding: 6px;
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: color 0.15s;
}

.welcome-send-btn:not(:disabled):hover {
    background: var(--bg-hover);
    color: var(--text-primary);
}

.welcome-send-btn:disabled {
    cursor: default;
    opacity: 0.4;
}

.stop-btn {
    background: none;
    border: none;
    color: var(--danger);
    cursor: pointer;
    padding: 6px;
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: background 0.15s;
}

.stop-btn:hover {
    background: var(--bg-hover);
}

.welcome-image-preview {
    padding: 0.3rem 0.4rem;
    position: relative;
    display: inline-block;
}

.welcome-image-preview img {
    max-height: 100px;
    max-width: 180px;
    border-radius: var(--radius);
    border: 1px solid var(--border);
}

.welcome-image-remove {
    position: absolute;
    top: 0;
    right: 0;
    width: 20px;
    height: 20px;
    background: var(--danger);
    color: #fff;
    border: none;
    border-radius: 50%;
    cursor: pointer;
    font-size: 0.85rem;
    line-height: 1;
    display: flex;
    align-items: center;
    justify-content: center;
}

/* === Suggestion Chips === */
.suggestion-chips {
    display: flex;
    flex-wrap: wrap;
    justify-content: center;
    gap: 10px;
    margin-top: 24px;
}

.chip {
    display: flex;
    align-items: center;
    gap: 6px;
    background: var(--bg-white);
    border: 1px solid var(--border);
    padding: 8px 14px;
    border-radius: 20px;
    font-size: 0.85rem;
    color: var(--text-secondary);
    cursor: pointer;
    font-family: inherit;
    transition: background 0.2s, border-color 0.2s;
}

.chip svg {
    color: var(--text-muted);
}

.chip:hover {
    background: var(--bg-hover);
    border-color: #d2d0cb;
    color: var(--text-primary);
}

/* === Active Chat === */
.active-chat {
    flex: 1;
    display: flex;
    flex-direction: column;
    min-height: 0;
}

.active-chat.hidden {
    display: none;
}

/* Chat container */
.chat-container {
    flex: 1;
    overflow-y: auto;
    padding: 1.5rem 1rem 1rem;
    display: flex;
    flex-direction: column;
    max-width: 800px;
    width: 100%;
    margin: 0 auto;
}

.message {
    margin-bottom: 1.2rem;
    line-height: 1.6;
    word-wrap: break-word;
    white-space: pre-wrap;
    font-size: 0.95rem;
}

.message-user {
    background: var(--bg-user-msg);
    padding: 0.8rem 1rem;
    border-radius: var(--radius-lg);
    color: var(--text-primary);
    align-self: flex-end;
    max-width: 80%;
}

.message-assistant {
    color: var(--text-primary);
    padding: 0.2rem 0;
    max-width: 100%;
}

.typing-indicator {
    display: flex;
    gap: 4px;
    padding: 0.6rem 0;
}

.typing-indicator .dot {
    width: 8px;
    height: 8px;
    border-radius: 50%;
    background: var(--text-tertiary);
    animation: typing-bounce 1.4s infinite ease-in-out both;
}

.typing-indicator .dot:nth-child(2) {
    animation-delay: 0.16s;
}

.typing-indicator .dot:nth-child(3) {
    animation-delay: 0.32s;
}

@keyframes typing-bounce {
    0%, 80%, 100% { transform: scale(0.6); opacity: 0.4; }
    40% { transform: scale(1); opacity: 1; }
}

.message-error {
    background: #fef2f2;
    color: var(--danger);
    padding: 0.6rem 1rem;
    border-radius: var(--radius);
    font-size: 0.9rem;
    text-align: center;
    align-self: center;
}

.message img {
    max-width: 100%;
    max-height: 300px;
    border-radius: var(--radius);
    margin-top: 0.5rem;
    display: block;
}

/* Bottom input (during active chat) */
.input-container {
    padding: 0 1rem 1.2rem;
    max-width: 800px;
    width: 100%;
    margin: 0 auto;
}

.input-box {
    background: var(--bg-white);
    border: 1px solid var(--border);
    border-radius: var(--radius-xl);
    padding: 0.6rem 0.8rem;
    box-shadow: var(--shadow-sm);
    transition: box-shadow 0.2s, border-color 0.2s;
}

.input-box:focus-within {
    box-shadow: var(--shadow-md);
}

.input-box textarea {
    width: 100%;
    border: none;
    outline: none;
    resize: none;
    font-family: inherit;
    font-size: 0.95rem;
    color: var(--text-primary);
    background: transparent;
    padding: 0.4rem;
    line-height: 1.5;
    max-height: 200px;
    overflow-y: auto;
}

.input-box textarea::placeholder {
    color: var(--text-muted);
}

.input-actions {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding-top: 0.2rem;
}

.action-btn {
    background: none;
    border: none;
    color: var(--text-muted);
    cursor: pointer;
    padding: 6px;
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
}

.action-btn:hover {
    background: var(--bg-hover);
    color: var(--text-secondary);
}

.send-btn {
    background: none;
    border: none;
    color: var(--text-muted);
    cursor: pointer;
    padding: 6px;
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: color 0.15s;
}

.send-btn:not(:disabled):hover {
    background: var(--bg-hover);
    color: var(--text-primary);
}

.send-btn:disabled {
    cursor: default;
    opacity: 0.4;
}

/* Image preview in active chat */
.image-preview-container {
    position: relative;
    display: inline-block;
    padding: 0 0 0.4rem 0.4rem;
}

.image-preview-container.hidden {
    display: none;
}

.image-preview-container img {
    max-height: 100px;
    max-width: 180px;
    border-radius: var(--radius);
    border: 1px solid var(--border);
}

.image-remove-btn {
    position: absolute;
    top: -6px;
    right: -6px;
    width: 20px;
    height: 20px;
    background: var(--danger);
    color: #fff;
    border: none;
    border-radius: 50%;
    cursor: pointer;
    font-size: 0.85rem;
    line-height: 1;
    display: flex;
    align-items: center;
    justify-content: center;
}

/* === Thinking Toggle === */
.input-actions-right {
    display: flex;
    align-items: center;
    gap: 4px;
}

.thinking-toggle {
    display: flex;
    align-items: center;
    gap: 4px;
    padding: 4px 10px;
    background: none;
    border: 1px solid var(--border);
    border-radius: 20px;
    color: var(--text-muted);
    cursor: pointer;
    font-family: inherit;
    font-size: 0.8rem;
    transition: all 0.15s;
}

.thinking-toggle:hover {
    background: var(--bg-hover);
    color: var(--text-secondary);
}

.thinking-toggle.active {
    background: var(--accent);
    border-color: var(--accent);
    color: #fff;
}

.thinking-toggle.active:hover {
    opacity: 0.9;
}

.thinking-label {
    line-height: 1;
}

/* === Thinking Block (in messages) === */
.thinking-block {
    margin-bottom: 0.6rem;
}

.thinking-block summary {
    cursor: pointer;
    font-size: 0.82rem;
    font-weight: 500;
    color: var(--text-muted);
    padding: 0.3rem 0;
    user-select: none;
    list-style: none;
    display: flex;
    align-items: center;
    gap: 0.4rem;
}

.thinking-block summary::-webkit-details-marker {
    display: none;
}

.thinking-block summary::before {
    content: "";
    display: inline-block;
    width: 0;
    height: 0;
    border-left: 5px solid var(--text-muted);
    border-top: 4px solid transparent;
    border-bottom: 4px solid transparent;
    transition: transform 0.15s;
}

.thinking-block[open] summary::before {
    transform: rotate(90deg);
}

.thinking-block summary:hover {
    color: var(--text-secondary);
}

.thinking-content {
    margin-top: 0.4rem;
    padding: 0.6rem 0.8rem;
    background: var(--bg-sidebar);
    border-radius: var(--radius);
    border-left: 3px solid var(--border);
    font-size: 0.88rem;
    color: var(--text-secondary);
    line-height: 1.55;
    white-space: pre-wrap;
    word-wrap: break-word;
    max-height: 400px;
    overflow-y: auto;
}

.hidden {
    display: none !important;
}

/* === Settings Modal === */
.modal {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: rgba(0, 0, 0, 0.3);
    display: flex;
    align-items: center;
    justify-content: center;
    z-index: 100;
}

.modal.hidden {
    display: none;
}

.modal-content {
    background: var(--bg-white);
    border-radius: var(--radius-lg);
    width: 100%;
    max-width: 500px;
    max-height: 80vh;
    overflow-y: auto;
    box-shadow: var(--shadow-lg);
}

.modal-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 1rem 1.2rem;
    border-bottom: 1px solid var(--border-light);
}

.modal-header h2 {
    font-size: 1.05rem;
    font-weight: 600;
}

.modal-close {
    background: none;
    border: none;
    color: var(--text-muted);
    font-size: 1.4rem;
    cursor: pointer;
    padding: 2px 6px;
    border-radius: var(--radius);
    line-height: 1;
}

.modal-close:hover {
    background: var(--bg-hover);
    color: var(--text-primary);
}

.modal-body {
    padding: 1.2rem;
}

.modal-body label {
    display: block;
    font-size: 0.9rem;
    font-weight: 500;
    color: var(--text-primary);
    margin-bottom: 0.3rem;
}

.setting-hint {
    font-size: 0.82rem;
    color: var(--text-muted);
    margin-bottom: 0.6rem;
}

.modal-body textarea {
    width: 100%;
    padding: 0.6rem 0.8rem;
    background: var(--bg-page);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    color: var(--text-primary);
    font-size: 0.9rem;
    font-family: inherit;
    resize: vertical;
    outline: none;
    transition: border-color 0.15s;
}

.modal-body textarea:focus {
    border-color: var(--accent);
}

.settings-save-btn {
    margin-top: 0.8rem;
    padding: 0.5rem 1.2rem;
    background: var(--text-primary);
    color: var(--bg-white);
    border: none;
    border-radius: var(--radius);
    cursor: pointer;
    font-size: 0.9rem;
    font-weight: 500;
    transition: opacity 0.15s;
}

.settings-save-btn:hover {
    opacity: 0.85;
}

/* === Scrollbar === */
::-webkit-scrollbar {
    width: 6px;
}

::-webkit-scrollbar-track {
    background: transparent;
}

::-webkit-scrollbar-thumb {
    background: var(--border);
    border-radius: 3px;
}

::-webkit-scrollbar-thumb:hover {
    background: var(--text-muted);
}

.settings-divider {
    border: none;
    border-top: 1px solid var(--border-light);
    margin: 1rem 0;
}

.settings-section-title {
    font-size: 0.95rem;
    font-weight: 600;
    color: var(--text-primary);
    margin-bottom: 0.6rem;
}

.settings-grid {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 0.6rem;
}

.setting-field label {
    display: block;
    font-size: 0.82rem;
    font-weight: 500;
    color: var(--text-secondary);
    margin-bottom: 0.2rem;
}

.setting-field input[type="number"] {
    width: 100%;
    padding: 0.45rem 0.6rem;
    background: var(--bg-page);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    color: var(--text-primary);
    font-size: 0.9rem;
    outline: none;
    transition: border-color 0.15s;
}

.setting-field input[type="number"]:focus {
    border-color: var(--accent);
}

.setting-field .setting-hint {
    margin-top: 0.15rem;
    margin-bottom: 0;
}

/* === Responsive === */
@media (max-width: 768px) {
    .sidebar {
        position: absolute;
        z-index: 50;
        height: 100%;
        box-shadow: var(--shadow-lg);
    }

    .sidebar-toggle {
        display: flex;
    }

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

    .welcome-input-wrapper {
        max-width: 95%;
    }
}
