@media (max-width: 768px) {
    #templatesTab {
        padding-top: 2.5rem !important;
    }

    #templatesTab .content-grid {
        display: flex;
        flex-direction: column;
        padding: 0.5rem;
        gap: 0.5rem;
        margin-top: 1.5rem !important;
    }

    #templatesTab .template-card {
        background: rgba(30, 30, 30, 0.95) !important;
        border: 1px solid rgba(138, 43, 226, 0.2) !important;
        border-radius: 10px !important;
        padding: 0.75rem !important;
        margin-bottom: 0.5rem !important;
        box-shadow: 0 2px 8px rgba(0, 0, 0, 0.2) !important;
    }

    #templatesTab .template-card h3 {
        font-size: 0.9rem !important;
        margin-bottom: 0.4rem !important;
        font-weight: 600 !important;
        color: var(--text) !important;
        display: flex !important;
        align-items: center !important;
        gap: 0.4rem !important;
    }

    #templatesTab .template-card h3 i {
        font-size: 0.8rem !important;
        color: var(--primary-light) !important;
    }

    #templatesTab .template-meta {
        display: flex !important;
        flex-wrap: wrap !important;
        gap: 0.3rem !important;
        margin-bottom: 0.5rem !important;
    }

    #templatesTab .template-meta-item {
        background: rgba(138, 43, 226, 0.1) !important;
        padding: 0.2rem 0.4rem !important;
        border-radius: 10px !important;
        font-size: 0.65rem !important;
        color: var(--text-secondary) !important;
        border: 1px solid rgba(138, 43, 226, 0.2) !important;
        display: inline-flex !important;
        align-items: center !important;
        gap: 0.2rem !important;
    }

    #templatesTab .template-meta-item i {
        font-size: 0.6rem !important;
    }

    #templatesTab .template-code-display {
        background: rgba(0, 0, 0, 0.5) !important;
        border: 1px solid rgba(138, 43, 226, 0.2) !important;
        border-radius: 8px !important;
        padding: 0.4rem !important;
        margin-bottom: 0.5rem !important;
        display: flex !important;
        align-items: center !important;
        gap: 0.4rem !important;
        height: auto !important;
    }

    #templatesTab .template-code-display code {
        flex: 1 !important;
        background: transparent !important;
        color: var(--primary-light) !important;
        padding: 0.3rem 0.5rem !important;
        font-size: 0.7rem !important;
        font-family: 'Fira Code', monospace !important;
        border: none !important;
        text-shadow: none !important;
        white-space: nowrap !important;
        overflow-x: auto !important;
        scrollbar-width: thin !important;
        max-width: calc(100% - 50px) !important;
    }

    #templatesTab .copy-btn {
        background: rgba(40, 167, 69, 0.2) !important;
        border: 1px solid rgba(40, 167, 69, 0.3) !important;
        color: #28a745 !important;
        padding: 0.3rem 0.4rem !important;
        border-radius: 6px !important;
        font-size: 0.7rem !important;
        min-width: auto !important;
        flex-shrink: 0 !important;
        display: flex !important;
        align-items: center !important;
        justify-content: center !important;
        height: 28px !important;
        width: 28px !important;
    }

    #templatesTab .copy-btn span {
        display: none !important;
    }

    #templatesTab .copy-btn i {
        font-size: 0.65rem !important;
        margin: 0 !important;
    }

    #templatesTab .template-actions {
        display: flex !important;
        gap: 0.4rem !important;
        margin-top: 0.5rem !important;
    }

    #templatesTab .template-actions .btn {
        flex: 1 !important;
        padding: 0.4rem 0.6rem !important;
        font-size: 0.75rem !important;
        border-radius: 6px !important;
        height: 32px !important;
        display: flex !important;
        align-items: center !important;
        justify-content: center !important;
        gap: 0.3rem !important;
    }

    #templatesTab .template-actions .btn i {
        font-size: 0.7rem !important;
    }

    #templatesTab .template-actions .btn.primary {
        background: linear-gradient(135deg, var(--primary) 0%, var(--primary-dark) 100%) !important;
        border: none !important;
        color: white !important;
    }

    #templatesTab .template-actions .btn.secondary {
        background: rgba(255, 255, 255, 0.05) !important;
        border: 1px solid rgba(255, 255, 255, 0.2) !important;
        color: var(--text-secondary) !important;
    }

    #templatesTab .tab-header {
        padding: 1rem 0.75rem !important;
        margin-bottom: 1.5rem !important;
        margin-top: 1rem !important;
    }

    #templatesTab .tab-header h2 {
        font-size: 1.1rem !important;
        margin-bottom: 0.3rem !important;
    }

    #templatesTab .tab-header p {
        font-size: 0.8rem !important;
        margin: 0 !important;
    }

    #templatesTab .content-actions {
        display: flex !important;
        gap: 0.5rem !important;
        margin-bottom: 1rem !important;
        margin-top: 0.5rem !important;
        padding: 0 0.5rem !important;
    }

    #templatesTab .content-actions .btn {
        flex: 1 !important;
        padding: 0.5rem 0.75rem !important;
        font-size: 0.8rem !important;
        min-width: auto !important;
        height: 36px !important;
    }

    /* Remove excessive animations */
    #templatesTab .template-card::before,
    #templatesTab .template-card::after {
        display: none !important;
    }

    /* Simplify card interactions */
    #templatesTab .template-card:active {
        transform: scale(0.98) !important;
        transition: transform 0.1s ease !important;
    }

    /* Empty state optimization */
    #templatesTab .empty-state {
        padding: 1.5rem 1rem !important;
        text-align: center !important;
    }

    #templatesTab .empty-state i {
        font-size: 1.8rem !important;
        margin-bottom: 0.5rem !important;
    }

    #templatesTab .empty-state h3 {
        font-size: 1rem !important;
        margin-bottom: 0.3rem !important;
    }

    #templatesTab .empty-state p {
        font-size: 0.8rem !important;
        margin-bottom: 0.75rem !important;
    }

    #templatesTab .empty-state .btn {
        padding: 0.5rem 1rem !important;
        font-size: 0.8rem !important;
    }

    /* Mobile Content Section Fix */
    #templatesTab .mobile-content-section {
        padding: 0.5rem !important;
        padding-top: 2.5rem !important;
    }

    #templatesTab .mobile-section-header {
        padding: 1rem !important;
        margin-bottom: 1.5rem !important;
        margin-top: 1rem !important;
        background: rgba(138, 43, 226, 0.1) !important;
        border-radius: 8px !important;
    }

    #templatesTab .mobile-section-header h2 {
        font-size: 1rem !important;
        margin-bottom: 0.2rem !important;
    }

    #templatesTab .mobile-section-header p {
        font-size: 0.75rem !important;
        margin: 0 !important;
        opacity: 0.8 !important;
    }

    /* Fix for account content wrapper */
    .account-content {
        padding-top: 1rem !important;
    }

    /* Fix for tab content wrapper */
    .tab-content#templatesTab {
        padding-top: 1.5rem !important;
    }
}

