/* =============================================================================
   MOBILE BIODIVERSITY PWA - RESPONSIVE STYLES
   BEM methodology with mobile-first approach
   ============================================================================= */

/* -----------------------------------------------------------------------------
   01. MOBILE TOUCH & ACCESSIBILITY ENHANCEMENTS
   ----------------------------------------------------------------------------- */
@media (pointer: coarse) {
    .filter__btn,
    .species-card,
    .search-results__item,
    .btn--icon,
    .select,
    .btn--secondary,
    .btn--primary {
        min-height: 44px;
        -webkit-touch-callout: none;
        -webkit-user-select: none;
        user-select: none;
    }
}

/* iOS Safari optimizations */
@supports (-webkit-touch-callout: none) {
    body {
        -webkit-text-size-adjust: 100%;
    }
    
    .header {
        padding-top: max(1rem, env(safe-area-inset-top));
        padding-left: max(1rem, env(safe-area-inset-left));
        padding-right: max(1rem, env(safe-area-inset-right));
    }
    
    .app__content {
        padding-bottom: env(safe-area-inset-bottom);
    }
    
    .input {
        -webkit-appearance: none;
        border-radius: 0.5rem;
    }
    
    .species-card {
        -webkit-transform: translateZ(0);
        transform: translateZ(0);
    }
}

/* Prevent zoom on input focus */
@media screen and (max-width: 767px) {
    .input {
        font-size: 16px;
    }
}

/* High-DPI screen optimizations */
@media (-webkit-min-device-pixel-ratio: 2), (min-resolution: 192dpi) {
    .species-card__image {
        image-rendering: -webkit-optimize-contrast;
        image-rendering: crisp-edges;
    }
}

/* -----------------------------------------------------------------------------
   02. MOBILE LAYOUT (max-width: 768px)
   ----------------------------------------------------------------------------- */
@media (max-width: 768px) {
    /* Species grid mobile layout */
    .species-grid {
        grid-template-columns: repeat(auto-fill, minmax(180px, 1fr));
        gap: 0.75rem;
        padding: 0.75rem;
    }
    
    .species-card {
        border-radius: 0.75rem;
        aspect-ratio: 1;
    }
    
    .species-card__overlay {
        padding: 1.5rem 0.75rem 0.75rem;
    }
    
    .species-card__name {
        font-size: 1rem;
    }
    
    .species-card__scientific {
        font-size: 0.8rem;
    }
    
    /* Filter adjustments */
    .filter__container {
        padding: 0 0.75rem;
    }
    
    .filter__btn {
        min-width: 60px;
        padding: 0.5rem;
    }
    
    .filter__icon {
        font-size: 1.25rem;
    }
    
    .filter__text {
        font-size: 0.75rem;
    }
    
    /* Status badge mobile sizing */
    .status-badge {
        font-size: 0.8rem;
        padding: 0.4rem 0.8rem;
    }
    
    /* Map mobile optimizations */
    .map {
        height: max(200px, min(35vh, 300px));
        margin-bottom: 0.75rem;
        min-height: 180px;
    }
    
    .modal--location .map {
        height: calc(100% - 200px);
        min-height: 180px;
        flex: 1;
    }
    
    .btn--primary {
        padding: 1rem;
        font-size: 1.1rem;
        min-height: 52px;
    }
    
    /* Map controls mobile enhancement */
    .map .leaflet-control-zoom {
        margin-left: 8px;
        margin-top: 8px;
    }
    
    .map .leaflet-control-zoom a {
        width: 36px;
        height: 36px;
        line-height: 34px;
        font-size: 20px;
    }
    
    .map .leaflet-control-attribution {
        font-size: 0.6rem;
        padding: 2px 4px;
    }
    
    /* Pull-to-refresh space */
    .app__content {
        overscroll-behavior-y: contain;
    }
}

/* -----------------------------------------------------------------------------
   03. SMALL MOBILE LAYOUT (max-width: 480px)
   ----------------------------------------------------------------------------- */
@media (max-width: 480px) {
    .header__content {
        gap: 0.5rem;
    }
    
    .header__title {
        font-size: 1.1rem;
    }
    
    .btn--icon,
    .select {
        min-width: 40px;
        min-height: 40px;
        padding: 0.4rem;
    }
    
    .species-grid {
        grid-template-columns: repeat(auto-fill, minmax(160px, 1fr));
        gap: 0.5rem;
        padding: 0.5rem;
    }
    
    /* Modal optimizations */
    .modal__content {
        margin: 0.5rem;
        border-radius: 0.75rem;
    }
    
    .modal__body {
        padding: 1.5rem;
    }
    
    /* Location modal mobile positioning */
    .modal--location {
        padding-top: max(1vh, 0.5rem);
        padding-bottom: max(1vh, 0.5rem);
        align-items: flex-start;
    }
    
    .modal--location .modal__content {
        height: min(90vh, 600px);
        max-height: 95vh;
    }
    
    .modal--location .modal__body {
        padding: 1rem;
        gap: 0.75rem;
    }
    
    .modal--location .map {
        height: calc(100% - 180px);
        min-height: 150px;
    }
    
    .modal--share,
    .modal--taxon {
        padding-top: 10vh;
        align-items: flex-start;
    }
    
    /* Status badge extra small sizing */
    .status-badge {
        font-size: 0.75rem;
        padding: 0.3rem 0.6rem;
        margin: 0 0.25rem 0 0;
    }
}

