/* ===== TOKENS ===== */
:root {
    --bg-app:      #f5f4ef;
    --bg-surface:  #ffffff;
    --bg-hover:    #f0f0f0;
    --primary:     #2563eb;
    --primary-h:   #1d4ed8;
    --primary-lt:  #eff6ff;
    --danger:      #dc2626;
    --danger-lt:   #fef2f2;
    --text-main:   #1f2937;
    --text-muted:  #6b7280;
    --text-light:  #9ca3af;
    --border:      #e5e7eb;
    --border-dark: #d1d5db;
    --shadow-sm:   0 1px 3px rgba(0,0,0,.08);
    --shadow-md:   0 4px 12px rgba(0,0,0,.10);
    --radius:      8px;
    --radius-lg:   12px;
}

/* ===== RESET ===== */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }
html { font-size: 16px; -webkit-text-size-adjust: 100%; }
body {
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', system-ui, sans-serif;
    background: var(--bg-app);
    color: var(--text-main);
    line-height: 1.5;
    min-height: 100vh;
}
a { color: var(--primary); text-decoration: none; }
img { max-width: 100%; }
button { cursor: pointer; font-family: inherit; }

/* ===== BUTTONS ===== */
.btn {
    display: inline-flex;
    align-items: center;
    gap: 6px;
    padding: 8px 16px;
    border-radius: var(--radius);
    font-size: .875rem;
    font-weight: 500;
    border: none;
    transition: background .15s, box-shadow .15s;
    white-space: nowrap;
}
.btn-primary { background: var(--primary); color: #fff; }
.btn-primary:hover { background: var(--primary-h); }
.btn-ghost {
    background: transparent;
    color: var(--text-muted);
    border: 1px solid var(--border);
}
.btn-ghost:hover { background: var(--bg-hover); color: var(--text-main); }
.btn-danger { background: var(--danger); color: #fff; }
.btn-danger:hover { background: #b91c1c; }
.btn-icon {
    padding: 6px 8px;
    background: transparent;
    border: none;
    border-radius: 6px;
    color: var(--text-muted);
    font-size: 1rem;
    line-height: 1;
}
.btn-icon:hover { background: var(--bg-hover); color: var(--text-main); }
.btn-full { width: 100%; justify-content: center; }
.btn-sm { padding: 5px 10px; font-size: .8rem; }

/* ===== FORMS ===== */
.field { margin-bottom: 16px; }
.field label {
    display: block;
    font-size: .875rem;
    font-weight: 500;
    margin-bottom: 6px;
    color: var(--text-main);
}
input[type="text"],
input[type="email"],
input[type="password"],
textarea,
select {
    width: 100%;
    padding: 9px 12px;
    border: 1px solid var(--border-dark);
    border-radius: var(--radius);
    font-size: .9rem;
    font-family: inherit;
    color: var(--text-main);
    background: var(--bg-surface);
    transition: border-color .15s, box-shadow .15s;
}
input:focus, textarea:focus, select:focus {
    outline: none;
    border-color: var(--primary);
    box-shadow: 0 0 0 3px rgba(37,99,235,.15);
}
textarea { resize: vertical; min-height: 80px; }

/* ===== ALERTS ===== */
.alert {
    padding: 10px 14px;
    border-radius: var(--radius);
    font-size: .875rem;
    margin-bottom: 16px;
}
.alert-error { background: var(--danger-lt); color: var(--danger); border: 1px solid #fca5a5; }
.alert-success { background: #f0fdf4; color: #15803d; border: 1px solid #86efac; }

/* ===== MODAL ===== */
.modal-backdrop {
    position: fixed; inset: 0;
    background: rgba(0,0,0,.4);
    display: flex; align-items: center; justify-content: center;
    z-index: 1000;
    opacity: 0; pointer-events: none;
    transition: opacity .2s;
}
.modal-backdrop.open { opacity: 1; pointer-events: all; }
.modal {
    background: var(--bg-surface);
    border-radius: var(--radius-lg);
    padding: 24px;
    width: 100%; max-width: 420px;
    box-shadow: var(--shadow-md);
    transform: translateY(8px);
    transition: transform .2s;
}
.modal-backdrop.open .modal { transform: translateY(0); }
.modal-title {
    font-size: 1.1rem;
    font-weight: 600;
    margin-bottom: 20px;
    color: var(--text-main);
}
.modal-actions {
    display: flex;
    gap: 8px;
    justify-content: flex-end;
    margin-top: 20px;
}

/* ===== LOGIN ===== */
.login-body {
    display: flex;
    align-items: center;
    justify-content: center;
    min-height: 100vh;
    background: var(--bg-app);
}
.login-wrap {
    background: var(--bg-surface);
    border-radius: var(--radius-lg);
    padding: 36px;
    width: 100%; max-width: 380px;
    box-shadow: var(--shadow-md);
}
.login-logo {
    text-align: center;
    margin-bottom: 28px;
}
.login-logo svg { display: block; margin: 0 auto 12px; }
.login-logo h1 { font-size: 1.5rem; font-weight: 700; color: var(--text-main); }
.login-logo p { color: var(--text-muted); font-size: .9rem; margin-top: 4px; }
.login-form .btn-primary { margin-top: 4px; padding: 12px; font-size: 1rem; }

/* ===== APP HEADER ===== */
.app-header {
    height: 56px;
    background: var(--bg-surface);
    border-bottom: 1px solid var(--border);
    display: flex;
    align-items: center;
    padding: 0 20px;
    gap: 12px;
    position: sticky; top: 0;
    z-index: 100;
    box-shadow: var(--shadow-sm);
}
.app-header .logo {
    display: flex; align-items: center; gap: 8px;
    font-weight: 700; font-size: 1.05rem; color: var(--text-main);
    text-decoration: none;
}
.app-header .logo svg { flex-shrink: 0; }
.app-header .spacer { flex: 1; }
.app-header .user-info {
    font-size: .85rem; color: var(--text-muted);
    display: flex; align-items: center; gap: 10px;
}

/* ===== PANEL LAYOUT ===== */
.panel-layout {
    display: flex;
    height: calc(100vh - 56px);
    overflow: hidden;
}

/* SIDEBAR */
.sidebar {
    width: 240px;
    flex-shrink: 0;
    background: var(--bg-surface);
    border-right: 1px solid var(--border);
    display: flex;
    flex-direction: column;
    overflow: hidden;
}
.sidebar-header {
    padding: 16px;
    border-bottom: 1px solid var(--border);
    display: flex;
    align-items: center;
    gap: 8px;
}
.sidebar-title {
    font-size: .8rem;
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: .05em;
    color: var(--text-muted);
    flex: 1;
}
.sidebar-list {
    flex: 1;
    overflow-y: auto;
    padding: 8px 0;
}
.folder-item {
    display: flex;
    align-items: center;
    gap: 10px;
    padding: 9px 16px;
    cursor: pointer;
    transition: background .12s;
    border-radius: 0;
    font-size: .9rem;
    color: var(--text-main);
    position: relative;
}
.folder-item:hover { background: var(--bg-hover); }
.folder-item.active { background: var(--primary-lt); color: var(--primary); font-weight: 500; }
.folder-dot {
    width: 8px; height: 8px; border-radius: 50%;
    flex-shrink: 0;
}
.folder-name { flex: 1; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.folder-count {
    font-size: .75rem;
    color: var(--text-light);
    background: var(--bg-app);
    border-radius: 10px;
    padding: 1px 7px;
}
.folder-item.active .folder-count { background: #dbeafe; color: #1d4ed8; }

/* MAIN CONTENT */
.main-content {
    flex: 1;
    overflow-y: auto;
    padding: 24px;
    background: var(--bg-app);
}
.main-toolbar {
    display: flex;
    align-items: center;
    gap: 12px;
    margin-bottom: 20px;
}
.main-toolbar h2 {
    font-size: 1.1rem;
    font-weight: 600;
    flex: 1;
}
.section-title {
    font-size: .8rem;
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: .05em;
    color: var(--text-muted);
    margin-bottom: 12px;
}

/* PDF GRID */
.pdf-grid {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(160px, 1fr));
    gap: 16px;
}
.pdf-card {
    background: var(--bg-surface);
    border-radius: var(--radius-lg);
    overflow: hidden;
    box-shadow: var(--shadow-sm);
    cursor: pointer;
    transition: box-shadow .15s, transform .15s;
    border: 1px solid var(--border);
    position: relative;
}
.pdf-card:hover {
    box-shadow: var(--shadow-md);
    transform: translateY(-2px);
}
.pdf-thumb {
    height: 120px;
    background: linear-gradient(135deg, #e0e7ff 0%, #f0f4ff 100%);
    display: flex;
    align-items: center;
    justify-content: center;
    color: var(--primary);
    font-size: 2.5rem;
}
.pdf-info { padding: 10px 12px; }
.pdf-name {
    font-size: .85rem;
    font-weight: 500;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    color: var(--text-main);
    margin-bottom: 3px;
}
.pdf-meta {
    font-size: .75rem;
    color: var(--text-light);
}
.pdf-card-menu {
    position: absolute;
    top: 8px; right: 8px;
    background: rgba(255,255,255,.9);
    border-radius: 6px;
    padding: 2px;
    display: none;
}
.pdf-card:hover .pdf-card-menu { display: flex; gap: 2px; }

/* EMPTY STATE */
.empty-state {
    text-align: center;
    padding: 60px 20px;
    color: var(--text-muted);
}
.empty-state svg { margin-bottom: 16px; opacity: .3; }
.empty-state p { font-size: 1rem; margin-bottom: 6px; }
.empty-state small { font-size: .85rem; }

/* ===== LECTOR LAYOUT ===== */
.lector-wrap {
    display: flex;
    flex-direction: column;
    height: 100vh;
    overflow: hidden;
}
.lector-header {
    height: 52px;
    background: var(--bg-surface);
    border-bottom: 1px solid var(--border);
    display: flex;
    align-items: center;
    padding: 0 16px;
    gap: 12px;
    flex-shrink: 0;
    box-shadow: var(--shadow-sm);
}
.lector-title {
    font-size: .95rem;
    font-weight: 600;
    flex: 1;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}
.page-indicator {
    font-size: .85rem;
    color: var(--text-muted);
    white-space: nowrap;
}
.lector-body {
    display: flex;
    flex: 1;
    overflow: hidden;
}

/* PDF PANEL */
.pdf-panel {
    flex: 60;
    background: #525659;
    display: flex;
    flex-direction: column;
    overflow: hidden;
    position: relative;
}
.pdf-toolbar {
    height: 44px;
    background: #3c3f41;
    display: flex;
    align-items: center;
    gap: 10px;
    flex-shrink: 0;
    padding: 0 14px;
}
.pdf-toolbar button {
    background: rgba(255,255,255,.12);
    border: none;
    color: #fff;
    border-radius: 6px;
    padding: 5px 14px;
    font-size: 1rem;
    font-weight: 600;
    cursor: pointer;
    transition: background .12s;
    line-height: 1;
}
.pdf-toolbar button:hover { background: rgba(255,255,255,.22); }
.pdf-page-label { color: rgba(255,255,255,.5); font-size: .82rem; }
.pdf-page-num {
    color: #fff;
    font-size: .95rem;
    min-width: 24px;
    text-align: center;
    cursor: pointer;
    padding: 2px 6px;
    border-radius: 4px;
    border: 1px solid transparent;
    transition: background .12s, border-color .12s;
    user-select: none;
}
.pdf-page-num:hover {
    background: rgba(255,255,255,.15);
    border-color: rgba(255,255,255,.25);
}
.pdf-page-total { color: rgba(255,255,255,.5); font-size: .82rem; }

/* Navegación inline a página */
.pdf-nav-input {
    display: none;
    width: 52px;
    text-align: center;
    background: rgba(255,255,255,.15);
    border: 1px solid rgba(255,255,255,.4);
    border-radius: 4px;
    color: #fff;
    font-size: .88rem;
    padding: 3px 6px;
    -moz-appearance: textfield;
}
.pdf-nav-input::-webkit-inner-spin-button,
.pdf-nav-input::-webkit-outer-spin-button { -webkit-appearance: none; }
.pdf-nav-input:focus { outline: none; background: rgba(255,255,255,.25); }
.pdf-nav-go {
    display: none;
    background: var(--primary);
    border: none;
    color: #fff;
    border-radius: 5px;
    padding: 3px 10px;
    font-size: .8rem;
    font-weight: 600;
    cursor: pointer;
    transition: background .12s;
}
.pdf-nav-go:hover { background: var(--primary-h); }

/* Scroll continuo */
.pdf-scroll-area {
    flex: 1;
    overflow: auto;
    background: #525659;
}
#pdfContainer {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 16px;
    padding: 20px;
    min-width: fit-content;
}
.pdf-page-wrapper {
    background: #fff;
    box-shadow: 0 3px 18px rgba(0,0,0,.55);
    flex-shrink: 0;
    overflow: hidden;
    display: flex;
    align-items: center;
    justify-content: center;
}
.pdf-page-wrapper canvas {
    display: block;
}
.pdf-loading {
    position: absolute; inset: 0;
    display: flex; flex-direction: column;
    align-items: center; justify-content: center;
    color: #ccc; gap: 12px;
}
.spinner {
    width: 36px; height: 36px;
    border: 3px solid rgba(255,255,255,.2);
    border-top-color: var(--primary);
    border-radius: 50%;
    animation: spin .7s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

/* NOTES PANEL */
.notes-panel {
    flex: 40;
    background: #fffef7;
    border-left: 1px solid var(--border);
    display: flex;
    flex-direction: column;
    overflow: hidden;
}
.notes-header {
    padding: 12px 16px;
    border-bottom: 1px solid var(--border);
    display: flex;
    align-items: center;
    gap: 8px;
    flex-shrink: 0;
}
.notes-header-title {
    font-size: .875rem;
    font-weight: 600;
    flex: 1;
    color: var(--text-main);
}
.btn-add-note {
    width: 28px;
    height: 28px;
    border-radius: 50%;
    border: none;
    background: var(--primary);
    color: #fff;
    font-size: 1.2rem;
    line-height: 28px;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    flex-shrink: 0;
    padding: 0;
    transition: background .15s, transform .2s;
    user-select: none;
}
.btn-add-note span {
    display: block;
    line-height: 1;
    margin-top: -1px;
}
.btn-add-note:hover { background: var(--primary-h); }
.btn-add-note.is-open { transform: rotate(45deg); }

.notes-add-area {
    padding: 12px 16px;
    border-bottom: 1px solid var(--border);
    background: var(--bg-surface);
    flex-shrink: 0;
    max-height: 0;
    overflow: hidden;
    transition: max-height .25s ease, padding .25s ease;
    padding-top: 0;
    padding-bottom: 0;
}
.notes-add-area.is-open {
    max-height: 200px;
    padding-top: 12px;
    padding-bottom: 12px;
}
.notes-add-area textarea {
    background: #fffef7;
    border: 1px solid var(--border-dark);
    font-size: .85rem;
    min-height: 70px;
    resize: none;
    transition: border-color .15s, height .15s;
}
.notes-add-footer {
    display: flex;
    align-items: center;
    justify-content: space-between;
    margin-top: 8px;
    gap: 8px;
}
.page-badge {
    font-size: .75rem;
    background: var(--primary-lt);
    color: var(--primary);
    border-radius: 10px;
    padding: 2px 9px;
    font-weight: 500;
}
.notes-list {
    flex: 1;
    min-height: 0;
    overflow-y: auto;
    padding: 12px;
    display: flex;
    flex-direction: column;
    gap: 10px;
}
.note-card {
    background: var(--bg-surface);
    border-radius: var(--radius);
    border: 1px solid var(--border);
    border-left: 3px solid var(--primary);
    padding: 10px 12px;
    position: relative;
    animation: noteIn .2s ease;
}
@keyframes noteIn {
    from { opacity: 0; transform: translateY(-6px); }
    to   { opacity: 1; transform: translateY(0); }
}
.note-meta {
    display: flex;
    align-items: center;
    gap: 8px;
    margin-bottom: 6px;
}
.note-page-badge {
    font-size: .7rem;
    background: var(--primary-lt);
    color: var(--primary);
    border-radius: 6px;
    padding: 2px 8px;
    font-weight: 600;
    cursor: pointer;
    transition: background .12s;
}
.note-page-badge:hover { background: #dbeafe; }
.note-time { font-size: .72rem; color: var(--text-light); }
.note-actions { margin-left: auto; display: flex; gap: 2px; }
.note-text {
    font-size: .85rem;
    color: var(--text-main);
    line-height: 1.55;
    white-space: pre-wrap;
    word-break: break-word;
}
.note-edit-input {
    font-size: .85rem;
    min-height: 60px;
    width: 100%;
    margin-top: 6px;
}
.notes-export {
    padding: 10px 12px;
    border-top: 1px solid var(--border);
    display: flex;
    gap: 8px;
    flex-shrink: 0;
    background: var(--bg-surface);
}
.notes-export .btn { flex: 1; justify-content: center; font-size: .78rem; padding: 6px 8px; }
.notes-empty {
    text-align: center;
    padding: 40px 16px;
    color: var(--text-light);
    font-size: .875rem;
}
.notes-empty svg { margin-bottom: 10px; opacity: .25; }

/* ===== FAB + BACKDROP (sólo móvil, ocultos por defecto) ===== */
.fab-notes {
    display: none;
    position: fixed;
    bottom: 20px; right: 20px;
    z-index: 300;
    width: 52px; height: 52px;
    border-radius: 50%;
    background: var(--primary);
    color: #fff;
    border: none;
    cursor: pointer;
    box-shadow: 0 4px 16px rgba(0,0,0,.3);
    align-items: center;
    justify-content: center;
    transition: background .15s, transform .15s;
    user-select: none;
}
.fab-notes:active { transform: scale(0.9); }
.fab-badge {
    position: absolute;
    top: -2px; right: -2px;
    min-width: 18px; height: 18px;
    background: #ef4444;
    color: #fff;
    border-radius: 9px;
    font-size: .65rem;
    font-weight: 700;
    display: none;
    align-items: center;
    justify-content: center;
    padding: 0 4px;
    line-height: 1;
}
.fab-badge.visible { display: flex; }

.notes-backdrop {
    display: none;
    position: fixed;
    inset: 0;
    background: rgba(0,0,0,.38);
    z-index: 199;
    cursor: pointer;
}
.notes-backdrop.visible { display: block; }

.btn-notes-close {
    display: none;
    width: 28px; height: 28px;
    border: none;
    background: transparent;
    color: var(--text-muted);
    cursor: pointer;
    border-radius: var(--radius);
    align-items: center;
    justify-content: center;
    flex-shrink: 0;
    padding: 0;
    transition: background .12s;
}
.btn-notes-close:hover { background: var(--bg-app); }

/* ===== RESPONSIVE ===== */
@media (max-width: 768px), (orientation: landscape) and (max-height: 500px) {
    /* ---- Panel principal ---- */
    .panel-layout { flex-direction: column; height: auto; overflow: visible; }
    .sidebar {
        width: 100%;
        border-right: none;
        border-bottom: 1px solid var(--border);
        max-height: 180px;
    }
    .sidebar-list { display: flex; flex-direction: row; overflow-x: auto; padding: 8px; gap: 6px; flex-wrap: nowrap; }
    .folder-item  { padding: 7px 12px; border-radius: var(--radius); white-space: nowrap; flex-shrink: 0; }
    .main-content { padding: 16px; }
    .pdf-grid { grid-template-columns: repeat(auto-fill, minmax(140px, 1fr)); gap: 12px; }

    /* ---- Lector: PDF a pantalla completa, notas como overlay ---- */
    .lector-body {
        height: calc(100vh - 52px);
    }
    .pdf-panel {
        flex: 1;
        height: 100%;
    }

    /* Panel de notas: drawer desde la derecha */
    .notes-panel {
        position: fixed;
        top: 52px; right: 0; bottom: 0;
        width: 85vw;
        max-width: 360px;
        height: auto;
        flex: none;
        transform: translateX(105%);
        transition: transform .3s cubic-bezier(0.4, 0, 0.2, 1);
        z-index: 200;
        box-shadow: -6px 0 28px rgba(0,0,0,.22);
        border-left: 1px solid var(--border);
        border-top: none;
        display: flex;
        flex-direction: column;
        overflow: hidden;
        min-height: 0;
    }
    .notes-panel.is-visible { transform: translateX(0); }

    /* Botón cerrar visible en móvil */
    .btn-notes-close { display: flex; }

    /* Scroll notas en iOS/Android */
    .notes-list {
        min-height: 0;
        -webkit-overflow-scrolling: touch;
        overflow-y: scroll;
        touch-action: pan-y;
        overscroll-behavior: contain;
    }

    /* Toolbar compacto en móvil */
    .pdf-toolbar { height: 38px; padding: 0 10px; gap: 8px; }
    .pdf-toolbar button { padding: 4px 10px; font-size: .9rem; }

    /* Notes área compacta */
    .notes-add-area.is-open { max-height: 160px; padding: 8px 10px; }
    .notes-add-area textarea { min-height: 44px; font-size: .82rem; }
    .note-card { padding: 8px 10px; }
    .notes-export { padding: 6px 8px; gap: 6px; }
    .notes-export .btn { font-size: .74rem; padding: 5px 6px; }

    /* FAB visible en móvil */
    .fab-notes { display: flex; }
}

@media (max-width: 480px) {
    .lector-header { padding: 0 10px; gap: 8px; }
    .lector-title  { font-size: .85rem; }
    .page-indicator { font-size: .78rem; }
}

/* ===== COLOR PICKER ===== */
.color-picker { display: flex; gap: 8px; flex-wrap: wrap; margin-top: 8px; }
.color-swatch {
    width: 28px; height: 28px; border-radius: 50%;
    cursor: pointer; border: 3px solid transparent;
    transition: transform .12s;
}
.color-swatch:hover { transform: scale(1.15); }
.color-swatch.selected { border-color: var(--text-main); }

/* ===== UPLOAD AREA ===== */
.upload-zone {
    border: 2px dashed var(--border-dark);
    border-radius: var(--radius-lg);
    padding: 32px;
    text-align: center;
    color: var(--text-muted);
    cursor: pointer;
    transition: border-color .15s, background .15s;
    background: var(--bg-surface);
    margin-bottom: 20px;
}
.upload-zone:hover, .upload-zone.dragover {
    border-color: var(--primary);
    background: var(--primary-lt);
    color: var(--primary);
}
.upload-zone svg { display: block; margin: 0 auto 10px; opacity: .4; }
.upload-zone p { font-size: .9rem; }
.upload-zone small { font-size: .8rem; margin-top: 4px; display: block; }
