/* --- 1. FIX KHUNG BAO NGOÀI (AN TOÀN TUYỆT ĐỐI) --- */
#mtl-wrapper-isolation {
    display: block !important;
    width: 100% !important;          /* Chiếm 100% khung chứa của Theme */
    max-width: 100% !important;
    margin: 0 auto !important;       /* Căn giữa chuẩn */
    padding: 20px 0 !important;
    background: transparent;
    clear: both;
    /* Cơ chế bảo vệ: Nếu Theme quá hẹp, hiện thanh cuộn thay vì làm vỡ giao diện */
    overflow-x: auto !important;     
}

/* --- 2. GIAO DIỆN CHÍNH --- */
#mtl-app {
    font-family: 'Segoe UI', Roboto, Helvetica, Arial, sans-serif;
    
    /* Chiếm hết không gian cho phép */
    width: 100% !important; 
    min-width: 900px;                /* Đặt chiều rộng tối thiểu để giữ form đẹp */
    
    margin: 0 auto;
    background: #ffffff;
    border: 1px solid #e2e8f0;
    border-radius: 16px;
    padding: 35px 40px;              /* Padding vừa phải để tiết kiệm diện tích */
    box-shadow: 0 15px 35px -5px rgba(0,0,0,0.08);
    color: #334155;
    box-sizing: border-box;
}

#mtl-app * { box-sizing: border-box; }

