/* ============================================
   IMAP AI Email Client – Dark Theme
   ============================================ */

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

:root {
    --bg-darkest: #0a0a0f;
    --bg-dark: #0f0f16;
    --bg-panel: #141420;
    --bg-card: #1a1a2e;
    --bg-card-hover: #1f1f35;
    --bg-input: #12121c;
    --border: #2a2a3e;
    --border-light: #33334d;
    --accent: #6c63ff;
    --accent-hover: #7b73ff;
    --accent-glow: rgba(108, 99, 255, 0.15);
    --accent-secondary: #00d2ff;
    --danger: #ff4757;
    --success: #2ed573;
    --warning: #ffa502;
    --text-primary: #e8e8f0;
    --text-secondary: #8888a8;
    --text-muted: #555570;
    --text-accent: #a5a0ff;
    --radius-sm: 6px;
    --radius-md: 10px;
    --radius-lg: 14px;
    --radius-xl: 20px;
    --shadow: 0 2px 12px rgba(0, 0, 0, 0.4);
    --shadow-lg: 0 8px 32px rgba(0, 0, 0, 0.5);
    --transition: 0.2s ease;
}

html {
    font-size: 15px;
    -webkit-font-smoothing: antialiased;
    -moz-osx-font-smoothing: grayscale;
}

body {
    font-family: 'Inter', -apple-system, BlinkMacSystemFont, 'Segoe UI', system-ui, sans-serif;
    background: var(--bg-darkest);
    color: var(--text-primary);
    line-height: 1.6;
    overflow: hidden;
    height: 100vh;
}

/* --- 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);
}

/* --- Layout --- */
.app {
    display: grid;
    grid-template-rows: 56px 1fr;
    grid-template-columns: 340px 1fr;
    height: 100vh;
    gap: 0;
}

/* --- Header --- */
.header {
    grid-column: 1 / -1;
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 0 20px;
    background: var(--bg-panel);
    border-bottom: 1px solid var(--border);
    z-index: 100;
}

.header-logo {
    display: flex;
    align-items: center;
    gap: 10px;
    font-size: 1.15rem;
    font-weight: 700;
    color: var(--text-primary);
    letter-spacing: -0.02em;
}

.header-logo .logo-icon {
    width: 32px;
    height: 32px;
    background: linear-gradient(135deg, var(--accent), var(--accent-secondary));
    border-radius: var(--radius-sm);
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 1rem;
}

.header-actions {
    display: flex;
    align-items: center;
    gap: 8px;
}

/* --- Buttons --- */
.btn {
    display: inline-flex;
    align-items: center;
    gap: 6px;
    padding: 8px 16px;
    border: none;
    border-radius: var(--radius-sm);
    font-family: inherit;
    font-size: 0.85rem;
    font-weight: 500;
    cursor: pointer;
    transition: all var(--transition);
    white-space: nowrap;
}

.btn-primary {
    background: var(--accent);
    color: #fff;
}
.btn-primary:hover {
    background: var(--accent-hover);
    box-shadow: 0 0 20px var(--accent-glow);
}

.btn-secondary {
    background: var(--bg-card);
    color: var(--text-secondary);
    border: 1px solid var(--border);
}
.btn-secondary:hover {
    background: var(--bg-card-hover);
    color: var(--text-primary);
}

.btn-ghost {
    background: transparent;
    color: var(--text-secondary);
    padding: 8px;
}
.btn-ghost:hover {
    background: var(--bg-card);
    color: var(--text-primary);
}

.btn-icon {
    width: 36px;
    height: 36px;
    padding: 0;
    display: flex;
    align-items: center;
    justify-content: center;
    border-radius: var(--radius-sm);
}

.btn svg {
    width: 18px;
    height: 18px;
    flex-shrink: 0;
}

/* --- Sidebar (Email List) --- */
.sidebar {
    background: var(--bg-panel);
    border-right: 1px solid var(--border);
    display: flex;
    flex-direction: column;
    overflow: hidden;
}