/* -----------------------------------------------------------------------------
   04. EXTRA SMALL MOBILE (max-width: 360px)
   ----------------------------------------------------------------------------- */
@media (max-width: 360px) {
    .header__controls {
        gap: 0.25rem;
    }
    
    .btn--icon,
    .select {
        min-width: 36px;
        min-height: 36px;
        padding: 0.3rem;
    }
    
    .species-grid {
        grid-template-columns: repeat(auto-fill, minmax(150px, 1fr));
    }
    
    .filter__container {
        gap: 0.25rem;
    }
    
    .filter__btn {
        min-width: 50px;
        padding: 0.4rem 0.2rem;
    }
    
    .filter__text {
        font-size: 0.7rem;
    }
    
    /* Very small screen modal optimizations */
    .modal--location .modal__content {
        height: min(95vh, 500px);
    }
    
    .modal--location .map {
        height: calc(100% - 160px);
        min-height: 120px;
    }
    
}

/* -----------------------------------------------------------------------------
   05. LANDSCAPE MOBILE ORIENTATION
   ----------------------------------------------------------------------------- */
@media screen and (max-height: 500px) and (orientation: landscape) {
    .header {
        padding: 0.5rem 1rem;
    }
    
    .header__title {
        font-size: 1rem;
    }
    
    .filter {
        padding: 0.25rem 0;
    }
    
    .filter__btn {
        padding: 0.5rem;
        min-width: 55px;
    }
    
    .filter__icon {
        font-size: 1.1rem;
    }
    
    .filter__text {
        font-size: 0.7rem;
    }
    
    .species-grid {
        grid-template-columns: repeat(auto-fill, minmax(140px, 1fr));
        gap: 0.5rem;
        padding: 0.5rem;
    }
    
    .species-card__overlay {
        padding: 1rem 0.5rem 0.5rem;
    }
    
    .species-card__name {
        font-size: 0.9rem;
    }
    
    .species-card__scientific {
        font-size: 0.75rem;
    }
    
    /* Modal landscape optimizations */
}

/* -----------------------------------------------------------------------------
   06. PWA STANDALONE MODE
   ----------------------------------------------------------------------------- */
@media all and (display-mode: standalone) {
    .header {
        padding-top: max(1rem, env(safe-area-inset-top));
        -webkit-app-region: drag;
    }
    
    .header__controls {
        -webkit-app-region: no-drag;
    }
    
    .header__title {
        -webkit-app-region: no-drag;
    }
}

/* -----------------------------------------------------------------------------
   07. TABLET LANDSCAPE (768px - 1024px)
   ----------------------------------------------------------------------------- */
@media screen and (min-width: 768px) and (max-width: 1024px) and (orientation: landscape) {
    .species-grid {
        grid-template-columns: repeat(auto-fill, minmax(200px, 1fr));
        gap: 1rem;
        padding: 1rem;
    }
    
    .filter__container {
        justify-content: center;
        max-width: 100%;
        flex-wrap: wrap;
    }
    
    .app__content {
        max-width: 1200px;
        margin: 0 auto;
    }
}

/* -----------------------------------------------------------------------------
   08. DARK MODE SUPPORT
   ----------------------------------------------------------------------------- */
