/* ========== RESPONSIVE & MOBILE OPTIMIZATIONS ========== */

/* File actions bar: always 3 columns, never affected by responsive rules */
.file-preview-card .file-actions-bar {
    display: flex !important;
    align-items: center !important;
    justify-content: center !important;
    gap: 0.5rem !important;
    flex-wrap: nowrap !important;
    padding: 0.4rem !important;
}

.file-preview-card .file-actions-bar .btn-icon {
    flex: 1 !important;
    min-width: 32px !important;
    min-height: 32px !important;
    height: 32px !important;
    padding: 0.3rem !important;
}

/* Prevenir zoom en inputs */
@media (max-width: 768px) {
    input,
    textarea,
    select {
        font-size: 16px !important;
        /* Prevenir zoom automático en iOS */
    }
}

/* Touch-friendly sizes */
@media (hover: none) and (pointer: coarse) {
    /* Aumentar el área táctil de botones */
    button,
    .btn,
    .btn-icon,
    a {
        min-height: 44px;
        min-width: 44px;
        padding: 12px 16px;
    }

    /* Aumentar espacio entre elementos clicables */
    .sidebar-item {
        padding: 12px 16px;
        margin-bottom: 8px;
    }

    /* Inputs más grandes para touch */
    input,
    textarea,
    select {
        padding: 12px;
        min-height: 44px;
    }

    /* Aumentar área de checkboxes */
    input[type="checkbox"],
    input[type="radio"] {
        width: 24px;
        height: 24px;
        cursor: pointer;
    }

    /* Labels más fáciles de tocar */
    label {
        padding: 8px;
        display: block;
    }
}

/* Landscape orientation (tablets y phones landscape) */
@media (max-height: 600px) and (orientation: landscape) {
    .sidebar {
        height: auto;
        overflow-x: auto;
        overflow-y: hidden;
        flex-direction: row;
        padding: 0.5rem;
    }

    .sidebar-content {
        flex-direction: row;
    }

    .main-content {
        margin-left: 0;
        margin-top: 70px;
    }

    .header {
        flex-direction: row;
        gap: 1rem;
    }

    /* Modal en landscape debe ser más compacto */
    .modal-content {
        max-height: 80vh;
    }
}

/* Extra large screens */
@media (min-width: 1920px) {
    .sidebar {
        width: 300px;
    }

    .main-content {
        margin-left: 300px;
    }

    .projects-grid {
        grid-template-columns: repeat(auto-fill, minmax(400px, 1fr)) !important;
    }

    .login-box {
        max-width: 500px;
    }

    /* Ocultar controles de calendario en pantallas grandes */
    .calendar-controls {
        display: none !important;
    }
}

/* Mejorar experiencia en pantallas muy pequeñas */
@media (max-width: 360px) {
    .form-group input,
    .btn,
    input,
    textarea {
        font-size: 16px !important;
    }

    .sidebar-item {
        font-size: 0.8rem;
    }

    .product-tag {
        display: block;
        margin-top: 0.5rem;
    }

    .top-bar {
        margin-top: 0rem;
        padding: 0.5rem;
    }

    /* Esconder elementos menos importantes en pantallas muy pequeñas */
    .sidebar-divider {
        display: none;
    }
}

/* Pantallas medianas y grandes - ocultar controles del calendario */
@media (min-width: 769px) {
    .calendar-controls {
        display: none !important;
    }

    /* Calendario en vertical para todas las pantallas */
    .calendar-timeline {
        display: flex;
        flex-direction: column;
    }

    .calendar-timeline-row {
        display: contents;
    }

    .calendar-timeline-header {
        margin-bottom: 0;
    }

    /* Botones en tabla en fila (horizontal) para pantallas grandes */
    .table-container td:last-child {
        display: flex;
        gap: 0.5rem;
        align-items: center;
        justify-content: flex-start;
    }

    .table-container td:last-child button {
        flex: 0 1 auto;
        padding: 8px 12px;
        font-size: 0.9rem;
    }
}

