/* ==========================================
   1. CSS VARIABLES & DESIGN TOKENS
   ========================================== */
:root {
    /* Colors - Light Theme (Default) */
    --qhsea-bg: #f8fafc;
    --qhsea-card: #ffffff;
    --qhsea-border: #cbd5e1;
    --qhsea-text-main: #0f172a;
    --qhsea-text-muted: #64748b;
    
    /* Brand Colors */
    --qhsea-primary: #1d4ed8;
    --qhsea-primary-hover: #2563eb;
    --qhsea-primary-active: #1e40af;
    --qhsea-danger: #dc2626;
    --qhsea-danger-hover: #ef4444;
    --qhsea-success: #16a34a;
    --qhsea-info: #0ea5e9;

    /* Geometry & Effects */
    --qhsea-radius-sm: 4px;
    --qhsea-radius-md: 6px;
    --qhsea-radius-lg: 8px;
    --qhsea-transition: 0.2s ease-in-out;
    --qhsea-shadow-sm: 0 1px 3px rgba(0,0,0,0.1);
    --qhsea-shadow-md: 0 4px 6px rgba(0,0,0,0.1);
    --qhsea-shadow-lg: 0 10px 15px -3px rgba(0,0,0,0.1);
}

/* ==========================================
   2. MAIN LAYOUT & WORKSPACE (Full Width Breakout)
   ========================================== */
.qhsea-workspace { 
    width: 96vw;
    max-width: 96vw;
    position: relative;
    left: 50%;
    margin-left: -48vw; /* Perfectly centers the app without trapping fixed children */
    box-sizing: border-box; 
    background: var(--qhsea-bg); 
    padding: 24px; 
    border-radius: var(--qhsea-radius-lg); 
    border: 1px solid var(--qhsea-border);
    font-family: system-ui, -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Helvetica, Arial, sans-serif; 
    color: var(--qhsea-text-main);
    transition: background-color var(--qhsea-transition), border-color var(--qhsea-transition); 
}

/* ==========================================
   3. TYPOGRAPHY & HEADERS (Sticky)
   ========================================== */
.qhsea-header { 
    display: flex; 
    justify-content: space-between; 
    align-items: center;
    border-bottom: 2px solid var(--qhsea-border); 
    padding-bottom: 16px; 
    margin-bottom: 24px; 
    flex-wrap: wrap;
    gap: 12px;
}

.sticky-form-header {
    position: sticky;
    top: 0;
    background: rgba(248, 250, 252, 0.95);
    backdrop-filter: blur(8px);
    padding: 15px 0;
    margin-bottom: 20px;
    border-bottom: 2px solid var(--qhsea-border);
    z-index: 100;
    display: flex;
    justify-content: space-between;
    align-items: center;
    flex-wrap: wrap;
    gap: 15px;
}

.qhsea-header h2, .sticky-form-header h2 { 
    margin: 0; 
    color: var(--qhsea-text-main); 
    font-size: clamp(18px, 4vw, 22px); 
    font-weight: 700;
}

.qhsea-badge { 
    background: #dbeafe; 
    color: var(--qhsea-primary); 
    font-size: 12px; 
    font-weight: 700; 
    padding: 4px 10px; 
    border-radius: var(--qhsea-radius-sm); 
}

.qhsea-disclaimer { 
    font-size: 13px; 
    color: var(--qhsea-text-muted); 
    background: rgba(148, 163, 184, 0.1); 
    padding: 12px; 
    border-radius: var(--qhsea-radius-sm); 
    border-left: 4px solid var(--qhsea-border); 
    margin-bottom: 24px;
    line-height: 1.5;
}

/* ==========================================
   4. NAVIGATION TABS (Replacing old stepper)
   ========================================== */
.qhsea-stepper { 
    display: flex; 
    gap: 10px; 
    margin-bottom: 20px; 
    border-bottom: 2px solid var(--qhsea-border); 
    padding-bottom: 10px; 
    flex-wrap: wrap; 
}

