/* ========================================
   Subto.One — Mobile-Perfect Experience
   iOS Safari + Android Chrome + PWA
   ======================================== */

/* ========================================
   0. SAFE AREA + VIEWPORT FOUNDATION
   ======================================== */

/* iOS safe area support (notch, home indicator) */
:root {
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  --safe-left: env(safe-area-inset-left, 0px);
  --safe-right: env(safe-area-inset-right, 0px);
}

/* Apply touch-specific scrolling rules only on mobile/coarse-pointer devices.
   Applying these globally can interfere with desktop trackpad scrolling. */
@media (max-width: 768px), (pointer: coarse) {
  html {
    overflow-x: hidden;
    -webkit-text-size-adjust: 100%;
    -ms-text-size-adjust: 100%;
  }

  body {
    overflow-x: hidden;
    /* iOS momentum scrolling */
    -webkit-overflow-scrolling: touch;
    /* Prevent pull-to-refresh in standalone PWA */
    overscroll-behavior-y: contain;
  }

  /* Prevent iOS zoom on input focus — font-size >= 16px */
  input, select, textarea {
    font-size: 16px !important;
  }
}

/* ========================================
   1. GLOBAL TOUCH + INTERACTION
   ======================================== */

/* Remove 300ms tap delay on touch devices only */
@media (max-width: 768px), (pointer: coarse) {
  html {
    touch-action: manipulation;
  }

  /* Disable long-press context menus on interactive elements */
  button, a {
    -webkit-touch-callout: none;
  }

  /* Disable text selection on buttons */
  button {
    -webkit-user-select: none;
    user-select: none;
  }
}

/* Minimum 44px tap targets (WCAG 2.5.5) — mobile only */
@media (max-width: 768px) {
  button, a, .nav-link, .dropdown-item, .btn-primary,
  .btn-secondary, .btn-icon, .issue-tab, .graph-btn,
  .code-tab, .phase, .modal-close, .team-scan-item,
  .pwa-install-btn, .pwa-dismiss-btn, .google-signin-btn {
    min-height: 44px;
    min-width: 44px;
  }
}

/* Active state feedback for touch */
button:active, a:active, .nav-link:active,
.google-signin-btn:active, .btn-primary:active,
.btn-secondary:active, .btn-icon:active {
  opacity: 0.7;
  transition: opacity 50ms ease;
}

/* ========================================
   2. HEADER — MOBILE LAYOUT
   ======================================== */
@media (max-width: 768px) {
  .app-header {
    padding: var(--space-xs) var(--space-md);
    padding-top: calc(var(--space-xs) + var(--safe-top));
    position: sticky;
    top: 0;
    z-index: 100;
  }

  .header-content {
    flex-wrap: wrap;
    padding: 8px 0;
    gap: var(--space-sm);
  }

  .logo {
    gap: 8px;
    font-size: 1rem;
  }

  .logo-favicon {
    width: 28px;
    height: 28px;
  }

  .logo-text {
    font-size: 1rem;
  }

  /* Nav row beneath logo */
  .header-nav {
    order: 3;
    width: 100%;
    justify-content: center;
    gap: 2px;
    margin-left: 0;
    margin-top: 4px;
    padding: 4px 0;
    border-top: 1px solid var(--border-subtle);
  }

  .nav-link {
    padding: 10px 14px;
    font-size: 0.8125rem;
    border-radius: var(--radius);
    flex: 1;
    justify-content: center;
    min-height: 44px;
  }

  .nav-link svg {
    width: 14px;
    height: 14px;
  }

  /* Header actions */
  .header-actions {
    margin-left: auto;
    order: 2;
  }

  .google-signin-btn {
    padding: 10px 16px;
    font-size: 13px;
    border-radius: 10px;
    gap: 8px;
  }

  .google-btn-text {
    font-size: 13px;
  }

  .google-icon-wrapper {
    width: 20px;
    height: 20px;
  }

  .google-icon-wrapper svg {
    width: 16px;
    height: 16px;
  }

  /* User profile compact */
  .user-profile {
    gap: 8px;
  }

  .user-details {
    display: none;
  }

  .user-avatar {
    width: 32px;
    height: 32px;
  }

  /* Dropdown positioning for mobile */
  .dropdown-menu {
    position: fixed;
    top: auto;
    bottom: 0;
    left: 0;
    right: 0;
    border-radius: var(--radius-xl) var(--radius-xl) 0 0;
    max-height: 70vh;
    overflow-y: auto;
    transform: translateY(100%);
    padding-bottom: var(--safe-bottom);
  }

  .dropdown-menu.active {
    transform: translateY(0);
  }

  .dropdown-item {
    padding: 16px 20px;
    font-size: 15px;
    min-height: 48px;
  }
}

