/* ==========================================================================
   TuApp Theme - Cart Drawer & AJAX Cart Styles

   Estilos completos para el sistema de carrito moderno:
   - Drawer deslizante (slide-in from right).
   - Overlay con backdrop blur.
   - Ítems del carrito con imagen, título, precio, cantidad, eliminar.
   - Estados: loading, empty, error, skeleton.
   - Badge del carrito (indicador numérico en el header).
   - Notificaciones Toast (éxito, error, info).
   - Animaciones GPU-accelerated, respetan prefers-reduced-motion.
   - Responsive completo.
   - Accesibilidad: focus visible, aria-live, roles.

   Usa las CSS custom properties definidas en style.css.
   ========================================================================== */

/* ==========================================================================
   Cart Badge (Indicador numérico en el header)
   ========================================================================== */

.cart-badge {
    position: absolute;
    top: 2px;
    right: 2px;
    display: flex;
    align-items: center;
    justify-content: center;
    min-width: 18px;
    height: 18px;
    padding: 0 4px;
    background: var(--color-accent);
    color: #fff;
    font-size: 10px;
    font-weight: 700;
    line-height: 1;
    border-radius: var(--radius-full);
    pointer-events: none;
    animation: badge-pop-in 0.3s cubic-bezier(0.34, 1.56, 0.64, 1);
    white-space: nowrap;
}

.cart-badge:empty {
    display: none;
}

@keyframes badge-pop-in {
    0% {
        transform: scale(0);
        opacity: 0;
    }
    80% {
        transform: scale(1.2);
    }
    100% {
        transform: scale(1);
        opacity: 1;
    }
}

/* Posicionamiento relativo necesario en el botón del carrito */
.header-cart-btn {
    position: relative;
}

/* ==========================================================================
   Cart Drawer Overlay
   ========================================================================== */

.cart-drawer-overlay {
    position: fixed;
    inset: 0;
    background: rgba(0, 0, 0, 0.45);
    backdrop-filter: blur(4px);
    -webkit-backdrop-filter: blur(4px);
    z-index: var(--z-cart-drawer);
    opacity: 0;
    visibility: hidden;
    transition:
        opacity var(--transition-base),
        visibility var(--transition-base);
    will-change: opacity, visibility;
}

.cart-drawer-overlay.is-open {
    opacity: 1;
    visibility: visible;
}

/* ==========================================================================
   Cart Drawer (Panel deslizante)
   ========================================================================== */

.cart-drawer {
    position: fixed;
    top: 0;
    right: 0;
    bottom: 0;
    width: min(100vw, 440px);
    background: var(--color-surface);
    z-index: calc(var(--z-cart-drawer) + 1);
    transform: translateX(100%);
    transition: transform var(--transition-base);
    display: flex;
    flex-direction: column;
    box-shadow: var(--shadow-xl);
    will-change: transform;
    outline: none;
}

.cart-drawer.is-open {
    transform: translateX(0);
}

/* ==========================================================================
   Cart Drawer - Header
   ========================================================================== */

.cart-drawer__header {
    display: flex;
    align-items: center;
    justify-content: flex-start;
    gap: var(--space-sm);
    padding: var(--space-md);
    border-bottom: 1px solid var(--color-border);
    flex-shrink: 0;
    min-height: 64px;
}

.cart-drawer__title {
    font-size: var(--text-md);
    font-weight: 700;
    color: var(--color-text);
    margin: 0;
    line-height: 1.2;
}

.cart-drawer__count {
    font-size: var(--text-xs);
    color: var(--color-text-muted);
    background: var(--color-surface-alt);
    padding: 2px 10px;
    border-radius: var(--radius-full);
    font-weight: 500;
}

.cart-drawer__close {
    margin-left: auto;
    flex-shrink: 0;
    width: 36px;
    height: 36px;
    border-radius: var(--radius-full);
    display: flex;
    align-items: center;
    justify-content: center;
    background: transparent;
    border: none;
    color: var(--color-text-muted);
    cursor: pointer;
    transition: all var(--transition-fast);
}

.cart-drawer__close:hover {
    background: var(--color-surface-alt);
    color: var(--color-text);
}

.cart-drawer__close:focus-visible {
    outline: 3px solid var(--color-accent);
    outline-offset: 2px;
}

