/* =========================================
   CORE STYLES & RESETS
   ========================================= */
body { 
    font-family: 'Inter', sans-serif; 
    background-color: #f8fafc; 
    margin: 0;
    padding: 0;
}

/* Loader Spinner */
.loader { 
    border: 3px solid #f3f3f3; 
    border-top: 3px solid #2563eb; 
    border-radius: 50%; 
    width: 20px; 
    height: 20px; 
    animation: spin 1s linear infinite; 
}
@keyframes spin { 
    0% { transform: rotate(0deg); } 
    100% { transform: rotate(360deg); } 
}

/* Custom Scrollbar */
::-webkit-scrollbar { width: 8px; height: 8px; }
::-webkit-scrollbar-track { background: #f1f5f9; }
::-webkit-scrollbar-thumb { background: #cbd5e1; border-radius: 4px; }
::-webkit-scrollbar-thumb:hover { background: #94a3b8; }

/* Modal Animation */
.modal-content { animation: slide-up 0.3s ease-out; }
@keyframes slide-up { from { transform: translateY(20px); opacity: 0; } to { transform: translateY(0); opacity: 1; } }

/* =========================================
   ANIMATIONS & ALERTS
   ========================================= */

/* Alert Blink Animation (Hiệu ứng nhấp nháy cấp bách) */
@keyframes blink-urgent {
    0%, 100% { opacity: 1; transform: scale(1); }
    50% { opacity: 0.5; transform: scale(1.1); color: #ef4444; } 
}

/* Class dùng cho user config cũ */
.animate-blink-red {
    animation: blink-urgent 1s cubic-bezier(0.4, 0, 0.6, 1) infinite; 
    font-weight: bold;
}

/* [QUAN TRỌNG] Class khớp với Code JS "Cấp bách" */
.blink-urgent {
    animation: blink-urgent 1s cubic-bezier(0.4, 0, 0.6, 1) infinite;
    font-weight: bold;
    color: #dc2626 !important;
    background-color: #fef2f2;
    border: 1px solid #fee2e2;
}

/* =========================================
   MODULE SPECIFIC STYLES
   ========================================= */

/* --- OCR MODULE --- */
#ocr-image-preview {
    transition: transform 0.1s; 
    cursor: grab;
    transform-origin: center center;
}
#ocr-image-preview:active { cursor: grabbing; }

/* --- THEME OVERRIDE (TAX MODE) --- */
body.theme-tax .loader { border-top-color: #f97316; }
body.theme-tax .bg-blue-600 { background-color: #f97316 !important; }
body.theme-tax .hover\:bg-blue-700:hover { background-color: #ea580c !important; }
body.theme-tax .text-blue-600 { color: #f97316 !important; }
body.theme-tax .focus\:ring-blue-500:focus { --tw-ring-color: #f97316 !important; }

/* --- APP SWITCHER (SIDEBAR) --- */
.module-btn {
    transition: all 0.2s ease;
    position: relative;
}
.module-btn.active {
    background-color: rgba(30, 58, 138, 0.4); /* blue-900/40 */
    color: #60a5fa; /* blue-400 */
}
.module-btn.active::before {
    content: '';
    position: absolute;
    left: -8px; /* Dính vào lề trái container */
    top: 50%;
    transform: translateY(-50%);
    width: 4px;
    height: 24px;
    background-color: #60a5fa;
    border-radius: 0 4px 4px 0;
}
.module-content.hidden { display: none !important; }

/* --- DOCS MODULE --- */
.status-dot { width: 8px; height: 8px; border-radius: 50%; display: inline-block; }
.col-action { min-width: 140px; white-space: nowrap; text-align: center; }

/* Action Buttons */
.action-btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 32px;
    height: 32px;
    border-radius: 6px;
    margin: 0 4px;
    border: none;
    cursor: pointer;
    transition: all 0.2s;
    position: relative;
    z-index: 20; /* Fix lỗi click */
}
.btn-edit { background-color: #e0f2fe; color: #0284c7; }
.btn-edit:hover { background-color: #0284c7; color: white; }

.btn-qr { background-color: #f3e8ff; color: #9333ea; }
.btn-qr:hover { background-color: #9333ea; color: white; }

.btn-delete { background-color: #fee2e2; color: #dc2626; }
.btn-delete:hover { background-color: #dc2626; color: white; }

/* Toggle Switch */
.toggle-checkbox:checked { right: 0; border-color: #2563eb; }
.toggle-checkbox:checked + .toggle-label { background-color: #2563eb; }
.toggle-checkbox:checked + .toggle-label:before { transform: translateX(100%); }
.toggle-label {
    width: 44px; height: 24px; background-color: #e5e7eb; border-radius: 9999px;
    position: relative; cursor: pointer; transition: background-color 0.2s ease-in;
}
.toggle-label:before {
    content: ''; position: absolute; top: 2px; left: 2px; width: 20px; height: 20px;
    border-radius: 50%; background-color: white; box-shadow: 0 1px 2px rgba(0,0,0,0.2);
    transition: transform 0.2s ease-in;
}

/* Document Compact Card & Drag Drop */
.doc-card-compact { transition: all 0.2s; border: 1px solid #e2e8f0; }
.doc-card-compact:hover { border-color: #cbd5e1; box-shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.1); }
.drag-handle { cursor: grab; color: #94a3b8; }
.drag-handle:active { cursor: grabbing; color: #475569; }
.sortable-ghost { background-color: #f1f5f9; opacity: 0.5; border: 2px dashed #94a3b8; }
.sortable-drag { background-color: #fff; box-shadow: 0 10px 15px -3px rgba(0, 0, 0, 0.1); opacity: 1; }

/* =========================================
   AMC DEBT MODULE STYLES
   ========================================= */

/* 1. Status Colors & Badges */
.text-debt-green { color: #10b981; } 
.text-debt-yellow { color: #f59e0b; } 
.text-debt-orange { color: #f97316; } 
.text-debt-red { color: #ef4444; } 

.bg-debt-green-50 { background-color: #ecfdf5; }
.bg-debt-yellow-50 { background-color: #fffbeb; }
.bg-debt-orange-50 { background-color: #fff7ed; }
.bg-debt-red-50 { background-color: #fef2f2; }

.status-badge {
    display: inline-flex; align-items: center; padding: 2px 8px;
    border-radius: 9999px; font-size: 0.75rem; font-weight: 600;
}
.status-badge.paid { background-color: #dcfce7; color: #166534; }
.status-badge.partial { background-color: #fef3c7; color: #92400e; }
.status-badge.unpaid { background-color: #fee2e2; color: #991b1b; }

/* 2. KPI Cards */
.kpi-card { transition: transform 0.2s, box-shadow 0.2s; cursor: pointer; border: 1px solid #e2e8f0; }
.kpi-card:hover { transform: translateY(-2px); box-shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.1); border-color: #cbd5e1; }
.kpi-card.active { border-color: #3b82f6; background-color: #eff6ff; box-shadow: 0 0 0 2px #bfdbfe; }

/* 3. Quick Filter Chips */
.filter-chip {
    padding: 6px 16px; border-radius: 9999px; font-size: 0.875rem; cursor: pointer;
    border: 1px solid #e2e8f0; background: white; color: #64748b; transition: all 0.2s;
    display: inline-flex; align-items: center; gap: 6px;
}
.filter-chip:hover { background-color: #f8fafc; border-color: #cbd5e1; }
.filter-chip.active {
    background-color: #eff6ff; border-color: #3b82f6; color: #1d4ed8; font-weight: 500;
    box-shadow: 0 1px 2px rgba(0,0,0,0.05);
}

/* 4. Drawer & Modal */
.drawer-backdrop {
    position: fixed; inset: 0; background-color: rgba(0, 0, 0, 0.5);
    z-index: 40; opacity: 0; pointer-events: none; transition: opacity 0.3s ease;
}
.drawer-backdrop.open { opacity: 1; pointer-events: auto; }
.customer-drawer {
    position: fixed; top: 0; right: -100%; width: 100%; max-width: 600px; height: 100vh;
    background-color: white; z-index: 50; box-shadow: -4px 0 15px rgba(0, 0, 0, 0.1);
    transition: right 0.3s cubic-bezier(0.4, 0, 0.2, 1); display: flex; flex-direction: column;
}
.customer-drawer.open { right: 0; }

.upload-area {
    border: 2px dashed #cbd5e1; border-radius: 0.5rem; padding: 2rem; text-align: center;
    transition: all 0.2s; background-color: #f8fafc; cursor: pointer;
}
.upload-area:hover, .upload-area.dragover { border-color: #3b82f6; background-color: #eff6ff; }

/* 5. Timeline */
.timeline-container { position: relative; padding-left: 16px; }
.timeline-item { position: relative; padding-left: 24px; border-left: 2px solid #e2e8f0; padding-bottom: 24px; }
.timeline-item:last-child { border-left-color: transparent; }
.timeline-dot {
    position: absolute; left: -7px; top: 0; width: 12px; height: 12px;
    border-radius: 50%; background-color: #cbd5e1; border: 2px solid white;
}
.timeline-item.urgent .timeline-dot { background-color: #ef4444; }
.timeline-item.success .timeline-dot { background-color: #10b981; }

/* =========================================
   Z-INDEX & CLICK FIXES
   ========================================= */
/* Đảm bảo dòng bảng nằm trên các lớp nền */
#debt-table-body tr { position: relative; z-index: 1; }
/* Đảm bảo các ô click được có con trỏ tay */
.cursor-pointer { cursor: pointer !important; position: relative; z-index: 10; }

/* =========================================
   MOBILE REVAMP V2 (FULL SCREEN & CLEAN CARD)
   ========================================= */

@media (max-width: 768px) {
    /* 1. ẨN SIDEBAR & HEADER CŨ CỦA PC */
    #docs-sidebar-container, 
    #pc-view-header,
    .desktop-only { 
        display: none !important; 
    }

    /* 2. CẤU TRÚC KHUNG MOBILE */
    body {
        background-color: #f1f5f9; /* Nền xám nhẹ để nổi bật Card trắng */
        padding-bottom: 80px; /* Chừa chỗ cho Bottom Nav */
    }

    /* Container chính */
    #docs-main-body {
        position: static !important; /* Bỏ fixed cũ để scroll tự nhiên */
        width: 100% !important;
        height: auto !important;
        padding: 70px 12px 20px 12px !important; /* Top padding tránh đè Header */
        overflow: visible !important;
        margin: 0 !important;
        background: transparent !important;
    }

    /* 3. HEADER CỐ ĐỊNH (TOP BAR) */
    #mobile-header-bar {
        position: fixed; top: 0; left: 0; right: 0;
        height: 60px;
        background: white;
        z-index: 100;
        display: flex; align-items: center; gap: 10px;
        padding: 0 12px;
        box-shadow: 0 2px 10px rgba(0,0,0,0.03);
    }
    
    #mobile-header-search-container {
        flex-grow: 1; position: relative;
    }
    #mobile-search-input {
        width: 100%; height: 38px;
        border-radius: 8px; border: 1px solid #e2e8f0;
        background: #f8fafc; padding-left: 36px; padding-right: 10px;
        font-size: 14px; outline: none; transition: all 0.2s;
    }
    #mobile-search-input:focus { border-color: #3b82f6; background: white; }

    /* 4. THANH FILTER TRƯỢT NGANG (Dưới Header) */
    #mobile-filter-scroll {
        position: fixed; top: 60px; left: 0; right: 0;
        height: 44px;
        background: white;
        z-index: 90;
        display: flex; align-items: center; gap: 8px;
        padding: 0 12px;
        overflow-x: auto; /* Cho phép vuốt ngang */
        border-bottom: 1px solid #f1f5f9;
        white-space: nowrap;
        /* Ẩn thanh cuộn nhưng vẫn vuốt được */
        -ms-overflow-style: none; scrollbar-width: none;
    }
    #mobile-filter-scroll::-webkit-scrollbar { display: none; }

    /* Nâng body padding top lên để tránh bị che bởi Filter bar */
    #docs-main-body { padding-top: 110px !important; }

    /* 5. GIAO DIỆN CARD MỚI (DẠNG HỘP) */
    .mobile-card {
        background: white;
        border-radius: 12px;
        padding: 12px;
        margin-bottom: 12px;
        box-shadow: 0 1px 3px rgba(0,0,0,0.05);
        border: 1px solid white;
        position: relative;
        transition: transform 0.1s;
    }
    .mobile-card:active { transform: scale(0.98); background: #f8fafc; }

    /* Dòng 1: Tiêu đề & Menu */
    .m-card-header {
        display: flex; justify-content: space-between; align-items: flex-start;
        margin-bottom: 8px;
    }
    .m-card-title-group { padding-right: 40px; } /* Chừa chỗ cho nút menu */
    .m-card-stt { 
        display: inline-block; font-size: 10px; font-weight: 800; 
        color: #94a3b8; margin-right: 4px; 
    }
    .m-card-name {
        font-size: 14px; font-weight: 700; color: #1e293b; 
        line-height: 1.3; display: inline;
    }
    .m-card-short {
        font-size: 12px; color: #94a3b8; font-weight: 500;
        margin-left: 4px;
    }

    /* Nút Menu 3 chấm (Action) */
    .m-card-actions {
        position: absolute; top: 12px; right: 12px;
        display: flex; gap: 4px;
    }
    .m-btn-icon {
        width: 32px; height: 32px;
        border-radius: 6px;
        display: flex; align-items: center; justify-content: center;
        background: #f1f5f9; color: #64748b;
        border: none;
    }

    /* Dòng 2: Thông số (Docs & Warning) */
    .m-card-stats {
        display: flex; align-items: center; gap: 16px;
        padding-top: 8px; border-top: 1px dashed #e2e8f0;
        font-size: 13px; font-weight: 500;
    }
    .stat-item { display: flex; align-items: center; gap: 5px; }
    .stat-item.blue { color: #2563eb; }
    .stat-item.orange { color: #f97316; }

    /* 6. BOTTOM NAVIGATION (THANH DƯỚI ĐÁY) */
    #mobile-bottom-nav {
        display: flex !important; position: fixed; bottom: 0; left: 0; right: 0;
        height: 60px; background: white; 
        border-top: 1px solid #e2e8f0;
        z-index: 999;
        justify-content: space-around; align-items: center;
        padding-bottom: env(safe-area-inset-bottom);
        box-shadow: 0 -4px 10px rgba(0,0,0,0.03);
    }
    .nav-tab {
        flex: 1; display: flex; flex-direction: column; align-items: center; justify-content: center;
        height: 100%; color: #94a3b8; gap: 4px; font-size: 10px; font-weight: 600;
    }
    .nav-tab.active { color: #2563eb; }
    .nav-tab i { width: 20px; height: 20px; }

    /* --- ANIMATION CHO USER MENU (MOBILE) --- */
    @keyframes slideUpBottom {
        from { transform: translateY(100%); }
        to { transform: translateY(0); }
    }
    
    @keyframes fadeIn {
        from { opacity: 0; }
        to { opacity: 1; }
    }

    /* Áp dụng cho Backdrop (Nền đen) */
    #user-menu-backdrop {
        animation: fadeIn 0.2s ease-out forwards;
    }

    /* Áp dụng cho Hộp thoại (Trượt từ đáy) */
    #user-menu-backdrop > div {
        animation: slideUpBottom 0.3s cubic-bezier(0.16, 1, 0.3, 1) forwards;
    }
}

/* =========================================
   DEEP MOBILE PATCH V4.0 (SALES KIT & MODALS)
   ========================================= */

@media (max-width: 768px) {
    
    /* 1. FORCE FULL SCREEN MODAL ON MOBILE
       ------------------------------------
       Giúp Modal chiếm trọn màn hình điện thoại, 
       tạo cảm giác như một trang ứng dụng riêng biệt.
    */
    .modal {
        align-items: flex-end !important; /* Neo xuống đáy hoặc full */
        padding: 0 !important;
    }

    .modal-content {
        width: 100vw !important;
        height: 100dvh !important; /* Dynamic Viewport Height: Tránh lỗi thanh địa chỉ Safari */
        max-width: 100vw !important;
        max-height: 100dvh !important;
        border-radius: 0 !important;
        display: flex;
        flex-direction: column;
        margin: 0 !important;
    }

    /* Đảm bảo nội dung bên trong Modal cuộn được */
    .modal-content > div.flex-grow {
        flex-grow: 1;
        overflow-y: auto;
        -webkit-overflow-scrolling: touch; /* Cuộn mượt trên iOS */
    }

    /* 2. SALES KIT BUILDER (TAB STYLE)
       ------------------------------------
       Style cho thanh tab Nguồn/Đã chọn mới thêm ở JS
    */
    #form-saleskit {
        display: flex;
        flex-direction: column;
        height: 100%;
    }

    #form-saleskit .grid-cols-3 {
        display: flex !important;
        flex-direction: column !important;
        flex-grow: 1;
        overflow: hidden;
        margin: 0 !important;
        gap: 0 !important;
    }

    /* Ẩn hiện cột dựa trên JS (CSS hỗ trợ transition nếu cần) */
    .col-span-1, .col-span-2 {
        height: 100% !important;
        padding: 0 !important;
        border: none !important;
        overflow: hidden !important;
    }

    /* Style cho Tab Switcher */
    #sk-mobile-tabs {
        display: flex;
        background: #fff;
        border-bottom: 1px solid #e2e8f0;
        margin: 0 -1.5rem 1rem -1.5rem; /* Kéo tràn lề modal padding */
        padding: 0 0.5rem;
    }
    
    #sk-mobile-tabs button {
        position: relative;
        transition: all 0.2s;
        outline: none;
    }

/* 1. Ép cột chứa (Container) thành Flex dọc và full chiều cao */
/* Lưu ý: Thay .col-span-1 bằng class cụ thể hơn nếu có thể để tránh ảnh hưởng chỗ khác */
.col-span-1 {
    display: flex !important;
    flex-direction: column !important;
    height: 100% !important;
}

/* 2. Đảm bảo các ô nhập liệu (Tên, Mô tả) không bị giãn ra */
/* Chỉ áp dụng cho các div KHÔNG PHẢI là danh sách */
.col-span-1 > div:not(#sk-list-selected) {
    flex-shrink: 0 !important;
    flex-grow: 0 !important;
    margin-bottom: 8px !important; /* Force margin nhỏ (8px) */
}

#sk-list-selected {
    /* Bắt buộc danh sách phải chiếm hết khoảng trống còn lại */
    flex-grow: 1 !important; 
    
    /* Reset chiều cao để nó hoạt động linh hoạt theo flex-grow */
    height: auto !important; 
    min-height: 0 !important; /* Fix lỗi scroll trên Flexbox lồng nhau */
    
    /* Cho phép cuộn nội dung bên trong vùng màu xám */
    overflow-y: auto !important;
    
    /* Đảm bảo màu nền xám phủ kín xuống tận đáy */
    background-color: #f8fafc !important; 
    
    /* Giữ padding đáy để không bị nút Lưu che mất */
    padding-bottom: 80px !important; 
    
    /* Loại bỏ border/margin thừa nếu có */
    border: none !important;
    margin: 0 !important;
}



    /* Nút Lưu Hồ Sơ cố định ở đáy Modal */
    #form-saleskit .border-t {
        position: absolute;
        bottom: 0;
        left: 0;
        right: 0;
        background: white;
        z-index: 50;
        padding-bottom: max(1rem, env(safe-area-inset-bottom));
        box-shadow: 0 -4px 10px rgba(0,0,0,0.05);
    }

    /* 3. TINH CHỈNH SAFE AREA (IPHONE)
       ------------------------------------
    */
    /* Đẩy body lên để không bị Bottom Nav che */
    #docs-main-body {
        padding-bottom: calc(70px + env(safe-area-inset-bottom)) !important;
    }
    
    /* Bottom Nav căn chỉnh vùng an toàn */
    #mobile-bottom-nav {
        height: calc(60px + env(safe-area-inset-bottom)) !important;
        padding-bottom: env(safe-area-inset-bottom) !important;
    }
}

/* =========================================
   PRINT STYLES (BÁO CÁO A4)
   ========================================= */
@media print {
    @page { 
        size: A4; 
        margin: 10mm; 
    }

    /* 1. Ẩn tất cả mọi thứ trên trang web */
    body * { 
        visibility: hidden; 
        height: 0; /* Thu gọn chiều cao để không bị trang trắng */
        overflow: hidden; 
    }
    
    /* 2. Chỉ hiện đúng vùng chứa báo cáo */
    #printable-report-container, 
    #printable-report-container * {
        visibility: visible;
        height: auto;
        overflow: visible;
    }

    /* 3. Căn chỉnh vùng báo cáo đè lên tất cả */
    #printable-report-container {
        position: absolute;
        left: 0;
        top: 0;
        width: 100%;
        margin: 0;
        padding: 0;
        background: white;
        color: black;
        font-family: 'Times New Roman', Times, serif; /* Font chuẩn văn bản */
        z-index: 9999;
        display: block !important;
    }

    /* 4. Tinh chỉnh giao diện in */
    /* Tắt bóng đổ để văn bản sắc nét */
    .shadow-sm, .shadow-md, .shadow-lg, .shadow-2xl { 
        box-shadow: none !important; 
    }
    
    /* Ẩn tuyệt đối các nút bấm nếu lỡ còn sót */
    button, .no-print { 
        display: none !important; 
    }
    
    /* Đảm bảo bảng biểu có viền rõ ràng khi in */
    table, th, td {
        border-color: #000 !important; /* Chuyển màu viền xám sang đen cho rõ */
    }
}