/* ========================================
   3. LANDING — MOBILE FIRST
   ======================================== */
@media (max-width: 768px) {
  #app {
    padding: var(--space-md);
    padding-left: calc(var(--space-md) + var(--safe-left));
    padding-right: calc(var(--space-md) + var(--safe-right));
  }

  #landing {
    min-height: calc(100vh - 120px);
    min-height: calc(100dvh - 120px); /* dynamic viewport height for mobile */
    padding: var(--space-lg) var(--space-sm);
  }

  .landing-hero {
    padding: 0 var(--space-xs);
  }

  .landing-title, h1 {
    font-size: 1.5rem;
    line-height: 1.25;
    letter-spacing: -0.02em;
    text-align: center;
  }

  .landing-tagline {
    font-size: 0.875rem;
    line-height: 1.5;
    margin-bottom: var(--space-md);
  }

  .landing-purpose {
    font-size: 0.875rem;
    line-height: 1.5;
    padding: 0 var(--space-xs);
  }

  /* URL input row — stack on very small screens */
  .scan-input-row {
    flex-direction: column;
    gap: var(--space-sm);
    max-width: 100%;
    padding: 0;
  }

  .scan-input-row #url-input {
    width: 100%;
    max-width: 100%;
    height: 52px;
    font-size: 16px; /* prevent iOS zoom */
    padding: 0 16px;
    border-radius: var(--radius-lg);
  }

  .scan-btn {
    width: 100%;
    height: 52px;
    justify-content: center;
    font-size: 1rem;
    border-radius: var(--radius-lg);
  }

  .landing-subtitle {
    font-size: 0.75rem;
    text-align: center;
    margin-top: var(--space-md);
  }
}

@media (max-width: 380px) {
  .landing-title, h1 {
    font-size: 1.25rem;
  }
}

/* ========================================
   4. SCANNER / PROGRESS — MOBILE
   ======================================== */
@media (max-width: 768px) {
  #scanner {
    min-height: calc(100vh - 120px);
    min-height: calc(100dvh - 120px);
    padding: var(--space-md);
  }

  .scan-container {
    max-width: 100%;
    gap: var(--space-lg);
  }

  .scan-ring {
    width: 130px;
    height: 130px;
  }

  .scan-percentage {
    font-size: 24px;
  }

  .scan-phases {
    padding: var(--space-sm);
  }

  .phase {
    font-size: 13px;
    padding: var(--space-xs) var(--space-sm);
    min-height: 36px;
  }

  .scan-footer {
    padding: 0;
  }

  .scan-activity {
    flex-direction: column;
    gap: var(--space-xs);
    font-size: 11px;
  }

  .scan-skip {
    flex-direction: column;
    gap: var(--space-xs);
    text-align: center;
  }
}

/* ========================================
   5. RESULTS — MOBILE LAYOUT
   ======================================== */
