import { useState, useEffect, useCallback } from "react";
import { useAlert } from "../context/AlertContext";
import { useAuth } from "../context/AuthContext";
import { useNavigate, Navigate, useSearchParams } from "react-router-dom";
import { motion, AnimatePresence } from "framer-motion";
import ConfirmModal from "../components/ConfirmModal";
import FormField from "../components/FormField";
import useModalLock from "../hooks/useModalLock";
import CompanySettings from "./CompanySettings";

import apiFetch from "../utils/api";
const API_BASE = "/api/admin";

interface SystemSettingsData {
  break_threshold_hours: number;
  break_duration_short: number;
  break_duration_long: number;
  clock_rounding_minutes: number;
  invoice_alert_email: string;
  leave_notify_email: string;
  smtp_from: string;
  smtp_from_name: string;
  max_login_attempts: number;
  lockout_minutes: number;
  max_requests_per_minute: number;
  default_currency: string;
  default_vat_rate: number;
  available_vat_rates: number[];
  available_currencies: string[];
  quotation_prefix: string;
  order_type_code: string;
  invoice_type_code: string;
  offer_number_pattern: string;
  order_number_pattern: string;
  invoice_number_pattern: string;
  app_version: string;
}

const MODULE_LABELS: Record<string, string> = {
  attendance: "Docházka",
  trips: "Kniha jízd",
  offers: "Nabídky",
  orders: "Objednávky",
  projects: "Projekty",
  invoices: "Faktury",
  users: "Uživatelé",
  settings: "Nastavení",
};

interface Permission {
  id: number;
  name: string;
  display_name: string;
  description?: string;
}

interface Role {
  id: number;
  name: string;
  display_name: string;
  description: string | null;
  permissions: Permission[];
  role_permissions?: unknown[];
}

interface RoleForm {
  name: string;
  display_name: string;
  description: string;
  permissions: string[];
}

