/* ==================== ENHANCED MOBILE RESPONSIVENESS ==================== */

/* ==================== PREVENT HORIZONTAL SCROLL ==================== */
/* Global overflow prevention */
html {
    overflow-x: hidden;
    width: 100%;
}

body {
    overflow-x: hidden;
    overflow-y: auto;
    width: 100%;
    max-width: 100vw;
    position: relative;
}

/* Ensure all containers respect viewport width */
* {
    box-sizing: border-box;
}

/* Prevent any element from exceeding viewport width */
.main-content,
.dashboard-page,
.container,
.row,
.stats-grid,
.content-grid {
    max-width: 100%;
    overflow-x: hidden;
}

/* Hide scrollbars on cards but allow content to exist */
.card,
.shadow-box,
.login-box {
    scrollbar-width: none;
    /* Firefox */
    -ms-overflow-style: none;
    /* IE/Edge */
}

.card::-webkit-scrollbar,
.shadow-box::-webkit-scrollbar,
.login-box::-webkit-scrollbar {
    display: none;
}

/* Fix for images and media */
img,
video,
iframe,
svg {
    max-width: 100%;
    height: auto;
}

/* Prevent text overflow */
h1,
h2,
h3,
h4,
h5,
h6,
p,
span,
div {
    word-wrap: break-word;
    overflow-wrap: break-word;
}

/* Fix for tables */
table {
    max-width: 100%;
    table-layout: fixed;
}

/* Tighter table spacing on mobile */
.routine-table th,
.routine-table td {
    padding: 8px 10px !important;
}

/* Ensure cards don't have unwanted scrollbars */
.card,
.category-card,
.stat-card {
    overflow-y: visible !important;
    height: auto !important;
}

/* Ensure grids don't overflow */
.stats-grid,
.content-grid,
.summary-grid {
    width: 100%;
    max-width: 100%;
}

/* Fix for top bar */
.top-bar,
.top-bar-left,
.top-bar-right {
    max-width: 100%;
    overflow: visible;
}

/* ==================== END OVERFLOW PREVENTION ==================== */

/* Small phones (360px and up) */
@media (max-width: 360px) {
    body {
        font-size: 13px;
    }

    .main-content {
        padding: 15px 10px;
    }

    .card {
        padding: 16px 12px;
        border-radius: 20px;
    }

    .page-title h1 {
        font-size: 22px;
    }

    .stat-card {
        padding: 16px;
    }

    .stat-value {
        font-size: 28px;
    }

    .stat-label {
        font-size: 11px;
    }
}