.sidebar-header {
    padding: 14px 16px;
    border-bottom: 1px solid var(--border);
    display: flex;
    align-items: center;
    justify-content: space-between;
}

.sidebar-title {
    font-size: 0.85rem;
    font-weight: 600;
    color: var(--text-secondary);
    text-transform: uppercase;
    letter-spacing: 0.06em;
}

.email-count {
    font-size: 0.75rem;
    color: var(--text-muted);
    background: var(--bg-card);
    padding: 2px 8px;
    border-radius: 10px;
}

.email-list {
    flex: 1;
    overflow-y: auto;
    padding: 6px;
}

.email-item {
    padding: 12px 14px;
    border-radius: var(--radius-md);
    cursor: pointer;
    transition: all var(--transition);
    border: 1px solid transparent;
    margin-bottom: 2px;
}
.email-item:hover {
    background: var(--bg-card);
    border-color: var(--border);
}
.email-item.active {
    background: var(--accent-glow);
    border-color: var(--accent);
}
.email-item.unread {
    border-left: 3px solid var(--accent);
}

.email-item-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    margin-bottom: 4px;
}

.email-sender {
    font-size: 0.88rem;
    font-weight: 600;
    color: var(--text-primary);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    max-width: 200px;
}

.email-date {
    font-size: 0.72rem;
    color: var(--text-muted);
    white-space: nowrap;
    flex-shrink: 0;
}

.email-subject {
    font-size: 0.82rem;
    color: var(--text-secondary);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    margin-bottom: 3px;
}
.email-item.unread .email-subject {
    color: var(--text-primary);
    font-weight: 500;
}

.email-preview {
    font-size: 0.75rem;
    color: var(--text-muted);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.email-badges {
    display: flex;
    gap: 4px;
    margin-top: 4px;
}

.badge {
    font-size: 0.65rem;
    padding: 1px 6px;
    border-radius: 8px;
    background: var(--bg-card);
    color: var(--text-muted);
}
.badge-attachment {
    color: var(--warning);
    background: rgba(255, 165, 2, 0.1);
}

/* --- Main Content --- */
.main {
    display: flex;
    flex-direction: column;
    overflow: hidden;
    background: var(--bg-dark);
}

/* --- Summary Section --- */
.summary-section {
    flex: 1;
    overflow-y: auto;
    padding: 24px;
}

.summary-card {
    background: var(--bg-panel);
    border: 1px solid var(--border);
    border-radius: var(--radius-lg);
    padding: 24px;
    margin-bottom: 20px;
}

.summary-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    margin-bottom: 20px;
}

.summary-title {
    font-size: 1.1rem;
    font-weight: 700;
    color: var(--text-primary);
}

.summary-content {
    font-size: 0.9rem;
    line-height: 1.8;
    color: var(--text-secondary);
}

.summary-content h2 {
    font-size: 1rem;
    font-weight: 700;
    color: var(--text-primary);
    margin: 20px 0 10px 0;
    padding-bottom: 6px;
    border-bottom: 1px solid var(--border);
}
.summary-content h2:first-child {
    margin-top: 0;
}

.summary-content ul {
    list-style: none;
    padding-left: 0;
}
.summary-content ul li {
    padding: 6px 0;
    padding-left: 16px;
    position: relative;
}
.summary-content ul li::before {
    content: '';
    position: absolute;
    left: 0;
    top: 14px;
    width: 6px;
    height: 6px;
    border-radius: 50%;
    background: var(--accent);
}

.summary-content strong {
    color: var(--text-primary);
}

.summary-content p {
    margin-bottom: 8px;
}

.email-link {
    display: inline-block;
    color: var(--accent);
    cursor: pointer;
    font-size: 0.78rem;
    font-weight: 500;
    text-decoration: none;
    transition: color var(--transition);
}
.email-link:hover {
    color: var(--accent-hover);
    text-decoration: underline;
}

/* --- Welcome state --- */
.welcome-state {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    height: 100%;
    text-align: center;
    padding: 40px;
}

