/* ==========================================================================
   Gravity Mail – theme
   ========================================================================== */
:root,
[data-bs-theme="light"] {
    --gm-primary: #4f46e5;
    --gm-primary-2: #7c3aed;
    --gm-primary-soft: #eef2ff;
    --gm-bg: #f4f6fb;
    --gm-surface: #ffffff;
    --gm-surface-2: #f8fafc;
    --gm-border: #e7eaf3;
    --gm-text: #111827;
    --gm-muted: #6b7280;
    --gm-hover: #f5f7ff;
    --gm-selected: #eef2ff;
    --gm-unread-bg: #ffffff;
    --gm-read-bg: #fafbfe;
    --gm-shadow: 0 1px 2px rgba(16, 24, 40, .04), 0 4px 16px rgba(16, 24, 40, .06);
    --gm-radius: 18px;

    --bs-primary: var(--gm-primary);
    --bs-primary-rgb: 79, 70, 229;
    --bs-link-color: var(--gm-primary);
    --bs-link-color-rgb: 79, 70, 229;
    --bs-body-font-family: "Inter", system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
    --bs-body-bg: var(--gm-bg);
    --bs-body-color: var(--gm-text);
    --bs-border-color: var(--gm-border);
}

[data-bs-theme="dark"] {
    --gm-primary: #818cf8;
    --gm-primary-2: #a78bfa;
    --gm-primary-soft: rgba(129, 140, 248, .14);
    --gm-bg: #0b1020;
    --gm-surface: #121829;
    --gm-surface-2: #172036;
    --gm-border: #232c45;
    --gm-text: #e5e7eb;
    --gm-muted: #9aa3b8;
    --gm-hover: #1a2340;
    --gm-selected: rgba(129, 140, 248, .16);
    --gm-unread-bg: #151c30;
    --gm-read-bg: #121829;
    --gm-shadow: 0 1px 2px rgba(0, 0, 0, .3), 0 8px 24px rgba(0, 0, 0, .25);

    --bs-primary: var(--gm-primary);
    --bs-primary-rgb: 129, 140, 248;
    --bs-link-color: var(--gm-primary);
    --bs-link-color-rgb: 129, 140, 248;
    --bs-body-bg: var(--gm-bg);
    --bs-body-color: var(--gm-text);
    --bs-border-color: var(--gm-border);
    --bs-secondary-color: var(--gm-muted);
}

html, body { height: 100%; }
body {
    font-family: var(--bs-body-font-family);
    background: var(--gm-bg);
    color: var(--gm-text);
    -webkit-font-smoothing: antialiased;
}

.min-w-0 { min-width: 0; }
.text-body-secondary { color: var(--gm-muted) !important; }

