/**
 * Pace Planner - Responsive Layout & Media Queries
 * Fluid typography, touch ergonomics, off-canvas navigation drawer,
 * modern bottom navigation bar, safe-area adaptation, and mobile alignment stability.
 */

/* ==========================================================================
   Base Mobile Elements (Hidden on Desktop)
   ========================================================================== */
.mobile-bottom-bar {
  display: none;
}

.mobile-drawer-toggle {
  display: none;
}

.mobile-header-brand {
  display: none;
}

.sidebar-close-btn {
  display: none;
}

.sidebar-backdrop {
  display: none;
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background-color: rgba(0, 0, 0, 0.65);
  backdrop-filter: blur(3px);
  -webkit-backdrop-filter: blur(3px);
  z-index: 999;
  opacity: 0;
  transition: opacity var(--transition-normal);
}

.sidebar-backdrop.is-open {
  display: block;
  opacity: 1;
}

/* ==========================================================================
   Large Desktop Screens (>= 1280px)
   ========================================================================== */
@media (min-width: 1280px) {
  .view-viewport {
    padding: 2.5rem 3rem;
  }
}

/* ==========================================================================
   Tablets & Smaller Laptops (<= 1024px)
   ========================================================================== */
@media (max-width: 1024px) {
  .dash-columns-grid {
    grid-template-columns: 1fr;
    gap: 1.25rem;
  }

  .calendar-layout {
    grid-template-columns: 1fr;
    gap: 1.25rem;
  }

  .cal-agenda-card {
    max-height: 420px;
  }

  .pomo-wrapper {
    grid-template-columns: 1fr;
    gap: 1.25rem;
  }

  .stats-charts-row {
    grid-template-columns: 1fr;
    gap: 1.25rem;
  }
}

/* ==========================================================================
   Mobile Devices & Tablets (<= 768px)
   ========================================================================== */
