/* ================================================================
   WebDav MexMail — Hoja de estilos principal
   Soporte completo para tema claro/oscuro (prefers-color-scheme
   + toggle manual mediante data-theme en <html>)
   ================================================================ */

/* ---- Variables de tema claro (por defecto) ---- */
:root,
[data-theme="light"] {
    --color-bg:          #f5f7fa;
    --color-surface:     #ffffff;
    --color-surface-2:   #eef1f6;
    --color-border:      #d8dde6;
    --color-text:        #1a202c;
    --color-text-2:      #4a5568;
    --color-text-muted:  #718096;
    --color-primary:     #2563eb;
    --color-primary-h:   #1d4ed8;
    --color-primary-fg:  #ffffff;
    --color-success:     #16a34a;
    --color-warning:     #d97706;
    --color-danger:      #dc2626;
    --color-info:        #0891b2;
    --color-sidebar-bg:  #1e293b;
    --color-sidebar-fg:  #cbd5e1;
    --color-sidebar-act: #2563eb;
    --color-shadow:      rgba(0, 0, 0, 0.08);
    --color-overlay:     rgba(0, 0, 0, 0.45);

    --radius-sm:   4px;
    --radius-md:   8px;
    --radius-lg:  12px;
    --radius-xl:  16px;
    --radius-full: 9999px;

    --shadow-sm:  0 1px  3px var(--color-shadow);
    --shadow-md:  0 4px  6px var(--color-shadow);
    --shadow-lg:  0 10px 25px var(--color-shadow);

    --font-sans:  'Inter', system-ui, -apple-system, sans-serif;
    --font-mono:  'JetBrains Mono', 'Fira Code', monospace;
    --font-size:  14px;
    --line-height: 1.6;

    --sidebar-width:    240px;
    --navbar-height:    56px;
    --transition:       0.2s ease;
}

/* ---- Variables de tema oscuro ---- */
@media (prefers-color-scheme: dark) {
    :root:not([data-theme="light"]) {
        --color-bg:          #0f172a;
        --color-surface:     #1e293b;
        --color-surface-2:   #263349;
        --color-border:      #334155;
        --color-text:        #f1f5f9;
        --color-text-2:      #cbd5e1;
        --color-text-muted:  #94a3b8;
        --color-primary:     #3b82f6;
        --color-primary-h:   #2563eb;
        --color-primary-fg:  #ffffff;
        --color-success:     #22c55e;
        --color-warning:     #f59e0b;
        --color-danger:      #ef4444;
        --color-info:        #22d3ee;
        --color-sidebar-bg:  #0f172a;
        --color-sidebar-fg:  #94a3b8;
        --color-sidebar-act: #3b82f6;
        --color-shadow:      rgba(0, 0, 0, 0.4);
        --color-overlay:     rgba(0, 0, 0, 0.7);
    }
}

[data-theme="dark"] {
    --color-bg:          #0f172a;
    --color-surface:     #1e293b;
    --color-surface-2:   #263349;
    --color-border:      #334155;
    --color-text:        #f1f5f9;
    --color-text-2:      #cbd5e1;
    --color-text-muted:  #94a3b8;
    --color-primary:     #3b82f6;
    --color-primary-h:   #2563eb;
    --color-primary-fg:  #ffffff;
    --color-success:     #22c55e;
    --color-warning:     #f59e0b;
    --color-danger:      #ef4444;
    --color-info:        #22d3ee;
    --color-sidebar-bg:  #0f172a;
    --color-sidebar-fg:  #94a3b8;
    --color-sidebar-act: #3b82f6;
    --color-shadow:      rgba(0, 0, 0, 0.4);
    --color-overlay:     rgba(0, 0, 0, 0.7);
}

/* ================================================================
   Reset y base
   ================================================================ */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

html { font-size: var(--font-size); scroll-behavior: smooth; }

body {
    font-family:    var(--font-sans);
    background:     var(--color-bg);
    color:          var(--color-text);
    line-height:    var(--line-height);
    transition:     background var(--transition), color var(--transition);
    min-height:     100vh;
}

a { color: var(--color-primary); text-decoration: none; }
a:hover { text-decoration: underline; }

img, svg { display: block; max-width: 100%; }

button, input, select, textarea {
    font-family: inherit;
    font-size:   inherit;
}

/* ================================================================
   Layout: Navbar + Sidebar + Main
   ================================================================ */
