:root {
    --primary-color: #6366f1;
    --secondary-color: #a855f7;
    --accent-color: #22d3ee;
    --text-color: #1e293b;
    --text-light: #64748b;
    --bg-light: #f1f5f9;
    --glass-bg: rgba(255, 255, 255, 0.7);
    --glass-border: rgba(255, 255, 255, 0.4);
    --glass-shadow: rgba(0, 0, 0, 0.05);
    --white: #ffffff;
    --error: #ef4444;
    --success: #10b981;
    --warning: #f59e0b;
}

@media (prefers-color-scheme: dark) {
    :root {
        --bg-light: #0f172a;
        --text-color: #f8fafc;
        --text-light: #94a3b8;
        --glass-bg: rgba(30, 41, 59, 0.7);
        --glass-border: rgba(255, 255, 255, 0.1);
        --glass-shadow: rgba(0, 0, 0, 0.3);
    }
}

* {
    box-sizing: border-box;
}

/* Skip link for keyboard / screen reader users */
.skip-link {
    position: absolute;
    left: -9999px;
    top: 0;
    background: var(--primary-color);
    color: #fff;
    padding: 12px 18px;
    border-radius: 0 0 12px 0;
    font-weight: 700;
    z-index: 2000;
    text-decoration: none;
}
.skip-link:focus {
    left: 0;
    outline: 3px solid var(--accent-color);
    outline-offset: 2px;
}

/* Visually hidden helper (for screen-reader-only labels) */
.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;
}

/* Visible, high-contrast focus indicator (WCAG 2.2 SC 2.4.11) */
a:focus-visible,
button:focus-visible,
[role="button"]:focus-visible,
summary:focus-visible {
    outline: 3px solid var(--accent-color);
    outline-offset: 2px;
    border-radius: 6px;
}

/* Respect users who prefer reduced motion (WCAG 2.3.3) */
@media (prefers-reduced-motion: reduce) {
    *, *::before, *::after {
        animation-duration: 0.001ms !important;
        animation-iteration-count: 1 !important;
        transition-duration: 0.001ms !important;
        scroll-behavior: auto !important;
    }
    .blob { animation: none !important; }
}

body {
    font-family: 'Inter', system-ui, -apple-system, sans-serif;
    background-color: var(--bg-light);
    color: var(--text-color);
    margin: 0;
    padding: 0;
    min-height: 100vh;
    overflow-x: hidden;
    position: relative;
}