/* ==========================================================================
   Cart Drawer - Body (Lista de productos)
   ========================================================================== */

.cart-drawer__body {
    flex: 1;
    overflow-y: auto;
    overflow-x: hidden;
    padding: var(--space-md);
    overscroll-behavior: contain;
    -webkit-overflow-scrolling: touch;
    scrollbar-width: thin;
    scrollbar-color: var(--color-border) transparent;
}

.cart-drawer__body::-webkit-scrollbar {
    width: 5px;
}

.cart-drawer__body::-webkit-scrollbar-track {
    background: transparent;
}

.cart-drawer__body::-webkit-scrollbar-thumb {
    background: var(--color-border);
    border-radius: 999px;
}

/* ==========================================================================
   Empty Cart
   ========================================================================== */

.cart-drawer__empty {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    text-align: center;
    padding: var(--space-2xl) var(--space-md);
    min-height: 50vh;
    color: var(--color-text-muted);
}

.cart-drawer__empty svg {
    margin-bottom: var(--space-md);
    color: var(--color-border);
    opacity: 0.6;
}

.cart-drawer__empty p {
    font-size: var(--text-base);
    margin-bottom: var(--space-lg);
    color: var(--color-text-muted);
}

.cart-drawer__empty .btn {
    margin-top: var(--space-sm);
}

/* ==========================================================================
   Cart Item (Individual)
   ========================================================================== */

.cart-drawer__item {
    display: flex;
    gap: var(--space-sm);
    padding: var(--space-sm) 0;
    border-bottom: 1px solid var(--color-border-light);
    position: relative;
    animation: item-fade-in 0.3s ease-out both;
}

.cart-drawer__item:last-child {
    border-bottom: none;
}

@keyframes item-fade-in {
    from {
        opacity: 0;
        transform: translateX(20px);
    }
    to {
        opacity: 1;
        transform: translateX(0);
    }
}

/* Stagger delay for items */
.cart-drawer__item:nth-child(1) { animation-delay: 0ms; }
.cart-drawer__item:nth-child(2) { animation-delay: 50ms; }
.cart-drawer__item:nth-child(3) { animation-delay: 100ms; }
.cart-drawer__item:nth-child(4) { animation-delay: 150ms; }
.cart-drawer__item:nth-child(5) { animation-delay: 200ms; }
.cart-drawer__item:nth-child(n+6) { animation-delay: 250ms; }

/* ==========================================================================
   Cart Item - Image
   ========================================================================== */

.cart-drawer__item-image {
    width: 80px;
    height: 80px;
    flex-shrink: 0;
    border-radius: var(--radius-md);
    overflow: hidden;
    background: var(--color-surface-alt);
    position: relative;
}

.cart-drawer__item-image img {
    width: 100%;
    height: 100%;
    object-fit: cover;
    transition: transform var(--transition-slow);
}

.cart-drawer__item-image:hover img {
    transform: scale(1.05);
}

/* ==========================================================================
   Cart Item - Info
   ========================================================================== */

.cart-drawer__item-info {
    flex: 1;
    min-width: 0;
    display: flex;
    flex-direction: column;
    justify-content: center;
}

.cart-drawer__item-title {
    font-size: var(--text-sm);
    font-weight: 600;
    line-height: 1.3;
    margin-bottom: var(--space-3xs);
    color: var(--color-text);
    text-decoration: none;
    display: -webkit-box;
    -webkit-line-clamp: 2;
    -webkit-box-orient: vertical;
    overflow: hidden;
    word-break: break-word;
}

.cart-drawer__item-title:hover {
    color: var(--color-accent);
}

.cart-drawer__item-price {
    font-size: var(--text-sm);
    font-weight: 600;
    color: var(--color-accent);
    margin-bottom: var(--space-2xs);
}

.cart-drawer__item-price del {
    font-size: var(--text-xs);
    color: var(--color-text-muted);
    font-weight: 400;
    margin-left: var(--space-3xs);
}

/* Variant / Attribute info */
.cart-drawer__item-variant {
    font-size: var(--text-xs);
    color: var(--color-text-muted);
    margin-bottom: var(--space-2xs);
    line-height: 1.3;
}

/* ==========================================================================
   Cart Item - Quantity Controls
   ========================================================================== */

