:root {
    --bg-app: #f1f5f9;
    --bg-panel: #ffffff;
    --bg-sidebar: #0f172a;
    --bg-sidebar-hover: #1e293b;
    --bg-muted: #f8fafc;
    --border: #e2e8f0;
    --border-strong: #cbd5e1;
    --text-main: #0f172a;
    --text-sub: #475569;
    --text-muted: #94a3b8;
    --primary: #4f46e5;
    --primary-hover: #4338ca;
    --primary-soft: #eef2ff;
    --success: #10b981;
    --success-soft: #ecfdf5;
    --warning: #f59e0b;
    --warning-soft: #fffbeb;
    --danger: #ef4444;
    --danger-soft: #fef2f2;
    --info: #0ea5e9;
    --info-soft: #f0f9ff;
    --radius: 10px;
    --shadow-sm: 0 1px 2px rgba(15, 23, 42, 0.06);
    --shadow-md: 0 8px 24px rgba(15, 23, 42, 0.08);
}

* { box-sizing: border-box; }
html, body {
    margin: 0;
    padding: 0;
    font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "PingFang SC", "Microsoft YaHei", sans-serif;
    font-size: 14px;
    color: var(--text-main);
    background: var(--bg-app);
    -webkit-font-smoothing: antialiased;
}
a { color: var(--primary); text-decoration: none; }
a:hover { text-decoration: underline; }

