/* =============================================================================
   DYR Billing System - Responsive CSS
   Mobile-first approach for phones, tablets, and desktop
   ============================================================================= */

/* =============================================================================
   CSS Variables
   ============================================================================= */
:root {
    --sidebar-width: 220px;
    --sidebar-collapsed-width: 60px;
    --navbar-height: 46px;
    --transition-speed: 0.3s;
    --primary-color: #3498db;
    --sidebar-bg: #2c3e50;
}

/* =============================================================================
   Base Reset & Global
   ============================================================================= */
* {
    box-sizing: border-box;
}

body {
    overflow-x: hidden;
    -webkit-text-size-adjust: 100%;
}

/* =============================================================================
   Navbar Responsive
   ============================================================================= */
.navbar {
    background-color: var(--primary-color) !important;
    padding: 0.5rem 1rem;
    z-index: 1050;
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    height: var(--navbar-height);
}

.navbar ~ .content-wrapper {
    padding-top: var(--navbar-height);
}

.navbar .navbar-brand {
    font-size: 1rem;
    white-space: nowrap;
}

.navbar .badge-role {
    font-size: 0.65rem;
}

@media (max-width: 575.98px) {
    .navbar .navbar-brand {
        font-size: 0.85rem;
    }
    .navbar .navbar-brand i {
        margin-right: 0.25rem;
    }
    .navbar .d-flex.align-items-center > span.text-white {
        max-width: 120px;
        overflow: hidden;
        text-overflow: ellipsis;
        white-space: nowrap;
        font-size: 0.8rem;
    }
    .navbar .badge-role {
        display: none;
    }
    .navbar .badge.bg-secondary {
        display: none;
    }
    .navbar .btn-outline-light.btn-sm {
        padding: 0.2rem 0.5rem;
        font-size: 0.75rem;
    }
}

@media (max-width: 767.98px) {
    .navbar .badge.bg-secondary[title] {
        max-width: 100px;
        overflow: hidden;
        text-overflow: ellipsis;
        display: inline-block;
        vertical-align: middle;
    }
}

/* =============================================================================
   Sidebar Responsive
   ============================================================================= */
.sidebar {
    min-height: 100vh;
    background-color: var(--sidebar-bg);
    transition: all var(--transition-speed) ease;
    width: var(--sidebar-width);
    position: fixed;
    top: var(--navbar-height);
    left: 0;
    bottom: 0;
    overflow-y: auto;
    overflow-x: hidden;
    z-index: 1040;
    -webkit-overflow-scrolling: touch;
}

.sidebar.collapsed {
    width: var(--sidebar-collapsed-width);
}

.sidebar.collapsed .nav-link span {
    display: none;
}

.sidebar.collapsed .nav-link {
    text-align: center;
    padding: 0.75rem 0.5rem;
}

.sidebar.collapsed .nav-link i {
    font-size: 1.2rem;
    margin-right: 0;
}

.sidebar.collapsed .sidebar-section-label {
    display: none;
}

.sidebar .nav-link {
    padding: 0.6rem 1rem;
    color: #ecf0f1;
    font-size: 0.9rem;
    white-space: nowrap;
    transition: background-color 0.2s;
    border-radius: 6px;
    margin: 2px 6px;
}

.sidebar .nav-link:hover {
    background-color: rgba(255, 255, 255, 0.1);
    color: #fff;
}

.sidebar .nav-link.active {
    background-color: var(--primary-color);
    color: #fff;
}

.sidebar .nav-link i {
    width: 24px;
    text-align: center;
    margin-right: 8px;
    font-size: 1.1rem;
}

.sidebar-section-label {
    color: rgba(255, 255, 255, 0.4) !important;
    font-size: 0.7rem;
    text-transform: uppercase;
    letter-spacing: 0.5px;
    padding: 0.8rem 1rem 0.3rem;
    margin-top: 0.5rem;
}

.sidebar-toggle {
    cursor: pointer;
    color: #ecf0f1;
    padding: 0.5rem;
    margin-bottom: 1rem;
    text-align: right;
}

.sidebar-toggle:hover {
    color: #fff;
}

/* Sidebar overlay for mobile */
.sidebar-overlay {
    display: none;
    position: fixed;
    top: var(--navbar-height);
    left: 0;
    right: 0;
    bottom: 0;
    background: rgba(0, 0, 0, 0.5);
    z-index: 1035;
    -webkit-backdrop-filter: blur(2px);
    backdrop-filter: blur(2px);
}