@media (max-width: 768px) {
  #results {
    padding-top: var(--space-md);
  }

  .results-section {
    margin-bottom: var(--space-xl);
  }

  /* Overview grid — 2 columns on mobile */
  .overview-grid {
    grid-template-columns: repeat(2, 1fr);
    gap: var(--space-sm);
  }

  .overview-card {
    padding: var(--space-md);
  }

  .card-value {
    font-size: 1.5rem;
  }

  .card-label {
    font-size: 0.75rem;
  }

  /* Vitals grid — 1 column on small screens */
  .vitals-grid {
    grid-template-columns: 1fr;
    gap: var(--space-sm);
  }

  .vital-item {
    padding: var(--space-sm) var(--space-md);
    display: flex;
    justify-content: space-between;
    align-items: center;
  }

  .vital-name {
    margin-bottom: 0;
  }

  /* Graph controls — scrollable */
  .graph-controls {
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
    scrollbar-width: none;
    -ms-overflow-style: none;
    padding-bottom: var(--space-xs);
    gap: var(--space-xs);
    flex-wrap: nowrap;
  }

  .graph-controls::-webkit-scrollbar {
    display: none;
  }

  .graph-btn {
    white-space: nowrap;
    flex-shrink: 0;
    min-height: 44px;
    padding: var(--space-sm) var(--space-md);
  }

  #execution-graph {
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
  }

  /* Issue tabs — scrollable horizontal */
  .issues-tabs {
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
    scrollbar-width: none;
    flex-wrap: nowrap;
    gap: var(--space-xs);
    padding-bottom: var(--space-xs);
  }

  .issues-tabs::-webkit-scrollbar {
    display: none;
  }

  .issue-tab {
    white-space: nowrap;
    flex-shrink: 0;
    min-height: 44px;
    font-size: 0.8125rem;
  }

  /* SEO grid — single column */
  .seo-grid {
    grid-template-columns: 1fr;
  }

  /* Explorer — vertical stack */
  .explorer-container {
    grid-template-columns: 1fr;
    height: auto;
    gap: var(--space-md);
  }

  .file-tree {
    max-height: 180px;
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
  }

  .code-viewer {
    height: 300px;
    overflow: auto;
    -webkit-overflow-scrolling: touch;
  }

  .code-content {
    font-size: 11px;
    line-height: 1.5;
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
  }

  /* AI Chat — mobile sizing */
  .ai-chat-messages {
    height: 200px;
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
  }

  .ai-chat-input {
    font-size: 16px !important; /* prevent iOS zoom */
    min-height: 48px;
  }

  .ai-chat-send {
    min-width: 48px;
    min-height: 48px;
  }

  /* Summary grid */
  .summary-grid {
    grid-template-columns: 1fr;
  }

  /* Comprehensive testing */
  .comprehensive-container,
  .comprehensive-bottom-container {
    grid-template-columns: 1fr;
  }

  /* Diff viewer */
  .diff-container {
    max-height: 280px;
  }

  .diff-content {
    font-size: 11px;
  }

  /* Patch actions */
  .patch-actions {
    flex-direction: column;
    gap: var(--space-sm);
  }

  .patch-actions button {
    width: 100%;
    justify-content: center;
  }
}

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

  .card-value {
    font-size: 1.75rem;
  }
}

/* ========================================
   6. API SECTION — MOBILE
   ======================================== */
@media (max-width: 768px) {
  .api-section {
    padding: var(--space-lg) var(--space-sm);
  }

  .api-container {
    grid-template-columns: 1fr;
  }

  .api-key-card {
    grid-column: span 1;
  }

  .api-key-display {
    flex-direction: column;
    gap: var(--space-sm);
    align-items: stretch;
  }

  .api-key-display code {
    font-size: 12px;
    word-break: break-all;
    text-align: center;
  }

  .api-key-display .btn-icon {
    align-self: center;
  }

  .rate-limits {
    grid-template-columns: 1fr;
    gap: var(--space-sm);
  }

  /* API docs */
  .api-docs {
    padding: 0;
  }

  .api-docs-title {
    font-size: 1.25rem;
  }

  .api-code-block {
    font-size: 11px;
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
  }

  .api-code-block code {
    white-space: pre;
    font-size: 11px;
  }

  .api-doc-cta-layout {
    flex-direction: column;
    gap: var(--space-md);
    text-align: center;
  }

  .flex-1-right {
    text-align: center;
  }

  .endpoint-item {
    flex-direction: column;
    align-items: flex-start;
    gap: var(--space-sm);
  }

  .endpoint-desc {
    display: none;
  }

  .api-account-stats .stats-row {
    flex-direction: column;
    gap: var(--space-md);
  }
}