@media (max-width: 768px) {
  /* Prevent horizontal layout shifts and scrolling */
  html, body {
    width: 100%;
    max-width: 100vw;
    overflow-x: hidden;
  }

  .app-container {
    width: 100%;
    max-width: 100vw;
    overflow-x: hidden;
    position: relative;
  }

  /* Off-canvas Navigation Drawer */
  .app-sidebar {
    position: fixed;
    top: 0;
    bottom: 0;
    left: 0;
    width: 290px;
    max-width: 82vw;
    transform: translateX(-100%);
    box-shadow: var(--shadow-lg);
    z-index: 1000;
    transition: transform var(--transition-normal);
    background-color: var(--bg-sidebar);
  }

  .app-sidebar.is-open {
    transform: translateX(0);
  }

  .sidebar-header {
    padding: 0 1rem;
    justify-content: flex-start;
    gap: 0.75rem;
  }

  .sidebar-close-btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    margin-left: auto;
    color: var(--text-muted);
    padding: 0.5rem;
    border-radius: var(--radius-md);
  }

  .sidebar-close-btn:hover {
    color: var(--text-main);
    background-color: var(--bg-surface-elevated);
  }

  /* Main Application Container */
  .app-main {
    margin-left: 0;
    width: 100%;
    max-width: 100%;
    min-width: 0;
    padding-bottom: calc(72px + env(safe-area-inset-bottom, 0px));
  }

  /* Top App Bar */
  .app-header {
    height: calc(60px + env(safe-area-inset-top, 0px));
    padding: env(safe-area-inset-top, 0px) 1rem 0;
    justify-content: space-between;
  }

  .header-left {
    display: flex;
    align-items: center;
    gap: 0.75rem;
    min-width: 0;
  }

  .mobile-drawer-toggle {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 38px;
    height: 38px;
    padding: 0;
    color: var(--text-main);
    border-radius: var(--radius-md);
    background-color: var(--bg-surface);
    border: 1px solid var(--border-color);
    flex-shrink: 0;
  }

  .mobile-header-brand {
    display: flex;
    align-items: center;
    min-width: 0;
  }

  .mobile-header-brand .brand-name {
    font-size: 1.15rem;
    font-weight: 800;
    font-family: var(--font-heading);
    letter-spacing: -0.02em;
    background: linear-gradient(135deg, var(--text-main) 30%, var(--primary) 100%);
    -webkit-background-clip: text;
    -webkit-text-fill-color: transparent;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
  }

  .header-time-pill {
    display: none;
  }

  .header-right {
    gap: 0.5rem;
    flex-shrink: 0;
  }

  /* Viewport spacing */
  .view-viewport {
    padding: 1.25rem 1rem;
    width: 100%;
    max-width: 100%;
    box-sizing: border-box;
    min-width: 0;
  }

  /* Universal Section Headers */
  .section-header {
    margin-bottom: 1.25rem;
    flex-direction: column;
    align-items: stretch;
    gap: 0.85rem;
  }

  .section-title-wrap h1 {
    font-size: 1.45rem;
    line-height: 1.2;
  }

  .section-title-wrap p {
    font-size: 0.85rem;
    line-height: 1.4;
  }

  .section-header .btn {
    width: 100%;
    justify-content: center;
    padding: 0.75rem 1.25rem;
  }

  /* Cards */
  .card {
    padding: 1.15rem 1rem;
    border-radius: var(--radius-lg);
  }

  /* ========================================================================
     Dashboard Mobile Optimizations
     ======================================================================== */
  .dash-hero-banner {
    padding: 1.25rem 1rem;
    flex-direction: column;
    align-items: stretch;
    gap: 1rem;
    border-radius: var(--radius-lg);
    margin-bottom: 1.25rem;
  }

  .dash-hero-banner::before {
    display: none;
  }

  .dash-greeting-top {
    align-items: baseline;
    gap: 0.5rem;
  }

  .dash-greeting h2 {
    font-size: 1.35rem;
    line-height: 1.25;
  }

  .dash-greeting p {
    font-size: 0.85rem;
    line-height: 1.4;
  }

  .dash-hero-right {
    text-align: left;
    display: flex;
    align-items: baseline;
    justify-content: space-between;
    padding-top: 0.85rem;
    border-top: 1px solid var(--border-subtle);
    width: 100%;
  }

  #dash-live-time {
    font-size: 1.45rem;
    line-height: 1;
  }

  #dash-live-date {
    font-size: 0.8rem;
    margin-top: 0;
  }

  /* 2x2 Stats Cards Grid on Mobile */
  .dash-stats-grid {
    grid-template-columns: repeat(2, 1fr);
    gap: 0.65rem;
    margin-bottom: 1.25rem;
  }

  .stat-card {
    padding: 0.85rem 0.75rem;
    gap: 0.65rem;
    min-width: 0;
    border-radius: var(--radius-md);
  }

  .stat-icon-wrap {
    width: 36px;
    height: 36px;
    font-size: 1.05rem;
    border-radius: var(--radius-sm);
    flex-shrink: 0;
  }

  .stat-content {
    min-width: 0;
    flex: 1;
  }

  .stat-label {
    font-size: 0.68rem;
    letter-spacing: 0.02em;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    display: block;
    line-height: 1.2;
  }

  .stat-value {
    font-size: 1.35rem;
    line-height: 1.1;
  }

  /* Schedule and Upcoming Items */
  .dash-schedule-item {
    padding: 0.75rem 0.85rem;
    gap: 0.65rem;
  }

  .schedule-time-badge {
    font-size: 0.72rem;
    padding: 0.2rem 0.45rem;
  }

  .schedule-content {
    min-width: 0;
  }

  .schedule-title {
    font-size: 0.88rem;
  }

  .schedule-meta {
    flex-wrap: wrap;
    gap: 0.3rem;
  }

  .dash-upcoming-item {
    padding: 0.7rem 0.85rem;
    gap: 0.5rem;
  }

  .dash-upcoming-info {
    min-width: 0;
    flex: 1;
  }

  .dash-upcoming-title {
    font-size: 0.88rem;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
  }

  .dash-pomo-quick {
    padding: 1.25rem 1rem;
    border-radius: var(--radius-lg);
  }

  .dash-pomo-time {
    font-size: 2.25rem;
  }

  /* ========================================================================
     Tasks Management Mobile Optimizations
     ======================================================================== */
  .tasks-toolbar {
    padding: 0.85rem;
    gap: 0.75rem;
    border-radius: var(--radius-lg);
    margin-bottom: 1.25rem;
  }

  /* Horizontally scrollable status tab pills */
  .tabs-nav {
    gap: 0.4rem;
    padding-bottom: 0.35rem;
    -webkit-overflow-scrolling: touch;
    scrollbar-width: none;
  }

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

  .tab-btn {
    flex-shrink: 0;
    padding: 0.45rem 0.75rem;
    font-size: 0.82rem;
  }

  /* Search & Filter Controls */
  .tasks-controls-row {
    flex-direction: column;
    align-items: stretch;
    gap: 0.65rem;
  }

  .search-box-wrap {
    width: 100%;
    min-width: 0;
  }

  .search-box-wrap input {
    font-size: 0.88rem;
    padding-left: 2.2rem;
  }

  /* 2x2 Symmetrical Grid for Mobile Task Filters */
  .filter-group {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 0.5rem;
    width: 100%;
  }

  .filter-group select {
    width: 100%;
    min-width: 0;
    font-size: 0.82rem;
    padding: 0.55rem 0.65rem;
  }

  .sort-select-wrapper {
    display: flex;
    align-items: center;
    gap: 0.35rem;
    width: 100%;
    min-width: 0;
  }

  .sort-select-wrapper select {
    flex: 1;
    min-width: 0;
  }

  .sort-select-wrapper button {
    flex-shrink: 0;
    min-width: 38px;
    height: 38px;
    padding: 0;
  }

  /* Task Card Responsive Layout */
  .task-card {
    padding: 0.95rem;
    border-radius: var(--radius-md);
  }

  .task-card-header {
    gap: 0.65rem;
    margin-bottom: 0.65rem;
  }

  .task-title {
    font-size: 0.96rem;
  }

  .task-desc-preview {
    font-size: 0.82rem;
  }

  .task-actions {
    flex-shrink: 0;
  }

  .task-actions .btn-icon {
    padding: 0.35rem;
  }

  .task-card-meta {
    flex-direction: column;
    align-items: stretch;
    gap: 0.5rem;
    padding-top: 0.5rem;
  }

  .task-meta-left {
    flex-wrap: wrap;
    gap: 0.35rem;
  }

  .task-meta-right {
    width: 100%;
    display: flex;
    align-items: center;
    justify-content: space-between;
    font-size: 0.78rem;
  }

  /* ========================================================================
     Calendar Mobile Optimizations
     ======================================================================== */
  .cal-card {
    padding: 1rem;
    border-radius: var(--radius-lg);
  }

  .cal-header-bar {
    display: flex;
    align-items: center;
    justify-content: space-between;
    flex-wrap: wrap;
    gap: 0.5rem;
    margin-bottom: 0.85rem;
  }

  #cal-month-year {
    font-size: 1.25rem !important;
  }

  .cal-nav-group {
    gap: 0.35rem;
  }

  .cal-grid-header {
    font-size: 0.7rem;
    margin-bottom: 0.4rem;
  }

  .cal-days-grid {
    gap: 3px;
  }

  .cal-day-cell {
    aspect-ratio: 1;
    min-height: 40px;
    padding: 0.2rem;
    border-radius: var(--radius-sm);
  }

  .cal-day-num {
    font-size: 0.72rem;
  }

  .cal-day-badge {
    display: none; /* Hide fractional text to keep mobile calendar legible */
  }

  .cal-day-dot {
    width: 5px;
    height: 5px;
  }

  .cal-agenda-card {
    padding: 1rem;
    border-radius: var(--radius-lg);
  }

  /* ========================================================================
     Pomodoro Mobile Optimizations
     ======================================================================== */
  .pomo-main-card {
    padding: 1.5rem 1rem;
    border-radius: var(--radius-lg);
  }

  .pomo-mode-tabs {
    width: 100%;
    max-width: 360px;
    display: flex;
    padding: 0.25rem;
    gap: 0.25rem;
    margin-bottom: 1.25rem;
  }

  .pomo-mode-btn {
    flex: 1;
    padding: 0.5rem 0.25rem;
    font-size: 0.76rem;
    text-align: center;
    white-space: nowrap;
  }

  .pomo-timer-display {
    width: 190px;
    height: 190px;
    margin: 0.75rem 0 1.25rem;
  }

  .pomo-timer-text {
    font-size: 2.5rem;
  }

  .pomo-controls-row {
    width: 100%;
    max-width: 320px;
    gap: 0.5rem;
    justify-content: center;
  }

  #pomo-start-btn,
  #pomo-pause-btn {
    flex: 1;
    padding: 0.75rem 1rem;
    font-size: 0.95rem;
  }

  #pomo-reset-btn,
  #pomo-skip-btn {
    width: 44px;
    height: 44px;
    flex-shrink: 0;
    padding: 0;
  }

  .pomo-side-card {
    padding: 1.25rem 1rem;
    border-radius: var(--radius-lg);
  }

  /* ========================================================================
     Public Holidays Mobile Optimizations
     ======================================================================== */
  .holidays-toolbar {
    padding: 0.85rem;
    flex-direction: column;
    align-items: stretch;
    gap: 0.75rem;
    border-radius: var(--radius-lg);
  }

  .holidays-selectors {
    display: grid;
    grid-template-columns: 1fr 100px;
    gap: 0.65rem;
    width: 100%;
  }

  #holiday-retry-btn {
    width: 100%;
    justify-content: center;
  }

  .holidays-list-container {
    grid-template-columns: 1fr;
    gap: 0.85rem;
  }

  .holiday-card {
    padding: 1rem;
    gap: 0.75rem;
    border-radius: var(--radius-md);
  }

  .holiday-card-main {
    gap: 0.75rem;
  }

  .holiday-date-badge {
    width: 44px;
    height: 44px;
  }

  .holiday-date-day {
    font-size: 1.1rem;
  }

  .holiday-name {
    font-size: 0.95rem;
  }

  .holiday-actions .btn {
    width: 100%;
    justify-content: center;
  }

  /* ========================================================================
     Productivity Statistics Mobile Optimizations
     ======================================================================== */
  .stats-grid-cards {
    grid-template-columns: repeat(2, 1fr);
    gap: 0.65rem;
    margin-bottom: 1.25rem;
  }

  .chart-card {
    padding: 1.25rem 1rem;
    border-radius: var(--radius-lg);
  }

  .trend-chart-bars {
    height: 160px;
    padding-top: 1.25rem;
  }

  .trend-bar-group {
    width: 13%;
  }

  .trend-bar-label,
  .trend-bar-count {
    font-size: 0.7rem;
  }

  .cat-stat-header {
    flex-wrap: wrap;
    gap: 0.25rem;
  }

  .priority-legend {
    justify-content: flex-start;
    gap: 0.65rem 1rem;
  }

  /* ========================================================================
     Settings Mobile Optimizations
     ======================================================================== */
  .form-row {
    grid-template-columns: 1fr;
    gap: 0.85rem;
  }

  .settings-backup-actions {
    display: flex;
    flex-direction: column;
    gap: 0.75rem;
    width: 100%;
  }

  .settings-backup-actions .btn {
    width: 100%;
    justify-content: center;
    margin-left: 0 !important;
  }

  /* ========================================================================
     Modals Mobile Optimizations
     ======================================================================== */
  .modal-backdrop {
    padding: 0.75rem 0.5rem;
    align-items: flex-end;
  }

  @media (min-width: 480px) {
    .modal-backdrop {
      align-items: center;
    }
  }

  .modal-dialog {
    max-width: 100%;
    width: 100%;
    max-height: 85vh;
    border-radius: var(--radius-lg);
    margin: 0 auto;
  }

  .modal-header {
    padding: 1rem 1.15rem;
  }

  .modal-title {
    font-size: 1.15rem;
  }

  .modal-body {
    padding: 1rem 1.15rem;
  }

  .modal-footer {
    padding: 0.85rem 1.15rem;
    display: flex;
    gap: 0.5rem;
  }

  .modal-footer .btn {
    flex: 1;
    justify-content: center;
  }

  .task-detail-grid {
    grid-template-columns: 1fr;
    gap: 0.75rem;
  }

  /* ========================================================================
     Mobile Bottom Navigation Bar
     ======================================================================== */
  .mobile-bottom-bar {
    display: flex;
    position: fixed;
    bottom: 0;
    left: 0;
    right: 0;
    height: calc(58px + env(safe-area-inset-bottom, 0px));
    padding-bottom: env(safe-area-inset-bottom, 0px);
    background-color: var(--glass-bg);
    backdrop-filter: blur(16px);
    -webkit-backdrop-filter: blur(16px);
    border-top: 1px solid var(--border-color);
    z-index: 90;
    align-items: center;
    justify-content: space-around;
    padding-left: 0.25rem;
    padding-right: 0.25rem;
  }

  .mobile-nav-item {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 0.2rem;
    color: var(--text-muted);
    font-size: 0.7rem;
    font-weight: 600;
    flex: 1;
    height: 100%;
    min-height: 48px;
    padding: 0.25rem 0;
    text-decoration: none;
    transition: color var(--transition-fast), transform var(--transition-fast);
    -webkit-tap-highlight-color: transparent;
  }

  .mobile-nav-item:active {
    transform: scale(0.95);
  }

  .mobile-nav-item.active {
    color: var(--primary);
  }

  .mobile-nav-item .icon {
    width: 20px;
    height: 20px;
  }

  /* Floating Toasts on Mobile */
  .toast-container {
    bottom: calc(68px + env(safe-area-inset-bottom, 0px));
    right: 0.75rem;
    left: 0.75rem;
  }

  .toast {
    min-width: unset;
    max-width: 100%;
  }
}