.app-layout {
    display: grid;
    grid-template-columns: var(--sidebar-width) 1fr;
    grid-template-rows:    var(--navbar-height) 1fr;
    grid-template-areas:
        "navbar  navbar"
        "sidebar main";
    min-height: 100vh;
}

/* ---- Navbar ---- */
.navbar {
    grid-area:      navbar;
    position:       sticky;
    top:            0;
    z-index:        100;
    height:         var(--navbar-height);
    display:        flex;
    align-items:    center;
    padding:        0 1rem;
    gap:            1rem;
    background:     var(--color-surface);
    border-bottom:  1px solid var(--color-border);
    box-shadow:     var(--shadow-sm);
}

.navbar__brand {
    display:     flex;
    align-items: center;
    gap:         0.5rem;
    font-weight: 700;
    font-size:   1.1rem;
    color:       var(--color-text);
    white-space: nowrap;
}

.navbar__brand svg { color: var(--color-primary); }

.navbar__spacer { flex: 1; }

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

.navbar__user {
    display:     flex;
    align-items: center;
    gap:         0.5rem;
    padding:     0.4rem 0.75rem;
    border-radius: var(--radius-full);
    background:  var(--color-surface-2);
    font-size:   0.85rem;
    color:       var(--color-text-2);
}

/* ---- Sidebar ---- */
.sidebar {
    grid-area:    sidebar;
    position:     sticky;
    top:          var(--navbar-height);
    height:       calc(100vh - var(--navbar-height));
    overflow-y:   auto;
    background:   var(--color-sidebar-bg);
    border-right: 1px solid rgba(255,255,255,0.05);
    display:      flex;
    flex-direction: column;
    padding:      0.75rem 0;
    transition:   transform var(--transition), width var(--transition);
}

.sidebar__nav { flex: 1; }

.sidebar__item {
    display:        flex;
    align-items:    center;
    gap:            0.75rem;
    padding:        0.65rem 1.25rem;
    color:          var(--color-sidebar-fg);
    cursor:         pointer;
    border-left:    3px solid transparent;
    transition:     all var(--transition);
    font-size:      0.9rem;
    text-decoration: none;
}

.sidebar__item:hover {
    background: rgba(255,255,255,0.06);
    color:      #fff;
}

.sidebar__item.active {
    background:  rgba(59,130,246,0.15);
    border-left-color: var(--color-sidebar-act);
    color:       #fff;
}

.sidebar__item svg { flex-shrink: 0; width: 18px; height: 18px; }

/* ---- Main content ---- */
.main-content {
    grid-area: main;
    padding:   1.5rem;
    overflow:  auto;
}

/* ================================================================
   Botones
   ================================================================ */
.btn {
    display:       inline-flex;
    align-items:   center;
    gap:           0.4rem;
    padding:       0.45rem 1rem;
    border:        1px solid transparent;
    border-radius: var(--radius-md);
    font-size:     0.875rem;
    font-weight:   500;
    cursor:        pointer;
    transition:    all var(--transition);
    white-space:   nowrap;
    text-decoration: none;
}

.btn:focus-visible { outline: 2px solid var(--color-primary); outline-offset: 2px; }

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

.btn-secondary {
    background:   var(--color-surface-2);
    color:        var(--color-text);
    border-color: var(--color-border);
}
.btn-secondary:hover { background: var(--color-border); }

.btn-danger {
    background:   var(--color-danger);
    color:        #fff;
    border-color: var(--color-danger);
}
.btn-danger:hover { opacity: 0.85; }

.btn-ghost {
    background:   transparent;
    color:        var(--color-text-2);
    border-color: transparent;
}
.btn-ghost:hover { background: var(--color-surface-2); color: var(--color-text); }

.btn-icon {
    padding: 0.4rem;
    min-width: 32px;
    min-height: 32px;
    justify-content: center;
}

.btn-sm { padding: 0.3rem 0.7rem; font-size: 0.8rem; }
.btn-lg { padding: 0.65rem 1.4rem; font-size: 1rem; }
.btn[disabled], .btn.disabled { opacity: 0.5; pointer-events: none; }

/* ================================================================
   Formularios
   ================================================================ */
.form-group { margin-bottom: 1rem; }

.form-label {
    display:       block;
    margin-bottom: 0.35rem;
    font-size:     0.875rem;
    font-weight:   500;
    color:         var(--color-text-2);
}

