/* Material Design 3 Button Styles
 * Based on MD3 specifications with expressive design updates
 * Includes toggle states and updated shape guidelines
 */

.button-group {
    display: flex;
    align-items: center;
    gap: var(--size-2);
    width: 100%;
}

/* Mobile: buttons stack vertically and take full width */
@media (max-width: 767px) {
    .button-group {
        flex-direction: column;
        gap: var(--size-2);
    }
    
    .button-group .button {
        width: 100%;
    }
    
    .button-group details {
        width: 100%;
    }
}

.button {
    --button-justify: center;
    --button-corner-radius: var(--radius-full); /* Default to full radius (20dp) */
    
    display: inline-flex;
    align-items: center;
    justify-content: var(--button-justify);
    gap: var(--size-2);
    padding: var(--size-2) var(--size-3);
    border-radius: var(--button-corner-radius);
    font-size: var(--font-size-body-large);
    font-weight: var(--font-weight-5);
    border: none;
    cursor: pointer;
    text-decoration: none;
    transition: all 0.2s var(--ease-standard);
    min-height: 40px;
    max-height: 48px;
    position: relative;
    overflow: hidden;
    margin-bottom: 1px;
    -webkit-tap-highlight-color: transparent; /* Remove mobile tap highlight */
    transform: translateZ(0); /* Enable hardware acceleration */
    will-change: transform, box-shadow;
    
    /* Fix for border clipping */
    &.outlined {
        overflow: visible;
    }

    /* State layer for MD3 interaction states */
    &::before {
        content: '';
        position: absolute;
        top: 0;
        left: 0;
        right: 0;
        bottom: 0;
        background: currentColor;
        opacity: 0;
        transition: opacity 0.2s var(--ease-standard);
        pointer-events: none;
        border-radius: inherit; /* Match parent's border radius */
    }

    &:hover::before {
        opacity: 0.08;
    }

    &:focus-visible::before {
        opacity: 0.12;
    }

    &:active::before {
        opacity: 0.12;
    }

    /* Touch press animation */
    &:active {
        transform: scale(0.95);
        transition-duration: 0.05s;
    }

    /* Ripple effect for touch */
    &::after {
        content: '';
        position: absolute;
        top: 50%;
        left: 50%;
        width: 0;
        height: 0;
        border-radius: 50%;
        background: currentColor;
        opacity: 0;
        transform: translate(-50%, -50%);
        pointer-events: none;
    }

    &:active::after {
        animation: ripple 0.6s ease-out;
    }

    svg {
        height: 18px;
        width: 18px;
        flex-shrink: 0;
    }

    /* Filled button (highest emphasis) */
    &.filled {
        background: var(--primary);
        color: var(--on-primary) !important;
        
        &:hover {
            box-shadow: var(--elevation-1);
        }
        
        &:active {
            box-shadow: none;
        }
    }

    /* Disabled state */
    &:disabled {
        background: color-mix(in srgb, var(--on-surface) 12%, transparent);
        color: color-mix(in srgb, var(--on-surface) 38%, transparent);
        box-shadow: none;
        cursor: not-allowed;
        
        &::before {
            display: none;
        }
    }

    /* Filled tonal button (medium emphasis) */
    &.tonal {
        background: var(--secondary-container);
        color: var(--on-secondary-container);
        
        &:hover {
            box-shadow: var(--elevation-1);
        }
        
        &:active {
            box-shadow: none;
        }
    }

    /* Elevated button (medium emphasis with initial elevation) */
    &.elevated {
        background: var(--surface);
        color: var(--primary);
        box-shadow: var(--elevation-1);
        
        &:hover {
            box-shadow: var(--elevation-2);
        }
        
        &:active {
            box-shadow: var(--elevation-1);
        }
        
        &:disabled {
            background: color-mix(in srgb, var(--on-surface) 12%, transparent);
            color: color-mix(in srgb, var(--on-surface) 38%, transparent);
            box-shadow: none;
        }
    }

    /* Outlined button (medium emphasis) */
    &.outlined {
        background: transparent;
        border: 1px solid var(--outline);
        color: var(--primary);
        overflow: visible !important; /* Ensure border isn't clipped */
        
        /* Use background color change instead of overlay for outlined buttons */
        &:hover {
            background: color-mix(in srgb, var(--primary) 8%, transparent);
        }
        
        &:focus-visible {
            background: color-mix(in srgb, var(--primary) 12%, transparent);
        }
        
        &:active {
            background: color-mix(in srgb, var(--primary) 12%, transparent);
        }
        
        /* Disable overlay pseudo-elements for outlined buttons */
        &::before,
        &::after {
            display: none;
        }
        
        &:disabled {
            border-color: color-mix(in srgb, var(--on-surface) 12%, transparent);
            color: color-mix(in srgb, var(--on-surface) 38%, transparent);
            background: transparent;
        }
    }

    /* Text button (low emphasis) */
    &.text {
        background: transparent;
        color: var(--primary);
        padding: var(--size-2) var(--size-3);
        
        &:disabled {
            color: color-mix(in srgb, var(--on-surface) 38%, transparent);
        }
    }

    /* Icon button variations */
    &.icon {
        padding: var(--size-2);
        min-width: 40px;
        width: 40px;
        height: 40px;
        border-radius: var(--radius-full);
    }
    &.toggle {
            /* Toggle state for icon buttons */
            &.selected {
                background: var(--primary) !important;
                color: var(--on-primary) !important;
                --button-corner-radius: 16px;
            }
            
            &:not(.selected) {
                background: transparent;
                color: var(--on-surface-variant);
            }
        }

    /* Small icon button */
    &.icon.small {
        padding: 0;
        width: 28px;
        height: 28px;
        min-height: 28px;
        min-width: 28px;
        
        svg {
            height: 16px;
            width: 16px;
        }
    }

    /* Error state */
    &.error {
        background: var(--error);
        color: var(--on-error);
    }

    /* Shape variations - MD3 corner radius scale */
    &.shape-none {
        --button-corner-radius: 0;
    }
    
    &.shape-extra-small {
        --button-corner-radius: 4px;
    }
    
    &.shape-small {
        --button-corner-radius: 8px;
    }
    
    &.shape-medium {
        --button-corner-radius: 12px;
    }
    
    &.shape-large {
        --button-corner-radius: 16px;
    }
    
    &.shape-extra-large {
        --button-corner-radius: 28px;
    }
    
    &.shape-full {
        --button-corner-radius: var(--radius-full); /* Default */
    }

    /* Square variant - less rounded corners */
    &.square {
        --button-corner-radius: 16px;
        
        &.small {
            --button-corner-radius: 8px;
        }
        
        &.large {
            --button-corner-radius: 28px;
        }
    }

    /* Size variations */
    &.small {
        min-height: 32px;
        font-size: var(--font-size-body-medium);
        padding: var(--size-1) var(--size-2);
        
        svg {
            height: 16px;
            width: 16px;
        }
    }

    &.large {
        min-height: 48px;
        font-size: var(--font-size-title-medium);
        padding: var(--size-3) var(--size-4);
        
        svg {
            height: 20px;
            width: 20px;
        }
    }

    /* Toggle button states */
    &.toggle {
        &.selected {
            background: var(--secondary-container);
            color: var(--on-secondary-container);
            
            &.filled {
                background: var(--primary);
                color: var(--on-primary);
            }
            
            &.tonal {
                background: var(--secondary-container);
                color: var(--on-secondary-container);
            }
            
            &.outlined {
                background: var(--secondary-container);
                border-color: transparent;
                color: var(--on-secondary-container);
            }
        }
    }

    /* Full width button */
    &.full-width {
        width: 100%;
        --button-justify: center;
    }

    /* Leading icon alignment */
    &.icon-start {
        --button-justify: flex-start;
        padding-left: var(--size-3);
    }
}