.sidebar-overlay.active {
    display: block;
}

/* Mobile sidebar - offcanvas style */
@media (max-width: 991.98px) {
    .sidebar {
        transform: translateX(-100%);
        width: var(--sidebar-width) !important;
        z-index: 1045;
        box-shadow: 4px 0 15px rgba(0, 0, 0, 0.3);
    }

    .sidebar.mobile-open {
        transform: translateX(0);
    }

    .sidebar.collapsed {
        transform: translateX(-100%);
    }

    /* Show hamburger button on mobile */
    .sidebar-mobile-toggle {
        display: inline-flex !important;
    }
}

@media (min-width: 992px) {
    .sidebar-mobile-toggle {
        display: none !important;
    }

    .sidebar-overlay {
        display: none !important;
    }
}

/* =============================================================================
   Content Wrapper
   ============================================================================= */
.content-wrapper {
    display: flex;
    min-height: calc(100vh - var(--navbar-height));
}

.main-content {
    flex: 1;
    padding: 1.5rem;
    transition: margin-left var(--transition-speed) ease;
    margin-left: var(--sidebar-width);
    min-width: 0;
    width: 100%;
}

.sidebar.collapsed ~ .main-content,
.main-content.sidebar-collapsed {
    margin-left: var(--sidebar-collapsed-width);
}

@media (max-width: 991.98px) {
    .main-content {
        margin-left: 0 !important;
        padding: 1rem 0.75rem;
    }
}

@media (max-width: 575.98px) {
    .main-content {
        padding: 0.75rem 0.5rem;
    }
}

/* =============================================================================
   Page Headers
   ============================================================================= */
.page-header,
.d-flex.justify-content-between.align-items-center.mb-4 {
    flex-wrap: wrap;
    gap: 0.5rem;
}

@media (max-width: 575.98px) {
    .page-header h1,
    h1 {
        font-size: 1.3rem;
    }

    h2 {
        font-size: 1.2rem;
    }
}

/* =============================================================================
   Cards Responsive
   ============================================================================= */
.card {
    transition: box-shadow 0.2s ease;
    border-radius: 8px;
    overflow: hidden;
}

.card:hover {
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
}

.card-header {
    padding: 0.75rem 1rem;
}

.card-body {
    padding: 1rem;
}

@media (max-width: 575.98px) {
    .card-header {
        padding: 0.6rem 0.75rem;
        font-size: 0.9rem;
    }

    .card-body {
        padding: 0.75rem;
    }

    .card-header h5,
    .card-header h6 {
        font-size: 0.95rem;
    }
}

/* Stats Cards */
.stats-card .display-5,
.card .display-5 {
    font-size: 2rem;
}

@media (max-width: 767.98px) {
    .stats-card .display-5,
    .card .display-5 {
        font-size: 1.5rem;
    }
}

@media (max-width: 575.98px) {
    .stats-card .display-5,
    .card .display-5 {
        font-size: 1.2rem;
    }
}

/* =============================================================================
   Tables Responsive
   ============================================================================= */
.table-responsive {
    -webkit-overflow-scrolling: touch;
}

.table {
    font-size: 0.9rem;
}

.table th,
.table td {
    padding: 0.6rem 0.75rem;
    vertical-align: middle;
}

/* Table hover effects */
.table-hover tbody tr:hover {
    background-color: rgba(52, 152, 219, 0.1);
}

@media (max-width: 767.98px) {
    .table {
        font-size: 0.8rem;
    }

    .table th,
    .table td {
        padding: 0.4rem 0.5rem;
    }

    .table .btn-sm {
        padding: 0.2rem 0.4rem;
        font-size: 0.7rem;
    }
}

@media (max-width: 575.98px) {
    .table {
        font-size: 0.75rem;
    }

    .table th,
    .table td {
        padding: 0.3rem 0.4rem;
    }

    .table .badge {
        font-size: 0.65rem;
        padding: 0.2rem 0.4rem;
    }
}

/* =============================================================================
   Mobile Card View for Tables
   Converts tables to card-based layout on very small screens
   ============================================================================= */