/* ==========================================================================
   Small Mobile Phones (<= 480px)
   ========================================================================== */
@media (max-width: 480px) {
  /* Prevent iOS input auto-zoom by keeping font size at 16px */
  input[type="text"],
  input[type="search"],
  input[type="date"],
  input[type="time"],
  input[type="number"],
  select,
  textarea {
    font-size: 16px;
  }

  .view-viewport {
    padding: 1rem 0.75rem;
  }

  .dash-hero-banner {
    padding: 1.15rem 0.85rem;
  }

  .dash-stats-grid,
  .stats-grid-cards {
    gap: 0.5rem;
  }

  .stat-card {
    padding: 0.75rem 0.6rem;
  }

  .stat-icon-wrap {
    width: 32px;
    height: 32px;
    font-size: 0.95rem;
  }

  .stat-value {
    font-size: 1.25rem;
  }

  .stat-label {
    font-size: 0.65rem;
  }

  .pomo-mode-btn {
    font-size: 0.72rem;
    padding: 0.45rem 0.15rem;
  }
}

/* ==========================================================================
   Extra Small Mobile Screens (<= 360px)
   ========================================================================== */
@media (max-width: 360px) {
  .header-add-text {
    display: none;
  }

  #header-quick-add-btn {
    padding: 0.45rem;
    border-radius: var(--radius-md);
  }

  .filter-group {
    grid-template-columns: 1fr;
  }

  .dash-stats-grid,
  .stats-grid-cards {
    grid-template-columns: 1fr;
  }
}

/* ==========================================================================
   Accessibility: 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;
  }
}
