<?php defined('BASEPATH') OR exit('No direct script access allowed'); ?>

<style>
/* ─── DESIGN TOKENS ──────────────────────────────────────── */
:root {
  --teal-900: #0d3d38;
  --teal-800: #0b5a52;
  --teal-700: #0b7a6e;
  --teal-600: #0d9488;
  --teal-500: #14b8a6;
  --teal-400: #2dd4bf;
  --teal-100: #ccfbf1;
  --teal-50:  #f0fdfa;
  --green-500: #10b981;
  --green-600: #059669;
  --amber-400: #fbbf24;
  --amber-500: #f59e0b;
  --amber-600: #d97706;
  --amber-50:  #fffbeb;
  --amber-100: #fef3c7;
  --red-400:  #f87171;
  --red-500:  #ef4444;
  --red-600:  #dc2626;
  --blue-500: #3b82f6;
  --blue-600: #2563eb;
  --slate-50: #f8fafc;
  --slate-100:#f1f5f9;
  --slate-200:#e2e8f0;
  --slate-400:#94a3b8;
  --slate-500:#64748b;
  --slate-700:#334155;
  --slate-900:#0f172a;
  --card-shadow: 0 1px 3px rgba(0,0,0,.06), 0 4px 20px rgba(0,0,0,.06);

  --radius:    14px;
  --radius-sm: 9px;
  --radius-xs: 6px;
  --shadow:    0 4px 24px rgba(13,148,136,0.10), 0 1px 4px rgba(0,0,0,0.05);
  --shadow-sm: var(--card-shadow);
}

/* ─── WRAPPER ────────────────────────────────────────────── */
.ps-wrapper {
  max-width: 1400px;
  margin: 0 auto;
  padding: 0 28px 60px;
}

/* ─── PAGE HEADER ────────────────────────────────────────── */
.ps-page-header {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  margin-bottom: 24px;
  flex-wrap: wrap;
  gap: 12px;
}
.ps-header-left h4 {
  font-size: 1.2rem;
  font-weight: 800;
  color: var(--teal-800);
  margin: 0 0 5px;
  display: flex;
  align-items: center;
  gap: 11px;
  letter-spacing: -.3px;
}
.ps-header-left h4 .hi {
  width: 40px; height: 40px;
  border-radius: 11px;
  background: linear-gradient(135deg, var(--teal-800), var(--teal-500));
  display: flex; align-items: center; justify-content: center;
  color: #fff; font-size: 1rem;
  box-shadow: 0 4px 12px rgba(13,148,136,0.32);
  flex-shrink: 0;
}
.ps-breadcrumb {
  display: flex;
  align-items: center;
  gap: 6px;
  font-size: 0.77rem;
  color: var(--slate-400);
  padding-left: 51px;
}
.ps-breadcrumb a { color: var(--teal-600); text-decoration: none; font-weight: 600; }
.ps-breadcrumb a:hover { text-decoration: underline; }
.ps-breadcrumb .sep { color: var(--slate-200); }

.btn-tambah-peserta {
  display: inline-flex;
  align-items: center;
  gap: 7px;
  padding: 10px 22px;
  border-radius: 50px;
  border: none;
  background: linear-gradient(135deg, var(--teal-800), var(--teal-600));
  color: #fff;
  font-size: 0.85rem;
  font-weight: 700;
  text-decoration: none;
  box-shadow: 0 3px 14px rgba(13,148,136,0.36);
  transition: all .2s;
  white-space: nowrap;
}
.btn-tambah-peserta:hover {
  background: linear-gradient(135deg, var(--teal-900), var(--teal-700));
  box-shadow: 0 6px 18px rgba(13,148,136,0.44);
  transform: translateY(-1px);
  color: #fff;
}

/* ─── FLASH ALERTS ────────────────────────────────────────── */
.flash-alert {
  display: flex;
  align-items: flex-start;
  gap: 10px;
  border-radius: var(--radius-sm);
  padding: 13px 16px;
  margin-bottom: 20px;
  font-size: 0.85rem;
  position: relative;
}
.flash-alert.success {
  background: var(--teal-50);
  border-left: 4px solid var(--teal-600);
  color: var(--teal-800);
}
.flash-alert.error {
  background: #fef2f2;
  border-left: 4px solid var(--red-500);
  color: #7f1d1d;
}
.flash-alert .fa-close {
  margin-left: auto;
  cursor: pointer;
  font-size: 1rem;
  opacity: .55;
  background: none;
  border: none;
  color: inherit;
  padding: 0;
  line-height: 1;
}
.flash-alert .fa-close:hover { opacity: 1; }

