/* APK Dark Mode Final Clean 2027
   Hard-loaded last. Purpose: fix dark-mode white cards, invisible text, stuck submenu cards, and unclickable buttons caused by legacy CSS. */
:root{
  --apk-final-radius: 18px;
}
html[data-apk-theme="dark"]{
  --apk-d-bg:#07111f;
  --apk-d-bg2:#0a1628;
  --apk-d-sidebar:#0b1729;
  --apk-d-surface:#101d31;
  --apk-d-surface2:#14243c;
  --apk-d-surface3:#1a2f4f;
  --apk-d-input:#0c1a2d;
  --apk-d-border:rgba(148,178,220,.30);
  --apk-d-border2:rgba(148,178,220,.45);
  --apk-d-text:#f7fbff;
  --apk-d-heading:#ffffff;
  --apk-d-muted:#c3d2e8;
  --apk-d-muted2:#9eb1cc;
  --apk-d-blue:#60a5fa;
  --apk-d-purple:#a78bfa;
  --apk-d-pink:#f472b6;
  --apk-d-green:#34d399;
  --apk-d-red:#fb7185;
  --apk-d-shadow:0 24px 70px rgba(0,0,0,.45);
}
html[data-apk-theme="dark"],
html[data-apk-theme="dark"] body,
html[data-apk-theme="dark"] #app,
html[data-apk-theme="dark"] #main,
html[data-apk-theme="dark"] .main-content,
html[data-apk-theme="dark"] .page-content,
html[data-apk-theme="dark"] .content-wrapper{
  background:
    radial-gradient(circle at 78% 0%,rgba(244,114,182,.13),transparent 30%),
    radial-gradient(circle at 0% 20%,rgba(96,165,250,.12),transparent 28%),
    linear-gradient(135deg,var(--apk-d-bg),var(--apk-d-bg2))!important;
  color:var(--apk-d-text)!important;
}

/* Global dark text safety */
html[data-apk-theme="dark"] #main :where(h1,h2,h3,h4,h5,h6,.page-heading h3,.page-title,.card-title,.modal-title){
  color:var(--apk-d-heading)!important;
  -webkit-text-fill-color:var(--apk-d-heading)!important;
}
html[data-apk-theme="dark"] #main :where(p,div,span,label,small,b,strong,li,td,th,a,legend,.text-muted,.dataTables_info){
  color:var(--apk-d-text)!important;
}
html[data-apk-theme="dark"] #main :where(.text-muted,small,.muted,.help-text,.form-text,.dataTables_info,.dataTables_length label,.dataTables_filter label){
  color:var(--apk-d-muted)!important;
  -webkit-text-fill-color:var(--apk-d-muted)!important;
}

/* Important: legacy fresh-2026 made every .form-group a white card. Override it globally in dark mode. */
html[data-apk-theme="dark"] #main :where(.form-group,.card,.card-body,.content-card,.data-card,.section-card,.table-card,.form-card,.apk-card,.apk-panel,.apk-page-hero,.apk-table-shell,.dataTables_wrapper,.table-responsive,.modal-content,.modal-header,.modal-body,.modal-footer,.apk-entry-modal__dialog,.apk-entry-dialog,.apk-entry-card,.project-flow-page .pf-card,.project-flow-page .pf-status-card,.project-flow-page .pf-kv,.project-flow-page .pf-guide-item,.alert,.dropdown-menu,.select2-dropdown,.Zebra_DatePicker,.datepicker){
  background:linear-gradient(180deg,rgba(18,32,54,.98),rgba(13,25,44,.98))!important;
  color:var(--apk-d-text)!important;
  -webkit-text-fill-color:initial!important;
  border-color:var(--apk-d-border)!important;
  box-shadow:var(--apk-d-shadow)!important;
}
html[data-apk-theme="dark"] #main :where(.form-group *,.card *,.card-body *,.content-card *,.data-card *,.section-card *,.table-card *,.form-card *,.apk-card *,.apk-panel *,.apk-page-hero *,.apk-table-shell *,.modal-content *,.apk-entry-dialog *,.project-flow-page .pf-card *,.project-flow-page .pf-status-card *,.alert *){
  color:var(--apk-d-text)!important;
  -webkit-text-fill-color:currentColor!important;
}
html[data-apk-theme="dark"] #main :where(.form-group label,.form-label,.control-label,label){
  color:#eaf2ff!important;
  -webkit-text-fill-color:#eaf2ff!important;
  opacity:1!important;
}

