/**
 * Mobile Optimization CSS
 *
 * Comprehensive mobile performance + usability improvements
 * Include this in <head> after other stylesheets
 *
 * Covers:
 * - Touch target sizing (44px minimum)
 * - Responsive images + lazy loading
 * - Font optimization
 * - Performance (animations, transforms)
 * - Safe area insets (notch support)
 * - Readable font sizes (prevent zoom)
 */

/* ===================================================================
   ROOT VARIABLES & SAFE AREA
   =================================================================== */

:root {
  /* Colors */
  --rp-primary: #176E48;
  --rp-secondary: #0B2A4A;
  --rp-accent: #D4A574;
  --rp-background: #F6F1E7;
  --rp-text: #333;
  --rp-text-light: #666;
  --rp-border: #E0D5C7;
  --rp-error: #C32F27;

  /* Spacing (mobile-first) */
  --sp-xs: 4px;
  --sp-sm: 8px;
  --sp-md: 12px;
  --sp-lg: 16px;
  --sp-xl: 20px;
  --sp-2xl: 24px;
  --sp-3xl: 32px;

  /* Font sizes (readable on mobile, 16px minimum for inputs) */
  --fs-xs: 12px;
  --fs-sm: 14px;
  --fs-base: 16px;
  --fs-lg: 18px;
  --fs-xl: 20px;
  --fs-2xl: 24px;
  --fs-3xl: 28px;

  /* Line heights */
  --lh-tight: 1.2;
  --lh-normal: 1.5;
  --lh-relaxed: 1.75;

  /* Touch targets */
  --touch-target: 44px;
  --touch-padding: 12px;

  /* Safe area insets (for notched devices) */
  --safe-inset-top: env(safe-area-inset-top, 0);
  --safe-inset-right: env(safe-area-inset-right, 0);
  --safe-inset-bottom: env(safe-area-inset-bottom, 0);
  --safe-inset-left: env(safe-area-inset-left, 0);

  /* Z-index scale */
  --z-dropdown: 100;
  --z-sticky: 500;
  --z-fixed: 1000;
  --z-modal: 9999;
}

/* ===================================================================
   TYPOGRAPHY OPTIMIZATION
   =================================================================== */

/* Prevent iOS from increasing font size on landscape */
html {
  -webkit-text-size-adjust: 100%;
  -moz-text-size-adjust: 100%;
  text-size-adjust: 100%;

  /* Smooth scrolling */
  scroll-behavior: smooth;
}

body {
  font-size: var(--fs-base);
  line-height: var(--lh-normal);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", sans-serif;
  color: var(--rp-text);
  background: var(--rp-background);

  /* Prevent font boosting */
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  text-rendering: optimizeLegibility;
}

h1, h2, h3, h4, h5, h6 {
  font-family: Georgia, "Times New Roman", serif;
  font-weight: 700;
  line-height: var(--lh-tight);
  margin-top: 0;
  margin-bottom: var(--sp-md);
  color: var(--rp-secondary);
  word-break: break-word;
}

h1 { font-size: var(--fs-3xl); }
h2 { font-size: var(--fs-2xl); }
h3 { font-size: var(--fs-xl); }
h4 { font-size: var(--fs-lg); }
h5 { font-size: var(--fs-base); }
h6 { font-size: var(--fs-sm); }

p {
  margin-top: 0;
  margin-bottom: var(--sp-lg);
  line-height: var(--lh-relaxed);
}

/* Links */
a {
  color: var(--rp-primary);
  text-decoration: none;
  transition: color 200ms;
}

a:hover {
  color: #145A3D;
  text-decoration: underline;
}

a:focus {
  outline: 2px solid var(--rp-primary);
  outline-offset: 2px;
  border-radius: 2px;
}

/* ===================================================================
   TOUCH TARGET SIZING
   =================================================================== */

/* All interactive elements must be at least 44x44px */
button,
a[role="button"],
input[type="button"],
input[type="submit"],
input[type="reset"],
input[type="checkbox"],
input[type="radio"],
select {
  min-height: var(--touch-target);
  min-width: var(--touch-target);
  padding: var(--touch-padding);
  border-radius: 8px;
  transition: background-color 150ms, box-shadow 150ms, transform 150ms;
  cursor: pointer;
  font-size: var(--fs-base);

  /* Prevent zoom on input focus (iOS Safari) */
  font-size: 16px !important;
}