.step-btn { 
    background: transparent !important; 
    border: none !important; 
    font-weight: bold !important; 
    color: #64748b !important; 
    cursor: pointer; 
    padding: 8px 16px !important; 
    border-radius: 4px !important; 
    box-shadow: none !important;
    transition: 0.2s !important; 
}

.step-btn:hover:not(.active) { 
    background: #e2e8f0 !important; 
    transform: none !important;
    box-shadow: none !important;
    color: #0f172a !important;
}

.step-btn.active { 
    background: var(--qhsea-primary) !important; 
    color: #fff !important; 
    box-shadow: inset 0 3px 6px rgba(0,0,0,0.2) !important;
}

/* ==========================================
   5. GRIDS & CARDS
   ========================================== */
.qhsea-grid-2 { display: grid; grid-template-columns: repeat(auto-fit, minmax(280px, 1fr)); gap: 16px; }
.qhsea-grid-4 { display: grid; grid-template-columns: repeat(auto-fit, minmax(180px, 1fr)); gap: 16px; }

.qhsea-card { 
    background: var(--qhsea-card); 
    padding: 20px; 
    border: 1px solid var(--qhsea-border); 
    border-radius: var(--qhsea-radius-md); 
    box-shadow: var(--qhsea-shadow-sm); 
    transition: box-shadow var(--qhsea-transition);
}
.qhsea-card:hover { box-shadow: var(--qhsea-shadow-md); }
.qhsea-card h4 { margin: 0 0 12px 0; color: var(--qhsea-text-main); }

/* ==========================================
   6. FORMS & INPUTS (With Focus & Required indicators)
   ========================================== */
.qhsea-field { display: flex; flex-direction: column; }

.qhsea-field label { 
    font-size: 12px; 
    font-weight: 700; 
    margin-bottom: 6px; 
    color: var(--qhsea-text-muted); 
    text-transform: uppercase;
    letter-spacing: 0.5px;
}

/* Automatically add red asterisks to required fields */
.qhsea-field label:has(+ input:required)::after,
.qhsea-field label:has(+ select:required)::after,
.qhsea-field label:has(+ textarea:required)::after {
    content: " *";
    color: var(--qhsea-danger);
}

.qhsea-field input, 
.qhsea-field select, 
.qhsea-field textarea { 
    width: 100%; 
    padding: 10px; 
    border: 1px solid var(--qhsea-border); 
    border-radius: var(--qhsea-radius-sm); 
    font-size: 14px; 
    box-sizing: border-box;
    background: var(--qhsea-card);
    color: var(--qhsea-text-main);
    transition: border-color var(--qhsea-transition), box-shadow var(--qhsea-transition);
}

/* Beautiful focus rings for inputs */
.qhsea-field input:focus, 
.qhsea-field select:focus, 
.qhsea-field textarea:focus {
    outline: none;
    border-color: var(--qhsea-primary);
    box-shadow: 0 0 0 3px rgba(29, 78, 216, 0.15);
}

/* ==========================================
   7. UNIVERSAL BUTTON UI
   ========================================== */
.qhsea-btn-primary, 
.qhsea-btn-secondary, 
.markup-tool,
.qhsea-btn-danger {
    background: #1d4ed8 !important;
    color: #ffffff !important;
    font-weight: 700 !important;
    border: 1px solid #1e40af !important;
    border-radius: 6px !important;
    padding: 8px 16px;
    box-shadow: 0 4px 6px rgba(0,0,0,0.1) !important;
    transition: all 0.3s ease !important;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 8px;
    text-align: center;
    cursor: pointer;
    text-decoration: none;
    font-size: 14px;
}

.qhsea-btn-primary:hover, 
.qhsea-btn-secondary:hover, 
.markup-tool:hover {
    background: #2563eb !important;
    transform: translateY(-2px);
    box-shadow: 0 6px 12px rgba(29,78,216,0.3) !important;
}