/* ─── INFO ARISAN CARD ───────────────────────────────────── */
.arisan-info-card {
  background: #fff;
  border-radius: var(--radius);
  box-shadow: var(--shadow-sm);
  border: 1.5px solid var(--slate-100);
  padding: 22px 26px;
  margin-bottom: 18px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  flex-wrap: wrap;
  gap: 18px;
}
.aic-left h5 {
  font-size: 1.05rem;
  font-weight: 800;
  color: var(--teal-800);
  margin: 0 0 12px;
  display: flex;
  align-items: center;
  gap: 8px;
}
.aic-left h5 i { color: var(--teal-500); }
.aic-meta {
  display: flex;
  flex-wrap: wrap;
  gap: 20px;
}
.aic-meta-item .mi-label {
  font-size: 0.71rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: .5px;
  color: var(--slate-400);
  margin-bottom: 3px;
}
.aic-meta-item .mi-value {
  font-size: 0.92rem;
  font-weight: 700;
  color: var(--slate-900);
}
.aic-meta-item .mi-value.teal { color: var(--teal-700); }

.periode-pill {
  display: inline-block;
  border-radius: 50px;
  padding: 3px 12px;
  font-size: 0.72rem;
  font-weight: 700;
}
.periode-pill.bulanan    { background: var(--teal-50);  color: var(--teal-700); border: 1px solid var(--teal-100); }
.periode-pill.mingguan   { background: var(--amber-50); color: var(--amber-600); border: 1px solid var(--amber-100); }
.periode-pill.dua-minggu { background: #f5f3ff; color: #6d28d9; border: 1px solid #ede9fe; }

.aic-actions {
  display: flex;
  gap: 8px;
  flex-shrink: 0;
}
.btn-outline-back {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 8px 16px;
  border-radius: 50px;
  border: 1.5px solid var(--slate-200);
  background: #fff;
  color: var(--slate-700);
  font-size: 0.82rem;
  font-weight: 600;
  text-decoration: none;
  transition: all .2s;
  white-space: nowrap;
}
.btn-outline-back:hover {
  border-color: var(--teal-400);
  color: var(--teal-700);
  background: var(--teal-50);
}
.btn-outline-pertemuan {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 8px 16px;
  border-radius: 50px;
  border: 1.5px solid var(--teal-200, #99f6e4);
  background: var(--teal-50);
  color: var(--teal-700);
  font-size: 0.82rem;
  font-weight: 600;
  text-decoration: none;
  transition: all .2s;
  white-space: nowrap;
}
.btn-outline-pertemuan:hover {
  background: var(--teal-100);
  border-color: var(--teal-500);
  color: var(--teal-800);
}

/* ─── MAIN CARD ───────────────────────────────────────────── */
.ps-card {
  background: #fff;
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  overflow: hidden;
  border: 1.5px solid var(--slate-100);
  margin-bottom: 18px;
}
.ps-card-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 18px 24px;
  border-bottom: 1.5px solid var(--slate-100);
  background: var(--slate-50);
  flex-wrap: wrap;
  gap: 10px;
}
.ps-card-header h5 {
  font-size: 0.92rem;
  font-weight: 700;
  color: var(--teal-800);
  margin: 0;
  display: flex;
  align-items: center;
  gap: 8px;
}
.ps-card-header h5 i { color: var(--teal-500); }

/* ─── EMPTY STATE ────────────────────────────────────────── */
.empty-state {
  text-align: center;
  padding: 60px 24px;
}
.empty-state-icon {
  width: 72px; height: 72px;
  border-radius: 50%;
  background: var(--teal-50);
  display: flex; align-items: center; justify-content: center;
  margin: 0 auto 16px;
  font-size: 2rem;
}
.empty-state h6 { font-weight: 700; color: var(--slate-700); margin-bottom: 5px; }
.empty-state p  { font-size: 0.84rem; color: var(--slate-400); margin: 0; }

/* ─── TABLE ───────────────────────────────────────────────── */
.ps-table-wrap { overflow-x: auto; -webkit-overflow-scrolling: touch; }
.ps-table {
  width: 100%;
  border-collapse: collapse;
  min-width: 700px;
}
.ps-table thead tr {
  background: var(--slate-50);
  border-bottom: 2px solid var(--slate-200);
}
.ps-table thead th {
  padding: 13px 18px;
  font-size: 0.68rem;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: .65px;
  color: var(--slate-400);
  white-space: nowrap;
}
.ps-table thead th.center { text-align: center; }
.ps-table tbody tr {
  border-bottom: 1px solid var(--slate-100);
  transition: background .15s;
}
.ps-table tbody tr:hover { background: var(--teal-50); }
.ps-table tbody tr:last-child { border-bottom: none; }
.ps-table tbody td {
  padding: 14px 18px;
  font-size: 0.87rem;
  color: var(--slate-700);
  vertical-align: middle;
}
.ps-table tbody td.center { text-align: center; }

/* ─── TABLE CELL PARTS ────────────────────────────────────── */
.nomor-badge {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-width: 36px; height: 36px;
  border-radius: 9px;
  background: linear-gradient(135deg, var(--teal-700), var(--teal-500));
  color: #fff;
  font-size: 0.88rem;
  font-weight: 800;
  box-shadow: 0 2px 8px rgba(13,148,136,0.28);
  padding: 0 8px;
}

.peserta-name { font-weight: 700; color: var(--slate-900); font-size: 0.9rem; }
.nik-cell     { font-size: 0.83rem; color: var(--slate-500); font-family: monospace; }

.wa-link {
  display: inline-flex;
  align-items: center;
  gap: 5px;
  text-decoration: none;
  color: var(--green-600);
  font-size: 0.84rem;
  font-weight: 600;
  transition: color .15s;
}
.wa-link:hover { color: var(--green-500); }
.wa-link i { font-size: 1rem; }

.date-cell { font-size: 0.82rem; color: var(--slate-500); }

.status-pill {
  display: inline-flex;
  align-items: center;
  gap: 5px;
  border-radius: 50px;
  padding: 4px 13px;
  font-size: 0.73rem;
  font-weight: 700;
  white-space: nowrap;
}
.status-pill.aktif   { background: var(--teal-50);   color: var(--teal-700);  border: 1px solid var(--teal-100); }
.status-pill.keluar  { background: var(--slate-100); color: var(--slate-500); border: 1px solid var(--slate-200); }

/* ─── ACTION BUTTON ───────────────────────────────────────── */
.act-btn-del {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 32px; height: 32px;
  border-radius: var(--radius-xs);
  border: 1.5px solid #fecaca;
  color: var(--red-500);
  background: #fef2f2;
  font-size: 0.84rem;
  text-decoration: none;
  cursor: pointer;
  transition: all .18s;
}
.act-btn-del:hover { background: #fecaca; border-color: var(--red-400); color: var(--red-600); }

/* ─── TOTAL DANA CARD ─────────────────────────────────────── */
.dana-card {
  background: #fff;
  border-radius: var(--radius);
  box-shadow: var(--shadow-sm);
  border: 1.5px solid var(--teal-100);
  padding: 22px 26px;
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 0;
}
.dana-item {
  text-align: center;
  padding: 8px 16px;
}
.dana-item + .dana-item {
  border-left: 1.5px solid var(--slate-100);
}
.dana-item .di-label {
  font-size: 0.73rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: .5px;
  color: var(--slate-400);
  margin-bottom: 6px;
}
.dana-item .di-value {
  font-size: 1.5rem;
  font-weight: 800;
  line-height: 1;
  margin-bottom: 4px;
}
.dana-item .di-value.teal   { color: var(--teal-700); }
.dana-item .di-value.green  { color: var(--green-600); }
.dana-item .di-sub {
  font-size: 0.74rem;
  color: var(--slate-400);
}

/* ─── DATATABLES OVERRIDES ───────────────────────────────── */
.ps-dt-top {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 14px 24px 10px;
  flex-wrap: wrap;
  gap: 10px;
}
.ps-dt-bot {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 10px 24px 16px;
  flex-wrap: wrap;
  gap: 10px;
}
.dataTables_filter label,
.dataTables_length label,
.dataTables_info { font-size: 0.82rem; color: var(--slate-500); }
.paginate_button { border-radius: var(--radius-xs) !important; font-size: 0.82rem !important; }
.paginate_button.current {
  background: var(--teal-600) !important;
  border-color: var(--teal-600) !important;
  color: #fff !important;
}

/* ─── MOBILE ─────────────────────────────────────────────── */
@media (max-width: 767px) {
  .ps-wrapper      { padding: 0 12px 48px; }
  .ps-page-header  { flex-direction: column; align-items: flex-start; }
  .arisan-info-card { flex-direction: column; }
  .aic-actions     { width: 100%; }
  .dana-card       { grid-template-columns: 1fr; }
  .dana-item + .dana-item { border-left: none; border-top: 1.5px solid var(--slate-100); }
}
</style>

<div class="ps-wrapper">

  <!-- ── Page Header ───────────────────────────────────────── -->
  <div class="ps-page-header">
    <div class="ps-header-left">
      <h4>
        <span class="hi"><i class="bi bi-people"></i></span>
        Kelola Peserta Arisan
      </h4>
      <div class="ps-breadcrumb">
        <a href="<?php echo base_url('dashboard'); ?>">Dashboard</a>
        <span class="sep">/</span>
        <a href="<?php echo base_url('arisan'); ?>">Arisan</a>
        <span class="sep">/</span>
        <span>Peserta</span>
      </div>
    </div>
    <?php if(is_admin()): ?>
    <a href="<?php echo base_url('arisan/tambah_peserta/' . $arisan->id); ?>" class="btn-tambah-peserta">
      <i class="bi bi-person-plus"></i> Tambah Peserta
    </a>
    <?php endif; ?>
  </div>

  <!-- ── Flash Alerts ──────────────────────────────────────── -->
  <?php if($this->session->flashdata('success')): ?>
  <div class="flash-alert success" id="flashSuccess">
    <i class="bi bi-check-circle-fill"></i>
    <span><?php echo $this->session->flashdata('success'); ?></span>
    <button class="fa-close" onclick="this.parentElement.remove()">✕</button>
  </div>
  <?php endif; ?>
  <?php if($this->session->flashdata('error')): ?>
  <div class="flash-alert error" id="flashError">
    <i class="bi bi-exclamation-triangle-fill"></i>
    <span><?php echo $this->session->flashdata('error'); ?></span>
    <button class="fa-close" onclick="this.parentElement.remove()">✕</button>
  </div>
  <?php endif; ?>

  <!-- ── Info Arisan Card ───────────────────────────────────── -->
  <div class="arisan-info-card">
    <div class="aic-left">
      <h5><i class="bi bi-gift-fill"></i><?php echo $arisan->nama_arisan; ?></h5>
      <div class="aic-meta">
        <div class="aic-meta-item">
          <div class="mi-label">Iuran per Pertemuan</div>
          <div class="mi-value teal">Rp <?php echo number_format($arisan->nominal_iuran, 0, ',', '.'); ?></div>
        </div>
        <div class="aic-meta-item">
          <div class="mi-label">Periode</div>
          <div class="mi-value">
            <?php
            $pc = 'bulanan';
            if($arisan->periode == 'mingguan')   $pc = 'mingguan';
            elseif($arisan->periode == 'dua_minggu') $pc = 'dua-minggu';
            ?>
            <span class="periode-pill <?php echo $pc; ?>"><?php echo ucfirst(str_replace('_',' ',$arisan->periode)); ?></span>
          </div>
        </div>
        <div class="aic-meta-item">
          <div class="mi-label">Total Peserta</div>
          <div class="mi-value"><?php echo $total_peserta; ?> Orang</div>
        </div>
      </div>
    </div>
    <div class="aic-actions">
      <a href="<?php echo base_url('arisan'); ?>" class="btn-outline-back">
        <i class="bi bi-arrow-left"></i> Kembali
      </a>
      <a href="<?php echo base_url('arisan/pertemuan/' . $arisan->id); ?>" class="btn-outline-pertemuan">
        <i class="bi bi-calendar-event"></i> Pertemuan
      </a>
    </div>
  </div>

  <!-- ── Daftar Peserta Card ────────────────────────────────── -->
  <div class="ps-card">
    <div class="ps-card-header">
      <h5><i class="bi bi-people-fill"></i> Daftar Peserta <span style="color:var(--slate-400);font-weight:500">(<?php echo $total_peserta; ?>)</span></h5>
    </div>

    <?php if(empty($peserta)): ?>
      <div class="empty-state">
       <div class="empty-state-icon"><i class="bi bi-people-fill"></i></div>
        <h6>Belum Ada Peserta</h6>
        <p>Tambahkan peserta untuk memulai arisan ini.</p>
      </div>
    <?php else: ?>

      <div class="ps-table-wrap">
        <table class="ps-table" id="tablePeserta">
          <thead>
            <tr>
              <th class="center" style="width:70px">No Urut</th>
              <th>NIK</th>
              <th>Nama Lengkap</th>
              <th>No. HP</th>
              <th>Tgl Daftar</th>
              <th>Status</th>
              <?php if(is_admin()): ?>
              <th class="center" style="width:70px">Aksi</th>
              <?php endif; ?>
            </tr>
          </thead>
          <tbody>
            <?php foreach($peserta as $p): ?>
            <tr>
              <td class="center">
                <span class="nomor-badge"><?php echo $p->nomor_urut; ?></span>
              </td>
              <td class="nik-cell"><?php echo $p->nik; ?></td>
              <td><span class="peserta-name"><?php echo $p->nama_lengkap; ?></span></td>
              <td>
                <?php if(!empty($p->no_hp)): ?>
                  <a href="https://wa.me/<?php echo preg_replace('/^0/', '62', $p->no_hp); ?>"
                     target="_blank" class="wa-link">
                    <i class="bi bi-whatsapp"></i> <?php echo $p->no_hp; ?>
                  </a>
                <?php else: ?>
                  <span style="color:var(--slate-400);font-size:.82rem">—</span>
                <?php endif; ?>
              </td>
              <td class="date-cell"><?php echo date('d/m/Y', strtotime($p->tanggal_daftar)); ?></td>
              <td>
                <?php if($p->status_peserta == 'aktif'): ?>
                  <span class="status-pill aktif"><i class="bi bi-circle-fill" style="font-size:.42rem"></i> Aktif</span>
                <?php else: ?>
                  <span class="status-pill keluar"><i class="bi bi-circle" style="font-size:.42rem"></i> Keluar</span>
                <?php endif; ?>
              </td>
              <?php if(is_admin()): ?>
              <td class="center">
              <!-- BENAR -->
<button class="act-btn-del" title="Hapus Peserta"
   onclick="konfirmasiHapusPeserta('<?php echo base_url('arisan/hapus_peserta/'.$p->id.'/'.$arisan->id); ?>','<?php echo addslashes($p->nama_lengkap); ?>')">
  <i class="bi bi-trash"></i>
</button>
              </td>
              <?php endif; ?>
            </tr>
            <?php endforeach; ?>
          </tbody>
        </table>
      </div>

    <?php endif; ?>
  </div>

  <!-- ── Total Dana Card ───────────────────────────────────── -->
  <?php if($total_peserta > 0): ?>
  <div class="dana-card">
    <div class="dana-item">
      <div class="di-label">Total Dana per Pertemuan</div>
      <div class="di-value teal">Rp <?php echo number_format($arisan->nominal_iuran * $total_peserta, 0, ',', '.'); ?></div>
      <div class="di-sub"><?php echo $total_peserta; ?> peserta × Rp <?php echo number_format($arisan->nominal_iuran, 0, ',', '.'); ?></div>
    </div>
    <div class="dana-item">
      <div class="di-label">Dana Didapat Pemenang</div>
      <div class="di-value green">Rp <?php echo number_format($arisan->nominal_iuran * $total_peserta, 0, ',', '.'); ?></div>
      <div class="di-sub">Dana yang dikumpulkan dari semua peserta</div>
    </div>
  </div>
  <?php endif; ?>

  <!-- Modal Konfirmasi Hapus Peserta -->
<div class="modal fade" id="modalHapusPeserta" tabindex="-1" aria-hidden="true">
  <div class="modal-dialog modal-dialog-centered" style="max-width:400px;">
    <div class="modal-content" style="border-radius:16px;border:none;overflow:hidden;">
      <div class="modal-header" style="background:#fef2f2;border-bottom:1.5px solid #fecaca;padding:18px 20px;">
        <div class="d-flex align-items-center gap-2">
          <div style="width:36px;height:36px;background:#fee2e2;border-radius:10px;display:flex;align-items:center;justify-content:center;">
            <i class="bi bi-person-x-fill" style="color:#dc2626;font-size:1rem;"></i>
          </div>
          <h6 class="modal-title mb-0 fw-bold" style="color:#991b1b;font-size:.95rem;">Hapus Peserta Arisan</h6>
        </div>
        <button type="button" class="btn-close" data-bs-dismiss="modal"></button>
      </div>
      <div class="modal-body" style="padding:20px;">
        <p style="font-size:.85rem;color:#334155;margin-bottom:10px;">Anda akan menghapus peserta:</p>
        <div style="background:#fef2f2;border:1.5px solid #fecaca;border-radius:10px;padding:12px 16px;display:flex;align-items:center;gap:10px;">
          <div style="width:36px;height:36px;border-radius:9px;background:linear-gradient(135deg,#0b7a6e,#0d9488);color:#fff;display:flex;align-items:center;justify-content:center;font-weight:800;font-size:1rem;flex-shrink:0;" id="hapusPesertaAvatar">?</div>
          <div style="font-weight:700;font-size:.9rem;color:#991b1b;" id="hapusPesertaNama">-</div>
        </div>
        <div style="background:#fffbeb;border:1.5px solid #fde68a;border-radius:8px;padding:9px 14px;margin-top:12px;font-size:.78rem;color:#92400e;">
          <i class="bi bi-exclamation-triangle-fill me-1"></i>
          Data peserta akan <strong>terhapus permanen</strong> dari arisan ini.
        </div>
      </div>
      <div class="modal-footer" style="border:none;padding:0 20px 18px;gap:8px;">
        <button type="button" class="btn" data-bs-dismiss="modal"
          style="border-radius:8px;border:1.5px solid #e5e7eb;font-size:.82rem;font-weight:600;color:#64748b;padding:7px 18px;flex:1;">
          <i class="bi bi-x-lg me-1"></i>Batal
        </button>
        <a href="#" id="btnKonfirmasiHapusPeserta"
          style="border-radius:8px;border:none;font-size:.82rem;font-weight:700;color:#fff;padding:7px 18px;flex:1;background:linear-gradient(135deg,#dc2626,#ef4444);box-shadow:0 4px 12px rgba(220,38,38,.28);cursor:pointer;display:inline-flex;align-items:center;justify-content:center;gap:6px;text-decoration:none;">
          <i class="bi bi-person-x-fill"></i>Ya, Hapus
        </a>
      </div>
    </div>
  </div>
</div>

</div><!-- /ps-wrapper -->
<script>
document.addEventListener('DOMContentLoaded', function() {

  // ── DataTables — tunggu jQuery lewat window.onload ──
  function initDataTable() {
    if(typeof $ !== 'undefined' && $.fn && $.fn.DataTable) {
      $('#tablePeserta').DataTable({
        language: { url: '//cdn.datatables.net/plug-ins/1.13.7/i18n/id.json' },
        order: [[0, 'asc']],
        pageLength: 50,
        responsive: true,
        dom: '<"ps-dt-top"lf>rt<"ps-dt-bot"ip>',
        initComplete: function() {
          $('.dataTables_filter input').css({
            'border': '1.5px solid #e2e8f0',
            'border-radius': '8px',
            'padding': '7px 13px',
            'font-size': '0.84rem',
            'outline': 'none'
          }).on('focus', function() {
            $(this).css({'border-color': '#0d9488', 'box-shadow': '0 0 0 3px rgba(13,148,136,0.12)'});
          }).on('blur', function() {
            $(this).css({'border-color': '#e2e8f0', 'box-shadow': 'none'});
          });
        }
      });
    }
  }

  if(typeof $ !== 'undefined') {
    initDataTable();
  } else {
    window.addEventListener('load', initDataTable);
  }

  // ── Hapus Peserta Modal ──
  var _bsModalHapusPeserta = null;

  window.konfirmasiHapusPeserta = function(url, nama) {
    var elNama   = document.getElementById('hapusPesertaNama');
    var elAvatar = document.getElementById('hapusPesertaAvatar');
    var elBtn    = document.getElementById('btnKonfirmasiHapusPeserta');
    if(elNama)   elNama.textContent   = nama || '-';
    if(elAvatar) elAvatar.textContent = nama ? nama.charAt(0).toUpperCase() : '?';
    if(elBtn)    elBtn.href           = url;

    var modalEl = document.getElementById('modalHapusPeserta');
    if(!modalEl) return;
    if(!_bsModalHapusPeserta) _bsModalHapusPeserta = new bootstrap.Modal(modalEl);
    _bsModalHapusPeserta.show();
  };

  // ── Auto-dismiss flash alerts ──
  setTimeout(function() {
    ['flashSuccess','flashError'].forEach(function(id) {
      var el = document.getElementById(id);
      if(el) {
        el.style.transition = 'opacity .4s';
        el.style.opacity = '0';
        setTimeout(function(){ if(el.parentNode) el.parentNode.removeChild(el); }, 420);
      }
    });
  }, 4500);

});
</script>