/* Additional mobile-specific improvements for form usability */

/* Fix readonly date inputs appearing gray on mobile */
input[readonly].form-control {
    background-color: #fff;
    cursor: pointer;
}

/* Ensure date inputs are touchable and look active */
input[readonly].form-control:focus {
    background-color: #fff;
    border-color: #007bff;
    box-shadow: 0 0 0 0.2rem rgba(0,123,255,.25);
}

/* Fix for iOS input zoom - ensure all inputs have 16px font size */
input[type="text"],
input[type="email"], 
input[type="password"],
input[type="number"],
input[type="tel"],
input[type="url"],
input[type="search"],
input[type="date"],
input[type="time"],
input[type="datetime-local"],
select,
textarea {
    font-size: 16px !important;
}

/* Better form field focus states for accessibility */
.form-control:focus {
    border-color: #007bff;
    box-shadow: 0 0 0 0.2rem rgba(0,123,255,.25);
    outline: none;
}

/* Improve Bootstrap tagsinput for mobile */
.bootstrap-tagsinput {
    border-radius: 6px;
    box-shadow: inset 0 1px 1px rgba(0,0,0,.075);
}

.bootstrap-tagsinput .tag {
    border-radius: 4px;
    font-size: 14px;
    margin: 2px;
    padding: 4px 8px;
}

/* Better date picker styling */
.datepicker {
    font-size: 16px;
    z-index: 10000 !important;
    background: #fff !important;
    background-color: #fff !important;
    border: 1px solid #ccc !important;
    border-radius: 4px !important;
    box-shadow: 0 6px 12px rgba(0,0,0,.175) !important;
    padding: 4px !important;
}

/* Datepicker dropdown - ensure visibility */
.datepicker-dropdown {
    z-index: 10000 !important;
    background: #fff !important;
    background-color: #fff !important;
    border: 1px solid #ccc !important;
    border-radius: 4px !important;
    box-shadow: 0 6px 12px rgba(0,0,0,.175) !important;
    /* Let bootstrap-datepicker handle positioning naturally */
}

/* Ensure all datepicker inner elements have white background */
.datepicker > div,
.datepicker .datepicker-days,
.datepicker .datepicker-months,
.datepicker .datepicker-years,
.datepicker table,
.datepicker thead,
.datepicker tbody {
    background: #fff !important;
    background-color: #fff !important;
}

/* Ensure datepicker is not clipped by parent containers */
.datepicker-dropdown.datepicker-orient-bottom {
    margin-top: 5px !important;
}

/* Desktop datepicker positioning */
@media (min-width: 768px) {
    /* Ensure container doesn't clip datepicker */
    #container {
        overflow: visible !important;
    }

    /* Force datepicker to appear below input */
    #startsDateContainer .datepicker-dropdown {
        top: 100% !important;
        bottom: auto !important;
        margin-top: 5px;
    }
}

/* Bootstrap datetimepicker background fix */
.bootstrap-datetimepicker-widget {
    background-color: #fff !important;
    border: 1px solid #ccc;
    border-radius: 4px;
    box-shadow: 0 6px 12px rgba(0,0,0,.175);
}

.bootstrap-datetimepicker-widget table {
    background-color: #fff;
}

.bootstrap-datetimepicker-widget .datepicker {
    background-color: #fff !important;
}

.bootstrap-datetimepicker-widget .timepicker {
    background-color: #fff !important;
}

.datepicker td, .datepicker th {
    width: 40px;
    height: 40px;
    font-size: 14px;
}

/* Ensure all datepicker text is visible with proper dark color */
.datepicker table tr td,
.datepicker table tr th {
    color: #333 !important;
}

/* Day of week headers */
.datepicker table tr th.dow {
    color: #333 !important;
    font-weight: bold;
}

/* Navigation arrows and month/year header */
.datepicker table tr th.prev,
.datepicker table tr th.next,
.datepicker table tr th.datepicker-switch {
    color: #333 !important;
}

/* Active/selected date */
.datepicker table tr td.active,
.datepicker table tr td.active:hover,
.datepicker table tr td.active.day {
    color: #fff !important;
    background-color: #337ab7 !important;
}

/* Today's date */
.datepicker table tr td.today {
    color: #333 !important;
    background-color: #ffdb99 !important;
}