/* ========================================
   7. TEAMS SECTION — MOBILE
   ======================================== */
@media (max-width: 768px) {
  .teams-section {
    padding: var(--space-lg) var(--space-sm);
  }

  .section-header h2 {
    font-size: 1.25rem;
  }

  .section-description {
    font-size: 0.875rem;
  }

  .teams-benefits {
    flex-direction: column;
    gap: var(--space-sm);
    align-items: flex-start;
    padding: var(--space-md);
  }

  .teams-container {
    grid-template-columns: 1fr;
  }

  .team-action-card,
  .team-dashboard,
  .teams-signin-prompt {
    grid-column: span 1;
  }

  .team-actions {
    flex-direction: column;
    width: 100%;
  }

  .team-actions button {
    width: 100%;
    justify-content: center;
  }

  .team-features {
    grid-template-columns: 1fr;
    gap: var(--space-md);
  }

  .feature-card {
    padding: var(--space-md);
  }

  .teams-signin-prompt {
    padding: var(--space-lg);
  }

  .teams-signin-prompt svg {
    width: 36px;
    height: 36px;
  }

  .team-header {
    flex-wrap: wrap;
    gap: var(--space-sm);
  }

  .team-avatar {
    width: 44px;
    height: 44px;
    font-size: 1.25rem;
  }

  .team-info h3 {
    font-size: 1.1rem;
  }
}

/* ========================================
   8. MODALS — MOBILE (FULL SCREEN)
   ======================================== */
@media (max-width: 768px) {
  .modal-overlay {
    padding: 0;
    align-items: flex-end;
  }

  .modal {
    width: 100%;
    max-width: 100%;
    max-height: 90vh;
    max-height: 90dvh;
    border-radius: var(--radius-xl) var(--radius-xl) 0 0;
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
    margin: 0;
  }

  .modal-content {
    border-radius: var(--radius-xl) var(--radius-xl) 0 0;
    max-height: 90vh;
    max-height: 90dvh;
  }

  .modal-lg {
    max-width: 100%;
  }

  .modal-body {
    padding: var(--space-lg) var(--space-md);
    padding-bottom: calc(var(--space-lg) + var(--safe-bottom));
  }

  .modal-header {
    padding: var(--space-md);
    position: sticky;
    top: 0;
    background: var(--surface);
    z-index: 10;
    border-bottom: 1px solid var(--border-subtle);
  }

  .modal-close {
    min-width: 44px;
    min-height: 44px;
    font-size: 1.5rem;
  }

  /* Form inputs in modals */
  .modal input[type="text"],
  .modal input[type="email"],
  .modal input[type="number"],
  .modal textarea,
  .modal select {
    font-size: 16px !important; /* prevent iOS zoom */
    padding: 14px;
    min-height: 48px;
    border-radius: var(--radius);
  }

  .modal .btn-primary,
  .modal .btn-secondary,
  .modal .btn-danger {
    min-height: 48px;
    font-size: 15px;
  }

  /* Scans list in modal */
  .scan-item {
    flex-direction: column;
    gap: var(--space-sm);
  }

  .scan-scores {
    flex-wrap: wrap;
    gap: var(--space-xs);
  }

  /* Account modal */
  .account-profile {
    flex-direction: column;
    text-align: center;
    gap: var(--space-md);
  }

  .account-stats {
    flex-direction: column;
    gap: var(--space-md);
  }
}

/* ========================================
   9. TOAST — MOBILE
   ======================================== */
