/**
 * Unified Button Component System
 * Single source of truth for ALL button styles across the application
 *
 * This file consolidates button styles from:
 * - styles.css (base button system)
 * - product-register.css (enterprise button system)
 * - dashboard.css, products.css, account.css (custom variants)
 * - detail.css (action buttons)
 * - modal.css, auth-modal.css (modal buttons)
 *
 * Uses global transitions from transitions.css for theme switching
 *
 * Usage:
 * - Simple: <button class="btn btn-primary">Save</button>
 * - With size: <button class="btn btn-secondary btn-sm">Cancel</button>
 * - With icon: <button class="btn btn-primary btn-icon-left"><svg>...</svg> Save</button>
 * - Link style: <a href="..." class="btn btn-outline">Learn More</a>
 * - PHP component: renderButton(['variant' => 'primary', 'text' => 'Save'])
 */

/* ===== BASE BUTTON ===== */

.btn {
    /* Layout */
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 0.5rem;

    /* Spacing */
    padding: 0.625rem 1.25rem;

    /* Typography */
    font-family: var(--font-family);
    font-size: var(--font-size-base, 1rem);
    font-weight: 500;
    line-height: 1.5;
    text-align: center;
    text-decoration: none;
    white-space: nowrap;

    /* Appearance */
    background: transparent;
    border: 2px solid transparent;
    border-radius: 8px;
    cursor: pointer;
    user-select: none;

    /* Accessibility - Minimum touch target size */
    min-height: 44px;
    min-width: 44px;

    /* Transitions - Inherited from transitions.css global rules */
    /* Theme properties (background-color, color, border-color) transition at 1s */
    /* Interactive properties (transform, opacity, box-shadow) transition at 0.2s */
}

/* Link buttons should behave like buttons */
a.btn {
    vertical-align: middle;
}

/* Button states */
.btn:focus-visible {
    outline: none;
    box-shadow: var(--focus-ring);
}

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

/* ===== BUTTON VARIANTS ===== */

/* Primary - Main actions */
.btn-primary {
    background: var(--primary);
    color: var(--text-inverse);
    border-color: var(--primary);
}

.btn-primary:hover:not(:disabled) {
    background: var(--primary-dark);
    border-color: var(--primary-dark);
    transform: translateY(-1px);
    box-shadow: var(--shadow-md);
}

.btn-primary:active:not(:disabled) {
    transform: translateY(0);
}

/* Secondary - Secondary actions */
.btn-secondary {
    background: transparent;
    color: var(--primary);
    border-color: var(--border);
}

.btn-secondary:hover:not(:disabled) {
    background: var(--bg-hover);
    border-color: var(--primary);
}

/* Accent - Call-to-action emphasis (sparingly) */
.btn-accent {
    background: var(--accent-accessible);
    color: #181818;
    border-color: var(--accent-accessible);
}

.btn-accent:hover:not(:disabled) {
    background: var(--accent-dark);
    border-color: var(--accent-dark);
    transform: translateY(-1px);
    box-shadow: var(--shadow-md);
}

.btn-accent:focus-visible {
    box-shadow: var(--focus-ring-accent);
}

/* Outline - Outlined buttons */
.btn-outline {
    background: transparent;
    color: var(--primary);
    border-color: var(--primary);
}

.btn-outline:hover:not(:disabled) {
    background: var(--primary);
    color: var(--text-inverse);
}

/* Success - Confirmation actions */
.btn-success {
    background: var(--success);
    color: white;
    border-color: var(--success);
}

.btn-success:hover:not(:disabled) {
    background: var(--success-dark);
    border-color: var(--success-dark);
    transform: translateY(-1px);
    box-shadow: var(--shadow-md);
}

/* Danger - Destructive actions */
.btn-danger {
    background: var(--error);
    color: white;
    border-color: var(--error);
}

.btn-danger:hover:not(:disabled) {
    background: var(--error-dark);
    border-color: var(--error-dark);
    transform: translateY(-1px);
    box-shadow: var(--shadow-md);
}

/* Warning - Alert actions */
.btn-warning {
    background: var(--warning);
    color: #181818;
    border-color: var(--warning);
}

