/* --------------------------------------------------------------------------
Mobile Polish & Optimization (Phase 5)
Premium mobile experience with optimized touch targets and UX
-------------------------------------------------------------------------- */

/* ===== VIEWPORT & DEVICE SETTINGS ===== */

/* Ensure proper mobile viewport */
@viewport {
  width: device-width;
  zoom: 1;
}

/* Prevent zooming on input focus (iOS) */
input,
textarea,
select {
  font-size: 16px; /* 16px prevents zoom on iOS */
}

/* ===== TOUCH TARGET OPTIMIZATION ===== */

/* All interactive elements: minimum 44×44px */
button,
a[role="button"],
.button,
input[type="button"],
input[type="submit"],
input[type="reset"],
input[type="checkbox"],
input[type="radio"],
.card-link,
.link-button {
  min-height: 44px;
  min-width: 44px;
  padding: 12px 16px;
}

/* Pagination links */
.navigation.pagination a,
.navigation.pagination span {
  min-height: 44px;
  min-width: 44px;
}

/* Menu items */
.main-navigation a {
  padding: 12px 16px;
  display: block;
  min-height: 44px;
  display: flex;
  align-items: center;
}

/* Category/Tag links */
.tag-cloud a,
.cat-links a,
.tags-links a {
  min-height: 40px;
  display: inline-flex;
  align-items: center;
  padding: 8px 12px;
}

/* Comment reply links */
.comment-reply-link,
.reply a {
  min-height: 44px;
  padding: 12px 16px;
  display: inline-flex;
  align-items: center;
}

/* ===== MOBILE TYPOGRAPHY ===== */

/* Ensure readable text */
body {
  -webkit-text-size-adjust: 100%;
  text-size-adjust: 100%;
}

/* Prevent horizontal scroll due to long words */
body,
.container,
main {
  overflow-wrap: break-word;
  word-break: break-word;
  word-wrap: break-word;
  hyphens: auto;
}

/* Code blocks on mobile */
pre,
code {
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
  white-space: pre-wrap;
  word-break: break-all;
}

/* ===== MOBILE IMAGE OPTIMIZATION ===== */

/* Responsive images */
img {
  max-width: 100%;
  height: auto;
  display: block;
}

/* Card images on mobile */
.card-thumbnail {
  max-width: 100%;
  height: auto;
}

/* Featured images */
.post-thumbnail img,
.featured-image img {
  width: 100%;
  height: auto;
}

/* ===== MOBILE FORM OPTIMIZATION ===== */

/* Full-width inputs on mobile */
input[type="text"],
input[type="email"],
input[type="url"],
input[type="password"],
input[type="search"],
input[type="tel"],
input[type="number"],
textarea,
select {
  width: 100%;
  max-width: 100%;
  font-size: 16px; /* Prevents zoom on iOS */
  padding: 12px;
  border-radius: 6px;
  border: 1px solid var(--color-border);
}

/* Form labels */
label {
  display: block;
  margin-bottom: 8px;
  font-weight: 500;
  font-size: 14px;
}

/* Form groups */
.form-group,
.form-field {
  margin-bottom: 20px;
}

/* Newsletter form on mobile */
.newsletter-form {
  flex-direction: column;
  gap: 12px;
}

.newsletter-form input[type="email"],
.newsletter-form button {
  width: 100%;
  min-height: 44px;
}

/* ===== MOBILE SPACING OPTIMIZATION ===== */

/* Increased padding for touch friendly layout */
@media (max-width: 767px) {
  /* Section padding */
  .hero,
  .featured-section,
  .categories-section,
  .recommendations-section,
  .newsletter-section {
    padding: 1.5rem 0;
  }

  /* Hero on mobile: remove tinted background block and keep typography consistent */
  .hero {
    background: transparent !important;
  }

  .hero-title {
    color: var(--color-text) !important;
  }

  .hero-description {
    color: var(--color-text-secondary) !important;
  }
}