/* Optimizar tablas para móviles */
@media (max-width: 768px) {
    .table-container {
        overflow-x: visible;
    }

    .table-container table {
        display: block;
        min-width: auto;
    }

    .table-container thead {
        display: none !important;
    }

    .table-container tbody {
        display: block;
    }

    .table-container tr {
        display: block;
        border: 1px solid var(--border-light);
        border-radius: 8px;
        background: #fff;
        padding: 1rem;
        box-shadow: 0 1px 3px rgba(0, 0, 0, 0.05);
        transition: box-shadow 0.2s ease;
        margin-bottom: 0.5rem;
    }

    .table-container tr:hover {
        box-shadow: 0 2px 6px rgba(0, 0, 0, 0.1);
    }

    .table-container td {
        display: flex;
        align-items: center;
        gap: 0.75rem;
        margin-bottom: 0.75rem;
        padding: 0;
        border: none;
    }

    .table-container td:last-child {
        display: flex;
        gap: 0.5rem;
        align-items: center;
        justify-content: stretch;
        margin-bottom: 0;
        margin-top: 0.75rem;
        padding-top: 0.75rem;
        border-top: 1px solid var(--border-light);
        width: 100%;
    }

    .table-container td:before {
        content: attr(data-label) ":";
        display: inline-block;
        font-weight: 600;
        color: var(--primary);
        font-size: 0.7rem;
        text-transform: uppercase;
        letter-spacing: 0.5px;
        white-space: nowrap;
        flex-shrink: 0;
    }

    .table-container td:last-child:before {
        display: none;
    }

    .table-container td:last-child button {
        flex: 1;
        display: flex;
        align-items: center;
        justify-content: center;
        padding: 8px;
    }
}

/* Optimizar grillas para móviles */
@media (max-width: 768px) {
    .projects-grid {
        grid-template-columns: 1fr !important;
        gap: 1rem;
    }

    .form-row {
        flex-direction: column;
    }

    .form-row > * {
        width: 100% !important;
        margin-bottom: 1rem;
    }
}

/* Notificaciones responsive */
@media (max-width: 768px) {
    .toast {
        bottom: 1rem;
        right: 1rem;
        left: 1rem;
        margin: 0 auto;
        max-width: 100%;
        width: auto;
    }
}