/* Buttons ------------------------------------------------------------------ */
.btn-primary {
    --bs-btn-bg: var(--gm-primary);
    --bs-btn-border-color: var(--gm-primary);
    --bs-btn-hover-bg: #4338ca;
    --bs-btn-hover-border-color: #4338ca;
    --bs-btn-active-bg: #3730a3;
    --bs-btn-active-border-color: #3730a3;
    --bs-btn-disabled-bg: var(--gm-primary);
    --bs-btn-disabled-border-color: var(--gm-primary);
    background-image: linear-gradient(135deg, var(--gm-primary), var(--gm-primary-2));
    border: 0;
    box-shadow: 0 6px 18px -6px rgba(79, 70, 229, .55);
}
[data-bs-theme="dark"] .btn-primary { color: #0b1020; }
.btn-outline-primary {
    --bs-btn-color: var(--gm-primary);
    --bs-btn-border-color: var(--gm-primary);
    --bs-btn-hover-bg: var(--gm-primary);
    --bs-btn-hover-border-color: var(--gm-primary);
    --bs-btn-active-bg: var(--gm-primary);
}
.btn-light { --bs-btn-bg: var(--gm-surface-2); --bs-btn-border-color: var(--gm-border); --bs-btn-hover-bg: var(--gm-hover); --bs-btn-color: var(--gm-text); }

.btn-icon {
    width: 38px; height: 38px;
    display: inline-flex; align-items: center; justify-content: center;
    border-radius: 50%;
    border: 0;
    color: var(--gm-muted);
    background: transparent;
    padding: 0;
    transition: background .15s, color .15s;
}
.btn-icon:hover, .btn-icon:focus-visible { background: var(--gm-hover); color: var(--gm-text); }
.btn-icon.disabled { opacity: .35; pointer-events: none; }

.form-control, .form-select, .input-group-text {
    border-radius: 12px;
    border-color: var(--gm-border);
    background-color: var(--gm-surface);
}
.input-group > .form-control { border-top-right-radius: 0; border-bottom-right-radius: 0; }
.input-group-text { background: var(--gm-surface-2); color: var(--gm-muted); }
.form-control:focus { border-color: var(--gm-primary); box-shadow: 0 0 0 4px var(--gm-primary-soft); }
.form-check-input:checked { background-color: var(--gm-primary); border-color: var(--gm-primary); }
.field-error, .field-validation-error { display: block; color: var(--bs-danger); font-size: .8rem; margin-top: .25rem; }

/* Brand -------------------------------------------------------------------- */
.brand { display: inline-flex; align-items: center; gap: .65rem; text-decoration: none; color: var(--gm-text); }
.brand-mark {
    width: 38px; height: 38px; border-radius: 12px;
    display: inline-flex; align-items: center; justify-content: center;
    background: linear-gradient(135deg, var(--gm-primary), var(--gm-primary-2));
    color: #fff; font-size: 1.15rem;
    box-shadow: 0 8px 20px -8px rgba(79, 70, 229, .8);
}
.brand-text { font-weight: 800; font-size: 1.2rem; letter-spacing: -.02em; }
.brand-text span { color: var(--gm-primary); font-weight: 600; }
.brand-light, .brand-light .brand-text { color: #fff; }
.brand-light .brand-text span { color: #c7d2fe; }
.brand-light .brand-mark { background: rgba(255, 255, 255, .18); backdrop-filter: blur(6px); }

/* App shell ---------------------------------------------------------------- */
.app-shell { display: flex; min-height: 100vh; }

.app-sidebar {
    width: 260px; flex-shrink: 0;
    flex-direction: column;
    padding: 1.25rem 1rem;
    position: sticky; top: 0; height: 100vh;
}
.app-sidebar .brand { padding: 0 .5rem; margin-bottom: 1.5rem; }
.app-sidebar-offcanvas { --bs-offcanvas-width: 280px; background: var(--gm-bg); }

.btn-compose {
    display: flex; align-items: center; gap: .6rem;
    padding: .9rem 1.4rem;
    border-radius: 16px;
    background: var(--gm-surface);
    color: var(--gm-text);
    font-weight: 600;
    box-shadow: var(--gm-shadow);
    border: 1px solid var(--gm-border);
    margin-bottom: 1.25rem;
    transition: transform .15s, box-shadow .15s;
}
.btn-compose i { color: var(--gm-primary); font-size: 1.1rem; }
.btn-compose:hover { transform: translateY(-1px); box-shadow: 0 10px 28px -10px rgba(79, 70, 229, .45); color: var(--gm-text); background: var(--gm-surface); }

.folder-nav { display: flex; flex-direction: column; gap: 2px; }
.folder-link {
    display: flex; align-items: center; gap: .85rem;
    padding: .6rem 1rem;
    border-radius: 12px;
    color: var(--gm-text);
    text-decoration: none;
    font-weight: 500;
    font-size: .95rem;
    transition: background .15s;
}
.folder-link i { font-size: 1.1rem; color: var(--gm-muted); width: 1.25rem; text-align: center; }
.folder-link span:first-of-type { flex-grow: 1; }
.folder-link:hover { background: var(--gm-hover); color: var(--gm-text); }
.folder-link.active { background: var(--gm-primary-soft); color: var(--gm-primary); font-weight: 700; }
.folder-link.active i { color: var(--gm-primary); }
.folder-badge { background: var(--gm-primary); color: #fff; font-size: .72rem; font-weight: 700; }
[data-bs-theme="dark"] .folder-badge { color: #0b1020; }
.folder-count { font-size: .78rem; color: var(--gm-muted); }
.folder-section { font-size: .72rem; text-transform: uppercase; letter-spacing: .08em; color: var(--gm-muted); padding: 1.25rem 1rem .4rem; font-weight: 600; }
.sidebar-footer { margin-top: auto; padding: 1rem; font-size: .78rem; color: var(--gm-muted); }

.app-main { flex: 1; min-width: 0; display: flex; flex-direction: column; }

.app-topbar {
    display: flex; align-items: center; gap: .75rem;
    padding: .9rem 1.5rem .5rem;
    position: sticky; top: 0; z-index: 1020;
    background: color-mix(in srgb, var(--gm-bg) 85%, transparent);
    backdrop-filter: blur(10px);
}
.topbar-search { position: relative; flex: 1; max-width: 720px; }
.topbar-search .bi-search { position: absolute; left: 1.1rem; top: 50%; transform: translateY(-50%); color: var(--gm-muted); }
.topbar-search .form-control {
    padding: .7rem 1rem .7rem 2.8rem;
    border-radius: 999px;
    border: 1px solid transparent;
    background: var(--gm-surface);
    box-shadow: var(--gm-shadow);
}
.topbar-search .form-control:focus { border-color: var(--gm-primary); }

.user-chip { display: inline-flex; align-items: center; gap: .5rem; border-radius: 999px; padding: .25rem .75rem .25rem .25rem; border: 1px solid var(--gm-border); background: var(--gm-surface); color: var(--gm-text); font-weight: 500; font-size: .9rem; }
.user-chip:hover { background: var(--gm-hover); }
.user-menu { border-radius: 14px; min-width: 240px; background: var(--gm-surface); }

[data-bs-theme="light"] .theme-icon-dark { display: none; }
[data-bs-theme="dark"] .theme-icon-light { display: none; }

.app-content { padding: .5rem 1.5rem 1.5rem; flex: 1; }

/* Avatars ------------------------------------------------------------------ */
.avatar {
    --hue: 240;
    width: 38px; height: 38px; flex-shrink: 0;
    border-radius: 50%;
    display: inline-flex; align-items: center; justify-content: center;
    font-weight: 700; font-size: .8rem; letter-spacing: .02em;
    color: hsl(var(--hue) 55% 32%);
    background: hsl(var(--hue) 80% 90%);
}
[data-bs-theme="dark"] .avatar { color: hsl(var(--hue) 80% 85%); background: hsl(var(--hue) 35% 25%); }
.avatar-xs { width: 22px; height: 22px; font-size: .6rem; }
.avatar-sm { width: 32px; height: 32px; font-size: .72rem; }
.avatar-lg { width: 48px; height: 48px; font-size: 1rem; }

/* Mail panel --------------------------------------------------------------- */
.mail-panel, .page-card {
    background: var(--gm-surface);
    border-radius: var(--gm-radius);
    box-shadow: var(--gm-shadow);
    border: 1px solid var(--gm-border);
    overflow: hidden;
}

.mail-toolbar {
    display: flex; align-items: center; gap: .5rem;
    padding: .6rem 1rem;
    border-bottom: 1px solid var(--gm-border);
    min-height: 58px;
}
.bulk-actions { opacity: 0; visibility: hidden; transform: translateX(-6px); transition: all .2s; }
.bulk-actions.show { opacity: 1; visibility: visible; transform: none; }

.mail-header { display: flex; align-items: center; gap: 1rem; padding: 1rem 1.25rem .5rem; flex-wrap: wrap; }
.mail-header h1 { font-weight: 700; letter-spacing: -.01em; }
.search-pill { display: inline-flex; align-items: center; gap: .35rem; background: var(--gm-primary-soft); color: var(--gm-primary); border-radius: 999px; padding: .2rem .4rem .2rem .8rem; font-size: .82rem; }
.search-pill a { color: inherit; display: inline-flex; }

.mail-list { list-style: none; margin: 0; padding: 0 0 .5rem; }
.mail-row {
    display: flex; align-items: center; gap: .35rem;
    padding: 0 1rem 0 1.25rem;
    border-bottom: 1px solid var(--gm-border);
    background: var(--gm-read-bg);
    position: relative;
    transition: background .12s, box-shadow .12s;
}
.mail-row:last-child { border-bottom: 0; }
.mail-row:hover { background: var(--gm-hover); box-shadow: inset 3px 0 0 var(--gm-primary); z-index: 1; }
.mail-row.selected { background: var(--gm-selected); }
.mail-row.unread { background: var(--gm-unread-bg); }
.mail-row.unread .mail-from, .mail-row.unread .mail-subject, .mail-row.unread .mail-date { font-weight: 700; color: var(--gm-text); }
.mail-row.unread::before {
    content: ""; position: absolute; left: 6px; top: 50%; width: 6px; height: 6px; border-radius: 50%;
    background: var(--gm-primary); transform: translateY(-50%);
}

.mail-link {
    flex: 1; min-width: 0;
    display: flex; align-items: center; gap: .9rem;
    padding: .75rem .25rem;
    color: var(--gm-text);
    text-decoration: none;
}
.mail-link:hover { color: var(--gm-text); }
.mail-from { width: 200px; flex-shrink: 0; font-weight: 500; color: var(--gm-text); }
.mail-summary { flex: 1; min-width: 0; }
.mail-subject { font-weight: 500; }
.mail-snippet { color: var(--gm-muted); }
.mail-date { flex-shrink: 0; font-size: .82rem; color: var(--gm-muted); min-width: 64px; text-align: right; }

.star-btn { border: 0; background: transparent; color: var(--gm-muted); padding: .25rem .4rem; line-height: 1; }
.star-btn:hover { color: #f59e0b; }
.star-btn.starred, .star-btn.starred:hover { color: #f59e0b; }
.btn-icon.star-btn { padding: 0; }

.empty-state { text-align: center; padding: 4.5rem 1.5rem 5rem; }
.empty-icon {
    width: 84px; height: 84px; border-radius: 26px;
    display: flex; align-items: center; justify-content: center;
    background: var(--gm-primary-soft); color: var(--gm-primary);
    font-size: 2.2rem; margin: 0 auto 1.25rem;
}

/* Read view ---------------------------------------------------------------- */
.read-pane { padding: 1.5rem 2rem 2rem; }
.read-subject { font-size: 1.45rem; font-weight: 700; letter-spacing: -.015em; margin-bottom: 1.25rem; display: flex; align-items: center; gap: .75rem; flex-wrap: wrap; }
.folder-tag { font-size: .7rem; font-weight: 600; text-transform: uppercase; letter-spacing: .06em; padding: .25rem .6rem; border-radius: 6px; background: var(--gm-surface-2); color: var(--gm-muted); border: 1px solid var(--gm-border); }
.read-meta { display: flex; align-items: flex-start; gap: 1rem; padding-bottom: 1.25rem; border-bottom: 1px solid var(--gm-border); margin-bottom: 1.25rem; }
.read-body { width: 100%; min-height: 160px; border: 0; background: #fff; border-radius: 12px; display: block; }
[data-bs-theme="dark"] .read-body { padding: 1rem; }

.attachments { margin-top: 1.5rem; padding-top: 1.25rem; border-top: 1px dashed var(--gm-border); }
.attachment-chip {
    display: inline-flex; align-items: center; gap: .5rem;
    max-width: 280px;
    padding: .5rem .8rem;
    border-radius: 12px;
    border: 1px solid var(--gm-border);
    background: var(--gm-surface-2);
    color: var(--gm-text);
    text-decoration: none;
    font-size: .88rem;
    transition: border-color .15s, background .15s;
}
.attachment-chip i { color: var(--gm-primary); }
a.attachment-chip:hover { border-color: var(--gm-primary); background: var(--gm-primary-soft); color: var(--gm-text); }
.attachment-chip .size { color: var(--gm-muted); font-size: .78rem; white-space: nowrap; }
.attachment-chip .btn-remove { border: 0; background: none; color: var(--gm-muted); padding: 0; line-height: 1; }
.attachment-chip .btn-remove:hover { color: var(--bs-danger); }

.read-actions { display: flex; gap: .6rem; flex-wrap: wrap; margin-top: 2rem; }

/* Compose ------------------------------------------------------------------ */
.compose-panel { max-width: 980px; }
.compose-header { display: flex; align-items: center; justify-content: space-between; padding: 1rem 1.25rem; border-bottom: 1px solid var(--gm-border); background: var(--gm-surface-2); }
.compose-field { display: flex; align-items: center; gap: .75rem; padding: .35rem 1.25rem; border-bottom: 1px solid var(--gm-border); }
.compose-field > label { width: 64px; flex-shrink: 0; font-size: .88rem; color: var(--gm-muted); margin: 0; }
.compose-field .form-control { border: 0; box-shadow: none; padding: .55rem 0; background: transparent; border-radius: 0; }
.compose-field .form-control:focus { box-shadow: none; }
.compose-panel .field-error { padding: 0 1.25rem 0 calc(64px + 2rem); }
.from-pill { display: inline-flex; align-items: center; gap: .5rem; padding: .3rem .75rem .3rem .3rem; margin: .25rem 0; border-radius: 999px; background: var(--gm-primary-soft); font-size: .88rem; font-weight: 500; }

.editor-wrap .ql-toolbar.ql-snow { border: 0; border-bottom: 1px solid var(--gm-border); padding: .6rem 1rem; background: var(--gm-surface); }
.editor-wrap .ql-container.ql-snow { border: 0; font-family: var(--bs-body-font-family); font-size: .98rem; }
.editor-wrap .ql-editor { min-height: 320px; padding: 1.25rem; line-height: 1.65; }
.editor-wrap .ql-editor.ql-blank::before { color: var(--gm-muted); font-style: normal; left: 1.25rem; }
.editor-wrap .ql-editor blockquote { border-left: 3px solid var(--gm-border); color: var(--gm-muted); }
[data-bs-theme="dark"] .ql-snow .ql-stroke { stroke: var(--gm-muted); }
[data-bs-theme="dark"] .ql-snow .ql-fill { fill: var(--gm-muted); }
[data-bs-theme="dark"] .ql-snow .ql-picker { color: var(--gm-muted); }
[data-bs-theme="dark"] .ql-snow .ql-picker-options { background: var(--gm-surface-2); }

.compose-attachments { display: flex; flex-wrap: wrap; gap: .5rem; padding: 0 1.25rem; }
.compose-attachments:not(:empty) { padding-top: .75rem; }
.compose-footer { display: flex; align-items: center; gap: .5rem; padding: 1rem 1.25rem; border-top: 1px solid var(--gm-border); margin-top: .75rem; }
.btn-send { border-radius: 999px; padding: .6rem 1.6rem; font-weight: 600; display: inline-flex; align-items: center; gap: .5rem; }

/* Page cards (admin, settings) --------------------------------------------- */
.page-card { padding: 1.5rem; }
.page-card.narrow { max-width: 560px; }
.page-card-header { display: flex; align-items: center; gap: 1rem; margin-bottom: 1.5rem; }
.icon-tile { width: 46px; height: 46px; border-radius: 14px; display: flex; align-items: center; justify-content: center; background: var(--gm-primary-soft); color: var(--gm-primary); font-size: 1.3rem; flex-shrink: 0; }

.user-table { --bs-table-bg: transparent; }
.user-table thead th { font-size: .75rem; text-transform: uppercase; letter-spacing: .06em; color: var(--gm-muted); font-weight: 600; border-bottom-color: var(--gm-border); }
.user-table td { border-bottom-color: var(--gm-border); padding-top: .85rem; padding-bottom: .85rem; }
.badge-soft-primary { background: var(--gm-primary-soft); color: var(--gm-primary); }
.badge-soft-secondary { background: var(--gm-surface-2); color: var(--gm-muted); border: 1px solid var(--gm-border); }
.status-dot { display: inline-block; width: 8px; height: 8px; border-radius: 50%; margin-right: .45rem; }
.status-dot.on { background: #10b981; box-shadow: 0 0 0 3px rgba(16, 185, 129, .18); }
.status-dot.off { background: #ef4444; box-shadow: 0 0 0 3px rgba(239, 68, 68, .18); }

/* Toasts ------------------------------------------------------------------- */
.app-toast { border-radius: 14px; box-shadow: 0 12px 32px -8px rgba(16, 24, 40, .35); color: #fff; background: #1f2937; }
.app-toast .btn-close { filter: invert(1) grayscale(1) brightness(2); }
.app-toast-success .bi { color: #34d399; }
.app-toast-danger .bi { color: #f87171; }
.app-toast-warning .bi { color: #fbbf24; }
.app-toast-info .bi { color: #93c5fd; }

/* Auth --------------------------------------------------------------------- */
.auth-body { background: var(--gm-bg); }
.auth-shell { display: flex; min-height: 100vh; }
.auth-hero {
    flex: 1.1;
    position: relative; overflow: hidden;
    align-items: center;
    padding: 4rem;
    color: #fff;
    background: radial-gradient(1200px 600px at 10% 10%, #6366f1 0%, transparent 60%),
                radial-gradient(900px 700px at 90% 90%, #a855f7 0%, transparent 55%),
                linear-gradient(135deg, #312e81, #4c1d95);
}
.auth-hero-inner { position: relative; z-index: 2; max-width: 520px; }
.auth-features { list-style: none; padding: 0; margin: 0; display: grid; gap: .9rem; }
.auth-features li { display: flex; align-items: center; gap: .75rem; font-weight: 500; }
.auth-features i { width: 36px; height: 36px; border-radius: 10px; display: inline-flex; align-items: center; justify-content: center; background: rgba(255, 255, 255, .14); }
.auth-orb { position: absolute; border-radius: 50%; filter: blur(10px); opacity: .5; z-index: 1; animation: float 12s ease-in-out infinite; }
.orb-1 { width: 340px; height: 340px; background: rgba(255, 255, 255, .12); top: -80px; right: -60px; }
.orb-2 { width: 220px; height: 220px; background: rgba(236, 72, 153, .35); bottom: 60px; left: 55%; animation-delay: -6s; }
@keyframes float { 50% { transform: translateY(24px) scale(1.04); } }
@media (prefers-reduced-motion: reduce) { .auth-orb { animation: none; } }

.auth-form-side { flex: 1; display: flex; flex-direction: column; align-items: center; justify-content: center; padding: 2rem 1rem; }
.auth-card { width: 100%; max-width: 420px; background: var(--gm-surface); border: 1px solid var(--gm-border); border-radius: 24px; padding: 2.5rem; box-shadow: var(--gm-shadow); }
.auth-card .form-floating > .form-control { border-radius: 12px; }
.password-toggle { position: absolute; right: .5rem; top: 10px; z-index: 5; }

/* Responsive ------------------------------------------------------------------ */
@media (max-width: 991.98px) {
    .app-topbar { padding: .75rem 1rem .5rem; }
    .app-content { padding: .5rem .75rem 1rem; }
    .mail-from { width: 140px; }
    .read-pane { padding: 1.25rem; }
}
@media (max-width: 767.98px) {
    .mail-link { flex-wrap: wrap; gap: .1rem .5rem; padding: .7rem .1rem; }
    .mail-from { width: auto; flex: 1; }
    .mail-summary { order: 3; flex-basis: 100%; }
    .mail-snippet { display: none; }
    .auth-card { padding: 1.75rem; }
    .page-card { padding: 1.1rem; }
    .compose-panel .field-error { padding-left: 1.25rem; }
}
