/* =========================================
   VARIABLES Y RESET
   ========================================= */
:root {
    --primary: #2c3e50;
    --secondary: #3498db;
    --accent: #e74c3c;
    --light: #ecf0f1;
    --bg-gradient: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
    --detail-image-bg: linear-gradient(135deg, rgba(245, 247, 250, 0.2) 0%, rgba(195, 207, 226, 0.2) 100%);
    --shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
    --shadow-hover: 0 10px 20px rgba(0, 0, 0, 0.2);
    --text-color: #333;
    --card-bg: white;
    --border-color: #e0e0e0;
    --image-bg: transparent;
    --radius: 10px;
    --transition: 0.3s ease;
    --scrollbar-track: transparent;
    --scrollbar-thumb: var(--secondary);
    --scrollbar-thumb-hover: var(--accent);
    --scrollbar-width: 8px;
}

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

body {
    font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
    background: var(--bg-gradient);
    min-height: 100vh;
    padding-bottom: 120px;
    color: var(--text-color);
}

/* =========================================
   HEADER Y NAVEGACIÓN
   ========================================= */
header {
    background: linear-gradient(to right, var(--primary), var(--secondary));
    color: white;
    padding: 1.5rem 2rem;
    display: flex;
    justify-content: space-between;
    align-items: center;
    flex-wrap: wrap;
    box-shadow: var(--shadow);
}

.logo h1 {
    font-size: 2.5rem;
    margin-bottom: 0.5rem;
}

.logo p {
    opacity: 0.8;
}

.main-nav ul {
    display: flex;
    list-style: none;
    gap: 2rem;
}

.main-nav a {
    color: white;
    text-decoration: none;
    padding: 0.5rem 1rem;
    border-radius: 20px;
    transition: all var(--transition);
}

.main-nav a:hover, .main-nav a.active {
    background: rgba(255, 255, 255, 0.2);
}

/* =========================================
   BÚSQUEDA Y TOGGLE
   ========================================= */
.search-box {
    display: flex;
    align-items: center;
    gap: 0.5rem;
    position: relative;
}

#search-input {
    padding: 0.5rem 1rem;
    border: none;
    border-radius: 20px;
    width: 250px;
}

#search-btn {
    background: var(--accent);
    color: white;
    border: none;
    padding: 0.5rem 1rem;
    border-radius: 20px;
    cursor: pointer;
}

#theme-toggle {
    background: var(--light);
    border: none;
    padding: 0.5rem 1rem;
    border-radius: 50%;
    cursor: pointer;
    font-size: 1.2rem;
    transition: transform 0.3s ease;
    margin-left: 1rem;
    box-shadow: var(--shadow);
}

#theme-toggle:hover {
    transform: scale(1.1);
}

/* =========================================
   BOTÓN BORRAR BÚSQUEDA
   ========================================= */
#clear-search {
    position: absolute;
    right: 60px;
    background: none;
    border: none;
    color: var(--text-color);
    font-size: 1.2rem;
    cursor: pointer;
    padding: 0.3rem;
    display: none;
    opacity: 0.5;
    transition: opacity 0.3s ease;
}

#clear-search:hover {
    opacity: 1;
}

#clear-search.visible {
    display: block;
}

/* =========================================
   LAYOUT PRINCIPAL
   ========================================= */
main {
    display: grid;
    grid-template-columns: 250px 1fr;
    gap: 2rem;
    padding: 2rem;
    max-width: 1400px;
    margin: 0 auto;
}

.sidebar {
    background: var(--card-bg);
    border-radius: var(--radius);
    padding: 1.5rem;
    box-shadow: var(--shadow);
    height: fit-content;
}

.filters, .stats {
    margin-bottom: 2rem;
}

.stats h3, .filters h3 {
    color: var(--primary);
    margin-bottom: 1rem;
    padding-bottom: 0.5rem;
    border-bottom: 2px solid var(--secondary);
}

.category-btn {
    display: block;
    background: var(--light);
    border: none;
    padding: 0.5rem 1rem;
    margin-bottom: 0.5rem;
    border-radius: 5px;
    cursor: pointer;
    width: 100%;
    text-align: left;
    transition: all var(--transition);
}

.category-btn:hover, .category-btn.active {
    background: var(--secondary);
    color: white;
}

/* =========================================
   GRID DE TARJETAS
   ========================================= */
.grid-container {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(280px, 1fr));
    gap: 1.5rem;
}

.content {
    display: flex;
    flex-direction: column;
    gap: 2rem;
    min-height: calc(100vh - 300px);
}