@media (prefers-color-scheme: dark) {
    :root {
        color-scheme: dark;
    }
    
    body {
        background-color: #121212;
        color: #ffffff;
    }
    
    .app {
        background-color: #121212;
    }
    
    .app__content {
        background-color: #121212;
    }
    
    .species-grid {
        background-color: #121212;
    }
    
    .species-card {
        background: #1e1e1e;
        box-shadow: 0 4px 12px rgba(0, 0, 0, 0.3);
    }
    
    .filter {
        background: #1e1e1e;
        border-bottom-color: #333;
    }
    
    .filter__btn {
        background: rgba(30, 30, 30, 0.8);
        border-color: rgba(76, 175, 80, 0.2);
        color: #ccc;
        box-shadow: 0 1px 3px rgba(0, 0, 0, 0.2);
    }
    
    .filter__btn:hover {
        background: rgba(46, 125, 50, 0.15);
        border-color: rgba(76, 175, 80, 0.4);
        box-shadow: 0 2px 8px rgba(76, 175, 80, 0.2);
    }
    
    .filter__btn.is-active {
        background: rgba(46, 125, 50, 0.25);
        border-color: #4CAF50;
        color: #4CAF50;
        box-shadow: 0 3px 12px rgba(76, 175, 80, 0.3);
    }
    
    .filter__icon {
        filter: drop-shadow(0 1px 2px rgba(255, 255, 255, 0.1)) grayscale(100%);
    }
    
    .filter__btn:hover .filter__icon,
    .filter__btn.is-active .filter__icon {
        filter: drop-shadow(0 1px 2px rgba(255, 255, 255, 0.1)) grayscale(0%);
    }
    
    .modal__content {
        background: #1e1e1e;
        color: #ffffff;
    }
    
    .modal__title {
        color: #e8e8e8;
        font-weight: 600;
        text-shadow: 0 1px 2px rgba(0, 0, 0, 0.3);
    }
    
    .modal__body p {
        color: #b8b8b8;
    }
    
    .modal__body p strong {
        color: #e0e0e0;
    }
    
    .modal__close {
        background: rgba(255, 255, 255, 0.1);
    }
    
    .modal__close:hover {
        background: rgba(255, 255, 255, 0.2);
    }
    
    .input {
        background: #2a2a2a;
        border-color: #444;
        color: #ffffff;
    }
    
    .input::placeholder {
        color: #aaa;
    }
    
    .search-results__item {
        background: #2a2a2a;
        border-color: #444;
    }
    
    .search-results__item:hover {
        background: rgba(46, 125, 50, 0.1);
    }
    
    .search-results__primary-name {
        color: #ffffff;
    }
    
    .search-results__coords,
    .search-results__secondary-name {
        color: #aaa;
    }
    
    .search-results__rank {
        background: #404040;
        color: #aaa;
    }
    
    .share__url {
        background: #2a2a2a;
        color: #ffffff;
    }
    
    .loading__spinner {
        border-color: #444;
        border-top-color: #4CAF50;
    }
    
    .location-confirm {
        background: rgba(76, 175, 80, 0.1);
        border-color: rgba(76, 175, 80, 0.3);
    }
    
    .location-confirm__name {
        color: #e8e8e8;
    }
    
    .location-confirm__coords {
        color: #aaa;
    }
    
    /* Help modal dark mode */
    .help__title {
        color: #4CAF50;
    }
    
    .help__content a {
        color: #4CAF50;
    }
    
    .help__version {
        color: #aaa;
    }
    
    .help__version-number {
        color: #4CAF50;
    }
    
    .help__check-time {
        color: #666;
    }
    
    .help__update-btn {
        color: #4CAF50;
        border-color: #4CAF50;
    }
    
    .help__update-btn:hover {
        background: #4CAF50;
        color: #1e1e1e;
    }
    
    /* Photo attribution in dark mode */
    .photo-attribution {
        color: #aaaaaa;
    }
}

/* -----------------------------------------------------------------------------
   09. HIGH CONTRAST MODE
   ----------------------------------------------------------------------------- */
@media (prefers-contrast: high) {
    .species-card__overlay {
        background: linear-gradient(transparent, rgba(0, 0, 0, 0.9));
    }
    
    .species-card__name,
    .species-card__scientific {
        text-shadow: 0 2px 4px rgba(0, 0, 0, 0.8);
    }
    
    .filter__btn.is-active {
        border-color: #000;
        background: #fff;
        color: #000;
    }
    
    .header {
        background: #000;
    }
}

/* -----------------------------------------------------------------------------
   10. REDUCED MOTION
   ----------------------------------------------------------------------------- */
@media (prefers-reduced-motion: reduce) {
    * {
        animation-duration: 0.01ms !important;
        animation-iteration-count: 1 !important;
        transition-duration: 0.01ms !important;
        scroll-behavior: auto !important;
    }
    
    .loading__spinner {
        animation: none;
        border: 3px solid #2E7D32;
    }
}

/* -----------------------------------------------------------------------------
   11. HELP MODAL RESPONSIVE
   ----------------------------------------------------------------------------- */
@media (max-width: 640px) {
    .modal--help {
        padding: 1rem 0.5rem;
        padding-top: 5vh !important;
    }
    
    .modal--help .modal__content {
        margin: 0;
        border-radius: 0.75rem;
    }
    
    .modal--help .modal__body {
        padding: 1.5rem;
    }
    
    .help__content p {
        font-size: 1rem;
        margin-bottom: 0.7rem;
        line-height: 1.5;
    }
    
    .help__links a {
        font-size: 1rem;
        padding: 0.8rem 1.6rem;
    }
}