@media (max-width: 768px) {
  .toast {
    bottom: calc(16px + var(--safe-bottom));
    left: var(--space-md);
    right: var(--space-md);
    transform: translateY(20px);
    max-width: none;
    width: auto;
  }

  .toast.toast-visible,
  .toast.show {
    transform: translateY(0);
  }
}

/* ========================================
   10. PWA INSTALL BANNER — MOBILE
   ======================================== */
@media (max-width: 768px) {
  .pwa-banner {
    padding: var(--space-sm) var(--space-md);
  }

  .pwa-banner-content {
    flex-wrap: wrap;
    gap: var(--space-sm);
  }

  .pwa-text {
    font-size: 13px;
    flex: 1;
    min-width: 0;
  }

  .pwa-install-btn {
    flex-shrink: 0;
    padding: 10px 16px;
    min-height: 44px;
  }

  .pwa-dismiss-btn {
    min-width: 44px;
    min-height: 44px;
    display: flex;
    align-items: center;
    justify-content: center;
  }
}

/* ========================================
   11. FOOTER — MOBILE
   ======================================== */
@media (max-width: 768px) {
  .footer-layout {
    padding: var(--space-md);
    padding-bottom: calc(var(--space-md) + var(--safe-bottom));
  }

  .footer-inner {
    flex-direction: column;
    text-align: center;
    gap: var(--space-sm);
  }

  .footer-brand {
    justify-content: center;
  }

  .legal-links a {
    margin: 0 8px;
    font-size: 12px;
  }
}

/* ========================================
   12. TEAM CHAT — MOBILE
   ======================================== */
@media (max-width: 768px) {
  #team-chat {
    padding: var(--space-sm);
    border-radius: var(--radius);
  }

  #team-chat .messages {
    max-height: 220px;
  }

  #team-chat .message .bubble {
    max-width: 85%;
    padding: 10px 14px;
    font-size: 14px;
  }

  #team-chat .chat-input {
    gap: var(--space-xs);
  }

  #team-chat input[type="text"] {
    font-size: 16px !important; /* prevent iOS zoom */
    padding: 12px;
    min-height: 48px;
  }

  #team-chat button.send {
    min-width: 48px;
    min-height: 48px;
    padding: 12px;
  }

  /* AI assistant widget */
  #team-ai-widget {
    bottom: calc(16px + var(--safe-bottom));
    right: 16px;
    width: 52px;
    height: 52px;
  }

  .assistant-text-input {
    font-size: 16px !important;
    min-height: 44px;
  }
}

/* ========================================
   13. DROP ZONE — MOBILE
   ======================================== */
@media (max-width: 768px) {
  .drop-zone {
    padding: var(--space-lg) var(--space-md);
  }

  .drop-text {
    font-size: 0.9375rem;
  }

  .drop-subtext {
    font-size: 0.75rem;
  }

  .drop-limits {
    font-size: 0.6875rem;
  }

  .upload-actions {
    flex-direction: column;
  }

  .upload-actions button {
    width: 100%;
    min-height: 48px;
  }
}

/* ========================================
   14. KEYBOARD + IOS QUIRKS
   ======================================== */

/* When iOS keyboard is open, reduce bottom-fixed elements */
@supports (height: 100dvh) {
  /* Use dynamic viewport units where supported */
  #landing {
    min-height: calc(100dvh - 120px);
  }
  #scanner {
    min-height: calc(100dvh - 120px);
  }
}

/* Fix iOS input styling */
input[type="text"],
input[type="email"],
input[type="url"],
input[type="search"],
input[type="number"],
textarea,
select {
  -webkit-appearance: none;
  -moz-appearance: none;
  appearance: none;
  border-radius: var(--radius);
}

/* Fix iOS Safari 100vh bug — use stretch where available */
@supports (-webkit-touch-callout: none) {
  body {
    min-height: -webkit-fill-available;
  }
}

/* ========================================
   15. PERFORMANCE OPTIMIZATIONS
   ======================================== */