/* Liquid Background Elements */
.liquid-bg {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    z-index: -1;
    overflow: hidden;
    background: linear-gradient(135deg, var(--bg-light) 0%, #e2e8f0 100%);
}

@media (prefers-color-scheme: dark) {
    .liquid-bg {
        background: linear-gradient(135deg, #0f172a 0%, #1e1b4b 100%);
    }
}

.blob {
    position: absolute;
    width: 500px;
    height: 500px;
    background: linear-gradient(135deg, var(--primary-color), var(--secondary-color));
    filter: blur(80px);
    border-radius: 50%;
    opacity: 0.4;
    animation: move 20s infinite alternate;
}

.blob:nth-child(2) {
    background: linear-gradient(135deg, var(--secondary-color), var(--accent-color));
    width: 400px;
    height: 400px;
    left: 50%;
    top: 20%;
    animation-delay: -5s;
    animation-duration: 25s;
}

.blob:nth-child(3) {
    background: linear-gradient(135deg, var(--accent-color), var(--primary-color));
    width: 600px;
    height: 600px;
    right: -10%;
    bottom: -10%;
    animation-delay: -10s;
    animation-duration: 30s;
}

@keyframes move {
    from { transform: translate(0, 0) scale(1); }
    to { transform: translate(100px, 100px) scale(1.2); }
}

header {
    background: transparent;
    padding: 2rem 0;
    text-align: center;
}

header h1 {
    margin: 0;
    font-size: 2.5rem;
    font-weight: 800;
    /* Fallback solid color for forced-colors / non-supporting browsers */
    color: var(--primary-color);
    background: linear-gradient(to right, var(--primary-color), var(--secondary-color));
    -webkit-background-clip: text;
    background-clip: text;
    -webkit-text-fill-color: transparent;
    letter-spacing: -1px;
}

@media (forced-colors: active) {
    header h1 {
        -webkit-text-fill-color: currentColor;
        color: CanvasText;
        background: none;
    }
}

nav {
    display: flex;
    justify-content: center;
    align-items: center;
    flex-wrap: wrap;
    row-gap: 4px;
    max-width: 800px;
    margin: 0 auto 2rem;
    padding: 0.75rem 1.5rem;
    background: var(--glass-bg);
    backdrop-filter: blur(12px);
    -webkit-backdrop-filter: blur(12px);
    border: 1px solid var(--glass-border);
    border-radius: 20px;
    box-shadow: 0 8px 32px var(--glass-shadow);
    position: sticky;
    top: 20px;
    z-index: 1000;
}

nav a {
    color: var(--text-color);
    text-decoration: none;
    margin: 0 8px;
    font-weight: 600;
    font-size: 0.95rem;
    transition: all 0.3s;
    /* Hit area >= 44px tall (WCAG 2.5.8 Target Size) */
    padding: 10px 12px;
    min-height: 44px;
    display: inline-flex;
    align-items: center;
    border-radius: 10px;
}

nav a[aria-current="page"] {
    background: rgba(99, 102, 241, 0.15);
    color: var(--primary-color);
}

nav a:hover {
    background: rgba(255, 255, 255, 0.1);
    color: var(--primary-color);
}

main {
    max-width: 1200px;
    margin: 0 auto 40px;
    padding: 40px;
    background: var(--glass-bg);
    backdrop-filter: blur(12px);
    -webkit-backdrop-filter: blur(12px);
    border: 1px solid var(--glass-border);
    border-radius: 24px;
    box-shadow: 0 20px 50px var(--glass-shadow);
}

.alert {
    padding: 16px 20px;
    margin-bottom: 30px;
    border-radius: 16px;
    font-weight: 600;
    display: flex;
    align-items: center;
    backdrop-filter: blur(8px);
    -webkit-backdrop-filter: blur(8px);
}

.alert-error {
    background: rgba(239, 68, 68, 0.15);
    color: #ef4444;
    border: 1px solid rgba(239, 68, 68, 0.2);
}

.alert-success {
    background: rgba(16, 185, 129, 0.15);
    color: #10b981;
    border: 1px solid rgba(16, 185, 129, 0.2);
}

footer {
    text-align: center;
    padding: 40px;
    font-size: 0.9rem;
    color: var(--text-light);
}

.btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    padding: 12px 24px;
    border-radius: 14px;
    text-decoration: none;
    font-weight: 700;
    cursor: pointer;
    border: 1px solid transparent;
    transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
    gap: 8px;
}

.btn-sm {
    padding: 10px 16px;
    font-size: 0.85rem;
    border-radius: 10px;
    min-height: 36px;
}

.btn {
    /* Ensure interactive btns satisfy 24x24 minimum (SC 2.5.8) */
    min-height: 44px;
}

.btn:hover {
    transform: translateY(-2px);
    box-shadow: 0 10px 20px rgba(0,0,0,0.1);
}

.btn:active {
    transform: translateY(0);
}

.btn-primary {
    background: linear-gradient(135deg, var(--primary-color), var(--secondary-color));
    color: white;
    box-shadow: 0 4px 15px rgba(99, 102, 241, 0.3);
}

.btn-secondary {
    background: var(--glass-bg);
    color: var(--text-color);
    border-color: var(--glass-border);
    backdrop-filter: blur(4px);
}

.btn-danger {
    background: rgba(239, 68, 68, 0.1);
    color: #ef4444;
    border-color: rgba(239, 68, 68, 0.2);
}

.btn-danger:hover {
    background: #ef4444;
    color: white;
}

/* Book Grid & Cards */
.book-grid {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(280px, 1fr));
    gap: 30px;
}