/* Legacy direct forms */
html[data-apk-theme="dark"] #main > form,
html[data-apk-theme="dark"] #main form[action*="brain/init.php"],
html[data-apk-theme="dark"] #main form[action*="../brain/init.php"],
html[data-apk-theme="dark"] #main form[action*="/brain/init.php"],
html[data-apk-theme="dark"] #main form[action*="formula/"],
html[data-apk-theme="dark"] #main form[action*="docgen/"]{
  background:linear-gradient(180deg,rgba(14,25,44,.98),rgba(9,18,32,.98))!important;
  color:var(--apk-d-text)!important;
  border:1px solid var(--apk-d-border)!important;
  box-shadow:0 22px 60px rgba(0,0,0,.34)!important;
}
html[data-apk-theme="dark"] #main > form > .form-group,
html[data-apk-theme="dark"] #main form[action*="brain/init.php"] > .form-group,
html[data-apk-theme="dark"] #main form[action*="../brain/init.php"] > .form-group,
html[data-apk-theme="dark"] #main form[action*="/brain/init.php"] > .form-group{
  background:rgba(16,31,53,.82)!important;
  border:1px solid rgba(148,178,220,.22)!important;
  border-radius:18px!important;
  box-shadow:none!important;
  padding:14px 16px!important;
}

/* Inputs/selects: never white-on-white or dark-on-dark */
html[data-apk-theme="dark"] #main :where(input,textarea,select,.form-control,.form-select,.custom-select,.select2-container--default .select2-selection--single,.select2-container--default .select2-selection--multiple,.select2-search__field){
  background:var(--apk-d-input)!important;
  color:#f8fbff!important;
  -webkit-text-fill-color:#f8fbff!important;
  border:1.5px solid var(--apk-d-border2)!important;
  box-shadow:none!important;
  caret-color:#ffffff!important;
}
html[data-apk-theme="dark"] #main :where(input[readonly],textarea[readonly],.form-control[readonly]){
  background:#0a1728!important;
  color:#dbeafe!important;
  -webkit-text-fill-color:#dbeafe!important;
}
html[data-apk-theme="dark"] #main :where(input::placeholder,textarea::placeholder,.select2-selection__placeholder){
  color:var(--apk-d-muted2)!important;
  -webkit-text-fill-color:var(--apk-d-muted2)!important;
  opacity:1!important;
}
html[data-apk-theme="dark"] #main :where(input:focus,textarea:focus,select:focus,.form-control:focus,.select2-container--open .select2-selection){
  background:#10243d!important;
  color:#ffffff!important;
  -webkit-text-fill-color:#ffffff!important;
  border-color:var(--apk-d-blue)!important;
  box-shadow:0 0 0 4px rgba(96,165,250,.18)!important;
  outline:none!important;
}
html[data-apk-theme="dark"] option{
  background:#0f1f35!important;
  color:#ffffff!important;
}