/* Modales responsivos */
@media (max-width: 768px) {
    .modal {
        position: fixed;
        top: 0;
        left: 0;
        right: 0;
        bottom: 0;
        display: flex;
        flex-direction: column;
        align-items: stretch;
        justify-content: flex-end;
        z-index: 1000;
        margin: 0;
    }

    .modal-content {
        width: 100%;
        max-width: 100%;
        max-height: 90vh;
        border-radius: var(--radius-md) var(--radius-md) 0 0;
        margin: 0;
        animation: slideUp 0.3s ease-out;
    }

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

/* Dropdown menus responsivos */
@media (max-width: 768px) {
    .dropdown-menu {
        position: fixed;
        top: auto;
        bottom: 0;
        left: 0;
        right: 0;
        width: 100%;
        max-width: 100%;
        border-radius: var(--radius-md) var(--radius-md) 0 0;
    }
}

/* Optimizar scrolling en móviles */
@media (max-width: 768px) {
    .main-content,
    .sidebar,
    .detail-main,
    .comments-feed {
        -webkit-overflow-scrolling: touch;
        /* Smooth scrolling en iOS */
    }

    /* Calendario responsive para móviles */
    .calendar-view-header {
        flex-direction: column;
        gap: 1rem;
        margin-bottom: 1.5rem;
    }

    .calendar-view-header > div:first-child {
        width: 100%;
        justify-content: space-between;
    }

    .calendar-grid {
        grid-template-columns: repeat(7, 1fr);
        gap: 0.5px;
    }

    .cal-header-cell {
        padding: 8px 4px;
        font-size: 0.7rem;
    }

    .cal-day-cell {
        min-height: 80px;
        padding: 4px;
    }

    .cal-day-num {
        font-size: 0.75rem;
        margin-bottom: 4px;
    }

    .cal-event {
        padding: 1px 3px;
        font-size: 0.65rem;
        margin-bottom: 2px;
    }
}

@media (max-width: 480px) {
    .calendar-view-header {
        flex-direction: column;
        gap: 0.75rem;
    }

    .calendar-view-header > div:first-child {
        width: 100%;
        justify-content: center;
    }

    .calendar-view-header h2 {
        width: 100% !important;
        font-size: 1.1rem;
    }

    .calendar-view-header button {
        padding: 8px 12px;
        font-size: 0.8rem;
    }

    .calendar-grid {
        grid-template-columns: repeat(7, 1fr);
        gap: 0.5px;
        border-radius: 4px;
    }

    .cal-header-cell {
        padding: 6px 2px;
        font-size: 0.6rem;
    }

    .cal-day-cell {
        min-height: 60px;
        padding: 3px;
        font-size: 0.7rem;
    }

    .cal-day-num {
        font-size: 0.65rem;
        margin-bottom: 2px;
        width: 18px;
        height: 18px;
    }

    .cal-day-cell.today .cal-day-num {
        width: 18px;
        height: 18px;
        font-size: 0.6rem;
    }

    .cal-event {
        padding: 1px 2px;
        font-size: 0.55rem;
        margin-bottom: 1px;
        white-space: nowrap;
        overflow: hidden;
        text-overflow: ellipsis;
    }
}

@media (max-width: 360px) {
    .calendar-view-header {
        flex-direction: column;
        gap: 0.5rem;
    }

    .calendar-view-header h2 {
        font-size: 1rem;
        width: 100% !important;
    }

    .calendar-grid {
        grid-template-columns: repeat(7, 1fr);
    }

    .cal-header-cell {
        padding: 4px 1px;
        font-size: 0.5rem;
    }

    .cal-day-cell {
        min-height: 50px;
        padding: 2px;
    }

    .cal-day-num {
        font-size: 0.6rem;
        margin-bottom: 1px;
    }

    .cal-event {
        display: none;
    }

    .cal-event.important {
        display: inline-block;
        padding: 0.5px 1px;
        font-size: 0.5rem;
    }
}

/* Fijar header en móviles */
@media (max-width: 768px) {
    .top-bar {
        position: sticky;
        top: 0;
        z-index: 100;
        background: var(--bg-card);
        border-bottom: 1px solid var(--border-light);
        flex-direction: column;
        justify-content: flex-start;
        gap: 1rem;
        padding: 1rem;
        margin-top: 0;
    }

    .breadcrumbs {
        font-size: 1.1rem;
        font-weight: 700;
        color: var(--text-primary);
        order: -1;
        white-space: nowrap;
        overflow: hidden;
        text-overflow: ellipsis;
        width: 100%;
        padding: 0;
        margin: 0;
        text-align: left;
    }

    .search-container {
        width: 100%;
        order: 2;
    }

    .search-container input {
        width: 100%;
        padding: 10px 12px;
        font-size: 14px;
        border: 1px solid var(--border-light);
        border-radius: 8px;
        background: #f5f5f5;
    }

    #search-results {
        width: 100% !important;
        left: 0 !important;
        right: 0 !important;
        max-height: 300px !important;
    }

    .actions {
        width: 100%;
        order: 3;
        display: flex;
        gap: 0.5rem;
    }

    .actions button {
        flex: 1;
        padding: 10px 12px;
        font-size: 0.9rem;
    }

    /* Calendario timeline responsive */
    .calendar-timeline {
        display: flex;
        flex-direction: column;
    }

    .calendar-timeline-row {
        display: contents;
    }

    .calendar-day-card {
        padding: 0.75rem;
        min-height: auto;
    }

    .calendar-day-header h3 {
        font-size: 0.95rem;
    }

    .calendar-event-item {
        font-size: 0.8rem;
        padding: 0.4rem;
    }
}

