/* ============================================
   Email Module - Gmail-style Layout (Responsive)
   ============================================ */

/* Nav badge */
.nav-badge {
    background: var(--danger);
    color: white;
    font-size: 11px;
    padding: 1px 6px;
    border-radius: 10px;
    margin-left: 6px;
    font-weight: 600;
}

/* Main email layout — always fits viewport, never overflows */
.email-app {
    display: flex;
    height: calc(100vh - var(--topbar-height) - 20px);
    background: var(--bg-white);
    border-radius: var(--radius);
    overflow: hidden;
    box-shadow: var(--shadow-md);
    width: 100%;
    box-sizing: border-box;
}

/* Override page padding for email — it needs full width */
#page-email {
    padding: 8px !important;
    overflow: hidden !important;
    max-width: 100% !important;
    box-sizing: border-box !important;
}
#page-email .email-app {
    width: 100% !important;
    max-width: 100% !important;
}

/* Prevent any parent container from scrolling horizontally */
.main-content:has(#page-email.active) {
    overflow-x: hidden !important;
}
.content-area:has(#page-email.active) {
    overflow-x: hidden !important;
}

/* Email Sidebar */
.email-sidebar {
    width: 200px;
    min-width: 160px;
    flex-shrink: 0;
    background: #f6f8fc;
    border-right: 1px solid var(--border-color);
    display: flex;
    flex-direction: column;
    padding: 12px 0;
    overflow-y: auto;
    overflow-x: hidden;
}

.email-compose-btn {
    margin: 0 8px 12px;
    padding: 8px 16px;
    border-radius: 24px;
    font-size: 13px;
    box-shadow: var(--shadow-md);
    white-space: nowrap;
}

.email-account-selector {
    padding: 0 6px;
    margin-bottom: 8px;
    border-bottom: 1px solid var(--border-color);
    padding-bottom: 8px;
}

.email-account-item {
    display: flex;
    align-items: center;
    gap: 6px;
    padding: 6px 8px;
    border-radius: 8px;
    cursor: pointer;
    transition: background 0.15s;
}
.email-account-item:hover { background: rgba(0,0,0,0.04); }
.email-account-item.active { background: rgba(102,126,234,0.12); }

.email-account-avatar {
    width: 28px; height: 28px;
    border-radius: 50%;
    background: linear-gradient(135deg, var(--primary), var(--secondary));
    color: white;
    display: flex; align-items: center; justify-content: center;
    font-weight: 600; font-size: 12px;
    flex-shrink: 0;
}