/* --- 3. HEADER --- */
.mtl-header {
    display: flex; flex-wrap: wrap; justify-content: space-between; align-items: center;
    border-bottom: 2px solid #f1f5f9; padding-bottom: 20px; margin-bottom: 30px;
}
.mtl-header h3 { margin: 0; font-size: 2rem; font-weight: 800; color: #0f172a; }
.sub-title { font-size: 1rem; color: #64748b; margin-top: 5px; font-weight: 500; }
.version-tag { background: #e0f2fe; color: #0284c7; font-size: 0.8rem; padding: 4px 10px; border-radius: 20px; font-weight: 700; vertical-align: middle; }

.header-controls { display: flex; align-items: center; gap: 20px; }
.switch-label { display: flex; align-items: center; cursor: pointer; font-size: 15px; font-weight: 600; color: #475569; }
.switch-label input { display: none; }
.slider { position: relative; display: inline-block; width: 48px; height: 26px; background-color: #cbd5e1; border-radius: 26px; transition: .3s; margin-right: 10px; }
.slider:before { position: absolute; content: ""; height: 20px; width: 20px; left: 3px; bottom: 3px; background-color: white; border-radius: 50%; transition: .3s; box-shadow: 0 2px 4px rgba(0,0,0,0.1); }
input:checked + .slider { background-color: #3b82f6; }
input:checked + .slider:before { transform: translateX(22px); }

#mtl-clean-mode { 
    background: #fee2e2; color: #dc2626; border: 1px solid #fecaca; padding: 10px 20px; 
    border-radius: 10px; font-weight: 700; cursor: pointer; font-size: 14px; transition: 0.2s;
}
#mtl-clean-mode:hover { background: #fecaca; }

/* --- 4. WORKSPACE (GRID SYSTEM - CÂN BẰNG) --- */
.mtl-workspace { 
    display: grid; 
    /* Chia 3 cột chuẩn: Ma trận A - Nút - Ma trận B */
    grid-template-columns: 1fr 150px 1fr; 
    gap: 30px; 
    align-items: start; 
    margin-bottom: 40px; 
    width: 100%; 
}

.mtl-box {
    background: #fff; border: 1px solid #e2e8f0;
    border-radius: 14px; padding: 25px;
    box-shadow: 0 4px 12px -2px rgba(0, 0, 0, 0.03);
    display: flex; flex-direction: column;
    height: 100%;
    width: 100%; /* Đảm bảo lấp đầy cột Grid */
}

.box-head { display: flex; justify-content: space-between; align-items: center; margin-bottom: 20px; border-bottom: 2px dashed #f1f5f9; padding-bottom: 15px; }
.box-title { font-weight: 800; font-size: 1.3rem; text-transform: uppercase; color: #334155; }
.badge-a { color: #2563eb; } .badge-b { color: #db2777; }

/* Controls */
.dims { display: flex; align-items: center; background: #f8fafc; border-radius: 8px; padding: 5px 10px; border: 1px solid #e2e8f0; }
.dims button { width: 32px; height: 32px; border: none; background: transparent; color: #64748b; font-weight: 800; cursor: pointer; font-size: 18px; transition: 0.2s; }
.dims button:hover { background: #fff; color: #2563eb; box-shadow: 0 2px 4px rgba(0,0,0,0.05); border-radius: 6px; }
.x-sign { font-size: 12px; color: #cbd5e1; margin: 0 8px; }
.dims span { font-weight: 700; min-width: 20px; text-align: center; font-size: 16px; color: #0f172a; }

/* LƯỚI NHẬP LIỆU (INPUT GRID) */
.grid-container {
    display: grid; gap: 10px; background: #f8fafc; padding: 20px; 
    border-radius: 12px; border: 1px solid #e2e8f0; margin-bottom: 20px; 
    width: 100%; 
    overflow-x: auto; 
}
.matrix-cell {
    width: 100% !important; 
    min-width: 60px;         /* Giữ kích thước vừa vặn */
    height: 55px !important;
    text-align: center !important;
    border: 2px solid #cbd5e1 !important; 
    border-radius: 8px; 
    font-size: 20px;         
    font-family: 'Times New Roman', serif; font-weight: 600;
    color: #1e293b; background: #fff; transition: 0.15s;
}
.matrix-cell:focus { border-color: #3b82f6 !important; box-shadow: 0 0 0 4px rgba(59, 130, 246, 0.15); outline: none; }

/* Tools Buttons */
.tools-bar { display: flex; gap: 10px; margin-bottom: 20px; flex-wrap: wrap; }
.btn-tool { 
    font-size: 13px; background: #fff; color: #475569; 
    border: 1px solid #cbd5e1; padding: 10px 16px; border-radius: 8px; 
    cursor: pointer; font-weight: 600; transition: 0.2s; display: flex; align-items: center; gap: 6px;
}
.btn-tool:hover { background: #f1f5f9; border-color: #94a3b8; color: #0f172a; }

/* Action Buttons */
.actions-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }
.actions-grid button {
    padding: 14px; background: #fff; border: 1px solid #e2e8f0;
    color: #475569; border-radius: 8px; font-size: 14px; cursor: pointer; font-weight: 700;
    transition: all 0.15s;
}
.actions-grid button:hover { border-color: #3b82f6; color: #2563eb; background: #eff6ff; transform: translateY(-2px); box-shadow: 0 4px 10px rgba(37, 99, 235, 0.1); }

/* --- 5. CỘT GIỮA (OPERATORS) --- */
.mtl-ops { 
    display: flex; flex-direction: column; gap: 20px; justify-content: center; 
    padding-top: 80px; 
}
.ops-basic { display: flex; flex-direction: column; gap: 12px; width: 100%; }
.btn-op {
    padding: 18px; background: #fff; border: 1px solid #cbd5e1; border-radius: 12px;
    font-weight: 800; cursor: pointer; transition: 0.2s; color: #334155; font-size: 18px;
    box-shadow: 0 4px 0 #e2e8f0; text-align: center;
}
.btn-op.highlight { background: #2563eb; color: white; border: none; box-shadow: 0 5px 0 #1d4ed8; }
.btn-op.highlight:hover { background: #1d4ed8; transform: translateY(-2px); }
.btn-op:active { transform: translateY(3px); box-shadow: none !important; }

.btn-util { background: #64748b; color: white; border: none; padding: 12px; border-radius: 8px; font-size: 14px; cursor: pointer; font-weight: 600; }

.solve-section { border-top: 2px dashed #e2e8f0; padding-top: 20px; text-align: center; margin-top: 10px; }
.solve-label { font-size: 11px; font-weight: 800; color: #94a3b8; text-transform: uppercase; margin-bottom: 10px; letter-spacing: 1px; }
.solve-btns { display: flex; flex-direction: column; gap: 8px; }
.solve-btns button { 
    background: #f0f9ff; color: #0284c7; border: 1px solid #bae6fd; 
    padding: 10px; border-radius: 8px; font-size: 14px; cursor: pointer; font-weight: 700; 
}
.solve-btns button:hover { background: #e0f2fe; border-color: #7dd3fc; }

/* --- 6. KẾT QUẢ --- */
#mtl-result-area {
    display: none; margin-top: 50px; background: #fff;
    border-radius: 16px; border: 1px solid #e2e8f0; overflow: hidden;
    box-shadow: 0 20px 40px -10px rgba(0, 0, 0, 0.1);
    width: 100%; 
}
.res-head { background: #1e293b; padding: 20px 30px; display: flex; justify-content: space-between; align-items: center; }
.res-head h4 { margin: 0; color: #fff; font-size: 1.4rem; font-weight: 700; }
.res-actions button { margin-left: 10px; padding: 8px 16px; border-radius: 6px; border: 1px solid rgba(255,255,255,0.2); background: rgba(255,255,255,0.1); color: white; cursor: pointer; font-size: 13px; font-weight: 600; }
.res-actions button:hover { background: rgba(255,255,255,0.2); }
.btn-close-res { background: #ef4444 !important; border-color: #ef4444 !important; }

#mtl-result-content { 
    padding: 50px; text-align: center; background-color: #f8fafc;
    width: 100%; overflow-x: auto; word-wrap: break-word;
}

.math-matrix { 
    display: inline-block; position: relative; margin: 20px auto; vertical-align: middle; 
    background: #fff; padding: 15px; border-radius: 12px; 
    box-shadow: 0 5px 15px rgba(0,0,0,0.05);
    max-width: 100%;
}
.math-matrix table { border-collapse: collapse; margin: 0 15px; }
.math-matrix td { 
    padding: 12px 25px; 
    font-family: 'Times New Roman', serif; font-size: 2rem; 
    color: #1e293b; text-align: center; white-space: nowrap; font-weight: 500; 
}
.math-matrix td.long-val { white-space: normal; word-break: break-all; min-width: 120px; }

.math-matrix:before, .math-matrix:after { content: ""; position: absolute; top: 0; bottom: 0; width: 12px; border: 3px solid #334155; }
.math-matrix:before { left: 0; border-right: none; border-radius: 12px 0 0 12px; }
.math-matrix:after { right: 0; border-left: none; border-radius: 0 12px 12px 0; }

/* Steps */
#mtl-steps-container { background: #fff; border-top: 1px solid #e2e8f0; }
.steps-head-bar { padding: 20px 30px; font-weight: 800; color: #334155; font-size: 18px; background: #f1f5f9; border-bottom: 1px solid #e2e8f0; }
#mtl-steps-log { padding: 30px; overflow-x: auto; }
.method-block { margin-bottom: 20px; border: 1px solid #e2e8f0; border-radius: 10px; overflow: hidden; background: #fff; }
.method-header { padding: 16px 25px; background: #fff; cursor: pointer; font-weight: 700; color: #334155; display: flex; justify-content: space-between; align-items: center; font-size: 16px; }
.method-header:hover { background: #f8fafc; color: #2563eb; }
.method-content { padding: 25px; display: none; overflow-x: auto; }

/* --- 7. IMPORT MODAL --- */
.import-wrapper { position: fixed; top: 0; left: 0; width: 100%; height: 100%; z-index: 999999; display: flex; justify-content: center; align-items: center; }
.import-overlay { position: absolute; width: 100%; height: 100%; background: rgba(15, 23, 42, 0.8); backdrop-filter: blur(4px); z-index: 1; }
.import-area { position: relative; z-index: 2; background: #fff; width: 90%; max-width: 600px; padding: 35px; border-radius: 20px; box-shadow: 0 25px 50px -12px rgba(0, 0, 0, 0.25); }
.import-area h5 { margin: 0 0 20px 0; font-size: 20px; font-weight: 800; color: #0f172a; }
.import-area textarea { width: 100%; height: 200px; border: 2px solid #cbd5e1; border-radius: 12px; padding: 15px; font-family: monospace; margin-bottom: 25px; font-size: 16px; }
.imp-btns { display: flex; justify-content: flex-end; gap: 15px; }
.btn-primary { background: #2563eb; color: white; border: none; padding: 12px 24px; border-radius: 8px; cursor: pointer; font-weight: 700; font-size: 15px; }

/* --- 8. HISTORY --- */
#mtl-history-section { margin-top: 60px; border-top: 1px solid #e2e8f0; padding-top: 40px; }
.hist-head { display: flex; justify-content: space-between; margin-bottom: 20px; }
.hist-item { display: flex; justify-content: space-between; background: #fff; border: 1px solid #e2e8f0; padding: 20px; margin-bottom: 15px; border-radius: 10px; border-left: 6px solid #94a3b8; }

/* --- 9. RESPONSIVE & MOBILE UX OPTIMIZATION --- */

/* Scrollbar đẹp hơn cho các vùng cuộn ngang trên mobile */
.grid-container::-webkit-scrollbar,
#mtl-result-content::-webkit-scrollbar,
.method-content::-webkit-scrollbar {
    height: 6px;
}
.grid-container::-webkit-scrollbar-thumb,
#mtl-result-content::-webkit-scrollbar-thumb,
.method-content::-webkit-scrollbar-thumb {
    background: #cbd5e1;
    border-radius: 4px;
}

@media only screen and (max-width: 1024px) {
    /* Tablet & Small Desktop */
    #mtl-app { min-width: auto; padding: 20px; border-radius: 12px; }
    .mtl-workspace { grid-template-columns: 1fr; gap: 30px; } /* Chuyển sang xếp dọc */
    .mtl-ops { padding: 20px; background: #f8fafc; border: 1px solid #e2e8f0; border-radius: 12px; flex-direction: row; justify-content: space-between; align-items: center; flex-wrap: wrap; }
    .ops-basic { flex-direction: row; flex: 1; width: auto; }
    .solve-section { border-top: none; border-left: 2px solid #e2e8f0; padding-top: 0; padding-left: 20px; margin-top: 0; text-align: left; }
    .solve-btns { flex-direction: row; }
}

@media only screen and (max-width: 768px) {
    /* --- MOBILE CHUYÊN BIỆT --- */

    /* Container chính: Tận dụng tối đa chiều rộng màn hình */
    #mtl-app {
        padding: 15px 10px !important; /* Giảm padding lề */
        border: none;
        box-shadow: none;
        border-radius: 0;
        width: 100% !important;
    }

    /* Header gọn hơn */
    .mtl-header {
        flex-direction: column;
        align-items: flex-start;
        gap: 15px;
    }
    .mtl-header h3 { font-size: 1.5rem; }
    .header-controls { width: 100%; justify-content: space-between; }

    /* --- TỐI ƯU GRID NHẬP LIỆU --- */
    .mtl-box { padding: 15px; }

    /* Grid container: Cho phép cuộn ngang mượt mà + Gợi ý cuộn (Shadow) */
    .grid-container {
        display: grid;
        gap: 5px; 
        padding: 10px;
        margin: 0 -10px 15px -10px; 
        width: calc(100% + 20px);
        overflow-x: auto;
        -webkit-overflow-scrolling: touch;
        
        /* Visual Cue: Tạo bóng mờ 2 bên */
        background: 
            linear-gradient(to right, white 30%, rgba(255,255,255,0)),
            linear-gradient(to right, rgba(255,255,255,0), white 70%) 0 100%,
            radial-gradient(farthest-side at 0 50%, rgba(0,0,0,0.15), rgba(0,0,0,0)),
            radial-gradient(farthest-side at 100% 50%, rgba(0,0,0,0.15), rgba(0,0,0,0)) 0 100%;
        background-repeat: no-repeat;
        background-color: #f8fafc;
        background-size: 40px 100%, 40px 100%, 14px 100%, 14px 100%;
        background-position: 0 0, 100% 0, 0 0, 100% 0;
        background-attachment: local, local, scroll, scroll;
    }

    /* Ô Input: Tinh chỉnh kích thước chạm & Font chữ */
    .matrix-cell {
        min-width: 48px; 
        height: 48px !important;
        font-size: 16px; /* BẮT BUỘC: 16px để iOS không auto-zoom */
        padding: 0;
        border-radius: 6px;
    }

    /* Nút bấm điều khiển */
    .dims button { width: 40px; height: 40px; background: #eff6ff; color: #2563eb; }

    .actions-grid { grid-template-columns: 1fr 1fr; gap: 8px; }
    .actions-grid button, .btn-tool {
        padding: 12px 5px; 
        font-size: 13px;
        white-space: nowrap; 
        overflow: hidden;
        text-overflow: ellipsis;
    }

    /* --- CỘT GIỮA (OPERATORS) --- */
    .mtl-ops { padding: 15px 0; background: transparent; border: none; gap: 15px; }
    .ops-basic {
        display: grid;
        grid-template-columns: 1fr 1fr 1fr; /* Xếp 3 nút tính toán hàng ngang */
        gap: 8px;
        width: 100%;
        flex-direction: row;
    }
    .btn-op { padding: 15px 5px; font-size: 16px; width: 100%; }
    
    .solve-section { 
        border-left: none; border-top: 2px solid #e2e8f0; 
        padding-left: 0; padding-top: 15px; width: 100%; text-align: center; 
    }
    .solve-btns { flex-direction: column; }

    /* --- KẾT QUẢ --- */
    #mtl-result-content { padding: 20px 10px; }
    
    /* Scale nhỏ lại một chút nếu quá to */
    .math-matrix td { padding: 8px 12px; font-size: 1.2rem; }
    
    /* Steps log */
    .step-highlight { overflow-x: auto; }
}