.card {
    background: var(--card-bg);
    border-radius: var(--radius);
    overflow: hidden;
    box-shadow: var(--shadow);
    transition: transform var(--transition), box-shadow var(--transition);
    cursor: pointer;
    display: flex;
    flex-direction: column;
    height: 100%;
    position: relative;
}

.card:hover {
    transform: translateY(-5px);
    box-shadow: var(--shadow-hover);
}

.card::before {
    content: '';
    position: absolute;
    top: -50%;
    left: -50%;
    width: 200%;
    height: 200%;
    background: radial-gradient(circle, rgba(52, 152, 219, 0.1) 0%, transparent 70%);
    transform: scale(0);
    transition: transform 0.5s ease;
    pointer-events: none;
}

.card:hover::before {
    transform: scale(1);
}

.card-image-container {
    width: 100%;
    height: 250px;
    overflow: hidden;
    background: #f5f5f5;
    position: relative;
}

.card-image {
    width: 100%;
    height: 100%;
    object-fit: cover;
    transition: transform var(--transition);
    opacity: 0; /* Prevent FOUT */
}

.card-image.loaded {
    opacity: 1;
}

.card:hover .card-image {
    transform: scale(1.05);
}

.card-content {
    padding: 1rem;
    flex: 1;
    display: flex;
    flex-direction: column;
}

.card h3 {
    color: var(--primary);
    margin-bottom: 0.5rem;
    font-size: 1.1rem;
}

.card-category {
    display: inline-block;
    background: var(--light);
    padding: 0.2rem 0.8rem;
    border-radius: 15px;
    font-size: 0.8rem;
    margin-bottom: 0.5rem;
    align-self: flex-start;
}

.card-description {
    color: var(--text-color);
    font-size: 0.9rem;
    line-height: 1.4;
    flex: 1;
    overflow: hidden;
    display: -webkit-box;
    -webkit-line-clamp: 3;
    -webkit-box-orient: vertical;
}

/* =========================================
   VISTA DETALLADA
   ========================================= */
.detail-view {
    background: var(--card-bg);
    border-radius: var(--radius);
    padding: 2rem;
    box-shadow: var(--shadow);
    display: none;
}

.detail-view.active {
    display: block;
}

.detail-header {
    display: grid;
    grid-template-columns: 120px 1fr;
    gap: 2rem;
    margin-bottom: 2rem;
    align-items: flex-start;
}

.detail-actions {
    display: flex;
    flex-direction: column;
    gap: 0.5rem;
    width: 120px;
    flex-shrink: 0;
}

.back-detail, .close-detail {
    width: 100%;
    padding: 0.5rem 1rem;
    border: none;
    border-radius: 6px;
    cursor: pointer;
    font-size: 0.9rem;
    transition: all var(--transition);
    text-align: center;
    color: white;
}

.back-detail {
    background: var(--secondary);
}

.back-detail:hover {
    background: var(--primary);
    transform: translateY(-2px);
}

.close-detail {
    background: var(--accent);
    font-weight: bold;
}

.close-detail:hover {
    background: #c0392b;
    transform: translateY(-2px);
}

.back-detail[hidden] {
    visibility: hidden;
}

.detail-content {
    grid-column: 2;
    display: flex;
    flex-direction: column;
    gap: 2rem;
}

.detail-main {
    display: grid;
    grid-template-columns: 300px 1fr;
    gap: 2rem;
    align-items: flex-start;
}

.detail-image-container {
    display: flex;
    flex-direction: column;
    gap: 0.5rem;
    align-items: center;
    width: 300px;
    background: var(--detail-image-bg) !important;
    padding: 15px;
    border-radius: var(--radius);
}

.detail-image {
    width: 100%;
    height: auto;
    object-fit: contain;
    max-height: 400px;
    border-radius: var(--radius);
    background: var(--detail-image-bg);
    display: block;
}

.image-thumbnails {
    display: flex;
    gap: 0.5rem;
    overflow-x: auto;
    padding: 0.5rem;
    width: 100%;
    justify-content: flex-start;
    min-height: 70px;
    align-items: center;
}

.thumbnail {
    width: 60px;
    height: 60px;
    object-fit: cover;
    border-radius: 5px;
    cursor: pointer;
    border: 3px solid transparent;
    transition: border-color 0.2s ease;
    flex-shrink: 0;
    background: #f5f5f5;
}

.thumbnail.active {
    border-color: var(--secondary);
}

.thumbnail:hover {
    border-color: var(--accent);
}