/* Ensure the hero stays clean even when the device is in dark mode */
@media (prefers-color-scheme: dark) and (max-width: 767px) {
  .hero {
    background: transparent !important;
  }
}

@media (max-width: 767px) {
  
  /* Container padding */
  .container {
    padding: 0 16px;
  }
  
  /* Card spacing */
  .card {
    margin-bottom: 16px;
  }
  
  /* Post spacing */
  .post,
  .page {
    margin-bottom: 40px;
  }
  
  /* Comment spacing */
  .comment-body {
    margin-bottom: 24px;
  }
}

/* ===== MOBILE NAVIGATION - PROFESSIONAL FULLSCREEN MENU ===== */

@media (max-width: 991px) {
  /* Prevent horizontal scroll when menu is open */
  body.menu-open {
    overflow: hidden;
  }

  /* Navigation container */
  .main-navigation {
    position: static;
  }

  /* Hidden by default */
  .main-navigation ul {
    display: none;
    margin: 0;
    padding: 0;
    list-style: none;
  }

  /* Fullscreen overlay menu when active */
  .main-navigation ul.active {
    display: flex;
    flex-direction: column;
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    width: 100%;
    height: 100vh;
    height: 100dvh; /* Dynamic viewport height for mobile browsers */
    background: #ffffff;
    z-index: 9998;
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
    padding: 100px 24px 40px;
    box-sizing: border-box;
    animation: menuFadeIn 0.25s ease-out;
  }

  @keyframes menuFadeIn {
    from {
      opacity: 0;
      transform: translateY(-10px);
    }
    to {
      opacity: 1;
      transform: translateY(0);
    }
  }

  /* Menu items */
  .main-navigation ul.active li {
    border-bottom: 1px solid #e5e7eb;
  }

  .main-navigation ul.active li:last-child {
    border-bottom: none;
  }

  .main-navigation ul.active a {
    display: flex;
    align-items: center;
    padding: 20px 8px;
    font-size: 1.25rem;
    font-weight: 500;
    color: #111827;
    text-decoration: none;
    transition: color 0.2s ease, background-color 0.2s ease;
  }

  .main-navigation ul.active a:hover,
  .main-navigation ul.active a:focus {
    color: var(--color-primary);
    background-color: #f9fafb;
  }

  .main-navigation ul.active .current-menu-item a {
    color: var(--color-primary);
  }

  /* Submenu styling */
  .main-navigation ul.active ul {
    display: block;
    position: static;
    background: transparent;
    box-shadow: none;
    padding: 0 0 0 16px;
  }

  .main-navigation ul.active ul a {
    font-size: 1.1rem;
    padding: 16px 8px;
  }

  /* Dark header variant */
  body.header-dark .main-navigation ul.active {
    background: #111827;
  }

  body.header-dark .main-navigation ul.active li {
    border-bottom-color: #374151;
  }

  body.header-dark .main-navigation ul.active a {
    color: #f9fafb;
  }

  body.header-dark .main-navigation ul.active a:hover,
  body.header-dark .main-navigation ul.active a:focus {
    color: var(--color-primary-light);
    background-color: #1f2937;
  }

  /* Menu toggle button - ensure it's above the overlay */
  .menu-toggle {
    position: relative;
    z-index: 9999;
    background: transparent;
    border: none;
    padding: 8px;
    min-width: 44px;
    min-height: 44px;
    cursor: pointer;
  }

  .menu-toggle.active {
    position: fixed;
    top: 16px;
    right: 16px;
  }

  /* Hamburger to X animation */
  .menu-toggle-icon {
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    width: 24px;
    height: 24px;
    position: relative;
  }

  .menu-toggle-icon span {
    display: block;
    width: 24px;
    height: 2px;
    background-color: #111827;
    border-radius: 2px;
    transition: all 0.3s ease;
    position: absolute;
  }

  .menu-toggle-icon span:nth-child(1) {
    top: 6px;
  }

  .menu-toggle-icon span:nth-child(2) {
    top: 11px;
  }

  .menu-toggle-icon span:nth-child(3) {
    top: 16px;
  }

  /* Active state - X icon */
  .menu-toggle.active .menu-toggle-icon span {
    background-color: #111827;
  }

  .menu-toggle.active .menu-toggle-icon span:nth-child(1) {
    top: 11px;
    transform: rotate(45deg);
  }

  .menu-toggle.active .menu-toggle-icon span:nth-child(2) {
    opacity: 0;
  }

  .menu-toggle.active .menu-toggle-icon span:nth-child(3) {
    top: 11px;
    transform: rotate(-45deg);
  }

  /* Dark header toggle color */
  body.header-dark .menu-toggle-icon span {
    background-color: #f9fafb;
  }

  body.header-dark .menu-toggle.active .menu-toggle-icon span {
    background-color: #f9fafb;
  }
}