@media (max-width: 575.98px) {
    .table-card-mobile thead {
        display: none;
    }

    .table-card-mobile tbody tr {
        display: block;
        border: 1px solid #dee2e6;
        border-radius: 8px;
        margin-bottom: 0.75rem;
        padding: 0.75rem;
        background: #fff;
        box-shadow: 0 1px 3px rgba(0, 0, 0, 0.05);
    }

    .table-card-mobile tbody td {
        display: flex;
        justify-content: space-between;
        align-items: center;
        padding: 0.3rem 0;
        border: none;
        border-bottom: 1px solid #f0f0f0;
    }

    .table-card-mobile tbody td:last-child {
        border-bottom: none;
        justify-content: flex-end;
        padding-top: 0.5rem;
        gap: 0.25rem;
    }

    .table-card-mobile tbody td::before {
        content: attr(data-label);
        font-weight: 600;
        font-size: 0.75rem;
        color: #6c757d;
        text-transform: uppercase;
        letter-spacing: 0.3px;
        flex-shrink: 0;
        margin-right: 0.5rem;
    }
}

/* =============================================================================
   Dashboard Specific
   ============================================================================= */
.dashboard-welcome {
    padding: 1.5rem;
    border-radius: 12px;
}

@media (max-width: 767.98px) {
    .dashboard-welcome {
        padding: 1rem;
        border-radius: 8px;
    }

    .dashboard-welcome h4 {
        font-size: 1.1rem;
    }
}

@media (max-width: 575.98px) {
    .dashboard-welcome .d-flex.align-items-center.justify-content-between {
        flex-direction: column;
        text-align: center;
    }

    .dashboard-welcome .d-flex.align-items-center.justify-content-between > .d-flex {
        justify-content: center;
    }

    .dashboard-welcome .mt-2.mt-md-0 {
        margin-top: 0.75rem !important;
        display: flex;
        gap: 0.5rem;
        justify-content: center;
    }

    .dashboard-welcome .btn {
        font-size: 0.8rem;
        padding: 0.35rem 0.75rem;
    }
}

/* Dashboard stat cards */
.dashboard-stat-card .card-body {
    padding: 1rem;
}

.dashboard-stat-card h3 {
    font-size: 1.5rem;
}

@media (max-width: 575.98px) {
    .dashboard-stat-card .card-body {
        padding: 0.75rem;
    }

    .dashboard-stat-card .d-flex.align-items-center {
        flex-direction: column;
        text-align: center;
    }

    .dashboard-stat-card .d-flex .rounded-circle,
    .dashboard-stat-card .d-flex .p-3 {
        margin-right: 0 !important;
        margin-bottom: 0.5rem;
        padding: 0.5rem !important;
    }

    .dashboard-stat-card h3 {
        font-size: 1.2rem;
    }

    .dashboard-stat-card h5 {
        font-size: 0.9rem;
    }

    .dashboard-stat-card small {
        font-size: 0.7rem;
    }

    .dashboard-stat-card .card-footer {
        font-size: 0.7rem;
        padding: 0.4rem;
    }
}

/* Invoice status cards in dashboard */
.invoice-status-grid .card {
    min-height: auto;
}

@media (max-width: 575.98px) {
    .invoice-status-grid .col-6 {
        flex: 0 0 50%;
        max-width: 50%;
    }

    .invoice-status-grid .card-body {
        padding: 0.5rem !important;
    }

    .invoice-status-grid .badge {
        font-size: 1rem !important;
    }

    .invoice-status-grid small,
    .invoice-status-grid .small {
        font-size: 0.65rem;
    }
}

/* Exchange rate card */
@media (max-width: 575.98px) {
    .exchange-rate-card .h4 {
        font-size: 1.1rem !important;
    }
}

/* =============================================================================
   Invoices List Specific
   ============================================================================= */
.invoices-filters .row {
    gap: 0;
}

@media (max-width: 767.98px) {
    .invoices-filters .col-md-2 {
        flex: 0 0 50%;
        max-width: 50%;
    }
}

@media (max-width: 575.98px) {
    .invoices-filters .col-md-2 {
        flex: 0 0 100%;
        max-width: 100%;
    }
}

/* Invoice clave (key) truncation */
.invoice-clave {
    max-width: 80px;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
    display: inline-block;
}

@media (min-width: 768px) {
    .invoice-clave {
        max-width: 120px;
    }
}

/* Currency summary cards */
@media (max-width: 575.98px) {
    .currency-summary .row .col-md-4,
    .currency-summary .row .col-md-6,
    .currency-summary .row .col-md-12 {
        flex: 0 0 100%;
        max-width: 100%;
    }

    .currency-summary .card-body {
        padding: 0.5rem;
    }

    .currency-summary .row.text-center .col-4 {
        padding: 0 0.25rem;
    }

    .currency-summary .row.text-center .col-4 small {
        font-size: 0.6rem;
    }

    .currency-summary .row.text-center .col-4 .fw-bold {
        font-size: 0.75rem;
    }
}