/* Select2 dropdown */
html[data-apk-theme="dark"] :where(.select2-dropdown,.select2-results,.select2-results__options){
  background:#101f35!important;
  color:#f8fbff!important;
  border-color:var(--apk-d-border2)!important;
}
html[data-apk-theme="dark"] :where(.select2-results__option){
  background:#101f35!important;
  color:#f8fbff!important;
  -webkit-text-fill-color:#f8fbff!important;
}
html[data-apk-theme="dark"] :where(.select2-results__option--highlighted,.select2-results__option[aria-selected="true"]){
  background:linear-gradient(135deg,#2563eb,#7c3aed)!important;
  color:#fff!important;
  -webkit-text-fill-color:#fff!important;
}

/* Tables and DataTables */
html[data-apk-theme="dark"] #main :where(table,.table,.dataTable,.dataTable-table){
  background:#0f1f35!important;
  color:#f8fbff!important;
  border-color:var(--apk-d-border)!important;
}
html[data-apk-theme="dark"] #main :where(table thead th,.table thead th,.dataTable thead th,.dataTable-table thead th){
  background:#1a2e4d!important;
  color:#eaf2ff!important;
  -webkit-text-fill-color:#eaf2ff!important;
  border-color:var(--apk-d-border)!important;
}
html[data-apk-theme="dark"] #main :where(table tbody tr,.table tbody tr,.dataTable tbody tr,.dataTable-table tbody tr){
  background:#0f1f35!important;
  color:#f8fbff!important;
}
html[data-apk-theme="dark"] #main :where(table tbody tr:nth-child(even),.table tbody tr:nth-child(even),.dataTable tbody tr:nth-child(even),.dataTable-table tbody tr:nth-child(even)){
  background:#142640!important;
}
html[data-apk-theme="dark"] #main :where(table td,.table td,.dataTable td,.dataTable-table td){
  background:transparent!important;
  color:#f8fbff!important;
  -webkit-text-fill-color:#f8fbff!important;
  border-color:var(--apk-d-border)!important;
}

/* Buttons: clickable + readable. Keep close/cancel usable. */
html[data-apk-theme="dark"] :where(button,.btn,.button,a.btn,a.button,input[type="submit"],input[type="button"],.apk-entry-open,.apk-flow-link,.apk-top-action){
  pointer-events:auto!important;
  position:relative!important;
  z-index:2;
  color:#ffffff!important;
  -webkit-text-fill-color:#ffffff!important;
  border-radius:999px!important;
}
html[data-apk-theme="dark"] :where(.btn-light,.btn-secondary,.btn-outline-secondary,.apk-secondary-action,.apk-btn-cancel,.apk-entry-close,.close,.closebtn,.btn-close,[aria-label="Tutup"],[aria-label="Close"]){
  background:#233b5d!important;
  color:#ffffff!important;
  -webkit-text-fill-color:#ffffff!important;
  border:1px solid rgba(219,234,254,.28)!important;
  box-shadow:0 10px 24px rgba(0,0,0,.25)!important;
}
html[data-apk-theme="dark"] :where(.btn-primary,.btn-info,.btn-success,.btn-print,.apk-print-button,.print-button,.apk-flow-link,.apk-flow-btn,.apk-top-action,.apk-primary-action,.apk-entry-open,input[type="submit"]){
  background:linear-gradient(135deg,#2563eb 0%,#7c3aed 58%,#ec4899 100%)!important;
  color:#fff!important;
  -webkit-text-fill-color:#fff!important;
  border-color:transparent!important;
  box-shadow:0 16px 38px rgba(37,99,235,.30),0 0 0 1px rgba(255,255,255,.10) inset!important;
}
html[data-apk-theme="dark"] :where(.btn-danger,.delete,.btn-delete){
  background:linear-gradient(135deg,#ef4444,#e11d48)!important;
  color:#fff!important;
  -webkit-text-fill-color:#fff!important;
}
html[data-apk-theme="dark"] :where(button::before,button::after,.btn::before,.btn::after,.button::before,.button::after){
  pointer-events:none!important;
}

/* Sidebar: final strongest rule. Avoid white submenu cards. */
html[data-apk-theme="dark"] :where(#sidebar,.sidebar-wrapper,.sidebar-menu,.sidebar-content,.apk-menu-shell){
  background:#0b1729!important;
  color:#f8fbff!important;
  border-color:rgba(148,178,220,.22)!important;
}
html[data-apk-theme="dark"] #sidebar :where(.submenu,ul.submenu,.submenu.active,.collapse.show){
  background:linear-gradient(180deg,#101f35,#132640)!important;
  color:#eaf2ff!important;
  border:1px solid rgba(96,165,250,.26)!important;
  box-shadow:0 18px 44px rgba(0,0,0,.30)!important;
}
html[data-apk-theme="dark"] #sidebar :where(.submenu *,.submenu a,.submenu li,.submenu span,.sidebar-link,.sidebar-link span,.menu-title){
  color:#eaf2ff!important;
  -webkit-text-fill-color:#eaf2ff!important;
  opacity:1!important;
}
html[data-apk-theme="dark"] #sidebar :where(.submenu a:hover,.submenu .active>a,.sidebar-item.active>.sidebar-link,.sidebar-link:hover){
  background:linear-gradient(135deg,rgba(37,99,235,.24),rgba(236,72,153,.16))!important;
  color:#ffffff!important;
  -webkit-text-fill-color:#ffffff!important;
}

