*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

:root {
    --primary: #4f46e5;
    --primary-hover: #4338ca;
    --accent: #595cea;
    --text: #111827;
    --muted: #6b7280;
    --text-muted: #3d4c72;
    --border: #e5e7eb;
    --bg: #f9fafb;
    --white: #ffffff;
    --radius: 8px;
    --sidebar-width: 220px;

    --high: #dc2626;
    --high-bg: #fef2f2;
    --high-border: #fecaca;
    --medium: #d97706;
    --medium-bg: #fffbeb;
    --medium-border: #fde68a;
    --low: #16a34a;
    --low-bg: #f0fdf4;
    --low-border: #bbf7d0;
    --bg-card: #fff;
    --bg-secondary: #ebefff;
}

body {
    font-family: system-ui, -apple-system, sans-serif;
    background: var(--bg);
    color: var(--text);
    font-size: 0.95rem;
}

/* ── Layout general ───────────────────────────────────────────── */
.app-layout {
    display: flex;
    min-height: 100vh;
}

.app-main {
    flex: 1;
    min-width: 0;
    margin-left: var(--sidebar-width);
}

.app-content {
    max-width: 900px;
    margin: 0 auto;
    padding: 2rem 1.5rem;
}

/* ── Sidebar ──────────────────────────────────────────────────── */
.sidebar {
    width: var(--sidebar-width);
    background: var(--white);
    border-right: 1px solid var(--border);
    display: flex;
    flex-direction: column;
    position: fixed;
    top: 0;
    left: 0;
    height: 100vh;
    padding: 1.5rem 0;
}

.sidebar-brand {
    font-weight: 700;
    font-size: 1.1rem;
    color: var(--primary);
    text-decoration: none;
    padding: 0 1.25rem;
    margin-bottom: 1.5rem;
    display: block;
}

.sidebar-nav {
    flex: 1;
    display: flex;
    flex-direction: column;
    gap: 2px;
}

.sidebar-link {
    display: flex;
    align-items: center;
    gap: 0.6rem;
    padding: 0.6rem 1.25rem;
    color: var(--muted);
    text-decoration: none;
    border-radius: 0;
    font-size: 0.9rem;
    transition: background 0.1s, color 0.1s;
}

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

.sidebar-link.active {
    background: #eef2ff;
    color: var(--primary);
    font-weight: 500;
    border-right: 2px solid var(--primary);
}

.sidebar-icon {
    font-size: 0.85rem;
    width: 18px;
    text-align: center;
}

.sidebar-footer {
    padding: 1rem 1.25rem;
    border-top: 1px solid var(--border);
    display: flex;
    flex-direction: column;
    gap: 0.6rem;
}

.sidebar-user {
    font-size: 0.8rem;
    color: var(--muted);
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
    text-align: center;
}

.btn-logout {
    background: none;
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 0.4rem 0.75rem;
    font-size: 0.8rem;
    color: var(--muted);
    cursor: pointer;
    width: 100%;
    transition: border-color 0.1s, color 0.1s;
}

.btn-logout:hover {
    border-color: var(--high);
    color: var(--high);
}

/* ── Page header ──────────────────────────────────────────────── */
.page-header {
    margin-bottom: 1.75rem;
    display: flex;
    align-items: baseline;
    gap: 0.75rem;
}

.page-header h1 {
    font-size: 1.4rem;
    font-weight: 600;
}

.page-subtitle {
    font-size: 0.875rem;
    color: var(--muted);
}

/* ── Stats grid ───────────────────────────────────────────────── */
.stats-grid {
    display: grid;
    grid-template-columns: repeat(3, 1fr);
    gap: 1rem;
    margin-bottom: 2rem;
}

.stat-card {
    background: var(--white);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 1.25rem;
    display: flex;
    flex-direction: column;
    gap: 0.4rem;
}

.stat-label {
    font-size: 0.8rem;
    color: var(--muted);
    text-transform: uppercase;
    letter-spacing: 0.04em;
}

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

/* ── Sections ─────────────────────────────────────────────────── */
.section {
    background: var(--white);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 1.5rem;
    margin-bottom: 1.5rem;
}

.section-title {
    font-size: 1rem;
    font-weight: 600;
    margin-bottom: 1.25rem;
}

/* ── Urgency cards ────────────────────────────────────────────── */
.urgency-grid {
    display: grid;
    grid-template-columns: repeat(3, 1fr);
    gap: 1rem;
    margin-bottom: 1.25rem;
}