.btn-warning:hover:not(:disabled) {
    background: var(--warning-dark);
    border-color: var(--warning-dark);
    transform: translateY(-1px);
    box-shadow: var(--shadow-md);
}

/* Warning Outline */
.btn-warning-outline {
    background: transparent;
    color: var(--warning-dark);
    border-color: var(--warning);
}

.btn-warning-outline:hover:not(:disabled) {
    background: var(--warning);
    color: #181818;
}

/* Ghost - Minimal button */
.btn-ghost {
    background: transparent;
    color: var(--text-primary);
    border-color: transparent;
}

.btn-ghost:hover:not(:disabled) {
    background: var(--bg-hover);
}

/* Link - Text-only button */
.btn-link {
    background: transparent;
    color: var(--primary);
    border-color: transparent;
    text-decoration: underline;
    min-height: auto;
    min-width: auto;
    padding: 0.25rem 0.5rem;
}

.btn-link:hover:not(:disabled) {
    color: var(--primary-dark);
    text-decoration: none;
}

/* Google OAuth */
.btn-google {
    background: white;
    color: #1f1f1f;
    border-color: #dadce0;
    box-shadow: 0 1px 2px rgba(0, 0, 0, 0.1);
}

.btn-google:hover:not(:disabled) {
    background: #f8f9fa;
    box-shadow: 0 1px 3px rgba(0, 0, 0, 0.15);
}

/* Outline White - For dark backgrounds (landing page hero) */
.btn-outline-white {
    background: transparent;
    color: white;
    border-color: rgba(255, 255, 255, 0.5);
}

.btn-outline-white:hover:not(:disabled) {
    background: rgba(255, 255, 255, 0.1);
    border-color: white;
    color: white;
}

/* ===== SIZE VARIANTS ===== */

.btn-xs {
    padding: 0.25rem 0.5rem;
    font-size: 0.75rem;
    min-height: 28px;
    border-radius: 4px;
}

.btn-sm,
.btn-small {
    padding: 0.375rem 0.75rem;
    font-size: 0.875rem;
    min-height: 36px;
    border-radius: 6px;
}

/* .btn-md is the default, no additional class needed */

.btn-lg,
.btn-large {
    padding: 0.875rem 1.75rem;
    font-size: 1.125rem;
    min-height: 52px;
    border-radius: 10px;
}

.btn-xl {
    padding: 1.125rem 2.25rem;
    font-size: 1.25rem;
    min-height: 60px;
    border-radius: 12px;
}

/* ===== WIDTH VARIANTS ===== */

.btn-block,
.btn-full-width {
    width: 100%;
    display: flex;
}

/* ===== ICON BUTTONS ===== */

/* Icon-only button (square) */
.btn-icon {
    padding: 0.625rem;
    aspect-ratio: 1;
    gap: 0;
}

.btn-icon svg,
.btn-icon img {
    width: 1.25rem;
    height: 1.25rem;
}

/* Icon with text - icon on left (default with gap) */
.btn-icon-left {
    /* gap already defined in base .btn */
}

.btn-icon-left svg,
.btn-icon-left img {
    width: 1.25rem;
    height: 1.25rem;
    margin-right: -0.125rem; /* Fine-tune spacing */
}

/* Icon with text - icon on right */
.btn-icon-right {
    flex-direction: row-reverse;
}

.btn-icon-right svg,
.btn-icon-right img {
    width: 1.25rem;
    height: 1.25rem;
    margin-left: -0.125rem; /* Fine-tune spacing */
}

/* Close button (common pattern) */
.btn-close {
    background: transparent;
    border: none;
    color: var(--text-secondary);
    font-size: 1.5rem;
    line-height: 1;
    padding: 0.25rem 0.5rem;
    min-height: auto;
    min-width: auto;
    cursor: pointer;
}

.btn-close:hover {
    color: var(--text-primary);
}

/* ===== STATE VARIANTS ===== */

/* Disabled state */
.btn:disabled,
.btn[aria-disabled="true"],
.btn.disabled {
    background-color: var(--btn-disabled-bg);
    border-color: var(--btn-disabled-border);
    color: var(--btn-disabled-text);
    cursor: not-allowed;
    opacity: 0.6;
    box-shadow: none;
    pointer-events: none;
    transform: none;
}

