/**
 * ily.dog Accessibility & Responsive Design
 * WCAG 2.1 AA compliant styles and responsive utilities
 */

/* ========== Focus Management ========== */

/* Enhanced focus states */
:focus {
    outline: none;
}

:focus-visible {
    outline: 2px solid var(--color-border-focus);
    outline-offset: 2px;
    border-radius: var(--radius-sm);
}

/* Focus within for complex components */
:focus-within {
    outline: 2px solid var(--color-border-focus);
    outline-offset: 2px;
    border-radius: var(--radius-sm);
}

/* Skip to main content link */
.skip-link {
    position: absolute;
    top: -40px;
    left: 0;
    background: var(--color-accent-primary);
    color: white;
    padding: var(--space-3) var(--space-4);
    z-index: var(--z-tooltip);
    transition: top var(--transition-fast);
}

.skip-link:focus {
    top: 0;
}

/* ========== Screen Reader Only ========== */
.sr-only {
    position: absolute;
    width: 1px;
    height: 1px;
    padding: 0;
    margin: -1px;
    overflow: hidden;
    clip: rect(0, 0, 0, 0);
    white-space: nowrap;
    border-width: 0;
}

.sr-only-focusable:focus,
.sr-only-focusable:active {
    position: static;
    width: auto;
    height: auto;
    padding: inherit;
    margin: inherit;
    overflow: visible;
    clip: auto;
    white-space: inherit;
}

/* ========== ARIA States ========== */

/* Disabled state */
[aria-disabled="true"],
.disabled {
    opacity: 0.5;
    cursor: not-allowed;
    pointer-events: none;
}

/* Hidden state */
[aria-hidden="true"] {
    display: none !important;
}

/* Expanded state */
[aria-expanded="false"] + .expandable-content {
    display: none;
}

[aria-expanded="true"] + .expandable-content {
    display: block;
}

/* Selected state */
[aria-selected="true"] {
    background: var(--color-accent-primary-subtle);
    border-color: var(--color-accent-primary);
}

/* Busy state */
[aria-busy="true"] {
    cursor: wait;
}

/* ========== High Contrast Mode ========== */
@media (prefers-contrast: high) {
    :root {
        --color-bg-primary: #000000;
        --color-bg-secondary: #1a1a1a;
        --color-bg-tertiary: #333333;
        --color-text-primary: #ffffff;
        --color-text-secondary: #e0e0e0;
        --color-border-primary: #666666;
        --color-accent-primary: #00ffff;
        --color-accent-success: #00ff00;
        --color-accent-danger: #ff0000;
        --color-accent-warning: #ffff00;
    }
    
    .btn,
    .card,
    .input,
    .select,
    .textarea {
        border-width: 2px;
    }
}

/* ========== Reduced Motion ========== */
@media (prefers-reduced-motion: reduce) {
    *,
    *::before,
    *::after {
        animation-duration: 0.01ms !important;
        animation-iteration-count: 1 !important;
        transition-duration: 0.01ms !important;
        scroll-behavior: auto !important;
    }
    
    .animate-spin,
    .animate-pulse,
    .animate-bounce,
    .animate-float {
        animation: none !important;
    }
}

/* ========== Color Scheme ========== */
@media (prefers-color-scheme: light) {
    :root {
        /* Light mode colors */
        --color-bg-primary: #ffffff;
        --color-bg-secondary: #f8f9fa;
        --color-bg-tertiary: #e9ecef;
        --color-bg-elevated: #ffffff;
        --color-text-primary: #212529;
        --color-text-secondary: #495057;
        --color-text-muted: #6c757d;
        --color-border-primary: #dee2e6;
        --color-border-secondary: #ced4da;
    }
}

/* ========== Responsive Typography ========== */
/* Fluid typography that scales with viewport */
h1 {
    font-size: clamp(1.8rem, 1.5rem + 1.5vw, 2.5rem);
}

h2 {
    font-size: clamp(1.5rem, 1.25rem + 1.25vw, 2rem);
}

h3 {
    font-size: clamp(1.25rem, 1.1rem + 0.75vw, 1.5rem);
}

h4 {
    font-size: clamp(1.1rem, 1rem + 0.5vw, 1.25rem);
}

p, li, td, th, label, input, textarea, select, button {
    font-size: clamp(0.9rem, 0.85rem + 0.25vw, 1rem);
}