/* Hover state for dates */
.datepicker table tr td.day:hover {
    color: #333 !important;
    background-color: #eee !important;
}

/* Style dates from previous/next months as grayed out */
.datepicker table tr td.old,
.datepicker table tr td.new {
    color: #999 !important;
}

/* Ensure datepicker is visible on mobile */
@media (max-width: 767px) {
    .datepicker {
        z-index: 10000 !important;
        background-color: #fff !important;
    }

    .datepicker-dropdown {
        z-index: 10000 !important;
        position: fixed !important;
        /* Position with safe margin to avoid browser UI cropping */
        top: 60px !important;
        left: 50% !important;
        right: auto !important;
        bottom: auto !important;
        -webkit-transform: translateX(-50%) !important;
        transform: translateX(-50%) !important;
        width: calc(100vw - 40px) !important;
        max-width: 320px;
        max-height: calc(100vh - 80px);
        overflow-y: auto;
        margin: 0 !important;
        background-color: #fff !important;
        border-radius: 8px;
        box-shadow: 0 10px 40px rgba(0,0,0,0.3);
    }

    /* Hide the arrow on mobile since it's centered */
    .datepicker-dropdown:before,
    .datepicker-dropdown:after {
        display: none !important;
    }

    /* Larger touch targets for datepicker on mobile */
    .datepicker td, .datepicker th {
        width: 44px;
        height: 44px;
        font-size: 16px;
        line-height: 44px;
        text-align: center;
    }

    .datepicker table {
        width: 100%;
    }

    .datepicker .datepicker-switch {
        width: auto;
        font-size: 16px;
        padding: 10px;
    }

    .datepicker .prev, .datepicker .next {
        font-size: 18px;
        padding: 10px 15px;
    }

    /* Ensure datepicker days are properly displayed */
    .datepicker .datepicker-days {
        background-color: #fff;
    }

    .datepicker .datepicker-days table {
        background-color: #fff;
    }

    /* Ensure the month/year navigation header is visible */
    .datepicker thead {
        background-color: #fff;
    }

    .datepicker thead tr:first-child th {
        padding: 12px 8px;
        font-size: 16px;
        font-weight: bold;
    }

    /* Better styling for navigation arrows */
    .datepicker thead tr:first-child th.prev,
    .datepicker thead tr:first-child th.next {
        min-width: 44px;
        min-height: 44px;
    }

    /* Native date input styling for mobile */
    input[type="date"].form-control {
        -webkit-appearance: none;
        -moz-appearance: none;
        appearance: none;
        padding: 12px 15px;
        font-size: 16px !important;
        line-height: 1.5;
        min-height: 48px;
        background-color: #fff;
    }

    /* Ensure date input looks tappable */
    input[type="date"].form-control::-webkit-calendar-picker-indicator {
        opacity: 1;
        cursor: pointer;
        padding: 5px;
    }

    /* Native time input styling for mobile */
    input[type="time"].form-control {
        -webkit-appearance: none;
        -moz-appearance: none;
        appearance: none;
        padding: 12px 15px;
        font-size: 16px !important;
        line-height: 1.5;
        min-height: 48px;
        background-color: #fff;
    }

    /* Ensure time input looks tappable */
    input[type="time"].form-control::-webkit-calendar-picker-indicator {
        opacity: 1;
        cursor: pointer;
        padding: 5px;
    }
}

/* Fix search container width for all screen sizes */
.search-container {
    max-width: 300px;
}

.search-container .input-group {
    width: 100%;
}

/* Mobile specific styles */
@media (max-width: 767px) {
    /* Make toolbar stack on mobile */
    .btn-toolbar {
        display: block !important;
    }
    
    .btn-toolbar .btn-group {
        display: block;
        width: 100%;
        margin-bottom: 10px;
    }
    
    /* Category buttons */
    .category-buttons {
        text-align: center;
    }
    
    .category-buttons .btn {
        margin: 2px;
    }
    
    /* Search container - override desktop max-width for mobile */
    .search-container {
        width: 100%;
        max-width: 400px !important;
        margin: 10px auto;
    }
    
    .search-container .input-group {
        width: 100%;
    }
    
    .search-container .form-control {
        max-width: none;
        width: 100%;
    }
    
    /* Add case button */
    .add-case-container {
        text-align: center;
    }
}

