/* ============================================================
   MailFlow — Custom Styles
   ============================================================ */

:root {
    --sidebar-bg:       #0d1b2a;
    --sidebar-border:   rgba(255,255,255,0.06);
    --sidebar-link:     rgba(255,255,255,0.55);
    --sidebar-link-hover: rgba(255,255,255,0.9);
    --sidebar-link-active-bg: rgba(99,172,255,0.12);
    --sidebar-link-active: #63acff;
    --sidebar-width:    260px;
    --sidebar-label:    rgba(255,255,255,0.28);

    --topbar-bg:        #ffffff;
    --topbar-border:    #e8edf2;
    --topbar-height:    64px;

    --page-bg:          #f0f4f8;
    --card-bg:          #ffffff;
    --card-border:      #e8edf2;
    --card-radius:      12px;

    --primary:          #2563eb;
    --primary-soft:     #eff6ff;
    --danger:           #dc2626;
    --danger-soft:      #fef2f2;
    --success:          #16a34a;
    --success-soft:     #f0fdf4;
    --warning:          #d97706;
    --warning-soft:     #fffbeb;
    --info:             #0891b2;

    --text-primary:     #0f172a;
    --text-secondary:   #64748b;
    --text-muted:       #94a3b8;

    --font-base:        'DM Sans', sans-serif;
    --font-mono:        'DM Mono', monospace;

    --transition:       0.2s ease;
}

/* ── Base ─────────────────────────────────────────────────── */
*, *::before, *::after { box-sizing: border-box; }

body {
    font-family: var(--font-base);
    background:  var(--page-bg);
    color:       var(--text-primary);
    margin:      0;
    min-height:  100vh;
    display:     flex;
}

a { text-decoration: none; color: inherit; }
code, pre { font-family: var(--font-mono); }

/* ── Sidebar ──────────────────────────────────────────────── */
.sidebar {
    width:       var(--sidebar-width);
    min-height:  100vh;
    background:  var(--sidebar-bg);
    display:     flex;
    flex-direction: column;
    position:    fixed;
    top:         0;
    left:        0;
    z-index:     1000;
    transition:  transform var(--transition);
    overflow-y:  auto;
    overflow-x:  hidden;
}

.sidebar-header {
    display:       flex;
    align-items:   center;
    justify-content: space-between;
    padding:       1.5rem 1.25rem 1rem;
    border-bottom: 1px solid var(--sidebar-border);
    flex-shrink:   0;
}

.sidebar-logo {
    display:     flex;
    align-items: center;
    gap:         0.6rem;
}