/* ===== MOBILE HEADER OPTIMIZATION ===== */

/* Sticky header optimization */
.site-header {
  position: sticky;
  top: 0;
  z-index: 999;
  padding: 12px 0;
}

/* Improve contrast on mobile: avoid overly transparent header over dark hero images */
@media (max-width: 991px) {
  :root {
    --header-height: 72px;
  }

  .site-header {
    background-color: rgba(255, 255, 255, 0.98);
    backdrop-filter: blur(12px);
    -webkit-backdrop-filter: blur(12px);
  }

  /* If the user selected dark header style, keep it dark but ensure branding is readable */
  body.header-dark .site-header {
    background-color: rgba(17, 24, 39, 0.98);
  }

  body.header-dark .site-header .site-title a {
    color: #f9fafb;
  }

  body.header-dark .site-header .site-description {
    color: #d1d5db;
  }

  /* Help logo visibility on dark headers */
  body.header-dark .custom-logo-link {
    background: rgba(255, 255, 255, 0.10);
    padding: 6px 8px;
    border-radius: 10px;
  }

  body.header-dark .custom-logo {
    display: block;
  }

  /* Subtle shadow improves readability over mixed backgrounds */
  .custom-logo {
    filter: drop-shadow(0 1px 2px rgba(0, 0, 0, 0.18));
  }
}

.site-header-inner {
  gap: 12px;
  padding: 8px 0;
}

/* Logo on mobile */
.custom-logo {
  max-height: 32px;
  width: auto;
}

/* Menu toggle size */
.menu-toggle {
  min-width: 44px;
  min-height: 44px;
  padding: 8px;
}

/* ===== MOBILE CARD OPTIMIZATION ===== */

/* Card sizing on mobile */
.card {
  border-radius: 8px;
  overflow: hidden;
}

/* Card visual ratio on mobile */
.card-visual {
  aspect-ratio: 16 / 10;
  min-height: 160px;
}

/* Larger badges on mobile for tappability */
.card-badge {
  padding: 6px 10px;
  font-size: 12px;
  border-radius: 4px;
}

/* ===== MOBILE TEXT READABILITY ===== */

/* Better line-height for mobile */
@media (max-width: 575px) {
  body {
    line-height: 1.6;
  }
  
  h1, h2, h3, h4, h5, h6 {
    line-height: 1.3;
  }
  
  p {
    line-height: 1.7;
  }
}

/* ===== MOBILE FOCUS STATES ===== */

/* Better focus on mobile */
@media (hover: none) and (pointer: coarse) {
  button:focus,
  a:focus,
  input:focus,
  textarea:focus,
  select:focus {
    outline: 3px solid var(--color-primary);
    outline-offset: 2px;
  }
}

/* ===== MOBILE ANIMATIONS OPTIMIZATION ===== */

/* Reduce animations on mobile for performance */
@media (max-width: 768px) {
  /* Shorter animations on mobile */
  * {
    animation-duration: 0.3s !important;
  }
  
  /* Disable hover on touch devices */
  @media (hover: none) and (pointer: coarse) {
    button:hover,
    a:hover,
    .button:hover {
      transform: none;
      box-shadow: none;
    }
  }
}

/* ===== MOBILE PERFORMANCE OPTIMIZATION ===== */