/* =============================================================================
   Invoice Form
   ============================================================================= */
.invoice-line {
    background-color: #f8f9fa;
    padding: 1rem;
    margin-bottom: 0.5rem;
    border-radius: 0.25rem;
    border-left: 3px solid var(--primary-color);
}

@media (max-width: 767.98px) {
    .invoice-line {
        padding: 0.75rem;
    }
}

.invoice-totals {
    background-color: #e9ecef;
    padding: 1rem;
    border-radius: 0.25rem;
}

.customer-search-results {
    position: absolute;
    z-index: 1000;
    width: calc(100% - 2rem);
    max-height: 250px;
    overflow-y: auto;
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
    border-radius: 0 0 8px 8px;
}

@media (max-width: 575.98px) {
    .customer-search-results {
        width: calc(100% - 1rem);
        max-height: 200px;
    }
}

/* =============================================================================
   CABYS List Specific
   ============================================================================= */
@media (max-width: 767.98px) {
    .cabys-filters .col-md-4,
    .cabys-filters .col-md-3,
    .cabys-filters .col-md-2,
    .cabys-filters .col-lg-2,
    .cabys-filters .col-lg-3 {
        flex: 0 0 50%;
        max-width: 50%;
    }

    .cabys-filters .col-md-12.col-lg-3 {
        flex: 0 0 100%;
        max-width: 100%;
    }
}

@media (max-width: 575.98px) {
    .cabys-filters .col-md-4,
    .cabys-filters .col-md-3,
    .cabys-filters .col-md-2,
    .cabys-filters .col-lg-2,
    .cabys-filters .col-lg-3 {
        flex: 0 0 100%;
        max-width: 100%;
    }
}

/* CABYS table description truncation */
.cabys-description {
    max-width: 200px;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
    display: inline-block;
}

@media (min-width: 768px) {
    .cabys-description {
        max-width: 400px;
    }
}

@media (min-width: 1200px) {
    .cabys-description {
        max-width: 500px;
    }
}

/* CABYS modal */
@media (max-width: 575.98px) {
    #detailModal .modal-dialog {
        margin: 0.5rem;
    }

    #detailModal .modal-body .table td {
        font-size: 0.75rem;
    }
}

/* =============================================================================
   Locations List Specific
   ============================================================================= */
@media (max-width: 767.98px) {
    .locations-filters .col-md-3,
    .locations-filters .col-md-6,
    .locations-filters .col-lg-2 {
        flex: 0 0 50%;
        max-width: 50%;
    }
}

@media (max-width: 575.98px) {
    .locations-filters .col-md-3,
    .locations-filters .col-md-6,
    .locations-filters .col-lg-2 {
        flex: 0 0 100%;
        max-width: 100%;
    }
}

/* =============================================================================
   Buttons Responsive
   ============================================================================= */
.btn {
    white-space: nowrap;
}

.btn-group-responsive {
    display: flex;
    gap: 0.25rem;
}

@media (max-width: 575.98px) {
    .btn-group-responsive {
        flex-direction: column;
    }

    .btn-group-responsive .btn {
        width: 100%;
    }
}

/* =============================================================================
   Forms Responsive
   ============================================================================= */
.form-label {
    font-size: 0.9rem;
    font-weight: 500;
}

.form-control,
.form-select {
    font-size: 0.9rem;
}

@media (max-width: 575.98px) {
    .form-label {
        font-size: 0.8rem;
        margin-bottom: 0.2rem;
    }

    .form-control,
    .form-select {
        font-size: 0.85rem;
        padding: 0.4rem 0.6rem;
    }

    .form-text {
        font-size: 0.7rem;
    }
}

/* =============================================================================
   Badges Responsive
   ============================================================================= */
.badge {
    font-size: 0.75rem;
}

@media (max-width: 575.98px) {
    .badge {
        font-size: 0.65rem;
        padding: 0.2rem 0.4rem;
    }
}

/* =============================================================================
   Pagination Responsive
   ============================================================================= */
.pagination {
    flex-wrap: wrap;
    justify-content: center;
    gap: 2px;
}

.pagination .page-link {
    padding: 0.35rem 0.65rem;
    font-size: 0.85rem;
}

@media (max-width: 575.98px) {
    .pagination .page-link {
        padding: 0.25rem 0.5rem;
        font-size: 0.75rem;
    }
}

