/* Theme variables */
:root {
    --bg-page: #f5f5f5;
    --bg-content: #fff;
    --bg-hover: #f0f7ff;
    --bg-table-header: #f9fafb;
    --text-primary: #1a1a1a;
    --text-secondary: #6b7280;
    --text-tertiary: #9ca3af;
    --text-heading: #374151;
    --border: #e5e7eb;
    --border-light: #f3f4f6;
    --input-border: #d1d5db;
    --sidebar-bg: #1e293b;
    --sidebar-text: #e2e8f0;
    --sidebar-hover: #334155;
    --sidebar-border: #334155;
    --accent: #2563eb;
    --accent-hover: #1d4ed8;
    --accent-light: #0066cc;
    --badge-bg: #3b82f6;
    --error: #dc2626;
    --error-bg: #fef2f2;
    --error-border: #fca5a5;
    --success: #16a34a;
    --success-bg: #f0fdf4;
    --success-border: #86efac;
    --attachment-bg: #f3f4f6;
    --calendar-empty: #fafafa;
    --calendar-today: #eff6ff;
    --tentative-bg: #fef9c3;
    --tentative-text: #a16207;
    --accepted-bg: #dcfce7;
    --accepted-text: #16a34a;
}

[data-theme="dark"] {
    --bg-page: #0f172a;
    --bg-content: #1e293b;
    --bg-hover: #334155;
    --bg-table-header: #1e293b;
    --text-primary: #e2e8f0;
    --text-secondary: #94a3b8;
    --text-tertiary: #64748b;
    --text-heading: #e2e8f0;
    --border: #334155;
    --border-light: #1e293b;
    --input-border: #475569;
    --sidebar-bg: #0f172a;
    --sidebar-text: #e2e8f0;
    --sidebar-hover: #1e293b;
    --sidebar-border: #1e293b;
    --accent: #3b82f6;
    --accent-hover: #2563eb;
    --accent-light: #60a5fa;
    --badge-bg: #3b82f6;
    --error: #f87171;
    --error-bg: #450a0a;
    --error-border: #991b1b;
    --success: #4ade80;
    --success-bg: #052e16;
    --success-border: #166534;
    --attachment-bg: #334155;
    --calendar-empty: #1e293b;
    --calendar-today: #172554;
    --tentative-bg: #422006;
    --tentative-text: #fbbf24;
    --accepted-bg: #052e16;
    --accepted-text: #4ade80;
}

/* Reset & base */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }
html, body { height: 100%; font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif; font-size: 14px; color: var(--text-primary); background: var(--bg-page); }
a { color: var(--accent-light); text-decoration: none; }
a:hover { text-decoration: underline; }