/* Danger Button Override */
.qhsea-btn-danger { 
    background: #dc2626 !important; 
    border-color: #991b1b !important;
}
.qhsea-btn-danger:hover {
    background: #ef4444 !important;
    transform: translateY(-2px);
    box-shadow: 0 6px 12px rgba(220,38,38,0.3) !important;
}

/* Table Buttons Specifics */
.qhsea-table button {
    padding: 6px 12px !important;
    font-size: 12px !important;
    min-width: max-content;
}

/* ==========================================
   8. TABLES (Responsive & Sticky Headers)
   ========================================== */
.qhsea-table-responsive {
    width: 100%;
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
    margin-top: 12px;
}
.qhsea-table { 
    width: 100%; 
    border-collapse: collapse; 
    background: var(--qhsea-card); 
    font-size: 14px; 
    box-shadow: var(--qhsea-shadow-sm);
    min-width: 600px; 
}
.qhsea-table th { 
    background: var(--qhsea-primary); 
    color: #ffffff; 
    padding: 12px; 
    text-align: left; 
    border-bottom: 2px solid var(--qhsea-border); 
    position: sticky;
    top: 0;
    z-index: 10;
    white-space: nowrap;
}
.qhsea-table td { 
    padding: 12px; 
    border-bottom: 1px solid var(--qhsea-border); 
    vertical-align: top;
    word-break: break-word;
}

/* ==========================================
   9. MODALS, DIALOGS & TOASTS
   ========================================== */
.qhsea-dialog-overlay { 
    position: fixed; 
    inset: 0; 
    background: rgba(15, 23, 42, 0.75); 
    backdrop-filter: blur(4px); 
    display: flex; 
    align-items: center; 
    justify-content: center; 
    z-index: 999999; 
    opacity: 0; 
    visibility: hidden; 
    transition: opacity var(--qhsea-transition); 
}
.qhsea-dialog-overlay.active { opacity: 1; visibility: visible; }

.qhsea-dialog-box { 
    background: var(--qhsea-card); 
    border-radius: var(--qhsea-radius-lg); 
    width: 100%; 
    max-width: 420px; 
    padding: 24px; 
    box-shadow: var(--qhsea-shadow-lg); 
    transform: translateY(20px);
    transition: transform var(--qhsea-transition);
}
.qhsea-dialog-overlay.active .qhsea-dialog-box { transform: translateY(0); }

.qhsea-dialog-title { 
    font-size: 18px; 
    font-weight: 700; 
    color: var(--qhsea-text-main); 
    margin: 0 0 16px 0; 
}

/* Toasts */
#dqdc-toast-container { 
    position: fixed; 
    bottom: 24px; 
    right: 24px; 
    z-index: 9999999; 
    display: flex; 
    flex-direction: column; 
    gap: 12px; 
    pointer-events: none; 
}
.dqdc-toast { 
    background: var(--qhsea-card); 
    padding: 12px 20px; 
    border-radius: var(--qhsea-radius-md); 
    color: var(--qhsea-text-main); 
    font-size: 14px; 
    font-weight: 600; 
    box-shadow: var(--qhsea-shadow-lg); 
    display: flex; 
    align-items: center; 
    gap: 12px; 
    transform: translateX(120%); 
    opacity: 0; 
    transition: all 0.4s cubic-bezier(0.16, 1, 0.3, 1); 
    border-left: 4px solid var(--qhsea-border); 
    pointer-events: auto; 
}
.dqdc-toast.show { transform: translateX(0); opacity: 1; }
.dqdc-toast-success { border-left-color: var(--qhsea-success); }
.dqdc-toast-error { border-left-color: var(--qhsea-danger); }
.dqdc-toast-info { border-left-color: var(--qhsea-info); }

/* ==========================================
   10. MODULE EXTENSIONS (Analytics, Lightbox, Single Page)
   ========================================== */