.logo-icon {
    width:            36px;
    height:           36px;
    background:       linear-gradient(135deg, #2563eb 0%, #3b82f6 100%);
    border-radius:    9px;
    display:          flex;
    align-items:      center;
    justify-content:  center;
    color:            #fff;
    font-size:        1rem;
    flex-shrink:      0;
    box-shadow:       0 4px 12px rgba(37,99,235,0.35);
}

.logo-text {
    font-weight:   700;
    font-size:     1.05rem;
    color:         #ffffff;
    letter-spacing: -0.01em;
}

.sidebar-section-label {
    font-size:      0.65rem;
    font-weight:    600;
    letter-spacing: 0.09em;
    text-transform: uppercase;
    color:          var(--sidebar-label);
    padding:        1.25rem 1.25rem 0.35rem;
}

.sidebar-nav {
    list-style: none;
    margin:     0;
    padding:    0 0.6rem;
}

.sidebar-link {
    display:       flex;
    align-items:   center;
    gap:           0.65rem;
    padding:       0.6rem 0.75rem;
    border-radius: 8px;
    color:         var(--sidebar-link);
    font-size:     0.875rem;
    font-weight:   500;
    transition:    all var(--transition);
    margin-bottom: 2px;
}

.sidebar-link i { font-size: 1rem; flex-shrink: 0; }

.sidebar-link:hover {
    color:      var(--sidebar-link-hover);
    background: rgba(255,255,255,0.06);
}

.sidebar-link.active {
    color:      var(--sidebar-link-active);
    background: var(--sidebar-link-active-bg);
}

.sidebar-footer {
    margin-top: auto;
    padding:    1rem 0.75rem;
    border-top: 1px solid var(--sidebar-border);
}

.sidebar-user-card {
    display:       flex;
    align-items:   center;
    gap:           0.65rem;
    padding:       0.6rem 0.5rem;
    border-radius: 8px;
    background:    rgba(255,255,255,0.04);
}

.sidebar-avatar {
    width:            34px;
    height:           34px;
    border-radius:    50%;
    background:       linear-gradient(135deg, #3b82f6, #06b6d4);
    display:          flex;
    align-items:      center;
    justify-content:  center;
    font-weight:      700;
    font-size:        0.85rem;
    color:            #fff;
    flex-shrink:      0;
}

.sidebar-user-info { flex: 1; overflow: hidden; }

.sidebar-user-name {
    font-size:     0.8rem;
    font-weight:   600;
    color:         rgba(255,255,255,0.85);
    white-space:   nowrap;
    overflow:      hidden;
    text-overflow: ellipsis;
}

.sidebar-user-role {
    font-size:   0.65rem;
    font-weight: 600;
    margin-top:  1px;
}

.badge-role-manager { color: #fbbf24; }
.badge-role-agent   { color: #34d399; }

.sidebar-logout {
    color:      rgba(255,255,255,0.35);
    font-size:  1rem;
    transition: color var(--transition);
}
.sidebar-logout:hover { color: #f87171; }

.sidebar-toggle {
    background: none;
    border:     none;
    color:      rgba(255,255,255,0.6);
    font-size:  1.1rem;
    cursor:     pointer;
    padding:    0.25rem;
}

/* ── Main wrapper ─────────────────────────────────────────── */
.main-wrapper {
    margin-left: var(--sidebar-width);
    flex:        1;
    min-height:  100vh;
    display:     flex;
    flex-direction: column;
    transition:  margin-left var(--transition);
}

/* ── Topbar ───────────────────────────────────────────────── */
.topbar {
    height:        var(--topbar-height);
    background:    var(--topbar-bg);
    border-bottom: 1px solid var(--topbar-border);
    display:       flex;
    align-items:   center;
    gap:           1rem;
    padding:       0 1.5rem;
    position:      sticky;
    top:           0;
    z-index:       100;
}

.topbar-title h1 {
    font-size:     1.05rem;
    font-weight:   600;
    color:         var(--text-primary);
    margin:        0;
}

.topbar-actions { margin-left: auto; }

.topbar-toggle {
    background: none;
    border:     none;
    font-size:  1.25rem;
    cursor:     pointer;
    color:      var(--text-secondary);
    padding:    0.25rem;
}

/* ── Page content ─────────────────────────────────────────── */
.page-content {
    padding: 1.75rem 1.5rem;
    flex:    1;
}

/* ── Cards ────────────────────────────────────────────────── */
.card {
    border:        1px solid var(--card-border);
    border-radius: var(--card-radius);
    background:    var(--card-bg);
    box-shadow:    0 1px 3px rgba(0,0,0,0.04);
}

.card-header {
    background:    transparent;
    border-bottom: 1px solid var(--card-border);
    padding:       1rem 1.25rem;
    font-weight:   600;
    font-size:     0.9rem;
}

/* ── Stat cards ───────────────────────────────────────────── */
.stat-card {
    border-radius: var(--card-radius);
    padding:       1.25rem 1.5rem;
    background:    var(--card-bg);
    border:        1px solid var(--card-border);
    display:       flex;
    align-items:   center;
    gap:           1rem;
    transition:    transform var(--transition), box-shadow var(--transition);
}

.stat-card:hover {
    transform:  translateY(-2px);
    box-shadow: 0 8px 24px rgba(0,0,0,0.07);
}

.stat-icon {
    width:           48px;
    height:          48px;
    border-radius:   12px;
    display:         flex;
    align-items:     center;
    justify-content: center;
    font-size:       1.3rem;
    flex-shrink:     0;
}

.stat-icon.blue   { background: #dbeafe; color: #2563eb; }
.stat-icon.green  { background: #dcfce7; color: #16a34a; }
.stat-icon.amber  { background: #fef3c7; color: #d97706; }
.stat-icon.purple { background: #ede9fe; color: #7c3aed; }
.stat-icon.teal   { background: #ccfbf1; color: #0d9488; }

.stat-value {
    font-size:   1.75rem;
    font-weight: 700;
    line-height: 1;
    color:       var(--text-primary);
}

.stat-label {
    font-size:  0.78rem;
    color:      var(--text-secondary);
    margin-top: 0.2rem;
}

/* ── Tables ───────────────────────────────────────────────── */
.table-card {
    border:        1px solid var(--card-border);
    border-radius: var(--card-radius);
    overflow:      hidden;
    background:    var(--card-bg);
}

.table-card-header {
    display:       flex;
    align-items:   center;
    justify-content: space-between;
    padding:       1rem 1.25rem;
    border-bottom: 1px solid var(--card-border);
    background:    var(--card-bg);
}

.table-card-title {
    font-weight: 600;
    font-size:   0.9rem;
    color:       var(--text-primary);
}

.table {
    margin: 0;
}

.table thead th {
    font-size:      0.72rem;
    font-weight:    600;
    letter-spacing: 0.05em;
    text-transform: uppercase;
    color:          var(--text-secondary);
    border-bottom:  2px solid var(--card-border);
    padding:        0.75rem 1rem;
    background:     #fafbfc;
}

.table tbody td {
    padding:    0.85rem 1rem;
    font-size:  0.875rem;
    color:      var(--text-primary);
    border-color: var(--card-border);
    vertical-align: middle;
}

.table tbody tr:last-child td { border-bottom: none; }

.table tbody tr:hover td { background: #f8fafc; }

/* ── Badges ───────────────────────────────────────────────── */
.badge {
    font-family:    var(--font-base);
    font-size:      0.7rem;
    font-weight:    600;
    padding:        0.3em 0.65em;
    border-radius:  6px;
}

.badge-sent    { background: #dcfce7; color: #15803d; }
.badge-pending { background: #fef9c3; color: #854d0e; }
.badge-failed  { background: #fee2e2; color: #b91c1c; }
.badge-input   { background: #dbeafe; color: #1d4ed8; }
.badge-formula { background: #ede9fe; color: #6d28d9; }
.badge-manager { background: #fef3c7; color: #92400e; }
.badge-agent   { background: #d1fae5; color: #065f46; }
.badge-active  { background: #dcfce7; color: #15803d; }
.badge-disabled { background: #f1f5f9; color: #64748b; }

/* ── Forms ────────────────────────────────────────────────── */
.form-label {
    font-size:   0.83rem;
    font-weight: 500;
    color:       var(--text-primary);
    margin-bottom: 0.35rem;
}

.form-control, .form-select {
    border-radius: 8px;
    border-color:  #d1d5db;
    font-size:     0.875rem;
    transition:    border-color var(--transition), box-shadow var(--transition);
}

.form-control:focus, .form-select:focus {
    border-color: var(--primary);
    box-shadow:   0 0 0 3px rgba(37,99,235,0.1);
}

textarea.form-control {
    font-family: var(--font-mono);
    font-size:   0.82rem;
    line-height: 1.6;
}

.form-hint {
    font-size:  0.75rem;
    color:      var(--text-muted);
    margin-top: 0.3rem;
}

/* ── Buttons ──────────────────────────────────────────────── */
.btn {
    font-family:   var(--font-base);
    font-weight:   500;
    border-radius: 8px;
    font-size:     0.875rem;
    transition:    all var(--transition);
}

.btn-primary {
    background: var(--primary);
    border-color: var(--primary);
}

.btn-primary:hover {
    background: #1d4ed8;
    border-color: #1d4ed8;
}

.btn-sm { font-size: 0.78rem; padding: 0.3rem 0.7rem; }

.btn-icon {
    width:           32px;
    height:          32px;
    border-radius:   8px;
    display:         inline-flex;
    align-items:     center;
    justify-content: center;
    font-size:       0.875rem;
    padding:         0;
    border:          1px solid transparent;
}

.btn-icon-edit {
    background:   #eff6ff;
    color:        var(--primary);
    border-color: #bfdbfe;
}
.btn-icon-edit:hover { background: #dbeafe; color: var(--primary); }

.btn-icon-delete {
    background:   var(--danger-soft);
    color:        var(--danger);
    border-color: #fecaca;
}
.btn-icon-delete:hover { background: #fee2e2; color: var(--danger); }

.btn-icon-view {
    background:   #f0fdf4;
    color:        var(--success);
    border-color: #bbf7d0;
}
.btn-icon-view:hover { background: #dcfce7; color: var(--success); }

/* ── Variable tag ─────────────────────────────────────────── */
.var-tag {
    display:       inline-flex;
    align-items:   center;
    background:    #eff6ff;
    border:        1px solid #bfdbfe;
    color:         #1e40af;
    border-radius: 5px;
    font-family:   var(--font-mono);
    font-size:     0.75rem;
    padding:       0.1em 0.45em;
    cursor:        pointer;
    transition:    all var(--transition);
    user-select:   none;
}
.var-tag:hover { background: #dbeafe; border-color: #93c5fd; }

/* ── Page header ──────────────────────────────────────────── */
.page-header {
    display:       flex;
    align-items:   center;
    justify-content: space-between;
    margin-bottom: 1.5rem;
    flex-wrap:     wrap;
    gap:           0.75rem;
}

.page-header-title {
    font-size:   1.25rem;
    font-weight: 700;
    color:       var(--text-primary);
    margin:      0;
}

/* ── Empty state ──────────────────────────────────────────── */
.empty-state {
    text-align: center;
    padding:    3rem 1.5rem;
}

.empty-state-icon {
    width:           64px;
    height:          64px;
    border-radius:   16px;
    background:      #f1f5f9;
    display:         flex;
    align-items:     center;
    justify-content: center;
    font-size:       1.75rem;
    color:           var(--text-muted);
    margin:          0 auto 1rem;
}

.empty-state-title {
    font-weight: 600;
    color:       var(--text-primary);
    margin-bottom: 0.35rem;
}

.empty-state-desc {
    font-size: 0.875rem;
    color:     var(--text-secondary);
}

/* ── Login page ───────────────────────────────────────────── */
.login-page {
    min-height: 100vh;
    display:    flex;
    align-items: center;
    justify-content: center;
    background: linear-gradient(135deg, #0d1b2a 0%, #1e3a5f 50%, #0d1b2a 100%);
    padding:    1rem;
}

.login-card {
    width:         100%;
    max-width:     420px;
    background:    #fff;
    border-radius: 16px;
    padding:       2.5rem;
    box-shadow:    0 25px 60px rgba(0,0,0,0.3);
}

.login-logo {
    display:       flex;
    align-items:   center;
    gap:           0.6rem;
    margin-bottom: 2rem;
    justify-content: center;
}

.login-logo .logo-icon {
    width:  40px;
    height: 40px;
    font-size: 1.1rem;
}

.login-logo .logo-text {
    color:     var(--text-primary);
    font-size: 1.3rem;
}

/* ── Email view ───────────────────────────────────────────── */
.email-meta { font-size: 0.82rem; color: var(--text-secondary); }
.email-body-preview {
    background:    #f8fafc;
    border:        1px solid var(--card-border);
    border-radius: 8px;
    padding:       1.25rem;
    font-size:     0.875rem;
    line-height:   1.7;
    white-space:   pre-wrap;
    font-family:   var(--font-base);
    word-break:    break-word;
}
/* When preview contains HTML (from rich editor), override pre-wrap */
.email-body-preview p,
.email-body-preview ul,
.email-body-preview ol,
.email-body-preview h1,
.email-body-preview h2,
.email-body-preview h3 {
    white-space: normal;
}

/* ── Formula editor ───────────────────────────────────────── */
.formula-row { display: none; }
.formula-row.show { display: block; }

/* ── Sidebar overlay (mobile) ─────────────────────────────── */
.sidebar-overlay {
    display:    none;
    position:   fixed;
    inset:      0;
    background: rgba(0,0,0,0.5);
    z-index:    999;
}

/* ── Responsive ───────────────────────────────────────────── */
@media (max-width: 991px) {
    .sidebar {
        transform: translateX(calc(-1 * var(--sidebar-width)));
    }
    .sidebar.open {
        transform: translateX(0);
    }
    .sidebar-overlay.show {
        display: block;
    }
    .main-wrapper {
        margin-left: 0;
    }
}

/* ── Alert improvements ───────────────────────────────────── */
.alert {
    border-radius: 10px;
    font-size:     0.875rem;
    border:        none;
}

.alert-success { background: var(--success-soft); color: #15803d; }
.alert-danger  { background: var(--danger-soft);  color: var(--danger); }
.alert-warning { background: var(--warning-soft); color: var(--warning); }
.alert-info    { background: #e0f2fe;             color: var(--info); }

/* ── Rich Text Editor ─────────────────────────────────────── */
.rte-wrap {
    border: 1px solid #d1d5db;
    border-radius: 8px;
    overflow: hidden;
    background: #fff;
    transition: border-color var(--transition), box-shadow var(--transition);
}
.rte-wrap:focus-within {
    border-color: var(--primary);
    box-shadow: 0 0 0 3px rgba(37,99,235,0.1);
}

.rte-toolbar {
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    gap: 2px;
    padding: 6px 8px;
    background: #f8fafc;
    border-bottom: 1px solid #e8edf2;
    user-select: none;
}

.rte-toolbar .rte-sep {
    width: 1px;
    height: 20px;
    background: #e2e8f0;
    margin: 0 4px;
    flex-shrink: 0;
}

.rte-toolbar button {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 30px;
    height: 30px;
    border: 1px solid transparent;
    border-radius: 5px;
    background: none;
    color: #475569;
    font-size: .82rem;
    cursor: pointer;
    transition: all 0.15s ease;
    padding: 0;
    flex-shrink: 0;
}
.rte-toolbar button:hover {
    background: #e2e8f0;
    color: #1e293b;
}
.rte-toolbar button.active {
    background: #dbeafe;
    color: var(--primary);
    border-color: #bfdbfe;
}

.rte-toolbar select {
    height: 30px;
    border: 1px solid #e2e8f0;
    border-radius: 5px;
    background: #fff;
    color: #475569;
    font-size: .75rem;
    padding: 0 6px;
    cursor: pointer;
    outline: none;
}
.rte-toolbar select:focus {
    border-color: var(--primary);
}

.rte-body {
    min-height: 280px;
    padding: 16px 18px;
    font-family: Arial, sans-serif;
    font-size: 14px;
    line-height: 1.7;
    color: #111827;
    outline: none;
    overflow-y: auto;
    word-break: break-word;
}
.rte-body:empty::before {
    content: attr(data-placeholder);
    color: #94a3b8;
    pointer-events: none;
}
.rte-body a { color: #2563eb; }
.rte-body p { margin: 0 0 0.5em; }
.rte-body h1, .rte-body h2, .rte-body h3 {
    margin: 0.6em 0 0.3em;
    line-height: 1.3;
}

.rte-source {
    display: none;
    width: 100%;
    min-height: 280px;
    padding: 16px 18px;
    border: none;
    outline: none;
    font-family: var(--font-mono);
    font-size: .82rem;
    line-height: 1.6;
    color: #1e293b;
    background: #f8fafc;
    resize: vertical;
}
.rte-wrap.source-mode .rte-body   { display: none; }
.rte-wrap.source-mode .rte-source { display: block; }

/* ── Animations ───────────────────────────────────────────── */
@keyframes fadeInUp {
    from { opacity: 0; transform: translateY(12px); }
    to   { opacity: 1; transform: translateY(0); }
}

.page-content > * { animation: fadeInUp 0.25s ease both; }
.page-content > *:nth-child(2) { animation-delay: 0.04s; }
.page-content > *:nth-child(3) { animation-delay: 0.08s; }
.page-content > *:nth-child(4) { animation-delay: 0.12s; }