/* Project flow / progress page */
html[data-apk-theme="dark"] .project-flow-page :where(.pf-card,.pf-hero,.pf-status-card,.pf-kv,.pf-guide-item,.pf-match-list a,.pf-empty,.pf-ok,.pf-warning){
  background:linear-gradient(180deg,#101f35,#132640)!important;
  color:#f8fbff!important;
  border-color:var(--apk-d-border)!important;
}
html[data-apk-theme="dark"] .project-flow-page :where(.pf-card *,.pf-hero *,.pf-status-card *,.pf-kv *,.pf-guide-item *,.pf-step *,.pf-empty,.pf-ok,.pf-warning){
  color:#f8fbff!important;
  -webkit-text-fill-color:currentColor!important;
}
html[data-apk-theme="dark"] .project-flow-page :where(.pf-step-role,.pf-status-label,.pf-badge){
  color:#bfdbfe!important;
  -webkit-text-fill-color:#bfdbfe!important;
}
html[data-apk-theme="dark"] .project-flow-page :where(.pf-step-meta,.pf-step-status,.pf-status-hint,.pf-kv span,.pf-guide-item span){
  color:#c3d2e8!important;
  -webkit-text-fill-color:#c3d2e8!important;
}

/* Datepicker */
html[data-apk-theme="dark"] :where(.Zebra_DatePicker,.Zebra_DatePicker *,.datepicker,.datepicker *){
  background:#101f35!important;
  color:#f8fbff!important;
  -webkit-text-fill-color:#f8fbff!important;
  border-color:var(--apk-d-border)!important;
}
html[data-apk-theme="dark"] :where(.Zebra_DatePicker .dp_selected,.datepicker .active){
  background:linear-gradient(135deg,#2563eb,#7c3aed)!important;
  color:#fff!important;
}

/* Light mode safety for menu/buttons */
html[data-apk-theme="light"] #sidebar :where(.submenu,.submenu a,.submenu li,.submenu span){
  background:#ffffff!important;
  color:#1f2a44!important;
  -webkit-text-fill-color:#1f2a44!important;
  opacity:1!important;
}
html[data-apk-theme="light"] :where(.btn-primary,.btn-info,.btn-success,.btn-danger,.btn-print,.apk-print-button,.print-button,.apk-flow-link,.apk-flow-btn,.apk-top-action,.apk-primary-action,.apk-entry-open,input[type="submit"]){
  color:#fff!important;
  -webkit-text-fill-color:#fff!important;
}

/* Elements fixed by JS contrast guard */
html[data-apk-theme="dark"] .apk-force-dark-surface{
  background:linear-gradient(180deg,#101f35,#132640)!important;
  color:#f8fbff!important;
  border-color:var(--apk-d-border)!important;
}
html[data-apk-theme="dark"] .apk-force-dark-text{
  color:#f8fbff!important;
  -webkit-text-fill-color:#f8fbff!important;
}
html[data-apk-theme="dark"] .apk-force-light-text{
  color:#10213f!important;
  -webkit-text-fill-color:#10213f!important;
}