#tpi-manual-form { gap: 16px; }
#tpi-manual-form .qhsea-field { 
    background: var(--qhsea-card); 
    padding: 12px; 
    border-radius: var(--qhsea-radius-sm); 
    box-shadow: var(--qhsea-shadow-sm); 
}
#logs-table-body td { font-family: monospace, monospace; border-bottom: 1px dashed var(--qhsea-border); }

/* Analytics */
.qhsea-kpi-container { display: flex; gap: 20px; margin-bottom: 24px; flex-wrap: wrap; }
.qhsea-kpi-box { 
    flex: 1; 
    min-width: 160px; 
    background: var(--qhsea-card); 
    padding: 24px; 
    border-radius: var(--qhsea-radius-md); 
    box-shadow: var(--qhsea-shadow-sm); 
    border-left: 4px solid var(--qhsea-primary); 
}
.qhsea-kpi-box h3 { margin: 0; color: var(--qhsea-text-muted); font-size: 13px; text-transform: uppercase; }
.qhsea-kpi-box p { font-size: 32px; font-weight: 800; margin: 12px 0 0 0; color: var(--qhsea-text-main); }
.qhsea-chart-wrapper { position: relative; height: 100%; min-height: 250px; max-height: 350px; width: 100%; display: flex; align-items: center; justify-content: center; }

/* Lightbox & Markup */
.markup-tool.active { background: var(--qhsea-primary-active) !important; border-color: var(--qhsea-primary-active) !important; }
#markup-canvas { touch-action: none; max-width: 100%; height: auto; display: block; }
.lb-nav-btn { 
    color: #ffffff; 
    font-size: 40px; 
    cursor: pointer; 
    user-select: none; 
    background: rgba(0,0,0,0.5); 
    border-radius: 50%; 
    width: 50px; 
    height: 50px; 
    display: flex; 
    align-items: center; 
    justify-content: center; 
    transition: background 0.2s, transform 0.2s; 
}
.lb-nav-btn:hover { background: rgba(29, 78, 216, 0.9); transform: scale(1.05); }
#lb-close:hover { color: var(--qhsea-danger); transform: scale(1.1); }

/* Single Page Mode */
.single-page-mode .qhsea-stepper { display: none; }
.single-page-mode .qhsea-step-content { 
    display: block !important; 
    margin-bottom: 48px; 
    padding-bottom: 24px; 
    border-bottom: 2px dashed var(--qhsea-border); 
}

/* ==========================================
   11. RTL SUPPORT (Arabic Translation)
   ========================================== */
[dir="rtl"] .qhsea-workspace { font-family: 'Tajawal', 'Cairo', system-ui, sans-serif; text-align: right; direction: rtl; }
[dir="rtl"] .qhsea-header, [dir="rtl"] .sticky-form-header { flex-direction: row-reverse; }
[dir="rtl"] .qhsea-table th { text-align: right; }
[dir="rtl"] .qhsea-disclaimer { border-left: none; border-right: 4px solid var(--qhsea-border); }
[dir="rtl"] .qhsea-kpi-box { border-left: none; border-right: 4px solid var(--qhsea-primary); }
[dir="rtl"] .dqdc-toast { transform: translateX(-120%); border-left: none; border-right: 4px solid var(--qhsea-border); }
[dir="rtl"] .dqdc-toast-success { border-right-color: var(--qhsea-success); }
[dir="rtl"] .dqdc-toast-error { border-right-color: var(--qhsea-danger); }
[dir="rtl"] .dqdc-toast-info { border-right-color: var(--qhsea-info); }
[dir="rtl"] #lb-close { right: auto !important; left: 35px !important; }
[dir="rtl"] .qhsea-drawer-close { float: left; }
/* ==========================================
   12. DARK MODE DYNAMIC TOKENS
   ========================================== */
[data-theme="dark"] {
    --qhsea-bg: #0f172a;
    --qhsea-card: #1e293b;
    --qhsea-border: #334155;
    --qhsea-text-main: #f8fafc;
    --qhsea-text-muted: #94a3b8;
}