/* ========== Responsive Spacing ========== */
/* Container with responsive padding */
.container {
    padding-left: clamp(1rem, 5vw, 3rem);
    padding-right: clamp(1rem, 5vw, 3rem);
}

/* Responsive gaps */
.gap-responsive {
    gap: clamp(1rem, 3vw, 2rem);
}

/* ========== Responsive Grid ========== */
/* Auto-fit grid with minimum column width */
.grid-auto {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(min(100%, 300px), 1fr));
    gap: var(--space-6);
}

/* Responsive columns */
.grid-responsive-2 {
    grid-template-columns: repeat(auto-fit, minmax(min(100%, 400px), 1fr));
}

.grid-responsive-3 {
    grid-template-columns: repeat(auto-fit, minmax(min(100%, 300px), 1fr));
}

.grid-responsive-4 {
    grid-template-columns: repeat(auto-fit, minmax(min(100%, 250px), 1fr));
}

/* ========== Responsive Breakpoints ========== */

/* Small devices (landscape phones, 576px and up) */
@media (min-width: 576px) {
    .container {
        max-width: 540px;
    }
    
    .sm\:hidden {
        display: none;
    }
    
    .sm\:block {
        display: block;
    }
    
    .sm\:flex {
        display: flex;
    }
    
    .sm\:grid {
        display: grid;
    }
}

/* Medium devices (tablets, 768px and up) */
@media (min-width: 768px) {
    .container {
        max-width: 720px;
    }
    
    .md\:hidden {
        display: none;
    }
    
    .md\:block {
        display: block;
    }
    
    .md\:flex {
        display: flex;
    }
    
    .md\:grid {
        display: grid;
    }
    
    .md\:flex-row {
        flex-direction: row;
    }
    
    .md\:flex-col {
        flex-direction: column;
    }
}

/* Large devices (desktops, 992px and up) */
@media (min-width: 992px) {
    .container {
        max-width: 960px;
    }
    
    .lg\:hidden {
        display: none;
    }
    
    .lg\:block {
        display: block;
    }
    
    .lg\:flex {
        display: flex;
    }
    
    .lg\:grid {
        display: grid;
    }
}

/* Extra large devices (large desktops, 1200px and up) */
@media (min-width: 1200px) {
    .container {
        max-width: 1140px;
    }
    
    .xl\:hidden {
        display: none;
    }
    
    .xl\:block {
        display: block;
    }
    
    .xl\:flex {
        display: flex;
    }
    
    .xl\:grid {
        display: grid;
    }
}

/* ========== Mobile-First Responsive Utilities ========== */

/* Hide on mobile, show on desktop */
.mobile\:hidden {
    display: none;
}

@media (min-width: 768px) {
    .mobile\:hidden {
        display: block;
    }
}

/* Show on mobile, hide on desktop */
.mobile\:block {
    display: block;
}

@media (min-width: 768px) {
    .mobile\:block {
        display: none;
    }
}

/* ========== Touch-Friendly Sizing ========== */
/* Larger touch targets on mobile */
@media (max-width: 767px) {
    .btn,
    button,
    [role="button"] {
        min-height: 44px;
        min-width: 44px;
        padding: var(--space-3) var(--space-4);
    }
    
    .input,
    .select,
    .textarea {
        min-height: 44px;
        padding: var(--space-3) var(--space-4);
    }
    
    .nav-link,
    a[role="button"] {
        padding: var(--space-3) var(--space-4);
    }
}

/* ========== Print Styles ========== */
@media print {
    *,
    *::before,
    *::after {
        background: transparent !important;
        color: #000 !important;
        box-shadow: none !important;
        text-shadow: none !important;
    }
    
    a,
    a:visited {
        text-decoration: underline;
    }
    
    a[href]::after {
        content: " (" attr(href) ")";
    }
    
    abbr[title]::after {
        content: " (" attr(title) ")";
    }
    
    a[href^="#"]::after,
    a[href^="javascript:"]::after {
        content: "";
    }
    
    pre {
        white-space: pre-wrap !important;
    }
    
    pre,
    blockquote {
        border: 1px solid #999;
        page-break-inside: avoid;
    }
    
    tr,
    img {
        page-break-inside: avoid;
    }
    
    p,
    h2,
    h3 {
        orphans: 3;
        widows: 3;
    }
    
    h2,
    h3 {
        page-break-after: avoid;
    }
    
    .header,
    .footer,
    nav,
    .sidebar {
        display: none !important;
    }
    
    .container {
        max-width: none !important;
        padding: 0 !important;
    }
}