export default function Settings() {
  const alert = useAlert();
  const { hasPermission } = useAuth();
  const navigate = useNavigate();
  const [loading, setLoading] = useState(true);
  const [roles, setRoles] = useState<Role[]>([]);
  const [, setAllPermissions] = useState<Permission[]>([]);
  const [permissionGroups, setPermissionGroups] = useState<
    Record<string, Permission[]>
  >({});

  const [require2FA, setRequire2FA] = useState(false);
  const [require2FALoading, setRequire2FALoading] = useState(true);
  const [require2FASaving, setRequire2FASaving] = useState(false);

  const [showModal, setShowModal] = useState(false);
  const [editingRole, setEditingRole] = useState<Role | null>(null);
  const [saving, setSaving] = useState(false);
  const [form, setForm] = useState<RoleForm>({
    name: "",
    display_name: "",
    description: "",
    permissions: [],
  });

  const [deleteConfirm, setDeleteConfirm] = useState<{
    show: boolean;
    role: Role | null;
  }>({ show: false, role: null });
  const [deleting, setDeleting] = useState(false);

  const [searchParams, setSearchParams] = useSearchParams();
  const tabParam = searchParams.get("tab");
  const activeTab = (
    tabParam === "system"
      ? "system"
      : tabParam === "firma"
        ? "firma"
        : "security"
  ) as "security" | "system" | "firma";
  const setActiveTab = (tab: "security" | "system" | "firma") =>
    setSearchParams({ tab }, { replace: true });

  const [sysSettings, setSysSettings] = useState<SystemSettingsData | null>(
    null,
  );
  const [sysSettingsLoading, setSysSettingsLoading] = useState(false);
  const [sysSettingsSaving, setSysSettingsSaving] = useState(false);
  const [systemInfo, setSystemInfo] = useState<Record<string, any> | null>(
    null,
  );
  const [sysForm, setSysForm] = useState<
    Omit<SystemSettingsData, "app_version">
  >({
    break_threshold_hours: 6,
    break_duration_short: 15,
    break_duration_long: 30,
    clock_rounding_minutes: 15,
    invoice_alert_email: "",
    leave_notify_email: "",
    smtp_from: "",
    smtp_from_name: "",
    max_login_attempts: 5,
    lockout_minutes: 15,
    max_requests_per_minute: 300,
    default_currency: "CZK",
    default_vat_rate: 21,
    available_vat_rates: [0, 10, 12, 15, 21],
    available_currencies: ["CZK", "EUR", "USD", "GBP"],
    quotation_prefix: "NA",
    order_type_code: "71",
    invoice_type_code: "81",
    offer_number_pattern: "{YYYY}/{PREFIX}/{NNN}",
    order_number_pattern: "{YY}{CODE}{NNNN}",
    invoice_number_pattern: "{YY}{CODE}{NNNN}",
  });

  const canManage = hasPermission("settings.manage");

  if (!canManage) {
    return <Navigate to="/" replace />;
  }

  useModalLock(showModal);

  const fetchData = useCallback(async () => {
    if (!canManage) {
      setLoading(false);
      return;
    }
    try {
      const [rolesRes, permsRes] = await Promise.all([
        apiFetch(`${API_BASE}/roles`),
        apiFetch(`${API_BASE}/roles/permissions`),
      ]);
      const rolesResult = await rolesRes.json();
      const permsResult = await permsRes.json();

      if (rolesResult.success) {
        setRoles(Array.isArray(rolesResult.data) ? rolesResult.data : []);
      } else {
        alert.error(rolesResult.error || "Nepodařilo se načíst role");
      }

      if (permsResult.success) {
        const perms: Permission[] = Array.isArray(permsResult.data)
          ? permsResult.data
          : [];
        setAllPermissions(perms);
        // Group by module (part before '.')
        const groups: Record<string, Permission[]> = {};
        for (const p of perms) {
          const mod = p.name.split(".")[0] || "other";
          if (!groups[mod]) groups[mod] = [];
          groups[mod].push(p);
        }
        setPermissionGroups(groups);
      }
    } catch {
      alert.error("Chyba připojení");
    } finally {
      setLoading(false);
    }
  }, [alert, canManage]);

  useEffect(() => {
    fetchData();
  }, [fetchData]);

  const fetch2FARequired = useCallback(async () => {
    if (!canManage) {
      setRequire2FALoading(false);
      return;
    }
    try {
      const response = await apiFetch(`${API_BASE}/totp/required`);
      const result = await response.json();
      if (result.success) {
        setRequire2FA(result.data.require_2fa);
      }
    } catch {
      /* ignore */
    } finally {
      setRequire2FALoading(false);
    }
  }, [canManage]);

  useEffect(() => {
    fetch2FARequired();
  }, [fetch2FARequired]);

  const fetchSystemSettings = useCallback(async () => {
    if (!canManage) return;
    setSysSettingsLoading(true);
    try {
      const response = await apiFetch(`${API_BASE}/company-settings`);
      const result = await response.json();
      if (result.success) {
        const d: SystemSettingsData = result.data;
        setSysSettings(d);
        setSysForm({
          break_threshold_hours: d.break_threshold_hours ?? 6,
          break_duration_short: d.break_duration_short ?? 15,
          break_duration_long: d.break_duration_long ?? 30,
          clock_rounding_minutes: d.clock_rounding_minutes ?? 15,
          invoice_alert_email: d.invoice_alert_email || "",
          leave_notify_email: d.leave_notify_email || "",
          smtp_from: d.smtp_from || "",
          smtp_from_name: d.smtp_from_name || "",
          max_login_attempts: d.max_login_attempts ?? 5,
          lockout_minutes: d.lockout_minutes ?? 15,
          max_requests_per_minute: d.max_requests_per_minute ?? 300,
          default_currency: d.default_currency || "CZK",
          default_vat_rate: d.default_vat_rate ?? 21,
          available_vat_rates:
            Array.isArray(d.available_vat_rates) &&
            d.available_vat_rates.length > 0
              ? d.available_vat_rates
              : [0, 10, 12, 15, 21],
          available_currencies:
            Array.isArray(d.available_currencies) &&
            d.available_currencies.length > 0
              ? d.available_currencies
              : ["CZK", "EUR", "USD", "GBP"],
          quotation_prefix: d.quotation_prefix || "NA",
          order_type_code: d.order_type_code || "71",
          invoice_type_code: d.invoice_type_code || "81",
          offer_number_pattern:
            d.offer_number_pattern || "{YYYY}/{PREFIX}/{NNN}",
          order_number_pattern: d.order_number_pattern || "{YY}{CODE}{NNNN}",
          invoice_number_pattern:
            d.invoice_number_pattern || "{YY}{CODE}{NNNN}",
        });
      } else {
        alert.error(result.error || "Nepodařilo se načíst systémová nastavení");
      }
    } catch {
      alert.error("Chyba připojení");
    } finally {
      setSysSettingsLoading(false);
    }
    // Fetch system info
    apiFetch(`${API_BASE}/company-settings/system-info`)
      .then((r) => r.json())
      .then((d) => {
        if (d.success) setSystemInfo(d.data);
      })
      .catch(() => {});
  }, [alert, canManage]);

  useEffect(() => {
    fetchSystemSettings();
  }, [fetchSystemSettings]);

  const handleSaveSystemSettings = async () => {
    setSysSettingsSaving(true);
    try {
      const response = await apiFetch(`${API_BASE}/company-settings`, {
        method: "PUT",
        headers: { "Content-Type": "application/json" },
        body: JSON.stringify(sysForm),
      });
      const result = await response.json();
      if (result.success) {
        alert.success(result.message || "Systémová nastavení byla uložena");
        fetchSystemSettings();
      } else {
        alert.error(result.error || "Nepodařilo se uložit nastavení");
      }
    } catch {
      alert.error("Chyba připojení");
    } finally {
      setSysSettingsSaving(false);
    }
  };

  const handleToggle2FARequired = async () => {
    setRequire2FASaving(true);
    try {
      const response = await apiFetch(`${API_BASE}/totp/required`, {
        method: "POST",
        headers: { "Content-Type": "application/json" },
        body: JSON.stringify({ required: !require2FA }),
      });
      const result = await response.json();
      if (result.success) {
        setRequire2FA(!require2FA);
        alert.success(result.message || "2FA nastavení uloženo");
      } else {
        alert.error(result.error || "Nepodařilo se uložit nastavení");
      }
    } catch {
      alert.error("Chyba připojení");
    } finally {
      setRequire2FASaving(false);
    }
  };

  const generateSlug = (text: string): string => {
    return text
      .toLowerCase()
      .normalize("NFD")
      .replace(/[\u0300-\u036f]/g, "")
      .replace(/[^a-z0-9]+/g, "-")
      .replace(/^-+|-+$/g, "");
  };

  const openCreateModal = () => {
    setEditingRole(null);
    setForm({ name: "", display_name: "", description: "", permissions: [] });
    setShowModal(true);
  };

  const openEditModal = (role: Role) => {
    setEditingRole(role);
    setForm({
      name: role.name,
      display_name: role.display_name,
      description: role.description || "",
      permissions: (role.permissions || []).map((p) =>
        typeof p === "string" ? p : p.name,
      ),
    });
    setShowModal(true);
  };

  const closeModal = () => {
    setShowModal(false);
    setEditingRole(null);
  };

  const handleDisplayNameChange = (value: string) => {
    const updates: Partial<RoleForm> = { display_name: value };
    if (!editingRole) {
      updates.name = generateSlug(value);
    }
    setForm((prev) => ({ ...prev, ...updates }));
  };

  const togglePermission = (permName: string) => {
    setForm((prev) => ({
      ...prev,
      permissions: prev.permissions.includes(permName)
        ? prev.permissions.filter((p) => p !== permName)
        : [...prev.permissions, permName],
    }));
  };

  const toggleModulePermissions = (moduleName: string) => {
    const modulePerms = (permissionGroups[moduleName] || []).map((p) => p.name);
    const allChecked = modulePerms.every((p) => form.permissions.includes(p));

    setForm((prev) => ({
      ...prev,
      permissions: allChecked
        ? prev.permissions.filter((p) => !modulePerms.includes(p))
        : [...new Set([...prev.permissions, ...modulePerms])],
    }));
  };

  const handleSubmit = async (e?: React.FormEvent) => {
    e?.preventDefault();

    if (!form.display_name.trim()) {
      alert.error("Zobrazovaný název je povinný");
      return;
    }

    if (!editingRole && !form.name.trim()) {
      alert.error("Název role je povinný");
      return;
    }

    setSaving(true);
    try {
      const url = editingRole
        ? `${API_BASE}/roles/${editingRole.id}`
        : `${API_BASE}/roles`;

      const response = await apiFetch(url, {
        method: editingRole ? "PUT" : "POST",
        headers: { "Content-Type": "application/json" },
        body: JSON.stringify({
          ...form,
          permission_ids: form.permissions
            .map((name) => {
              // Find permission ID by name from groups
              for (const perms of Object.values(permissionGroups)) {
                const found = perms.find((p) => p.name === name);
                if (found) return found.id;
              }
              return null;
            })
            .filter(Boolean),
        }),
      });

      const result = await response.json();

      if (result.success) {
        closeModal();
        await new Promise((resolve) => setTimeout(resolve, 300));
        alert.success(
          result.message ||
            (editingRole ? "Role byla aktualizována" : "Role byla vytvořena"),
        );
        fetchData();
      } else {
        alert.error(result.error || "Nepodařilo se uložit roli");
      }
    } catch {
      alert.error("Chyba připojení");
    } finally {
      setSaving(false);
    }
  };

  const handleDelete = async () => {
    if (!deleteConfirm.role) return;

    setDeleting(true);
    try {
      const response = await apiFetch(
        `${API_BASE}/roles/${deleteConfirm.role.id}`,
        {
          method: "DELETE",
        },
      );

      const result = await response.json();

      if (result.success) {
        setDeleteConfirm({ show: false, role: null });
        alert.success(result.message || "Role byla smazána");
        fetchData();
      } else {
        alert.error(result.error || "Nepodařilo se smazat roli");
      }
    } catch {
      alert.error("Chyba připojení");
    } finally {
      setDeleting(false);
    }
  };

  if (loading) {
    return (
      <div className="admin-skeleton" style={{ padding: 0, gap: "1.5rem" }}>
        <div
          className="admin-skeleton-row"
          style={{ justifyContent: "space-between" }}
        >
          <div>
            <div
              className="admin-skeleton-line h-8"
              style={{ width: "200px", marginBottom: "0.5rem" }}
            />
            <div className="admin-skeleton-line" style={{ width: "140px" }} />
          </div>
        </div>
        <div className="admin-card">
          <div className="admin-skeleton" style={{ gap: "1.25rem" }}>
            {[0, 1, 2, 3, 4].map((i) => (
              <div key={i} className="admin-skeleton-row">
                <div className="admin-skeleton-line circle" />
                <div className="flex-1">
                  <div className="admin-skeleton-line w-1/3 mb-2" />
                  <div
                    className="admin-skeleton-line w-1/4"
                    style={{ height: "10px" }}
                  />
                </div>
                <div className="admin-skeleton-line w-1/4" />
              </div>
            ))}
          </div>
        </div>
      </div>
    );
  }

  const isAdminRole = (role: Role) => role.name === "admin";

  const get2FADescription = (): React.ReactNode => {
    if (require2FALoading) {
      return (
        <div
          className="admin-skeleton-line"
          style={{ width: "200px", height: "12px" }}
        />
      );
    }
    if (require2FA)
      return "Všichni uživatelé musí mít aktivní 2FA pro přístup do systému";
    return "2FA je volitelná - uživatelé si ji mohou aktivovat v profilu";
  };

  const get2FAButtonLabel = (): string => {
    if (require2FASaving) return "Ukládání...";
    return require2FA ? "Vypnout" : "Zapnout";
  };

  const renderRoleButtonContent = (): React.ReactNode => {
    if (saving) {
      return (
        <>
          <div className="admin-spinner admin-spinner-sm" />
          Ukládání...
        </>
      );
    }
    return editingRole ? "Uložit změny" : "Vytvořit roli";
  };

  return (
    <div>
      <motion.div
        className="admin-page-header"
        initial={{ opacity: 0, y: 12 }}
        animate={{ opacity: 1, y: 0 }}
        transition={{ duration: 0.25 }}
      >
        <div>
          <h1 className="admin-page-title">Nastavení</h1>
          <p className="admin-page-subtitle">
            {activeTab === "system"
              ? "Systémová nastavení"
              : activeTab === "firma"
                ? "Informace o firmě"
                : "Zabezpečení a správa rolí"}
          </p>
        </div>
      </motion.div>

      {canManage && (
        <div style={{ display: "flex", gap: "0.5rem", marginBottom: "1.5rem" }}>
          <button
            className={`admin-btn admin-btn-sm ${activeTab === "security" ? "admin-btn-primary" : "admin-btn-secondary"}`}
            onClick={() => setActiveTab("security")}
          >
            Zabezpečení a role
          </button>
          <button
            className={`admin-btn admin-btn-sm ${activeTab === "system" ? "admin-btn-primary" : "admin-btn-secondary"}`}
            onClick={() => setActiveTab("system")}
          >
            Systémová nastavení
          </button>
          <button
            className={`admin-btn admin-btn-sm ${activeTab === "firma" ? "admin-btn-primary" : "admin-btn-secondary"}`}
            onClick={() => setActiveTab("firma")}
          >
            Firma
          </button>
        </div>
      )}

      {/* Security Settings */}
      {activeTab === "security" && canManage && (
        <motion.div
          className="admin-card"
          initial={{ opacity: 0, y: 12 }}
          animate={{ opacity: 1, y: 0 }}
          transition={{ duration: 0.25, delay: 0.06 }}
        >
          <div className="admin-card-header">
            <h2 className="admin-card-title">Zabezpečení</h2>
          </div>
          <div className="admin-card-body">
            <div
              style={{
                display: "flex",
                alignItems: "center",
                justifyContent: "space-between",
                gap: "1rem",
              }}
            >
              <div className="flex-row-gap">
                <div
                  style={{
                    width: 36,
                    height: 36,
                    borderRadius: "50%",
                    display: "flex",
                    alignItems: "center",
                    justifyContent: "center",
                    background: require2FA
                      ? "var(--success-light)"
                      : "rgba(var(--text-secondary-rgb, 107, 114, 128), 0.1)",
                    color: require2FA
                      ? "var(--success)"
                      : "var(--text-secondary)",
                    flexShrink: 0,
                  }}
                >
                  <svg
                    width="18"
                    height="18"
                    viewBox="0 0 24 24"
                    fill="none"
                    stroke="currentColor"
                    strokeWidth="2"
                  >
                    <rect x="3" y="11" width="18" height="11" rx="2" ry="2" />
                    <path d="M7 11V7a5 5 0 0 1 10 0v4" />
                  </svg>
                </div>
                <div>
                  <div
                    className="fw-500 text-md"
                    style={{
                      color: "var(--text-primary)",
                    }}
                  >
                    Povinné dvoufaktorové ověření (2FA)
                  </div>
                  <div
                    className="text-xs"
                    style={{
                      color: "var(--text-secondary)",
                    }}
                  >
                    {get2FADescription()}
                  </div>
                </div>
              </div>
              {!require2FALoading && (
                <button
                  onClick={handleToggle2FARequired}
                  disabled={require2FASaving}
                  className={`admin-btn admin-btn-sm ${require2FA ? "admin-btn-secondary" : "admin-btn-primary"}`}
                  style={require2FA ? { color: "var(--danger)" } : {}}
                >
                  {get2FAButtonLabel()}
                </button>
              )}
            </div>
          </div>
        </motion.div>
      )}

      {/* Login Security */}
      {activeTab === "security" && canManage && (
        <motion.div
          className="admin-card"
          initial={{ opacity: 0, y: 12 }}
          animate={{ opacity: 1, y: 0 }}
          transition={{ duration: 0.25, delay: 0.08 }}
        >
          <div className="admin-card-header">
            <h2 className="admin-card-title">Přihlašování</h2>
          </div>
          <div className="admin-card-body">
            <div className="admin-form">
              <div className="admin-form-row">
                <FormField label="Max. pokusů o přihlášení">
                  <input
                    type="number"
                    value={sysForm.max_login_attempts}
                    onChange={(e) =>
                      setSysForm((prev) => ({
                        ...prev,
                        max_login_attempts: Number(e.target.value),
                      }))
                    }
                    className="admin-form-input"
                    min={1}
                  />
                </FormField>
                <FormField label="Doba uzamčení účtu (minuty)">
                  <input
                    type="number"
                    value={sysForm.lockout_minutes}
                    onChange={(e) =>
                      setSysForm((prev) => ({
                        ...prev,
                        lockout_minutes: Number(e.target.value),
                      }))
                    }
                    className="admin-form-input"
                    min={1}
                  />
                </FormField>
              </div>
              <div style={{ marginTop: "0.75rem" }}>
                <button
                  onClick={handleSaveSystemSettings}
                  className="admin-btn admin-btn-primary admin-btn-sm"
                  disabled={sysSettingsSaving}
                >
                  {sysSettingsSaving ? "Ukládání..." : "Uložit"}
                </button>
              </div>
            </div>
          </div>
        </motion.div>
      )}

      {/* Roles Table */}
      {activeTab === "security" && canManage && (
        <motion.div
          className="admin-card"
          initial={{ opacity: 0, y: 12 }}
          animate={{ opacity: 1, y: 0 }}
          transition={{ duration: 0.25, delay: 0.12 }}
        >
          <div
            className="admin-card-header"
            style={{
              display: "flex",
              justifyContent: "space-between",
              alignItems: "center",
            }}
          >
            <h2 className="admin-card-title">Role</h2>
            <button
              onClick={openCreateModal}
              className="admin-btn admin-btn-primary admin-btn-sm"
            >
              <svg
                width="16"
                height="16"
                viewBox="0 0 24 24"
                fill="none"
                stroke="currentColor"
                strokeWidth="2"
              >
                <line x1="12" y1="5" x2="12" y2="19" />
                <line x1="5" y1="12" x2="19" y2="12" />
              </svg>
              Přidat roli
            </button>
          </div>
          <div className="admin-card-body">
            <div className="admin-table-responsive">
              <table className="admin-table">
                <thead>
                  <tr>
                    <th>Název</th>
                    <th>Popis</th>
                    <th>Oprávnění</th>
                    <th>Uživatelé</th>
                    <th>Akce</th>
                  </tr>
                </thead>
                <tbody>
                  {roles.map((role) => (
                    <tr key={role.id}>
                      <td>
                        <div
                          className="fw-500"
                          style={{
                            color: "var(--text-primary)",
                          }}
                        >
                          {role.display_name}
                        </div>
                        <div
                          className="text-xs"
                          style={{
                            color: "var(--text-tertiary)",
                          }}
                        >
                          {role.name}
                        </div>
                      </td>
                      <td style={{ color: "var(--text-secondary)" }}>
                        {role.description || "\u2014"}
                      </td>
                      <td>
                        <span className="admin-badge admin-badge-info">
                          {isAdminRole(role)
                            ? "Vše"
                            : (role.permissions?.length ?? 0)}
                        </span>
                      </td>
                      <td>
                        <span className="admin-badge admin-badge-secondary">
                          {0}
                        </span>
                      </td>
                      <td>
                        {!isAdminRole(role) && (
                          <div className="flex-row gap-2">
                            <button
                              onClick={() => openEditModal(role)}
                              className="admin-btn-icon"
                              title="Upravit"
                              aria-label="Upravit"
                            >
                              <svg
                                width="16"
                                height="16"
                                viewBox="0 0 24 24"
                                fill="none"
                                stroke="currentColor"
                                strokeWidth="2"
                              >
                                <path d="M11 4H4a2 2 0 0 0-2 2v14a2 2 0 0 0 2 2h14a2 2 0 0 0 2-2v-7" />
                                <path d="M18.5 2.5a2.121 2.121 0 0 1 3 3L12 15l-4 1 1-4 9.5-9.5z" />
                              </svg>
                            </button>
                            <button
                              onClick={() =>
                                setDeleteConfirm({ show: true, role })
                              }
                              className="admin-btn-icon danger"
                              title={
                                0 > 0
                                  ? "Nelze smazat roli s přiřazenými uživateli"
                                  : "Smazat"
                              }
                              aria-label={
                                0 > 0
                                  ? "Nelze smazat roli s přiřazenými uživateli"
                                  : "Smazat"
                              }
                              disabled={0 > 0}
                            >
                              <svg
                                width="16"
                                height="16"
                                viewBox="0 0 24 24"
                                fill="none"
                                stroke="currentColor"
                                strokeWidth="2"
                              >
                                <polyline points="3 6 5 6 21 6" />
                                <path d="M19 6v14a2 2 0 0 1-2 2H7a2 2 0 0 1-2-2V6m3 0V4a2 2 0 0 1 2-2h4a2 2 0 0 1 2 2v2" />
                              </svg>
                            </button>
                          </div>
                        )}
                      </td>
                    </tr>
                  ))}
                </tbody>
              </table>
            </div>
          </div>
        </motion.div>
      )}

      {/* System Settings Tab */}
      {activeTab === "system" && canManage && (
        <>
          {sysSettingsLoading ? (
            <div
              className="admin-skeleton"
              style={{ padding: 0, gap: "1.5rem" }}
            >
              {[0, 1, 2].map((i) => (
                <div key={i} className="admin-card">
                  <div className="admin-skeleton" style={{ gap: "1rem" }}>
                    <div className="admin-skeleton-line w-1/3 mb-2" />
                    <div className="admin-skeleton-line w-full" />
                    <div className="admin-skeleton-line w-full" />
                  </div>
                </div>
              ))}
            </div>
          ) : (
            <>
              {/* Section 1: Docházka */}
              <motion.div
                className="admin-card"
                initial={{ opacity: 0, y: 12 }}
                animate={{ opacity: 1, y: 0 }}
                transition={{ duration: 0.25, delay: 0.06 }}
              >
                <div className="admin-card-header">
                  <h2 className="admin-card-title">Docházka</h2>
                </div>
                <div className="admin-card-body">
                  <div className="admin-form">
                    <div className="admin-form-row">
                      <FormField label="Limit pro automatickou přestávku (hodiny)">
                        <input
                          type="number"
                          value={sysForm.break_threshold_hours}
                          onChange={(e) =>
                            setSysForm((prev) => ({
                              ...prev,
                              break_threshold_hours: Number(e.target.value),
                            }))
                          }
                          className="admin-form-input"
                          min={0}
                          step={0.5}
                        />
                      </FormField>
                      <FormField label="Krátká přestávka (minuty)">
                        <input
                          type="number"
                          value={sysForm.break_duration_short}
                          onChange={(e) =>
                            setSysForm((prev) => ({
                              ...prev,
                              break_duration_short: Number(e.target.value),
                            }))
                          }
                          className="admin-form-input"
                          min={0}
                        />
                      </FormField>
                    </div>
                    <div className="admin-form-row">
                      <FormField label="Dlouhá přestávka (minuty)">
                        <input
                          type="number"
                          value={sysForm.break_duration_long}
                          onChange={(e) =>
                            setSysForm((prev) => ({
                              ...prev,
                              break_duration_long: Number(e.target.value),
                            }))
                          }
                          className="admin-form-input"
                          min={0}
                        />
                      </FormField>
                      <FormField label="Zaokrouhlování času (minuty)">
                        <select
                          value={sysForm.clock_rounding_minutes}
                          onChange={(e) =>
                            setSysForm((prev) => ({
                              ...prev,
                              clock_rounding_minutes: Number(e.target.value),
                            }))
                          }
                          className="admin-form-input"
                        >
                          <option value={5}>5</option>
                          <option value={10}>10</option>
                          <option value={15}>15</option>
                          <option value={30}>30</option>
                        </select>
                      </FormField>
                    </div>
                  </div>
                </div>
              </motion.div>

              {/* Section 2: Emailové notifikace */}
              <motion.div
                className="admin-card"
                initial={{ opacity: 0, y: 12 }}
                animate={{ opacity: 1, y: 0 }}
                transition={{ duration: 0.25, delay: 0.12 }}
              >
                <div className="admin-card-header">
                  <h2 className="admin-card-title">Emailové notifikace</h2>
                </div>
                <div className="admin-card-body">
                  <div className="admin-form">
                    <div className="admin-form-row">
                      <FormField label="Odesílatel (email)">
                        <input
                          type="email"
                          value={sysForm.smtp_from}
                          onChange={(e) =>
                            setSysForm((prev) => ({
                              ...prev,
                              smtp_from: e.target.value,
                            }))
                          }
                          className="admin-form-input"
                          placeholder="noreply@firma.cz"
                        />
                      </FormField>
                      <FormField label="Odesílatel (jméno)">
                        <input
                          type="text"
                          value={sysForm.smtp_from_name}
                          onChange={(e) =>
                            setSysForm((prev) => ({
                              ...prev,
                              smtp_from_name: e.target.value,
                            }))
                          }
                          className="admin-form-input"
                          placeholder=""
                        />
                      </FormField>
                    </div>
                    <div className="admin-form-row">
                      <FormField label="Email pro upozornění na faktury">
                        <input
                          type="email"
                          value={sysForm.invoice_alert_email}
                          onChange={(e) =>
                            setSysForm((prev) => ({
                              ...prev,
                              invoice_alert_email: e.target.value,
                            }))
                          }
                          className="admin-form-input"
                          placeholder="fakturace@firma.cz"
                        />
                      </FormField>
                      <FormField label="Email pro notifikace dovolené">
                        <input
                          type="email"
                          value={sysForm.leave_notify_email}
                          onChange={(e) =>
                            setSysForm((prev) => ({
                              ...prev,
                              leave_notify_email: e.target.value,
                            }))
                          }
                          className="admin-form-input"
                          placeholder="hr@firma.cz"
                        />
                      </FormField>
                    </div>
                  </div>
                </div>
              </motion.div>

              {/* Section 4: Omezení požadavků */}
              <motion.div
                className="admin-card"
                initial={{ opacity: 0, y: 12 }}
                animate={{ opacity: 1, y: 0 }}
                transition={{ duration: 0.25, delay: 0.24 }}
              >
                <div className="admin-card-header">
                  <h2 className="admin-card-title">Omezení požadavků</h2>
                </div>
                <div className="admin-card-body">
                  <div className="admin-form">
                    <FormField label="Max. požadavků za minutu">
                      <input
                        type="number"
                        value={sysForm.max_requests_per_minute}
                        onChange={(e) =>
                          setSysForm((prev) => ({
                            ...prev,
                            max_requests_per_minute: Number(e.target.value),
                          }))
                        }
                        className="admin-form-input"
                        min={1}
                      />
                    </FormField>
                    <small
                      className="text-xs"
                      style={{
                        color: "var(--text-tertiary)",
                      }}
                    >
                      Změna se projeví po restartu serveru
                    </small>
                  </div>
                </div>
              </motion.div>

              {/* Section 5: Číslování dokladů */}
              <motion.div
                className="admin-card"
                initial={{ opacity: 0, y: 12 }}
                animate={{ opacity: 1, y: 0 }}
                transition={{ duration: 0.25, delay: 0.28 }}
              >
                <div className="admin-card-header">
                  <h2 className="admin-card-title">Číslování dokladů</h2>
                </div>
                <div className="admin-card-body">
                  <div className="admin-form">
                    <div
                      style={{
                        padding: "0.75rem",
                        background: "var(--bg-tertiary)",
                        borderRadius: 8,
                        fontSize: "0.8rem",
                        color: "var(--text-secondary)",
                        marginBottom: "1rem",
                      }}
                    >
                      <strong>Dostupné zástupné znaky:</strong>{" "}
                      <code>{"{YYYY}"}</code> rok, <code>{"{YY}"}</code> rok (2
                      číslice), <code>{"{PREFIX}"}</code> prefix nabídky,{" "}
                      <code>{"{CODE}"}</code> typový kód, <code>{"{NNN}"}</code>{" "}
                      pořadí (3 číslice), <code>{"{NNNN}"}</code> pořadí (4
                      číslice), <code>{"{NNNNN}"}</code> pořadí (5 číslic)
                    </div>

                    {[
                      {
                        label: "Nabídky",
                        patternKey: "offer_number_pattern" as const,
                        defaultPattern: "{YYYY}/{PREFIX}/{NNN}",
                        prefixKey: "quotation_prefix" as const,
                        prefixLabel: "Prefix",
                        codeKey: null,
                      },
                      {
                        label: "Objednávky a projekty",
                        patternKey: "order_number_pattern" as const,
                        defaultPattern: "{YY}{CODE}{NNNN}",
                        prefixKey: null,
                        codeKey: "order_type_code" as const,
                        codeLabel: "Typový kód",
                      },
                      {
                        label: "Faktury",
                        patternKey: "invoice_number_pattern" as const,
                        defaultPattern: "{YY}{CODE}{NNNN}",
                        prefixKey: null,
                        codeKey: "invoice_type_code" as const,
                        codeLabel: "Typový kód",
                      },
                    ].map((cfg, idx) => {
                      const pattern =
                        sysForm[cfg.patternKey] || cfg.defaultPattern;
                      const yyyy = String(new Date().getFullYear());
                      const yy = yyyy.slice(-2);
                      const prefix = cfg.prefixKey
                        ? sysForm[cfg.prefixKey] || "NA"
                        : "";
                      const code = cfg.codeKey
                        ? sysForm[cfg.codeKey] || ""
                        : "";
                      const preview = pattern.replace(
                        /\{(\w+)\}/g,
                        (m: string, k: string) => {
                          if (k === "YYYY") return yyyy;
                          if (k === "YY") return yy;
                          if (k === "PREFIX") return prefix;
                          if (k === "CODE") return code;
                          if (/^N+$/.test(k))
                            return "1".padStart(k.length, "0");
                          return m;
                        },
                      );

                      return (
                        <div key={cfg.patternKey}>
                          {idx > 0 && (
                            <hr
                              style={{
                                border: "none",
                                borderTop: "1px solid var(--border-color)",
                                margin: "1rem 0",
                              }}
                            />
                          )}
                          <div
                            className="fw-600 text-md"
                            style={{
                              marginBottom: "0.5rem",
                            }}
                          >
                            {cfg.label}
                          </div>
                          <div className="admin-form-row">
                            <FormField label="Formát">
                              <input
                                type="text"
                                value={sysForm[cfg.patternKey]}
                                onChange={(e) =>
                                  setSysForm((p) => ({
                                    ...p,
                                    [cfg.patternKey]: e.target.value,
                                  }))
                                }
                                className="admin-form-input"
                                placeholder={cfg.defaultPattern}
                              />
                            </FormField>
                            {cfg.prefixKey && (
                              <FormField label="Prefix">
                                <input
                                  type="text"
                                  value={sysForm[cfg.prefixKey]}
                                  onChange={(e) =>
                                    setSysForm((p) => ({
                                      ...p,
                                      [cfg.prefixKey!]: e.target.value,
                                    }))
                                  }
                                  className="admin-form-input"
                                  style={{ maxWidth: 100 }}
                                />
                              </FormField>
                            )}
                            {cfg.codeKey && (
                              <FormField label={cfg.codeLabel || "Kód"}>
                                <input
                                  type="text"
                                  value={sysForm[cfg.codeKey]}
                                  onChange={(e) =>
                                    setSysForm((p) => ({
                                      ...p,
                                      [cfg.codeKey!]: e.target.value,
                                    }))
                                  }
                                  className="admin-form-input"
                                  style={{ maxWidth: 100 }}
                                />
                              </FormField>
                            )}
                          </div>
                          <small
                            style={{
                              color: "var(--text-tertiary)",
                              fontSize: "0.8rem",
                            }}
                          >
                            Ukázka:{" "}
                            <strong style={{ color: "var(--text-primary)" }}>
                              {preview}
                            </strong>
                          </small>
                        </div>
                      );
                    })}
                  </div>
                </div>
              </motion.div>

              {/* Section 6: Měna a DPH */}
              <motion.div
                className="admin-card"
                initial={{ opacity: 0, y: 12 }}
                animate={{ opacity: 1, y: 0 }}
                transition={{ duration: 0.25, delay: 0.3 }}
              >
                <div className="admin-card-header">
                  <h2 className="admin-card-title">Měna a DPH</h2>
                </div>
                <div className="admin-card-body">
                  <div className="admin-form">
                    <div className="admin-form-row">
                      <FormField label="Výchozí měna">
                        <select
                          value={sysForm.default_currency}
                          onChange={(e) =>
                            setSysForm((prev) => ({
                              ...prev,
                              default_currency: e.target.value,
                            }))
                          }
                          className="admin-form-input"
                        >
                          {sysForm.available_currencies.map((c) => (
                            <option key={c} value={c}>
                              {c}
                            </option>
                          ))}
                        </select>
                      </FormField>
                      <FormField label="Výchozí sazba DPH (%)">
                        <input
                          type="number"
                          value={sysForm.default_vat_rate}
                          onChange={(e) =>
                            setSysForm((prev) => ({
                              ...prev,
                              default_vat_rate: Number(e.target.value),
                            }))
                          }
                          className="admin-form-input"
                          min={0}
                          step={1}
                        />
                      </FormField>
                    </div>
                    <div className="admin-form-row">
                      <FormField label="Dostupné měny">
                        <input
                          type="text"
                          value={sysForm.available_currencies.join(", ")}
                          onChange={(e) =>
                            setSysForm((prev) => ({
                              ...prev,
                              available_currencies: e.target.value
                                .split(",")
                                .map((s) => s.trim())
                                .filter(Boolean),
                            }))
                          }
                          className="admin-form-input"
                          placeholder="CZK, EUR, USD"
                        />
                      </FormField>
                      <FormField label="Dostupné sazby DPH (%)">
                        <input
                          type="text"
                          value={sysForm.available_vat_rates.join(", ")}
                          onChange={(e) =>
                            setSysForm((prev) => ({
                              ...prev,
                              available_vat_rates: e.target.value
                                .split(",")
                                .map((s) => s.trim())
                                .filter(Boolean)
                                .map(Number)
                                .filter((n) => !isNaN(n)),
                            }))
                          }
                          className="admin-form-input"
                          placeholder="0, 12, 21"
                        />
                      </FormField>
                    </div>
                  </div>
                </div>
              </motion.div>

              {/* Section 6: Informace o aplikaci */}
              <motion.div
                className="admin-card"
                initial={{ opacity: 0, y: 12 }}
                animate={{ opacity: 1, y: 0 }}
                transition={{ duration: 0.25, delay: 0.36 }}
              >
                <div className="admin-card-header">
                  <h2 className="admin-card-title">Informace o aplikaci</h2>
                </div>
                <div className="admin-card-body">
                  {systemInfo ? (
                    <table
                      className="w-full"
                      style={{
                        fontSize: "0.85rem",
                        borderCollapse: "collapse",
                      }}
                    >
                      <tbody>
                        {(
                          [
                            ["Verze", systemInfo.app_version],
                            ["Node.js", systemInfo.node_version],
                            ["Platforma", systemInfo.platform],
                            ["Uptime", systemInfo.uptime],
                            ["Prostředí", systemInfo.environment],
                            ["Časová zóna", systemInfo.timezone],
                          ] as [string, string][]
                        ).map(([label, val]) => (
                          <tr key={label}>
                            <td
                              className="whitespace-nowrap"
                              style={{
                                padding: "6px 12px 6px 0",
                                color: "var(--text-secondary)",
                                width: 160,
                              }}
                            >
                              {label}
                            </td>
                            <td className="fw-500" style={{ padding: "6px 0" }}>
                              {val}
                            </td>
                          </tr>
                        ))}
                        <tr>
                          <td
                            colSpan={2}
                            style={{
                              padding: "10px 0 4px",
                              fontWeight: 600,
                              fontSize: "0.8rem",
                              color: "var(--text-tertiary)",
                              textTransform: "uppercase",
                              letterSpacing: "0.05em",
                            }}
                          >
                            Paměť
                          </td>
                        </tr>
                        {(
                          [
                            ["Proces (RSS)", systemInfo.memory?.rss],
                            [
                              "Heap",
                              `${systemInfo.memory?.heap_used} / ${systemInfo.memory?.heap_total}`,
                            ],
                            [
                              "Systém",
                              `${systemInfo.memory?.system_free} volné z ${systemInfo.memory?.system_total}`,
                            ],
                          ] as [string, string][]
                        ).map(([label, val]) => (
                          <tr key={label}>
                            <td
                              style={{
                                padding: "4px 12px 4px 0",
                                color: "var(--text-secondary)",
                              }}
                            >
                              {label}
                            </td>
                            <td style={{ padding: "4px 0" }}>{val}</td>
                          </tr>
                        ))}
                        <tr>
                          <td
                            colSpan={2}
                            style={{
                              padding: "10px 0 4px",
                              fontWeight: 600,
                              fontSize: "0.8rem",
                              color: "var(--text-tertiary)",
                              textTransform: "uppercase",
                              letterSpacing: "0.05em",
                            }}
                          >
                            Databáze
                          </td>
                        </tr>
                        <tr>
                          <td
                            style={{
                              padding: "4px 12px 4px 0",
                              color: "var(--text-secondary)",
                            }}
                          >
                            Stav
                          </td>
                          <td style={{ padding: "4px 0" }}>
                            <span
                              className={`admin-badge ${systemInfo.database?.status === "ok" ? "admin-badge-success" : "admin-badge-danger"}`}
                            >
                              {systemInfo.database?.status === "ok"
                                ? "Připojeno"
                                : "Chyba"}
                            </span>
                          </td>
                        </tr>
                        <tr>
                          <td
                            style={{
                              padding: "4px 12px 4px 0",
                              color: "var(--text-secondary)",
                            }}
                          >
                            Migrace
                          </td>
                          <td style={{ padding: "4px 0" }}>
                            {systemInfo.database?.migrations_applied}
                          </td>
                        </tr>
                        <tr>
                          <td
                            colSpan={2}
                            style={{
                              padding: "10px 0 4px",
                              fontWeight: 600,
                              fontSize: "0.8rem",
                              color: "var(--text-tertiary)",
                              textTransform: "uppercase",
                              letterSpacing: "0.05em",
                            }}
                          >
                            NAS úložiště
                          </td>
                        </tr>
                        {(
                          [
                            ["Projekty", systemInfo.nas?.projects],
                            ["Finance", systemInfo.nas?.financials],
                            ["Nabídky", systemInfo.nas?.offers],
                          ] as [string, Record<string, any>][]
                        ).map(([label, info]) => (
                          <tr key={label}>
                            <td
                              style={{
                                padding: "4px 12px 4px 0",
                                color: "var(--text-secondary)",
                              }}
                            >
                              {label}
                            </td>
                            <td style={{ padding: "4px 0" }}>
                              <span
                                className={`admin-badge ${info?.configured ? "admin-badge-success" : "admin-badge-secondary"}`}
                              >
                                {info?.configured
                                  ? "Připojeno"
                                  : "Nenakonfigurováno"}
                              </span>
                              {info?.configured && (
                                <span
                                  className="text-xs"
                                  style={{
                                    marginLeft: 8,
                                    color: "var(--text-tertiary)",
                                  }}
                                >
                                  {info.path}
                                </span>
                              )}
                            </td>
                          </tr>
                        ))}
                      </tbody>
                    </table>
                  ) : (
                    <div
                      className="admin-skeleton-line"
                      style={{ width: "60%", height: 14 }}
                    />
                  )}
                </div>
              </motion.div>

              {/* Save button */}
              <motion.div
                initial={{ opacity: 0, y: 12 }}
                animate={{ opacity: 1, y: 0 }}
                transition={{ duration: 0.25, delay: 0.42 }}
              >
                <button
                  onClick={handleSaveSystemSettings}
                  disabled={sysSettingsSaving}
                  className="admin-btn admin-btn-primary w-full"
                >
                  {sysSettingsSaving ? (
                    <>
                      <div className="admin-spinner admin-spinner-sm" />
                      Ukládání...
                    </>
                  ) : (
                    "Uložit"
                  )}
                </button>
              </motion.div>
            </>
          )}
        </>
      )}

      {/* Firma tab */}
      {activeTab === "firma" && canManage && <CompanySettings embedded />}

      {/* Create/Edit Modal */}
      <AnimatePresence>
        {showModal && (
          <motion.div
            className="admin-modal-overlay"
            initial={{ opacity: 0 }}
            animate={{ opacity: 1 }}
            exit={{ opacity: 0 }}
            transition={{ duration: 0.2 }}
          >
            <div className="admin-modal-backdrop" onClick={closeModal} />
            <motion.div
              className="admin-modal admin-modal-lg"
              initial={{ opacity: 0, scale: 0.95, y: 20 }}
              animate={{ opacity: 1, scale: 1, y: 0 }}
              exit={{ opacity: 0, scale: 0.95, y: 20 }}
              transition={{ duration: 0.2 }}
            >
              <div className="admin-modal-header">
                <h2 className="admin-modal-title">
                  {editingRole ? "Upravit roli" : "Nová role"}
                </h2>
              </div>

              <div className="admin-modal-body">
                <div className="admin-form">
                  {editingRole && isAdminRole(editingRole) && (
                    <div className="admin-role-locked-notice">
                      <svg
                        width="16"
                        height="16"
                        viewBox="0 0 24 24"
                        fill="none"
                        stroke="currentColor"
                        strokeWidth="2"
                      >
                        <circle cx="12" cy="12" r="10" />
                        <line x1="12" y1="16" x2="12" y2="12" />
                        <line x1="12" y1="8" x2="12.01" y2="8" />
                      </svg>
                      Administrátor má vždy plný přístup ke všem funkcím
                    </div>
                  )}

                  <FormField label="Zobrazovaný název">
                    <input
                      type="text"
                      value={form.display_name}
                      onChange={(e) => handleDisplayNameChange(e.target.value)}
                      className="admin-form-input"
                      placeholder="např. Manažer"
                      disabled={!!(editingRole && isAdminRole(editingRole))}
                    />
                  </FormField>

                  <FormField label="Systémový název (slug)">
                    <input
                      type="text"
                      value={form.name}
                      onChange={(e) =>
                        setForm((prev) => ({ ...prev, name: e.target.value }))
                      }
                      className="admin-form-input"
                      placeholder="např. manager"
                      disabled={!!editingRole}
                    />
                    {!editingRole && (
                      <small
                        className="text-xs"
                        style={{
                          color: "var(--text-tertiary)",
                        }}
                      >
                        Pouze malá písmena, čísla a pomlčky. Nelze později
                        změnit.
                      </small>
                    )}
                  </FormField>

                  <FormField label="Popis">
                    <textarea
                      value={form.description}
                      onChange={(e) =>
                        setForm((prev) => ({
                          ...prev,
                          description: e.target.value,
                        }))
                      }
                      className="admin-form-input"
                      rows={2}
                      placeholder="Volitelný popis role"
                      disabled={!!(editingRole && isAdminRole(editingRole))}
                    />
                  </FormField>

                  <div className="admin-form-group">
                    <label
                      className="admin-form-label"
                      style={{ marginBottom: "0.75rem" }}
                    >
                      Oprávnění
                    </label>

                    {Object.entries(permissionGroups)
                      .sort(([a, aPerms], [b, bPerms]) => {
                        if (a === "settings") return 1;
                        if (b === "settings") return -1;
                        const aMin = Math.min(...aPerms.map((p) => p.id));
                        const bMin = Math.min(...bPerms.map((p) => p.id));
                        return aMin - bMin;
                      })
                      .map(([module, perms], index) => {
                        const modulePerms = perms.map((p) => p.name);
                        const allChecked = modulePerms.every((p) =>
                          form.permissions.includes(p),
                        );
                        const someChecked = modulePerms.some((p) =>
                          form.permissions.includes(p),
                        );
                        const disabled = !!(
                          editingRole && isAdminRole(editingRole)
                        );

                        return (
                          <div key={module}>
                            {index > 0 && (
                              <hr
                                style={{
                                  border: "none",
                                  borderTop:
                                    "1px solid var(--border-color, #e0e0e0)",
                                  margin: "0.75rem 0",
                                }}
                              />
                            )}
                            <div className="admin-permission-group">
                              <div className="admin-permission-group-title">
                                <label className="admin-form-checkbox">
                                  <input
                                    type="checkbox"
                                    checked={allChecked}
                                    ref={(el) => {
                                      if (el)
                                        el.indeterminate =
                                          someChecked && !allChecked;
                                    }}
                                    onChange={() =>
                                      toggleModulePermissions(module)
                                    }
                                    disabled={disabled}
                                  />
                                  <span>{MODULE_LABELS[module] || module}</span>
                                </label>
                              </div>
                              <div className="admin-permission-list">
                                {perms.map((perm) => (
                                  <div
                                    key={perm.id}
                                    className="admin-permission-item"
                                  >
                                    <label className="admin-form-checkbox">
                                      <input
                                        type="checkbox"
                                        checked={form.permissions.includes(
                                          perm.name,
                                        )}
                                        onChange={() =>
                                          togglePermission(perm.name)
                                        }
                                        disabled={disabled}
                                      />
                                      <span>{perm.display_name}</span>
                                    </label>
                                    {perm.description && (
                                      <div className="admin-permission-desc">
                                        {perm.description}
                                      </div>
                                    )}
                                  </div>
                                ))}
                              </div>
                            </div>
                          </div>
                        );
                      })}
                  </div>
                </div>
              </div>

              <div className="admin-modal-footer">
                <button
                  type="button"
                  onClick={closeModal}
                  className="admin-btn admin-btn-secondary"
                  disabled={saving}
                >
                  Zrušit
                </button>
                {!(editingRole && isAdminRole(editingRole)) && (
                  <button
                    type="button"
                    onClick={handleSubmit}
                    className="admin-btn admin-btn-primary"
                    disabled={saving}
                  >
                    {renderRoleButtonContent()}
                  </button>
                )}
              </div>
            </motion.div>
          </motion.div>
        )}
      </AnimatePresence>

      {/* Delete Confirm Modal */}
      <ConfirmModal
        isOpen={deleteConfirm.show}
        onClose={() => setDeleteConfirm({ show: false, role: null })}
        onConfirm={handleDelete}
        title="Smazat roli"
        message={`Opravdu chcete smazat roli "${deleteConfirm.role?.display_name}"? Tato akce je nevratná.`}
        confirmText="Smazat"
        cancelText="Zrušit"
        type="danger"
        loading={deleting}
      />
    </div>
  );
}