button:focus,
a[role="button"]:focus,
input:focus,
select:focus,
textarea:focus {
  outline: 2px solid var(--rp-primary);
  outline-offset: 2px;
}

/* Remove default iOS button styling */
button {
  border: none;
  background: transparent;
  -webkit-appearance: none;
  appearance: none;
}

/* Input fields */
input[type="text"],
input[type="tel"],
input[type="email"],
input[type="password"],
input[type="number"],
input[type="date"],
input[type="time"],
textarea,
select {
  width: 100%;
  padding: var(--touch-padding);
  border: 1px solid var(--rp-border);
  border-radius: 8px;
  font-family: inherit;
  font-size: 16px; /* Prevent zoom */
  background: white;
  color: var(--rp-text);
  -webkit-appearance: none;
  appearance: none;
  box-sizing: border-box;
}

input:focus,
textarea:focus,
select:focus {
  border-color: var(--rp-primary);
  box-shadow: 0 0 0 3px rgba(23, 110, 72, 0.1);
  outline: none;
}

/* Remove default select arrow in Firefox */
select {
  background-image: url("data:image/svg+xml;charset=UTF-8,%3csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='none' stroke='%23333' stroke-width='2'%3e%3cpolyline points='6 9 12 15 18 9'%3e%3c/polyline%3e%3c/svg%3e");
  background-repeat: no-repeat;
  background-position: right 12px center;
  background-size: 16px;
  padding-right: 40px;
}

/* Checkbox & Radio */
input[type="checkbox"],
input[type="radio"] {
  width: 20px;
  height: 20px;
  margin: 2px;
  cursor: pointer;
  accent-color: var(--rp-primary);
}

input[type="checkbox"]:focus,
input[type="radio"]:focus {
  outline: 2px solid var(--rp-primary);
  outline-offset: 2px;
}

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

img {
  max-width: 100%;
  height: auto;
  display: block;

  /* Improve rendering */
  image-rendering: -webkit-optimize-contrast;
  image-rendering: crisp-edges;
}

/* Lazy loading */
img[loading="lazy"] {
  background: linear-gradient(90deg, var(--rp-border) 25%, transparent 25%, transparent 50%, var(--rp-border) 50%, var(--rp-border) 75%, transparent 75%, transparent);
  background-size: 20px 20px;
  background-color: var(--rp-background);
}

/* Picture elements */
picture {
  display: block;
}

/* Prevent image distortion on high-res displays */
@media (-webkit-min-device-pixel-ratio: 2), (min-resolution: 192dpi) {
  img {
    image-rendering: -webkit-optimize-contrast;
  }
}

/* ===================================================================
   NAVIGATION & MENUS
   =================================================================== */

nav {
  /* Safe area support for notched devices */
  padding-top: var(--safe-inset-top);
  padding-left: var(--safe-inset-left);
  padding-right: var(--safe-inset-right);
}

/* Bottom navigation (common on mobile) */
.nav-bottom,
[role="tablist"] {
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  padding-bottom: var(--safe-inset-bottom);
  background: white;
  border-top: 1px solid var(--rp-border);
  z-index: var(--z-sticky);
  display: flex;
  justify-content: space-around;
  gap: 0;
}

.nav-bottom a,
[role="tab"] {
  flex: 1;
  padding: var(--sp-lg) var(--sp-md);
  text-align: center;
  color: var(--rp-text-light);
  font-size: var(--fs-xs);
  border-radius: 0;
  transition: color 150ms, background 150ms;
}

.nav-bottom a:hover,
.nav-bottom a:focus,
[role="tab"]:hover,
[role="tab"]:focus {
  color: var(--rp-primary);
  background: rgba(23, 110, 72, 0.05);
}

.nav-bottom a.active,
[role="tab"][aria-selected="true"] {
  color: var(--rp-primary);
  border-top: 3px solid var(--rp-primary);
  border-bottom: -3px transparent;
}

/* ===================================================================
   CARDS & CONTAINERS
   =================================================================== */

.card,
[role="article"],
.panel {
  padding: var(--sp-lg);
  margin-bottom: var(--sp-lg);
  background: white;
  border-radius: 12px;
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.1);
  transition: box-shadow 200ms, transform 150ms;
}