.thumbnail.broken {
    opacity: 0.5;
    pointer-events: none;
}

.image-description {
    margin-top: 0.5rem;
    padding: 0.5rem;
    background: var(--light);
    border-radius: 5px;
    font-size: 0.9rem;
    color: var(--text-color);
    text-align: center;
    word-wrap: break-word;
}

.image-description.hidden {
    display: none;
}

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

.detail-info h2 {
    color: var(--primary);
    margin-bottom: 1rem;
}

.detail-category {
    background: var(--secondary);
    color: white;
    padding: 0.3rem 1rem;
    border-radius: 20px;
    display: inline-block;
    margin-bottom: 1rem;
    font-size: 0.9rem;
}

.detail-description {
    line-height: 1.6;
    margin-bottom: 1.5rem;
    word-wrap: break-word;
}

.attributes {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
    gap: 1rem;
}

.attribute {
    background: var(--light);
    padding: 1rem;
    border-radius: 5px;
}

.attribute h4 {
    color: var(--primary);
    margin-bottom: 0.5rem;
}

.related-section,
.history-section {
    margin-top: 0;
}

.related-section h3 {
    margin-bottom: 1rem;
    color: var(--primary);
}

.books-section h3 {
    margin-bottom: 1.5rem;
}

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

.related-card {
    background: var(--light);
    border-radius: 8px;
    overflow: hidden;
    cursor: pointer;
    transition: transform var(--transition), box-shadow var(--transition);
    text-align: center;
    display: flex;
    flex-direction: column;
    height: 100%;
}

.related-card:hover {
    transform: translateY(-3px);
    box-shadow: 0 5px 15px rgba(0, 0, 0, 0.1);
}

.related-card-image {
    width: 100%;
    height: 120px;
    object-fit: cover;
    background: #f5f5f5;
}

.related-card-name {
    padding: 0.5rem;
    font-weight: 500;
    font-size: 0.9rem;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

/* =========================================
   HISTORY
   ========================================= */
.history-section {
    margin-top: 3rem;
    padding-top: 2rem;
    border-top: 1px solid var(--light);
}

.history-section h3 {
    color: var(--primary);
    margin-bottom: 1.5rem;
    font-size: 1.5rem;
    display: flex;
    align-items: center;
    gap: 0.5rem;
}

.history-section h3::before {
    content: "📖";
    font-size: 1.8rem;
}

.history-content {
    line-height: 1.8;
    font-size: 1.05rem;
    color: var(--text-color);
    background: var(--light);
    padding: 1.5rem;
    border-radius: var(--radius);
    border-left: 4px solid var(--secondary);
}

/* =========================================
   SCROLL BARS
   ========================================= */
/* Scrollbar para WebKit (Chrome, Safari, Edge) */
.image-thumbnails::-webkit-scrollbar {
    height: var(--scrollbar-width);
}

.image-thumbnails::-webkit-scrollbar-track {
    background: var(--scrollbar-track);
    border-radius: 10px;
}

.image-thumbnails::-webkit-scrollbar-thumb {
    background: var(--scrollbar-thumb);
    border-radius: 10px;
    border: 2px solid var(--card-bg); /* Marco que contrasta con fondo */
}

.image-thumbnails::-webkit-scrollbar-thumb:hover {
    background: var(--scrollbar-thumb-hover);
}

/* Scrollbar para Firefox */
.image-thumbnails {
    scrollbar-width: thin;
    scrollbar-color: var(--scrollbar-thumb) var(--scrollbar-track);
}

/* =========================================
   FOOTER
   ========================================= */
footer {
    background: var(--primary);
    color: white;
    padding: 1rem;
    position: fixed;
    bottom: 0;
    width: 100%;
    text-align: center !important;
}

footer p {
    margin-bottom: 0.5rem;
    opacity: 0.8;
}

/* =========================================
   MODO OSCURO
   ========================================= */
body.dark-mode {
    --primary: #1a252f;
    --secondary: #2980b9;
    --accent: #c0392b;
    --light: #34495e;
    --bg-gradient: linear-gradient(135deg, #1a1a2e 0%, #16213e 100%);
    --detail-image-bg: linear-gradient(135deg, rgba(26, 26, 46, 0.2) 0%, rgba(22, 33, 62, 0.2) 100%);
    --shadow: 0 4px 6px rgba(0, 0, 0, 0.5);
    --shadow-hover: 0 10px 20px rgba(0, 0, 0, 0.6);
    --text-color: #ecf0f1;
    --card-bg: #2c3e50;
    --border-color: #3a4a5c;
    --scrollbar-track: rgba(255, 255, 255, 0.05);
    --scrollbar-thumb: var(--accent);
}

#theme-toggle {
    background: var(--light);
    border: none;
    padding: 0.5rem 1rem;
    border-radius: 50%;
    cursor: pointer;
    font-size: 1.2rem;
    transition: transform 0.3s ease;
    margin-left: 1rem;
    box-shadow: var(--shadow);
}

#theme-toggle:hover {
    transform: scale(1.1);
}