.cart-drawer__item-qty {
    display: inline-flex;
    align-items: center;
    gap: 0;
    margin-top: auto;
    border: 1px solid var(--color-border);
    border-radius: var(--radius-sm);
    overflow: hidden;
    width: fit-content;
    background: var(--color-surface);
}

.cart-drawer__item-qty button {
    width: 32px;
    height: 30px;
    display: flex;
    align-items: center;
    justify-content: center;
    background: var(--color-surface-alt);
    border: none;
    font-size: var(--text-base);
    font-weight: 600;
    color: var(--color-text);
    cursor: pointer;
    transition: all var(--transition-fast);
    line-height: 1;
    user-select: none;
    -webkit-user-select: none;
    flex-shrink: 0;
}

.cart-drawer__item-qty button:hover {
    background: var(--color-border-light);
    color: var(--color-accent);
}

.cart-drawer__item-qty button:disabled {
    opacity: 0.35;
    cursor: not-allowed;
    background: var(--color-surface-alt);
    color: var(--color-text-muted);
}

.cart-drawer__item-qty button:focus-visible {
    outline: 2px solid var(--color-accent);
    outline-offset: -2px;
    z-index: 2;
}

.cart-drawer__item-qty-value {
    display: flex;
    align-items: center;
    justify-content: center;
    min-width: 36px;
    height: 30px;
    padding: 0 var(--space-2xs);
    font-size: var(--text-sm);
    font-weight: 600;
    color: var(--color-text);
    text-align: center;
    background: var(--color-surface);
    border-left: 1px solid var(--color-border-light);
    border-right: 1px solid var(--color-border-light);
}

/* ==========================================================================
   Cart Item - Remove Button
   ========================================================================== */

.cart-drawer__item-remove {
    position: absolute;
    top: var(--space-xs);
    right: 0;
    width: 28px;
    height: 28px;
    display: flex;
    align-items: center;
    justify-content: center;
    background: transparent;
    border: none;
    color: var(--color-text-muted);
    cursor: pointer;
    border-radius: var(--radius-full);
    transition: all var(--transition-fast);
    opacity: 0;
    z-index: 2;
}

.cart-drawer__item:hover .cart-drawer__item-remove,
.cart-drawer__item:focus-within .cart-drawer__item-remove {
    opacity: 1;
}

.cart-drawer__item-remove:hover {
    background: rgba(var(--color-error-rgb), 0.1);
    color: var(--color-error);
}

.cart-drawer__item-remove:focus-visible {
    opacity: 1;
    outline: 3px solid var(--color-error);
    outline-offset: 2px;
}

/* ==========================================================================
   Cart Drawer - Footer
   ========================================================================== */

.cart-drawer__footer {
    flex-shrink: 0;
    padding: var(--space-md);
    border-top: 1px solid var(--color-border);
    background: var(--color-surface);
}

.cart-drawer__total {
    display: flex;
    align-items: center;
    justify-content: space-between;
    font-weight: 700;
    margin-bottom: var(--space-md);
    font-size: var(--text-md);
    color: var(--color-text);
}

.cart-drawer__total .js-cart-total {
    font-size: var(--text-lg);
    color: var(--color-accent);
}

/* Free shipping progress bar (optional) */
.cart-drawer__shipping-progress {
    margin-bottom: var(--space-md);
}

.cart-drawer__shipping-progress-bar {
    width: 100%;
    height: 6px;
    background: var(--color-border-light);
    border-radius: 3px;
    overflow: hidden;
    margin-bottom: var(--space-2xs);
}

.cart-drawer__shipping-progress-fill {
    height: 100%;
    background: linear-gradient(
        90deg,
        var(--color-accent),
        var(--color-success)
    );
    border-radius: 3px;
    transition: width var(--transition-base);
}

.cart-drawer__shipping-progress-text {
    font-size: var(--text-xs);
    color: var(--color-text-muted);
}

.cart-drawer__shipping-progress-text strong {
    color: var(--color-success);
    font-weight: 600;
}

/* ==========================================================================
   Cart Drawer Buttons in Footer
   ========================================================================== */

.cart-drawer__footer .btn {
    margin-bottom: var(--space-2xs);
}

.cart-drawer__footer .btn:last-child {
    margin-bottom: 0;
}