/* Fix spacing issues on mobile account page */
@media (max-width: 768px) {
    /* Reduce mobile account header spacing */
    .mobile-account-header {
        margin-bottom: 0.5rem !important;
        padding: 0.75rem !important;
    }

    /* Reduce padding-top that was added earlier */
    #templatesTab {
        padding-top: 0.5rem !important;
    }

    #templatesTab .mobile-content-section {
        padding-top: 0.5rem !important;
    }

    #templatesTab .tab-header {
        margin-top: 0 !important;
        margin-bottom: 0.75rem !important;
        padding: 0.5rem !important;
    }

    #templatesTab .mobile-section-header {
        margin-top: 0 !important;
        margin-bottom: 0.75rem !important;
        padding: 0.75rem !important;
    }

    /* Reduce gap between user info and content */
    .account-content {
        padding-top: 0 !important;
    }

    .tab-content#templatesTab {
        padding-top: 0 !important;
    }

    /* Make the mobile nav more compact */
    .mobile-account-nav {
        margin-bottom: 0.5rem !important;
    }

    /* If there's a user profile section, make it more compact */
    .user-profile {
        margin-bottom: 1rem !important;
        padding-bottom: 1rem !important;
    }
}

/* Extra Small Devices (< 480px) */
@media (max-width: 480px) {
    #templatesTab .template-card {
        padding: 0.6rem !important;
    }

    #templatesTab .template-card h3 {
        font-size: 0.85rem !important;
    }

    #templatesTab .template-code-display code {
        font-size: 0.65rem !important;
        padding: 0.25rem 0.4rem !important;
    }

    #templatesTab .template-actions .btn {
        font-size: 0.7rem !important;
        padding: 0.35rem 0.5rem !important;
        height: 28px !important;
    }

    #templatesTab .copy-btn {
        width: 26px !important;
        height: 26px !important;
    }

    #templatesTab .content-actions .btn {
        font-size: 0.75rem !important;
        height: 32px !important;
    }
}

