/* 響應式設計 - 解決手機端文字破版問題 */

/* 基礎響應式設定 */
* {
    box-sizing: border-box;
}

/* 手機端 (小於768px) */
@media (max-width: 767.98px) {
    /* 基礎文字大小調整 */
    body {
        font-size: 14px !important;
        line-height: 1.4 !important;
    }
    
    /* 標題文字調整 */
    h1 {
        font-size: 1.5rem !important;
        margin-bottom: 0.5rem !important;
    }
    
    h2 {
        font-size: 1.3rem !important;
        margin-bottom: 0.4rem !important;
    }
    
    h3 {
        font-size: 1.1rem !important;
        margin-bottom: 0.3rem !important;
    }
    
    h4, h5, h6 {
        font-size: 1rem !important;
        margin-bottom: 0.2rem !important;
    }
    
    /* 卡片標題調整 */
    .card-title {
        font-size: 1rem !important;
        font-weight: 600 !important;
    }
    
    /* 內容區域調整 */
    .content-wrapper {
        padding: 10px !important;
    }
    
    .content-header {
        padding: 10px 15px !important;
    }
    
    .content-header h1 {
        font-size: 1.3rem !important;
        margin: 0 !important;
    }
    
    /* 卡片間距調整 */
    .card {
        margin-bottom: 15px !important;
    }
    
    .card-body {
        padding: 15px !important;
    }
    
    .card-header {
        padding: 10px 15px !important;
    }
    
    /* 按鈕調整 */
    .btn {
        font-size: 0.875rem !important;
        padding: 0.375rem 0.75rem !important;
    }
    
    .btn-sm {
        font-size: 0.75rem !important;
        padding: 0.25rem 0.5rem !important;
    }
    
    /* 表單元素調整 */
    .form-control {
        font-size: 14px !important;
        padding: 0.375rem 0.75rem !important;
    }
    
    .form-label {
        font-size: 0.875rem !important;
        margin-bottom: 0.25rem !important;
    }
    
    /* 統計卡片調整 */
    .small-box {
        margin-bottom: 15px !important;
    }
    
    .small-box .inner h3 {
        font-size: 1.5rem !important;
        font-weight: bold !important;
    }
    
    .small-box .inner p {
        font-size: 0.875rem !important;
    }
}

/* 通用元件調整 */
/* 麵包屑調整 */
.breadcrumb {
    font-size: 0.875rem !important;
    margin-bottom: 0.5rem !important;
}

/* 警告訊息調整 */
.alert {
    font-size: 0.875rem !important;
    padding: 0.75rem 1rem !important;
}

/* 徽章調整 */
.badge {
    font-size: 0.75rem !important;
    padding: 0.25rem 0.5rem !important;
}

/* 工具提示調整 */
.tooltip {
    font-size: 0.75rem !important;
}

/* 分頁調整 */
.pagination {
    font-size: 0.875rem !important;
}

.page-link {
    padding: 0.375rem 0.75rem !important;
}

/* 平板端 (768px - 991.98px) */
@media (min-width: 768px) and (max-width: 991.98px) {
    body {
        font-size: 15px !important;
    }
    
    h1 {
        font-size: 1.75rem !important;
    }
    
    h2 {
        font-size: 1.5rem !important;
    }
    
    h3 {
        font-size: 1.25rem !important;
    }
    
    .card-title {
        font-size: 1.1rem !important;
    }
    
    .content-wrapper {
        padding: 15px !important;
        margin-left: 250px !important; /* 確保平板端也有左邊距 */
    }
    
    .small-box .inner h3 {
        font-size: 1.75rem !important;
    }
}

/* 左側選單固定定位 - 適用於所有螢幕尺寸 */
.main-sidebar {
    position: fixed !important;
    top: 0 !important;
    left: 0 !important;
    height: 100vh !important;
    z-index: 1040 !important;
    overflow-y: auto !important;
    overflow-x: hidden !important;
}

/* 確保選單內容可以獨立滾動 */
.main-sidebar .sidebar {
    height: calc(100vh - 57px) !important; /* 減去頂部logo區域高度 */
    overflow-y: auto !important;
    overflow-x: hidden !important;
    padding-bottom: 2rem !important;
}

/* 選單滾動條樣式 */
.main-sidebar .sidebar::-webkit-scrollbar {
    width: 6px !important;
}

.main-sidebar .sidebar::-webkit-scrollbar-track {
    background: rgba(255, 255, 255, 0.1) !important;
}

.main-sidebar .sidebar::-webkit-scrollbar-thumb {
    background: rgba(255, 255, 255, 0.3) !important;
    border-radius: 3px !important;
}