.urgency-card {
    border-radius: var(--radius);
    padding: 1rem;
    display: flex;
    flex-direction: column;
    gap: 0.4rem;
    border: 1px solid;
}

.urgency-high  { background: var(--high-bg);   border-color: var(--high-border);   }
.urgency-medium{ background: var(--medium-bg); border-color: var(--medium-border); }
.urgency-low   { background: var(--low-bg);    border-color: var(--low-border);    }

.urgency-label {
    font-size: 0.8rem;
    font-weight: 500;
}
.urgency-high   .urgency-label { color: var(--high);   }
.urgency-medium .urgency-label { color: var(--medium); }
.urgency-low    .urgency-label { color: var(--low);    }

.urgency-count {
    font-size: 1.75rem;
    font-weight: 700;
    line-height: 1;
}
.urgency-high   .urgency-count { color: var(--high);   }
.urgency-medium .urgency-count { color: var(--medium); }
.urgency-low    .urgency-count { color: var(--low);    }

/* ── Urgency bar ──────────────────────────────────────────────── */
.urgency-bar {
    height: 8px;
    border-radius: 4px;
    overflow: hidden;
    display: flex;
    background: var(--border);
}

.urgency-bar-segment {
    height: 100%;
    transition: width 0.3s ease;
}

.bar-high   { background: var(--high);   }
.bar-medium { background: var(--medium); }
.bar-low    { background: var(--low);    }

/* ── Empty state ──────────────────────────────────────────────── */
.empty-state {
    color: var(--muted);
    font-size: 0.875rem;
    text-align: center;
    padding: 1.5rem 0 0.5rem;
    line-height: 1.6;
}

/* ── Auth card (login) ────────────────────────────────────────── */
.container { max-width: 1200px; margin: 0 auto; padding: 1.5rem; }

.auth-card {
    max-width: 380px;
    margin: 5rem auto;
    background: var(--white);
    padding: 2.5rem 2rem;
    border-radius: var(--radius);
    border: 1px solid var(--border);
    text-align: center;
}

.auth-card h1 { font-size: 1.75rem; margin-bottom: 0.5rem; }
.auth-subtitle { color: var(--muted); margin-bottom: 2rem; font-size: 0.95rem; }

.btn-google {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 0.75rem;
    width: 100%;
    padding: 0.75rem 1rem;
    background: var(--white);
    color: var(--text);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    font-size: 0.95rem;
    font-weight: 500;
    text-decoration: none;
    transition: box-shadow 0.15s, border-color 0.15s;
}

.btn-google:hover {
    box-shadow: 0 1px 8px rgba(0,0,0,0.1);
    border-color: #c0c0c0;
}

.auth-disclaimer {
    margin-top: 1.5rem;
    font-size: 0.8rem;
    color: var(--muted);
    line-height: 1.6;
}

/* ── Info banner ──────────────────────────────────────────────── */
.info-banner {
    display: flex;
    gap: 0.75rem;
    background: #eff6ff;
    border: 1px solid #bfdbfe;
    border-radius: var(--radius);
    padding: 1rem 1.25rem;
    margin-bottom: 1rem;
    font-size: 0.875rem;
    line-height: 1.6;
    color: #1e40af;
}

.info-banner-icon {
    font-size: 1rem;
    flex-shrink: 0;
    margin-top: 1px;
}

.info-banner-text strong {
    display: block;
    margin-bottom: 0.2rem;
}

/* ── Sync status ──────────────────────────────────────────────── */
.sync-status {
    display: flex;
    align-items: center;
    gap: 0.5rem;
    font-size: 0.8rem;
    color: var(--muted);
    margin-bottom: 1.25rem;
}

.sync-dot {
    width: 8px;
    height: 8px;
    border-radius: 50%;
    flex-shrink: 0;
}

.sync-ok      { background: var(--low); }
.sync-pending { background: var(--medium); }

/* ── Empty state mejorado ─────────────────────────────────────── */
.empty-state {
    text-align: center;
    padding: 2rem 1rem;
    color: var(--muted);
    font-size: 0.875rem;
    line-height: 1.8;
}

.empty-icon {
    display: block;
    font-size: 2rem;
    margin-bottom: 0.75rem;
    opacity: 0.4;
}