/* Fix modal button alignment */
@media (max-width: 767px) {
    .modal-footer {
        text-align: center;
        padding: 15px 20px;
    }
    
    .modal-footer .btn {
        width: 48%;
        margin: 5px 1%;
        display: inline-block;
        float: none !important;
    }
    
    .modal-footer .pull-right {
        float: none !important;
    }
    
    .modal-dialog {
        margin: 10px;
        width: auto;
        max-width: 95vw;
    }
    
    .modal-header {
        padding: 15px 20px;
        text-align: center;
    }
    
    .modal-header .close {
        position: absolute;
        right: 15px;
        top: 15px;
        margin-top: 0;
    }
    
    .modal-body {
        padding: 20px;
    }
    
    .modal-body .form-control {
        font-size: 16px !important;
        min-height: 44px;
    }
    
    .modal-body .form-group {
        margin-bottom: 20px;
    }
    
    .modal-body label {
        font-weight: bold;
        margin-bottom: 8px;
    }
}

/* Desktop modal improvements */
@media (min-width: 768px) {
    .modal-footer {
        text-align: right;
    }
    
    .modal-footer .btn {
        width: auto;
        margin-left: 10px;
    }
}

/* Improve time picker for mobile */
.bootstrap-timepicker-widget {
    font-size: 16px;
    z-index: 10000 !important;
    background-color: #fff !important;
}

.bootstrap-timepicker-widget table td {
    padding: 10px;
}

.bootstrap-timepicker-widget .btn {
    padding: 8px 12px;
}

/* Ensure timepicker is visible on mobile */
@media (max-width: 767px) {
    .bootstrap-timepicker-widget {
        position: absolute !important;
        top: auto !important;
        left: auto !important;
        right: auto !important;
        -webkit-transform: none !important;
        transform: none !important;
        z-index: 10000 !important;
        background-color: #fff !important;
        border-radius: 8px;
        box-shadow: 0 6px 12px rgba(0,0,0,0.175);
    }

    .bootstrap-timepicker-widget.dropdown-menu {
        position: absolute !important;
    }
}

/* Better file input styling */
.file-input .btn-file {
    font-size: 16px;
    padding: 10px 15px;
}

/* Improve modal dialogs for mobile devices */
@media (max-width: 767px) {
    .modal {
        padding: 10px;
    }
    
    .modal-dialog {
        width: calc(100% - 20px);
        margin: 10px auto;
    }
    
    .modal-content {
        border-radius: 8px;
    }
    
    .modal-header {
        padding: 20px;
        border-bottom: 1px solid #dee2e6;
    }
    
    .modal-body {
        padding: 20px;
        max-height: calc(100vh - 200px);
        overflow-y: auto;
    }
    
    .modal-footer {
        padding: 20px;
        border-top: 1px solid #dee2e6;
    }
}

/* Improve alert and flash message styling */
@media (max-width: 767px) {
    .alert {
        margin-bottom: 20px;
        border-radius: 6px;
        font-size: 16px;
    }
    
    #alert-header {
        margin: 10px;
        border-radius: 6px;
        font-size: 16px;
    }
    
    .flash-message {
        margin: 10px;
        font-size: 16px;
        border-radius: 6px;
    }
}

/* Better spacing and layout for complex forms */
@media (max-width: 767px) {
    /* Improve guardian contact table */
    .table-responsive .table {
        border: none;
        font-size: 14px;
    }
    
    .table-responsive .table td {
        border: 1px solid #ddd;
        vertical-align: top;
    }
    
    .table-responsive .table .input-sm {
        width: 100%;
        box-sizing: border-box;
    }
    
    /* Better button spacing */
    .btn + .btn {
        margin-left: 0;
        margin-top: 10px;
    }
    
    .pull-right {
        float: none !important;
        display: block;
        width: 100%;
        margin-top: 15px;
    }
    
    /* Improve checkbox/radio groups */
    .checkbox-inline + .checkbox-inline,
    .radio-inline + .radio-inline {
        margin-top: 10px;
        margin-left: 0;
    }
}

/* Accessibility improvements */
.sr-only {
    position: absolute;
    width: 1px;
    height: 1px;
    padding: 0;
    margin: -1px;
    overflow: hidden;
    clip: rect(0,0,0,0);
    border: 0;
}

