/* Responsive polish across Ultrawide, Desktop, Narrow/Split, Tablet, and Mobile */

/* Prevent horizontal page overflow everywhere */
html,
body {
  overflow-x: clip;
}

/* Prevent CSS Grid / Flex children from stretching beyond viewport width */
.panel,
.dashboard-grid > *,
.wellness-grid > *,
.goals-grid > *,
.shop-layout > *,
.finance-grid > *,
.finance-layout > *,
.finance-main,
.finance-side,
.fin-filter-bar {
  min-width: 0;
  max-width: 100%;
}

/* Segmented tabs scroll cleanly instead of overflowing narrow viewports */
.habits-tabs {
  max-width: 100%;
  overflow-x: auto;
  scrollbar-width: none;
  flex-wrap: nowrap;
  -webkit-overflow-scrolling: touch;
}

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

.habits-tabs .view-tab {
  white-space: nowrap;
  flex-shrink: 0;
}

/* Ultrawide (1600px+) */
@media (min-width: 1600px) {
  .view-container {
    max-width: 1520px;
  }

  .books-grid {
    grid-template-columns: repeat(auto-fill, minmax(185px, 1fr));
  }
}

/* Narrow laptop / split-screen (<= 1120px) */
@media (max-width: 1120px) {
  .main-content {
    padding:
      calc(var(--space-lg) + env(safe-area-inset-top))
      calc(var(--space-lg) + env(safe-area-inset-right))
      calc(var(--space-xl) + env(safe-area-inset-bottom))
      var(--space-lg);
  }

  .dashboard-grid {
    grid-template-columns: 1.2fr 1fr;
  }

  .shop-layout {
    grid-template-columns: minmax(0, 1.35fr) 280px;
  }
}

/* Tablet / very narrow desktop (<= 900px) */
@media (max-width: 900px) {
  .metrics-grid {
    grid-template-columns: repeat(2, minmax(0, 1fr));
    gap: var(--space-sm);
  }

  .dashboard-grid,
  .wellness-grid,
  .goals-grid,
  .shop-layout {
    grid-template-columns: 1fr;
  }

  .pomodoro-panel,
  .sin-history {
    position: static;
  }

  .wl-inline-form {
    grid-template-columns: 1fr 1fr;
  }

  .wl-inline-form select,
  .wl-inline-form button[type="submit"] {
    grid-column: 1 / -1;
  }

  .books-sync-bar {
    flex-direction: column;
    align-items: flex-start;
  }
}

/* Mobile bottom-dock layout (<= 680px) */
@media (max-width: 680px) {
  .app-layout,
  .app-layout.is-collapsed {
    --current-sidebar-width: 0px;
  }

  .sidebar {
    position: fixed;
    top: auto;
    left: 0;
    right: 0;
    bottom: 0;
    width: 100% !important;
    height: auto;
    min-height: 0;
    padding: 6px 8px calc(6px + env(safe-area-inset-bottom));
    flex-direction: row;
    align-items: center;
    gap: 4px;
    border-right: 0;
    border-top: 1px solid var(--border-subtle);
    z-index: 40;
  }

  .sidebar-top,
  .user-info,
  .sidebar-toggle {
    display: none !important;
  }

  .sidebar-upper {
    flex: 1;
    min-width: 0;
    display: flex;
    flex-direction: row;
    gap: 0;
  }

  .sidebar-nav {
    display: flex;
    flex-direction: row;
    align-items: center;
    gap: 2px;
    width: 100%;
    overflow-x: auto;
    scrollbar-width: none;
    -webkit-overflow-scrolling: touch;
  }

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

  .sidebar-nav .nav-item {
    flex: 0 0 auto;
    width: auto !important;
    min-width: 40px;
    height: 44px;
    padding: 0 8px;
    justify-content: center;
  }

  .sidebar-nav .nav-label {
    display: none !important;
  }

  .sidebar-footer {
    flex-shrink: 0;
    padding: 0 !important;
    border-top: 0;
    position: static;
    display: flex;
    align-items: center;
  }

  .sidebar-footer .icon-btn {
    position: static !important;
    width: 40px;
    height: 40px;
  }

  .main-content {
    margin-left: 0 !important;
    padding:
      calc(var(--space-md) + env(safe-area-inset-top))
      calc(14px + env(safe-area-inset-right))
      calc(82px + env(safe-area-inset-bottom))
      calc(14px + env(safe-area-inset-left));
  }

  .page-header {
    align-items: flex-start;
    gap: 10px;
    margin-bottom: var(--space-md);
  }

  .page-header h1 {
    font-size: 1.3rem;
  }

  .page-header p {
    font-size: 0.85rem;
  }

  .profile-card {
    padding: 14px;
    gap: 12px;
  }

  .dash-hero-right {
    width: 100%;
    justify-content: space-between;
  }

  .task-main {
    flex-wrap: wrap;
  }

  .task-actions {
    width: 100%;
    justify-content: flex-end;
    padding-top: 4px;
  }

  .modal-row,
  .budgets-form-grid {
    grid-template-columns: 1fr;
  }

  .habits-chart-spacer {
    display: none;
  }

  .matrix-corner,
  .matrix-habit-cell {
    width: 118px;
    padding: 8px;
  }

  .books-grid {
    grid-template-columns: repeat(2, minmax(0, 1fr));
    gap: 10px;
  }

  .book-reader-stage {
    padding: 12px;
    min-height: 320px;
  }

  .settings-modal {
    width: calc(100vw - 16px);
    max-height: 90dvh;
  }

  .focus-widget {
    bottom: calc(68px + env(safe-area-inset-bottom));
    right: 12px;
    left: 12px;
    width: auto;
  }

  .toast-stack,
  .reward-toast {
    bottom: calc(70px + env(safe-area-inset-bottom));
  }
}

/* Very small screens (<= 420px) */
@media (max-width: 420px) {
  .metric-card {
    padding: 12px;
  }

  .dash-fin-strip {
    grid-template-columns: 1fr;
    gap: 6px;
  }

  .settings-theme-grid {
    grid-template-columns: 1fr;
  }

  .notes-toolbar,
  .goals-toolbar,
  .fin-filter-bar {
    gap: 8px;
  }
}

/* Touch / mobile-native polish: prevent iOS input zoom & sticky hover */
@media (hover: none) and (pointer: coarse) {
  input:not([type="checkbox"]):not([type="radio"]):not([type="range"]),
  select,
  textarea {
    font-size: 16px;
  }

  .note-card:hover,
  .book-card:hover,
  .dash-kpi:hover,
  .sin-card.is-ready:hover {
    transform: none;
  }
}