.cart-drawer__footer .btn--ghost {
    color: var(--color-text-muted);
}

.cart-drawer__footer .btn--ghost:hover {
    color: var(--color-text);
    background: var(--color-surface-alt);
}

/* ==========================================================================
   Cart Item - Loading / Skeleton State
   ========================================================================== */

.cart-drawer__item--loading {
    pointer-events: none;
    opacity: 0.6;
}

.cart-drawer__item--loading .cart-drawer__item-image {
    animation: skeleton-shimmer 1.5s ease-in-out infinite;
    background: var(--color-surface-alt);
}

.cart-drawer__item--loading .cart-drawer__item-title {
    height: 1em;
    width: 70%;
    background: var(--color-surface-alt);
    border-radius: var(--radius-sm);
    animation: skeleton-shimmer 1.5s ease-in-out infinite;
    color: transparent;
}

.cart-drawer__item--loading .cart-drawer__item-price {
    height: 0.9em;
    width: 40%;
    background: var(--color-surface-alt);
    border-radius: var(--radius-sm);
    animation: skeleton-shimmer 1.5s ease-in-out infinite;
    color: transparent;
}

@keyframes skeleton-shimmer {
    0% {
        background-color: var(--color-surface-alt);
    }
    50% {
        background-color: var(--color-border-light);
    }
    100% {
        background-color: var(--color-surface-alt);
    }
}

/* ==========================================================================
   Cart Item - Removing State
   ========================================================================== */

.cart-drawer__item--removing {
    animation: item-slide-out 0.3s ease-in forwards;
    pointer-events: none;
}

@keyframes item-slide-out {
    to {
        opacity: 0;
        transform: translateX(-30px);
        max-height: 0;
        padding-top: 0;
        padding-bottom: 0;
        margin-top: 0;
        margin-bottom: 0;
        border-bottom-width: 0;
        overflow: hidden;
    }
}

/* ==========================================================================
   Toast Notifications
   ========================================================================== */

.toast-container {
    position: fixed;
    bottom: var(--space-lg);
    right: var(--space-lg);
    z-index: var(--z-toast);
    display: flex;
    flex-direction: column-reverse;
    gap: var(--space-sm);
    pointer-events: none;
    max-width: 380px;
    width: calc(100vw - var(--space-lg) * 2);
}

.toast {
    display: flex;
    align-items: center;
    gap: var(--space-sm);
    padding: var(--space-sm) var(--space-md);
    background: var(--color-primary);
    color: var(--color-text-on-dark);
    border-radius: var(--radius-lg);
    box-shadow: var(--shadow-lg);
    font-size: var(--text-sm);
    font-weight: 500;
    line-height: 1.4;
    pointer-events: all;
    cursor: pointer;
    animation: toast-slide-in 0.35s cubic-bezier(0.16, 1, 0.3, 1) forwards;
    word-break: break-word;
}

.toast__icon {
    flex-shrink: 0;
    display: flex;
    align-items: center;
    justify-content: center;
}

.toast__message {
    flex: 1;
    min-width: 0;
}

.toast--success {
    background: var(--color-success);
    color: #fff;
}

.toast--error {
    background: var(--color-error);
    color: #fff;
}

.toast--info {
    background: var(--color-primary);
    color: #fff;
}

.toast--removing {
    animation: toast-slide-out 0.25s ease-in forwards;
    pointer-events: none;
}

@keyframes toast-slide-in {
    0% {
        opacity: 0;
        transform: translateY(24px) scale(0.94);
    }
    100% {
        opacity: 1;
        transform: translateY(0) scale(1);
    }
}

@keyframes toast-slide-out {
    0% {
        opacity: 1;
        transform: translateY(0) scale(1);
    }
    100% {
        opacity: 0;
        transform: translateY(12px) scale(0.92);
    }
}

/* ==========================================================================
   Loading Spinner (Add to Cart Button)
   ========================================================================== */

.btn--loading {
    position: relative;
    color: transparent !important;
    pointer-events: none;
}

.btn--loading::after {
    content: '';
    position: absolute;
    top: 50%;
    left: 50%;
    width: 1.2em;
    height: 1.2em;
    margin: -0.6em 0 0 -0.6em;
    border: 2.5px solid currentColor;
    border-right-color: transparent;
    border-radius: 50%;
    animation: btn-spin 0.6s linear infinite;
    color: inherit;
}

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