/* ── Filters bar ──────────────────────────────────────────────── */
.filters-bar {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 1rem;
    margin-bottom: 1rem;
    flex-wrap: wrap;
}

.filters-left {
    display: flex;
    align-items: center;
    gap: 0.75rem;
    flex-wrap: wrap;
}

.filters-right {
    display: flex;
    align-items: center;
    gap: 0.5rem;
}

/* Búsqueda */
.search-wrapper { position: relative; }

.search-input {
    width: 260px;
    padding: 0.5rem 0.75rem;
    border: 1px solid var(--border);
    border-radius: var(--radius);
    font-size: 0.875rem;
    outline: none;
    transition: border-color 0.15s;
    background: var(--white);
}

.search-input:focus { border-color: var(--primary); }

/* Tabs de urgencia */
.filter-tabs {
    display: flex;
    gap: 2px;
    background: var(--border);
    border-radius: var(--radius);
    padding: 2px;
}

.filter-tab {
    padding: 0.35rem 0.75rem;
    border-radius: calc(var(--radius) - 2px);
    font-size: 0.8rem;
    font-weight: 500;
    text-decoration: none;
    color: var(--muted);
    transition: background 0.1s, color 0.1s;
}

.filter-tab:hover { color: var(--text); }

.filter-tab.active {
    background: var(--white);
    color: var(--text);
    box-shadow: 0 1px 3px rgba(0,0,0,0.08);
}

.urgency-tab-high.active  { color: var(--high);   }
.urgency-tab-medium.active{ color: var(--medium); }
.urgency-tab-low.active   { color: var(--low);    }

/* Selector de orden */
.order-select {
    padding: 0.5rem 0.75rem;
    border: 1px solid var(--border);
    border-radius: var(--radius);
    font-size: 0.875rem;
    background: var(--white);
    color: var(--text);
    outline: none;
    cursor: pointer;
}

/* ── Tabla de correos ─────────────────────────────────────────── */
.table-wrapper {
    background: var(--white);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    overflow: visible;
    margin-bottom: 1rem;
    width: 100%;
}

.emails-table {
    width: 100%;
    border-collapse: collapse;
    font-size: 0.875rem;
    table-layout: fixed;    /* Clave: fuerza que las columnas respeten el ancho definido */
}

.emails-table thead {
    background: var(--bg);
    border-bottom: 1px solid var(--border);
}

.emails-table thead tr:first-child th:first-child {
    border-top-left-radius: var(--radius);
}
.emails-table thead tr:first-child th:last-child {
    border-top-right-radius: var(--radius);
}

.emails-table th {
    padding: 0.7rem 1rem;
    text-align: left;
    font-size: 0.75rem;
    font-weight: 600;
    color: var(--muted);
    text-transform: uppercase;
    letter-spacing: 0.04em;
    white-space: nowrap;
    overflow: hidden;
}

.emails-table td {
    padding: 0.75rem 1rem;
    border-bottom: 1px solid var(--border);
    vertical-align: middle;
    overflow: hidden;       /* Sin esto el texto desborda aunque tenga ellipsis */
}

.email-row:last-child td { border-bottom: none; }
.email-row:hover { background: var(--bg); }

/* Anchos fijos — deben sumar ~100% */
.col-urgency  { width: 100px; }
.col-subject  { width: 40%; }
.col-sender   { width: 30%; }
.col-date     { width: 110px; white-space: nowrap; }
.col-actions  { width: 48px; text-align: center; }