.main-sidebar .sidebar::-webkit-scrollbar-thumb:hover {
    background: rgba(255, 255, 255, 0.5) !important;
}

/* 調整內容區域，避免被選單遮擋 */
.content-wrapper {
    margin-left: 250px !important;
    min-height: 100vh !important;
}

/* 當選單收合時調整內容區域 */
.sidebar-collapse .content-wrapper {
    margin-left: 57px !important;
}

/* 導航和選單調整 */
@media (max-width: 767.98px) {
    .main-header {
        padding: 0.5rem 1rem !important;
    }
    
    .navbar-nav .nav-link {
        padding: 0.5rem !important;
    }
    
    .main-sidebar {
        width: 250px !important;
        /* 手機端選單預設隱藏 */
        transform: translateX(-100%) !important;
        transition: transform 0.3s ease-in-out !important;
    }
    
    .sidebar .nav-link {
        padding: 0.5rem 1rem !important;
    }
    
    /* 手機端選單展開時 */
    .sidebar-open .main-sidebar {
        transform: translateX(0) !important;
    }
    
    /* 手機端內容區域不需要左邊距 */
    .content-wrapper {
        margin-left: 0 !important;
    }
    
    .sidebar-collapse .content-wrapper {
        margin-left: 0 !important;
    }
    
    /* 手機端選單遮罩 */
    .sidebar-open::before {
        content: '' !important;
        position: fixed !important;
        top: 0 !important;
        left: 0 !important;
        width: 100% !important;
        height: 100% !important;
        background: rgba(0, 0, 0, 0.5) !important;
        z-index: 1039 !important;
    }
}

/* 表格響應式調整 */
@media (max-width: 991.98px) {
    /* 表格容器 - 平板和手機都使用水平滾動 */
    .table-responsive,
    .table-responsive-mobile,
    .card-body {
        overflow-x: auto !important;
        -webkit-overflow-scrolling: touch !important;
    }
    
    /* 報表表格特殊處理 */
    .table-fixed,
    .table {
        font-size: 0.8rem !important;
        margin-bottom: 0 !important;
        min-width: 800px !important; /* 確保表格有足夠寬度避免擠壓 */
        white-space: nowrap !important;
    }
}