.form-control {
    width:         100%;
    padding:       0.5rem 0.75rem;
    background:    var(--color-surface);
    color:         var(--color-text);
    border:        1px solid var(--color-border);
    border-radius: var(--radius-md);
    transition:    border-color var(--transition), box-shadow var(--transition);
    outline:       none;
}

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

.form-error {
    margin-top: 0.25rem;
    font-size:  0.8rem;
    color:      var(--color-danger);
}

/* ================================================================
   Cards
   ================================================================ */
.card {
    background:    var(--color-surface);
    border:        1px solid var(--color-border);
    border-radius: var(--radius-lg);
    padding:       1.25rem;
    box-shadow:    var(--shadow-sm);
}

.card-header {
    display:        flex;
    align-items:    center;
    justify-content: space-between;
    margin-bottom:  1rem;
}

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

/* ================================================================
   Toast / Notificaciones
   ================================================================ */
#toast-container {
    position:  fixed;
    bottom:    1.5rem;
    right:     1.5rem;
    z-index:   9999;
    display:   flex;
    flex-direction: column;
    gap:       0.5rem;
    max-width: 380px;
}

.toast {
    display:       flex;
    align-items:   flex-start;
    gap:           0.75rem;
    padding:       0.85rem 1rem;
    background:    var(--color-surface);
    border:        1px solid var(--color-border);
    border-radius: var(--radius-md);
    box-shadow:    var(--shadow-lg);
    animation:     toastIn 0.25s ease;
    font-size:     0.875rem;
}

.toast.success { border-left: 4px solid var(--color-success); }
.toast.error   { border-left: 4px solid var(--color-danger);  }
.toast.info    { border-left: 4px solid var(--color-info);    }
.toast.warning { border-left: 4px solid var(--color-warning); }

.toast__icon { width: 18px; height: 18px; flex-shrink: 0; margin-top: 1px; }
.toast__body { flex: 1; }
.toast__title { font-weight: 600; margin-bottom: 0.15rem; }
.toast__msg   { color: var(--color-text-2); }
.toast__close { cursor: pointer; color: var(--color-text-muted); flex-shrink: 0; }
.toast__close:hover { color: var(--color-text); }

@keyframes toastIn {
    from { transform: translateX(100%); opacity: 0; }
    to   { transform: translateX(0);    opacity: 1; }
}

/* ================================================================
   Modal
   ================================================================ */
.modal-overlay {
    position:   fixed;
    inset:      0;
    background: var(--color-overlay);
    z-index:    500;
    display:    flex;
    align-items: center;
    justify-content: center;
    animation:  fadeIn 0.15s ease;
}

.modal {
    background:    var(--color-surface);
    border:        1px solid var(--color-border);
    border-radius: var(--radius-xl);
    box-shadow:    var(--shadow-lg);
    max-width:     520px;
    width:         calc(100% - 2rem);
    max-height:    90vh;
    overflow-y:    auto;
    animation:     slideUp 0.2s ease;
}

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

.modal-title { font-size: 1rem; font-weight: 600; }

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

@keyframes fadeIn  { from { opacity: 0; } to { opacity: 1; } }
@keyframes slideUp { from { transform: translateY(20px); opacity: 0; }
                     to   { transform: translateY(0);    opacity: 1; } }

/* ================================================================
   Árbol de archivos
   ================================================================ */
.file-tree { font-size: 0.85rem; }

.tree-item {
    display:      flex;
    align-items:  center;
    gap:          0.4rem;
    padding:      0.3rem 0.5rem;
    border-radius: var(--radius-sm);
    cursor:       pointer;
    color:        var(--color-text-2);
    transition:   background var(--transition);
    user-select:  none;
}

.tree-item:hover   { background: var(--color-surface-2); color: var(--color-text); }
.tree-item.active  { background: rgba(37,99,235,0.12); color: var(--color-primary); font-weight: 500; }