/* Segmented button group (for toggle groups) */
.segmented-button-group {
    display: inline-flex;
    border: 1px solid var(--outline);
    border-radius: var(--radius-full);
    overflow: hidden;
    
    .button {
        border: none;
        border-radius: 0;
        margin: 0;
        position: relative;
        
        &:not(:last-child)::after {
            content: '';
            position: absolute;
            right: 0;
            top: 25%;
            bottom: 25%;
            width: 1px;
            background: var(--outline);
        }
        
        &:first-child {
            border-top-left-radius: var(--radius-full);
            border-bottom-left-radius: var(--radius-full);
        }
        
        &:last-child {
            border-top-right-radius: var(--radius-full);
            border-bottom-right-radius: var(--radius-full);
        }
        
        &.selected {
            background: var(--secondary-container);
            color: var(--on-secondary-container);
        }
    }
}

/* Floating Action Button (FAB) - Updated for MD3 */
.fab {
    --fab-size: 56px;
    --fab-corner-radius: 16px; /* MD3 default for FAB */
    --icon-size: 24px;

    display: flex;
    align-items: center;
    justify-content: center;
    height: var(--fab-size);
    width: var(--fab-size);
    border-radius: var(--fab-corner-radius);
    box-shadow: var(--elevation-3);
    transition: all 0.2s var(--ease-standard);
    border: none;
    cursor: pointer;
    z-index: var(--layer-top);
    position: relative;
    overflow: hidden;

    /* State layer */
    &::before {
        content: '';
        position: absolute;
        top: 0;
        left: 0;
        right: 0;
        bottom: 0;
        background: currentColor;
        opacity: 0;
        transition: opacity 0.2s var(--ease-standard);
        pointer-events: none;
    }

    &:hover::before {
        opacity: 0.08;
    }

    &:hover {
        box-shadow: var(--elevation-4);
    }

    &:focus-visible::before {
        opacity: 0.12;
    }

    &:active::before {
        opacity: 0.12;
    }

    &:active {
        box-shadow: var(--elevation-3);
    }

    svg {
        height: var(--icon-size);
        width: var(--icon-size);
    }

    /* FAB sizes */
    &.small {
        --fab-size: 40px;
        --fab-corner-radius: 12px;
        --icon-size: 20px;
    }

    &.large {
        --fab-size: 96px;
        --fab-corner-radius: 28px;
        --icon-size: 36px;
    }

    /* FAB color variants */
    &.primary {
        background: var(--primary-container);
        color: var(--on-primary-container);
    }

    &.surface {
        background: var(--surface);
        color: var(--primary);
    }

    &.secondary {
        background: var(--secondary-container);
        color: var(--on-secondary-container);
    }

    &.tertiary {
        background: var(--tertiary-container);
        color: var(--on-tertiary-container);
    }

    /* Extended FAB */
    &.extended {
        width: auto;
        padding: 0 var(--size-4);
        gap: var(--size-2);
        
        svg {
            margin: 0;
        }
    }

    /* Fixed positioning */
    &.fixed {
        position: fixed;
        bottom: var(--size-4);
        right: var(--size-4);
        z-index: var(--layer-modal);
    }
      /* Center the mobile toggle FAB at the bottom */
    &.mobile-toggle {
        position: fixed;
        bottom: var(--size-4);
        z-index: var(--layer-modal);
        left: 50%;
        transform: translateX(-50%);
        /* Keep same bottom position */
    }

    /* Lowered FAB (less elevation) */
    &.lowered {
        box-shadow: var(--elevation-1);
        
        &:hover {
            box-shadow: var(--elevation-2);
        }
        
        &:active {
            box-shadow: var(--elevation-1);
        }
    }
}