.welcome-icon {
    width: 80px;
    height: 80px;
    border-radius: 50%;
    background: linear-gradient(135deg, var(--accent-glow), rgba(0, 210, 255, 0.1));
    display: flex;
    align-items: center;
    justify-content: center;
    margin-bottom: 20px;
    font-size: 2.2rem;
}

.welcome-state h2 {
    font-size: 1.3rem;
    font-weight: 700;
    margin-bottom: 8px;
}
.welcome-state p {
    color: var(--text-secondary);
    font-size: 0.9rem;
    max-width: 400px;
    margin-bottom: 20px;
}

/* --- Chat Section --- */
.chat-section {
    border-top: 1px solid var(--border);
    background: var(--bg-panel);
    display: flex;
    flex-direction: column;
    max-height: 340px;
    min-height: 60px;
}

.chat-toggle {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 10px 20px;
    cursor: pointer;
    user-select: none;
    transition: background var(--transition);
}
.chat-toggle:hover {
    background: var(--bg-card);
}

.chat-toggle-title {
    font-size: 0.85rem;
    font-weight: 600;
    color: var(--text-secondary);
    display: flex;
    align-items: center;
    gap: 8px;
}

.chat-toggle-icon {
    transition: transform var(--transition);
    color: var(--text-muted);
}
.chat-section.expanded .chat-toggle-icon {
    transform: rotate(180deg);
}

.chat-body {
    display: none;
    flex-direction: column;
    flex: 1;
    overflow: hidden;
}
.chat-section.expanded .chat-body {
    display: flex;
}

.chat-messages {
    flex: 1;
    overflow-y: auto;
    padding: 12px 20px;
    display: flex;
    flex-direction: column;
    gap: 10px;
    max-height: 220px;
}

.chat-msg {
    max-width: 85%;
    padding: 10px 14px;
    border-radius: var(--radius-md);
    font-size: 0.85rem;
    line-height: 1.6;
}
.chat-msg.user {
    align-self: flex-end;
    background: var(--accent);
    color: #fff;
    border-bottom-right-radius: 4px;
}
.chat-msg.assistant {
    align-self: flex-start;
    background: var(--bg-card);
    color: var(--text-secondary);
    border-bottom-left-radius: 4px;
}
.chat-msg.assistant strong {
    color: var(--text-primary);
}

.chat-input-wrap {
    display: flex;
    gap: 8px;
    padding: 12px 20px;
    border-top: 1px solid var(--border);
}

.chat-input {
    flex: 1;
    background: var(--bg-input);
    border: 1px solid var(--border);
    border-radius: var(--radius-md);
    padding: 10px 14px;
    color: var(--text-primary);
    font-family: inherit;
    font-size: 0.85rem;
    outline: none;
    transition: border-color var(--transition);
}
.chat-input:focus {
    border-color: var(--accent);
}
.chat-input::placeholder {
    color: var(--text-muted);
}

.chat-send {
    width: 40px;
    height: 40px;
    background: var(--accent);
    border: none;
    border-radius: var(--radius-md);
    color: #fff;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: all var(--transition);
    flex-shrink: 0;
}
.chat-send:hover {
    background: var(--accent-hover);
}
.chat-send:disabled {
    opacity: 0.4;
    cursor: not-allowed;
}
.chat-send svg {
    width: 18px;
    height: 18px;
}

/* --- Modal --- */
.modal-overlay {
    position: fixed;
    inset: 0;
    background: rgba(0, 0, 0, 0.7);
    backdrop-filter: blur(4px);
    display: none;
    align-items: center;
    justify-content: center;
    z-index: 1000;
    padding: 20px;
}
.modal-overlay.active {
    display: flex;
}

.modal {
    background: var(--bg-panel);
    border: 1px solid var(--border);
    border-radius: var(--radius-xl);
    width: 100%;
    max-width: 500px;
    box-shadow: var(--shadow-lg);
    overflow: hidden;
    animation: modalIn 0.25s ease;
}