.tree-item__icon { width: 16px; height: 16px; flex-shrink: 0; }
.tree-item__name { flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.tree-item__arrow { transition: transform var(--transition); color: var(--color-text-muted); }
.tree-item__arrow.open { transform: rotate(90deg); }

.tree-children { margin-left: 1.25rem; border-left: 1px solid var(--color-border); }

/* ================================================================
   Tabla de archivos
   ================================================================ */
.file-list-table {
    width:           100%;
    border-collapse: collapse;
}

.file-list-table th,
.file-list-table td {
    padding:     0.6rem 0.75rem;
    text-align:  left;
    border-bottom: 1px solid var(--color-border);
    font-size:   0.875rem;
    white-space: nowrap;
}

.file-list-table th {
    background:  var(--color-surface-2);
    font-weight: 600;
    color:       var(--color-text-2);
    position:    sticky;
    top:         0;
}

.file-list-table tr:hover td { background: var(--color-surface-2); }

.file-list-table .file-name {
    display:     flex;
    align-items: center;
    gap:         0.5rem;
    max-width:   300px;
    overflow:    hidden;
    text-overflow: ellipsis;
}

.file-list-table .actions {
    display:     flex;
    gap:         0.25rem;
    justify-content: flex-end;
}

/* ================================================================
   Barra de progreso de subida
   ================================================================ */
.upload-progress {
    margin-top:    0.5rem;
    background:    var(--color-surface-2);
    border-radius: var(--radius-full);
    height:        6px;
    overflow:      hidden;
}

.upload-progress__bar {
    height:        100%;
    background:    var(--color-primary);
    border-radius: var(--radius-full);
    transition:    width 0.15s ease;
    width:         0%;
}

/* ================================================================
   Zona de Drag & Drop
   ================================================================ */
.drop-zone {
    border:        2px dashed var(--color-border);
    border-radius: var(--radius-lg);
    padding:       2rem;
    text-align:    center;
    color:         var(--color-text-muted);
    transition:    all var(--transition);
    cursor:        pointer;
}

.drop-zone.drag-over {
    border-color: var(--color-primary);
    background:   rgba(37,99,235,0.05);
    color:        var(--color-primary);
}

/* ================================================================
   Dashboard — Tarjetas de métricas
   ================================================================ */
.metrics-grid {
    display:               grid;
    grid-template-columns: repeat(auto-fill, minmax(200px, 1fr));
    gap:                   1rem;
    margin-bottom:         1.5rem;
}

.metric-card {
    background:    var(--color-surface);
    border:        1px solid var(--color-border);
    border-radius: var(--radius-lg);
    padding:       1.25rem;
    display:       flex;
    flex-direction: column;
    gap:           0.5rem;
}

.metric-card__label {
    font-size:   0.8rem;
    color:       var(--color-text-muted);
    text-transform: uppercase;
    letter-spacing: 0.05em;
    font-weight: 600;
}

.metric-card__value {
    font-size:   1.75rem;
    font-weight: 700;
    color:       var(--color-text);
    line-height: 1.2;
}

.metric-card__sub {
    font-size: 0.8rem;
    color:     var(--color-text-muted);
}

.charts-grid {
    display:               grid;
    grid-template-columns: repeat(auto-fill, minmax(320px, 1fr));
    gap:                   1.25rem;
}

.chart-container {
    background:    var(--color-surface);
    border:        1px solid var(--color-border);
    border-radius: var(--radius-lg);
    padding:       1.25rem;
    height:        300px;
    position:      relative;
}

/* ================================================================
   Editor inline
   ================================================================ */
.editor-toolbar {
    display:      flex;
    align-items:  center;
    gap:          0.5rem;
    padding:      0.5rem 0.75rem;
    background:   var(--color-surface-2);
    border-bottom: 1px solid var(--color-border);
    flex-wrap:    wrap;
}

.editor-area {
    width:        100%;
    height:       400px;
    padding:      1rem;
    background:   var(--color-surface);
    color:        var(--color-text);
    border:       none;
    resize:       vertical;
    font-family:  var(--font-mono);
    font-size:    0.85rem;
    line-height:  1.6;
    outline:      none;
}

/* ================================================================
   Login — pantalla centrada sin navbar
   ================================================================ */
.login-page {
    min-height:      100vh;
    display:         flex;
    align-items:     center;
    justify-content: center;
    padding:         1rem;
    background:      var(--color-bg);
}

.login-card {
    background:    var(--color-surface);
    border:        1px solid var(--color-border);
    border-radius: var(--radius-xl);
    padding:       2.5rem 2rem;
    width:         100%;
    max-width:     400px;
    box-shadow:    var(--shadow-lg);
}

.login-logo {
    text-align:    center;
    margin-bottom: 2rem;
}

.login-logo__title {
    font-size:   1.5rem;
    font-weight: 800;
    color:       var(--color-text);
    margin-top:  0.75rem;
}

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

/* ================================================================
   Breadcrumb
   ================================================================ */
.breadcrumb {
    display:     flex;
    align-items: center;
    flex-wrap:   wrap;
    gap:         0.25rem;
    font-size:   0.875rem;
    color:       var(--color-text-muted);
    margin-bottom: 1rem;
}

.breadcrumb__item { cursor: pointer; }
.breadcrumb__item:hover { color: var(--color-primary); }
.breadcrumb__sep  { color: var(--color-border); }

/* ================================================================
   Badge / Etiquetas
   ================================================================ */
.badge {
    display:       inline-flex;
    align-items:   center;
    padding:       0.2rem 0.55rem;
    border-radius: var(--radius-full);
    font-size:     0.75rem;
    font-weight:   600;
    line-height:   1;
}

.badge-primary { background: rgba(37,99,235,0.15); color: var(--color-primary); }
.badge-success { background: rgba(22,163,74,0.15);  color: var(--color-success); }
.badge-warning { background: rgba(217,119,6,0.15);  color: var(--color-warning); }
.badge-danger  { background: rgba(220,38,38,0.15);  color: var(--color-danger);  }

/* ================================================================
   Toggle de tema
   ================================================================ */
.theme-toggle {
    width:         40px;
    height:        22px;
    border-radius: var(--radius-full);
    background:    var(--color-surface-2);
    border:        1px solid var(--color-border);
    position:      relative;
    cursor:        pointer;
    transition:    background var(--transition);
}

.theme-toggle::after {
    content:       '';
    position:      absolute;
    top:           2px;
    left:          2px;
    width:         16px;
    height:        16px;
    border-radius: 50%;
    background:    var(--color-primary);
    transition:    transform var(--transition);
}

[data-theme="dark"] .theme-toggle::after,
.dark .theme-toggle::after {
    transform: translateX(18px);
}

/* ================================================================
   Responsive
   ================================================================ */
@media (max-width: 768px) {
    .app-layout {
        grid-template-columns: 1fr;
        grid-template-areas:
            "navbar"
            "main";
    }

    .sidebar {
        position:   fixed;
        left:       0;
        top:        var(--navbar-height);
        z-index:    200;
        transform:  translateX(-100%);
        height:     calc(100vh - var(--navbar-height));
        width:      var(--sidebar-width);
        box-shadow: var(--shadow-lg);
    }

    .sidebar.open { transform: translateX(0); }

    .main-content { padding: 1rem; }

    .metrics-grid  { grid-template-columns: 1fr 1fr; }
    .charts-grid   { grid-template-columns: 1fr; }

    .file-list-table th:nth-child(3),
    .file-list-table td:nth-child(3),
    .file-list-table th:nth-child(4),
    .file-list-table td:nth-child(4) { display: none; }

    .modal { max-width: 100%; }
}

@media (max-width: 480px) {
    .metrics-grid  { grid-template-columns: 1fr; }
    .login-card    { padding: 1.5rem 1rem; }
}

/* ================================================================
   Utilidades
   ================================================================ */
.sr-only {
    position: absolute; width: 1px; height: 1px;
    padding: 0; margin: -1px; overflow: hidden;
    clip: rect(0,0,0,0); white-space: nowrap; border: 0;
}

.text-muted    { color: var(--color-text-muted); }
.text-sm       { font-size: 0.875rem; }
.text-xs       { font-size: 0.75rem; }
.font-mono     { font-family: var(--font-mono); }
.flex          { display: flex; }
.flex-col      { flex-direction: column; }
.items-center  { align-items: center; }
.justify-between { justify-content: space-between; }
.gap-1         { gap: 0.25rem; }
.gap-2         { gap: 0.5rem; }
.gap-3         { gap: 0.75rem; }
.gap-4         { gap: 1rem; }
.mt-1          { margin-top: 0.25rem; }
.mt-2          { margin-top: 0.5rem; }
.mt-3          { margin-top: 0.75rem; }
.mt-4          { margin-top: 1rem; }
.mb-4          { margin-bottom: 1rem; }
.p-4           { padding: 1rem; }
.w-full        { width: 100%; }
.truncate      { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.cursor-pointer { cursor: pointer; }
.hidden        { display: none !important; }
.loading {
    display:         inline-block;
    width:           18px;
    height:          18px;
    border:          2px solid var(--color-border);
    border-top-color: var(--color-primary);
    border-radius:   50%;
    animation:       spin 0.6s linear infinite;
}

@keyframes spin { to { transform: rotate(360deg); } }