/* Button best practices helper classes */
.button-primary-action {
    /* Use filled button for primary actions */
    @extend .button.filled;
}

.button-secondary-action {
    /* Use outlined or tonal for secondary actions */
    @extend .button.outlined;
}

.button-tertiary-action {
    /* Use text button for tertiary actions */
    @extend .button.text;
}

/* Animations */
@keyframes ripple {
    0% {
        width: 0;
        height: 0;
        opacity: 0.5;
    }
    100% {
        width: 200px;
        height: 200px;
        opacity: 0;
    }
}

/* State change animation for Turbo Frame reloads */
@keyframes state-change {
    0% {
        transform: scale(1);
    }
    50% {
        transform: scale(0.9);
    }
    100% {
        transform: scale(1);
    }
}

/* Pulse animation for selection */
@keyframes pulse {
    0% {
        box-shadow: 0 0 0 0 rgba(var(--primary), 0.4);
    }
    70% {
        box-shadow: 0 0 0 10px rgba(var(--primary), 0);
    }
    100% {
        box-shadow: 0 0 0 0 rgba(var(--primary), 0);
    }
}

/* Apply state change animations */
.button {
    /* Animate state changes */
    &.toggle {
        &.selected {
            animation: state-change 0.3s ease-out;
            
            /* Pulse on selection for better feedback */
            &.pulse {
                animation: pulse 0.6s ease-out, state-change 0.3s ease-out;
            }
        }
    }
    
    /* Bounce effect for error states */
    &.error {
        &.shake {
            animation: shake 0.5s ease-in-out;
        }
    }
}

/* Icon rotation for loading states */
@keyframes spin {
    from {
        transform: rotate(0deg);
    }
    to {
        transform: rotate(360deg);
    }
}

/* Shake animation for errors */
@keyframes shake {
    0%, 100% {
        transform: translateX(0);
    }
    10%, 30%, 50%, 70%, 90% {
        transform: translateX(-2px);
    }
    20%, 40%, 60%, 80% {
        transform: translateX(2px);
    }
}

/* Loading state */
.button.loading {
    pointer-events: none;
    color: transparent;
    
    &::after {
        content: '';
        position: absolute;
        width: 16px;
        height: 16px;
        top: 50%;
        left: 50%;
        margin: -8px 0 0 -8px;
        border: 2px solid currentColor;
        border-right-color: transparent;
        border-radius: 50%;
        animation: spin 0.8s linear infinite;
        opacity: 1;
    }
}

/* Enhanced press feedback for mobile */
@media (hover: none) and (pointer: coarse) {
    .button {
        /* Stronger press effect on mobile */
        &:active {
            transform: scale(0.92);
            transition-duration: 0.05s;
        }
        
        /* Larger ripple on mobile */
        &:active::after {
            animation: ripple-mobile 0.8s ease-out;
        }
    }
}

@keyframes ripple-mobile {
    0% {
        width: 0;
        height: 0;
        opacity: 0.6;
    }
    100% {
        width: 300px;
        height: 300px;
        opacity: 0;
    }
}

/* Smooth transitions for Turbo Frame updates */
.turbo-frame {
    .button {
        transition: all 0.3s var(--ease-standard);
    }
    
    /* Fade in new buttons */
    .button[data-turbo-loaded] {
        animation: fade-in 0.3s ease-out;
    }
}

@keyframes fade-in {
    from {
        opacity: 0;
        transform: translateY(4px);
    }
    to {
        opacity: 1;
        transform: translateY(0);
    }
}

/* Focus ring animation */
.button:focus-visible {
    outline: 2px solid var(--primary);
    outline-offset: 2px;
    animation: focus-ring 0.3s ease-out;
}

@keyframes focus-ring {
    from {
        outline-offset: 0;
        outline-width: 0;
    }
    to {
        outline-offset: 2px;
        outline-width: 2px;
    }
}