@keyframes modalIn {
    from { opacity: 0; transform: scale(0.95) translateY(10px); }
    to { opacity: 1; transform: scale(1) translateY(0); }
}

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

.modal-title {
    font-size: 1.05rem;
    font-weight: 700;
}

.modal-close {
    width: 32px;
    height: 32px;
    background: var(--bg-card);
    border: none;
    border-radius: var(--radius-sm);
    color: var(--text-secondary);
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: all var(--transition);
}
.modal-close:hover {
    background: var(--danger);
    color: #fff;
}

.modal-body {
    padding: 24px;
}

.modal-footer {
    padding: 16px 24px;
    border-top: 1px solid var(--border);
    display: flex;
    justify-content: flex-end;
    gap: 8px;
}

/* Email detail modal */
.modal-email {
    max-width: 720px;
}
.modal-email .modal-body {
    max-height: 60vh;
    overflow-y: auto;
}

.email-detail-meta {
    margin-bottom: 16px;
    padding-bottom: 16px;
    border-bottom: 1px solid var(--border);
}
.email-detail-subject {
    font-size: 1.15rem;
    font-weight: 700;
    margin-bottom: 10px;
}
.email-detail-info {
    display: flex;
    flex-direction: column;
    gap: 4px;
    font-size: 0.82rem;
    color: var(--text-secondary);
}
.email-detail-info span {
    display: flex;
    gap: 6px;
}
.email-detail-info strong {
    color: var(--text-muted);
    min-width: 50px;
}
.email-detail-body {
    font-size: 0.9rem;
    line-height: 1.7;
    color: var(--text-secondary);
}
.email-detail-body img {
    max-width: 100%;
    height: auto;
}

/* --- Form --- */
.form-group {
    margin-bottom: 16px;
}

.form-label {
    display: block;
    font-size: 0.8rem;
    font-weight: 600;
    color: var(--text-secondary);
    margin-bottom: 6px;
    text-transform: uppercase;
    letter-spacing: 0.04em;
}

.form-input {
    width: 100%;
    background: var(--bg-input);
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    padding: 10px 14px;
    color: var(--text-primary);
    font-family: inherit;
    font-size: 0.9rem;
    outline: none;
    transition: border-color var(--transition);
}
.form-input:focus {
    border-color: var(--accent);
}
.form-input::placeholder {
    color: var(--text-muted);
}

.form-select {
    width: 100%;
    background: var(--bg-input);
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    padding: 10px 14px;
    color: var(--text-primary);
    font-family: inherit;
    font-size: 0.9rem;
    outline: none;
    appearance: none;
    cursor: pointer;
    background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='12' height='12' viewBox='0 0 24 24' fill='none' stroke='%238888a8' stroke-width='2' stroke-linecap='round' stroke-linejoin='round'%3E%3Cpolyline points='6 9 12 15 18 9'%3E%3C/polyline%3E%3C/svg%3E");
    background-repeat: no-repeat;
    background-position: right 14px center;
}
.form-select:focus {
    border-color: var(--accent);
}

.form-row {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 12px;
}

.form-hint {
    font-size: 0.72rem;
    color: var(--text-muted);
    margin-top: 4px;
}

/* --- Loader --- */
.loader {
    display: flex;
    align-items: center;
    gap: 8px;
    padding: 16px 0;
    color: var(--text-muted);
    font-size: 0.85rem;
}

.loader-dots {
    display: flex;
    gap: 4px;
}

.loader-dot {
    width: 6px;
    height: 6px;
    border-radius: 50%;
    background: var(--accent);
    animation: loaderPulse 1.4s ease-in-out infinite;
}
.loader-dot:nth-child(2) { animation-delay: 0.2s; }
.loader-dot:nth-child(3) { animation-delay: 0.4s; }

@keyframes loaderPulse {
    0%, 80%, 100% { transform: scale(0.6); opacity: 0.3; }
    40% { transform: scale(1); opacity: 1; }
}