.card:hover {
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
  transform: translateY(-2px);
}

.card:active {
  transform: translateY(0);
}

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

.btn,
button,
input[type="button"],
input[type="submit"],
.button {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: var(--sp-md);
  min-height: var(--touch-target);
  padding: var(--sp-md) var(--sp-lg);
  border: none;
  border-radius: 8px;
  font-size: var(--fs-base);
  font-weight: 600;
  cursor: pointer;
  transition: all 200ms;
  text-transform: uppercase;
  letter-spacing: 0.5px;
  -webkit-appearance: none;
  appearance: none;
}

.btn-primary,
button.primary,
input[type="submit"] {
  background: var(--rp-primary);
  color: white;
}

.btn-primary:hover,
button.primary:hover {
  background: #145A3D;
  transform: translateY(-2px);
  box-shadow: 0 4px 12px rgba(23, 110, 72, 0.3);
}

.btn-primary:active,
button.primary:active {
  transform: translateY(0);
}

.btn-secondary,
button.secondary {
  background: transparent;
  color: var(--rp-secondary);
  border: 2px solid var(--rp-border);
}

.btn-secondary:hover,
button.secondary:hover {
  background: var(--rp-background);
  border-color: var(--rp-primary);
  color: var(--rp-primary);
}

.btn:disabled,
button:disabled {
  opacity: 0.6;
  cursor: not-allowed;
  transform: none !important;
}

/* Full-width buttons on mobile */
@media (max-width: 640px) {
  .btn-block,
  button.block {
    width: 100%;
  }
}

/* ===================================================================
   FORMS
   =================================================================== */

form {
  display: flex;
  flex-direction: column;
  gap: var(--sp-lg);
}

.form-group {
  display: flex;
  flex-direction: column;
  gap: var(--sp-sm);
}

label {
  font-size: var(--fs-base);
  font-weight: 600;
  color: var(--rp-secondary);
}

.form-hint,
.field-help {
  font-size: var(--fs-xs);
  color: var(--rp-text-light);
  margin-top: var(--sp-sm);
}

.form-error {
  color: var(--rp-error);
  font-size: var(--fs-xs);
  margin-top: var(--sp-sm);
}

input.error,
textarea.error,
select.error {
  border-color: var(--rp-error);
  box-shadow: 0 0 0 3px rgba(195, 47, 39, 0.1);
}

/* Horizontal form on tablet+ */
@media (min-width: 768px) {
  .form-inline .form-group {
    flex-direction: row;
    align-items: center;
  }

  .form-inline label {
    min-width: 120px;
  }
}

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

.grid {
  display: grid;
  gap: var(--sp-lg);
  grid-template-columns: 1fr;
}

@media (min-width: 480px) {
  .grid-2 { grid-template-columns: repeat(2, 1fr); }
}

@media (min-width: 768px) {
  .grid-3 { grid-template-columns: repeat(3, 1fr); }
  .grid-4 { grid-template-columns: repeat(4, 1fr); }
}

@media (min-width: 1024px) {
  .grid-4 { grid-template-columns: repeat(4, 1fr); }
  .grid-5 { grid-template-columns: repeat(5, 1fr); }
}

/* ===================================================================
   SAFE AREAS & NOTCHES
   =================================================================== */

/* Padding for notched devices */
.safe-area {
  padding-top: var(--safe-inset-top);
  padding-right: var(--safe-inset-right);
  padding-bottom: var(--safe-inset-bottom);
  padding-left: var(--safe-inset-left);
}

.safe-padding-top { padding-top: calc(var(--sp-lg) + var(--safe-inset-top)); }
.safe-padding-bottom { padding-bottom: calc(var(--sp-lg) + var(--safe-inset-bottom)); }

/* Sidebar safe area (iPad landscape) */
@supports (padding: max(0px)) {
  .with-safe-area {
    padding-left: max(var(--sp-lg), var(--safe-inset-left));
    padding-right: max(var(--sp-lg), var(--safe-inset-right));
    padding-bottom: max(var(--sp-lg), var(--safe-inset-bottom));
  }
}

/* ===================================================================
   PERFORMANCE OPTIMIZATIONS
   =================================================================== */

/* Use transform + opacity for animations (GPU accelerated) */
.fade-in {
  animation: fadeIn 300ms ease-in forwards;
}