@media (max-width: 767.98px) {
    /* 手機端表格進一步優化 */
    .table-fixed,
    .table {
        font-size: 0.75rem !important;
        min-width: 900px !important; /* 手機端需要更大的最小寬度 */
    }
    
    .table th,
    .table td {
        padding: 0.4rem 0.3rem !important;
        vertical-align: middle !important;
        border-top: 1px solid #dee2e6 !important;
        white-space: nowrap !important;
        min-width: 90px !important; /* 增加最小欄位寬度 */
    }
    
    .table thead th {
        font-size: 0.75rem !important;
        font-weight: 600 !important;
        background-color: #f8f9fa !important;
        border-bottom: 2px solid #dee2e6 !important;
        position: sticky !important;
        top: 0 !important;
        z-index: 10 !important;
        padding: 0.5rem 0.4rem !important; /* 表頭稍微增加內距 */
    }
    
    /* 報表表格容器特殊處理 - 使用更精確的選擇器 */
    .card .card-body.p-0 {
        padding: 0 !important; /* 移除內距讓表格有更多空間 */
    }
    
    /* 篩選條件卡片保持正常內距 */
    .card .card-body:not(.p-0) {
        padding: 1rem !important;
    }
    
    /* 報表表格專用樣式 - 禁用卡片模式，強制使用水平滾動 */
    .report-table {
        display: table !important;
        width: 100% !important;
        table-layout: auto !important;
    }
    
    .report-table thead,
    .report-table tbody,
    .report-table tr {
        display: table-row-group !important;
    }
    
    .report-table thead tr,
    .report-table tbody tr {
        display: table-row !important;
    }
    
    .report-table th,
    .report-table td {
        display: table-cell !important;
    }
    
    /* 確保報表表格不會轉換為卡片模式 */
    .report-table.mobile-responsive-table.card-layout-active {
        display: table !important;
    }
    
    .report-table.mobile-responsive-table.card-layout-active thead {
        display: table-header-group !important;
    }
    
    .report-table.mobile-responsive-table.card-layout-active tbody {
        display: table-row-group !important;
    }
    
    .report-table.mobile-responsive-table.card-layout-active tr {
        display: table-row !important;
        border: none !important;
        margin: 0 !important;
        padding: 0 !important;
        background: transparent !important;
        box-shadow: none !important;
    }
    
    .report-table.mobile-responsive-table.card-layout-active td {
        display: table-cell !important;
        border-top: 1px solid #dee2e6 !important;
        padding: 0.4rem 0.3rem !important;
        white-space: nowrap !important;
    }
    
    /* 表格滾動條樣式優化 */
    .card-body::-webkit-scrollbar {
        height: 8px !important;
    }
    
    .card-body::-webkit-scrollbar-track {
        background: #f1f1f1 !important;
        border-radius: 4px !important;
    }
    
    .card-body::-webkit-scrollbar-thumb {
        background: #c1c1c1 !important;
        border-radius: 4px !important;
    }
    
    .card-body::-webkit-scrollbar-thumb:hover {
        background: #a8a8a8 !important;
    }
    
    /* 滾動提示 */
    .table-scroll-hint {
        position: relative !important;
    }
    
    .table-scroll-hint::after {
        content: "← 左右滑動查看更多 →" !important;
        position: absolute !important;
        bottom: -25px !important;
        left: 50% !important;
        transform: translateX(-50%) !important;
        font-size: 0.7rem !important;
        color: #6c757d !important;
        background: rgba(255, 255, 255, 0.9) !important;
        padding: 2px 8px !important;
        border-radius: 10px !important;
        white-space: nowrap !important;
        z-index: 5 !important;
        animation: fadeInOut 3s ease-in-out !important;
    }
    
    @keyframes fadeInOut {
        0%, 100% { opacity: 0; }
        20%, 80% { opacity: 1; }
    }
    
    /* 隱藏滾動提示當表格被滾動過後 */
    .table-scroll-hint.scrolled::after {
        display: none !important;
    }
    
    /* 卡片式表格佈局 */
    .mobile-responsive-table.card-layout-active {
        min-width: auto !important;
        display: block !important;
    }
    
    .mobile-responsive-table.card-layout-active thead {
        display: none !important;
    }
    
    .mobile-responsive-table.card-layout-active tbody {
        display: block !important;
    }
    
    .mobile-responsive-table.card-layout-active .mobile-card-row {
        display: block !important;
        border: 1px solid #dee2e6 !important;
        border-radius: 0.375rem !important;
        margin-bottom: 1rem !important;
        padding: 0.75rem !important;
        background: white !important;
        box-shadow: 0 2px 4px rgba(0,0,0,0.1) !important;
    }
    
    .mobile-responsive-table.card-layout-active .mobile-card-row td {
        display: block !important;
        border: none !important;
        padding: 0.25rem 0 !important;
        min-width: auto !important;
        white-space: normal !important;
        word-wrap: break-word !important;
    }
    
    .mobile-responsive-table.card-layout-active .mobile-card-row td:before {
        content: attr(data-label) ": " !important;
        font-weight: 600 !important;
        color: #495057 !important;
        display: inline-block !important;
        width: 40% !important;
        margin-right: 0.5rem !important;
    }
    
    /* 手機端標籤樣式 */
    .mobile-label {
        font-weight: 600 !important;
        color: #495057 !important;
        display: inline-block !important;
        min-width: 35% !important;
        margin-right: 0.5rem !important;
        font-size: 0.75rem !important;
    }
    
    /* 數字欄位樣式 */
    .mobile-responsive-table .numeric-cell,
    .mobile-responsive-table .currency-cell {
        text-align: right !important;
        font-family: 'Courier New', monospace !important;
        font-weight: 500 !important;
    }
    
    .mobile-responsive-table.card-layout-active .numeric-cell,
    .mobile-responsive-table.card-layout-active .currency-cell {
        text-align: left !important;
    }
    
    /* 狀態欄位樣式 */
    .mobile-responsive-table .status-cell .badge {
        font-size: 0.65rem !important;
        padding: 0.2rem 0.4rem !important;
    }
    
    /* 固定表頭的表格 */
    .table-fixed {
        display: block !important;
        width: 100% !important;
        overflow-x: auto !important;
        white-space: nowrap !important;
    }
    
    .table-fixed thead {
        display: table-header-group !important;
    }
    
    .table-fixed tbody {
        display: table-row-group !important;
        max-height: 300px !important;
        overflow-y: auto !important;
    }
    
    /* 表格內的按鈕 */
    .table .btn {
        font-size: 0.7rem !important;
        padding: 0.2rem 0.4rem !important;
        margin: 0.1rem !important;
    }
    
    .table .btn-sm {
        font-size: 0.65rem !important;
        padding: 0.15rem 0.3rem !important;
    }
    
    /* 表格內的徽章 */
    .table .badge {
        font-size: 0.65rem !important;
        padding: 0.2rem 0.4rem !important;
        margin: 0.1rem !important;
    }
    
    /* 表格搜尋容器 */
    .table-search-container {
        margin-bottom: 0.75rem !important;
    }
    
    .table-search-container .form-control {
        font-size: 0.875rem !important;
    }
    
    /* 表格分頁 */
    .table-pagination .pagination {
        margin-bottom: 0 !important;
    }
    
    .table-pagination .page-link {
        font-size: 0.75rem !important;
        padding: 0.375rem 0.5rem !important;
    }
    
    /* 隱藏/顯示欄位控制 */
    .optional-column {
        display: none !important;
    }
    
    .important-column {
        display: table-cell !important;
    }
    
    /* 卡片佈局時顯示所有欄位 */
    .mobile-responsive-table.card-layout-active .optional-column {
        display: block !important;
    }
    
    /* 表格工具列 */
    .table-tools {
        margin-bottom: 0.75rem !important;
        display: flex !important;
        flex-wrap: wrap !important;
        gap: 0.5rem !important;
    }
    
    .table-tools .btn {
        font-size: 0.75rem !important;
        padding: 0.25rem 0.5rem !important;
    }
    
    /* 表格載入狀態 */
    .table-loading {
        position: relative !important;
    }
    
    .table-loading:after {
        content: "載入中..." !important;
        position: absolute !important;
        top: 50% !important;
        left: 50% !important;
        transform: translate(-50%, -50%) !important;
        background: rgba(255, 255, 255, 0.9) !important;
        padding: 1rem !important;
        border-radius: 0.375rem !important;
        font-size: 0.875rem !important;
        z-index: 100 !important;
    }
}