/* Skeleton loader */
.skeleton {
    background: linear-gradient(90deg, var(--bg-card) 25%, var(--bg-card-hover) 50%, var(--bg-card) 75%);
    background-size: 200% 100%;
    animation: skeleton 1.5s ease infinite;
    border-radius: var(--radius-sm);
}
@keyframes skeleton {
    0% { background-position: 200% 0; }
    100% { background-position: -200% 0; }
}

.skeleton-line {
    height: 14px;
    margin-bottom: 10px;
}
.skeleton-line:last-child {
    width: 60%;
}
.skeleton-title {
    height: 20px;
    width: 50%;
    margin-bottom: 16px;
}

/* --- Notification / Toast --- */
.toast-container {
    position: fixed;
    top: 70px;
    right: 20px;
    z-index: 2000;
    display: flex;
    flex-direction: column;
    gap: 8px;
}

.toast {
    background: var(--bg-card);
    border: 1px solid var(--border);
    border-radius: var(--radius-md);
    padding: 12px 18px;
    font-size: 0.85rem;
    box-shadow: var(--shadow-lg);
    animation: toastIn 0.3s ease;
    max-width: 360px;
    display: flex;
    align-items: center;
    gap: 10px;
}
.toast.error {
    border-color: var(--danger);
    color: var(--danger);
}
.toast.success {
    border-color: var(--success);
    color: var(--success);
}

@keyframes toastIn {
    from { opacity: 0; transform: translateX(30px); }
    to { opacity: 1; transform: translateX(0); }
}

/* --- Empty state --- */
.empty-state {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    height: 100%;
    color: var(--text-muted);
    text-align: center;
    padding: 40px 20px;
}
.empty-state svg {
    width: 48px;
    height: 48px;
    margin-bottom: 14px;
    opacity: 0.3;
}
.empty-state p {
    font-size: 0.85rem;
}

/* --- Status indicator --- */
.status-dot {
    width: 8px;
    height: 8px;
    border-radius: 50%;
    display: inline-block;
}
.status-dot.connected {
    background: var(--success);
    box-shadow: 0 0 8px rgba(46, 213, 115, 0.4);
}
.status-dot.disconnected {
    background: var(--text-muted);
}

/* --- Responsive --- */
@media (max-width: 1024px) {
    .app {
        grid-template-columns: 280px 1fr;
    }
}

@media (max-width: 768px) {
    .app {
        grid-template-columns: 1fr;
        grid-template-rows: 56px 1fr;
    }

    .sidebar {
        display: none;
        position: fixed;
        top: 56px;
        left: 0;
        right: 0;
        bottom: 0;
        z-index: 500;
        border-right: none;
    }
    .sidebar.mobile-open {
        display: flex;
    }

    .btn-mobile-menu {
        display: flex !important;
    }

    .summary-section {
        padding: 16px;
    }

    .summary-card {
        padding: 16px;
    }

    .chat-input-wrap {
        padding: 10px 14px;
    }
}

.btn-mobile-menu {
    display: none;
}

/* --- Markdown in AI responses --- */
.md-content h2 {
    font-size: 1rem;
    font-weight: 700;
    color: var(--text-primary);
    margin: 18px 0 8px 0;
    padding-bottom: 6px;
    border-bottom: 1px solid var(--border);
}
.md-content h3 {
    font-size: 0.92rem;
    font-weight: 600;
    color: var(--text-primary);
    margin: 14px 0 6px 0;
}
.md-content p {
    margin-bottom: 8px;
}
.md-content ul, .md-content ol {
    margin: 6px 0 10px 18px;
}
.md-content li {
    margin-bottom: 4px;
}
.md-content code {
    background: var(--bg-card);
    padding: 2px 6px;
    border-radius: 4px;
    font-size: 0.82rem;
}
.md-content blockquote {
    border-left: 3px solid var(--accent);
    padding-left: 14px;
    color: var(--text-muted);
    margin: 8px 0;
}