.book-card {
    background: var(--glass-bg);
    backdrop-filter: blur(8px);
    -webkit-backdrop-filter: blur(8px);
    border: 1px solid var(--glass-border);
    border-radius: 20px;
    padding: 24px;
    transition: all 0.4s cubic-bezier(0.175, 0.885, 0.32, 1.275);
    display: flex;
    flex-direction: column;
    height: 100%;
}

.book-card:hover {
    transform: translateY(-10px) scale(1.02);
    box-shadow: 0 30px 60px var(--glass-shadow);
    background: rgba(255, 255, 255, 0.25);
}

@media (prefers-color-scheme: dark) {
    .book-card:hover {
        background: rgba(255, 255, 255, 0.05);
    }
}

.book-card img {
    width: 100%;
    height: 320px;
    object-fit: cover;
    border-radius: 16px;
    margin-bottom: 20px;
    box-shadow: 0 10px 20px rgba(0,0,0,0.2);
}

.book-card-no-cover {
    width: 100%;
    height: 320px;
    border-radius: 16px;
    margin-bottom: 20px;
    display: flex;
    align-items: center;
    justify-content: center;
    background: var(--glass-bg);
    border: 1px dashed var(--border);
    color: var(--text-light);
    font-size: 0.85rem;
}

.search-source-badge {
    display: inline-block;
    background: rgba(99, 102, 241, 0.12);
    color: var(--primary);
    border-radius: 20px;
    padding: 2px 10px;
    font-size: 0.75rem;
    font-weight: 600;
    letter-spacing: 0.02em;
    margin-bottom: 10px;
}

.book-card h3 {
    margin: 0 0 10px;
    font-size: 1.25rem;
    font-weight: 700;
    line-height: 1.3;
}

.book-card p {
    color: var(--text-light);
    margin: 0 0 15px;
    font-size: 0.95rem;
}

/* Forms */
.form-control, input[type="text"], input[type="number"], input[type="date"], select, textarea {
    width: 100%;
    padding: 12px 16px;
    background: var(--glass-bg);
    border: 1px solid var(--glass-border);
    border-radius: 12px;
    color: var(--text-color);
    font-size: 1rem;
    transition: all 0.3s;
    outline: none;
}

.form-control:focus,
select:focus,
textarea:focus {
    border-color: var(--primary-color);
    box-shadow: 0 0 0 4px rgba(99, 102, 241, 0.1);
    background: rgba(255, 255, 255, 0.85);
}

@media (prefers-color-scheme: dark) {
    .form-control:focus,
    select:focus,
    textarea:focus {
        background: rgba(30, 41, 59, 0.9);
    }
}

/* Select: quita el estilo nativo del navegador, añade chevron propio */
select {
    -webkit-appearance: none;
    -moz-appearance: none;
    appearance: none;
    background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='16' height='16' viewBox='0 0 24 24' fill='none' stroke='%2364748b' stroke-width='2.5' stroke-linecap='round' stroke-linejoin='round'%3E%3Cpolyline points='6 9 12 15 18 9'%3E%3C/polyline%3E%3C/svg%3E");
    background-repeat: no-repeat;
    background-position: right 14px center;
    background-color: var(--glass-bg);
    padding-right: 42px;
    cursor: pointer;
}

/* Options: el desplegable nativo se estiliza con colores del sistema de diseño */
select option {
    background: #f1f5f9;
    color: #1e293b;
    font-weight: 500;
}

@media (prefers-color-scheme: dark) {
    select {
        background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='16' height='16' viewBox='0 0 24 24' fill='none' stroke='%2394a3b8' stroke-width='2.5' stroke-linecap='round' stroke-linejoin='round'%3E%3Cpolyline points='6 9 12 15 18 9'%3E%3C/polyline%3E%3C/svg%3E");
        background-color: rgba(30, 41, 59, 0.7);
    }
    select option {
        background: #1e293b;
        color: #f8fafc;
    }
}

label {
    display: block;
    margin-bottom: 8px;
    font-weight: 600;
    font-size: 0.9rem;
    color: var(--text-light);
}