/* High contrast mode support */
@media (prefers-contrast: high) {
    .form-control {
        border: 2px solid #000;
    }
    
    .btn {
        border: 2px solid #000;
    }
}

/* Reduced motion support */
@media (prefers-reduced-motion: reduce) {
    * {
        animation-duration: 0.01ms !important;
        animation-iteration-count: 1 !important;
        transition-duration: 0.01ms !important;
    }
}

/* Mobile navbar improvements */
@media (max-width: 767px) {
    .navbar {
        -webkit-transition: -webkit-transform 0.3s ease-in-out;
        transition: transform 0.3s ease-in-out;
    }

    .navbar-hidden {
        -webkit-transform: translateY(-100%);
        transform: translateY(-100%);
    }
    
    .navbar-collapse.in {
        max-height: calc(100vh - 60px);
        overflow-y: auto;
    }
    
    .navbar-nav > li > a {
        border-bottom: 1px solid rgba(255,255,255,0.1);
    }
    
    .navbar-nav > li:last-child > a {
        border-bottom: none;
    }
}

/* Loading button states */
.btn:disabled {
    opacity: 0.6;
    cursor: not-allowed;
}

.btn .fa-spinner {
    margin-right: 5px;
}

/* Improved focus styles for better accessibility */
.form-control:focus,
.btn:focus,
.dropdown-toggle:focus {
    outline: 2px solid #007bff;
    outline-offset: 2px;
}

/* Better error handling on mobile */
@media (max-width: 767px) {
    .popover {
        position: fixed !important;
        z-index: 9999;
        max-width: calc(100vw - 40px);
        left: 20px !important;
        right: 20px !important;
        width: auto !important;
    }
    
    .popover.top .arrow,
    .popover.bottom .arrow {
        left: 50%;
        -webkit-transform: translateX(-50%);
        transform: translateX(-50%);
    }
    
    /* Fix all modal/popover button alignment issues */
    .popover .popover-content .btn-group {
        display: block;
        text-align: center;
    }
    
    .popover .popover-content .btn {
        width: 45%;
        margin: 2px 2%;
        display: inline-block;
        float: none !important;
    }
    
    /* Bootstrap-editable modal buttons */
    .editable-popup .editable-buttons {
        text-align: center;
    }
    
    .editable-popup .editable-buttons .btn {
        width: 45%;
        margin: 2px 2%;
        display: inline-block;
        float: none !important;
    }
    
    /* Bootstrap-confirmation modal buttons */
    .confirmation-popover .popover-content .btn-group {
        display: block;
        text-align: center;
    }
    
    .confirmation-popover .popover-content .btn {
        width: 45%;
        margin: 2px 2%;
        display: inline-block;
        float: none !important;
    }
}

/* Touch-friendly improvements */
@media (pointer: coarse) {
    .dropdown-menu > li > a {
        padding: 12px 20px;
        font-size: 16px;
    }

    .navbar-toggle {
        padding: 8px 10px;
        margin: 8px 15px;
    }

    .navbar-toggle .icon-bar {
        width: 22px;
        height: 3px;
    }
}

/* Also apply touch-friendly styles on mobile width (for emulator compatibility) */
@media (max-width: 767px) {
    .dropdown-menu > li > a {
        padding: 12px 20px;
        font-size: 16px;
    }

    .navbar-toggle {
        padding: 8px 10px;
        margin: 8px 15px;
    }

    .navbar-toggle .icon-bar {
        width: 22px;
        height: 3px;
    }
}

/* Touch action for better mobile scrolling and interaction */
.btn,
.dropdown-toggle,
.navbar-toggle,
a {
    -ms-touch-action: manipulation;
    touch-action: manipulation;
}

/* Prevent text selection during touch interactions */
.btn,
.dropdown-toggle,
.navbar-toggle {
    -webkit-user-select: none;
    -moz-user-select: none;
    -ms-user-select: none;
    user-select: none;
}

/* Ensure proper tap highlighting on iOS */
a,
button,
.btn,
input[type="submit"],
input[type="button"] {
    -webkit-tap-highlight-color: rgba(0, 0, 0, 0.1);
}

/* Orientation change handling */
@media (orientation: landscape) and (max-height: 500px) {
    .navbar-collapse.in {
        max-height: 200px;
    }
    
    .login-container {
        margin-top: 20px;
    }
    
    .modal-dialog {
        margin: 5px auto;
    }
}