/* Layout */
.layout { display: flex; height: 100vh; }
.sidebar { width: 240px; background: var(--sidebar-bg); color: var(--sidebar-text); display: flex; flex-direction: column; flex-shrink: 0; }
.sidebar-header { padding: 16px; border-bottom: 1px solid var(--sidebar-border); }
.sidebar-logo { width: 140px; height: auto; display: block; margin-bottom: 8px; }
.sidebar-header .user-name { font-size: 12px; color: #94a3b8; }
.sidebar-actions { padding: 12px 16px; display: flex; flex-direction: column; gap: 8px; }
.folder-list { list-style: none; flex: 1; overflow-y: auto; }
.folder-list li a { display: flex; justify-content: space-between; padding: 8px 16px; color: #cbd5e1; font-size: 13px; transition: background 0.15s; }
.folder-list li a:hover { background: var(--sidebar-hover); text-decoration: none; }
.folder-list li.active a { background: var(--sidebar-hover); color: #fff; font-weight: 600; }
.folder-list li.folder-child a { padding-left: 32px; font-size: 12px; }
.badge { background: var(--badge-bg); color: #fff; font-size: 11px; padding: 1px 6px; border-radius: 10px; }
.sidebar-footer { padding: 12px 16px; border-top: 1px solid var(--sidebar-border); display: flex; flex-direction: column; gap: 8px; }

/* Main column (content area + top bar) */
.main-column { flex: 1; display: flex; flex-direction: column; min-width: 0; }
.top-bar { display: flex; align-items: center; gap: 12px; padding: 8px 16px; background: var(--sidebar-bg); border-bottom: 1px solid var(--sidebar-border); flex-shrink: 0; }
.top-bar form { flex: 1; }
.top-bar input[type="text"] { width: 100%; padding: 6px 12px; border: 1px solid #475569; border-radius: 4px; background: #334155; color: #e2e8f0; font-size: 13px; }
.top-bar input[type="text"]::placeholder { color: #94a3b8; }
.top-bar input[type="text"]:focus { outline: none; border-color: var(--accent); }
.theme-toggle { background: none; border: 1px solid #475569; border-radius: 4px; color: #e2e8f0; font-size: 18px; cursor: pointer; padding: 4px 8px; line-height: 1; flex-shrink: 0; }
.theme-toggle:hover { background: #334155; }

/* Search dropdown */
.search-wrapper { position: relative; flex: 1; }
.search-dropdown { display: none; position: absolute; top: 100%; left: 0; right: 0; background: #334155; border: 1px solid #475569; border-top: none; border-radius: 0 0 4px 4px; max-height: 300px; overflow-y: auto; z-index: 200; box-shadow: 0 4px 12px rgba(0,0,0,0.2); }
.search-dropdown-item { display: flex; align-items: center; gap: 8px; padding: 8px 12px; color: #e2e8f0; font-size: 13px; cursor: pointer; }
.search-dropdown-item:hover { background: #475569; }
.search-dropdown-item .search-icon { color: #94a3b8; font-size: 12px; }
.search-dropdown-clear { padding: 6px 12px; font-size: 11px; color: #94a3b8; cursor: pointer; border-top: 1px solid #475569; text-align: center; }
.search-dropdown-clear:hover { color: #e2e8f0; }

.content { flex: 1; overflow-y: auto; padding: 0; background: var(--bg-content); }

/* Buttons */
.btn { display: inline-block; padding: 8px 16px; border: 1px solid var(--input-border); border-radius: 4px; background: var(--bg-content); color: var(--text-heading); font-size: 13px; cursor: pointer; text-align: center; }
.btn:hover { background: var(--bg-table-header); text-decoration: none; }
.btn-primary { background: var(--accent); color: #fff; border-color: var(--accent); }
.btn-primary:hover { background: var(--accent-hover); }
.btn-compose { display: block; width: 100%; background: var(--accent); color: #fff; border: none; border-radius: 4px; padding: 10px; text-align: center; font-weight: 600; }
.btn-compose:hover { background: var(--accent-hover); text-decoration: none; }
.btn-sm { padding: 4px 10px; font-size: 12px; }
.btn-danger { color: var(--error); border-color: var(--error-border); }
.btn-danger:hover { background: var(--error-bg); }
.btn-full { width: 100%; }
.btn-logout { color: #94a3b8; font-size: 13px; }
.btn-logout:hover { color: #fff; }

/* Login page */
.login-page { display: flex; align-items: center; justify-content: center; min-height: 100vh; background: var(--sidebar-bg); }
.login-container { background: var(--bg-content); padding: 40px; border-radius: 8px; width: 360px; box-shadow: 0 4px 24px rgba(0,0,0,0.2); }
.login-container h1 { text-align: center; margin-bottom: 24px; color: var(--text-primary); }
.login-logo { display: block; width: 180px; height: auto; margin: 0 auto 24px; }

/* Forms */
.form-group { margin-bottom: 16px; }
.form-group label { display: block; margin-bottom: 4px; font-weight: 500; color: var(--text-heading); font-size: 13px; }
.form-group input, .form-group textarea, .form-group select { width: 100%; padding: 8px 12px; border: 1px solid var(--input-border); border-radius: 4px; font-size: 14px; font-family: inherit; background: var(--bg-content); color: var(--text-primary); }
.form-group input:focus, .form-group textarea:focus { outline: none; border-color: var(--accent); box-shadow: 0 0 0 2px rgba(37,99,235,0.15); }
.form-group textarea { resize: vertical; }
.form-actions { display: flex; gap: 8px; margin-top: 16px; }

/* Alerts */
.alert { padding: 10px 14px; border-radius: 4px; margin-bottom: 16px; font-size: 13px; }
.alert-error { background: var(--error-bg); color: var(--error); border: 1px solid var(--error-border); }
.alert-success { background: var(--success-bg); color: var(--success); border: 1px solid var(--success-border); }

/* Message list */
.message-list-header { padding: 16px 20px; border-bottom: 1px solid var(--border); }
.message-list-header h3 { font-size: 16px; color: var(--text-heading); }
.message-table { width: 100%; border-collapse: collapse; }
.message-table thead { background: var(--bg-table-header); }
.message-table th { text-align: left; padding: 8px 12px; font-size: 12px; color: var(--text-secondary); font-weight: 500; text-transform: uppercase; border-bottom: 1px solid var(--border); }
.message-table td { padding: 10px 12px; border-bottom: 1px solid var(--border-light); font-size: 13px; color: var(--text-primary); }
.message-table tr { cursor: pointer; transition: background 0.1s; }
.message-table tbody tr:hover { background: var(--bg-hover); }
.message-table tr.unread td { font-weight: 600; }
.col-from { width: 200px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.col-date { width: 130px; white-space: nowrap; color: var(--text-secondary); }
.col-subject .preview { color: var(--text-tertiary); font-weight: 400; margin-left: 8px; }
.att-icon { margin-right: 4px; }

/* Pagination */
.pagination { display: flex; align-items: center; gap: 12px; padding: 12px 20px; border-top: 1px solid var(--border); }
.page-info { font-size: 13px; color: var(--text-secondary); }

/* Empty state */
.empty-state { padding: 60px 20px; text-align: center; color: var(--text-tertiary); }

/* Message view */
.message-view { padding: 20px; }
.message-toolbar { display: flex; gap: 6px; align-items: center; flex-wrap: wrap; margin-bottom: 16px; padding-bottom: 12px; border-bottom: 1px solid var(--border-light); }
.message-header { margin-bottom: 16px; padding: 12px 16px; background: var(--bg-table-header); border-radius: 6px; }
.message-header h2 { font-size: 20px; margin-bottom: 8px; color: var(--text-primary); }
.message-meta { font-size: 13px; color: var(--text-secondary); line-height: 1.8; }
.attachments { padding: 10px 0; margin-bottom: 12px; border-bottom: 1px solid var(--border); font-size: 13px; }
.attachment-link { display: inline-block; margin: 4px 8px 4px 0; padding: 4px 10px; background: var(--attachment-bg); border-radius: 4px; color: var(--accent); }
.attachment-link:hover { background: var(--border); text-decoration: none; }
.message-body { line-height: 1.6; overflow-wrap: break-word; word-break: break-word; }
.message-body img { max-width: 100%; height: auto; }

/* Keep email body readable in dark mode */
[data-theme="dark"] .message-body { background: #fff; color: #1a1a1a; padding: 16px; border-radius: 4px; }

/* Compose */
.compose-view { max-width: 700px; margin: 0 auto; padding: 20px; }
.compose-view h2 { margin-bottom: 16px; }
#editor { min-height: 200px; }
.quoted-message { margin-top: 16px; border: 1px solid var(--input-border); border-radius: 4px; background: var(--bg-table-header); }
.quoted-message-label { padding: 8px 12px; font-size: 12px; font-weight: 600; color: var(--text-secondary); text-transform: uppercase; border-bottom: 1px solid var(--input-border); }
.quoted-message-content { padding: 12px; max-height: 400px; overflow-y: auto; font-size: 13px; line-height: 1.5; }
.quoted-message-content img { max-width: 100%; height: auto; }

/* Force compose popup to always render in light mode */
[data-theme="dark"] .compose-popup { background: #fff; }
[data-theme="dark"] .compose-popup-body { color: #1a1a1a; }
[data-theme="dark"] .compose-popup-body .form-group label { color: #374151; }
[data-theme="dark"] .compose-popup-body .form-group input,
[data-theme="dark"] .compose-popup-body .form-group textarea,
[data-theme="dark"] .compose-popup-body .form-group select { background: #fff; color: #1a1a1a; border-color: #d1d5db; }
[data-theme="dark"] .compose-popup-body .autocomplete-dropdown { background: #fff; border-color: #d1d5db; }
[data-theme="dark"] .compose-popup-body .autocomplete-item:hover { background: #f0f7ff; }
[data-theme="dark"] .compose-popup-body .quoted-message { background: #f9fafb; border-color: #d1d5db; }
[data-theme="dark"] .compose-popup-body .quoted-message-label { color: #6b7280; border-color: #d1d5db; }
[data-theme="dark"] .compose-popup-body .btn { background: #fff; color: #374151; border-color: #d1d5db; }
[data-theme="dark"] .compose-popup-body .btn:hover { background: #f9fafb; }
[data-theme="dark"] .compose-popup-body .btn-primary { background: #2563eb; color: #fff; border-color: #2563eb; }
[data-theme="dark"] .compose-popup-body .btn-primary:hover { background: #1d4ed8; }

/* Force full-page compose view to light mode too */
[data-theme="dark"] .compose-view { color: #1a1a1a; }
[data-theme="dark"] .compose-view .form-group label { color: #374151; }
[data-theme="dark"] .compose-view .form-group input,
[data-theme="dark"] .compose-view .form-group textarea,
[data-theme="dark"] .compose-view .form-group select { background: #fff; color: #1a1a1a; border-color: #d1d5db; }
[data-theme="dark"] .compose-view .compose-popup { background: #fff; }
[data-theme="dark"] .compose-view .autocomplete-dropdown { background: #fff; border-color: #d1d5db; }
[data-theme="dark"] .compose-view .autocomplete-item:hover { background: #f0f7ff; }
[data-theme="dark"] .compose-view .quoted-message { background: #f9fafb; border-color: #d1d5db; }
[data-theme="dark"] .compose-view .quoted-message-label { color: #6b7280; border-color: #d1d5db; }
[data-theme="dark"] .compose-view .btn { background: #fff; color: #374151; border-color: #d1d5db; }
[data-theme="dark"] .compose-view .btn:hover { background: #f9fafb; }
[data-theme="dark"] .compose-view .btn-primary { background: #2563eb; color: #fff; border-color: #2563eb; }
[data-theme="dark"] .compose-view .btn-primary:hover { background: #1d4ed8; }

/* Move form */
.move-form select { padding: 3px 6px; font-size: 12px; border: 1px solid var(--input-border); border-radius: 4px; background: var(--bg-content); color: var(--text-primary); }

/* Calendar view */
.calendar-view { max-width: 900px; margin: 0 auto; padding: 20px; }
.calendar-nav { display: flex; align-items: center; gap: 12px; margin-bottom: 16px; }
.calendar-nav h2 { flex: 1; text-align: center; font-size: 18px; margin: 0; }

/* Calendar grid */
.calendar-grid { display: grid; grid-template-columns: repeat(7, 1fr); gap: 1px; background: var(--border); border: 1px solid var(--border); border-radius: 4px; overflow: hidden; margin-bottom: 24px; }
.calendar-dow { background: var(--bg-table-header); padding: 8px 4px; text-align: center; font-size: 12px; font-weight: 600; color: var(--text-secondary); text-transform: uppercase; }
.calendar-day { background: var(--bg-content); min-height: 80px; padding: 4px 6px; position: relative; }
.calendar-day.empty { background: var(--calendar-empty); }
.calendar-day.today { background: var(--calendar-today); }
.calendar-day.today .day-number { background: var(--accent); color: #fff; border-radius: 50%; width: 24px; height: 24px; display: inline-flex; align-items: center; justify-content: center; }
.calendar-day.selected { outline: 2px solid var(--accent); outline-offset: -2px; }
.calendar-day.has-events .day-number { font-weight: 600; }
.day-number { display: inline-block; font-size: 13px; color: var(--text-heading); padding: 2px; }
.day-number:hover { text-decoration: none; color: var(--accent); }
.event-dots { display: flex; gap: 3px; margin-top: 4px; flex-wrap: wrap; }
.event-dot { width: 6px; height: 6px; border-radius: 50%; background: var(--accent); }
.event-dot-more { font-size: 10px; color: var(--text-secondary); }

/* Agenda */
.agenda-section { border-top: 1px solid var(--border); padding-top: 16px; }
.agenda-section h3 { font-size: 16px; color: var(--text-heading); margin-bottom: 12px; }
.agenda-list { list-style: none; }
.agenda-item { display: flex; gap: 16px; padding: 10px 0; border-bottom: 1px solid var(--border-light); }
.agenda-time { min-width: 140px; font-size: 13px; color: var(--text-secondary); white-space: nowrap; }
.agenda-details { flex: 1; }
.agenda-subject { font-size: 14px; font-weight: 500; color: var(--text-primary); }
.agenda-subject:hover { color: var(--accent); }
.agenda-location { display: block; font-size: 12px; color: var(--text-secondary); margin-top: 2px; }
.empty-state-sm { padding: 20px; text-align: center; color: var(--text-tertiary); font-size: 13px; }

/* Event detail view */
.event-view { max-width: 900px; margin: 0 auto; padding: 20px; }
.attendees-section { margin-bottom: 16px; padding: 12px 0; border-bottom: 1px solid var(--border); font-size: 13px; }
.attendees-list { list-style: none; margin-top: 6px; padding-left: 0; }
.attendees-list li { padding: 3px 0; }
.attendee-status { font-size: 11px; padding: 1px 6px; border-radius: 10px; margin-left: 6px; }
.attendee-accepted { background: var(--accepted-bg); color: var(--accepted-text); }
.attendee-declined { background: var(--error-bg); color: var(--error); }
.attendee-tentativelyAccepted { background: var(--tentative-bg); color: var(--tentative-text); }

/* Contacts view */
.contacts-view { max-width: 900px; margin: 0 auto; padding: 20px; }
.contacts-view h2 { margin-bottom: 16px; }
.contacts-search { margin-bottom: 16px; }
.contacts-search input { width: 100%; padding: 8px 12px; border: 1px solid var(--input-border); border-radius: 4px; font-size: 14px; background: var(--bg-content); color: var(--text-primary); }
.contacts-search input:focus { outline: none; border-color: var(--accent); box-shadow: 0 0 0 2px rgba(37,99,235,0.15); }
.contacts-table { width: 100%; border-collapse: collapse; }
.contacts-table thead { background: var(--bg-table-header); }
.contacts-table th { text-align: left; padding: 8px 12px; font-size: 12px; color: var(--text-secondary); font-weight: 500; text-transform: uppercase; border-bottom: 1px solid var(--border); }
.contacts-table td { padding: 10px 12px; border-bottom: 1px solid var(--border-light); font-size: 13px; }
.contacts-table tbody tr:hover { background: var(--bg-hover); }

/* Autocomplete */
.autocomplete-wrapper { position: relative; }
.autocomplete-wrapper input { width: 100%; }
.autocomplete-dropdown { display: none; position: absolute; top: 100%; left: 0; right: 0; background: var(--bg-content); border: 1px solid var(--input-border); border-top: none; border-radius: 0 0 4px 4px; max-height: 200px; overflow-y: auto; z-index: 100; box-shadow: 0 4px 12px rgba(0,0,0,0.1); }
.autocomplete-item { padding: 8px 12px; font-size: 13px; cursor: pointer; }
.autocomplete-item:hover { background: var(--bg-hover); }

/* Compose popup */
.compose-popup { position: fixed; bottom: 0; right: 80px; width: 500px; max-height: 600px; background: var(--bg-content); border-radius: 8px 8px 0 0; box-shadow: 0 -2px 16px rgba(0,0,0,0.25); z-index: 1000; display: flex; flex-direction: column; overflow: hidden; }
.compose-popup-header { display: flex; align-items: center; justify-content: space-between; padding: 8px 12px; background: var(--sidebar-bg); color: #fff; cursor: pointer; flex-shrink: 0; border-radius: 8px 8px 0 0; }
.compose-popup-title { font-size: 14px; font-weight: 600; }
.compose-popup-controls { display: flex; gap: 4px; }
.compose-popup-btn { background: none; border: none; color: #94a3b8; font-size: 18px; cursor: pointer; padding: 0 4px; line-height: 1; }
.compose-popup-btn:hover { color: #fff; }
.compose-popup-body { flex: 1; overflow-y: auto; padding: 12px; }
.compose-popup-body .form-group { margin-bottom: 8px; }
.compose-popup-body .form-group label { font-size: 12px; margin-bottom: 2px; }
.compose-popup-body .form-group input[type="text"] { padding: 6px 8px; font-size: 13px; }
.compose-popup-body .form-actions { margin-top: 8px; }
.compose-popup-body #editor { min-height: 120px; max-height: 200px; overflow-y: auto; height: auto; }
.compose-popup-body .ql-editor { min-height: 100px; }
.compose-popup-body .quoted-message { margin-top: 8px; }
.compose-popup-body .quoted-message-content { max-height: 150px; }
.compose-popup.minimized .compose-popup-body { display: none; }
.compose-popup.minimized { max-height: none; }

/* Full-page compose fallback */
.compose-view .compose-popup { position: static; max-height: none; width: 100%; max-width: 700px; box-shadow: none; border-radius: 0; border: 1px solid var(--border); }
.compose-view .compose-popup-header { border-radius: 0; }
.compose-view .compose-popup-controls { display: none; }
.compose-view .compose-popup-body #editor { min-height: 200px; max-height: none; }
.compose-view .compose-popup-body .ql-editor { min-height: 180px; }

/* Password change view */
.password-view { max-width: 400px; margin: 0 auto; padding: 20px; }

/* Responsive */
@media (max-width: 768px) {
    .layout { flex-direction: column; }
    .sidebar { width: 100%; height: auto; max-height: 50vh; }
    .col-from { width: 120px; }
    .col-date { display: none; }
    .calendar-day { min-height: 50px; }
    .agenda-time { min-width: 100px; }
    .compose-popup { right: 0; left: 0; width: 100%; border-radius: 0; }
    .compose-popup-header { border-radius: 0; }
}