/* Enhanced 480px breakpoint - Large phones */
@media (max-width: 480px) {

    /* Typography */
    body {
        font-size: 14px;
    }

    .page-title h1 {
        font-size: 24px;
    }

    .page-title p {
        font-size: 13px;
    }

    /* Touch targets - minimum 44px */
    .theme-btn,
    .login-btn,
    button {
        min-height: 44px;
        padding: 12px 20px;
        font-size: 15px;
    }

    /* Form inputs */
    .login-form input[type="text"],
    .login-form input[type="password"],
    input[type="text"],
    input[type="email"],
    input[type="password"],
    select,
    textarea {
        min-height: 44px;
        padding: 12px 16px;
        font-size: 16px;
        /* Prevents zoom on iOS */
    }

    /* Navigation items */
    .nav-item {
        min-height: 44px;
        padding: 12px 15px;
        font-size: 16px;
    }

    .nav-item i {
        font-size: 22px;
    }

    /* Cards and spacing */
    .card,
    .stat-card,
    .shadow-box {
        padding: 24px 20px;
        border-radius: 24px !important;
    }

    .shadow-box::after,
    .shadow-box::before {
        border-radius: 24px !important;
    }

    .main-content {
        padding: 20px 16px;
    }

    /* Stats grid and icons */
    .stat-icon {
        width: 50px;
        height: 50px;
        font-size: 24px;
        border-radius: 12px !important;
    }

    .stat-value {
        font-size: 32px;
    }

    /* Month selector */
    .month-selector {
        padding: 10px 16px;
        gap: 12px;
    }

    .month-selector button {
        min-width: 44px;
        min-height: 44px;
        font-size: 20px;
    }

    .month-selector span {
        font-size: 15px;
    }

    /* Routine table */
    .routine-table {
        font-size: 14px;
    }

    .task-checkbox {
        width: 36px;
        height: 36px;
        min-width: 36px;
        min-height: 36px;
    }

    .task-checkbox i {
        font-size: 16px;
    }

    /* Top bar */
    .top-bar {
        flex-direction: column;
        align-items: flex-start;
        gap: 16px;
        padding-bottom: 16px;
    }

    .top-bar-left {
        width: 100%;
        gap: 12px;
        align-items: center;
    }

    .menu-toggle {
        padding: 0;
        margin: 0;
        margin-left: -4px;
        /* Slight adjustment to align icon visually with card content */
    }

    .top-bar-right {
        width: 100%;
        display: flex;
        flex-direction: column;
        gap: 8px;
    }

    #addRoutineBtn {
        width: 100%;
        justify-content: center;
    }

    .info-pill {
        width: auto;
        flex: 0 1 auto;
        font-size: 11px;
        padding: 6px 10px;
        margin-right: 0;
        justify-content: center;
        min-width: 0;
        white-space: nowrap;
        overflow: hidden;
        text-overflow: ellipsis;
    }

    .info-pill i {
        display: none !important;
    }

    /* Modals */
    .modal {
        padding: 20px;
    }

    .modal-content {
        max-width: 100%;
        padding: 24px 20px;
        border-radius: 20px;
    }

    .modal-header h3 {
        font-size: 20px;
    }

    .modal-close {
        width: 36px;
        height: 36px;
        font-size: 20px;
    }

    /* Progress circle */
    .progress-circle-container {
        padding: 24px 0 !important;
    }

    .progress-circle {
        width: 140px !important;
        height: 140px !important;
    }

    .progress-circle .value .percentage {
        font-size: 32px;
    }

    .progress-circle .value .label {
        font-size: 11px;
    }

    /* Grid layout enforcement */
    .content-grid {
        display: block !important;
        width: 100% !important;
        max-width: 100% !important;
        padding: 0 !important;
        margin: 0 !important;
    }

    .main-area,
    .sidebar-area {
        display: block !important;
        width: 100% !important;
        max-width: 100% !important;
        margin-bottom: 20px !important;
    }

    /* Card widths */
    .card,
    .shadow-box {
        width: 100% !important;
        box-sizing: border-box !important;
    }

    /* Streak & Progress Card Centering */
    .streak-card,
    .progress-circle-container {
        display: flex !important;
        flex-direction: column !important;
        align-items: center !important;
        text-align: center !important;
        width: 100% !important;
    }

    .streak-icon,
    .streak-value,
    .streak-label,
    .streak-days {
        width: 100% !important;
        display: flex !important;
        justify-content: center !important;
        align-items: center !important;
        margin-left: 0 !important;
        margin-right: 0 !important;
    }

    /* Period tabs */
    .period-tabs {
        gap: 8px;
    }

    .period-tab {
        min-height: 40px;
        padding: 10px 16px;
        font-size: 14px;
    }

    /* Chart container */
    .chart-container {
        padding: 16px 8px;
        min-height: 250px;
    }

    /* Sidebar user */
    .sidebar-user {
        padding: 16px 12px;
    }

    .logout-btn {
        min-height: 44px;
        font-size: 15px;
    }
}

/* Enhanced 640px breakpoint - Small tablets / landscape phones */
@media (max-width: 640px) {

    /* Better popover positioning */
    .save-discard-popover {
        left: 20px;
        right: 20px;
        width: auto;
        bottom: -200px !important;
        opacity: 0;
        pointer-events: none;
        transform: none !important;
    }

    .popover-content {
        flex-direction: column;
        gap: 16px;
        padding: 18px 20px;
    }

    .popover-actions {
        width: 100%;
        gap: 12px;
    }

    .popover-actions button {
        flex: 1;
        min-height: 48px;
        justify-content: center;
        font-size: 15px;
    }

    .save-discard-popover.show {
        bottom: 20px !important;
        opacity: 1;
        pointer-events: all;
    }

    /* Context menu */
    .context-menu {
        min-width: 220px;
    }

    .context-menu-item {
        min-height: 48px;
        padding: 12px 18px;
        font-size: 15px;
    }

    .context-menu-item i {
        font-size: 20px;
    }

    /* Table improvements */
    .routine-table-wrapper {
        overflow-x: auto;
        -webkit-overflow-scrolling: touch;
    }

    /* Better scrollbars on mobile */
    .routine-table-wrapper::-webkit-scrollbar {
        height: 8px;
    }

    .routine-table-wrapper::-webkit-scrollbar-track {
        background: rgba(255, 255, 255, 0.05);
        border-radius: 4px;
    }

    .routine-table-wrapper::-webkit-scrollbar-thumb {
        background: var(--primary_color);
        border-radius: 4px;
    }
}