.form-group {
    margin-bottom: 24px;
}

/* Tags & Ratings */
.status-tag {
    display: inline-block;
    padding: 4px 12px;
    border-radius: 20px;
    font-size: 0.75rem;
    font-weight: 800;
    text-transform: uppercase;
    letter-spacing: 0.5px;
}

.status-leído { background: rgba(16, 185, 129, 0.15); color: #10b981; }
.status-leyendo { background: rgba(59, 130, 246, 0.15); color: #3b82f6; }
.status-pendiente { background: rgba(245, 158, 11, 0.15); color: #f59e0b; }
.status-abandonado { background: rgba(107, 114, 128, 0.15); color: #6b7280; }

.rating-display {
    font-weight: 800;
    color: #f59e0b;
    font-size: 1.1rem;
}

.rating-large {
    font-size: 2rem;
    margin: 10px 0;
}

.info-box {
    margin-top: 20px;
    padding: 20px;
    background: rgba(255, 255, 255, 0.05);
    border-radius: 16px;
    border: 1px solid var(--glass-border);
}

.observation-box {
    margin-top: 30px;
    padding: 24px;
    background: rgba(99, 102, 241, 0.05);
    border-radius: 16px;
    border-left: 4px solid var(--primary-color);
}

.book-cover-large {
    flex: 0 0 300px;
}

.book-cover-large img {
    width: 100%;
    border-radius: 20px;
    box-shadow: 0 20px 40px rgba(0,0,0,0.3);
}

.description-text {
    line-height: 1.8;
    color: var(--text-color);
    opacity: 0.9;
}

.api-source-info {
    margin-top: 40px;
    font-size: 0.8rem;
    color: var(--text-light);
    font-style: italic;
}

/* Details & Stats */
.card {
    background: var(--glass-bg);
    backdrop-filter: blur(12px);
    -webkit-backdrop-filter: blur(12px);
    border: 1px solid var(--glass-border);
    border-radius: 20px;
    padding: 24px;
    box-shadow: 0 8px 32px var(--glass-shadow);
}

.book-detail {
    display: flex;
    gap: 40px;
    animation: fadeIn 0.6s ease-out;
}

@media (max-width: 768px) {
    .book-detail { flex-direction: column; }
    .book-detail img { width: 100% !important; max-width: 300px; margin: 0 auto; }
    main { padding: 20px; border-radius: 16px; }
    nav { width: 95%; top: 10px; flex-wrap: wrap; padding: 0.5rem 0.75rem; }
    nav a { margin: 2px; font-size: 0.9rem; }
    header { padding: 1rem 0; }
    header h1 { font-size: 1.85rem; }
}

/* Intermediate breakpoint: tablet portrait — keep ranking 2-col but
   reduce the desktop padding so cards don't cramp content. */
@media (max-width: 900px) and (min-width: 769px) {
    main { padding: 28px; }
    .ranking-grid { gap: 16px; }
    .book-cover-large { flex: 0 0 220px; }
}

.stats-grid {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
    gap: 20px;
    margin-bottom: 40px;
}

.stats-card {
    text-align: center;
    padding: 30px;
}

.stats-number {
    display: block;
    font-size: 2.5rem;
    font-weight: 800;
    color: var(--primary-color);
}

.stats-label {
    color: var(--text-light);
    font-weight: 600;
}

/* Utils */
.d-flex { display: flex; }
.d-inline { display: inline; }
.d-inline-block { display: inline-block; }
.align-items-center, .align-center { align-items: center; }
.justify-between { justify-content: space-between; }
.justify-center { justify-content: center; }
.gap-10 { gap: 10px; }
.gap-20 { gap: 20px; }
.flex-1 { flex: 1; }
.flex-wrap { flex-wrap: wrap; }
.mt-0 { margin-top: 0; }
.mt-20 { margin-top: 20px; }
.mb-20 { margin-bottom: 20px; }
.text-light { color: var(--text-light); }
.text-large { font-size: 1.1rem; }
.w-full { width: 100%; }

@media (max-width: 768px) {
    .no-column-mobile { flex-direction: row !important; }
}

/* Empty States */
.empty-state {
    text-align: center;
    padding: 60px 20px;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
}

.empty-state-icon {
    font-size: 4rem;
    margin-bottom: 20px;
    filter: drop-shadow(0 0 10px rgba(99, 102, 241, 0.3));
}

.empty-state h3 {
    font-size: 1.75rem;
    margin-bottom: 10px;
}

.empty-state p {
    color: var(--text-light);
    max-width: 400px;
}

/* Stats Table */
.stats-table {
    width: 100%;
    border-collapse: collapse;
}
.stats-table td,
.stats-table tbody th {
    padding: 12px 0;
    border-bottom: 1px solid var(--glass-border);
    font-weight: 600;
    text-align: left;
}
.stats-count {
    text-align: right;
    font-weight: 800;
    color: var(--primary-color);
}

/* Pagination */
.pagination {
    display: flex;
    justify-content: center;
    align-items: center;
    gap: 20px;
    background: var(--glass-bg);
    padding: 15px;
    border-radius: 15px;
    backdrop-filter: blur(8px);
}
.pagination-info {
    font-weight: 600;
    color: var(--text-light);
}

/* Grouped List (Accordion style) */
.year-block {
    margin-bottom: 20px;
    background: var(--glass-bg);
    border: 1px solid var(--glass-border);
    border-radius: 20px;
    overflow: hidden;
    backdrop-filter: blur(8px);
}
.year-summary {
    padding: 20px 30px;
    cursor: pointer;
    display: flex;
    justify-content: space-between;
    align-items: center;
    list-style: none;
    transition: background 0.3s;
}
.year-summary::-webkit-details-marker { display: none; }
.year-summary:hover { background: rgba(255, 255, 255, 0.1); }
.year-title { font-size: 1.5rem; font-weight: 800; }
.year-meta { font-size: 0.9rem; color: var(--text-light); }
.year-body { padding: 30px; border-top: 1px solid var(--glass-border); }
.status-section-title { margin: 30px 0 15px; border-bottom: 2px solid var(--glass-border); padding-bottom: 10px; }

/* Tabs for Add Book */
.add-book-tabs details {
    margin-bottom: 15px;
    background: var(--glass-bg);
    border: 1px solid var(--glass-border);
    border-radius: 16px;
    backdrop-filter: blur(8px);
}
.add-tab-title {
    padding: 15px 20px;
    font-weight: 700;
    cursor: pointer;
    list-style: none;
}
.add-tab-body { padding: 20px; border-top: 1px solid var(--glass-border); }

/* Debug Log */
.debug-log {
    background: rgba(0,0,0,0.05);
    padding: 20px;
    border-radius: 12px;
    font-family: monospace;
    font-size: 0.85rem;
}

/* Book Cover in Edit/Details */
.book-cover-container {
    flex: 0 0 250px;
}
.book-cover-img {
    width: 100%;
    border-radius: 16px;
    box-shadow: 0 15px 30px var(--glass-shadow);
}
.form-label {
    display: block;
    margin-bottom: 8px;
    font-weight: 600;
}
.hr-spacer {
    height: 1px;
    background: var(--glass-border);
    margin: 40px 0;
}

/* Ranking Page */
.ranking-hero {
    display: flex;
    flex-wrap: wrap;
    justify-content: space-between;
    align-items: baseline;
    gap: 8px 16px;
    margin-bottom: 28px;
    padding-bottom: 16px;
    border-bottom: 1px solid var(--glass-border);
}

.ranking-section-title {
    margin: 0 0 16px;
    padding-bottom: 10px;
    border-bottom: 2px solid var(--glass-border);
    font-size: 0.8rem;
    font-weight: 700;
    text-transform: uppercase;
    letter-spacing: 0.6px;
    /* Stronger contrast than --text-light against blurred glass bg (WCAG 1.4.3) */
    color: var(--text-color);
    opacity: 0.85;
}

.ranking-grid {
    display: grid;
    grid-template-columns: repeat(2, minmax(0, 1fr));
    gap: 24px;
}

.ranking-grid + .ranking-grid {
    margin-top: 24px;
}

@media (max-width: 768px) {
    .ranking-grid { grid-template-columns: 1fr; gap: 16px; }
    .ranking-grid + .ranking-grid { margin-top: 16px; }
}

.ranking-list {
    list-style: none;
    padding: 0;
    margin: 0;
    display: flex;
    flex-direction: column;
}

.ranking-item {
    display: flex;
    align-items: center;
    gap: 12px;
    padding: 10px 0;
    border-bottom: 1px solid var(--glass-border);
    /* Avoid horizontal overflow when titles are long */
    min-width: 0;
}

.ranking-item:last-child { border-bottom: none; }

.ranking-num {
    width: 28px;
    height: 28px;
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 0.78rem;
    font-weight: 800;
    flex-shrink: 0;
    background: rgba(100, 116, 139, 0.12);
    color: var(--text-light);
}

/* Medal palette tuned for >= 4.5:1 contrast on translucent backgrounds */
.ranking-num.gold   { background: rgba(245, 158, 11, 0.22); color: #92400e; }
.ranking-num.silver { background: rgba(100, 116, 139, 0.22); color: #334155; }
.ranking-num.bronze { background: rgba(180, 110, 50, 0.22);  color: #7c2d12; }

@media (prefers-color-scheme: dark) {
    .ranking-num.gold   { color: #fcd34d; }
    .ranking-num.silver { color: #cbd5e1; }
    .ranking-num.bronze { color: #fdba74; }
}

.ranking-cover {
    width: 38px;
    height: 54px;
    object-fit: cover;
    border-radius: 6px;
    flex-shrink: 0;
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.15);
}

.ranking-info {
    flex: 1 1 auto;
    min-width: 0;
}

.ranking-info strong {
    display: block;
    font-size: 0.93rem;
    font-weight: 700;
    /* Allow up to 2 lines on small screens, ellipsis after */
    display: -webkit-box;
    -webkit-line-clamp: 2;
    -webkit-box-orient: vertical;
    overflow: hidden;
    word-break: break-word;
}

.ranking-info span {
    display: block;
    font-size: 0.8rem;
    color: var(--text-light);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.ranking-badges {
    display: flex;
    flex-direction: column;
    align-items: flex-end;
    gap: 4px;
    flex-shrink: 0;
    max-width: 40%;
    text-align: right;
}

/* Tighter ranking layout on narrow viewports */
@media (max-width: 480px) {
    .ranking-item { gap: 8px; flex-wrap: wrap; }
    .ranking-cover { width: 32px; height: 46px; }
    .ranking-num { width: 26px; height: 26px; font-size: 0.72rem; }
    .ranking-info strong { font-size: 0.88rem; }
    .ranking-badges { max-width: 100%; flex-direction: row; flex-wrap: wrap; align-items: center; gap: 6px; margin-left: 40px; }
}

.stats-table th {
    padding: 8px 0;
    font-size: 0.72rem;
    font-weight: 700;
    text-transform: uppercase;
    letter-spacing: 0.5px;
    color: var(--text-light);
    border-bottom: 2px solid var(--glass-border);
}

/* Animations */
@keyframes fadeIn {
    from { opacity: 0; transform: translateY(10px); }
    to { opacity: 1; transform: translateY(0); }
}

.card, .book-card {
    animation: fadeIn 0.5s ease-out backwards;
}

.book-card:nth-child(1) { animation-delay: 0.1s; }
.book-card:nth-child(2) { animation-delay: 0.15s; }
.book-card:nth-child(3) { animation-delay: 0.2s; }
.book-card:nth-child(4) { animation-delay: 0.25s; }
.book-card:nth-child(5) { animation-delay: 0.3s; }

/* Custom Scrollbar */
::-webkit-scrollbar {
    width: 8px;
}
::-webkit-scrollbar-track {
    background: transparent;
}
::-webkit-scrollbar-thumb {
    background: var(--primary-color);
    border-radius: 10px;
    opacity: 0.5;
}