/* 圖表響應式調整 */
@media (max-width: 767.98px) {
    .chart-container {
        height: 250px !important;
        margin-bottom: 15px !important;
    }
    
    /* Chart.js 響應式 */
    .chart-container canvas {
        max-height: 250px !important;
    }
    
    /* 圖表標題 */
    .chart-title {
        font-size: 0.875rem !important;
        margin-bottom: 10px !important;
    }
    
    /* 圖例調整 */
    .chart-legend {
        font-size: 0.75rem !important;
    }
}

/* 篩選表單響應式調整 */
@media (max-width: 991.98px) {
    /* 平板端篩選條件優化 */
    .card .card-body form.row {
        margin: 0 !important;
        padding: 0.5rem !important;
    }
    
    .card .card-body .col-md-1,
    .card .card-body .col-md-2,
    .card .card-body .col-md-3,
    .card .card-body .col-md-4,
    .card .card-body .col-md-6 {
        padding: 0.5rem !important;
        margin-bottom: 0.75rem !important;
    }
}

@media (max-width: 767.98px) {
    /* 手機端篩選條件特殊處理 */
    .card .card-body {
        padding: 1rem !important; /* 確保卡片內有足夠內距 */
    }
    
    .card .card-body form.row {
        margin: 0 !important;
        padding: 0 !important; /* 表單本身不需要額外內距 */
    }
    
    .card .card-body .col-md-1,
    .card .card-body .col-md-2,
    .card .card-body .col-md-3,
    .card .card-body .col-md-4,
    .card .card-body .col-md-6,
    .card .card-body [class*="col-"] {
        padding: 0.5rem 0.75rem !important; /* 增加欄位內距 */
        margin-bottom: 1rem !important;
    }
    
    .card .card-body .form-label {
        font-size: 0.875rem !important;
        margin-bottom: 0.5rem !important;
        font-weight: 600 !important;
        color: #495057 !important;
    }
    
    .card .card-body .form-control,
    .card .card-body .form-select,
    .card .card-body select {
        font-size: 0.875rem !important;
        padding: 0.5rem 0.75rem !important;
        border-radius: 0.375rem !important;
        border: 1px solid #ced4da !important;
        background-color: #fff !important;
        min-height: 38px !important; /* 確保觸控友善的最小高度 */
    }
    
    .card .card-body .form-control:focus,
    .card .card-body .form-select:focus,
    .card .card-body select:focus {
        border-color: #80bdff !important;
        box-shadow: 0 0 0 0.2rem rgba(0, 123, 255, 0.25) !important;
    }
    
    .card .card-body .btn {
        width: 100% !important;
        margin-top: 0.5rem !important;
        padding: 0.5rem 1rem !important;
        font-size: 0.875rem !important;
        min-height: 38px !important;
    }
    
    /* 日期輸入框特殊處理 */
    .card .card-body input[type="date"] {
        padding: 0.4rem 0.75rem !important;
    }
    
    /* 分析期間的雙欄位處理 */
    .card .card-body .row .col-6 {
        padding: 0 0.25rem !important;
        margin-bottom: 0 !important;
    }
    
    .card .card-body .row .col-6:first-child {
        padding-left: 0 !important;
    }
    
    .card .card-body .row .col-6:last-child {
        padding-right: 0 !important;
    }
    
    /* 按鈕容器對齊 */
    .card .card-body .d-flex.align-items-end {
        align-items: flex-start !important;
        padding-top: 1.5rem !important;
    }
    
    /* 篩選條件卡片標題 */
    .card .card-header {
        padding: 0.75rem 1rem !important;
    }
    
    .card .card-header .card-title {
        font-size: 1rem !important;
        margin-bottom: 0 !important;
    }
    
    /* 確保所有表單元件在手機端有適當間距 */
    .card .card-body .form-group {
        margin-bottom: 1rem !important;
    }
    
    /* 下拉選單箭頭優化 */
    .card .card-body select {
        /* background-image: url("data:image/svg+xml,%3csvg xmlns='http://www.w3.org/2000/svg' fill='none' viewBox='0 0 20 20'%3e%3cpath stroke='%236b7280' stroke-linecap='round' stroke-linejoin='round' stroke-width='1.5' d='m6 8 4 4 4-4'/%3e%3c/svg%3e") !important; */
        background-position: right 0.5rem center !important;
        background-repeat: no-repeat !important;
        background-size: 1.5em 1.5em !important;
        padding-right: 2.5rem !important;
    }
}