[data-theme="dark"] .qhsea-table th { background: #334155; }
[data-theme="dark"] .qhsea-badge { background: #1e3a8a; color: #bfdbfe; }
[data-theme="dark"] .qhsea-disclaimer { background: rgba(51, 65, 85, 0.4); }
[data-theme="dark"] .sticky-form-header { background: rgba(15, 23, 42, 0.95); }

/* Select2 Dark Mode Support */
[data-theme="dark"] .select2-container--default .select2-selection--single,
[data-theme="dark"] .select2-dropdown {
    background-color: var(--qhsea-card);
    border-color: var(--qhsea-border);
}
[data-theme="dark"] .select2-container--default .select2-selection--single .select2-selection__rendered,
[data-theme="dark"] .select2-results__option { color: var(--qhsea-text-main); }
[data-theme="dark"] .select2-search__field { 
    background-color: var(--qhsea-bg); 
    color: var(--qhsea-text-main); 
    border-color: var(--qhsea-border); 
}

/* TPI Expiry Flash Warning */
@keyframes flashWarning {
    0% { background-color: #fef08a; color: #854d0e; }
    50% { background-color: #ea580c; color: #ffffff; box-shadow: 0 0 8px rgba(234, 88, 12, 0.6); }
    100% { background-color: #fef08a; color: #854d0e; }
}
.flash-orange-badge {
    animation: flashWarning 1.5s infinite;
    padding: 3px 8px;
    border-radius: 4px;
    font-weight: bold;
    font-size: 11px;
    display: inline-block;
    border: 1px solid #ea580c;
}

/* ==========================================
   13. SLIDE-OUT DRAWERS (Forms)
   ========================================== */
.qhsea-drawer {
    position: fixed !important;
    top: 0;
    right: -100%;
    width: 100%;
    max-width: 500px;
    height: 100vh;
    background: var(--qhsea-card) !important;
    box-shadow: -5px 0 25px rgba(0,0,0,0.3) !important;
    z-index: 999999;
    padding: 24px;
    padding-bottom: 80px !important; /* Prevents buttons from getting cut off at the bottom */
    overflow-y: auto;
    transition: right 0.3s cubic-bezier(0.4, 0, 0.2, 1);
    border-left: 4px solid var(--qhsea-primary) !important;
    display: block !important; /* Overrides inline none */
}
.qhsea-drawer.active {
    right: 0;
}
.qhsea-drawer-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 20px;
    border-bottom: 1px solid var(--qhsea-border);
    padding-bottom: 10px;
}
.qhsea-drawer-close {
    font-size: 28px;
    cursor: pointer;
    color: var(--qhsea-danger);
    line-height: 1;
}

/* ==========================================
   14. QUICK ACTION DASHBOARD & QR SCANNER
   ========================================== */
.qhsea-quick-dash {
    background: #fffbeb;
    border-left: 4px solid #f59e0b;
    padding: 12px 20px;
    border-radius: 6px;
    margin-bottom: 20px;
    display: flex;
    justify-content: space-between;
    align-items: center;
    box-shadow: 0 1px 3px rgba(0,0,0,0.1);
}
#qr-reader {
    width: 100%;
    max-width: 400px;
    margin: 0 auto;
}


/* Custom DataTables Wrapper Styling */
.qhsea-table-controls {
    display: flex;
    justify-content: flex-end;
    padding-bottom: 12px;
}

.qhsea-table-controls .dataTables_filter input {
    width: 250px;
    padding: 8px 12px;
    border: 1px solid var(--qhsea-border);
    border-radius: var(--qhsea-radius-sm);
    font-size: 14px;
    outline: none;
    transition: border-color var(--qhsea-transition), box-shadow var(--qhsea-transition);
}

.qhsea-table-controls .dataTables_filter input:focus {
    border-color: var(--qhsea-primary);
    box-shadow: 0 0 0 3px rgba(29, 78, 216, 0.15);
}

/* Push the Length Menu and Pagination to the bottom */
.qhsea-table-footer {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding-top: 15px;
    border-top: 1px solid var(--qhsea-border);
    font-size: 13px;
    color: var(--qhsea-text-muted);
}

.qhsea-table-footer select {
    padding: 4px;
    border-radius: 4px;
    border: 1px solid var(--qhsea-border);
}

/* ==========================================
   15. SELECT2 PROFESSIONAL UI OVERRIDES
   ========================================== */
.select2-container .select2-selection--single {
    height: 44px !important; /* Taller, more clickable */
    border: 1px solid var(--qhsea-border) !important;
    border-radius: var(--qhsea-radius-md) !important;
    background: var(--qhsea-card) !important;
    display: flex;
    align-items: center;
    box-shadow: inset 0 1px 2px rgba(0,0,0,0.02);
    transition: all var(--qhsea-transition) !important;
}
.select2-container--open .select2-selection--single {
    border-color: var(--qhsea-primary) !important;
    box-shadow: 0 0 0 3px rgba(29, 78, 216, 0.15), inset 0 1px 2px rgba(0,0,0,0.02) !important;
}
.select2-container--default .select2-selection--single .select2-selection__arrow {
    height: 42px !important;
    right: 8px !important;
}
.select2-container--default .select2-selection--single .select2-selection__rendered {
    color: var(--qhsea-text-main) !important;
    line-height: normal !important;
    padding-left: 14px !important;
    font-weight: 500;
}
.select2-dropdown {
    border: none !important;
    box-shadow: 0 10px 25px -5px rgba(0,0,0,0.15), 0 8px 10px -6px rgba(0,0,0,0.1) !important;
    border-radius: var(--qhsea-radius-md) !important;
    overflow: hidden;
    margin-top: 4px;
}
.select2-container--default .select2-search--dropdown .select2-search__field {
    border: 1px solid var(--qhsea-border);
    border-radius: var(--qhsea-radius-sm);
    padding: 8px 12px;
}
.select2-results__option {
    padding: 10px 14px !important;
    font-size: 14px;
    transition: background-color 0.15s ease;
}
.select2-container--default .select2-results__option--highlighted[aria-selected] {
    background-color: #eff6ff !important;
    color: var(--qhsea-primary) !important;
    font-weight: 600;
}
.select2-container--default .select2-results__option[aria-selected=true] {
    background-color: var(--qhsea-primary) !important;
    color: white !important;
}

/* Ensure Select2 dropdowns always display on top of modals and drawers */
.select2-container--open {
    z-index: 10000000 !important;
}
.select2-dropdown {
    z-index: 10000001 !important;
}

/* ==========================================
   16. RUGGED FIELD TABLET MODE
   ========================================== */
.field-tablet-mode { font-size: 16px !important; }
.field-tablet-mode input, 
.field-tablet-mode select, 
.field-tablet-mode textarea {
    padding: 16px !important;
    font-size: 16px !important;
    border-width: 2px !important;
}
.field-tablet-mode .qhsea-btn-primary, 
.field-tablet-mode .qhsea-btn-secondary, 
.field-tablet-mode .qhsea-btn-danger, 
.field-tablet-mode .step-btn {
    padding: 14px 20px !important;
    font-size: 16px !important;
    border-width: 2px !important;
}
.field-tablet-mode .qhsea-table th, 
.field-tablet-mode .qhsea-table td {
    padding: 16px !important;
    font-size: 15px !important;
}
.field-tablet-mode .qhsea-table button {
    padding: 10px 14px !important;
    font-size: 14px !important;
}
.field-tablet-mode .select2-container .select2-selection--single {
    height: 54px !important;
    border-width: 2px !important;
}
.field-tablet-mode .select2-container--default .select2-selection--single .select2-selection__rendered {
    line-height: 50px !important;
    font-size: 16px !important;
}