/* Prevent layout shift */
img,
video,
iframe {
  display: block;
}

/* Smooth scroll on mobile */
html {
  scroll-behavior: smooth;
}

/* Optimize painting */
.card,
.button,
button {
  will-change: transform, box-shadow;
}

/* ===== MOBILE FOOTER OPTIMIZATION ===== */

.site-footer {
  padding: 40px 0 20px;
}

.footer-widgets {
  gap: 24px;
}

.footer-menu {
  flex-direction: column;
  gap: 12px;
}

.footer-menu li {
  margin: 0;
  padding: 4px 0;
}

.footer-menu a {
  padding: 8px 0;
  display: block;
  min-height: 44px;
  display: flex;
  align-items: center;
}

/* ===== MOBILE VISIBILITY HELPERS ===== */

/* Hide on mobile if needed */
.hide-mobile {
  display: none !important;
}

/* Show only on mobile if needed */
.show-mobile {
  display: block !important;
}

@media (min-width: 768px) {
  .hide-mobile {
    display: block !important;
  }
  
  .show-mobile {
    display: none !important;
  }
}

/* ===== MOBILE SCROLL OPTIMIZATION ===== */

/* Smooth momentum scrolling */
.scrollable,
.entry-content table,
pre {
  -webkit-overflow-scrolling: touch;
}

/* ===== MOBILE MODAL/POPUP OPTIMIZATION ===== */

/* Prevent body scroll when modal is open */
body.modal-open {
  overflow: hidden;
}

/* Modal optimization */
.modal,
.popup {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  z-index: 1000;
  display: flex;
  align-items: flex-end;
}

@media (min-height: 500px) {
  .modal,
  .popup {
    align-items: center;
  }
}

.modal-content,
.popup-content {
  width: 100%;
  max-width: 100%;
  border-radius: 16px 16px 0 0;
  max-height: 90vh;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
}

@media (min-height: 500px) {
  .modal-content,
  .popup-content {
    max-width: 500px;
    border-radius: 16px;
  }
}

/* ===== MOBILE BREAKPOINT OPTIMIZATION ===== */

/* Ultra-small devices (< 360px) */
@media (max-width: 359px) {
  .container {
    padding: 0 12px;
  }
  
  h1 { font-size: 1.75rem; }
  h2 { font-size: 1.375rem; }
  h3 { font-size: 1.125rem; }
  
  button, .button {
    padding: 10px 14px;
    font-size: 14px;
  }
}

/* Small devices (360px - 575px) */
@media (min-width: 360px) and (max-width: 575px) {
  .container {
    padding: 0 16px;
  }
}

/* Tablet devices (576px - 767px) */
@media (min-width: 576px) and (max-width: 767px) {
  .container {
    padding: 0 20px;
  }
  
  .posts-grid {
    grid-template-columns: repeat(2, 1fr);
  }
}

/* ===== SAFE AREA SUPPORT ===== */

/* iPhone X+ notch support */
@supports (padding: max(0px)) {
  body {
    padding-left: max(12px, env(safe-area-inset-left));
    padding-right: max(12px, env(safe-area-inset-right));
    padding-top: max(12px, env(safe-area-inset-top));
    padding-bottom: max(12px, env(safe-area-inset-bottom));
  }
  
  .site-header {
    padding-left: max(0px, env(safe-area-inset-left));
    padding-right: max(0px, env(safe-area-inset-right));
  }
}

/* ===== DARK MODE SUPPORT ===== */
/*
  Disabled on mobile.
  This theme already has its own styling and we were getting low-contrast text
  on iOS when the OS is in dark mode.
  If we want a true dark mode later, we should implement it consistently across
  all stylesheets, not only here.
*/

/* ===== PRINT OPTIMIZATION ===== */

@media print {
  body {
    background: white;
    color: black;
  }
  
  .site-header,
  .site-footer,
  .navigation,
  .comments-area,
  .newsletter-section {
    display: none;
  }
  
  .entry-content,
  .page-content {
    max-width: 100%;
  }
}