/* GPU-accelerated transitions for smoothness */
.modal, .dropdown-menu, .toast {
  will-change: transform, opacity;
}

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

  .ring-pulse, .ring-pulse-2 {
    animation: none !important;
  }

  .scan-dot {
    animation: none !important;
  }
}

/* ========================================
   16. LOADING / SKELETON STATES
   ======================================== */
.mobile-loading {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: var(--space-xl);
  gap: var(--space-md);
}

.mobile-loading-spinner {
  width: 32px;
  height: 32px;
  border: 3px solid var(--border-subtle);
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
}

.mobile-loading-text {
  font-size: 0.875rem;
  color: var(--text-secondary);
}

/* Retry button for failed loads */
.mobile-retry {
  display: inline-flex;
  align-items: center;
  gap: var(--space-sm);
  padding: 12px 24px;
  background: var(--surface);
  border: 1px solid var(--border-subtle);
  border-radius: var(--radius);
  color: var(--text-primary);
  font-size: 14px;
  cursor: pointer;
  min-height: 48px;
}

.mobile-retry:active {
  background: var(--surface-hover);
}

/* Offline indicator */
.offline-banner {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  padding: 8px var(--space-md);
  padding-top: calc(8px + var(--safe-top));
  background: var(--error);
  color: #fff;
  text-align: center;
  font-size: 13px;
  font-weight: 500;
  z-index: 10000;
  transform: translateY(-100%);
  transition: transform 200ms var(--ease-smooth);
}

.offline-banner.visible {
  transform: translateY(0);
}

/* Slow network indicator */
.slow-network-banner {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  padding: 6px var(--space-md);
  padding-top: calc(6px + var(--safe-top));
  background: var(--warning);
  color: #000;
  text-align: center;
  font-size: 12px;
  font-weight: 500;
  z-index: 9999;
  transform: translateY(-100%);
  transition: transform 200ms var(--ease-smooth);
}

.slow-network-banner.visible {
  transform: translateY(0);
}

/* ========================================
   17. EXTRA SMALL SCREENS (<380px)
   ======================================== */
@media (max-width: 380px) {
  :root {
    --space-md: 12px;
    --space-lg: 16px;
    --space-xl: 24px;
  }

  .app-header {
    padding: var(--space-xs) var(--space-sm);
  }

  .nav-link {
    padding: 8px 10px;
    font-size: 0.75rem;
  }

  .google-signin-btn {
    padding: 8px 12px;
    font-size: 12px;
  }

  .scan-ring {
    width: 110px;
    height: 110px;
  }

  .scan-percentage {
    font-size: 20px;
  }

  h2 {
    font-size: 1.125rem;
  }

  h3 {
    font-size: 0.9375rem;
  }
}

/* ========================================
   18. LANDSCAPE MOBILE
   ======================================== */
@media (max-height: 500px) and (orientation: landscape) {
  #landing {
    min-height: auto;
    padding: var(--space-md);
  }

  .landing-hero {
    margin-bottom: var(--space-sm);
  }

  .landing-title {
    font-size: 1.25rem;
  }

  .landing-purpose {
    display: none;
  }

  .scan-ring {
    width: 100px;
    height: 100px;
  }

  #scanner {
    min-height: auto;
  }
}

/* ========================================
   19. PWA STANDALONE MODE
   ======================================== */
@media (display-mode: standalone) {
  .app-header {
    padding-top: calc(var(--space-sm) + var(--safe-top));
  }

  /* Hide install banner in installed PWA */
  .pwa-banner {
    display: none !important;
  }
}

/* ========================================
   20. DARK MODE AMOLED OPTIMIZATION
   ======================================== */
@media (prefers-color-scheme: dark) {
  /* Already dark by default — enhance for AMOLED screens */
  .overview-card, .api-card, .team-card, .feature-card,
  .seo-card, .validation-results, .api-checks-results,
  .no-js-results, .interaction-results, .test-results {
    border-color: rgba(255, 255, 255, 0.05);
  }
}