.about-content {
    background: var(--card-bg) !important;
    color: var(--text-color) !important;
    border: 1px solid var(--border-color) !important;
}

.about-content h2,
.about-content h3 {
    color: var(--text-color) !important;
}

.about-content code {
    background: var(--light) !important;
    color: var(--text-color) !important;
}

/* =========================================
   MODO OSCURO - COMPONENTES
   ========================================= */
body.dark-mode .card,
body.dark-mode .sidebar,
body.dark-mode .detail-view,
body.dark-mode .about-content,
body.dark-mode .category-btn,
body.dark-mode .attributes,
body.dark-mode .attribute,
body.dark-mode .related-card,
body.dark-mode .history-content {
    background: var(--card-bg);
    color: var(--text-color);
    border-color: var(--border-color);
}

body.dark-mode .related-card,
body.dark-mode .attribute {
    background: var(--light);
    border: 1px solid var(--border-color);
}

body.dark-mode .card h3,
body.dark-mode .card-description {
    color: var(--text-color);
}

body.dark-mode .category-btn:hover,
body.dark-mode .category-btn.active {
    background: var(--secondary);
    color: white;
}

body.dark-mode .attribute h4,
body.dark-mode .detail-info h2,
body.dark-mode .detail-description {
    color: var(--text-color);
}

body.dark-mode .thumbnail {
    background: var(--light);
    border-color: var(--border-color);
}

body.dark-mode .back-detail,
body.dark-mode .close-detail {
    color: white;
    box-shadow: 0 2px 4px rgba(0, 0, 0, 0.3);
}

body.dark-mode .history-content {
    background: var(--light) !important;
    border-left: 4px solid var(--secondary) !important;
    color: var(--text-color) !important;
}

body.dark-mode .related-section h3,
body.dark-mode .history-section h3,
body.dark-mode .books-section h3 {
    color: var(--text-color) !important;
    font-weight: 600;
    text-shadow: 0 1px 2px rgba(0, 0, 0, 0.5);
}

#clear-search {
    color: var(--accent) !important;
    display: none !important;
    opacity: 0.7;
}

#clear-search.visible {
    display: block !important;
}

body.dark-mode #clear-search:hover {
    opacity: 1;
    background: rgba(255, 255, 255, 0.1);
    border-radius: 50%;
}

/* =========================================
   RESPONSIVE
   ========================================= */
@media (max-width: 1024px) {
    main {
        grid-template-columns: 1fr;
    }

    .sidebar {
        display: grid;
        grid-template-columns: repeat(2, 1fr);
        gap: 2rem;
    }

    header {
        flex-direction: column;
        gap: 1rem;
        text-align: center;
    }

    .main-nav ul {
        justify-content: center;
        flex-wrap: wrap;
    }

    .detail-main {
        grid-template-columns: 1fr;
    }

    .detail-actions {
        width: 100%;
        flex-direction: row;
        justify-content: space-between;
    }

    .grid-container {
        grid-template-columns: repeat(auto-fill, minmax(250px, 1fr));
    }

    .card-image-container {
        height: 220px;
    }
}

@media (max-width: 768px) {
    .grid-container {
        grid-template-columns: repeat(auto-fill, minmax(200px, 1fr));
    }

    .card-image-container {
        height: 180px;
    }

    .detail-image-container {
        width: 100%;
    }

    .search-box {
        width: 100%;
    }

    #search-input {
        width: 100%;
    }

    .sidebar {
        grid-template-columns: 1fr;
    }

    .attributes {
        grid-template-columns: 1fr;
    }

    .detail-info h2 {
        font-size: 2rem;
    }
}

.loading-skeleton {
    animation: pulse 1.5s ease-in-out infinite;
}

@keyframes pulse {
    0%, 100% {
        opacity: 1;
    }
    50% {
        opacity: 0.5;
    }
}

.skeleton-card {
    background: linear-gradient(90deg,
    var(--light) 25%,
    rgba(255, 255, 255, 0.5) 50%,
    var(--light) 75%);
    background-size: 200% 100%;
    animation: loading 1.5s infinite;
}