/* Loading state */
.btn.loading,
.btn-loading {
    position: relative;
    color: transparent;
    pointer-events: none;
}

.btn.loading::after,
.btn-loading::after {
    content: "";
    position: absolute;
    width: 16px;
    height: 16px;
    top: 50%;
    left: 50%;
    margin-left: -8px;
    margin-top: -8px;
    border: 2px solid currentColor;
    border-right-color: transparent;
    border-radius: 50%;
    animation: button-spin 0.6s linear infinite;
}

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

/* Active state (for toggles) */
.btn.active {
    background: var(--primary);
    color: var(--text-inverse);
    border-color: var(--primary);
}

.btn-outline.active,
.btn-secondary.active {
    background: var(--primary);
    color: var(--text-inverse);
    border-color: var(--primary);
}

/* ===== BUTTON GROUPS ===== */

.btn-group {
    display: inline-flex;
    gap: 0;
}

.btn-group .btn {
    border-radius: 0;
    border-right-width: 1px;
}

.btn-group .btn:first-child {
    border-top-left-radius: 8px;
    border-bottom-left-radius: 8px;
}

.btn-group .btn:last-child {
    border-top-right-radius: 8px;
    border-bottom-right-radius: 8px;
    border-right-width: 2px;
}

.btn-group .btn:not(:last-child) {
    border-right-color: rgba(255, 255, 255, 0.2);
}

/* ===== LEGACY COMPATIBILITY ===== */
/* Map old class names to new system for gradual migration */

.button {
    /* Alias for .btn */
    composes: btn;
}

.btn-text-small,
.btn-text-mini {
    background: none;
    border: none;
    color: var(--primary);
    font-size: 0.875rem;
    cursor: pointer;
    padding: 4px 8px;
    border-radius: 4px;
    min-height: auto;
}

.btn-text-small:hover,
.btn-text-mini:hover {
    background: var(--bg-hover);
}

.btn-text-mini {
    font-size: 0.75rem;
    padding: 2px 6px;
}

/* Action buttons (detail.php) */
.action-button {
    /* Same as .btn-outline */
    composes: btn btn-outline;
}

.action-button-track {
    composes: btn-outline;
}

.action-button-untrack {
    composes: btn-secondary;
}

.action-button-manage {
    composes: btn-success;
}

.action-button-own {
    composes: btn-primary;
}

/* Account buttons (account.php) */
.account-btn {
    composes: btn;
}

.account-btn-primary {
    composes: btn-primary;
}

.account-btn-secondary {
    composes: btn-secondary;
}

.account-btn-danger {
    composes: btn-danger;
}

/* ===== RESPONSIVE ===== */

@media (max-width: 768px) {
    .btn {
        padding: 0.5rem 1rem;
        font-size: 0.9375rem;
    }

    .btn-lg {
        padding: 0.75rem 1.5rem;
        font-size: 1rem;
    }

    .btn-xl {
        padding: 1rem 2rem;
        font-size: 1.125rem;
    }

    /* Allow buttons to stack on mobile */
    .btn-block-mobile {
        width: 100%;
        display: flex;
    }
}

@media (max-width: 480px) {
    .btn {
        font-size: 0.875rem;
    }
}

/* ===== DARK MODE ADJUSTMENTS ===== */

[data-theme="dark"] .btn-google {
    background: #2d2d2d;
    color: #e8eaed;
    border-color: #5f6368;
}

[data-theme="dark"] .btn-google:hover:not(:disabled) {
    background: #3c4043;
}

[data-theme="dark"] .btn-close {
    color: var(--text-secondary);
}

[data-theme="dark"] .btn-close:hover {
    color: var(--text-primary);
}

/* ===== ACCESSIBILITY ENHANCEMENTS ===== */

/* High contrast mode support */
@media (prefers-contrast: high) {
    .btn {
        border-width: 3px;
    }
}

/* Focus visible for keyboard navigation */
.btn:focus-visible {
    outline: 3px solid var(--primary);
    outline-offset: 2px;
}

/* Reduce motion for users who prefer it */
@media (prefers-reduced-motion: reduce) {
    .btn {
        transition-duration: 0.01ms;
    }

    .btn:hover {
        transform: none;
    }
}