/* Buttons & Inputs */
.btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 6px;
    padding: 7px 14px;
    border-radius: 8px;
    border: 1px solid transparent;
    font-size: 13px;
    font-weight: 500;
    cursor: pointer;
    transition: all 0.15s ease;
    white-space: nowrap;
    background: #fff;
    color: var(--text-main);
    border-color: var(--border-strong);
}
.btn:hover { background: var(--bg-muted); text-decoration: none; }
.btn:disabled { opacity: 0.55; cursor: not-allowed; }
.btn-primary { background: var(--primary); color: #fff; border-color: var(--primary); }
.btn-primary:hover { background: var(--primary-hover); }
.btn-success { background: var(--success); color: #fff; border-color: var(--success); }
.btn-success:hover { background: #059669; }
.btn-danger { background: var(--danger); color: #fff; border-color: var(--danger); }
.btn-danger:hover { background: #dc2626; }
.btn-warning { background: var(--warning); color: #fff; border-color: var(--warning); }
.btn-secondary { background: #fff; color: var(--text-sub); border-color: var(--border-strong); }
.btn-ghost { background: transparent; border-color: transparent; color: var(--text-sub); }
.btn-ghost:hover { background: var(--bg-muted); }
.btn-sm { padding: 4px 10px; font-size: 12px; border-radius: 6px; }
.btn-lg { padding: 10px 20px; font-size: 15px; }

input[type="text"], input[type="password"], input[type="number"], input[type="search"], select, textarea {
    width: 100%;
    padding: 8px 11px;
    border: 1px solid var(--border-strong);
    border-radius: 8px;
    font-size: 13px;
    color: var(--text-main);
    background: #fff;
    outline: none;
    transition: border-color 0.15s ease, box-shadow 0.15s ease;
    font-family: inherit;
}
input:focus, select:focus, textarea:focus {
    border-color: var(--primary);
    box-shadow: 0 0 0 3px rgba(79, 70, 229, 0.12);
}
textarea.mono, input.mono, .mono {
    font-family: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, monospace;
}

/* Badges */
.badge {
    display: inline-flex;
    align-items: center;
    gap: 4px;
    padding: 2px 8px;
    border-radius: 999px;
    font-size: 11px;
    font-weight: 600;
    line-height: 1.5;
}
.badge-primary { background: var(--primary-soft); color: var(--primary); }
.badge-success { background: var(--success-soft); color: #047857; }
.badge-warning { background: var(--warning-soft); color: #b45309; }
.badge-danger { background: var(--danger-soft); color: #b91c1c; }
.badge-info { background: var(--info-soft); color: #0369a1; }
.badge-muted { background: #f1f5f9; color: var(--text-sub); }

/* Alerts */
.alert { padding: 12px 16px; border-radius: 8px; margin-bottom: 16px; font-size: 13px; }
.alert-error { background: var(--danger-soft); color: #991b1b; border: 1px solid #fecaca; }
.alert-success { background: var(--success-soft); color: #065f46; border: 1px solid #a7f3d0; }
.alert-info { background: var(--info-soft); color: #075985; border: 1px solid #bae6fd; }

/* Install Page */
.install-body {
    min-height: 100vh;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 32px 16px;
    background: linear-gradient(135deg, #eef2ff 0%, #f8fafc 100%);
}
.install-card {
    width: 100%;
    max-width: 780px;
    background: #fff;
    border-radius: 14px;
    box-shadow: var(--shadow-md);
    border: 1px solid var(--border);
    padding: 28px 32px;
}
.install-header {
    display: flex;
    align-items: center;
    gap: 14px;
    padding-bottom: 18px;
    border-bottom: 1px solid var(--border);
    margin-bottom: 20px;
}
.install-header h1 { margin: 0 0 4px; font-size: 19px; }
.install-header p { margin: 0; color: var(--text-sub); font-size: 13px; }
.brand-badge {
    width: 42px;
    height: 42px;
    border-radius: 10px;
    background: linear-gradient(135deg, #6366f1, #4f46e5);
    color: #fff;
    display: flex;
    align-items: center;
    justify-content: center;
    font-weight: 800;
    font-size: 18px;
    flex-shrink: 0;
}
.brand-badge.sm { width: 32px; height: 32px; font-size: 14px; border-radius: 8px; }
.form-section-title {
    font-weight: 700;
    font-size: 13px;
    color: var(--primary);
    margin: 18px 0 10px;
    text-transform: uppercase;
    letter-spacing: 0.03em;
}
.form-grid-2 {
    display: grid;
    grid-template-columns: repeat(2, minmax(0, 1fr));
    gap: 12px 16px;
}
.install-form label {
    display: flex;
    flex-direction: column;
    gap: 5px;
    font-size: 12px;
    font-weight: 600;
    color: var(--text-sub);
}
.install-actions { margin-top: 24px; display: flex; justify-content: flex-end; }

/* ========================
   Web Client (Foxmail Style)
   ======================== */
.mail-app {
    height: 100vh;
    display: flex;
    flex-direction: column;
    overflow: hidden;
}
.topbar {
    min-height: 56px;
    height: auto;
    background: #ffffff;
    border-bottom: 1px solid var(--border);
    padding: 8px 16px;
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 12px;
    flex-shrink: 0;
    flex-wrap: wrap;
}
.topbar-left, .topbar-right {
    display: flex;
    align-items: center;
    gap: 10px;
}
.topbar-title {
    font-size: 15px;
    font-weight: 700;
    margin-right: 6px;
}
.group-pill-bar {
    display: flex;
    align-items: center;
    gap: 8px;
    padding: 7px 16px;
    background: #fafbff;
    border-bottom: 1px solid var(--border);
    overflow-x: auto;
    flex-shrink: 0;
}
.group-chip {
    display: inline-flex;
    align-items: center;
    gap: 8px;
    padding: 4px 10px;
    background: #fff;
    border: 1px solid var(--border);
    border-radius: 8px;
    font-size: 12px;
    cursor: pointer;
    white-space: nowrap;
    transition: all 0.15s;
}
.group-chip:hover { border-color: var(--primary); }
.group-chip.active { background: var(--primary-soft); border-color: var(--primary); color: var(--primary); }
.group-chip .mini-bar {
    width: 44px;
    height: 5px;
    background: #e2e8f0;
    border-radius: 99px;
    overflow: hidden;
}
.group-chip .mini-bar > i {
    display: block;
    height: 100%;
    background: var(--primary);
}

.mail-workspace {
    flex: 1;
    display: grid;
    grid-template-columns: 315px 360px 1fr;
    min-height: 0;
    overflow: hidden;
}
.pane {
    display: flex;
    flex-direction: column;
    min-height: 0;
    background: #fff;
    border-right: 1px solid var(--border);
}
.pane:last-child { border-right: none; }
.pane-header {
    padding: 10px 14px;
    border-bottom: 1px solid var(--border);
    background: var(--bg-muted);
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 8px;
    flex-shrink: 0;
}
.pane-scroll {
    flex: 1;
    overflow-y: auto;
    min-height: 0;
}

/* Account items */
.pane input[type="checkbox"] {
    width: 14px;
    height: 14px;
    min-width: 14px;
    margin: 0;
    flex-shrink: 0;
    accent-color: var(--primary);
    cursor: pointer;
}
.acc-item {
    padding: 9px 12px;
    border-bottom: 1px solid #f1f5f9;
    cursor: pointer;
    transition: background 0.12s;
}
.acc-item:hover { background: #f8fafc; }
.acc-item.active { background: var(--primary-soft); border-left: 3px solid var(--primary); padding-left: 9px; }
.acc-item.in-folder { padding-left: 26px; }
.acc-item.in-folder.active { padding-left: 23px; }
.acc-group-folder {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 6px;
    padding: 8px 12px;
    background: #f8fafc;
    border-bottom: 1px solid var(--border);
    cursor: pointer;
    user-select: none;
    transition: background 0.12s;
    font-size: 12px;
}
.acc-group-folder:hover { background: #eef2ff; }
.acc-group-folder.expanded { background: #f1f5f9; }
.acc-group-left {
    display: flex;
    align-items: center;
    gap: 6px;
    flex: 1;
    min-width: 0;
    overflow: hidden;
}
.acc-chevron {
    width: 10px;
    flex-shrink: 0;
    font-size: 10px;
    line-height: 1;
    color: var(--text-sub);
    text-align: center;
}
.acc-folder-ico {
    flex-shrink: 0;
    font-size: 12px;
    line-height: 1;
}
.acc-group-title {
    font-weight: 600;
    font-size: 12.5px;
    line-height: 1.3;
    color: var(--text-main);
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}
.acc-group-actions {
    display: flex;
    align-items: center;
    gap: 2px;
    flex-shrink: 0;
}
.acc-icon-btn {
    width: 22px;
    height: 22px;
    padding: 0 !important;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    font-size: 12px;
    line-height: 1;
    flex-shrink: 0;
    border-radius: 5px;
}
.acc-email {
    font-weight: 600;
    font-size: 12.5px;
    line-height: 1.3;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}
.acc-group-pager {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 5px 12px 5px 24px;
    background: #fafbfc;
    border-bottom: 1px solid var(--border);
    font-size: 11px;
    color: var(--text-sub);
}
.ctx-menu {
    position: fixed;
    z-index: 9999;
    min-width: 196px;
    max-width: 260px;
    background: #ffffff;
    border: 1px solid var(--border);
    border-radius: 8px;
    box-shadow: 0 12px 28px rgba(15, 23, 42, 0.16);
    padding: 5px 0;
    font-size: 12.5px;
}
.ctx-label {
    padding: 4px 12px;
    font-size: 11px;
    font-weight: 700;
    color: var(--text-muted);
}
.ctx-item {
    display: flex;
    align-items: center;
    gap: 8px;
    width: 100%;
    padding: 7px 12px;
    border: none;
    background: transparent;
    text-align: left;
    font-size: 12.5px;
    color: var(--text-main);
    cursor: pointer;
}
.ctx-item:hover { background: var(--primary-soft); color: var(--primary); }
.ctx-item.danger { color: var(--danger); }
.ctx-item.danger:hover { background: #fef2f2; color: #b91c1c; }
.ctx-sep { height: 1px; background: var(--border); margin: 4px 0; }
.acc-item-top {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 6px;
    margin-bottom: 4px;
}
.acc-email {
    font-weight: 600;
    font-size: 13px;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}
.acc-meta {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 6px;
    font-size: 11px;
    color: var(--text-sub);
}
.dot {
    width: 8px;
    height: 8px;
    border-radius: 50%;
    display: inline-block;
    flex-shrink: 0;
}
.dot-idle { background: #cbd5e1; }
.dot-loading { background: var(--warning); animation: pulse 1s infinite; }
.dot-ok { background: var(--success); }
.dot-err { background: var(--danger); }
@keyframes pulse { 0%, 100% { opacity: 1; } 50% { opacity: 0.35; } }

/* Spinners & Fetch Progress */
.spinner {
    width: 13px;
    height: 13px;
    border: 2px solid rgba(79, 70, 229, 0.22);
    border-top-color: var(--primary);
    border-radius: 50%;
    display: inline-block;
    animation: spin 0.7s linear infinite;
    flex-shrink: 0;
}
.spinner.spinner-white {
    border-color: rgba(255, 255, 255, 0.35);
    border-top-color: #ffffff;
}
.spinner.spinner-lg {
    width: 30px;
    height: 30px;
    border-width: 3px;
}
@keyframes spin { to { transform: rotate(360deg); } }

.fetch-progress-track {
    height: 3px;
    width: 100%;
    background: #e2e8f0;
    overflow: hidden;
    flex-shrink: 0;
    display: none;
}
.fetch-progress-track.active { display: block; }
.fetch-progress-fill {
    height: 100%;
    width: 0%;
    background: linear-gradient(90deg, var(--primary), var(--success));
    transition: width 0.25s ease;
}
.fetch-progress-banner {
    padding: 8px 14px;
    background: #eef2ff;
    color: var(--primary);
    font-size: 12px;
    font-weight: 600;
    display: flex;
    align-items: center;
    gap: 8px;
    border-bottom: 1px solid #c7d2fe;
}

/* Mail list items */
.mail-item {
    padding: 12px 14px;
    border-bottom: 1px solid #f1f5f9;
    cursor: pointer;
    transition: background 0.12s;
}
.mail-item:hover { background: #f8fafc; }
.mail-item.active { background: #eef2ff; }
.mail-item.unread .mail-subject { font-weight: 700; color: var(--text-main); }
.mail-item-row1 {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 8px;
    margin-bottom: 4px;
}
.mail-from {
    font-size: 12px;
    font-weight: 600;
    color: var(--text-sub);
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}
.mail-date { font-size: 11px; color: var(--text-muted); flex-shrink: 0; }
.mail-subject {
    font-size: 13px;
    font-weight: 500;
    margin-bottom: 4px;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}
.mail-preview {
    font-size: 12px;
    color: var(--text-muted);
    display: -webkit-box;
    -webkit-line-clamp: 2;
    -webkit-box-orient: vertical;
    overflow: hidden;
    line-height: 1.4;
}
.code-pill {
    display: inline-flex;
    align-items: center;
    gap: 4px;
    background: #ecfdf5;
    color: #047857;
    border: 1px solid #a7f3d0;
    padding: 1px 7px;
    border-radius: 6px;
    font-size: 11px;
    font-weight: 700;
    font-family: ui-monospace, monospace;
    cursor: pointer;
}
.code-pill:hover { background: #d1fae5; }

/* Reader Pane */
.reader-empty {
    height: 100%;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    color: var(--text-muted);
    padding: 24px;
    text-align: center;
}
.reader-head {
    padding: 16px 22px;
    border-bottom: 1px solid var(--border);
    background: #fff;
}
.reader-subject { font-size: 18px; font-weight: 700; margin: 0 0 10px; line-height: 1.35; }
.reader-meta {
    display: flex;
    flex-wrap: wrap;
    gap: 8px 18px;
    font-size: 12px;
    color: var(--text-sub);
}
.otp-banner {
    margin: 12px 22px 0;
    padding: 12px 16px;
    background: linear-gradient(90deg, #ecfdf5, #f0f9ff);
    border: 1px solid #a7f3d0;
    border-radius: 10px;
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 12px;
}
.otp-code-big {
    font-size: 22px;
    font-weight: 800;
    letter-spacing: 2px;
    color: #047857;
    font-family: ui-monospace, monospace;
}
.reader-body-wrap {
    flex: 1;
    min-height: 0;
    display: flex;
    flex-direction: column;
    padding: 14px 22px 22px;
}
.reader-iframe {
    width: 100%;
    flex: 1;
    min-height: 360px;
    border: 1px solid var(--border);
    border-radius: 8px;
    background: #fff;
}
.reader-text {
    white-space: pre-wrap;
    word-break: break-word;
    font-size: 13px;
    line-height: 1.65;
    padding: 14px;
    background: var(--bg-muted);
    border: 1px solid var(--border);
    border-radius: 8px;
    flex: 1;
    overflow-y: auto;
}

/* ========================
   Admin Console
   ======================== */
.admin-layout {
    min-height: 100vh;
    display: grid;
    grid-template-columns: 230px 1fr;
}
.admin-sidebar {
    background: var(--bg-sidebar);
    color: #e2e8f0;
    display: flex;
    flex-direction: column;
    padding: 18px 12px;
}
.admin-brand {
    display: flex;
    align-items: center;
    gap: 10px;
    padding: 6px 10px 18px;
    border-bottom: 1px solid rgba(255,255,255,0.08);
    margin-bottom: 14px;
}
.admin-nav {
    display: flex;
    flex-direction: column;
    gap: 4px;
    flex: 1;
}
.nav-item {
    display: flex;
    align-items: center;
    gap: 10px;
    padding: 10px 12px;
    border-radius: 8px;
    color: #cbd5e1;
    font-size: 13px;
    font-weight: 500;
    cursor: pointer;
    border: none;
    background: transparent;
    text-align: left;
    width: 100%;
}
.nav-item:hover { background: var(--bg-sidebar-hover); color: #fff; }
.nav-item.active { background: var(--primary); color: #fff; }
.admin-main {
    display: flex;
    flex-direction: column;
    min-width: 0;
}
.admin-topbar {
    height: 58px;
    background: #fff;
    border-bottom: 1px solid var(--border);
    padding: 0 24px;
    display: flex;
    align-items: center;
    justify-content: space-between;
}
.admin-content {
    padding: 22px 24px;
    flex: 1;
}
.stats-grid {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(185px, 1fr));
    gap: 14px;
    margin-bottom: 20px;
}
.stat-card {
    background: #fff;
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 16px 18px;
    box-shadow: var(--shadow-sm);
}
.stat-label { font-size: 12px; color: var(--text-sub); margin-bottom: 6px; }
.stat-val { font-size: 24px; font-weight: 800; color: var(--text-main); }
.stat-sub { font-size: 11px; color: var(--text-muted); margin-top: 4px; }

.card {
    background: #fff;
    border: 1px solid var(--border);
    border-radius: var(--radius);
    box-shadow: var(--shadow-sm);
    margin-bottom: 18px;
    overflow: hidden;
}
.card-header {
    padding: 14px 18px;
    border-bottom: 1px solid var(--border);
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 12px;
    flex-wrap: wrap;
}
.card-title { font-size: 15px; font-weight: 700; margin: 0; }
.card-body { padding: 18px; }

.toolbar {
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    gap: 10px;
    margin-bottom: 14px;
}
.table-wrap { overflow-x: auto; }
table.data-table {
    width: 100%;
    border-collapse: collapse;
    font-size: 13px;
}
table.data-table th, table.data-table td {
    padding: 10px 12px;
    border-bottom: 1px solid var(--border);
    text-align: left;
    vertical-align: middle;
}
table.data-table th {
    background: var(--bg-muted);
    color: var(--text-sub);
    font-weight: 600;
    font-size: 12px;
    white-space: nowrap;
}
table.data-table tr:hover td { background: #fafbff; }

.progress-bar {
    height: 7px;
    background: #e2e8f0;
    border-radius: 99px;
    overflow: hidden;
    min-width: 90px;
}
.progress-bar > span {
    display: block;
    height: 100%;
    background: linear-gradient(90deg, var(--primary), #06b6d4);
}

/* Modal */
.modal-backdrop {
    position: fixed;
    inset: 0;
    background: rgba(15, 23, 42, 0.55);
    display: none;
    align-items: center;
    justify-content: center;
    padding: 20px;
    z-index: 999;
}
.modal-backdrop.open { display: flex; }
.modal-box {
    width: 100%;
    max-width: 680px;
    max-height: 90vh;
    background: #fff;
    border-radius: 12px;
    box-shadow: var(--shadow-md);
    display: flex;
    flex-direction: column;
    overflow: hidden;
}
.modal-head {
    padding: 14px 20px;
    border-bottom: 1px solid var(--border);
    display: flex;
    align-items: center;
    justify-content: space-between;
}
.modal-head h3 { margin: 0; font-size: 16px; }
.modal-body { padding: 18px 20px; overflow-y: auto; }
.modal-foot {
    padding: 12px 20px;
    border-top: 1px solid var(--border);
    display: flex;
    justify-content: flex-end;
    gap: 10px;
    background: var(--bg-muted);
}

/* Toast */
.toast-wrap {
    position: fixed;
    top: 18px;
    right: 18px;
    z-index: 2000;
    display: flex;
    flex-direction: column;
    gap: 8px;
}
.toast {
    padding: 10px 16px;
    border-radius: 8px;
    background: #0f172a;
    color: #fff;
    font-size: 13px;
    box-shadow: var(--shadow-md);
    animation: slideIn 0.2s ease;
}
.toast.success { background: #065f46; }
.toast.error { background: #991b1b; }
@keyframes slideIn { from { opacity: 0; transform: translateY(-8px); } to { opacity: 1; transform: translateY(0); } }

/* Login Card */
.login-wrap {
    min-height: 100vh;
    display: flex;
    align-items: center;
    justify-content: center;
    background: linear-gradient(135deg, #0f172a, #1e293b);
    padding: 20px;
}
.login-card {
    width: 100%;
    max-width: 390px;
    background: #fff;
    border-radius: 14px;
    padding: 28px;
    box-shadow: var(--shadow-md);
}

@media (max-width: 1024px) {
    .mail-workspace { grid-template-columns: 240px 300px 1fr; }
    .admin-layout { grid-template-columns: 190px 1fr; }
}
@media (max-width: 820px) {
    .mail-workspace { grid-template-columns: 1fr; grid-template-rows: 220px 260px 1fr; }
    .admin-layout { grid-template-columns: 1fr; }
}