/* 統計卡片網格調整 */
@media (max-width: 767.98px) {
    .row .col-lg-3,
    .row .col-lg-4,
    .row .col-lg-6,
    .row .col-md-3,
    .row .col-md-4,
    .row .col-md-6 {
        margin-bottom: 15px !important;
    }
}

/* 載入指示器調整 */
@media (max-width: 767.98px) {
    .loading-overlay {
        font-size: 0.875rem !important;
    }
    
    .loading-overlay .fa-spin {
        font-size: 1.5rem !important;
    }
}

/* 工具列調整 */
@media (max-width: 767.98px) {
    .content-header .d-flex {
        flex-direction: column !important;
        align-items: flex-start !important;
    }
    
    .content-header .btn {
        margin-top: 0.5rem !important;
        margin-left: 0 !important;
    }
    
    .card-tools {
        font-size: 0.75rem !important;
    }
}

/* 模態框調整 */
@media (max-width: 767.98px) {
    .modal-dialog {
        margin: 0.5rem !important;
        max-width: calc(100% - 1rem) !important;
    }
    
    .modal-title {
        font-size: 1.1rem !important;
    }
    
    .modal-body {
        padding: 1rem !important;
        font-size: 0.875rem !important;
    }
}

/* 下拉選單調整 */
@media (max-width: 767.98px) {
    .dropdown-menu {
        font-size: 0.875rem !important;
        min-width: 150px !important;
    }
    
    .dropdown-item {
        padding: 0.375rem 1rem !important;
    }
}

/* 進度條調整 */
@media (max-width: 767.98px) {
    .progress {
        height: 1rem !important;
    }
    
    .progress-bar {
        font-size: 0.75rem !important;
    }
}

/* 文字截斷處理 */
@media (max-width: 767.98px) {
    .text-truncate-mobile {
        overflow: hidden !important;
        text-overflow: ellipsis !important;
        white-space: nowrap !important;
        max-width: 150px !important;
    }
    
    .text-wrap-mobile {
        white-space: normal !important;
        word-wrap: break-word !important;
        word-break: break-all !important;
    }
}

/* 隱藏/顯示工具類別 */
@media (max-width: 767.98px) {
    .d-mobile-none {
        display: none !important;
    }
    
    .d-mobile-block {
        display: block !important;
    }
    
    .d-mobile-inline {
        display: inline !important;
    }
    
    .d-mobile-inline-block {
        display: inline-block !important;
    }
}

@media (min-width: 768px) {
    .d-desktop-none {
        display: none !important;
    }
}

/* 特殊調整 - AdminLTE 覆蓋 */
@media (max-width: 767.98px) {
    /* 修正 AdminLTE 的固定問題 */
    .content-wrapper,
    .main-footer {
        margin-left: 0 !important;
    }
    
    /* 側邊欄在手機上的處理 */
    .sidebar-mini.sidebar-collapse .main-sidebar {
        margin-left: -250px !important;
    }
    
    /* 確保內容區域正確顯示 */
    .wrapper .content-wrapper {
        min-height: calc(100vh - 56px) !important;
    }
}