@keyframes fadeIn {
  from { opacity: 0; }
  to { opacity: 1; }
}

.slide-up {
  animation: slideUp 300ms ease-out forwards;
}

@keyframes slideUp {
  from {
    opacity: 0;
    transform: translateY(20px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

/* Reduce motion for accessibility */
@media (prefers-reduced-motion: reduce) {
  *,
  *::before,
  *::after {
    animation-duration: 0.01ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: 0.01ms !important;
  }
}

/* ===================================================================
   UTILITY CLASSES
   =================================================================== */

.text-center { text-align: center; }
.text-right { text-align: right; }
.text-left { text-align: left; }

.m-0 { margin: 0; }
.mt-lg { margin-top: var(--sp-lg); }
.mb-lg { margin-bottom: var(--sp-lg); }
.mx-auto { margin-left: auto; margin-right: auto; }

.p-lg { padding: var(--sp-lg); }
.px-lg { padding-left: var(--sp-lg); padding-right: var(--sp-lg); }
.py-lg { padding-top: var(--sp-lg); padding-bottom: var(--sp-lg); }

.opacity-50 { opacity: 0.5; }
.opacity-75 { opacity: 0.75; }

.hidden { display: none !important; }
.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;
}

/* ===================================================================
   MEDIA QUERIES
   =================================================================== */

/* Small phones (320px - 480px) */
@media (max-width: 480px) {
  :root {
    --sp-lg: 12px;
    --fs-base: 15px;
    --fs-lg: 17px;
    --fs-2xl: 22px;
  }

  body {
    font-size: var(--fs-base);
  }

  /* Stack forms vertically */
  .form-inline .form-group {
    flex-direction: column !important;
  }

  .form-inline label {
    min-width: auto !important;
  }

  /* Larger touch targets */
  button,
  input[type="button"],
  input[type="submit"],
  a[role="button"] {
    min-height: 48px;
    padding: var(--sp-lg);
  }

  /* Reduce padding on containers */
  .card,
  .panel {
    padding: var(--sp-lg);
    margin-bottom: var(--sp-lg);
    border-radius: 8px;
  }

  /* Full-width modals on small screens */
  .modal {
    width: 100% !important;
    max-width: 100% !important;
    height: 100% !important;
    max-height: 100% !important;
    border-radius: 0 !important;
    padding-top: var(--safe-inset-top) !important;
    padding-bottom: var(--safe-inset-bottom) !important;
  }

  h1 { font-size: var(--fs-2xl); }
  h2 { font-size: var(--fs-xl); }
  h3 { font-size: var(--fs-lg); }

  /* Stack grid columns */
  .grid-2,
  .grid-3,
  .grid-4 {
    grid-template-columns: 1fr !important;
  }
}

/* Large phones & small tablets (481px - 768px) */
@media (min-width: 481px) and (max-width: 768px) {
  :root {
    --sp-lg: 14px;
    --fs-base: 15px;
  }

  .grid-2 { grid-template-columns: repeat(2, 1fr); }
  .grid-3 { grid-template-columns: repeat(2, 1fr); }
}

/* Tablets & up (769px+) */
@media (min-width: 769px) {
  :root {
    --sp-lg: 16px;
    --fs-base: 16px;
  }

  .grid-2 { grid-template-columns: repeat(2, 1fr); }
  .grid-3 { grid-template-columns: repeat(3, 1fr); }
  .grid-4 { grid-template-columns: repeat(4, 1fr); }
}

/* ===================================================================
   DARK MODE (optional)
   =================================================================== */

@media (prefers-color-scheme: dark) {
  :root {
    --rp-background: #1A1A1A;
    --rp-text: #E0E0E0;
    --rp-text-light: #999;
    --rp-border: #333;
  }

  body {
    background: var(--rp-background);
    color: var(--rp-text);
  }

  .card,
  .panel {
    background: #2A2A2A;
  }

  input,
  textarea,
  select {
    background: #333;
    color: var(--rp-text);
    border-color: var(--rp-border);
  }
}

/* ===================================================================
   PRINT STYLES
   =================================================================== */

@media print {
  body {
    background: white;
    color: black;
  }

  a {
    text-decoration: underline;
    color: black;
  }

  button,
  .no-print {
    display: none;
  }
}