@media (max-width: 480px) {
    /* Profile and Logout buttons for mobile - Icons only */
    .sidebar-logout-tablet {
        display: flex !important;
        flex-direction: column;
        gap: 8px;
        justify-content: center;
        padding: 1rem 0;
        border-top: 1px solid #333;
        margin-top: auto;
    }

    .profile-link-tablet {
        display: flex !important;
        align-items: center;
        justify-content: center;
        padding: 0.75rem;
        color: #888;
        text-decoration: none;
        border-radius: 6px;
        transition: all 0.2s ease;
        font-size: 0.85rem;
    }

    .profile-link-tablet span {
        display: none;
    }

    .profile-link-tablet i {
        font-size: 1.3rem;
        margin: 0;
    }

    .profile-link-tablet:hover {
        background-color: #222;
        color: var(--primary);
    }

    .logout-link-tablet {
        display: flex !important;
        align-items: center;
        justify-content: center;
        padding: 0.75rem;
        color: #888;
        text-decoration: none;
        border-radius: 6px;
        transition: all 0.2s ease;
        font-size: 0.85rem;
    }

    .logout-link-tablet span {
        display: none;
    }

    .logout-link-tablet i {
        font-size: 1.3rem;
        margin: 0;
    }

    .logout-link-tablet:hover {
        background-color: #222;
        color: #ff3b30;
    }
    .top-bar {
        padding: 0.875rem;
        flex-direction: column;
        justify-content: flex-start;
        gap: 0.875rem;
        margin-top: 0rem;
    }

    .breadcrumbs {
        font-size: 1rem;
        font-weight: 700;
        white-space: nowrap;
        overflow: hidden;
        text-overflow: ellipsis;
        width: 100%;
        padding: 0;
        margin: 0;
        text-align: left;
    }

    .search-container {
        width: 100%;
        order: 2;
    }

    .search-container input {
        padding: 10px 12px;
        font-size: 14px;
        border-radius: 8px;
        background: #f5f5f5;
    }

    .actions {
        width: 100%;
        order: 3;
        display: flex;
        gap: 0.5rem;
    }

    .actions button {
        padding: 10px 12px;
        font-size: 0.9rem;
        flex: 1;
    }

    /* Calendario timeline responsive para móviles */
    .calendar-timeline {
        display: flex;
        flex-direction: column;
    }

    .calendar-timeline-row {
        display: contents;
    }

    .calendar-day-card {
        padding: 0.75rem;
        min-height: auto;
    }

    .calendar-day-header h3 {
        font-size: 0.95rem;
    }

    .calendar-event-item {
        font-size: 0.8rem;
        padding: 0.4rem;
    }
}

@media (max-width: 360px) {
    .top-bar {
        padding: 0.75rem;
        gap: 0.75rem;
    }

    .breadcrumbs {
        font-size: 0.95rem;
        font-weight: 700;
        white-space: nowrap;
        overflow: hidden;
        text-overflow: ellipsis;
        width: 100%;
        padding: 0;
        margin: 0;
        text-align: left;
    }

    .breadcrumbs {
        font-size: 0.85rem;
        font-weight: 600;
        background: rgba(0, 0, 0, 0.04);
        color: var(--text-primary);
        padding: 0.5rem 0.75rem;
        margin: 0;
        border-radius: 6px;
        white-space: nowrap;
        overflow: hidden;
        text-overflow: ellipsis;
        width: auto;
        flex: 1;
    }

    .search-container input {
        padding: 8px 10px;
        font-size: 13px;
    }

    .actions {
        grid-template-columns: 1fr;
    }

    .actions button {
        width: 100%;
        padding: 8px 10px;
        font-size: 0.75rem;
    }
}

/* Ocultar scroll bars en móviles pero mantener funcionalidad */
@media (max-width: 768px) {
    .sidebar::-webkit-scrollbar,
    .main-content::-webkit-scrollbar {
        display: none;
    }

    .sidebar,
    .main-content {
        -ms-overflow-style: none;
        scrollbar-width: none;
    }
}

/* Safe area para notched devices */
@supports (padding: max(0px)) {
    body {
        padding-left: env(safe-area-inset-left);
        padding-right: env(safe-area-inset-right);
    }

    .sidebar {
        padding-top: max(2rem, env(safe-area-inset-top));
        padding-bottom: max(1rem, env(safe-area-inset-bottom));
    }
}

/* Print styles */
@media print {
    .sidebar,
    .header,
    .modal,
    .btn,
    .toast {
        display: none !important;
    }

    .main-content {
        margin-left: 0;
    }

    body {
        overflow-y: auto;
    }
}