/* Fix for template modal on mobile */
@media (max-width: 768px) {
    /* Ensure modal takes full screen */
    .modal.fullscreen#templateCreatorModal,
    #templateCreatorModal {
        padding: 0 !important;
        position: fixed !important;
        inset: 0 !important;
        width: 100% !important;
        height: 100% !important;
    }

    .modal.fullscreen#templateCreatorModal .modal-content,
    #templateCreatorModal .modal-content {
        width: 100% !important;
        height: 100vh !important;
        max-width: 100% !important;
        max-height: 100vh !important;
        margin: 0 !important;
        border-radius: 0 !important;
        border: none !important;
    }

    /* Fix z-index issues */
    .modal.fullscreen#templateCreatorModal,
    #templateCreatorModal {
        z-index: 9999 !important;
    }

    /* Fix header positioning */
    #templateCreatorModal .modal-header {
        position: fixed !important;
        top: 0 !important;
        left: 0 !important;
        right: 0 !important;
        height: 50px !important;
        z-index: 10000 !important;
        background: rgba(18, 18, 18, 1) !important;
        box-shadow: 0 2px 10px rgba(0, 0, 0, 0.5) !important;
    }

    /* Ensure content is scrollable */
    .modal.fullscreen#templateCreatorModal .template-creator-container,
    #templateCreatorModal .template-creator-container {
        height: calc(100vh - 60px) !important;
        overflow-y: auto !important;
        -webkit-overflow-scrolling: touch !important;
        padding-bottom: 80px !important; /* Space for sticky buttons */
        padding-top: 70px !important; /* More space from top to prevent cutoff */
        margin-top: 0 !important;
    }

    /* Fix form elements in modal */
    #templateCreatorModal input,
    #templateCreatorModal textarea {
        font-size: 16px !important; /* Prevents zoom on iOS */
    }

    /* Ensure sticky buttons are visible */
    #templateCreatorModal .modal-actions {
        position: fixed !important;
        bottom: 0 !important;
        left: 0 !important;
        right: 0 !important;
        background: rgba(18, 18, 18, 0.98) !important;
        backdrop-filter: blur(10px) !important;
        z-index: 100 !important;
    }

    /* First form section should have extra margin */
    #templateCreatorModal .form-section:first-child {
        margin-top: 1rem !important;
    }

    /* Center all content in template creator on mobile */
    #templateCreatorModal .template-creator-container {
        text-align: center !important;
    }

    #templateCreatorModal .form-section {
        text-align: center !important;
        padding: 1rem 0.5rem !important;
    }

    #templateCreatorModal .form-section h4 {
        justify-content: center !important;
        text-align: center !important;
        width: 100% !important;
        font-size: 1.1rem !important;
        margin-bottom: 1rem !important;
    }

    #templateCreatorModal .form-group {
        text-align: center !important;
        margin-bottom: 1.25rem !important;
    }

    #templateCreatorModal .form-group label {
        justify-content: center !important;
        text-align: center !important;
        width: 100% !important;
        margin-bottom: 0.5rem !important;
        font-size: 0.85rem !important;
    }

    #templateCreatorModal .form-group input,
    #templateCreatorModal .form-group textarea {
        text-align: center !important;
        margin: 0 auto !important;
        width: 90% !important;
        max-width: 300px !important;
    }

    #templateCreatorModal .form-group small {
        text-align: center !important;
        display: block !important;
        width: 100% !important;
        margin-top: 0.3rem !important;
        font-size: 0.7rem !important;
    }

    #templateCreatorModal .commands-container {
        text-align: center !important;
        padding: 1rem !important;
    }

    #templateCreatorModal .commands-container h4 {
        text-align: center !important;
        margin-bottom: 1rem !important;
    }

    #templateCreatorModal .command-inputs {
        max-width: 90% !important;
        margin: 0 auto 1rem auto !important;
    }

    #templateCreatorModal .command-item {
        justify-content: center !important;
        max-width: 100% !important;
    }

    #templateCreatorModal .command-item input {
        max-width: 200px !important;
    }

    #templateCreatorModal #addCommandBtn {
        margin: 1rem auto !important;
        display: block !important;
    }

    #templateCreatorModal .modal-actions {
        justify-content: center !important;
        text-align: center !important;
        gap: 1rem !important;
        padding: 1rem !important;
    }

    #templateCreatorModal .modal-actions .btn {
        min-width: 100px !important;
        max-width: 150px !important;
    }
}

/* Improved touch targets */
@media (pointer: coarse) {
    #templatesTab .btn,
    #templatesTab .copy-btn,
    #templatesTab .template-actions button {
        min-height: 44px !important;
        min-width: 44px !important;
    }

    #templatesTab .template-card {
        cursor: pointer !important;
    }
}

/* Dark mode optimizations for OLED screens */
@media (max-width: 768px) and (prefers-color-scheme: dark) {
    #templatesTab .template-card {
        background: #000 !important;
        border-color: rgba(138, 43, 226, 0.3) !important;
    }

    #templatesTab .template-code-display {
        background: rgba(138, 43, 226, 0.05) !important;
    }
}

/* Landscape mobile specific */
@media (max-width: 812px) and (orientation: landscape) {
    #templatesTab .content-grid {
        padding: 0.25rem !important;
    }

    #templatesTab .template-card {
        padding: 0.5rem !important;
    }

    #templatesTab .tab-header {
        display: none !important;
    }

    #templatesTab .content-actions {
        margin-bottom: 0.5rem !important;
    }
}