.card-view-container {
    display: none;
    /* Use auto-fit to create a responsive column layout.
       Each column will be at least 380px wide and can grow. */
    grid-template-columns: repeat(auto-fit, minmax(380px, 1fr));
    gap: 24px;
    padding: 24px;
    align-items: start; /* This ensures cards align to top and don't stretch */
    box-sizing: border-box;
}

@media (max-width: 768px) {
    .card-view-container {
        /* Override the minmax for tablets and smaller screens */
        grid-template-columns: 1fr;
        gap: 16px;
        padding: 16px;
    }
}

@media (max-width: 480px) {
    .card-view-container {
        /* Keep single column for mobile */
        grid-template-columns: 1fr;
        padding: 8px;
        gap: 16px;
    }
}

/* Center the loading spinner when it's inside the grid */
.card-view-container > .loading-spinner {
    grid-column: 1 / -1; /* Span all columns */
}

.card {
    background: linear-gradient(145deg, #ffffff 0%, #f8f9fa 100%);
    border-radius: 16px;
    box-shadow: 
        0 4px 20px rgba(0, 0, 0, 0.08),
        0 1px 3px rgba(0, 0, 0, 0.1);
    cursor: pointer;
    display: flex;
    flex-direction: column;
    overflow: hidden;
    transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
    border: 1px solid rgba(0, 0, 0, 0.05);
    position: relative;
    width: 100%; /* Ensure card takes full width */
    justify-self: start; /* Prevent grid stretching */
    min-width: 0; /* Prevents overflow */
    box-sizing: border-box;
}

.card:hover {
    transform: translateY(-8px);
    box-shadow: 
        0 12px 40px rgba(0, 0, 0, 0.15),
        0 4px 12px rgba(0, 0, 0, 0.1);
}

.card::before {
    content: '';
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    height: 3px;
    background: linear-gradient(90deg, #007bff, #0056b3);
    opacity: 0;
    transition: opacity 0.3s ease;
}

.card:hover::before {
    opacity: 1;
}

/* Actions at the top */
.card-actions {
    display: flex;
    gap: 6px; /* Reduced gap between buttons */
    justify-content: flex-end;
    padding: 8px 12px; /* Reduced top/bottom and left/right padding */
    background: rgba(255, 255, 255, 0.7);
    backdrop-filter: blur(10px);
    border-bottom: 1px solid rgba(0, 0, 0, 0.08);
    order: -1; /* This moves it to the top */
    min-height: 58px; /* Ensures consistent height even when empty */
    box-sizing: border-box; /* Includes padding and border in the height calculation */
}


.card-actions .delete-btn,
.card-actions .message-owner,
.card-actions .favorite-btn {
    padding: 6px 10px; /* Reduced button padding */
    border-radius: 8px;
    text-decoration: none;
    font-size: 13px; /* Slightly smaller font */
    border: none;
    cursor: pointer;
    transition: all 0.2s cubic-bezier(0.4, 0, 0.2, 1);
    font-weight: 500;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    min-width: 32px; /* Reduced button size */
    height: 32px; /* Reduced button size */
}

.card-actions .edit-btn {
    color: white;
    background: linear-gradient(135deg, #A2D5AB, #3e854e);
    border: 1px solid #82B58E;        /* slightly darker green border */
}

.card-actions .edit-btn:hover {
    background: linear-gradient(135deg, #3e854e, #228338);
    transform: translateY(-1px);
}

.card-actions .delete-btn {
    background: linear-gradient(135deg, #dc3545, #c82333);
    color: white;
    box-shadow: 0 2px 8px rgba(220, 53, 69, 0.3);
}

.card-actions .delete-btn:hover {
    background: linear-gradient(135deg, #c82333, #a71e2a);
    transform: translateY(-1px);
}


.card-actions .message-owner {
    background: linear-gradient(135deg, #28a745, #1e7e34);
    color: white;
    box-shadow: 0 2px 8px rgba(40, 167, 69, 0.3);
}

.card-actions .message-owner:hover {
    background: linear-gradient(135deg, #1e7e34, #155724);
    transform: translateY(-1px);
}

.card-actions .favorite-btn {
    background: linear-gradient(135deg, #2E86AB, #23789c);
    color: #212529;
    box-shadow: 0 2px 8px rgba(255, 193, 7, 0.3);
}

.card-actions .favorite-btn:hover {
    background: linear-gradient(135deg, #21638A, #1b577a);
    transform: translateY(-1px);
}

.card-content {
    display: flex;
    padding: 20px;
    flex-grow: 1;
    min-height: 180px;
}

.card-image {
    flex: 0 0 150px;
    margin-right: 20px;
    position: relative;
}

/* Enhanced Image carousel styles */
.image-carousel {
    position: relative;
    width: 100%;
    height: 220px; /* Fixed height for consistency. Increase this value to make it taller. */
    overflow: hidden;
    border-radius: 12px;
    background-color: #f5f5f5;
}

.image-slider {
    width: 100%;
    height: 100%;
    position: relative;
}

.image-wrapper {
    width: 100%;
    height: 100%;
    display: flex;
    align-items: center;
    justify-content: center;
    background-color: #f9f9f9;
    overflow: hidden;
}

.image-wrapper img {
    width: 100%;
    height: auto;
    object-fit: cover;
    object-position: center;
    border-radius: 12px;
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
    transition: transform 0.3s ease;
}

.image-wrapper img:hover {
    transform: scale(1.02);
}

.carousel-arrow {
    position: absolute;
    top: 50%;
    transform: translateY(-50%);
    background: rgba(0, 0, 0, 0.6);
    color: white;
    border: none;
    width: 32px;
    height: 32px;
    border-radius: 50%;
    cursor: pointer;
    font-size: 18px;
    font-weight: bold;
    display: flex;
    align-items: center;
    justify-content: center;
    z-index: 3;
    transition: background-color 0.2s ease;
    user-select: none;
}

.carousel-arrow:hover {
    background: rgba(0, 0, 0, 0.8);
}

.carousel-arrow:active {
    transform: translateY(-50%) scale(0.95);
}

.carousel-prev {
    left: 8px;
}

.carousel-next {
    right: 8px;
}

.image-counter {
    position: absolute;
    bottom: 8px;
    right: 8px;
    background: rgba(0, 0, 0, 0.7);
    color: white;
    padding: 3px 8px;
    border-radius: 12px;
    font-size: 12px;
    font-weight: 500;
    z-index: 3;
    user-select: none;
}

.card-details {
    flex: 1;
    display: flex;
    flex-direction: column;
    gap: 8px;
    overflow-wrap: break-word;
    position: relative; /* Added for absolute positioning of updated text */
    padding-bottom: 8px; /* This padding ensures that the absolutely positioned 'updated'/'faved' text at the bottom does not overlap with the card's main content. */
}

.card-details h3 {
    margin: 0 0 4px 0;
    font-size: 1.25rem;
    font-weight: 700;
    color: #1a1a1a;
    line-height: 1.3;
    overflow-wrap: break-word;    /* Prevent long words from overflowing the card */
    word-wrap: break-word; /* Legacy fallback for older browsers */
}

.card-details p {
    margin: 0;
    line-height: 1.4;
}

.card-details .author {
    font-style: italic;
    font-weight: 600;
    color: #6c757d;
    font-size: 0.95rem;
    overflow-wrap: break-word;
    word-wrap: break-word; /* Legacy fallback for older browsers */
}

.card-details .location, .card-details .distance {
    font-weight: 600;
    color: #495057;
    font-size: 0.9rem;
}

.card-details .updated, .card-details .faved {
    font-weight: 500;
    color: #6c757d;
    font-size: 0.75rem;
    position: absolute;
    bottom: 0;
    right: 0;
    background: rgba(255, 255, 255, 0.9);
    padding: 4px 8px;
    border-radius: 6px;
    border: 1px solid rgba(0, 0, 0, 0.1);
}

.card-details .price-info {
    font-weight: 700;
    color: #28a745;
    font-size: 1rem;
    background: rgba(40, 167, 69, 0.1);
    padding: 4px 8px;
    border-radius: 6px;
    display: inline-block;
    white-space: pre-wrap;
}

.card-details .borrow-info {
    font-weight: 700;
    color: #007bff;
    font-size: 0.9rem;
    background: rgba(0, 123, 255, 0.1);
    padding: 4px 8px;
    border-radius: 6px;
    display: inline-block;
    white-space: pre-wrap;
}

/* Availability badge */
.availability-badge {
    position: absolute;
    top: 12px;
    left: 12px;
    padding: 6px 12px;
    border-radius: 20px;
    font-size: 0.8rem;
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: 0.5px;
    z-index: 2;
    backdrop-filter: blur(10px);
    border: 1px solid rgba(255, 255, 255, 0.2);
}

.availability-badge.availability-0 {
    background: rgba(0, 123, 255, 0.9);
    color: white;
}

.availability-badge.availability-1 {
    background: rgba(40, 167, 69, 0.9);
    color: white;
}

.availability-badge.availability-2 {
    background: rgba(255, 193, 7, 0.9);
    color: white;
}

.card-details .owner {
    display: flex;
    align-items: center;
    margin-bottom: 24px; /* Add space for the updated text */
}

.card-details .owner img {
    width: 32px;
    height: 32px;
    border-radius: 50%;
    margin-right: 10px;
    border: 2px solid rgba(0, 123, 255, 0.2);
    transition: border-color 0.2s ease;
}

.card-details .owner a {
    text-decoration: none;
    display: flex;
    align-items: center;
    transition: opacity 0.2s ease;
}

.card-details .owner a:hover {
    opacity: 0.8;
}

.card-details .owner a:hover img {
    border-color: rgba(0, 123, 255, 0.5);
}

.card-details .owner .username-text {
    font-weight: 600;
    color: #495057;
    font-size: 0.9rem;
}

.card-expansion {
    max-height: 0; /* Initial state for animation */
    overflow: hidden; /* Hide content when collapsed */
    padding: 0 20px;
    border-top: 1px solid transparent;
    background: rgba(248, 249, 250, 0.8);
    backdrop-filter: blur(10px);
    transition: max-height 0.4s cubic-bezier(0.4, 0, 0.2, 1), 
                padding 0.4s cubic-bezier(0.4, 0, 0.2, 1), 
                opacity 0.3s ease;
    opacity: 0;
}

.card-expansion.expanded {
    max-height: 60vh; /* Use viewport height for a responsive max height */
    overflow-y: auto; /* Allow vertical scrolling if content is too long */
    padding: 20px;
    border-top: 1px solid rgba(0, 0, 0, 0.1);
    opacity: 1;
}

/* Fix for the two-column layout issue in card expansion */
.card-expansion .details-content {
    display: block; /* Override the flex/grid layout from the table view */
}

.card-expansion .book-images {
    display: none; /* Hide the empty image container which takes up space */
}

.card-expansion .details-grid {
    width: 100%;
}

.view-toggle-buttons {
    text-align: left;
    margin-bottom: 10px;
}

.view-toggle-btn {
    background: linear-gradient(135deg, #f8f9fa, #e9ecef);
    border: 1px solid #dee2e6;
    padding: 8px 12px;
    margin: 0 4px;
    cursor: pointer;
    transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
    border-radius: 8px;
    font-weight: 600;
    font-size: 0.5rem;
    position: relative;
    overflow: hidden;
}

.view-toggle-btn::before {
    content: '';
    position: absolute;
    top: 0;
    left: -100%;
    width: 100%;
    height: 100%;
    background: linear-gradient(90deg, transparent, rgba(255, 255, 255, 0.4), transparent);
    transition: left 0.5s;
}

.view-toggle-btn:hover::before {
    left: 100%;
}

.view-toggle-btn:hover {
    transform: translateY(-2px);
    box-shadow: 0 4px 12px rgba(0, 123, 255, 0.15);
}

.view-toggle-btn.active {
    background: linear-gradient(135deg, #007bff, #0056b3);
    color: white;
    border-color: #007bff;
    box-shadow: 0 4px 12px rgba(0, 123, 255, 0.3);
}

.view-toggle-btn.active:hover {
    background: linear-gradient(135deg, #0056b3, #004085);
    transform: translateY(-2px);
    box-shadow: 0 6px 16px rgba(0, 123, 255, 0.4);
}

/* Mobile specific carousel adjustments */
@media (max-width: 480px) {
    .card-content {
        padding: 16px;
        min-height: 160px;
    }
    
    .card-image {
        flex: 0 0 130px;
        margin-right: 16px;
    }
    
    .image-carousel {
        height: 200px; /* Slightly smaller on mobile */
    }
    
    .carousel-arrow {
        width: 28px;
        height: 28px;
        font-size: 16px;
    }
    
    .carousel-prev {
        left: 6px;
    }
    
    .carousel-next {
        right: 6px;
    }
    
    .image-counter {
        font-size: 11px;
        padding: 2px 6px;
        bottom: 6px;
        right: 6px;
    }
    
    /* Ensure card action buttons are square on mobile */
    .card-actions .edit-btn,
    .card-actions .delete-btn {
        width: 34px;
        height: 32px;
        min-width: 28px;
        padding: 4px;
        font-size: 12px;
    }
}

/* Tablet adjustments */
@media (min-width: 481px) and (max-width: 768px) {
    .image-carousel {
        height: 210px;
    }
}

/* Desktop enhancements */
@media (min-width: 769px) {
    .image-carousel {
        height: 240px; /* Slightly taller on desktop */
    }
    
    /* Optional: Show arrows only on hover for desktop */
    .carousel-arrow {
        opacity: 0.7;
        transition: opacity 0.2s ease, background-color 0.2s ease;
    }
    
    .image-carousel:hover .carousel-arrow {
        opacity: 1;
    }
}

/* Large desktop */
@media (min-width: 1200px) {
    .image-carousel {
        height: 260px;
    }
}