.email-account-info { flex: 1; min-width: 0; overflow: hidden; }
.email-account-label { font-weight: 500; font-size: 12px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.email-account-email { font-size: 10px; color: var(--text-muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }

.email-unread-count {
    background: var(--danger);
    color: white;
    font-size: 10px;
    padding: 1px 5px;
    border-radius: 10px;
    font-weight: 600;
    flex-shrink: 0;
}

/* Account gear button */
.email-account-gear {
    background: none;
    border: none;
    color: var(--text-muted);
    cursor: pointer;
    padding: 2px 4px;
    border-radius: 4px;
    font-size: 11px;
    opacity: 0;
    transition: opacity 0.15s;
    flex-shrink: 0;
}
.email-account-item:hover .email-account-gear { opacity: 1; }
.email-account-gear:hover { background: rgba(0,0,0,0.08); color: var(--text-primary); }

/* Folder List */
.email-folder-list {
    flex: 1;
    overflow-y: auto;
    padding: 4px 6px;
}

.email-folder-item {
    display: flex;
    align-items: center;
    gap: 8px;
    padding: 7px 10px;
    border-radius: 0 20px 20px 0;
    cursor: pointer;
    font-size: 12px;
    color: var(--text-secondary);
    transition: all 0.15s;
    white-space: nowrap;
}
.email-folder-item:hover { background: rgba(0,0,0,0.04); }
.email-folder-item.active { background: rgba(102,126,234,0.15); color: var(--primary); font-weight: 600; }
.email-folder-item i { width: 16px; text-align: center; font-size: 13px; flex-shrink: 0; }

.email-folder-name { flex: 1; overflow: hidden; text-overflow: ellipsis; }
.email-folder-count { font-size: 11px; font-weight: 600; color: var(--text-muted); flex-shrink: 0; }

.email-sidebar-footer {
    padding: 8px;
    border-top: 1px solid var(--border-color);
}
.email-sidebar-footer .btn { width: 100%; font-size: 12px; }

.email-no-accounts {
    padding: 16px;
    text-align: center;
    color: var(--text-muted);
    font-size: 12px;
}

/* Email Content Area — takes remaining space */
.email-content {
    flex: 1;
    display: flex;
    flex-direction: column;
    min-width: 0;
    overflow: hidden;
}

/* Toolbar */
.email-toolbar {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 6px 12px;
    border-bottom: 1px solid var(--border-color);
    gap: 6px;
    flex-shrink: 0;
    flex-wrap: wrap;
}
.email-toolbar-left { display: flex; align-items: center; gap: 6px; flex: 1; min-width: 0; }
.email-toolbar-right { display: flex; align-items: center; gap: 6px; flex-shrink: 0; }

.email-search {
    flex: 1;
    min-width: 100px;
    max-width: 300px;
    padding: 5px 10px;
    border: 1px solid var(--border-color);
    border-radius: 20px;
    font-size: 12px;
    outline: none;
    background: var(--bg-light);
}
.email-search:focus { border-color: var(--primary); background: white; }

.email-pagination {
    font-size: 11px;
    color: var(--text-muted);
    display: flex;
    align-items: center;
    gap: 4px;
    white-space: nowrap;
}
.email-pagination .btn { padding: 2px 6px; }

/* Email Body (list + reading pane side by side) */
.email-body {
    flex: 1;
    display: flex;
    overflow: hidden;
    min-height: 0;
}

/* Message List */
.email-message-list {
    flex: 1;
    overflow-y: auto;
    min-width: 0;
}

/* Bulk action bar */
.email-bulk-bar {
    display: flex;
    align-items: center;
    gap: 6px;
    padding: 4px 12px;
    background: #e8f0fe;
    border-bottom: 1px solid #d0ddf0;
    font-size: 12px;
    flex-wrap: wrap;
}

.email-list-item {
    display: flex;
    align-items: center;
    padding: 6px 12px;
    border-bottom: 1px solid #f0f0f0;
    cursor: pointer;
    transition: background 0.1s;
    font-size: 12px;
    line-height: 1.4;
}
.email-list-item:hover { background: #f8f9fa; }
.email-list-item.selected { background: #e8f0fe; }
.email-list-item.unread { background: #ffffff; }
.email-list-item.unread .email-list-from { font-weight: 700; color: var(--text-primary); }
.email-list-item.unread .email-list-subject { font-weight: 600; color: var(--text-primary); }

.email-list-checkbox { flex-shrink: 0; margin-right: 6px; }
.email-list-checkbox input { cursor: pointer; width: 14px; height: 14px; }

.email-list-from {
    width: 140px;
    min-width: 80px;
    font-size: 12px;
    color: var(--text-secondary);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    padding-right: 8px;
    flex-shrink: 0;
}

.email-list-content {
    flex: 1;
    min-width: 0;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}
.email-list-subject { color: var(--text-primary); font-size: 12px; }
.email-list-preview { color: var(--text-muted); font-size: 11px; }

.email-list-meta {
    display: flex;
    align-items: center;
    gap: 4px;
    margin-left: 8px;
    flex-shrink: 0;
    color: var(--text-muted);
    font-size: 11px;
}
.email-list-meta i { font-size: 11px; }

/* Reading Pane — uses flex percentage, not fixed width */
.email-reading-pane {
    width: 50%;
    max-width: 55%;
    min-width: 0;
    border-left: 1px solid var(--border-color);
    display: flex;
    flex-direction: column;
    overflow-y: auto;
    overflow-x: hidden;
    background: white;
    flex-shrink: 0;
}

.email-read-header {
    padding: 12px 16px;
    border-bottom: 1px solid #f0f0f0;
}
.email-read-actions {
    display: flex;
    gap: 4px;
    margin-bottom: 10px;
    flex-wrap: wrap;
}
.email-read-actions .btn { padding: 3px 8px; font-size: 11px; }

.email-read-subject {
    font-size: 18px;
    font-weight: 400;
    margin-bottom: 12px;
    line-height: 1.3;
    word-wrap: break-word;
}

.email-read-meta { display: flex; flex-direction: column; gap: 6px; }
.email-read-from { display: flex; align-items: center; gap: 10px; }

.email-avatar-lg {
    width: 36px; height: 36px;
    border-radius: 50%;
    background: linear-gradient(135deg, var(--primary), var(--secondary));
    color: white;
    display: flex; align-items: center; justify-content: center;
    font-weight: 600; font-size: 16px;
    flex-shrink: 0;
}

.email-read-address { color: var(--text-muted); font-size: 12px; }
.email-read-date { color: var(--text-muted); font-size: 11px; margin-top: 2px; }
.email-read-recipients { font-size: 11px; color: var(--text-muted); padding-left: 46px; word-break: break-all; }

.email-attachments {
    margin-top: 10px;
    padding: 6px 10px;
    background: var(--bg-light);
    border-radius: 8px;
    font-size: 11px;
    color: var(--text-secondary);
    display: flex;
    align-items: center;
    gap: 4px;
    flex-wrap: wrap;
}
.email-attachment-chip {
    background: white;
    padding: 2px 8px;
    border-radius: 12px;
    border: 1px solid var(--border-color);
    font-size: 11px;
}

.email-read-body { flex: 1; padding: 0; overflow: hidden; }
.email-body-iframe {
    width: 100%;
    min-height: 200px;
    border: none;
    padding: 16px;
    box-sizing: border-box;
}

/* Empty state */
.email-empty-state {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    height: 100%;
    min-height: 200px;
    color: var(--text-muted);
    gap: 10px;
}

.email-loading {
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 40px;
    color: var(--text-muted);
    gap: 8px;
}

/* ============================================
   Compose Window (Gmail-style floating popup)
   ============================================ */
#composeWrapper {
    position: fixed;
    bottom: 0;
    right: 20px;
    z-index: 2000;
    max-width: calc(100vw - 40px);
}

.compose-window {
    width: 480px;
    max-width: calc(100vw - 40px);
    background: white;
    border-radius: 12px 12px 0 0;
    box-shadow: 0 -4px 20px rgba(0,0,0,0.2);
    display: flex;
    flex-direction: column;
    max-height: 70vh;
    overflow: hidden;
}

.compose-window.minimized { max-height: 44px; overflow: hidden; }

.compose-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 8px 14px;
    background: var(--bg-dark);
    color: white;
    font-size: 13px;
    font-weight: 500;
    border-radius: 12px 12px 0 0;
    cursor: pointer;
}
.compose-btn {
    background: none;
    border: none;
    color: white;
    cursor: pointer;
    padding: 4px 6px;
    font-size: 13px;
    opacity: 0.8;
}
.compose-btn:hover { opacity: 1; }

.compose-body {
    padding: 6px 12px;
    flex: 1;
    overflow-y: auto;
}

.compose-field {
    display: flex;
    align-items: center;
    border-bottom: 1px solid #f0f0f0;
    padding: 3px 0;
}
.compose-field label {
    width: 45px;
    font-size: 12px;
    color: var(--text-muted);
    flex-shrink: 0;
}
.compose-field input, .compose-field select {
    flex: 1;
    border: none;
    outline: none;
    font-size: 12px;
    padding: 5px 4px;
    background: transparent;
    min-width: 0;
}

/* Compose Rich Text Toolbar */
.compose-toolbar {
    display: flex;
    align-items: center;
    gap: 1px;
    padding: 3px 2px;
    border-bottom: 1px solid var(--border-color);
    background: #fafafa;
    flex-wrap: wrap;
}
.compose-toolbar button {
    background: none;
    border: none;
    cursor: pointer;
    padding: 3px 5px;
    border-radius: 3px;
    color: var(--text-secondary);
    font-size: 12px;
}
.compose-toolbar button:hover { background: rgba(0,0,0,0.08); color: var(--text-primary); }
.compose-toolbar select {
    border: 1px solid var(--border-color);
    border-radius: 3px;
    padding: 1px 2px;
    font-size: 10px;
    cursor: pointer;
    background: white;
    max-width: 70px;
}
.compose-toolbar-sep {
    width: 1px;
    height: 18px;
    background: var(--border-color);
    margin: 0 2px;
}

.compose-editor { margin-top: 6px; flex: 1; }
.compose-textarea {
    min-height: 180px;
    padding: 6px;
    font-size: 12px;
    line-height: 1.5;
    outline: none;
    overflow-y: auto;
}

.compose-footer {
    display: flex;
    gap: 6px;
    padding: 8px 12px;
    border-top: 1px solid var(--border-color);
    flex-wrap: wrap;
}
.compose-footer .btn { font-size: 12px; }

/* Schedule panel */
.compose-schedule-wrap { position: relative; }
.compose-schedule-panel {
    position: absolute;
    bottom: 40px;
    left: 0;
    background: white;
    border: 1px solid var(--border-color);
    border-radius: 8px;
    padding: 8px 10px;
    box-shadow: var(--shadow-lg);
    display: flex;
    align-items: center;
    gap: 6px;
    z-index: 10;
    white-space: nowrap;
    font-size: 12px;
}

/* ============================================
   Email Autocomplete Dropdown
   ============================================ */
.email-autocomplete-list {
    display: none;
    position: absolute;
    top: 100%;
    left: 45px;
    right: 0;
    background: white;
    border: 1px solid var(--border-color);
    border-radius: 8px;
    box-shadow: 0 4px 12px rgba(0,0,0,0.15);
    z-index: 100;
    max-height: 200px;
    overflow-y: auto;
}
.email-ac-item {
    padding: 8px 12px;
    font-size: 12px;
    cursor: pointer;
    border-bottom: 1px solid #f5f5f5;
}
.email-ac-item:last-child { border-bottom: none; }
.email-ac-item:hover { background: #e8f0fe; }

/* ============================================
   Responsive Breakpoints
   ============================================ */
@media (max-width: 1200px) {
    .email-sidebar { width: 180px; min-width: 140px; }
    .email-list-from { width: 110px; min-width: 70px; }
    .email-reading-pane { width: 45%; }
}

@media (max-width: 992px) {
    .email-sidebar { width: 160px; min-width: 120px; }
    .email-reading-pane { width: 45%; }
    .email-list-from { width: 90px; min-width: 60px; }
    .email-list-preview { display: none; }
    .email-read-subject { font-size: 16px; }
}

@media (max-width: 768px) {
    .email-app { flex-direction: column; height: auto; min-height: calc(100vh - var(--topbar-height) - 20px); }
    .email-sidebar {
        width: 100% !important;
        min-width: unset !important;
        max-height: 150px;
        flex-direction: row;
        flex-wrap: wrap;
        padding: 8px;
        gap: 6px;
        border-right: none;
        border-bottom: 1px solid var(--border-color);
    }
    .email-account-selector { border-bottom: none; padding: 0; margin: 0; display: flex; gap: 4px; flex-wrap: wrap; }
    .email-folder-list { display: flex; gap: 2px; padding: 0; overflow-x: auto; overflow-y: hidden; }
    .email-folder-item { border-radius: 16px; padding: 4px 10px; font-size: 11px; }
    .email-compose-btn { margin: 0; padding: 6px 12px; font-size: 12px; }
    .email-sidebar-footer { padding: 0; border: none; }

    .email-body { flex-direction: column; }
    .email-message-list { max-height: 40vh; }
    .email-reading-pane {
        width: 100% !important;
        max-width: 100% !important;
        min-width: unset !important;
        border-left: none;
        border-top: 1px solid var(--border-color);
    }

    .email-list-from { width: 80px; min-width: 60px; }
    .email-list-preview { display: none; }

    .compose-window { width: calc(100vw - 20px); max-width: calc(100vw - 20px); }
    #composeWrapper { right: 10px; }
}

@media (max-width: 480px) {
    .email-toolbar { padding: 4px 8px; }
    .email-search { max-width: 150px; font-size: 11px; padding: 4px 8px; }
    .email-list-item { padding: 5px 8px; }
    .email-list-from { width: 60px; min-width: 50px; font-size: 11px; }
    .email-list-subject { font-size: 11px; }
    .email-read-header { padding: 10px 12px; }
    .email-read-subject { font-size: 15px; }
    .compose-toolbar select { display: none; }
    .compose-toolbar-sep:nth-child(2) { display: none; }
}