/* ========== MOBILE APP EXPERIENCE (max-width: 768px) ========== */
@media (max-width: 768px) {
    
    /* --- APP FEEL & TOUCH OPTIMIZATIONS --- */
    * {
        -webkit-tap-highlight-color: transparent;
    }
    
    .bottom-nav, .btn, button, .nav-links, .card {
        user-select: none;
        -webkit-user-select: none;
    }

    /* --- LAYOUT & NAVIGATION --- */
    
    /* Hide Sidebar & Toggle */
    .sidebar, 
    .sidebar-toggle {
        display: none !important;
    }

    /* Show Bottom Navigation */
    .bottom-nav {
        display: flex !important;
        position: fixed;
        bottom: 0;
        left: 0;
        width: 100%;
        height: 60px; /* Fixed height */
        background: rgba(255, 255, 255, 0.98);
        backdrop-filter: blur(20px);
        -webkit-backdrop-filter: blur(20px);
        border-top: 1px solid var(--border-light);
        padding: 0 10px;
        justify-content: space-around;
        align-items: center;
        z-index: 980;
        box-shadow: 0 -4px 20px rgba(0,0,0,0.04);
        padding-bottom: env(safe-area-inset-bottom);
        height: calc(60px + env(safe-area-inset-bottom));
    }

    .bottom-nav a {
        flex: 1;
        display: flex;
        flex-direction: column;
        align-items: center;
        justify-content: center;
        color: var(--text-secondary);
        text-decoration: none;
        font-size: 10px;
        font-weight: 500;
        gap: 4px;
        height: 100%;
        border-radius: 8px;
        transition: color 0.2s ease;
    }

    .bottom-nav a i {
        font-size: 20px;
        margin-bottom: 2px;
    }

    .bottom-nav a.active {
        color: var(--primary);
    }
    
    /* Active indicator dot (optional style) */
    .bottom-nav a.active::after {
        content: '';
        width: 4px;
        height: 4px;
        background-color: var(--primary);
        border-radius: 50%;
        position: absolute;
        bottom: 5px;
    }

    /* --- MAIN CONTENT ADJUSTMENTS --- */
    
    .main-content {
        margin-left: 0 !important;
        width: 100%;
        height: 100%;
    }

    .content-wrapper {
        padding: 1rem !important;
        padding-bottom: calc(80px + env(safe-area-inset-bottom)) !important; /* Space for bottom nav */
        overflow-x: hidden;
    }

    /* --- HEADER SIMPLIFICATION --- */
    
    .top-bar {
        padding: 0.75rem 1rem;
        min-height: 60px;
        position: sticky;
        top: 0;
        z-index: 900;
        background: rgba(255, 255, 255, 0.95);
        backdrop-filter: blur(10px);
        -webkit-backdrop-filter: blur(10px);
        gap: 0.5rem;
    }

    .breadcrumbs {
        font-size: 0.85rem;
        font-weight: 600;
        white-space: nowrap;
        overflow: hidden;
        text-overflow: ellipsis;
        width: 100%;
    }

    .search-container {
        flex: 1;
        max-width: none;
    }
    
    .search-container input {
        font-size: 16px; /* Prevent zoom */
        background-color: #f2f2f7;
        border: none;
    }

    /* --- COMPONENT OPTIMIZATIONS --- */

    /* Cards go full width or near full width */
    .card, .project-card {
        border-radius: 16px;
        box-shadow: 0 2px 8px rgba(0,0,0,0.04);
        margin-bottom: 1rem;
    }

    /* Typography */
    h1 { font-size: 1.75rem; }
    h2 { font-size: 1.5rem; }
    h3 { font-size: 1.25rem; }

    /* Buttons */
    .btn, button {
        min-height: 44px; /* Touch target */
        display: inline-flex;
        align-items: center;
        justify-content: center;
    }
    
    /* Tables - Horizontal Scroll */
    .table-responsive {
        display: block;
        width: 100%;
        overflow-x: auto;
        -webkit-overflow-scrolling: touch;
    }

    /* Modals - Bottom Sheet style */
    .modal {
        width: 100%;
        height: auto;
        max-height: 90vh;
        bottom: 0;
        top: auto;
        border-radius: 20px 20px 0 0;
        transform: translateY(100%);
        animation: slideUp 0.3s forwards;
        margin: 0;
    }
    
    @keyframes slideUp {
        to { transform: translateY(0); }
    }

    .modal-content {
        max-height: 80vh;
        overflow-y: auto;
    }
    
    /* Login Screen */
    .login-box {
        box-shadow: none;
        background: transparent;
        padding: 1rem;
    }
    
    .login-container {
        background: #fff;
        align-items: flex-start;
        padding-top: 20vh;
    }
}

/* Dashboard Split Grid Responsive */
@media (max-width: 768px) {
    .dashboard-split-grid {
        grid-template-columns: 1fr !important;
        gap: 1rem !important;
    }
}