/* Ellipsis en celdas — el display:block + max-width:100% es lo que activa el corte */
.email-subject,
.email-sender {
    display: block;
    max-width: 100%;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.email-subject { font-weight: 500; color: var(--text); }
.email-sender  { color: var(--muted); font-size: 0.8rem; }
.email-date    { color: var(--muted); font-size: 0.8rem; }

/* Tooltip nativo mejorado con CSS puro
   El atributo title ya existe en el HTML, pero el tooltip
   del navegador es lento y feo. Este reemplaza ese comportamiento
   con un tooltip CSS que aparece inmediatamente al hacer hover. */
.email-subject-cell {
    position: relative;
}

.email-subject-cell:hover .email-tooltip {
    opacity: 1;
    visibility: visible;
    transform: translateY(0);
}

.email-tooltip {
    position: fixed;
    left: 0;
    top: calc(100% + 4px);
    background: #1f2937;
    color: #f9fafb;
    padding: 0.5rem 0.75rem;
    border-radius: var(--radius);
    font-size: 0.8rem;
    line-height: 1.5;
    white-space: normal;
    max-width: 420px;
    min-width: 200px;
    z-index: 9999;
    opacity: 0;
    visibility: hidden;
    transform: translateY(-4px);
    transition: opacity 0.15s, transform 0.15s, visibility 0.15s;
    pointer-events: none;
    box-shadow: 0 4px 12px rgba(0,0,0,0.15);
    word-break: break-word;
}

/* Flecha del tooltip */
.email-tooltip::before {
    content: '';
    position: absolute;
    top: -4px;
    left: 16px;
    width: 8px;
    height: 8px;
    background: #1f2937;
    transform: rotate(45deg);
}

/* ── Paginación ───────────────────────────────────────────────── */
.pagination {
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 1rem;
    padding: 0.5rem 0;
}

.page-btn {
    padding: 0.45rem 1rem;
    border: 1px solid var(--border);
    border-radius: var(--radius);
    font-size: 0.875rem;
    text-decoration: none;
    color: var(--text);
    background: var(--white);
    transition: border-color 0.1s, color 0.1s;
}

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

.page-btn-disabled {
    color: var(--muted);
    cursor: default;
    pointer-events: none;
}

.page-info { font-size: 0.875rem; color: var(--muted); }

/* Limpiar filtros */
.btn-clear-filters {
    display: inline-block;
    margin-top: 0.75rem;
    padding: 0.5rem 1rem;
    border: 1px solid var(--border);
    border-radius: var(--radius);
    font-size: 0.875rem;
    color: var(--muted);
    text-decoration: none;
    transition: border-color 0.1s, color 0.1s;
}

.btn-clear-filters:hover { border-color: var(--primary); color: var(--primary); }

.emails-page .app-content {
    max-width: 1100px;
}

/* ── Reglas ───────────────────────────────────────────────────── */
.rule-name  { display: block; font-weight: 500; }
.rule-desc  { display: block; font-size: 0.8rem; color: var(--muted); margin-top: 2px; }

.rule-pattern {
    font-family: monospace;
    font-size: 0.8rem;
    background: var(--bg);
    padding: 2px 6px;
    border-radius: 4px;
    border: 1px solid var(--border);
    display: inline-block;
    max-width: 200px;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
    vertical-align: middle;
}

.field-badge {
    font-size: 0.75rem;
    color: var(--muted);
    background: var(--bg);
    border: 1px solid var(--border);
    border-radius: 4px;
    padding: 2px 6px;
    white-space: nowrap;
}

.priority-value {
    font-size: 0.875rem;
    font-weight: 600;
    color: var(--muted);
}

.text-center { text-align: center; }

/* Botones de mover */
.move-btns {
    display: flex;
    flex-direction: column;
    gap: 1px;
    align-items: center;
}

.move-btn {
    background: none;
    border: 1px solid var(--border);
    border-radius: 3px;
    width: 22px;
    height: 18px;
    font-size: 0.6rem;
    cursor: pointer;
    color: var(--muted);
    line-height: 1;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: background 0.1s, color 0.1s;
}

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

/* Toggle switch */
.toggle-switch {
    position: relative;
    display: inline-block;
    width: 36px;
    height: 20px;
    cursor: pointer;
}

.toggle-switch input { opacity: 0; width: 0; height: 0; }

.toggle-slider {
    position: absolute;
    inset: 0;
    background: var(--border);
    border-radius: 20px;
    transition: background 0.2s;
}

.toggle-slider::before {
    content: '';
    position: absolute;
    width: 14px;
    height: 14px;
    left: 3px;
    top: 3px;
    background: white;
    border-radius: 50%;
    transition: transform 0.2s;
    box-shadow: 0 1px 3px rgba(0,0,0,0.2);
}

.toggle-switch input:checked + .toggle-slider {
    background: var(--low);
}

.toggle-switch input:checked + .toggle-slider::before {
    transform: translateX(16px);
}

/* Botones de acción en fila */
.row-actions {
    display: flex;
    gap: 4px;
    justify-content: flex-end;
}

.action-btn {
    background: none;
    border: 1px solid var(--border);
    border-radius: 4px;
    width: 28px;
    height: 28px;
    font-size: 0.85rem;
    cursor: pointer;
    color: var(--muted);
    display: flex;
    align-items: center;
    justify-content: center;
    transition: background 0.1s, color 0.1s, border-color 0.1s;
}

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

.action-btn-danger:hover {
    color: var(--high);
    border-color: var(--high-border);
    background: var(--high-bg);
}

/* ── Modal ────────────────────────────────────────────────────── */
.modal-overlay {
    position: fixed;
    inset: 0;
    background: rgba(0,0,0,0.4);
    display: flex;
    align-items: center;
    justify-content: center;
    z-index: 1000;
    padding: 1rem;
}

.modal-overlay[hidden] { display: none; }

.modal {
    background: var(--white);
    border-radius: var(--radius);
    width: 100%;
    max-width: 520px;
    box-shadow: 0 8px 32px rgba(0,0,0,0.15);
}

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

.modal-header h2 { font-size: 1.1rem; font-weight: 600; }

.modal-close {
    background: none;
    border: none;
    font-size: 1rem;
    color: var(--muted);
    cursor: pointer;
    width: 28px;
    height: 28px;
    display: flex;
    align-items: center;
    justify-content: center;
    border-radius: 4px;
    transition: background 0.1s;
}

.modal-close:hover { background: var(--bg); color: var(--text); }

.modal-body {
    padding: 1.5rem;
    display: flex;
    flex-direction: column;
    gap: 1rem;
}

.modal-footer {
    padding: 1rem 1.5rem;
    border-top: 1px solid var(--border);
    display: flex;
    justify-content: flex-end;
    gap: 0.75rem;
}

/* Form dentro del modal */
.form-group {
    display: flex;
    flex-direction: column;
    gap: 0.35rem;
}

.form-group label {
    font-size: 0.875rem;
    font-weight: 500;
    color: var(--text);
}

.form-group input,
.form-group select {
    padding: 0.55rem 0.75rem;
    border: 1px solid var(--border);
    border-radius: var(--radius);
    font-size: 0.875rem;
    outline: none;
    transition: border-color 0.15s;
    background: var(--white);
}

.form-group input:focus,
.form-group select:focus { border-color: var(--primary); }

.form-row {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 1rem;
}

.required { color: var(--high); }
.label-hint { font-weight: 400; color: var(--muted); font-size: 0.8rem; }
.field-hint { font-size: 0.78rem; color: var(--muted); margin-top: 2px; }

/* Botones */
.btn-primary {
    padding: 0.55rem 1.1rem;
    background: var(--primary);
    color: white;
    border: none;
    border-radius: var(--radius);
    font-size: 0.875rem;
    font-weight: 500;
    cursor: pointer;
    transition: background 0.15s;
}

.btn-primary:hover { background: var(--primary-hover); }
.btn-primary:disabled { opacity: 0.6; cursor: not-allowed; }

.btn-secondary {
    padding: 0.55rem 1.1rem;
    background: var(--white);
    color: var(--text);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    font-size: 0.875rem;
    font-weight: 500;
    cursor: pointer;
    transition: border-color 0.15s;
}

.btn-secondary:hover { border-color: var(--muted); }

/* ── Condition builder ────────────────────────────────────────── */
.condition-builder {
    display: flex;
    gap: 0.5rem;
}

.condition-builder select {
    flex-shrink: 0;
    width: 170px;
}

.condition-builder input {
    flex: 1;
}

.condition-text {
    font-size: 0.875rem;
    color: var(--muted);
}

.condition-text code {
    font-family: monospace;
    font-size: 0.8rem;
    background: var(--bg);
    padding: 1px 5px;
    border-radius: 3px;
    border: 1px solid var(--border);
    color: var(--text);
}

/* ── Urgency badges (tabla de correos y reglas) ───────────────── */
.urgency-badge {
    display: inline-block;
    font-size: 0.75rem;
    font-weight: 600;
    padding: 2px 8px;
    border-radius: 10px;
    white-space: nowrap;
}

.badge-high   { background: var(--high-bg);   color: var(--high);   border: 1px solid var(--high-border); }
.badge-medium { background: var(--medium-bg); color: var(--medium); border: 1px solid var(--medium-border); }
.badge-low    { background: var(--low-bg);    color: var(--low);    border: 1px solid var(--low-border); }

/* Badge de prioritario — remitente en whitelist */
.priority-badge {
    display: inline-block;
    font-size: 0.75rem;
    color: var(--high);
    margin-left: 4px;
    vertical-align: middle;
    title: "Remitente en lista blanca";
}