.btn--primary.btn--loading::after {
    color: #ffffff;
}

.btn--secondary.btn--loading::after {
    color: var(--color-accent);
}

/* ==========================================================================
   Quick View / Wishlist Buttons inside Cart Items (optional)
   ========================================================================== */

.cart-drawer__item-wishlist {
    position: absolute;
    top: var(--space-xs);
    right: 32px;
    width: 24px;
    height: 24px;
    display: flex;
    align-items: center;
    justify-content: center;
    background: transparent;
    border: none;
    color: var(--color-text-muted);
    cursor: pointer;
    border-radius: var(--radius-full);
    transition: all var(--transition-fast);
    opacity: 0;
}

.cart-drawer__item:hover .cart-drawer__item-wishlist {
    opacity: 1;
}

.cart-drawer__item-wishlist:hover {
    color: var(--color-error);
}

/* ==========================================================================
   Mobile Menu Cart Link
   ========================================================================== */

.mobile-menu__cart-link {
    display: flex;
    align-items: center;
    gap: var(--space-xs);
    font-weight: 600;
    color: var(--color-text);
}

.mobile-menu__cart-link .cart-badge {
    position: static;
    margin-left: auto;
    min-width: 22px;
    height: 22px;
    font-size: 11px;
}

/* ==========================================================================
   Responsive: Mobile
   ========================================================================== */

@media (max-width: 480px) {
    .cart-drawer {
        width: 100vw;
    }

    .cart-drawer__header {
        padding: var(--space-sm) var(--space-md);
        min-height: 56px;
    }

    .cart-drawer__body {
        padding: var(--space-sm);
    }

    .cart-drawer__footer {
        padding: var(--space-sm) var(--space-md);
    }

    .cart-drawer__item-image {
        width: 64px;
        height: 64px;
    }

    .cart-drawer__item-title {
        font-size: var(--text-xs);
        -webkit-line-clamp: 1;
    }

    .cart-drawer__item-remove {
        opacity: 1;
        top: var(--space-2xs);
        right: var(--space-2xs);
    }

    .toast-container {
        bottom: var(--space-md);
        right: var(--space-sm);
        left: var(--space-sm);
        max-width: none;
        width: auto;
    }

    .toast {
        padding: var(--space-sm);
        font-size: var(--text-xs);
    }
}

/* ==========================================================================
   Accessibility: Reduced Motion
   ========================================================================== */

@media (prefers-reduced-motion: reduce) {
    .cart-drawer {
        transition: none;
    }

    .cart-badge {
        animation: none;
    }

    .cart-drawer__item {
        animation: none;
    }

    .toast {
        animation: none;
    }

    .cart-drawer__item--removing {
        animation: none;
        opacity: 0;
    }

    .btn--loading::after {
        animation: none;
        border-color: currentColor;
        border-right-color: currentColor;
    }
}

/* ==========================================================================
   Dark Mode Preparation (Future)
   ========================================================================== */

/*
.is-dark-mode .cart-drawer {
    background: var(--color-surface-dark);
    color: var(--color-text-on-dark);
}

.is-dark-mode .cart-drawer__item {
    border-bottom-color: rgba(255, 255, 255, 0.08);
}

.is-dark-mode .cart-drawer__item-qty {
    background: rgba(255, 255, 255, 0.05);
    border-color: rgba(255, 255, 255, 0.1);
}

.is-dark-mode .cart-drawer__item-qty button {
    background: rgba(255, 255, 255, 0.05);
    color: var(--color-text-on-dark);
}

.is-dark-mode .cart-drawer__item-qty-value {
    background: rgba(255, 255, 255, 0.03);
    border-color: rgba(255, 255, 255, 0.08);
    color: var(--color-text-on-dark);
}

.is-dark-mode .cart-drawer__footer {
    background: var(--color-surface-dark);
}

.is-dark-mode .cart-drawer-overlay {
    background: rgba(0, 0, 0, 0.7);
}
*/

/* ==========================================================================
   Print Styles
   ========================================================================== */

@media print {
    .cart-drawer,
    .cart-drawer-overlay,
    .toast-container,
    .cart-badge {
        display: none !important;
    }
}