/* ========== Landscape Mode ========== */
@media (orientation: landscape) and (max-height: 500px) {
    .header {
        padding: var(--space-2) 0;
    }
    
    .modal-body {
        max-height: 60vh;
    }
}

/* ========== Safe Area Insets (for notched devices) ========== */
@supports (padding: max(0px)) {
    .container {
        padding-left: max(var(--space-6), env(safe-area-inset-left));
        padding-right: max(var(--space-6), env(safe-area-inset-right));
    }
    
    .footer {
        padding-bottom: max(var(--space-8), env(safe-area-inset-bottom));
    }
}

/* ========== Dark Mode Preference ========== */
@media (prefers-color-scheme: dark) {
    :root {
        color-scheme: dark;
    }
}

/* ========== Light Mode Preference ========== */
@media (prefers-color-scheme: light) {
    :root {
        color-scheme: light;
    }
}

/* ========== Forced Colors Mode (Windows High Contrast) ========== */
@media (forced-colors: active) {
    .btn,
    .card,
    .input,
    .select,
    .textarea {
        border: 2px solid ButtonText;
    }
    
    .btn-primary {
        background: Highlight;
        color: HighlightText;
    }
    
    .badge,
    .status-badge {
        border: 1px solid ButtonText;
    }
}

/* ========== Container Queries (Progressive Enhancement) ========== */
@supports (container-type: inline-size) {
    .container-query {
        container-type: inline-size;
    }
    
    @container (min-width: 400px) {
        .cq\:grid-2 {
            grid-template-columns: repeat(2, 1fr);
        }
    }
    
    @container (min-width: 600px) {
        .cq\:grid-3 {
            grid-template-columns: repeat(3, 1fr);
        }
    }
    
    @container (min-width: 800px) {
        .cq\:grid-4 {
            grid-template-columns: repeat(4, 1fr);
        }
    }
}

/* ========== Responsive Images ========== */
img,
video,
svg {
    max-width: 100%;
    height: auto;
}

/* ========== Responsive Tables ========== */
.table-responsive {
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
}

@media (max-width: 767px) {
    .table-responsive table {
        min-width: 600px;
    }
}

/* ========== Responsive Typography Scale ========== */
/* More aggressive scaling on mobile */
@media (max-width: 640px) {
    h1 {
        font-size: clamp(1.5rem, 1.25rem + 1vw, 2rem);
    }
    
    h2 {
        font-size: clamp(1.25rem, 1.1rem + 0.75vw, 1.5rem);
    }
    
    h3 {
        font-size: clamp(1.1rem, 1rem + 0.5vw, 1.25rem);
    }
}

/* ========== Responsive Spacing Scale ========== */
@media (max-width: 640px) {
    .space-y-responsive > * + * {
        margin-top: clamp(1rem, 3vw, 2rem);
    }
    
    .space-x-responsive > * + * {
        margin-left: clamp(1rem, 3vw, 2rem);
    }
}

/* ========== Responsive Card Layout ========== */
@media (max-width: 767px) {
    .card {
        padding: var(--space-4);
    }
    
    .card-actions {
        flex-direction: column;
    }
    
    .card-actions .btn {
        width: 100%;
    }
}

/* ========== Responsive Navigation ========== */
@media (max-width: 767px) {
    .header-nav {
        flex-wrap: wrap;
        justify-content: center;
        gap: var(--space-2);
    }
    
    .header-nav a {
        padding: var(--space-2) var(--space-3);
        font-size: var(--text-xs);
    }
}

/* ========== Responsive Modal ========== */
@media (max-width: 767px) {
    .modal {
        margin: var(--space-4);
        max-width: calc(100% - var(--space-8));
        max-height: calc(100vh - var(--space-8));
    }
    
    .modal-header,
    .modal-body,
    .modal-footer {
        padding: var(--space-4);
    }
}

/* ========== Responsive Forms ========== */
@media (max-width: 640px) {
    .form-row {
        flex-direction: column;
    }
    
    .form-row > * {
        width: 100%;
    }
}

/* ========== Responsive Stats Grid ========== */
@media (max-width: 767px) {
    .stats-grid {
        grid-template-columns: repeat(2, 1fr);
    }
}

@media (max-width: 480px) {
    .stats-grid {
        grid-template-columns: 1fr;
    }
}