<x-app-layout title="Dashboard Instansi">

@push('styles')
<link href="https://fonts.googleapis.com/css2?family=Plus+Jakarta+Sans:wght@400;500;600;700;800;900&display=swap" rel="stylesheet">
<style>
:root {
    --primary:#4f46e5;--primary-d:#3730a3;--primary-l:#eef2ff;
    --success:#059669;--success-l:#ecfdf5;--danger:#dc2626;--danger-l:#fef2f2;
    --warning:#d97706;--warning-l:#fffbeb;--sky:#0284c7;--sky-l:#e0f2fe;
    --rose:#e11d48;--rose-l:#fff1f2;
    --text:#0f172a;--muted:#64748b;--muted2:#94a3b8;--border:#e2e8f0;
    --bg:#f0f4ff;--white:#ffffff;--sh:0 1px 3px rgba(0,0,0,.06),0 4px 16px rgba(0,0,0,.05);
}
*,*::before,*::after{box-sizing:border-box;margin:0;padding:0;}
body{font-family:'Plus Jakarta Sans',sans-serif;background:var(--bg);color:var(--text);}

.ins-wrap{max-width:1200px;margin:0 auto;padding:28px 28px 52px;}

/* HERO */
.ins-hero{background:linear-gradient(135deg,#1e1b4b 0%,#3730a3 45%,#4f46e5 80%,#6366f1 100%);border-radius:22px;padding:28px 32px;margin-bottom:24px;position:relative;overflow:hidden;box-shadow:0 12px 40px rgba(79,70,229,.35);display:flex;align-items:center;justify-content:space-between;gap:24px;}
.hero-orb{position:absolute;border-radius:50%;pointer-events:none;filter:blur(65px);}
.hero-orb-1{width:320px;height:320px;top:-130px;right:-60px;background:rgba(139,92,246,.3);}
.hero-orb-2{width:160px;height:160px;bottom:-60px;right:200px;background:rgba(196,181,253,.12);}
.hero-left{position:relative;z-index:1;}
.hero-badge{display:inline-flex;align-items:center;gap:6px;background:rgba(255,255,255,.12);border:1px solid rgba(255,255,255,.2);border-radius:100px;padding:4px 12px;margin-bottom:12px;font-size:10.5px;font-weight:800;color:rgba(255,255,255,.75);text-transform:uppercase;letter-spacing:.1em;}
.hero-badge svg{width:10px;height:10px;stroke:rgba(255,255,255,.65);}
.hero-title{font-size:1.65rem;font-weight:900;color:#fff;letter-spacing:-.03em;line-height:1.2;margin-bottom:14px;}
.hero-title span{color:#c7d2fe;}
.hero-pills{display:flex;gap:8px;flex-wrap:wrap;}
.hpill{display:inline-flex;align-items:center;gap:6px;background:rgba(255,255,255,.1);border:1px solid rgba(255,255,255,.15);border-radius:8px;padding:5px 12px;font-size:11.5px;font-weight:700;color:rgba(255,255,255,.85);}
.hpill svg{width:12px;height:12px;stroke:rgba(255,255,255,.7);flex-shrink:0;}
.hpill.green{background:rgba(52,211,153,.18);border-color:rgba(52,211,153,.3);color:#6ee7b7;}
.hpill.green svg{stroke:#6ee7b7;}
.hero-right{position:relative;z-index:1;display:flex;gap:10px;flex-shrink:0;}
.hero-stat{background:rgba(255,255,255,.1);border:1px solid rgba(255,255,255,.15);border-radius:14px;padding:14px 18px;text-align:center;min-width:78px;}
.hero-stat-n{font-size:1.8rem;font-weight:900;color:#fff;letter-spacing:-.04em;line-height:1;}
.hero-stat-l{font-size:9.5px;font-weight:700;color:rgba(255,255,255,.5);text-transform:uppercase;letter-spacing:.08em;margin-top:4px;}

/* KPI */
.kpi-row{display:grid;grid-template-columns:repeat(4,1fr);gap:14px;margin-bottom:22px;}
.kpi-card{background:var(--white);border:1px solid var(--border);border-radius:18px;padding:18px 18px 16px;position:relative;overflow:hidden;box-shadow:var(--sh);transition:transform .2s,box-shadow .2s;}
.kpi-card:hover{transform:translateY(-3px);box-shadow:0 8px 28px rgba(0,0,0,.09);}
.kpi-card::before{content:'';position:absolute;top:0;left:0;right:0;height:3px;border-radius:18px 18px 0 0;}
.kpi-card.kv::before{background:linear-gradient(90deg,#6d28d9,#8b5cf6);}
.kpi-card.ks::before{background:linear-gradient(90deg,#059669,#34d399);}
.kpi-card.ka::before{background:linear-gradient(90deg,#d97706,#fbbf24);}
.kpi-card.kr::before{background:linear-gradient(90deg,#e11d48,#f43f5e);}
.kpi-top{display:flex;align-items:flex-start;justify-content:space-between;margin-bottom:14px;}
.kpi-ico{width:42px;height:42px;border-radius:12px;display:flex;align-items:center;justify-content:center;}
.kpi-ico svg{width:20px;height:20px;}
.kpi-ico.kv{background:linear-gradient(135deg,#ede9fe,#ddd6fe);}
.kpi-ico.ks{background:linear-gradient(135deg,#d1fae5,#a7f3d0);}
.kpi-ico.ka{background:linear-gradient(135deg,#fef3c7,#fde68a);}
.kpi-ico.kr{background:linear-gradient(135deg,#ffe4e6,#fecdd3);}
.kpi-tag{font-size:10px;font-weight:800;padding:3px 9px;border-radius:6px;}
.tag-v{background:#ede9fe;color:#5b21b6;}.tag-s{background:#d1fae5;color:#065f46;}
.tag-a{background:#fef9c3;color:#92400e;}.tag-r{background:#ffe4e6;color:#9f1239;}
.kpi-num{font-size:2.2rem;font-weight:900;color:var(--text);letter-spacing:-.04em;line-height:1;margin-bottom:3px;}
.kpi-lbl{font-size:12px;font-weight:600;color:var(--muted);}

/* LAYOUT */
.ins-cols{display:grid;grid-template-columns:1fr 340px;gap:20px;margin-bottom:22px;align-items:start;}
.ins-cols-3{display:grid;grid-template-columns:repeat(3,1fr);gap:20px;margin-bottom:22px;align-items:start;}

/* PANEL */
.panel{background:var(--white);border:1px solid var(--border);border-radius:20px;overflow:hidden;box-shadow:var(--sh);}
.panel-hd{padding:16px 20px;border-bottom:1px solid #f1f5f9;display:flex;align-items:center;justify-content:space-between;gap:10px;}
.panel-hd-title{font-size:13.5px;font-weight:800;color:var(--text);}
.panel-hd-badge{font-size:11px;font-weight:700;background:#f1f5f9;color:var(--muted);border-radius:100px;padding:3px 10px;}
.panel-hd-link{font-size:12px;font-weight:700;color:var(--primary);text-decoration:none;display:flex;align-items:center;gap:4px;transition:gap .15s;}
.panel-hd-link:hover{gap:7px;}
.panel-ft{padding:13px 18px;border-top:1px solid #f1f5f9;display:flex;justify-content:flex-end;}

/* ABSENSI TABLE */
.abs-table{width:100%;border-collapse:collapse;font-size:13px;}
.abs-table thead tr{background:#fafbff;}
.abs-table thead th{padding:10px 18px;text-align:left;font-size:10px;font-weight:800;letter-spacing:.08em;text-transform:uppercase;color:var(--muted2);border-bottom:1px solid #f1f5f9;}
.abs-table tbody tr{border-bottom:1px solid #f8f9fc;transition:background .12s;}
.abs-table tbody tr:last-child{border-bottom:none;}
.abs-table tbody tr:hover{background:#fafbff;}
.abs-table tbody td{padding:12px 18px;vertical-align:middle;}
.kar-cell{display:flex;align-items:center;gap:10px;}
.kar-av{width:38px;height:38px;border-radius:10px;flex-shrink:0;display:flex;align-items:center;justify-content:center;font-size:13px;font-weight:800;color:#fff;}
.kar-name{font-size:13px;font-weight:700;color:var(--text);}
.kar-jab{font-size:11px;color:var(--muted2);font-weight:500;margin-top:1px;}
.time-pill{font-size:12.5px;font-weight:700;}
.time-pill.ok{color:var(--success);}
.time-pill.late{color:var(--warning);}
.time-pill.none{color:var(--muted2);}
.status-dot{display:inline-flex;align-items:center;gap:5px;font-size:11px;font-weight:700;padding:3px 10px;border-radius:100px;}
.sd-hadir{background:#dcfce7;color:#065f46;} .sd-hadir::before{content:'';width:6px;height:6px;border-radius:50%;background:#10b981;}
.sd-terlambat{background:#fef9c3;color:#92400e;} .sd-terlambat::before{content:'';width:6px;height:6px;border-radius:50%;background:#f59e0b;}
.sd-absen{background:#fee2e2;color:#991b1b;} .sd-absen::before{content:'';width:6px;height:6px;border-radius:50%;background:#ef4444;}
.tbl-action{font-size:11.5px;font-weight:700;color:var(--primary);text-decoration:none;padding:5px 12px;background:var(--primary-l);border-radius:8px;transition:background .15s;}
.tbl-action:hover{background:#ddd6fe;}
.empty-tbl{padding:44px 20px;text-align:center;}
.empty-tbl svg{stroke:#e2e8f0;margin-bottom:12px;}
.empty-tbl p{font-size:13px;color:var(--muted2);font-weight:600;}

/* QUICK ACTIONS — 4 kolom, 2 baris */
.qa-grid{display:grid;grid-template-columns:repeat(4,1fr);gap:10px;padding:16px;}
.qa-btn{background:#f8fafc;border:1px solid var(--border);border-radius:14px;padding:16px 12px;text-decoration:none;color:var(--text);display:flex;flex-direction:column;align-items:center;gap:9px;transition:all .2s;cursor:pointer;}
.qa-btn:hover{background:var(--primary-l);border-color:rgba(79,70,229,.25);transform:translateY(-2px);box-shadow:0 6px 20px rgba(79,70,229,.12);}
.qa-ico{width:46px;height:46px;border-radius:13px;display:flex;align-items:center;justify-content:center;}
.qa-ico svg{width:20px;height:20px;}
.qa-ico.q-v{background:linear-gradient(135deg,#8b5cf6,#6d28d9);}
.qa-ico.q-s{background:linear-gradient(135deg,#34d399,#059669);}
.qa-ico.q-b{background:linear-gradient(135deg,#38bdf8,#0284c7);}
.qa-ico.q-a{background:linear-gradient(135deg,#fbbf24,#d97706);}
.qa-ico.q-r{background:linear-gradient(135deg,#f87171,#dc2626);}
.qa-ico.q-sl{background:linear-gradient(135deg,#94a3b8,#475569);}
.qa-ico.q-cy{background:linear-gradient(135deg,#22d3ee,#0891b2);}
.qa-ico.q-em{background:linear-gradient(135deg,#a78bfa,#7c3aed);}
.qa-lbl{font-size:11px;font-weight:700;color:var(--muted);text-align:center;line-height:1.3;}

/* CHART */
.chart-wrap{padding:18px 20px;}
.chart-bars{display:flex;align-items:flex-end;gap:8px;height:100px;margin-bottom:8px;}
.bar-col{flex:1;display:flex;flex-direction:column;align-items:center;gap:4px;}
.bar-bg{width:100%;background:#f1f5f9;border-radius:6px 6px 0 0;position:relative;overflow:hidden;}
.bar-fill{position:absolute;bottom:0;left:0;right:0;border-radius:6px 6px 0 0;background:linear-gradient(180deg,#8b5cf6,#6d28d9);transition:height .6s cubic-bezier(.4,0,.2,1);}
.bar-fill.today{background:linear-gradient(180deg,#34d399,#059669);}
.bar-day{font-size:9.5px;font-weight:700;color:var(--muted2);text-transform:uppercase;letter-spacing:.04em;}
.chart-legend{display:flex;gap:16px;padding:0 2px;}
.chart-leg-item{display:flex;align-items:center;gap:5px;font-size:11px;font-weight:600;color:var(--muted);}
.chart-leg-dot{width:8px;height:8px;border-radius:50%;}

/* JADWAL */
.jadwal-card{background:linear-gradient(135deg,var(--primary),#7c3aed);border-radius:16px;padding:18px 20px;margin:0 16px 16px;display:flex;align-items:center;gap:14px;}
.jadwal-ico{width:44px;height:44px;border-radius:12px;background:rgba(255,255,255,.15);display:flex;align-items:center;justify-content:center;flex-shrink:0;}
.jadwal-ico svg{width:20px;height:20px;stroke:white;}
.jadwal-name{font-size:12px;font-weight:700;color:rgba(255,255,255,.7);margin-bottom:4px;}
.jadwal-times{display:flex;gap:18px;}
.jadwal-time{font-size:11px;color:rgba(255,255,255,.65);font-weight:600;}
.jadwal-time strong{display:block;font-size:17px;font-weight:800;color:white;letter-spacing:-.3px;}
.jadwal-tol{font-size:10.5px;font-weight:700;color:rgba(255,255,255,.65);margin-top:5px;background:rgba(255,255,255,.12);border-radius:100px;padding:2px 10px;display:inline-block;}

/* KAR SUMMARY */
.kar-sum-item{display:flex;align-items:center;gap:12px;padding:12px 18px;border-bottom:1px solid #f8f9fc;transition:background .12s;}
.kar-sum-item:last-child{border-bottom:none;}
.kar-sum-item:hover{background:#fafbff;}
.ksav{width:36px;height:36px;border-radius:10px;flex-shrink:0;display:flex;align-items:center;justify-content:center;font-size:13px;font-weight:800;color:#fff;}
.ks-name{font-size:13px;font-weight:700;color:var(--text);flex:1;min-width:0;}
.ks-jab{font-size:11px;color:var(--muted2);font-weight:500;white-space:nowrap;overflow:hidden;text-overflow:ellipsis;}

/* TOP KARYAWAN */
.top-item{display:flex;align-items:center;gap:12px;padding:11px 18px;border-bottom:1px solid #f8f9fc;}
.top-item:last-child{border-bottom:none;}
.top-rank{width:24px;height:24px;border-radius:7px;flex-shrink:0;display:flex;align-items:center;justify-content:center;font-size:11px;font-weight:900;}
.rank-1{background:linear-gradient(135deg,#fbbf24,#d97706);color:#fff;}
.rank-2{background:linear-gradient(135deg,#94a3b8,#64748b);color:#fff;}
.rank-3{background:linear-gradient(135deg,#c2a06a,#a07850);color:#fff;}
.rank-n{background:#f1f5f9;color:var(--muted);}
.top-name{font-size:13px;font-weight:700;color:var(--text);flex:1;}
.top-bar-wrap{width:80px;height:6px;background:#f1f5f9;border-radius:100px;overflow:hidden;}
.top-bar-fill{height:100%;border-radius:100px;background:linear-gradient(90deg,#8b5cf6,#6d28d9);}
.top-count{font-size:12px;font-weight:800;color:var(--primary);min-width:28px;text-align:right;}

/* IZIN */
.izin-item{display:flex;align-items:center;gap:11px;padding:11px 18px;border-bottom:1px solid #f8f9fc;}
.izin-item:last-child{border-bottom:none;}
.izin-av{width:34px;height:34px;border-radius:9px;flex-shrink:0;display:flex;align-items:center;justify-content:center;font-size:12px;font-weight:800;color:#fff;}
.izin-name{font-size:12.5px;font-weight:700;color:var(--text);line-height:1.3;}
.izin-type{font-size:11px;color:var(--muted2);font-weight:500;margin-top:1px;}
.izin-status{font-size:10.5px;font-weight:700;padding:3px 9px;border-radius:100px;white-space:nowrap;}
.is-pending{background:var(--warning-l);color:var(--warning);}
.is-approved{background:var(--success-l);color:var(--success);}
.izin-actions{display:flex;gap:4px;margin-top:4px;}
.izin-actions button{font-size:10px;font-weight:700;padding:3px 9px;border-radius:6px;border:none;cursor:pointer;transition:opacity .15s;}
.izin-actions button:hover{opacity:.8;}
.btn-setuju{background:#dcfce7;color:#065f46;}
.btn-tolak{background:#fee2e2;color:#991b1b;}

/* ACTIVITY FEED */
.feed-item{display:flex;align-items:flex-start;gap:11px;padding:13px 18px;border-bottom:1px solid #f8f9fc;transition:background .12s;}
.feed-item:last-child{border-bottom:none;}
.feed-item:hover{background:#fafbff;}
.feed-ico{width:32px;height:32px;border-radius:9px;flex-shrink:0;display:flex;align-items:center;justify-content:center;margin-top:1px;}
.feed-ico svg{width:14px;height:14px;}
.fi-g{background:#dcfce7;} .fi-v{background:#ede9fe;} .fi-s{background:#dbeafe;} .fi-a{background:#fef9c3;} .fi-r{background:#fee2e2;}
.feed-body{flex:1;min-width:0;}
.feed-lbl{font-size:12.5px;font-weight:700;color:var(--text);}
.feed-sub{font-size:11px;color:var(--muted2);font-weight:500;margin-top:2px;}
.feed-time{font-size:10px;font-weight:700;color:#cbd5e1;white-space:nowrap;padding-top:2px;}

/* ANIMATIONS */
@keyframes fadeUp{from{opacity:0;transform:translateY(16px)}to{opacity:1;transform:translateY(0)}}
.fu1{animation:fadeUp .4s .00s ease both;}
.fu2{animation:fadeUp .4s .07s ease both;}
.fu3{animation:fadeUp .4s .12s ease both;}
.fu4{animation:fadeUp .4s .17s ease both;}

@media(max-width:1100px){.ins-cols{grid-template-columns:1fr;}}
@media(max-width:900px){.kpi-row{grid-template-columns:repeat(2,1fr);}.ins-cols-3{grid-template-columns:1fr;}.qa-grid{grid-template-columns:repeat(4,1fr);}}
@media(max-width:640px){.ins-wrap{padding:16px 16px 40px;}.hero-right{display:none;}.kpi-row{grid-template-columns:repeat(2,1fr);}.qa-grid{grid-template-columns:repeat(2,1fr);}}
</style>
@endpush

@php
    $user     = Auth::user();
    $instansi = $user->instansi ?? null;

    $totalKaryawan    = \App\Models\Karyawan::where('instansi_id', $instansi?->id)->count();
    $hadirHariIni     = \App\Models\Absensi::whereHas('karyawan', fn($q)=>$q->where('instansi_id',$instansi?->id))
                         ->whereDate('tanggal', today())->whereNotNull('jam_masuk')->count();
    $terlambatHariIni = \App\Models\Absensi::whereHas('karyawan', fn($q)=>$q->where('instansi_id',$instansi?->id))
                         ->whereDate('tanggal', today())->where('status_waktu','terlambat')->count();
    $belumAbsen       = max(0, $totalKaryawan - $hadirHariIni);

    $absensiHariIni = \App\Models\Absensi::with(['karyawan.jabatan','karyawan.user'])
        ->whereHas('karyawan', fn($q)=>$q->where('instansi_id',$instansi?->id))
        ->whereDate('tanggal', today())->latest('jam_masuk')->limit(8)->get();

    $topKaryawan = \App\Models\Absensi::selectRaw('karyawan_id, count(*) as total')
        ->whereHas('karyawan', fn($q)=>$q->where('instansi_id',$instansi?->id))
        ->whereMonth('tanggal', now()->month)->whereNotNull('jam_masuk')
        ->groupBy('karyawan_id')->orderByDesc('total')->limit(5)
        ->with('karyawan.jabatan')->get();
    $maxHadir = $topKaryawan->first()?->total ?: 1;

    $karyawanList = \App\Models\Karyawan::with('jabatan')->where('instansi_id', $instansi?->id)->latest()->limit(6)->get();
    $jamKerja     = $instansi?->jamKerja;

    // Izin pending
    $izinPending = \App\Models\IzinSakit::with('karyawan.user')
        ->whereHas('karyawan', fn($q)=>$q->where('instansi_id',$instansi?->id))
        ->where('status','pending')->latest()->limit(5)->get();

    // Chart data 7 hari
    $chartData = [];
    for($i=6; $i>=0; $i--){
        $date = now()->subDays($i);
        $chartData[] = [
            'day'   => $date->isoFormat('ddd'),
            'count' => \App\Models\Absensi::whereHas('karyawan',fn($q)=>$q->where('instansi_id',$instansi?->id))
                        ->whereDate('tanggal',$date)->whereNotNull('jam_masuk')->count(),
            'today' => $date->isToday(),
        ];
    }
    $maxChart = max(array_column($chartData,'count') ?: [1]);

    // Lembur pending
    $lemburPendingCount = class_exists('\App\Models\Lembur')
        ? \App\Models\Lembur::whereHas('karyawan', fn($q)=>$q->where('instansi_id',$instansi?->id))->where('status','pending')->count()
        : 0;

    // Cuti pending
    $cutiPendingCount = class_exists('\App\Models\Cuti')
        ? \App\Models\Cuti::whereHas('karyawan', fn($q)=>$q->where('instansi_id',$instansi?->id))->where('status','pending')->count()
        : 0;
@endphp

<div class="ins-wrap">

{{-- HERO --}}
<div class="ins-hero fu1">
    <div class="hero-orb hero-orb-1"></div>
    <div class="hero-orb hero-orb-2"></div>
    <div class="hero-left">
        <div class="hero-badge">
            <svg viewBox="0 0 24 24" fill="none" stroke-width="2.5" stroke-linecap="round" stroke-linejoin="round"><path d="M3 9l9-7 9 7v11a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2z"/></svg>
            Dashboard Instansi
        </div>
        <h2 class="hero-title"><span>{{ $instansi?->nama_instansi ?? 'Instansi' }}</span></h2>
        <div class="hero-pills">
            <span class="hpill">
                <svg viewBox="0 0 24 24" fill="none" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><circle cx="12" cy="12" r="10"/><polyline points="12 6 12 12 16 14"/></svg>
                <span id="live-clock">--:--:--</span>
            </span>
            <span class="hpill">
                <svg viewBox="0 0 24 24" fill="none" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><rect x="3" y="4" width="18" height="18" rx="2"/><line x1="16" y1="2" x2="16" y2="6"/><line x1="8" y1="2" x2="8" y2="6"/><line x1="3" y1="10" x2="21" y2="10"/></svg>
                <span id="live-date">--</span>
            </span>
            <span class="hpill green">
                <svg viewBox="0 0 24 24" fill="none" stroke-width="2.5" stroke-linecap="round" stroke-linejoin="round"><path d="M22 11.08V12a10 10 0 1 1-5.93-9.14"/><polyline points="22 4 12 14.01 9 11.01"/></svg>
                Sistem Online
            </span>
        </div>
    </div>
    <div class="hero-right">
        <div class="hero-stat"><div class="hero-stat-n">{{ $totalKaryawan }}</div><div class="hero-stat-l">Karyawan</div></div>
        <div class="hero-stat"><div class="hero-stat-n">{{ $hadirHariIni }}</div><div class="hero-stat-l">Hadir</div></div>
        <div class="hero-stat"><div class="hero-stat-n">{{ $belumAbsen }}</div><div class="hero-stat-l">Belum</div></div>
    </div>
</div>

{{-- KPI --}}
<div class="kpi-row fu2">
    <div class="kpi-card kv">
        <div class="kpi-top">
            <div class="kpi-ico kv"><svg viewBox="0 0 24 24" fill="none" stroke="#6d28d9" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M17 21v-2a4 4 0 0 0-4-4H5a4 4 0 0 0-4 4v2"/><circle cx="9" cy="7" r="4"/><path d="M23 21v-2a4 4 0 0 0-3-3.87"/><path d="M16 3.13a4 4 0 0 1 0 7.75"/></svg></div>
            <span class="kpi-tag tag-v">Total</span>
        </div>
        <div class="kpi-num">{{ $totalKaryawan }}</div><div class="kpi-lbl">Total Karyawan</div>
    </div>
    <div class="kpi-card ks">
        <div class="kpi-top">
            <div class="kpi-ico ks"><svg viewBox="0 0 24 24" fill="none" stroke="#059669" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M22 11.08V12a10 10 0 1 1-5.93-9.14"/><polyline points="22 4 12 14.01 9 11.01"/></svg></div>
            <span class="kpi-tag tag-s">Hari Ini</span>
        </div>
        <div class="kpi-num">{{ $hadirHariIni }}</div><div class="kpi-lbl">Hadir Hari Ini</div>
    </div>
    <div class="kpi-card ka">
        <div class="kpi-top">
            <div class="kpi-ico ka"><svg viewBox="0 0 24 24" fill="none" stroke="#d97706" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><circle cx="12" cy="12" r="10"/><line x1="12" y1="8" x2="12" y2="12"/><line x1="12" y1="16" x2="12.01" y2="16"/></svg></div>
            <span class="kpi-tag tag-a">Hari Ini</span>
        </div>
        <div class="kpi-num">{{ $terlambatHariIni }}</div><div class="kpi-lbl">Terlambat</div>
    </div>
    <div class="kpi-card kr">
        <div class="kpi-top">
            <div class="kpi-ico kr"><svg viewBox="0 0 24 24" fill="none" stroke="#e11d48" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><circle cx="12" cy="12" r="10"/><line x1="15" y1="9" x2="9" y2="15"/><line x1="9" y1="9" x2="15" y2="15"/></svg></div>
            <span class="kpi-tag tag-r">Hari Ini</span>
        </div>
        <div class="kpi-num">{{ $belumAbsen }}</div><div class="kpi-lbl">Belum Absen</div>
    </div>
</div>

{{-- QUICK ACTIONS — 4 kolom rapi --}}
<div class="panel fu2" style="margin-bottom:22px;">
    <div class="panel-hd">
        <span class="panel-hd-title">Aksi Cepat</span>
    </div>
    <div class="qa-grid">
        <a href="{{ route('instansi.karyawan.index') }}" class="qa-btn">
            <div class="qa-ico q-v"><svg viewBox="0 0 24 24" fill="none" stroke="white" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M17 21v-2a4 4 0 0 0-4-4H5a4 4 0 0 0-4 4v2"/><circle cx="9" cy="7" r="4"/><path d="M23 21v-2a4 4 0 0 0-3-3.87"/><path d="M16 3.13a4 4 0 0 1 0 7.75"/></svg></div>
            <span class="qa-lbl">Kelola Karyawan</span>
        </a>
        <a href="{{ route('instansi.absensi.index') }}" class="qa-btn">
            <div class="qa-ico q-b"><svg viewBox="0 0 24 24" fill="none" stroke="white" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><line x1="18" y1="20" x2="18" y2="10"/><line x1="12" y1="20" x2="12" y2="4"/><line x1="6" y1="20" x2="6" y2="14"/></svg></div>
            <span class="qa-lbl">Monitoring Absensi</span>
        </a>
        <a href="{{ route('instansi.rekap.index') }}" class="qa-btn">
            <div class="qa-ico q-a"><svg viewBox="0 0 24 24" fill="none" stroke="white" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><rect x="3" y="4" width="18" height="18" rx="2"/><line x1="16" y1="2" x2="16" y2="6"/><line x1="8" y1="2" x2="8" y2="6"/><line x1="3" y1="10" x2="21" y2="10"/></svg></div>
            <span class="qa-lbl">Rekap Bulanan</span>
        </a>
        <a href="{{ route('instansi.pengaturan.index') }}" class="qa-btn">
            <div class="qa-ico q-sl"><svg viewBox="0 0 24 24" fill="none" stroke="white" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><circle cx="12" cy="12" r="3"/><path d="M19.4 15a1.65 1.65 0 0 0 .33 1.82l.06.06a2 2 0 0 1-2.83 2.83l-.06-.06a1.65 1.65 0 0 0-1.82-.33 1.65 1.65 0 0 0-1 1.51V21a2 2 0 0 1-4 0v-.09A1.65 1.65 0 0 0 9 19.4a1.65 1.65 0 0 0-1.82.33l-.06.06a2 2 0 0 1-2.83-2.83l.06-.06A1.65 1.65 0 0 0 4.68 15a1.65 1.65 0 0 0-1.51-1H3a2 2 0 0 1 0-4h.09A1.65 1.65 0 0 0 4.6 9a1.65 1.65 0 0 0-.33-1.82l-.06-.06a2 2 0 0 1 2.83-2.83l.06.06A1.65 1.65 0 0 0 9 4.68a1.65 1.65 0 0 0 1-1.51V3a2 2 0 0 1 4 0v.09a1.65 1.65 0 0 0 1 1.51 1.65 1.65 0 0 0 1.82-.33l.06-.06a2 2 0 0 1 2.83 2.83l-.06.06A1.65 1.65 0 0 0 19.4 9a1.65 1.65 0 0 0 1.51 1H21a2 2 0 0 1 0 4h-.09a1.65 1.65 0 0 0-1.51 1z"/></svg></div>
            <span class="qa-lbl">Pengaturan</span>
        </a>
        <a href="{{ route('instansi.izin.index') }}" class="qa-btn" style="position:relative;">
            <div class="qa-ico q-r"><svg viewBox="0 0 24 24" fill="none" stroke="white" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M9 11l3 3L22 4"/><path d="M21 12v7a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2V5a2 2 0 0 1 2-2h11"/></svg></div>
            <span class="qa-lbl">Kelola Izin</span>
        </a>
        <a href="{{ route('instansi.cuti.index') }}" class="qa-btn" style="position:relative;">
            <div class="qa-ico q-em"><svg viewBox="0 0 24 24" fill="none" stroke="white" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><rect x="3" y="4" width="18" height="18" rx="2"/><line x1="16" y1="2" x2="16" y2="6"/><line x1="8" y1="2" x2="8" y2="6"/><line x1="3" y1="10" x2="21" y2="10"/><path d="M8 14h.01M12 14h.01M16 14h.01M8 18h.01M12 18h.01"/></svg></div>
            <span class="qa-lbl">Kelola Cuti{{ $cutiPendingCount > 0 ? ' ('.$cutiPendingCount.')' : '' }}</span>
        </a>
        <a href="{{ route('instansi.lembur.index') }}" class="qa-btn" style="position:relative;">
            <div class="qa-ico q-cy"><svg viewBox="0 0 24 24" fill="none" stroke="white" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><circle cx="12" cy="12" r="10"/><polyline points="12 6 12 12 16 14"/><line x1="14.5" y1="18" x2="19" y2="18"/><line x1="19" y1="15" x2="19" y2="21"/></svg></div>
            <span class="qa-lbl">Kelola Lembur{{ $lemburPendingCount > 0 ? ' ('.$lemburPendingCount.')' : '' }}</span>
        </a>
        <a href="{{ route('instansi.karyawan.create') }}" class="qa-btn">
            <div class="qa-ico q-s"><svg viewBox="0 0 24 24" fill="none" stroke="white" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M16 21v-2a4 4 0 0 0-4-4H6a4 4 0 0 0-4 4v2"/><circle cx="9" cy="7" r="4"/><line x1="19" y1="8" x2="19" y2="14"/><line x1="22" y1="11" x2="16" y2="11"/></svg></div>
            <span class="qa-lbl">Tambah Karyawan</span>
        </a>
    </div>
</div>

{{-- ABSENSI TABLE + CHART --}}
<div class="ins-cols fu3">
    <div class="panel">
        <div class="panel-hd">
            <span class="panel-hd-title">Absensi Hari Ini</span>
            <span class="panel-hd-badge">{{ $hadirHariIni }}/{{ $totalKaryawan }}</span>
        </div>
        <table class="abs-table">
            <thead>
                <tr>
                    <th>Karyawan</th>
                    <th>Jam Masuk</th>
                    <th>Status</th>
                    <th>Aksi</th>
                </tr>
            </thead>
            <tbody>
                @forelse($absensiHariIni as $ab)
                @php
                    $namaKaryawan = $ab->karyawan->user->name ?? $ab->karyawan->nama ?? '-';
                    $inisial      = strtoupper(substr($namaKaryawan, 0, 1));
                    $avatarColors = ['#6d28d9','#0284c7','#059669','#d97706','#dc2626','#475569'];
                    $avatarColor  = $avatarColors[ord($inisial) % count($avatarColors)];
                    $hasLokasi    = !empty($ab->latitude_masuk) && !empty($ab->longitude_masuk);
                    $mapsUrl      = $hasLokasi ? "https://maps.google.com/?q={$ab->latitude_masuk},{$ab->longitude_masuk}" : null;
                @endphp
                <tr>
                    <td>
                        <div class="kar-cell">
                            <div class="kar-av" style="background:{{ $avatarColor }};">{{ $inisial }}</div>
                            <div>
                                <div class="kar-name">{{ $namaKaryawan }}</div>
                                <div class="kar-jab" style="display:flex;align-items:center;gap:5px;">
                                    {{ $ab->karyawan->jabatan->nama ?? $ab->karyawan->jabatan->nama_jabatan ?? '-' }}
                                    @if($mapsUrl)
                                    <a href="{{ $mapsUrl }}" target="_blank" style="display:inline-flex;color:#0284c7;">
                                        <svg width="11" height="11" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2.5" stroke-linecap="round" stroke-linejoin="round"><path d="M21 10c0 7-9 13-9 13s-9-6-9-13a9 9 0 0 1 18 0z"/><circle cx="12" cy="10" r="3"/></svg>
                                    </a>
                                    @endif
                                </div>
                            </div>
                        </div>
                    </td>
                    <td><span class="time-pill {{ $ab->status_waktu === 'terlambat' ? 'late' : 'ok' }}">{{ $ab->jam_masuk ? \Carbon\Carbon::parse($ab->jam_masuk)->format('H:i') : '—' }}</span></td>
                    <td>
                        @if($ab->status_waktu === 'tepat_waktu')<span class="status-dot sd-hadir">Hadir</span>
                        @elseif($ab->status_waktu === 'terlambat')<span class="status-dot sd-terlambat">+{{ $ab->terlambat_menit }}m</span>
                        @else<span class="status-dot sd-absen">—</span>@endif
                    </td>
                    <td>
                        @if(Route::has('instansi.karyawan.show'))
                            <a href="{{ route('instansi.karyawan.show', $ab->karyawan->id) }}" class="tbl-action">Detail</a>
                        @else
                            <a href="#" class="tbl-action">Detail</a>
                        @endif
                    </td>
                </tr>
                @empty
                <tr><td colspan="4">
                    <div class="empty-tbl">
                        <svg width="44" height="44" viewBox="0 0 24 24" fill="none" stroke-width="1.3" stroke-linecap="round" stroke-linejoin="round"><path d="M9 5H7a2 2 0 0 0-2 2v12a2 2 0 0 0 2 2h10a2 2 0 0 0 2-2V7a2 2 0 0 0-2-2h-2"/><rect x="9" y="3" width="6" height="4" rx="1"/></svg>
                        <p>Belum ada absensi hari ini</p>
                    </div>
                </td></tr>
                @endforelse
            </tbody>
        </table>
        <div class="panel-ft"><a href="{{ route('instansi.absensi.index') }}" class="panel-hd-link">Lihat semua absensi →</a></div>
    </div>

    <div style="display:flex;flex-direction:column;gap:20px;">
        {{-- CHART --}}
        <div class="panel">
            <div class="panel-hd">
                <span class="panel-hd-title">Kehadiran 7 Hari</span>
                <span class="panel-hd-badge">{{ now()->isoFormat('MMM YYYY') }}</span>
            </div>
            <div class="chart-wrap">
                <div class="chart-bars">
                    @foreach($chartData as $cd)
                    <div class="bar-col">
                        <div class="bar-bg" style="height:90px;">
                            <div class="bar-fill {{ $cd['today'] ? 'today' : '' }}" style="height:{{ $maxChart > 0 ? round(($cd['count']/$maxChart)*90) : 0 }}px;"></div>
                        </div>
                        <span class="bar-day">{{ $cd['day'] }}</span>
                    </div>
                    @endforeach
                </div>
                <div class="chart-legend">
                    <div class="chart-leg-item"><div class="chart-leg-dot" style="background:#6d28d9;"></div> Normal</div>
                    <div class="chart-leg-item"><div class="chart-leg-dot" style="background:#059669;"></div> Hari Ini</div>
                </div>
            </div>
        </div>

        {{-- JADWAL KERJA --}}
        @if($jamKerja)
        <div class="panel">
            <div class="panel-hd"><span class="panel-hd-title">Jadwal Kerja</span></div>
            <div class="jadwal-card">
                <div class="jadwal-ico"><svg viewBox="0 0 24 24" fill="none" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><circle cx="12" cy="12" r="10"/><polyline points="12 6 12 12 16 14"/></svg></div>
                <div>
                    <div class="jadwal-name">Shift Reguler</div>
                    <div class="jadwal-times">
                        <div class="jadwal-time">Masuk<strong>{{ \Carbon\Carbon::parse($jamKerja->jam_masuk)->format('H:i') }}</strong></div>
                        <div class="jadwal-time">Pulang<strong>{{ \Carbon\Carbon::parse($jamKerja->jam_pulang)->format('H:i') }}</strong></div>
                    </div>
                    <span class="jadwal-tol">Toleransi {{ $jamKerja->toleransi_menit }} menit</span>
                </div>
            </div>
        </div>
        @endif
    </div>
</div>

{{-- BOTTOM ROW --}}
<div class="ins-cols-3 fu4">

    {{-- TOP KARYAWAN --}}
    <div class="panel">
        <div class="panel-hd">
            <span class="panel-hd-title">Karyawan Paling Rajin</span>
            <span class="panel-hd-badge">{{ now()->isoFormat('MMMM') }}</span>
        </div>
        @forelse($topKaryawan as $i => $tk)
        <div class="top-item">
            <div class="top-rank {{ $i===0?'rank-1':($i===1?'rank-2':($i===2?'rank-3':'rank-n')) }}">{{ $i+1 }}</div>
            <div>
                <div class="top-name">{{ $tk->karyawan->user->name ?? $tk->karyawan->nama ?? '-' }}</div>
                <div style="font-size:11px;color:var(--muted2);font-weight:500;">{{ $tk->karyawan->jabatan->nama ?? $tk->karyawan->jabatan->nama_jabatan ?? '-' }}</div>
            </div>
            <div class="top-bar-wrap"><div class="top-bar-fill" style="width:{{ round(($tk->total/$maxHadir)*100) }}%;"></div></div>
            <span class="top-count">{{ $tk->total }}x</span>
        </div>
        @empty
        <div class="empty-tbl"><p style="padding:24px 0;">Belum ada data bulan ini</p></div>
        @endforelse
    </div>

    {{-- DAFTAR KARYAWAN --}}
    <div class="panel">
        <div class="panel-hd">
            <span class="panel-hd-title">Daftar Karyawan</span>
            <a href="{{ route('instansi.karyawan.index') }}" class="panel-hd-link">Lihat semua →</a>
        </div>
        @forelse($karyawanList as $i => $kar)
        @php
            $colors = ['linear-gradient(135deg,#6d28d9,#8b5cf6)','linear-gradient(135deg,#0284c7,#38bdf8)','linear-gradient(135deg,#059669,#34d399)','linear-gradient(135deg,#d97706,#fbbf24)','linear-gradient(135deg,#dc2626,#f87171)','linear-gradient(135deg,#475569,#94a3b8)'];
        @endphp
        <div class="kar-sum-item">
            <div class="ksav" style="background:{{ $colors[$i%count($colors)] }};">{{ strtoupper(substr($kar->user->name ?? $kar->nama ?? '?',0,1)) }}</div>
            <div style="flex:1;min-width:0;">
                <div class="ks-name">{{ $kar->user->name ?? $kar->nama ?? '-' }}</div>
                <div class="ks-jab">{{ $kar->jabatan->nama_jabatan ?? $kar->jabatan->nama ?? 'Karyawan' }}</div>
            </div>
            <a href="{{ route('instansi.karyawan.show', $kar) }}" class="tbl-action">Detail</a>
        </div>
        @empty
        <div class="empty-tbl"><p style="padding:24px 0;">Belum ada karyawan</p></div>
        @endforelse
    </div>

    {{-- IZIN PENDING + AKTIVITAS --}}
    <div style="display:flex;flex-direction:column;gap:20px;">

        {{-- IZIN PENDING --}}
        <div class="panel">
            <div class="panel-hd">
                <span class="panel-hd-title">Pengajuan Izin</span>
                @if($izinPending->count())
                <span class="panel-hd-badge" style="background:#fef9c3;color:#92400e;">{{ $izinPending->count() }} Pending</span>
                @endif
            </div>
            @forelse($izinPending as $i => $iz)
            <div class="izin-item" id="dash-izin-{{ $iz->id }}">
                <div class="izin-av" style="background:{{ ['linear-gradient(135deg,#f87171,#dc2626)','linear-gradient(135deg,#fb923c,#d97706)','linear-gradient(135deg,#a78bfa,#7c3aed)','linear-gradient(135deg,#34d399,#059669)','linear-gradient(135deg,#38bdf8,#0284c7)'][$i%5] }};">{{ strtoupper(substr($iz->karyawan->user->name ?? $iz->karyawan->nama ?? '?',0,1)) }}</div>
                <div style="flex:1;min-width:0;">
                    <div class="izin-name">{{ $iz->karyawan->user->name ?? $iz->karyawan->nama ?? '-' }}</div>
                    <div class="izin-type">{{ $iz->jenis ?? 'Izin' }} · {{ \Carbon\Carbon::parse($iz->tanggal_mulai)->isoFormat('D MMM') }}</div>
                    @if($iz->status === 'pending')
                    <div class="izin-actions">
                        <button class="btn-setuju" onclick="dashIzin({{ $iz->id }},'approve',this)">✓ Setuju</button>
                        <button class="btn-tolak"  onclick="dashIzin({{ $iz->id }},'reject',this)">✗ Tolak</button>
                    </div>
                    @endif
                </div>
                <span class="izin-status {{ $iz->status === 'disetujui' ? 'is-approved' : 'is-pending' }}" id="dash-izin-status-{{ $iz->id }}">{{ $iz->status === 'disetujui' ? 'Disetujui' : 'Pending' }}</span>
            </div>
            @empty
            <div style="padding:24px 20px;text-align:center;">
                <p style="font-size:12px;color:var(--muted2);font-weight:600;">Belum ada pengajuan izin pending</p>
            </div>
            @endforelse
            <div class="panel-ft"><a href="{{ route('instansi.izin.index') }}" class="panel-hd-link">Lihat semua →</a></div>
        </div>

        {{-- AKTIVITAS --}}
        <div class="panel">
            <div class="panel-hd"><span class="panel-hd-title">Aktivitas Terkini</span></div>
            <div class="feed-item">
                <div class="feed-ico fi-g"><svg viewBox="0 0 24 24" fill="none" stroke="#059669" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M22 11.08V12a10 10 0 1 1-5.93-9.14"/><polyline points="22 4 12 14.01 9 11.01"/></svg></div>
                <div class="feed-body"><div class="feed-lbl">{{ $hadirHariIni }} karyawan hadir</div><div class="feed-sub">Absensi hari ini</div></div>
                <span class="feed-time">Hari ini</span>
            </div>
            @if($cutiPendingCount > 0)
            <div class="feed-item">
                <div class="feed-ico fi-v"><svg viewBox="0 0 24 24" fill="none" stroke="#7c3aed" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><rect x="3" y="4" width="18" height="18" rx="2"/><line x1="3" y1="10" x2="21" y2="10"/></svg></div>
                <div class="feed-body"><div class="feed-lbl">{{ $cutiPendingCount }} pengajuan cuti</div><div class="feed-sub">Menunggu persetujuan</div></div>
                <span class="feed-time"><a href="{{ route('instansi.cuti.index') }}" style="color:var(--primary);font-weight:700;font-size:10px;">Review →</a></span>
            </div>
            @endif
            @if($lemburPendingCount > 0)
            <div class="feed-item">
                <div class="feed-ico fi-s"><svg viewBox="0 0 24 24" fill="none" stroke="#0891b2" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><circle cx="12" cy="12" r="10"/><polyline points="12 6 12 12 16 14"/></svg></div>
                <div class="feed-body"><div class="feed-lbl">{{ $lemburPendingCount }} pengajuan lembur</div><div class="feed-sub">Menunggu persetujuan</div></div>
                <span class="feed-time"><a href="{{ route('instansi.lembur.index') }}" style="color:#0891b2;font-weight:700;font-size:10px;">Review →</a></span>
            </div>
            @endif
            @if($terlambatHariIni > 0)
            <div class="feed-item">
                <div class="feed-ico fi-a"><svg viewBox="0 0 24 24" fill="none" stroke="#d97706" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><circle cx="12" cy="12" r="10"/><line x1="12" y1="8" x2="12" y2="12"/><line x1="12" y1="16" x2="12.01" y2="16"/></svg></div>
                <div class="feed-body"><div class="feed-lbl">{{ $terlambatHariIni }} karyawan terlambat</div><div class="feed-sub">Absensi hari ini</div></div>
                <span class="feed-time">Hari ini</span>
            </div>
            @endif
            <div class="feed-item">
                <div class="feed-ico fi-v"><svg viewBox="0 0 24 24" fill="none" stroke="#7c3aed" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M17 21v-2a4 4 0 0 0-4-4H5a4 4 0 0 0-4 4v2"/><circle cx="9" cy="7" r="4"/></svg></div>
                <div class="feed-body"><div class="feed-lbl">{{ $totalKaryawan }} karyawan terdaftar</div><div class="feed-sub">Di instansi ini</div></div>
                <span class="feed-time">Total</span>
            </div>
        </div>
    </div>
</div>

</div>

@push('scripts')
<script>
function tick(){
    const n=new Date(),p=x=>String(x).padStart(2,'0');
    const ec=document.getElementById('live-clock'),ed=document.getElementById('live-date');
    if(ec) ec.textContent=`${p(n.getHours())}:${p(n.getMinutes())}:${p(n.getSeconds())}`;
    if(ed) ed.textContent=n.toLocaleDateString('id-ID',{weekday:'long',day:'numeric',month:'long',year:'numeric'});
}
tick(); setInterval(tick,1000);

async function dashIzin(id, action, btn) {
    btn.disabled = true;
    try {
        const res = await fetch(`/instansi/izin/${id}/${action}`, {
            method:'POST',
            headers:{'X-CSRF-TOKEN':document.querySelector('meta[name="csrf-token"]')?.content,'Accept':'application/json'}
        });
        const data = await res.json();
        if(data.success){
            const row=document.getElementById(`dash-izin-${id}`);
            if(row){
                row.querySelector('.izin-actions')?.remove();
                const s=document.getElementById(`dash-izin-status-${id}`);
                if(s){s.textContent=action==='approve'?'Disetujui':'Ditolak';s.className='izin-status '+(action==='approve'?'is-approved':'');if(action==='reject')s.style.cssText='background:#fee2e2;color:#991b1b;';}
            }
        }
    } catch(e){}
}
</script>
@endpush

</x-app-layout>