/*******************************
* 5. SHORTCODE [job_details] - UPDATED WITH BETTER CONTRAST
********************************/
function job_details_shortcode() {
    if ( ! is_single() ) return '';

    $enable_job = get_post_meta( get_the_ID(), 'enable_job', true );
    if ( $enable_job !== 'on' ) return '';

    $icons = [
        'company'  => '<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="currentColor" width="20" height="20"><path fill-rule="evenodd" d="M4.5 2.25a.75.75 0 000 1.5v16.5h-.75a.75.75 0 000 1.5h16.5a.75.75 0 000-1.5h-.75V3.75a.75.75 0 000-1.5h-15zM9 6a.75.75 0 000 1.5h1.5a.75.75 0 000-1.5H9zm-.75 3.75A.75.75 0 019 9h1.5a.75.75 0 010 1.5H9a.75.75 0 01-.75-.75zM9 12a.75.75 0 000 1.5h1.5a.75.75 0 000-1.5H9zm3.75-5.25A.75.75 0 0113.5 6H15a.75.75 0 010 1.5h-1.5a.75.75 0 01-.75-.75zM13.5 9a.75.75 0 000 1.5H15A.75.75 0 0015 9h-1.5zm-.75 3.75a.75.75 0 01.75-.75H15a.75.75 0 010 1.5h-1.5a.75.75 0 01-.75-.75zM9 19.5v-2.25a.75.75 0 01.75-.75h4.5a.75.75 0 01.75.75v2.25a.75.75 0 01-.75.75h-4.5A.75.75 0 019 19.5z" clip-rule="evenodd"/></svg>',
        'location' => '<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="currentColor" width="20" height="20"><path fill-rule="evenodd" d="M11.54 22.351l.07.04.028.016a.76.76 0 00.723 0l.028-.015.071-.041a16.975 16.975 0 001.144-.742 19.58 19.58 0 002.683-2.282c1.944-2.013 3.952-5.044 3.952-9.827a8.25 8.25 0 00-16.5 0c0 4.783 2.007 7.814 3.952 9.827a19.58 19.58 0 002.682 2.282 16.975 16.975 0 001.144.742zM12 13.5a3 3 0 100-6 3 3 0 000 6z" clip-rule="evenodd"/></svg>',
        'salary'   => '<svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24"><path fill="currentColor" d="M21 7.28V5c0-1.1-.9-2-2-2H5a2 2 0 0 0-2 2v14a2 2 0 0 0 2 2h14c1.1 0 2-.9 2-2v-2.28A2 2 0 0 0 22 15V9a2 2 0 0 0-1-1.72M20 9v6h-7V9zM5 19V5h14v2h-6c-1.1 0-2 .9-2 2v6c0 1.1.9 2 2 2h6v2z"/><circle cx="16" cy="12" r="1.5" fill="currentColor"/></svg>',
        'post'     => '<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="currentColor" width="20" height="20"><path fill-rule="evenodd" d="M7.5 5.25a3 3 0 013-3h3a3 3 0 013 3v.205c.933.085 1.857.197 2.774.334 1.454.218 2.476 1.483 2.476 2.917v3.033c0 1.211-.734 2.352-1.936 2.752A24.726 24.726 0 0112 15.75c-2.73 0-5.357-.442-7.814-1.259-1.202-.4-1.936-1.541-1.936-2.752V8.706c0-1.434 1.022-2.7 2.476-2.917A48.814 48.814 0 017.5 5.455V5.25zm7.5 0v.09a48.488 48.488 0 00-6 0v-.09a1.5 1.5 0 011.5-1.5h3a1.5 1.5 0 011.5 1.5zm-3 8.25a.75.75 0 100-1.5.75.75 0 000 1.5z" clip-rule="evenodd"/><path d="M3 18.4v-2.796a4.3 4.3 0 00.713.31A26.226 26.226 0 0012 17.25c2.892 0 5.68-.468 8.287-1.335.252-.084.49-.189.713-.311V18.4c0 1.452-1.047 2.728-2.523 2.923-2.12.282-4.282.427-6.477.427a49.19 49.19 0 01-6.477-.427C4.047 21.128 3 19.852 3 18.4z"/></svg>',
        'qual'     => '<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="currentColor" width="20" height="20"><path d="M11.7 2.805a.75.75 0 01.6 0A60.65 60.65 0 0122.83 8.72a.75.75 0 01-.231 1.337 49.949 49.949 0 00-9.902 3.912l-.003.002-.34.18a.75.75 0 01-.707 0l-.341-.18-.003-.002a49.95 49.95 0 00-9.903-3.912.75.75 0 01-.23-1.337A60.653 60.653 0 0111.7 2.805z"/><path d="M13.06 15.473a48.45 48.45 0 017.666-3.282c.134 1.414.22 2.843.255 4.285a.75.75 0 01-.46.71 47.878 47.878 0 00-8.105 4.342.75.75 0 01-.832 0 47.877 47.877 0 00-8.104-4.342.75.75 0 01-.461-.71c.035-1.442.121-2.87.255-4.286A48.4 48.4 0 016 13.18v1.27a1.5 1.5 0 00-.14 2.508c-.09.38-.222.753-.397 1.11.452.213.901.434 1.346.661a6.729 6.729 0 00.551-1.608 1.5 1.5 0 00.14-2.67v-.645a48.549 48.549 0 013.44 1.668 2.25 2.25 0 002.12 0z"/></svg>',
        'exp'      => '<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="currentColor" width="20" height="20"><path fill-rule="evenodd" d="M7.5 5.25a3 3 0 013-3h3a3 3 0 013 3v.205c.933.085 1.857.197 2.774.334 1.454.218 2.476 1.483 2.476 2.917v3.033c0 1.211-.734 2.352-1.936 2.752A24.726 24.726 0 0112 15.75c-2.73 0-5.357-.442-7.814-1.259-1.202-.4-1.936-1.541-1.936-2.752V8.706c0-1.434 1.022-2.7 2.476-2.917A48.814 48.814 0 017.5 5.455V5.25zm7.5 0v.09a48.488 48.488 0 00-6 0v-.09a1.5 1.5 0 011.5-1.5h3a1.5 1.5 0 011.5 1.5zm-3 8.25a.75.75 0 100-1.5.75.75 0 000 1.5z" clip-rule="evenodd"/><path d="M3 18.4v-2.796a4.3 4.3 0 00.713.31A26.226 26.226 0 0012 17.25c2.892 0 5.68-.468 8.287-1.335.252-.084.49-.189.713-.311V18.4c0 1.452-1.047 2.728-2.523 2.923-2.12.282-4.282.427-6.477.427a49.19 49.19 0 01-6.477-.427C4.047 21.128 3 19.852 3 18.4z"/></svg>',
        'age'      => '<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="currentColor" width="20" height="20"><path fill-rule="evenodd" d="M7.5 6a4.5 4.5 0 119 0 4.5 4.5 0 01-9 0zM3.751 20.105a8.25 8.25 0 0116.498 0 .75.75 0 01-.437.695A18.683 18.683 0 0112 22.5c-2.786 0-5.433-.608-7.812-1.7a.75.75 0 01-.437-.695z" clip-rule="evenodd"/></svg>',
        'exam'     => '<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="currentColor" width="20" height="20"><path fill-rule="evenodd" d="M6.75 2.25A.75.75 0 017.5 3v1.5h9V3A.75.75 0 0118 3v1.5h.75a3 3 0 013 3v11.25a3 3 0 01-3 3H5.25a3 3 0 01-3-3V7.5a3 3 0 013-3H6V3a.75.75 0 01.75-.75zm13.5 9a1.5 1.5 0 00-1.5-1.5H5.25a1.5 1.5 0 00-1.5 1.5v7.5a1.5 1.5 0 001.5 1.5h13.5a1.5 1.5 0 001.5-1.5v-7.5z" clip-rule="evenodd"/></svg>',
        'deadline' => '<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="currentColor" width="20" height="20"><path fill-rule="evenodd" d="M12 2.25c-5.385 0-9.75 4.365-9.75 9.75s4.365 9.75 9.75 9.75 9.75-4.365 9.75-9.75S17.385 2.25 12 2.25zM12.75 6a.75.75 0 00-1.5 0v6c0 .414.336.75.75.75h4.5a.75.75 0 000-1.5h-3.75V6z" clip-rule="evenodd"/></svg>',
        'visa'     => '<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="currentColor" width="20" height="20"><path d="M12 1.5a10.5 10.5 0 100 21 10.5 10.5 0 000-21zM3.063 10.5H6.75a15.277 15.277 0 00-.227-2.816A15.41 15.41 0 013.063 10.5zm0 3a15.41 15.41 0 003.46-1.184A15.28 15.28 0 006.75 13.5H3.063zm10.124 5.5c.413-.61.79-1.384 1.112-2.297A13.82 13.82 0 0017.25 13.5h2.687a9.026 9.026 0 01-6.75 5.5zm-6.374 0A9.026 9.026 0 013.063 13.5H5.75c.32.96.697 1.817 1.111 2.547.322.913.699 1.687 1.113 2.297-.3-.05-.597-.11-.887-.177a13.828 13.828 0 01-2.28-.667zM12 19.5c-.512-.62-.993-1.459-1.418-2.47A13.818 13.818 0 0110.5 15h3a13.818 13.818 0 01-.082 2.03C13.038 18.017 12.556 18.856 12 19.5zm-1.5-9h3a13.82 13.82 0 00-.082-2.03C13.038 7.483 12.556 6.644 12 6c-.556.644-1.038 1.483-1.418 2.47A13.818 13.818 0 0010.5 10.5zm5.25 0h3.687a15.41 15.41 0 00-3.46-1.184A15.28 15.28 0 0015.75 10.5zm0 3a15.277 15.277 0 00.227 2.816A15.41 15.41 0 0119.437 13.5H15.75z"/></svg>',
        'hours'    => '<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="currentColor" width="20" height="20"><path fill-rule="evenodd" d="M12 2.25c-5.385 0-9.75 4.365-9.75 9.75s4.365 9.75 9.75 9.75 9.75-4.365 9.75-9.75S17.385 2.25 12 2.25zM12.75 6a.75.75 0 00-1.5 0v6c0 .414.336.75.75.75h4.5a.75.75 0 000-1.5h-3.75V6z" clip-rule="evenodd"/></svg>',
        'type'     => '<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="currentColor" width="20" height="20"><path fill-rule="evenodd" d="M7.502 6h7.128A3.375 3.375 0 0118 9.375v9.375a3 3 0 003-3V6.108c0-1.505-1.125-2.811-2.664-2.94a48.972 48.972 0 00-.673-.05A3 3 0 0015 1.5h-1.5a3 3 0 00-2.663 1.618c-.225.015-.45.032-.673.05C8.662 3.295 7.554 4.542 7.502 6zM13.5 3A1.5 1.5 0 0012 4.5h4.5A1.5 1.5 0 0015 3h-1.5z" clip-rule="evenodd"/><path fill-rule="evenodd" d="M3 9.375C3 8.339 3.84 7.5 4.875 7.5h9.75c1.036 0 1.875.84 1.875 1.875v11.25c0 1.035-.84 1.875-1.875 1.875h-9.75A1.875 1.875 0 013 20.625V9.375zm9.586 4.594a.75.75 0 00-1.172-.938l-2.476 3.096-.908-.907a.75.75 0 00-1.06 1.06l1.5 1.5a.75.75 0 001.116-.062l3-3.75z" clip-rule="evenodd"/></svg>',
        'apply'    => '<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="currentColor" width="20" height="20"><path fill-rule="evenodd" d="M12 2.25c-5.385 0-9.75 4.365-9.75 9.75s4.365 9.75 9.75 9.75 9.75-4.365 9.75-9.75S17.385 2.25 12 2.25zm4.28 10.28a.75.75 0 000-1.06l-3-3a.75.75 0 10-1.06 1.06l1.72 1.72H8.25a.75.75 0 000 1.5h5.69l-1.72 1.72a.75.75 0 101.06 1.06l3-3z" clip-rule="evenodd"/></svg>',
    ];

    $pid = get_the_ID();
    $d   = [
        'company'   => get_post_meta( $pid, 'job_company',   true ),
        'location'  => get_post_meta( $pid, 'job_location',  true ),
        'desc'      => get_post_meta( $pid, 'job_desc',      true ),
        'salary'    => get_post_meta( $pid, 'job_salary',    true ),
        'post'      => get_post_meta( $pid, 'job_post',      true ),
        'qual'      => get_post_meta( $pid, 'qualification', true ),
        'exp'       => get_post_meta( $pid, 'experience',     true ),
        'age'       => get_post_meta( $pid, 'age_limit',     true ),
        'exam'      => get_post_meta( $pid, 'exam_date',     true ),
        'deadline'  => get_post_meta( $pid, 'last_apply',    true ),
        'apply_url' => get_post_meta( $pid, 'apply_url',     true ),
        'visa'      => get_post_meta( $pid, 'job_visa',      true ),
        'hours'     => get_post_meta( $pid, 'job_hours',     true ),
        'type'      => get_post_meta( $pid, 'job_type',      true ),
        'logo_id'   => get_post_meta( $pid, 'job_logo_id',   true ),
    ];

    if ( ! empty( $d['exam'] ) )     $d['exam']     = date( 'd M, Y', strtotime( $d['exam'] ) );
    if ( ! empty( $d['deadline'] ) ) $d['deadline'] = date( 'd M, Y', strtotime( $d['deadline'] ) );
    if ( ! empty( $d['salary'] ) )   $d['salary']   = '€' . $d['salary'];

    $visa_label = '';
    if ( $d['visa'] === 'yes' ) $visa_label = '✔ Available';
    if ( $d['visa'] === 'no'  ) $visa_label = '✘ Not Available';

    $logo_url = '';
    if ( ! empty( $d['logo_id'] ) ) {
        $src = wp_get_attachment_image_src( $d['logo_id'], 'medium' );
        if ( $src ) $logo_url = $src[0];
    }

    $tile = function( $icon, $label, $value, $bg, $fg ) {
        $out  = '<div style="display:flex;gap:15px;align-items:flex-start;">';
        $out .= '<div style="background:' . $bg . ';width:42px;height:42px;border-radius:10px;'
              . 'display:flex;align-items:center;justify-content:center;flex-shrink:0;color:' . $fg . ';">'
              . $icon . '</div>';
        $out .= '<div>';
        $out .= '<div style="font-size:0.85em;color:#64748b;margin-bottom:3px;">' . esc_html( $label ) . '</div>';
        $out .= '<div style="font-weight:700;font-size:1.05em;color:#1e293b;">' . esc_html( $value ) . '</div>';
        $out .= '</div></div>';
        return $out;
    };

    $html = '<div class="job-details-card" style="border:1px solid #e2e8f0;border-radius:12px;overflow:hidden;margin:30px 0;background:#fff;box-shadow:0 4px 6px -1px rgba(0,0,0,0.07),0 2px 4px -1px rgba(0,0,0,0.04);">';

    // Header
    $html .= '<div style="background:#2563eb;padding:18px 22px;color:#fff;position:relative;overflow:hidden;">';
    $html .= '<div style="display:flex;align-items:center;justify-content:space-between;position:relative;z-index:2;">';
    $html .= '<div style="display:flex;align-items:center;gap:12px;font-size:1.4em;font-weight:700;">' . $icons['post'] . ' Job Details</div>';
    if ( $logo_url ) {
        $html .= '<div style="width:68px;height:68px;border-radius:10px;overflow:hidden;background:#fff;display:flex;align-items:center;justify-content:center;padding:5px;flex-shrink:0;box-shadow:0 2px 8px rgba(0,0,0,0.15);">';
        $html .= '<img src="' . esc_url( $logo_url ) . '" alt="' . esc_attr( $d['company'] ) . ' Logo" style="width:100%;height:100%;object-fit:contain;">';
        $html .= '</div>';
    }
    $html .= '</div>';
    $html .= '<div style="position:absolute;top:-20px;right:-20px;width:100px;height:100px;border-radius:50%;background:rgba(255,255,255,0.15);z-index:1;"></div>';
    $html .= '<div style="position:absolute;bottom:-30px;left:-30px;width:80px;height:80px;border-radius:50%;background:rgba(255,255,255,0.1);z-index:1;"></div>';
    $html .= '</div>';

    // Description
    if ( ! empty( $d['desc'] ) ) {
        $html .= '<div style="padding:16px 22px;line-height:1.7;color:#334155;font-size:1em;position:relative;background:#eff6ff;">';
        $html .= '<div style="position:absolute;top:0;left:0;width:4px;height:100%;background:#2563eb;"></div>';
        $html .= wp_kses_post( $d['desc'] );
        $html .= '</div>';
    }

    // Grid
    $html .= '<div style="display:grid;grid-template-columns:repeat(auto-fit,minmax(250px,1fr));gap:20px;padding:25px 22px;">';
    if ( ! empty( $d['company'] ) )  $html .= $tile( $icons['company'],  'Company',          $d['company'],   '#e0f2fe', '#0284c7' );
    if ( ! empty( $d['location'] ) ) $html .= $tile( $icons['location'], 'Location',         $d['location'],  '#fef9c3', '#ca8a04' );
    if ( ! empty( $d['salary'] ) )   $html .= $tile( $icons['salary'],   'Salary',           $d['salary'],    '#dbeafe', '#2563eb' );
    if ( ! empty( $d['post'] ) )     $html .= $tile( $icons['post'],     'Job Post',         $d['post'],      '#dbeafe', '#2563eb' );
    if ( ! empty( $d['type'] ) )     $html .= $tile( $icons['type'],     'Job Type',         $d['type'],      '#f0fdf4', '#16a34a' );
    if ( ! empty( $d['hours'] ) )    $html .= $tile( $icons['hours'],    'Working Hours',    $d['hours'],     '#fff7ed', '#ea580c' );
    if ( ! empty( $d['qual'] ) )     $html .= $tile( $icons['qual'],     'Qualification',    $d['qual'],      '#f3e8ff', '#9333ea' );
    // Experience tile with HIGH CONTRAST colors - dark background with white text
    if ( ! empty( $d['exp'] ) )      $html .= $tile( $icons['exp'],      'Experience',       $d['exp'],       '#1e40af', '#ffffff' );
    if ( ! empty( $d['age'] ) )      $html .= $tile( $icons['age'],      'Age Limit',        $d['age'],       '#fce7f3', '#db2777' );
    if ( $visa_label )               $html .= $tile( $icons['visa'],     'Visa Sponsorship', $visa_label,     '#f0f9ff', '#0369a1' );
    if ( ! empty( $d['exam'] ) )     $html .= $tile( $icons['exam'],     'Exam Date',        $d['exam'],      '#dcfce7', '#16a34a' );
    if ( ! empty( $d['deadline'] ) ) $html .= $tile( $icons['deadline'], 'Last Apply Date',  $d['deadline'],  '#ffedd5', '#ea580c' );
    $html .= '</div>';

    // Apply button
    if ( ! empty( $d['apply_url'] ) ) {
        $html .= '<div style="padding:0 22px 28px;text-align:center;">';
        $html .= '<a href="' . esc_url( $d['apply_url'] ) . '" target="_blank" rel="noopener noreferrer"'
               . ' style="display:inline-flex;align-items:center;gap:10px;background:#2563eb;'
               . 'color:#fff;padding:12px 60px;border-radius:50px;text-decoration:none;font-weight:700;'
               . 'font-size:1em;transition:all .3s ease;'
               . 'box-shadow:0 4px 6px -1px rgba(37,99,235,.3),0 2px 4px -1px rgba(37,99,235,.2);">';
        $html .= 'Apply Now ' . $icons['apply'];
        $html .= '</a>';
        $html .= '</div>';
    }

    $html .= '</div>';
    return $html;
}
add_shortcode( 'job_details', 'job_details_shortcode' );