/* =============================================================================
   Alerts Responsive
   ============================================================================= */
@media (max-width: 575.98px) {
    .alert {
        padding: 0.6rem 0.75rem;
        font-size: 0.85rem;
    }

    .alert h6 {
        font-size: 0.9rem;
    }
}

/* =============================================================================
   Modals Responsive
   ============================================================================= */
@media (max-width: 575.98px) {
    .modal-dialog {
        margin: 0.5rem;
        max-width: calc(100% - 1rem);
    }

    .modal-header {
        padding: 0.75rem;
    }

    .modal-body {
        padding: 0.75rem;
    }

    .modal-footer {
        padding: 0.5rem 0.75rem;
    }
}

/* =============================================================================
   Loading & Spinners
   ============================================================================= */
.spinner-overlay {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: rgba(255, 255, 255, 0.8);
    display: flex;
    justify-content: center;
    align-items: center;
    z-index: 9999;
}

/* htmx indicators */
.htmx-indicator {
    display: none;
}

.htmx-request .htmx-indicator,
.htmx-request.htmx-indicator {
    display: inline-block;
}

/* =============================================================================
   Toast Notifications
   ============================================================================= */
.toast-container {
    position: fixed;
    top: 1rem;
    right: 1rem;
    z-index: 1080;
}

@media (max-width: 575.98px) {
    .toast-container {
        top: auto;
        bottom: 1rem;
        right: 0.5rem;
        left: 0.5rem;
    }

    .toast-container .toast {
        width: 100%;
    }
}

/* =============================================================================
   Login Page
   ============================================================================= */
@media (max-width: 767.98px) {
    .login-page .w-75 {
        width: 90% !important;
    }
}

/* =============================================================================
   Print Styles
   ============================================================================= */
@media print {
    .sidebar,
    .navbar,
    .sidebar-overlay,
    .sidebar-mobile-toggle,
    .toast-container {
        display: none !important;
    }

    .main-content {
        margin-left: 0 !important;
        padding: 0 !important;
    }

    .card {
        border: 1px solid #ccc !important;
        box-shadow: none !important;
        break-inside: avoid;
    }
}

/* =============================================================================
   Utility Classes
   ============================================================================= */
.text-truncate-2 {
    display: -webkit-box;
    -webkit-line-clamp: 2;
    line-clamp: 2;
    -webkit-box-orient: vertical;
    overflow: hidden;
}

.text-truncate-3 {
    display: -webkit-box;
    -webkit-line-clamp: 3;
    line-clamp: 3;
    -webkit-box-orient: vertical;
    overflow: hidden;
}

/* Hover shadow */
.hover-shadow:hover {
    box-shadow: 0 4px 12px rgba(0,0,0,0.1) !important;
    transform: translateY(-2px);
}

/* Spin animation */
.spin {
    animation: spin 0.5s linear infinite;
}

@keyframes spin {
    from { transform: rotate(0deg); }
    to { transform: rotate(360deg); }
}

/* Scrollbar styling for sidebar */
.sidebar::-webkit-scrollbar {
    width: 4px;
}

.sidebar::-webkit-scrollbar-track {
    background: transparent;
}

.sidebar::-webkit-scrollbar-thumb {
    background: rgba(255, 255, 255, 0.2);
    border-radius: 2px;
}

.sidebar::-webkit-scrollbar-thumb:hover {
    background: rgba(255, 255, 255, 0.3);
}

/* Safe area insets for notched phones */
@supports (padding: env(safe-area-inset-bottom)) {
    .sidebar {
        padding-bottom: env(safe-area-inset-bottom);
    }

    .main-content {
        padding-bottom: calc(1rem + env(safe-area-inset-bottom));
    }

    .navbar {
        padding-left: calc(1rem + env(safe-area-inset-left));
        padding-right: calc(1rem + env(safe-area-inset-right));
    }
}

/* =============================================================================
   Touch-friendly targets (min 44px)
   ============================================================================= */
@media (max-width: 991.98px) {
    .sidebar .nav-link {
        min-height: 44px;
        display: flex;
        align-items: center;
    }

    .table .btn-sm {
        min-width: 36px;
        min-height: 36px;
        display: inline-flex;
        align-items: center;
        justify-content: center;
    }

    .pagination .page-link {
        min-width: 40px;
        min-height: 40px;
        display: flex;
        align-items: center;
        justify-content: center;
    }
}
