"use client";

import { useEffect, useState } from "react";
import Link from "next/link";

type Education = {
  id: number;
  institution: string;
  degree: string;
  field: string | null;
  startDate: string;
  endDate: string | null;
  description: string | null;
};

export default function EducationAdminPage() {
  const [educations, setEducations] = useState<Education[]>([]);
  const [loading, setLoading] = useState(true);
  const [sidebarOpen, setSidebarOpen] = useState(false);
  const [deletingId, setDeletingId] = useState<number | null>(null);

  // =========================================================
  // GET EDUCATION
  // =========================================================

  const getEducations = async () => {
    try {
      setLoading(true);

      const response = await fetch("/api/education", {
        cache: "no-store",
      });

      const result = await response.json();

      if (result.success) {
        setEducations(result.data);
      } else {
        alert(result.message || "Failed to load education data.");
      }
    } catch (error) {
      console.error("GET EDUCATION ERROR:", error);
      alert("Failed to load education data.");
    } finally {
      setLoading(false);
    }
  };

  useEffect(() => {
    getEducations();
  }, []);

  // =========================================================
  // DELETE EDUCATION
  // =========================================================

  const deleteEducation = async (id: number) => {
    const education = educations.find((item) => item.id === id);

    const confirmDelete = window.confirm(
      `Delete education data from "${
        education?.institution || "this institution"
      }"?`
    );

    if (!confirmDelete) return;

    setDeletingId(id);

    try {
      const response = await fetch(`/api/education?id=${id}`, {
        method: "DELETE",
      });

      const result = await response.json();

      if (!response.ok || !result.success) {
        alert(result.message || "Failed to delete education.");
        return;
      }

      alert("Education successfully deleted.");

      await getEducations();
    } catch (error) {
      console.error("DELETE EDUCATION ERROR:", error);
      alert("An error occurred while deleting education.");
    } finally {
      setDeletingId(null);
    }
  };

  // =========================================================
  // FORMAT YEAR
  // =========================================================

  const formatYear = (date: string) => {
    const parsedDate = new Date(date);

    if (Number.isNaN(parsedDate.getTime())) {
      return "-";
    }

    return parsedDate.getFullYear();
  };

  // =========================================================
  // DEGREE LABEL
  // =========================================================

  const getDegreeLabel = (degree: string) => {
    switch (degree) {
      case "SMA":
        return "High School";

      case "SMK":
        return "Vocational High School";

      case "D3":
        return "Diploma";

      case "S1":
        return "Bachelor's Degree";

      case "S2":
        return "Master's Degree";

      case "S3":
        return "Doctoral Degree";

      default:
        return degree;
    }
  };

  // =========================================================
  // STATISTICS
  // =========================================================

  const totalEducation = educations.length;

  const currentEducation = educations.filter(
    (education) => !education.endDate
  ).length;

  const completedEducation = educations.filter(
    (education) => education.endDate
  ).length;

  // =========================================================
  // CLOSE SIDEBAR
  // =========================================================

  const closeSidebar = () => {
    setSidebarOpen(false);
  };

  return (
    <>
      <style>{`
        /* =====================================================
           GLOBAL
        ===================================================== */

        * {
          box-sizing: border-box;
        }

        html,
        body {
          max-width: 100%;
          overflow-x: hidden;
          margin: 0;
          padding: 0;
        }

        body {
          background: #f5f7fb;
        }

        .education-admin {
          min-height: 100vh;
          width: 100%;
          background: #f5f7fb;
          color: #1f2937;
          overflow-x: hidden;
        }

        /* =====================================================
           LAYOUT
        ===================================================== */

        .admin-layout {
          min-height: 100vh;
          width: 100%;
          display: flex;
          position: relative;
        }

        /* =====================================================
           SIDEBAR
        ===================================================== */

        .admin-sidebar {
          width: 270px;
          min-width: 270px;
          height: 100vh;
          position: fixed;
          top: 0;
          left: 0;
          z-index: 1050;
          background: #ffffff;
          border-right: 1px solid #e8ecf1;
          transition: transform 0.3s ease;
        }

        .sidebar-inner {
          height: 100%;
          display: flex;
          flex-direction: column;
          padding: 24px 16px;
          overflow-y: auto;
          overflow-x: hidden;
        }

        .sidebar-brand {
          display: flex;
          align-items: center;
          gap: 12px;
          padding: 4px 10px 28px;
          border-bottom: 1px solid #edf0f4;
          margin-bottom: 22px;
          min-width: 0;
        }

        .brand-icon {
          width: 42px;
          height: 42px;
          min-width: 42px;
          border-radius: 12px;
          display: flex;
          align-items: center;
          justify-content: center;
          background: linear-gradient(135deg, #198754, #20c997);
          color: #ffffff;
          font-size: 18px;
          box-shadow: 0 8px 20px rgba(25, 135, 84, 0.2);
        }

        .brand-text {
          min-width: 0;
        }

        .brand-text strong {
          display: block;
          font-size: 16px;
          line-height: 1.2;
          color: #17202a;
          white-space: nowrap;
        }

        .brand-text span {
          display: block;
          margin-top: 3px;
          font-size: 11px;
          color: #8b95a5;
          font-weight: 600;
          letter-spacing: 0.08em;
          text-transform: uppercase;
        }

        .sidebar-close {
          margin-left: auto;
          border: 0;
          background: transparent;
          color: #7b8492;
          font-size: 17px;
          cursor: pointer;
        }

        .sidebar-section-label {
          padding: 0 12px;
          margin-bottom: 8px;
          color: #9aa3af;
          font-size: 10px;
          font-weight: 700;
          letter-spacing: 0.1em;
        }

        .sidebar-nav {
          display: flex;
          flex-direction: column;
          gap: 4px;
        }

        .sidebar-link {
          display: flex;
          align-items: center;
          gap: 12px;
          min-height: 44px;
          width: 100%;
          padding: 10px 12px;
          border-radius: 11px;
          color: #697386;
          text-decoration: none;
          font-size: 14px;
          font-weight: 500;
          transition:
            background 0.2s ease,
            color 0.2s ease,
            transform 0.2s ease;
        }

        .sidebar-link:hover {
          background: #f0f8f4;
          color: #198754;
          transform: translateX(2px);
        }

        .sidebar-link.active {
          background: linear-gradient(
            90deg,
            rgba(25, 135, 84, 0.12),
            rgba(25, 135, 84, 0.06)
          );
          color: #198754;
          font-weight: 700;
        }

        .sidebar-link-icon {
          width: 23px;
          min-width: 23px;
          text-align: center;
          font-size: 16px;
          flex-shrink: 0;
        }

        .sidebar-bottom {
          margin-top: auto;
          padding-top: 18px;
          border-top: 1px solid #edf0f4;
        }

        .sidebar-bottom a {
          display: flex;
          align-items: center;
          gap: 10px;
          padding: 11px 12px;
          border-radius: 10px;
          color: #6b7280;
          text-decoration: none;
          font-size: 13px;
          transition: 0.2s ease;
        }

        .sidebar-bottom a:hover {
          background: #f5f7fa;
          color: #198754;
        }

        /* =====================================================
           MAIN
        ===================================================== */

        .admin-main {
          width: calc(100% - 270px);
          margin-left: 270px;
          min-width: 0;
          min-height: 100vh;
        }

        /* =====================================================
           TOPBAR
        ===================================================== */

        .admin-topbar {
          width: 100%;
          height: 76px;
          background: rgba(255, 255, 255, 0.96);
          border-bottom: 1px solid #e9edf2;
          display: flex;
          align-items: center;
          justify-content: space-between;
          padding: 0 34px;
          position: sticky;
          top: 0;
          z-index: 900;
          backdrop-filter: blur(12px);
        }

        .topbar-left {
          display: flex;
          align-items: center;
          gap: 14px;
          min-width: 0;
        }

        .mobile-menu-button {
          width: 40px;
          height: 40px;
          border: 1px solid #e2e7ed;
          background: #ffffff;
          border-radius: 10px;
          color: #344054;
          font-size: 18px;
          cursor: pointer;
          flex-shrink: 0;
        }

        .mobile-menu-button:hover {
          border-color: #b7ddca;
          color: #198754;
          background: #f4fbf7;
        }

        .topbar-label {
          color: #9aa3af;
          font-size: 10px;
          font-weight: 700;
          letter-spacing: 0.1em;
          margin-bottom: 3px;
        }

        .topbar-title {
          color: #17202a;
          font-size: 15px;
          font-weight: 700;
          white-space: nowrap;
        }

        .topbar-right {
          display: flex;
          align-items: center;
          gap: 10px;
          flex-shrink: 0;
        }

        .topbar-website,
        .topbar-logout {
          min-height: 40px;
          display: inline-flex;
          align-items: center;
          justify-content: center;
          gap: 8px;
          border-radius: 10px;
          padding: 8px 13px;
          text-decoration: none;
          font-size: 13px;
          font-weight: 600;
          transition: 0.2s ease;
        }

        .topbar-website {
          color: #667085;
          border: 1px solid #e4e8ed;
          background: #ffffff;
        }

        .topbar-website:hover {
          color: #198754;
          border-color: #b7ddca;
          background: #f4fbf7;
        }

        .topbar-logout {
          color: #dc3545;
          border: 1px solid #f1c4c9;
          background: #ffffff;
        }

        .topbar-logout:hover {
          background: #fff5f6;
          border-color: #e8aeb5;
        }

        /* =====================================================
           CONTENT
        ===================================================== */

        .admin-content {
          width: 100%;
          max-width: 1500px;
          margin: 0 auto;
          padding: 34px;
          animation: contentEnter 0.45s ease both;
        }

        @keyframes contentEnter {
          from {
            opacity: 0;
            transform: translateY(10px);
          }

          to {
            opacity: 1;
            transform: translateY(0);
          }
        }

        /* =====================================================
           PAGE HEADER
        ===================================================== */

        .page-heading {
          width: 100%;
          display: flex;
          align-items: flex-end;
          justify-content: space-between;
          gap: 25px;
          margin-bottom: 28px;
        }

        .page-heading-left {
          min-width: 0;
          flex: 1;
        }

        .back-dashboard {
          display: inline-flex;
          align-items: center;
          gap: 7px;
          margin-bottom: 18px;
          color: #6b7280;
          font-size: 13px;
          font-weight: 600;
          text-decoration: none;
          transition: 0.2s ease;
        }

        .back-dashboard:hover {
          color: #198754;
          transform: translateX(-2px);
        }

        .page-title-row {
          display: flex;
          align-items: center;
          gap: 15px;
          min-width: 0;
        }

        .page-title-icon {
          width: 54px;
          height: 54px;
          min-width: 54px;
          border-radius: 15px;
          background: linear-gradient(
            135deg,
            rgba(25, 135, 84, 0.13),
            rgba(32, 201, 151, 0.08)
          );
          color: #198754;
          display: flex;
          align-items: center;
          justify-content: center;
          font-size: 22px;
          flex-shrink: 0;
        }

        .page-eyebrow {
          color: #198754;
          font-size: 10px;
          font-weight: 800;
          letter-spacing: 0.11em;
          margin-bottom: 4px;
        }

        .page-title {
          margin: 0;
          font-size: clamp(26px, 3vw, 34px);
          font-weight: 800;
          letter-spacing: -0.025em;
          color: #182230;
          line-height: 1.15;
        }

        .page-description {
          margin: 7px 0 0;
          color: #7a8493;
          font-size: 14px;
          line-height: 1.7;
          max-width: 700px;

          /* JUSTIFY */
          text-align: justify;
          text-justify: inter-word;
        }

        .page-heading-action {
          flex-shrink: 0;
        }

        .add-button {
          min-height: 46px;
          display: inline-flex;
          align-items: center;
          justify-content: center;
          gap: 8px;
          padding: 10px 18px;
          border: 0;
          border-radius: 11px;
          background: linear-gradient(135deg, #198754, #20a46c);
          color: #ffffff;
          font-size: 13px;
          font-weight: 700;
          text-decoration: none;
          box-shadow: 0 8px 18px rgba(25, 135, 84, 0.18);
          transition: 0.25s ease;
          white-space: nowrap;
        }

        .add-button:hover {
          color: #ffffff;
          transform: translateY(-2px);
          box-shadow: 0 12px 24px rgba(25, 135, 84, 0.25);
        }

        /* =====================================================
           STATISTICS
        ===================================================== */

        .stats-grid {
          display: grid;
          grid-template-columns: repeat(4, minmax(0, 1fr));
          gap: 16px;
          margin-bottom: 24px;
        }

        .stat-card {
          background: #ffffff;
          border: 1px solid #e8edf2;
          border-radius: 15px;
          padding: 18px;
          display: flex;
          align-items: center;
          gap: 14px;
          min-width: 0;
          box-shadow: 0 4px 15px rgba(20, 30, 45, 0.025);
          transition:
            transform 0.25s ease,
            box-shadow 0.25s ease;
          animation: cardEnter 0.5s ease both;
        }

        .stat-card:nth-child(1) {
          animation-delay: 0.04s;
        }

        .stat-card:nth-child(2) {
          animation-delay: 0.08s;
        }

        .stat-card:nth-child(3) {
          animation-delay: 0.12s;
        }

        .stat-card:nth-child(4) {
          animation-delay: 0.16s;
        }

        @keyframes cardEnter {
          from {
            opacity: 0;
            transform: translateY(8px);
          }

          to {
            opacity: 1;
            transform: translateY(0);
          }
        }

        .stat-card:hover {
          transform: translateY(-3px);
          box-shadow: 0 12px 25px rgba(20, 30, 45, 0.07);
        }

        .stat-icon {
          width: 45px;
          height: 45px;
          min-width: 45px;
          border-radius: 12px;
          display: flex;
          align-items: center;
          justify-content: center;
          font-size: 18px;
          flex-shrink: 0;
        }

        .stat-icon.green {
          background: #eaf7f0;
          color: #198754;
        }

        .stat-icon.blue {
          background: #edf5ff;
          color: #3478db;
        }

        .stat-icon.dark {
          background: #eef0f3;
          color: #344054;
        }

        .stat-label {
          color: #8a93a0;
          font-size: 11px;
          font-weight: 600;
          margin-bottom: 3px;
        }

        .stat-value {
          color: #1d2939;
          font-size: 21px;
          font-weight: 800;
          line-height: 1;
        }

        /* =====================================================
           CONTENT CARD
        ===================================================== */

        .content-card {
          background: #ffffff;
          border: 1px solid #e7ebef;
          border-radius: 17px;
          overflow: hidden;
          box-shadow: 0 5px 20px rgba(20, 30, 45, 0.03);
          width: 100%;
          min-width: 0;
        }

        .content-card-header {
          padding: 23px 25px;
          display: flex;
          align-items: center;
          justify-content: space-between;
          gap: 15px;
          border-bottom: 1px solid #edf0f3;
        }

        .content-card-header > div:first-child {
          min-width: 0;
        }

        .content-card-eyebrow {
          color: #198754;
          font-size: 10px;
          font-weight: 800;
          letter-spacing: 0.1em;
          margin-bottom: 4px;
        }

        .content-card-title {
          margin: 0;
          color: #182230;
          font-size: 18px;
          font-weight: 800;
        }

        .content-card-description {
          margin: 6px 0 0;
          color: #8a93a0;
          font-size: 12px;
          line-height: 1.6;
          max-width: 600px;

          /* JUSTIFY */
          text-align: justify;
          text-justify: inter-word;
        }

        .content-card-count {
          display: inline-flex;
          align-items: center;
          gap: 7px;
          padding: 8px 11px;
          border-radius: 9px;
          background: #f4f8f6;
          color: #198754;
          font-size: 12px;
          font-weight: 700;
          white-space: nowrap;
          flex-shrink: 0;
        }

        /* =====================================================
           DESKTOP TABLE
        ===================================================== */

        .education-table-wrapper {
          width: 100%;
          overflow-x: auto;
          overflow-y: hidden;
          -webkit-overflow-scrolling: touch;
        }

        .education-table {
          width: 100%;
          min-width: 900px;
          border-collapse: separate;
          border-spacing: 0;
        }

        .education-table thead th {
          background: #fafbfc;
          color: #7b8492;
          border-bottom: 1px solid #e9edf1;
          padding: 13px 18px;
          font-size: 10px;
          font-weight: 800;
          letter-spacing: 0.06em;
          text-transform: uppercase;
          white-space: nowrap;
          text-align: left;
        }

        .education-table tbody td {
          padding: 18px;
          border-bottom: 1px solid #f0f2f5;
          vertical-align: middle;
        }

        .education-table tbody tr:last-child td {
          border-bottom: 0;
        }

        .education-table tbody tr {
          transition: background 0.2s ease;
        }

        .education-table tbody tr:hover {
          background: #fbfdfc;
        }

        .education-main {
          display: flex;
          align-items: center;
          gap: 13px;
          min-width: 280px;
        }

        .education-icon {
          width: 49px;
          height: 49px;
          min-width: 49px;
          border-radius: 13px;
          display: flex;
          align-items: center;
          justify-content: center;
          background: linear-gradient(
            135deg,
            #eaf7f0,
            #f1faf6
          );
          color: #198754;
          font-size: 19px;
          flex-shrink: 0;
        }

        .education-degree {
          color: #1d2939;
          font-size: 13px;
          font-weight: 800;
          line-height: 1.35;
        }

        .education-institution {
          margin-top: 4px;
          color: #7a8493;
          font-size: 12px;
          line-height: 1.5;
          overflow-wrap: anywhere;

          /* JUSTIFY */
          text-align: justify;
          text-justify: inter-word;
        }

        .field-text {
          color: #596273;
          font-size: 12px;
          line-height: 1.55;
          max-width: 210px;
          overflow-wrap: anywhere;

          /* JUSTIFY */
          text-align: justify;
          text-justify: inter-word;
        }

        .period-wrapper {
          display: inline-flex;
          align-items: center;
          gap: 7px;
          padding: 7px 10px;
          border-radius: 8px;
          background: #f7f8fa;
          color: #596273;
          font-size: 11px;
          font-weight: 700;
          white-space: nowrap;
        }

        .period-wrapper i {
          color: #198754;
        }

        .description-cell {
          max-width: 270px;
        }

        .description-text {
          color: #7a8493;
          font-size: 11px;
          line-height: 1.65;
          overflow-wrap: anywhere;

          /* JUSTIFY */
          text-align: justify;
          text-justify: inter-word;
        }

        .action-group {
          display: flex;
          align-items: center;
          justify-content: flex-end;
          gap: 7px;
          white-space: nowrap;
        }

        .action-button {
          min-height: 34px;
          display: inline-flex;
          align-items: center;
          justify-content: center;
          gap: 5px;
          padding: 6px 10px;
          border-radius: 8px;
          font-size: 11px;
          font-weight: 700;
          transition: 0.2s ease;
          text-decoration: none;
        }

        .edit-button {
          color: #3478db;
          background: #ffffff;
          border: 1px solid #cbdcf5;
        }

        .edit-button:hover {
          color: #2563b9;
          background: #f3f7fd;
          border-color: #9fc0ed;
        }

        .delete-button {
          color: #dc3545;
          background: #ffffff;
          border: 1px solid #f1c4c9;
          cursor: pointer;
        }

        .delete-button:hover:not(:disabled) {
          color: #bb2635;
          background: #fff5f6;
          border-color: #e7a5ad;
        }

        .delete-button:disabled {
          opacity: 0.6;
          cursor: not-allowed;
        }

        /* =====================================================
           MOBILE EDUCATION CARDS
        ===================================================== */

        .education-mobile-list {
          display: none;
          padding: 14px;
        }

        .education-mobile-card {
          width: 100%;
          background: #ffffff;
          border: 1px solid #e8edf2;
          border-radius: 15px;
          padding: 16px;
          margin-bottom: 12px;
          box-shadow: 0 4px 14px rgba(20, 30, 45, 0.035);
          min-width: 0;
        }

        .education-mobile-card:last-child {
          margin-bottom: 0;
        }

        .mobile-education-header {
          display: flex;
          align-items: flex-start;
          gap: 12px;
          min-width: 0;
        }

        .mobile-education-info {
          flex: 1;
          min-width: 0;
        }

        .mobile-education-degree {
          color: #1d2939;
          font-size: 14px;
          font-weight: 800;
          line-height: 1.4;
          overflow-wrap: anywhere;
        }

        .mobile-education-institution {
          color: #7a8493;
          font-size: 12px;
          line-height: 1.6;
          margin-top: 4px;
          overflow-wrap: anywhere;

          /* JUSTIFY */
          text-align: justify;
          text-justify: inter-word;
        }

        .mobile-education-details {
          display: grid;
          grid-template-columns: 1fr;
          gap: 10px;
          margin-top: 16px;
          padding-top: 14px;
          border-top: 1px solid #edf0f3;
        }

        .mobile-detail {
          min-width: 0;
        }

        .mobile-detail-label {
          display: flex;
          align-items: center;
          gap: 6px;
          color: #98a1ae;
          font-size: 9px;
          font-weight: 800;
          letter-spacing: 0.07em;
          text-transform: uppercase;
          margin-bottom: 4px;
        }

        .mobile-detail-label i {
          color: #198754;
          font-size: 11px;
        }

        .mobile-detail-value {
          color: #596273;
          font-size: 12px;
          line-height: 1.6;
          overflow-wrap: anywhere;

          /* JUSTIFY */
          text-align: justify;
          text-justify: inter-word;
        }

        .mobile-description {
          color: #7a8493;
          font-size: 11px;
          line-height: 1.7;
          overflow-wrap: anywhere;

          /* JUSTIFY */
          text-align: justify;
          text-justify: inter-word;
        }

        .mobile-actions {
          display: grid;
          grid-template-columns: 1fr 1fr;
          gap: 8px;
          margin-top: 15px;
          padding-top: 14px;
          border-top: 1px solid #edf0f3;
        }

        .mobile-actions .action-button {
          width: 100%;
          min-height: 40px;
          font-size: 12px;
        }

        /* =====================================================
           LOADING
        ===================================================== */

        .loading-state {
          min-height: 360px;
          display: flex;
          flex-direction: column;
          align-items: center;
          justify-content: center;
          text-align: center;
          padding: 50px 20px;
        }

        .loading-spinner {
          width: 44px;
          height: 44px;
          border: 3px solid #dcefe5;
          border-top-color: #198754;
          border-radius: 50%;
          animation: spin 0.8s linear infinite;
          margin-bottom: 18px;
        }

        @keyframes spin {
          to {
            transform: rotate(360deg);
          }
        }

        .loading-state h5 {
          color: #263241;
          margin-bottom: 5px;
          font-weight: 800;
        }

        .loading-state p {
          color: #8a93a0;
          font-size: 13px;
          margin: 0;

          /* JUSTIFY */
          text-align: justify;
        }

        /* =====================================================
           EMPTY
        ===================================================== */

        .empty-state {
          padding: 70px 25px;
          text-align: center;
        }

        .empty-icon {
          width: 76px;
          height: 76px;
          margin: 0 auto 20px;
          border-radius: 22px;
          background: #eef8f3;
          color: #198754;
          display: flex;
          align-items: center;
          justify-content: center;
          font-size: 30px;
        }

        .empty-state h4 {
          color: #1d2939;
          font-weight: 800;
          margin-bottom: 8px;
        }

        .empty-state p {
          color: #8a93a0;
          font-size: 13px;
          line-height: 1.7;
          margin: 0 auto 22px;
          max-width: 500px;

          /* JUSTIFY */
          text-align: justify;
          text-justify: inter-word;
        }

        /* =====================================================
           FOOTER
        ===================================================== */

        .admin-page-footer {
          padding: 20px 3px 5px;
          display: flex;
          align-items: center;
          justify-content: space-between;
          gap: 15px;
          color: #9aa3af;
          font-size: 11px;
        }

        .admin-page-footer div {
          display: flex;
          align-items: center;
          gap: 7px;
        }

        .admin-page-footer i {
          color: #198754;
        }

        .admin-page-footer a {
          color: #7b8492;
          text-decoration: none;
          font-weight: 600;
        }

        .admin-page-footer a:hover {
          color: #198754;
        }

        /* =====================================================
           SIDEBAR OVERLAY
        ===================================================== */

        .sidebar-overlay {
          display: none;
        }

        /* =====================================================
           LARGE TABLE
        ===================================================== */

        @media (max-width: 1399.98px) {
          .education-table {
            min-width: 860px;
          }

          .education-table thead th,
          .education-table tbody td {
            padding-left: 14px;
            padding-right: 14px;
          }

          .admin-content {
            padding: 30px;
          }
        }

        /* =====================================================
           TABLET
        ===================================================== */

        @media (max-width: 1199.98px) {
          .stats-grid {
            grid-template-columns: repeat(2, minmax(0, 1fr));
          }

          .education-table {
            min-width: 840px;
          }

          .admin-content {
            padding: 28px;
          }

          .page-heading {
            gap: 18px;
          }
        }

        /* =====================================================
           MOBILE SIDEBAR
        ===================================================== */

        @media (max-width: 991.98px) {
          .admin-sidebar {
            transform: translateX(-100%);
            box-shadow: 15px 0 40px rgba(20, 30, 45, 0.1);
          }

          .admin-sidebar.open {
            transform: translateX(0);
          }

          .admin-main {
            width: 100%;
            margin-left: 0;
          }

          .sidebar-overlay {
            position: fixed;
            inset: 0;
            z-index: 1040;
            display: block;
            background: rgba(15, 23, 42, 0.35);
            backdrop-filter: blur(2px);
          }

          .admin-topbar {
            padding: 0 24px;
          }
        }

        /* =====================================================
           MOBILE
        ===================================================== */

        @media (max-width: 767.98px) {
          .admin-topbar {
            height: 68px;
            padding: 0 16px;
          }

          .topbar-title {
            font-size: 13px;
          }

          .admin-content {
            width: 100%;
            max-width: 100%;
            padding: 23px 16px;
          }

          .page-heading {
            align-items: stretch;
            flex-direction: column;
            gap: 18px;
            margin-bottom: 22px;
          }

          .page-heading-action {
            width: 100%;
          }

          .add-button {
            width: 100%;
          }

          .page-title-row {
            align-items: flex-start;
          }

          .page-title-icon {
            width: 48px;
            height: 48px;
            min-width: 48px;
            border-radius: 13px;
            font-size: 19px;
          }

          .page-title {
            font-size: 27px;
          }

          .page-description {
            font-size: 13px;
            line-height: 1.7;
            max-width: 100%;
          }

          .stats-grid {
            grid-template-columns: 1fr 1fr;
            gap: 10px;
            margin-bottom: 18px;
          }

          .stat-card {
            padding: 13px;
            gap: 10px;
            border-radius: 13px;
          }

          .stat-icon {
            width: 38px;
            height: 38px;
            min-width: 38px;
            border-radius: 10px;
            font-size: 15px;
          }

          .stat-label {
            font-size: 9px;
          }

          .stat-value {
            font-size: 18px;
          }

          .content-card {
            border-radius: 14px;
          }

          .content-card-header {
            padding: 18px;
            align-items: flex-start;
          }

          .content-card-title {
            font-size: 16px;
          }

          .content-card-description {
            font-size: 11px;
            line-height: 1.7;
          }

          .content-card-count {
            padding: 7px 9px;
            font-size: 10px;
          }

          /*
           * Desktop table hidden on mobile.
           * Mobile cards displayed instead.
           */

          .education-table-wrapper {
            display: none;
          }

          .education-mobile-list {
            display: block;
          }

          .admin-page-footer {
            flex-direction: column;
            align-items: flex-start;
          }
        }

        /* =====================================================
           SMALL MOBILE
        ===================================================== */

        @media (max-width: 575.98px) {
          .admin-content {
            padding: 20px 12px;
          }

          .page-title {
            font-size: 24px;
          }

          .page-description {
            font-size: 12px;
          }

          .stats-grid {
            gap: 8px;
          }

          .stat-card {
            min-height: 76px;
            padding: 10px;
          }

          .stat-icon {
            width: 34px;
            height: 34px;
            min-width: 34px;
            font-size: 14px;
          }

          .stat-label {
            font-size: 8px;
          }

          .stat-value {
            font-size: 16px;
          }

          .content-card-header {
            padding: 16px;
          }

          .content-card-count span {
            display: none;
          }

          .education-mobile-list {
            padding: 10px;
          }

          .education-mobile-card {
            padding: 14px;
            border-radius: 13px;
          }

          .education-icon {
            width: 43px;
            height: 43px;
            min-width: 43px;
            border-radius: 11px;
            font-size: 17px;
          }

          .mobile-education-degree {
            font-size: 13px;
          }

          .mobile-education-institution {
            font-size: 11px;
          }

          .mobile-actions .action-button {
            min-height: 38px;
            font-size: 11px;
          }
        }

        /* =====================================================
           EXTRA SMALL MOBILE
        ===================================================== */

        @media (max-width: 399.98px) {
          .topbar-right {
            gap: 6px;
          }

          .topbar-website,
          .topbar-logout {
            width: 38px;
            height: 38px;
            min-height: 38px;
            padding: 0;
            border-radius: 9px;
          }

          .topbar-title {
            max-width: 145px;
            overflow: hidden;
            text-overflow: ellipsis;
            white-space: nowrap;
          }

          .page-title-row {
            gap: 11px;
          }

          .page-title-icon {
            width: 43px;
            height: 43px;
            min-width: 43px;
            font-size: 17px;
          }

          .page-title {
            font-size: 22px;
          }

          .stats-grid {
            grid-template-columns: 1fr;
          }

          .stat-card {
            min-height: 68px;
          }

          .mobile-education-details {
            gap: 9px;
          }

          .mobile-actions {
            grid-template-columns: 1fr;
          }
        }

        /* =====================================================
           REDUCED MOTION
        ===================================================== */

        @media (prefers-reduced-motion: reduce) {
          .education-admin *,
          .education-admin *::before,
          .education-admin *::after {
            animation-duration: 0.01ms !important;
            animation-iteration-count: 1 !important;
            transition-duration: 0.01ms !important;
          }
        }
      `}</style>

      <div className="education-admin">
        <div className="admin-layout">

          {/* ==================================================
              MOBILE OVERLAY
          ================================================== */}

          {sidebarOpen && (
            <div
              className="sidebar-overlay"
              onClick={closeSidebar}
              aria-hidden="true"
            />
          )}

          {/* ==================================================
              SIDEBAR
          ================================================== */}

          <aside
            className={`admin-sidebar ${
              sidebarOpen ? "open" : ""
            }`}
          >
            <div className="sidebar-inner">

              {/* BRAND */}

              <div className="sidebar-brand">
                <div className="brand-icon">
                  <i className="bi bi-grid-1x2-fill"></i>
                </div>

                <div className="brand-text">
                  <strong>GloVaTech</strong>
                  <span>Admin Panel</span>
                </div>

                <button
                  type="button"
                  className="sidebar-close d-lg-none"
                  onClick={closeSidebar}
                  aria-label="Close sidebar"
                >
                  <i className="bi bi-x-lg"></i>
                </button>
              </div>

              {/* MAIN MENU */}

              <div className="sidebar-section-label">
                MAIN MENU
              </div>

              <nav className="sidebar-nav">
                <Link
                  href="/secure-panel"
                  className="sidebar-link"
                  onClick={closeSidebar}
                >
                  <span className="sidebar-link-icon">
                    <i className="bi bi-speedometer2"></i>
                  </span>

                  <span>Dashboard</span>
                </Link>
              </nav>

              {/* CONTENT */}

              <div className="sidebar-section-label mt-4">
                CONTENT
              </div>

              <nav className="sidebar-nav">

                <Link
                  href="/secure-panel/hero"
                  className="sidebar-link"
                  onClick={closeSidebar}
                >
                  <span className="sidebar-link-icon">
                    <i className="bi bi-stars"></i>
                  </span>

                  <span>Hero</span>
                </Link>

                <Link
                  href="/secure-panel/about"
                  className="sidebar-link"
                  onClick={closeSidebar}
                >
                  <span className="sidebar-link-icon">
                    <i className="bi bi-person-vcard"></i>
                  </span>

                  <span>About</span>
                </Link>

                <Link
  href="/secure-panel/about/stats"
  className="sidebar-link"
  onClick={closeSidebar}
>
  <span className="sidebar-link-icon">
    <i className="bi bi-bar-chart"></i>
  </span>

  <span>About Stats</span>
</Link>

                <Link
                  href="/secure-panel/education"
                  className="sidebar-link active"
                  onClick={closeSidebar}
                >
                  <span className="sidebar-link-icon">
                    <i className="bi bi-mortarboard"></i>
                  </span>

                  <span>Education</span>
                </Link>

                <Link
                  href="/secure-panel/skills"
                  className="sidebar-link"
                  onClick={closeSidebar}
                >
                  <span className="sidebar-link-icon">
                    <i className="bi bi-tools"></i>
                  </span>

                  <span>Skills</span>
                </Link>

                <Link
                  href="/secure-panel/experience"
                  className="sidebar-link"
                  onClick={closeSidebar}
                >
                  <span className="sidebar-link-icon">
                    <i className="bi bi-briefcase"></i>
                  </span>

                  <span>Experience</span>
                </Link>

                <Link
                  href="/secure-panel/projects"
                  className="sidebar-link"
                  onClick={closeSidebar}
                >
                  <span className="sidebar-link-icon">
                    <i className="bi bi-code-square"></i>
                  </span>

                  <span>Projects</span>
                </Link>

                <Link
                  href="/secure-panel/certifications"
                  className="sidebar-link"
                  onClick={closeSidebar}
                >
                  <span className="sidebar-link-icon">
                    <i className="bi bi-patch-check"></i>
                  </span>

                  <span>Certification</span>
                </Link>

                <Link
                  href="/secure-panel/achievements"
                  className="sidebar-link"
                  onClick={closeSidebar}
                >
                  <span className="sidebar-link-icon">
                    <i className="bi bi-trophy"></i>
                  </span>

                  <span>Achievement</span>
                </Link>

                <Link
                  href="/secure-panel/services"
                  className="sidebar-link"
                  onClick={closeSidebar}
                >
                  <span className="sidebar-link-icon">
                    <i className="bi bi-gear-wide-connected"></i>
                  </span>

                  <span>Services</span>
                </Link>

                <Link
                  href="/secure-panel/gallery"
                  className="sidebar-link"
                  onClick={closeSidebar}
                >
                  <span className="sidebar-link-icon">
                    <i className="bi bi-images"></i>
                  </span>

                  <span>Gallery</span>
                </Link>

                <Link
                  href="/secure-panel/contact-info"
                  className="sidebar-link"
                  onClick={closeSidebar}
                >
                  <span className="sidebar-link-icon">
                    <i className="bi bi-person-lines-fill"></i>
                  </span>

                  <span>Contact</span>
                </Link>

                <Link
  href="/secure-panel/contact-info/list"
  className="sidebar-link"
  onClick={closeSidebar}
>
  <span className="sidebar-link-icon">
    <i className="bi bi-share-fill"></i>
  </span>

  <span>Contact Social Media</span>
</Link>

                <Link
                  href="/secure-panel/articles"
                  className="sidebar-link"
                  onClick={closeSidebar}
                >
                  <span className="sidebar-link-icon">
                    <i className="bi bi-journal-text"></i>
                  </span>

                  <span>Articles</span>
                </Link>

                <Link
  href="/secure-panel/contact-messages"
  className="sidebar-link"
  onClick={closeSidebar}
>
  <span className="sidebar-link-icon">
    <i className="bi bi-chat-left-text"></i>
  </span>

  <span>Contact Messages</span>
</Link>

<Link
  href="/secure-panel/contact-users"
  className="sidebar-link"
  onClick={closeSidebar}
>
  <span className="sidebar-link-icon">
    <i className="bi bi-people-fill"></i>
  </span>

  <span>Contact User</span>
</Link>

              </nav>

              {/* SIDEBAR BOTTOM */}

              <div className="sidebar-bottom">
                <Link href="/">
                  <i className="bi bi-globe2"></i>

                  <span>View Website</span>

                  <i className="bi bi-arrow-up-right ms-auto"></i>
                </Link>
              </div>

            </div>
          </aside>

          {/* ==================================================
              MAIN
          ================================================== */}

          <div className="admin-main">

            {/* ==================================================
                TOPBAR
            ================================================== */}

            <header className="admin-topbar">

              <div className="topbar-left">

                <button
                  type="button"
                  className="mobile-menu-button d-lg-none"
                  onClick={() => setSidebarOpen(true)}
                  aria-label="Open menu"
                >
                  <i className="bi bi-list"></i>
                </button>

                <div>
                  <div className="topbar-label">
                    ADMIN PANEL
                  </div>

                  <div className="topbar-title">
                    Education Management
                  </div>
                </div>

              </div>

              <div className="topbar-right">

                <Link
                  href="/"
                  className="topbar-website"
                  title="View Website"
                >
                  <i className="bi bi-globe2"></i>

                  <span className="d-none d-sm-inline">
                    Website
                  </span>
                </Link>

                <Link
                  href="/secure-panel/login"
                  className="topbar-logout"
                  title="Logout"
                >
                  <i className="bi bi-box-arrow-right"></i>

                  <span className="d-none d-sm-inline">
                    Logout
                  </span>
                </Link>

              </div>

            </header>

            {/* ==================================================
                PAGE CONTENT
            ================================================== */}

            <main className="admin-content">

              {/* ==================================================
                  PAGE HEADER
              ================================================== */}

              <section className="page-heading">

                <div className="page-heading-left">

                  <Link
                    href="/secure-panel"
                    className="back-dashboard"
                  >
                    <i className="bi bi-arrow-left"></i>

                    Kembali ke Dashboard
                  </Link>

                  <div className="page-title-row">

                    <div className="page-title-icon">
                      <i className="bi bi-mortarboard-fill"></i>
                    </div>

                    <div>

                      <div className="page-eyebrow">
                        WEBSITE CONTENT
                      </div>

                      <h1 className="page-title">
                        Education
                      </h1>

                      <p className="page-description">
                        Kelola riwayat pendidikan dan latar
                        belakang akademik yang ditampilkan
                        pada website portfolio GloVaTech.
                      </p>

                    </div>

                  </div>

                </div>

                <div className="page-heading-action">

                  <Link
                    href="/secure-panel/education/create"
                    className="add-button"
                  >
                    <i className="bi bi-plus-lg"></i>

                    <span>Add Education</span>
                  </Link>

                </div>

              </section>

              {/* ==================================================
                  STATISTICS
              ================================================== */}

              <section className="stats-grid">

                <div className="stat-card">

                  <div className="stat-icon green">
                    <i className="bi bi-collection"></i>
                  </div>

                  <div>
                    <div className="stat-label">
                      TOTAL EDUCATION
                    </div>

                    <div className="stat-value">
                      {loading ? "—" : totalEducation}
                    </div>
                  </div>

                </div>

                <div className="stat-card">

                  <div className="stat-icon blue">
                    <i className="bi bi-check-circle"></i>
                  </div>

                  <div>
                    <div className="stat-label">
                      COMPLETED
                    </div>

                    <div className="stat-value">
                      {loading ? "—" : completedEducation}
                    </div>
                  </div>

                </div>

                <div className="stat-card">

                  <div className="stat-icon green">
                    <i className="bi bi-hourglass-split"></i>
                  </div>

                  <div>
                    <div className="stat-label">
                      IN PROGRESS
                    </div>

                    <div className="stat-value">
                      {loading ? "—" : currentEducation}
                    </div>
                  </div>

                </div>

                <div className="stat-card">

                  <div className="stat-icon dark">
                    <i className="bi bi-database-check"></i>
                  </div>

                  <div>
                    <div className="stat-label">
                      CONTENT STATUS
                    </div>

                    <div className="stat-value">
                      {loading ? "—" : "Ready"}
                    </div>
                  </div>

                </div>

              </section>

              {/* ==================================================
                  CONTENT MANAGEMENT
              ================================================== */}

              <section className="content-card">

                <div className="content-card-header">

                  <div>

                    <div className="content-card-eyebrow">
                      CONTENT MANAGEMENT
                    </div>

                    <h2 className="content-card-title">
                      Academic History
                    </h2>

                    <p className="content-card-description">
                      Daftar pendidikan yang tersimpan
                      pada database website.
                    </p>

                  </div>

                  <div className="content-card-count">

                    <i className="bi bi-mortarboard"></i>

                    <span>
                      {loading
                        ? "..."
                        : `${totalEducation} ${
                            totalEducation === 1
                              ? "Record"
                              : "Records"
                          }`}
                    </span>

                  </div>

                </div>

                {/* ==================================================
                    LOADING
                ================================================== */}

                {loading ? (

                  <div className="loading-state">

                    <div className="loading-spinner"></div>

                    <h5>
                      Memuat data Education
                    </h5>

                    <p>
                      Sedang mengambil informasi dari
                      database...
                    </p>

                  </div>

                ) : educations.length === 0 ? (

                  /* ==================================================
                     EMPTY
                  ================================================== */

                  <div className="empty-state">

                    <div className="empty-icon">
                      <i className="bi bi-mortarboard"></i>
                    </div>

                    <h4>
                      No Education Data
                    </h4>

                    <p>
                      Belum ada data pendidikan.
                      Tambahkan riwayat pendidikan
                      pertama kamu.
                    </p>

                    <Link
                      href="/secure-panel/education/create"
                      className="add-button"
                    >
                      <i className="bi bi-plus-lg"></i>

                      Add Education
                    </Link>

                  </div>

                ) : (

                  <>
                    {/* ==================================================
                        DESKTOP TABLE
                    ================================================== */}

                    <div className="education-table-wrapper">

                      <table className="education-table">

                        <thead>

                          <tr>

                            <th>
                              Education
                            </th>

                            <th>
                              Field of Study
                            </th>

                            <th>
                              Period
                            </th>

                            <th>
                              Description
                            </th>

                            <th className="text-end">
                              Action
                            </th>

                          </tr>

                        </thead>

                        <tbody>

                          {educations.map(
                            (education) => (

                              <tr
                                key={education.id}
                              >

                                {/* EDUCATION */}

                                <td>

                                  <div className="education-main">

                                    <div className="education-icon">
                                      <i className="bi bi-mortarboard-fill"></i>
                                    </div>

                                    <div>

                                      <div className="education-degree">
                                        {getDegreeLabel(
                                          education.degree
                                        )}
                                      </div>

                                      <div className="education-institution">
                                        {education.institution}
                                      </div>

                                    </div>

                                  </div>

                                </td>

                                {/* FIELD */}

                                <td>

                                  <div className="field-text">
                                    {education.field || "-"}
                                  </div>

                                </td>

                                {/* PERIOD */}

                                <td>

                                  <div className="period-wrapper">

                                    <i className="bi bi-calendar3"></i>

                                    <span>

                                      {formatYear(
                                        education.startDate
                                      )}

                                      {" - "}

                                      {education.endDate
                                        ? formatYear(
                                            education.endDate
                                          )
                                        : "Present"}

                                    </span>

                                  </div>

                                </td>

                                {/* DESCRIPTION */}

                                <td className="description-cell">

                                  <div className="description-text">

                                    {education.description
                                      ? education.description
                                          .length > 90
                                        ? `${education.description.substring(
                                            0,
                                            90
                                          )}...`
                                        : education.description
                                      : "No description"}

                                  </div>

                                </td>

                                {/* ACTION */}

                                <td>

                                  <div className="action-group">

                                    <Link
                                      href={`/secure-panel/education/${education.id}/edit`}
                                      className="action-button edit-button"
                                    >
                                      <i className="bi bi-pencil"></i>

                                      <span>
                                        Edit
                                      </span>
                                    </Link>

                                    <button
                                      type="button"
                                      className="action-button delete-button"
                                      disabled={
                                        deletingId ===
                                        education.id
                                      }
                                      onClick={() =>
                                        deleteEducation(
                                          education.id
                                        )
                                      }
                                    >

                                      {deletingId ===
                                      education.id ? (

                                        <>

                                          <span
                                            className="spinner-border spinner-border-sm"
                                            style={{
                                              width: "11px",
                                              height: "11px",
                                              borderWidth: "2px",
                                            }}
                                          ></span>

                                          <span>
                                            Deleting...
                                          </span>

                                        </>

                                      ) : (

                                        <>

                                          <i className="bi bi-trash3"></i>

                                          <span>
                                            Delete
                                          </span>

                                        </>

                                      )}

                                    </button>

                                  </div>

                                </td>

                              </tr>

                            )
                          )}

                        </tbody>

                      </table>

                    </div>

                    {/* ==================================================
                        MOBILE CARDS
                    ================================================== */}

                    <div className="education-mobile-list">

                      {educations.map(
                        (education) => (

                          <article
                            className="education-mobile-card"
                            key={`mobile-${education.id}`}
                          >

                            <div className="mobile-education-header">

                              <div className="education-icon">
                                <i className="bi bi-mortarboard-fill"></i>
                              </div>

                              <div className="mobile-education-info">

                                <div className="mobile-education-degree">
                                  {getDegreeLabel(
                                    education.degree
                                  )}
                                </div>

                                <div className="mobile-education-institution">
                                  {education.institution}
                                </div>

                              </div>

                            </div>

                            <div className="mobile-education-details">

                              {/* FIELD */}

                              <div className="mobile-detail">

                                <div className="mobile-detail-label">

                                  <i className="bi bi-book"></i>

                                  Field of Study

                                </div>

                                <div className="mobile-detail-value">
                                  {education.field || "-"}
                                </div>

                              </div>

                              {/* PERIOD */}

                              <div className="mobile-detail">

                                <div className="mobile-detail-label">

                                  <i className="bi bi-calendar3"></i>

                                  Period

                                </div>

                                <div className="mobile-detail-value">

                                  {formatYear(
                                    education.startDate
                                  )}

                                  {" - "}

                                  {education.endDate
                                    ? formatYear(
                                        education.endDate
                                      )
                                    : "Present"}

                                </div>

                              </div>

                              {/* DESCRIPTION */}

                              <div className="mobile-detail">

                                <div className="mobile-detail-label">

                                  <i className="bi bi-text-paragraph"></i>

                                  Description

                                </div>

                                <div className="mobile-description">

                                  {education.description
                                    ? education.description
                                        .length > 180
                                      ? `${education.description.substring(
                                          0,
                                          180
                                        )}...`
                                      : education.description
                                    : "No description"}

                                </div>

                              </div>

                            </div>

                            {/* ACTIONS */}

                            <div className="mobile-actions">

                              <Link
                                href={`/secure-panel/education/${education.id}/edit`}
                                className="action-button edit-button"
                              >
                                <i className="bi bi-pencil"></i>

                                <span>
                                  Edit
                                </span>
                              </Link>

                              <button
                                type="button"
                                className="action-button delete-button"
                                disabled={
                                  deletingId ===
                                  education.id
                                }
                                onClick={() =>
                                  deleteEducation(
                                    education.id
                                  )
                                }
                              >

                                {deletingId ===
                                education.id ? (

                                  <>

                                    <span
                                      className="spinner-border spinner-border-sm"
                                      style={{
                                        width: "11px",
                                        height: "11px",
                                        borderWidth: "2px",
                                      }}
                                    ></span>

                                    <span>
                                      Deleting...
                                    </span>

                                  </>

                                ) : (

                                  <>

                                    <i className="bi bi-trash3"></i>

                                    <span>
                                      Delete
                                    </span>

                                  </>

                                )}

                              </button>

                            </div>

                          </article>

                        )
                      )}

                    </div>
                  </>

                )}

              </section>

              {/* ==================================================
                  FOOTER
              ================================================== */}

              <footer className="admin-page-footer">

                <div>

                  <i className="bi bi-shield-check"></i>

                  <span>
                    Admin Content Management System
                  </span>

                </div>

                <Link href="/secure-panel">

                  <i className="bi bi-arrow-left me-1"></i>

                  Dashboard

                </Link>

              </footer>

            </main>

          </div>

        </div>
      </div>
    </>
  );
}