/* Enhanced 768px breakpoint - Tablets */
@media (max-width: 768px) {

    /* Sidebar improvements */
    .sidebar {
        width: 280px;
    }

    /* Content grid */
    .content-grid {
        gap: 20px;
    }

    /* Card headers */
    .card-header {
        flex-direction: column;
        align-items: flex-start;
        gap: 16px;
    }

    .card-header h2 {
        font-size: 20px;
    }

    .card-header .actions {
        width: 100%;
    }

    /* Quick stats */
    .quick-stats {
        gap: 16px;
    }

    /* Top routines */
    .top-routine-item {
        padding: 14px 0;
    }

    .routine-info {
        font-size: 14px;
    }

    /* Streak card */
    .streak-card {
        padding: 24px 20px;
    }

    .streak-value {
        font-size: 48px;
    }

    .streak-days {
        gap: 8px;
    }

    .streak-day {
        width: 36px;
        height: 36px;
        font-size: 11px;
    }
}

/* Landscape orientation optimizations */
@media (max-height: 500px) and (orientation: landscape) {
    .sidebar {
        padding: 20px 15px;
    }

    .sidebar-header {
        margin-bottom: 20px;
    }

    .nav-section {
        margin-bottom: 20px;
    }

    .nav-item {
        padding: 10px 12px;
        font-size: 14px;
    }

    .sidebar-user {
        padding: 16px 12px;
    }

    .modal-content {
        max-height: 90vh;
        overflow-y: auto;
    }
}

/* Touch device optimizations */
@media (hover: none) and (pointer: coarse) {

    /* Larger touch targets */
    button,
    a.nav-item,
    .task-checkbox,
    .checkbox {
        -webkit-tap-highlight-color: rgba(210, 20, 90, 0.2);
    }

    /* Remove hover transforms on touch devices to prevent accidental scrolls/jitter */
    .nav-item:hover,
    .card:hover,
    .stat-card:hover,
    .summary-card:hover,
    .category-card:hover,
    .shadow-box:hover,
    .theme-btn:hover,
    .login-btn:hover {
        transform: none !important;
    }

    /* Prevent accidental background changes on touch */
    .nav-item:not(.active):hover {
        background: transparent;
    }

    .nav-item:active {
        background: linear-gradient(120deg, rgba(255, 255, 255, 0.08), rgba(255, 255, 255, 0.02));
    }

    /* Better active states */
    button:active,
    .theme-btn:active {
        transform: scale(0.98);
    }

    /* Smooth scrolling */
    * {
        -webkit-overflow-scrolling: touch;
    }
}

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

/* High contrast mode support */
@media (prefers-contrast: high) {
    .border-color {
        --border-color: rgba(255, 255, 255, 0.3);
    }

    .nav-item {
        border: 1px solid transparent;
    }

    .nav-item.active {
        border-color: var(--primary_color);
    }
}

/* Analytics & Settings Polish */
.analytics-row {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 24px;
}

@media (max-width: 992px) {
    .analytics-row {
        grid-template-columns: 1fr;
    }
}

@media (max-width: 576px) {
    .setting-item {
        flex-direction: column;
        align-items: flex-start !important;
        gap: 12px !important;
    }

    .setting-item .theme-btn,
    .setting-item .form-check-input {
        align-self: flex-start;
    }

    .settings-card {
        padding: 20px !important;
    }

    .settings-header {
        gap: 15px !important;
    }

    .settings-icon {
        width: 45px !important;
        height: 45px !important;
        font-size: 20px !important;
    }
}